store emojis for note_edit

This commit is contained in:
Lhcfl 2024-04-01 11:01:59 +08:00
parent a562d9bb39
commit 0b226f7013
10 changed files with 69 additions and 5 deletions

View File

@ -0,0 +1,15 @@
import type { MigrationInterface, QueryRunner } from "typeorm";
export class ExpandNoteEdit1711936358554 implements MigrationInterface {
async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE "note_edit" ADD "emojis" character varying(128) array NOT NULL DEFAULT '{}'::varchar[]
`);
}
async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE "note_edit" DROP COLUMN "emojis"
`);
}
}

View File

@ -8,6 +8,7 @@ import { decodeReaction } from "./reaction-lib.js";
import config from "@/config/index.js";
import { query } from "@/prelude/url.js";
import { redisClient } from "@/db/redis.js";
import type { NoteEdit } from "@/models/entities/note-edit.js";
const cache = new Cache<Emoji | null>("populateEmojis", 60 * 60 * 12);
@ -110,6 +111,23 @@ export async function populateEmojis(
return emojis.filter((x): x is PopulatedEmoji => x != null);
}
export function aggregateNoteEditEmojis(
noteEdits: NoteEdit[],
sourceHost: string | null,
) {
let emojis: string[] = [];
for (const noteEdit of noteEdits) {
emojis = emojis.concat(noteEdit.emojis);
}
emojis = Array.from(new Set(emojis));
return emojis
.map((e) => parseEmojiStr(e, sourceHost))
.filter((x) => x.name != null) as {
name: string;
host: string | null;
}[];
}
export function aggregateNoteEmojis(notes: Note[]) {
let emojis: { name: string | null; host: string | null }[] = [];
for (const note of notes) {
@ -145,7 +163,7 @@ export function aggregateNoteEmojis(notes: Note[]) {
}
/**
*
* Get the given list of emojis from the database and adds them to the cache
*/
export async function prefetchEmojis(
emojis: { name: string; host: string | null }[],

View File

@ -50,4 +50,11 @@ export class NoteEdit {
comment: "The updated date of the Note.",
})
public updatedAt: Date;
@Column("varchar", {
length: 128,
array: true,
default: "{}",
})
public emojis: string[];
}

View File

@ -1,11 +1,17 @@
import { db } from "@/db/postgre.js";
import { NoteEdit } from "@/models/entities/note-edit.js";
import type { Note } from "@/models/entities/note.js";
import { awaitAll } from "@/prelude/await-all.js";
import type { Packed } from "@/misc/schema.js";
import { DriveFiles } from "../index.js";
import {
aggregateNoteEditEmojis,
populateEmojis,
prefetchEmojis,
} from "@/misc/populate-emojis.js";
export const NoteEditRepository = db.getRepository(NoteEdit).extend({
async pack(noteEdit: NoteEdit) {
async pack(noteEdit: NoteEdit, sourceNote: Note) {
const packed: Packed<"NoteEdit"> = await awaitAll({
id: noteEdit.id,
noteId: noteEdit.noteId,
@ -14,15 +20,20 @@ export const NoteEditRepository = db.getRepository(NoteEdit).extend({
cw: noteEdit.cw,
fileIds: noteEdit.fileIds,
files: DriveFiles.packMany(noteEdit.fileIds),
emojis: populateEmojis(noteEdit.emojis, sourceNote.userHost),
});
return packed;
},
async packMany(noteEdits: NoteEdit[]) {
async packMany(noteEdits: NoteEdit[], sourceNote: Note) {
if (noteEdits.length === 0) return [];
await prefetchEmojis(
aggregateNoteEditEmojis(noteEdits, sourceNote.userHost),
);
const promises = await Promise.allSettled(
noteEdits.map((n) => this.pack(n)),
noteEdits.map((n) => this.pack(n, sourceNote)),
);
// filter out rejected promises, only keep fulfilled values

View File

@ -56,5 +56,10 @@ export const packedNoteEdit = {
ref: "DriveFile",
},
},
emojis: {
type: "object",
optional: true,
nullable: true,
},
},
} as const;

View File

@ -773,6 +773,7 @@ export async function updateNote(value: string | IObject, resolver?: Resolver) {
cw: note.cw,
fileIds: note.fileIds,
updatedAt: update.updatedAt,
emojis: note.emojis,
});
publishing = true;

View File

@ -621,6 +621,7 @@ export default define(meta, paramDef, async (ps, user) => {
cw: note.cw,
fileIds: note.fileIds,
updatedAt: new Date(),
emojis: note.emojis,
});
publishing = true;

View File

@ -63,5 +63,5 @@ export default define(meta, paramDef, async (ps, user) => {
},
});
return await NoteEdits.packMany(history);
return await NoteEdits.packMany(history, note);
});

View File

@ -93,6 +93,7 @@ function convertNoteEditsToNotes(noteEdits: NoteEdit[]) {
cw: note.value.cw,
files: note.value.files,
fileIds: note.value.fileIds,
emojis: note.value.emojis,
};
return [now]
@ -108,6 +109,7 @@ function convertNoteEditsToNotes(noteEdits: NoteEdit[]) {
_shouldInsertAd_: false,
files: noteEdit.files,
fileIds: noteEdit.fileIds,
emojis: note.value.emojis.concat(noteEdit.emojis),
});
});
}

View File

@ -186,6 +186,10 @@ export type NoteEdit = {
updatedAt: string;
fileIds: DriveFile["id"][];
files: DriveFile[];
emojis: {
name: string;
url: string;
}[];
};
export type NoteReaction = {