From 52b4fdf0d214f9425ed9dfa09f537d58fe866e0e Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Thu, 6 Jul 2023 04:00:44 +0200 Subject: [PATCH] [mastodon-client] code cleanup, remove redundant function calls --- .../api/mastodon/endpoints/notifications.ts | 18 ++--- .../server/api/mastodon/endpoints/timeline.ts | 78 +------------------ .../backend/src/server/api/stream/index.ts | 14 +--- 3 files changed, 14 insertions(+), 96 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/notifications.ts b/packages/backend/src/server/api/mastodon/endpoints/notifications.ts index 64f1886dd..198717d5c 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/notifications.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/notifications.ts @@ -3,7 +3,7 @@ import Router from "@koa/router"; import { koaBody } from "koa-body"; import { convertId, IdType } from "../../index.js"; import { getClient } from "../ApiMastodonCompatibleService.js"; -import { convertTimelinesArgsId, toTextWithReaction } from "./timeline.js"; +import { convertTimelinesArgsId } from "./timeline.js"; import { convertNotification } from "../converters.js"; function toLimitToInt(q: any) { 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); if (n.type !== "follow" && n.type !== "follow_request") { if (n.type === "reaction") n.type = "favourite"; - n.status = toTextWithReaction( - n.status ? [n.status] : [], - ctx.hostname, - )[0]; return n; } else { return n; @@ -52,11 +48,13 @@ export function apiNotificationsMastodon(router: Router): void { convertId(ctx.params.id, IdType.CalckeyId), ); const data = convertNotification(dataRaw.data); - if (data.type !== "follow" && data.type !== "follow_request") { - if (data.type === "reaction") data.type = "favourite"; - ctx.body = toTextWithReaction([data as any], ctx.request.hostname)[0]; - } else { - ctx.body = data; + ctx.body = data; + if ( + data.type !== "follow" && + data.type !== "follow_request" && + data.type === "reaction" + ) { + data.type = "favourite"; } } catch (e: any) { console.error(e); diff --git a/packages/backend/src/server/api/mastodon/endpoints/timeline.ts b/packages/backend/src/server/api/mastodon/endpoints/timeline.ts index 93fa77d36..a155cc93e 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/timeline.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/timeline.ts @@ -1,8 +1,5 @@ import Router from "@koa/router"; -import megalodon, { Entity, MegalodonInterface } from "megalodon"; import { getClient } from "../ApiMastodonCompatibleService.js"; -import { statusModel } from "./status.js"; -import Autolinker from "autolinker"; import { ParsedUrlQuery } from "querystring"; import { convertAccount, convertList, convertStatus } from "../converters.js"; import { convertId, IdType } from "../../index.js"; @@ -41,69 +38,6 @@ export function convertTimelinesArgsId(q: ParsedUrlQuery) { 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 = `

${autoLinker(t.content, host)}

${reactions.join( - ", ", - )}

`; - */ - 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 `@${match.getMention()}`; - case "hashtag": - console.log("Hashtag: ", match.getHashtag()); - return `#${match.getHashtag()}`; - } - return false; - }, - }); -} - export function apiTimelineMastodon(router: Router): void { router.get("/v1/timelines/public", async (ctx, reply) => { const BASE_URL = `${ctx.protocol}://${ctx.hostname}`; @@ -118,8 +52,7 @@ export function apiTimelineMastodon(router: Router): void { : await client.getPublicTimeline( convertTimelinesArgsId(argsToBools(limitToInt(query))), ); - let resp = data.data.map((status) => convertStatus(status)); - ctx.body = toTextWithReaction(resp, ctx.hostname); + ctx.body = data.data.map((status) => convertStatus(status)); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -138,8 +71,7 @@ export function apiTimelineMastodon(router: Router): void { ctx.params.hashtag, convertTimelinesArgsId(argsToBools(limitToInt(ctx.query))), ); - let resp = data.data.map((status) => convertStatus(status)); - ctx.body = toTextWithReaction(resp, ctx.hostname); + ctx.body = data.data.map((status) => convertStatus(status)); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -156,8 +88,7 @@ export function apiTimelineMastodon(router: Router): void { const data = await client.getHomeTimeline( convertTimelinesArgsId(limitToInt(ctx.query)), ); - let resp = data.data.map((status) => convertStatus(status)); - ctx.body = toTextWithReaction(resp, ctx.hostname); + ctx.body = data.data.map((status) => convertStatus(status)); } catch (e: any) { console.error(e); console.error(e.response.data); @@ -176,8 +107,7 @@ export function apiTimelineMastodon(router: Router): void { convertId(ctx.params.listId, IdType.CalckeyId), convertTimelinesArgsId(limitToInt(ctx.query)), ); - let resp = data.data.map((status) => convertStatus(status)); - ctx.body = toTextWithReaction(resp, ctx.hostname); + ctx.body = data.data.map((status) => convertStatus(status)); } catch (e: any) { console.error(e); console.error(e.response.data); diff --git a/packages/backend/src/server/api/stream/index.ts b/packages/backend/src/server/api/stream/index.ts index 18ac38e0a..e778c4946 100644 --- a/packages/backend/src/server/api/stream/index.ts +++ b/packages/backend/src/server/api/stream/index.ts @@ -27,7 +27,6 @@ import type Channel from "./channel.js"; import type { StreamEventEmitter, StreamMessages } from "./types.js"; import { Converter } from "megalodon"; import { getClient } from "../mastodon/ApiMastodonCompatibleService.js"; -import { toTextWithReaction } from "../mastodon/endpoints/timeline.js"; /** * Main stream connection @@ -400,12 +399,7 @@ export default class Connection { JSON.stringify({ stream: [payload.id], event: "update", - payload: JSON.stringify( - toTextWithReaction( - [Converter.note(payload.body, this.host)], - this.host, - )[0], - ), + payload: JSON.stringify(Converter.note(payload.body, this.host)), }), ); this.onSubscribeNote({ @@ -415,7 +409,7 @@ export default class Connection { // reaction const client = getClient(this.host, this.accessToken); client.getStatus(payload.id).then((data) => { - const newPost = toTextWithReaction([data.data], this.host); + const newPost = [data.data]; const targetPost = newPost[0]; for (const stream of this.currentSubscribe) { this.wsConnection.send( @@ -442,10 +436,6 @@ export default class Connection { if (payload.id === "user") { const body = Converter.notification(payload.body, this.host); if (body.type === "reaction") body.type = "favourite"; - body.status = toTextWithReaction( - body.status ? [body.status] : [], - "", - )[0]; this.wsConnection.send( JSON.stringify({ stream: ["user"],