[mastodon-client] code cleanup, remove redundant function calls
This commit is contained in:
parent
4e2c73dba9
commit
52b4fdf0d2
3 changed files with 14 additions and 96 deletions
|
@ -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);
|
||||
|
|
|
@ -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 = `<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 {
|
||||
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);
|
||||
|
|
|
@ -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"],
|
||||
|
|
Loading…
Reference in a new issue