[mastodon-client] code cleanup, remove redundant function calls
This commit is contained in:
parent
a315c101d4
commit
c2e17fbaff
@ -3,7 +3,7 @@ import Router from "@koa/router";
|
|||||||
import { koaBody } from "koa-body";
|
import { koaBody } from "koa-body";
|
||||||
import { convertId, IdType } from "../../index.js";
|
import { convertId, IdType } from "../../index.js";
|
||||||
import { getClient } from "../ApiMastodonCompatibleService.js";
|
import { getClient } from "../ApiMastodonCompatibleService.js";
|
||||||
import { convertTimelinesArgsId, toTextWithReaction } from "./timeline.js";
|
import { convertTimelinesArgsId } from "./timeline.js";
|
||||||
import { convertNotification } from "../converters.js";
|
import { convertNotification } from "../converters.js";
|
||||||
function toLimitToInt(q: any) {
|
function toLimitToInt(q: any) {
|
||||||
if (q.limit) if (typeof q.limit === "string") q.limit = parseInt(q.limit, 10);
|
if (q.limit) if (typeof q.limit === "string") q.limit = parseInt(q.limit, 10);
|
||||||
@ -25,10 +25,6 @@ export function apiNotificationsMastodon(router: Router): void {
|
|||||||
n = convertNotification(n);
|
n = convertNotification(n);
|
||||||
if (n.type !== "follow" && n.type !== "follow_request") {
|
if (n.type !== "follow" && n.type !== "follow_request") {
|
||||||
if (n.type === "reaction") n.type = "favourite";
|
if (n.type === "reaction") n.type = "favourite";
|
||||||
n.status = toTextWithReaction(
|
|
||||||
n.status ? [n.status] : [],
|
|
||||||
ctx.hostname,
|
|
||||||
)[0];
|
|
||||||
return n;
|
return n;
|
||||||
} else {
|
} else {
|
||||||
return n;
|
return n;
|
||||||
@ -52,11 +48,13 @@ export function apiNotificationsMastodon(router: Router): void {
|
|||||||
convertId(ctx.params.id, IdType.CalckeyId),
|
convertId(ctx.params.id, IdType.CalckeyId),
|
||||||
);
|
);
|
||||||
const data = convertNotification(dataRaw.data);
|
const data = convertNotification(dataRaw.data);
|
||||||
if (data.type !== "follow" && data.type !== "follow_request") {
|
ctx.body = data;
|
||||||
if (data.type === "reaction") data.type = "favourite";
|
if (
|
||||||
ctx.body = toTextWithReaction([data as any], ctx.request.hostname)[0];
|
data.type !== "follow" &&
|
||||||
} else {
|
data.type !== "follow_request" &&
|
||||||
ctx.body = data;
|
data.type === "reaction"
|
||||||
|
) {
|
||||||
|
data.type = "favourite";
|
||||||
}
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
import Router from "@koa/router";
|
import Router from "@koa/router";
|
||||||
import megalodon, { Entity, MegalodonInterface } from "megalodon";
|
|
||||||
import { getClient } from "../ApiMastodonCompatibleService.js";
|
import { getClient } from "../ApiMastodonCompatibleService.js";
|
||||||
import { statusModel } from "./status.js";
|
|
||||||
import Autolinker from "autolinker";
|
|
||||||
import { ParsedUrlQuery } from "querystring";
|
import { ParsedUrlQuery } from "querystring";
|
||||||
import { convertAccount, convertList, convertStatus } from "../converters.js";
|
import { convertAccount, convertList, convertStatus } from "../converters.js";
|
||||||
import { convertId, IdType } from "../../index.js";
|
import { convertId, IdType } from "../../index.js";
|
||||||
@ -41,69 +38,6 @@ export function convertTimelinesArgsId(q: ParsedUrlQuery) {
|
|||||||
return q;
|
return q;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toTextWithReaction(status: Entity.Status[], host: string) {
|
|
||||||
return status.map((t) => {
|
|
||||||
if (!t) return statusModel(null, null, [], "no content");
|
|
||||||
t.quote = null as any;
|
|
||||||
// disabled for now
|
|
||||||
/*
|
|
||||||
if (!t.emoji_reactions) return t;
|
|
||||||
if (t.reblog) t.reblog = toTextWithReaction([t.reblog], host)[0];
|
|
||||||
const reactions = t.emoji_reactions.map((r) => {
|
|
||||||
const emojiNotation = r.url ? `:${r.name.replace("@.", "")}:` : r.name;
|
|
||||||
return `${emojiNotation} (${r.count}${r.me ? `* ` : ""})`;
|
|
||||||
});
|
|
||||||
const reaction = t.emoji_reactions as Entity.Reaction[];
|
|
||||||
const emoji = t.emojis || [];
|
|
||||||
for (const r of reaction) {
|
|
||||||
if (!r.url) continue;
|
|
||||||
emoji.push({
|
|
||||||
shortcode: r.name,
|
|
||||||
url: r.url,
|
|
||||||
static_url: r.url,
|
|
||||||
visible_in_picker: true,
|
|
||||||
category: "",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
const isMe = reaction.findIndex((r) => r.me) > -1;
|
|
||||||
const total = reaction.reduce((sum, reaction) => sum + reaction.count, 0);
|
|
||||||
t.favourited = isMe;
|
|
||||||
t.favourites_count = total;
|
|
||||||
t.emojis = emoji;
|
|
||||||
t.content = `<p>${autoLinker(t.content, host)}</p><p>${reactions.join(
|
|
||||||
", ",
|
|
||||||
)}</p>`;
|
|
||||||
*/
|
|
||||||
return t;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
export function autoLinker(input: string, host: string) {
|
|
||||||
return Autolinker.link(input, {
|
|
||||||
hashtag: "twitter",
|
|
||||||
mention: "twitter",
|
|
||||||
email: false,
|
|
||||||
stripPrefix: false,
|
|
||||||
replaceFn: function (match) {
|
|
||||||
switch (match.type) {
|
|
||||||
case "url":
|
|
||||||
return true;
|
|
||||||
case "mention":
|
|
||||||
console.log("Mention: ", match.getMention());
|
|
||||||
console.log("Mention Service Name: ", match.getServiceName());
|
|
||||||
return `<a href="https://${host}/@${encodeURIComponent(
|
|
||||||
match.getMention(),
|
|
||||||
)}" target="_blank">@${match.getMention()}</a>`;
|
|
||||||
case "hashtag":
|
|
||||||
console.log("Hashtag: ", match.getHashtag());
|
|
||||||
return `<a href="https://${host}/tags/${encodeURIComponent(
|
|
||||||
match.getHashtag(),
|
|
||||||
)}" target="_blank">#${match.getHashtag()}</a>`;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function apiTimelineMastodon(router: Router): void {
|
export function apiTimelineMastodon(router: Router): void {
|
||||||
router.get("/v1/timelines/public", async (ctx, reply) => {
|
router.get("/v1/timelines/public", async (ctx, reply) => {
|
||||||
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
||||||
@ -118,8 +52,7 @@ export function apiTimelineMastodon(router: Router): void {
|
|||||||
: await client.getPublicTimeline(
|
: await client.getPublicTimeline(
|
||||||
convertTimelinesArgsId(argsToBools(limitToInt(query))),
|
convertTimelinesArgsId(argsToBools(limitToInt(query))),
|
||||||
);
|
);
|
||||||
let resp = data.data.map((status) => convertStatus(status));
|
ctx.body = data.data.map((status) => convertStatus(status));
|
||||||
ctx.body = toTextWithReaction(resp, ctx.hostname);
|
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
console.error(e.response.data);
|
console.error(e.response.data);
|
||||||
@ -138,8 +71,7 @@ export function apiTimelineMastodon(router: Router): void {
|
|||||||
ctx.params.hashtag,
|
ctx.params.hashtag,
|
||||||
convertTimelinesArgsId(argsToBools(limitToInt(ctx.query))),
|
convertTimelinesArgsId(argsToBools(limitToInt(ctx.query))),
|
||||||
);
|
);
|
||||||
let resp = data.data.map((status) => convertStatus(status));
|
ctx.body = data.data.map((status) => convertStatus(status));
|
||||||
ctx.body = toTextWithReaction(resp, ctx.hostname);
|
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
console.error(e.response.data);
|
console.error(e.response.data);
|
||||||
@ -156,8 +88,7 @@ export function apiTimelineMastodon(router: Router): void {
|
|||||||
const data = await client.getHomeTimeline(
|
const data = await client.getHomeTimeline(
|
||||||
convertTimelinesArgsId(limitToInt(ctx.query)),
|
convertTimelinesArgsId(limitToInt(ctx.query)),
|
||||||
);
|
);
|
||||||
let resp = data.data.map((status) => convertStatus(status));
|
ctx.body = data.data.map((status) => convertStatus(status));
|
||||||
ctx.body = toTextWithReaction(resp, ctx.hostname);
|
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
console.error(e.response.data);
|
console.error(e.response.data);
|
||||||
@ -176,8 +107,7 @@ export function apiTimelineMastodon(router: Router): void {
|
|||||||
convertId(ctx.params.listId, IdType.CalckeyId),
|
convertId(ctx.params.listId, IdType.CalckeyId),
|
||||||
convertTimelinesArgsId(limitToInt(ctx.query)),
|
convertTimelinesArgsId(limitToInt(ctx.query)),
|
||||||
);
|
);
|
||||||
let resp = data.data.map((status) => convertStatus(status));
|
ctx.body = data.data.map((status) => convertStatus(status));
|
||||||
ctx.body = toTextWithReaction(resp, ctx.hostname);
|
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
console.error(e.response.data);
|
console.error(e.response.data);
|
||||||
|
@ -27,7 +27,6 @@ import type Channel from "./channel.js";
|
|||||||
import type { StreamEventEmitter, StreamMessages } from "./types.js";
|
import type { StreamEventEmitter, StreamMessages } from "./types.js";
|
||||||
import { Converter } from "megalodon";
|
import { Converter } from "megalodon";
|
||||||
import { getClient } from "../mastodon/ApiMastodonCompatibleService.js";
|
import { getClient } from "../mastodon/ApiMastodonCompatibleService.js";
|
||||||
import { toTextWithReaction } from "../mastodon/endpoints/timeline.js";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main stream connection
|
* Main stream connection
|
||||||
@ -400,12 +399,7 @@ export default class Connection {
|
|||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
stream: [payload.id],
|
stream: [payload.id],
|
||||||
event: "update",
|
event: "update",
|
||||||
payload: JSON.stringify(
|
payload: JSON.stringify(Converter.note(payload.body, this.host)),
|
||||||
toTextWithReaction(
|
|
||||||
[Converter.note(payload.body, this.host)],
|
|
||||||
this.host,
|
|
||||||
)[0],
|
|
||||||
),
|
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
this.onSubscribeNote({
|
this.onSubscribeNote({
|
||||||
@ -415,7 +409,7 @@ export default class Connection {
|
|||||||
// reaction
|
// reaction
|
||||||
const client = getClient(this.host, this.accessToken);
|
const client = getClient(this.host, this.accessToken);
|
||||||
client.getStatus(payload.id).then((data) => {
|
client.getStatus(payload.id).then((data) => {
|
||||||
const newPost = toTextWithReaction([data.data], this.host);
|
const newPost = [data.data];
|
||||||
const targetPost = newPost[0];
|
const targetPost = newPost[0];
|
||||||
for (const stream of this.currentSubscribe) {
|
for (const stream of this.currentSubscribe) {
|
||||||
this.wsConnection.send(
|
this.wsConnection.send(
|
||||||
@ -442,10 +436,6 @@ export default class Connection {
|
|||||||
if (payload.id === "user") {
|
if (payload.id === "user") {
|
||||||
const body = Converter.notification(payload.body, this.host);
|
const body = Converter.notification(payload.body, this.host);
|
||||||
if (body.type === "reaction") body.type = "favourite";
|
if (body.type === "reaction") body.type = "favourite";
|
||||||
body.status = toTextWithReaction(
|
|
||||||
body.status ? [body.status] : [],
|
|
||||||
"",
|
|
||||||
)[0];
|
|
||||||
this.wsConnection.send(
|
this.wsConnection.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
stream: ["user"],
|
stream: ["user"],
|
||||||
|
Loading…
Reference in New Issue
Block a user