[mastodon-client] Implement glitch reactions

This commit is contained in:
Laura Hausmann 2023-10-07 23:43:29 +02:00
parent 9d24f8aea5
commit 23a1114c02
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605
3 changed files with 31 additions and 11 deletions

View File

@ -61,6 +61,7 @@ export function convertLegacyReactions(reactions: Record<string, number>) {
export async function toDbReaction( export async function toDbReaction(
reaction?: string | null, reaction?: string | null,
reacterHost?: string | null, reacterHost?: string | null,
recurse: boolean = true
): Promise<string> { ): Promise<string> {
if (!reaction) return await getFallbackReaction(); if (!reaction) return await getFallbackReaction();
@ -88,7 +89,9 @@ export async function toDbReaction(
if (emoji) return reacterHost ? `:${name}@${reacterHost}:` : `:${name}:`; if (emoji) return reacterHost ? `:${name}@${reacterHost}:` : `:${name}:`;
} }
return await getFallbackReaction(); return recurse && reacterHost == null && reaction !== null
? await toDbReaction(`:${reaction}:`, reacterHost, false)
: await getFallbackReaction();
} }
type DecodedReaction = { type DecodedReaction = {

View File

@ -6,7 +6,7 @@ import mfm from "mfm-js";
import { UserConverter } from "@/server/api/mastodon/converters/user.js"; import { UserConverter } from "@/server/api/mastodon/converters/user.js";
import { VisibilityConverter } from "@/server/api/mastodon/converters/visibility.js"; import { VisibilityConverter } from "@/server/api/mastodon/converters/visibility.js";
import { escapeMFM } from "@/server/api/mastodon/converters/mfm.js"; import { escapeMFM } from "@/server/api/mastodon/converters/mfm.js";
import { populateEmojis } from "@/misc/populate-emojis.js"; import { PopulatedEmoji, populateEmojis } from "@/misc/populate-emojis.js";
import { EmojiConverter } from "@/server/api/mastodon/converters/emoji.js"; import { EmojiConverter } from "@/server/api/mastodon/converters/emoji.js";
import { DriveFiles, NoteFavorites, NoteReactions, Notes, NoteThreadMutings, UserNotePinings } from "@/models/index.js"; import { DriveFiles, NoteFavorites, NoteReactions, Notes, NoteThreadMutings, UserNotePinings } from "@/models/index.js";
import { decodeReaction } from "@/misc/reaction-lib.js"; import { decodeReaction } from "@/misc/reaction-lib.js";
@ -35,13 +35,14 @@ export class NoteConverter {
.map((x) => decodeReaction(x).reaction) .map((x) => decodeReaction(x).reaction)
.map((x) => x.replace(/:/g, "")); .map((x) => x.replace(/:/g, ""));
const noteEmoji = host.then(async host => populateEmojis( const populated = host.then(async host => populateEmojis(
note.emojis.concat(reactionEmojiNames), note.emojis.concat(reactionEmojiNames),
host, host,
)) ));
.then(noteEmoji => noteEmoji
.filter((e) => e.name.indexOf("@") === -1) const noteEmoji = populated.then(noteEmoji => noteEmoji
.map((e) => EmojiConverter.encode(e))); .filter((e) => e.name.indexOf("@") === -1)
.map((e) => EmojiConverter.encode(e)));
const reactionCount = NoteReactions.countBy({ noteId: note.id }); const reactionCount = NoteReactions.countBy({ noteId: note.id });
@ -114,7 +115,6 @@ export class NoteConverter {
content: text.then(text => text !== null ? MfmHelpers.toHtml(mfm.parse(text), JSON.parse(note.mentionedRemoteUsers)) ?? escapeMFM(text) : ""), content: text.then(text => text !== null ? MfmHelpers.toHtml(mfm.parse(text), JSON.parse(note.mentionedRemoteUsers)) ?? escapeMFM(text) : ""),
text: text, text: text,
created_at: note.createdAt.toISOString(), created_at: note.createdAt.toISOString(),
// Remove reaction emojis with names containing @ from the emojis list.
emojis: noteEmoji, emojis: noteEmoji,
replies_count: note.repliesCount, replies_count: note.repliesCount,
reblogs_count: note.renoteCount, reblogs_count: note.renoteCount,
@ -133,8 +133,7 @@ export class NoteConverter {
application: null, //FIXME application: null, //FIXME
language: null, //FIXME language: null, //FIXME
pinned: isPinned, pinned: isPinned,
// Use emojis list to provide URLs for emoji reactions. reactions: populated.then(populated => Promise.resolve(reaction).then(reaction => this.encodeReactions(note.reactions, reaction?.reaction, populated))),
reactions: [], //FIXME: this.mapReactions(n.emojis, n.reactions, n.myReaction),
bookmarked: isBookmarked, bookmarked: isBookmarked,
quote: Promise.resolve(renote).then(renote => recurse && renote && note.text !== null ? this.encode(renote, ctx, false) : null), quote: Promise.resolve(renote).then(renote => recurse && renote && note.text !== null ? this.encode(renote, ctx, false) : null),
edited_at: note.updatedAt?.toISOString() edited_at: note.updatedAt?.toISOString()
@ -145,4 +144,22 @@ export class NoteConverter {
const encoded = notes.map(n => this.encode(n, ctx)); const encoded = notes.map(n => this.encode(n, ctx));
return Promise.all(encoded); return Promise.all(encoded);
} }
private static encodeReactions(reactions: Record<string, number>, myReaction: string | undefined, populated: PopulatedEmoji[]): MastodonEntity.Reaction[] {
return Object.keys(reactions).map(key => {
const isCustom = key.startsWith(':') && key.endsWith(':');
const name = isCustom ? key.substring(1, key.length - 1) : key;
const populatedName = isCustom && name.indexOf('@') === -1 ? `${name}@.` : name;
const url = isCustom ? populated.find(p => p.name === populatedName)?.url : undefined;
return {
count: reactions[key],
me: key === myReaction,
name: name,
url: url,
static_url: url,
};
});
}
} }

View File

@ -210,7 +210,7 @@ export function setupEndpointsStatus(router: Router): void {
"/v1/statuses/:id/unreact/:name", "/v1/statuses/:id/unreact/:name",
auth(true, ["write:favourites"]), auth(true, ["write:favourites"]),
async (ctx) => { async (ctx) => {
const note = await NoteHelpers.getNoteOr404(ctx.params.name, ctx); const note = await NoteHelpers.getNoteOr404(ctx.params.id, ctx);
ctx.body = await NoteHelpers.removeReactFromNote(note, ctx) ctx.body = await NoteHelpers.removeReactFromNote(note, ctx)
.then(p => NoteConverter.encode(p, ctx)); .then(p => NoteConverter.encode(p, ctx));