summaryrefslogtreecommitdiff
path: root/packages/backend/src/core
diff options
context:
space:
mode:
authorAmelia Yukii <123300075+Insert5StarName@users.noreply.github.com>2023-09-22 22:43:13 +0200
committerGitHub <noreply@github.com>2023-09-22 22:43:13 +0200
commita19473eb46d09ca8b4a9f324e01b8d7cb8ce7176 (patch)
treee20e59ebca94a8a89cc9af600461898af9cf1932 /packages/backend/src/core
parentPartial Rebrand (diff)
parentfix: AP Services throwing errors (diff)
downloadsharkey-a19473eb46d09ca8b4a9f324e01b8d7cb8ce7176.tar.gz
sharkey-a19473eb46d09ca8b4a9f324e01b8d7cb8ce7176.tar.bz2
sharkey-a19473eb46d09ca8b4a9f324e01b8d7cb8ce7176.zip
Merge pull request #3 from transfem-org/feature/edit-posts
add: Editing Notes
Diffstat (limited to 'packages/backend/src/core')
-rw-r--r--packages/backend/src/core/CoreModule.ts6
-rw-r--r--packages/backend/src/core/NoteEditService.ts755
-rw-r--r--packages/backend/src/core/activitypub/ApMfmService.ts2
-rw-r--r--packages/backend/src/core/activitypub/ApRendererService.ts4
-rw-r--r--packages/backend/src/core/entities/NoteEntityService.ts3
5 files changed, 766 insertions, 4 deletions
diff --git a/packages/backend/src/core/CoreModule.ts b/packages/backend/src/core/CoreModule.ts
index 18271ee346..232080ad7f 100644
--- a/packages/backend/src/core/CoreModule.ts
+++ b/packages/backend/src/core/CoreModule.ts
@@ -31,6 +31,7 @@ import { MetaService } from './MetaService.js';
import { MfmService } from './MfmService.js';
import { ModerationLogService } from './ModerationLogService.js';
import { NoteCreateService } from './NoteCreateService.js';
+import { NoteEditService } from './NoteEditService.js';
import { NoteDeleteService } from './NoteDeleteService.js';
import { NotePiningService } from './NotePiningService.js';
import { NoteReadService } from './NoteReadService.js';
@@ -156,6 +157,7 @@ const $MetaService: Provider = { provide: 'MetaService', useExisting: MetaServic
const $MfmService: Provider = { provide: 'MfmService', useExisting: MfmService };
const $ModerationLogService: Provider = { provide: 'ModerationLogService', useExisting: ModerationLogService };
const $NoteCreateService: Provider = { provide: 'NoteCreateService', useExisting: NoteCreateService };
+const $NoteEditService: Provider = { provide: 'NoteEditService', useExisting: NoteEditService };
const $NoteDeleteService: Provider = { provide: 'NoteDeleteService', useExisting: NoteDeleteService };
const $NotePiningService: Provider = { provide: 'NotePiningService', useExisting: NotePiningService };
const $NoteReadService: Provider = { provide: 'NoteReadService', useExisting: NoteReadService };
@@ -285,6 +287,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
MfmService,
ModerationLogService,
NoteCreateService,
+ NoteEditService,
NoteDeleteService,
NotePiningService,
NoteReadService,
@@ -407,6 +410,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
$MfmService,
$ModerationLogService,
$NoteCreateService,
+ $NoteEditService,
$NoteDeleteService,
$NotePiningService,
$NoteReadService,
@@ -530,6 +534,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
MfmService,
ModerationLogService,
NoteCreateService,
+ NoteEditService,
NoteDeleteService,
NotePiningService,
NoteReadService,
@@ -651,6 +656,7 @@ const $ApQuestionService: Provider = { provide: 'ApQuestionService', useExisting
$MfmService,
$ModerationLogService,
$NoteCreateService,
+ $NoteEditService,
$NoteDeleteService,
$NotePiningService,
$NoteReadService,
diff --git a/packages/backend/src/core/NoteEditService.ts b/packages/backend/src/core/NoteEditService.ts
new file mode 100644
index 0000000000..c8cf69f934
--- /dev/null
+++ b/packages/backend/src/core/NoteEditService.ts
@@ -0,0 +1,755 @@
+/*
+ * SPDX-FileCopyrightText: syuilo and other misskey contributors
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+import { setImmediate } from 'node:timers/promises';
+import * as mfm from 'mfm-js';
+import { In } from 'typeorm';
+import * as Redis from 'ioredis';
+import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
+import RE2 from 're2';
+import { extractMentions } from '@/misc/extract-mentions.js';
+import { extractCustomEmojisFromMfm } from '@/misc/extract-custom-emojis-from-mfm.js';
+import { extractHashtags } from '@/misc/extract-hashtags.js';
+import type { IMentionedRemoteUsers } from '@/models/Note.js';
+import { MiNote } from '@/models/Note.js';
+import type { NoteEditRepository, ChannelsRepository, InstancesRepository, MutedNotesRepository, MutingsRepository, NotesRepository, NoteThreadMutingsRepository, UserProfilesRepository, UsersRepository } from '@/models/_.js';
+import type { MiDriveFile } from '@/models/DriveFile.js';
+import type { MiApp } from '@/models/App.js';
+import { concat } from '@/misc/prelude/array.js';
+import { IdService } from '@/core/IdService.js';
+import type { MiUser, MiLocalUser, MiRemoteUser } from '@/models/User.js';
+import type { IPoll } from '@/models/Poll.js';
+import { checkWordMute } from '@/misc/check-word-mute.js';
+import type { MiChannel } from '@/models/Channel.js';
+import { normalizeForSearch } from '@/misc/normalize-for-search.js';
+import { MemorySingleCache } from '@/misc/cache.js';
+import type { MiUserProfile } from '@/models/UserProfile.js';
+import { RelayService } from '@/core/RelayService.js';
+import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
+import { DI } from '@/di-symbols.js';
+import type { Config } from '@/config.js';
+import InstanceChart from '@/core/chart/charts/instance.js';
+import ActiveUsersChart from '@/core/chart/charts/active-users.js';
+import { GlobalEventService } from '@/core/GlobalEventService.js';
+import { NotificationService } from '@/core/NotificationService.js';
+import { WebhookService } from '@/core/WebhookService.js';
+import { HashtagService } from '@/core/HashtagService.js';
+import { QueueService } from '@/core/QueueService.js';
+import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
+import { UserEntityService } from '@/core/entities/UserEntityService.js';
+import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
+import { ApDeliverManagerService } from '@/core/activitypub/ApDeliverManagerService.js';
+import { NoteReadService } from '@/core/NoteReadService.js';
+import { RemoteUserResolveService } from '@/core/RemoteUserResolveService.js';
+import { bindThis } from '@/decorators.js';
+import { DB_MAX_NOTE_TEXT_LENGTH } from '@/const.js';
+import { RoleService } from '@/core/RoleService.js';
+import { MetaService } from '@/core/MetaService.js';
+import { SearchService } from '@/core/SearchService.js';
+
+const mutedWordsCache = new MemorySingleCache<{ userId: MiUserProfile['userId']; mutedWords: MiUserProfile['mutedWords']; }[]>(1000 * 60 * 5);
+
+type NotificationType = 'reply' | 'renote' | 'quote' | 'mention';
+
+class NotificationManager {
+ private notifier: { id: MiUser['id']; };
+ private note: MiNote;
+ private queue: {
+ target: MiLocalUser['id'];
+ reason: NotificationType;
+ }[];
+
+ constructor(
+ private mutingsRepository: MutingsRepository,
+ private notificationService: NotificationService,
+ notifier: { id: MiUser['id']; },
+ note: MiNote,
+ ) {
+ this.notifier = notifier;
+ this.note = note;
+ this.queue = [];
+ }
+
+ @bindThis
+ public push(notifiee: MiLocalUser['id'], reason: NotificationType) {
+ // 自分自身へは通知しない
+ if (this.notifier.id === notifiee) return;
+
+ const exist = this.queue.find(x => x.target === notifiee);
+
+ if (exist) {
+ // 「メンションされているかつ返信されている」場合は、メンションとしての通知ではなく返信としての通知にする
+ if (reason !== 'mention') {
+ exist.reason = reason;
+ }
+ } else {
+ this.queue.push({
+ reason: reason,
+ target: notifiee,
+ });
+ }
+ }
+
+ @bindThis
+ public async deliver() {
+ for (const x of this.queue) {
+ // ミュート情報を取得
+ const mentioneeMutes = await this.mutingsRepository.findBy({
+ muterId: x.target,
+ });
+
+ const mentioneesMutedUserIds = mentioneeMutes.map(m => m.muteeId);
+
+ // 通知される側のユーザーが通知する側のユーザーをミュートしていない限りは通知する
+ if (!mentioneesMutedUserIds.includes(this.notifier.id)) {
+ this.notificationService.createNotification(x.target, x.reason, {
+ notifierId: this.notifier.id,
+ noteId: this.note.id,
+ });
+ }
+ }
+ }
+}
+
+type MinimumUser = {
+ id: MiUser['id'];
+ host: MiUser['host'];
+ username: MiUser['username'];
+ uri: MiUser['uri'];
+};
+
+type Option = {
+ createdAt?: Date | null;
+ name?: string | null;
+ text?: string | null;
+ reply?: MiNote | null;
+ renote?: MiNote | null;
+ files?: MiDriveFile[] | null;
+ poll?: IPoll | null;
+ localOnly?: boolean | null;
+ reactionAcceptance?: MiNote['reactionAcceptance'];
+ cw?: string | null;
+ visibility?: string;
+ visibleUsers?: MinimumUser[] | null;
+ channel?: MiChannel | null;
+ apMentions?: MinimumUser[] | null;
+ apHashtags?: string[] | null;
+ apEmojis?: string[] | null;
+ uri?: string | null;
+ url?: string | null;
+ app?: MiApp | null;
+ updatedAt?: Date | null;
+ editcount?: boolean | null;
+};
+
+@Injectable()
+export class NoteEditService implements OnApplicationShutdown {
+ #shutdownController = new AbortController();
+
+ constructor(
+ @Inject(DI.config)
+ private config: Config,
+
+ @Inject(DI.redis)
+ private redisClient: Redis.Redis,
+
+ @Inject(DI.usersRepository)
+ private usersRepository: UsersRepository,
+
+ @Inject(DI.notesRepository)
+ private notesRepository: NotesRepository,
+
+ @Inject(DI.mutingsRepository)
+ private mutingsRepository: MutingsRepository,
+
+ @Inject(DI.instancesRepository)
+ private instancesRepository: InstancesRepository,
+
+ @Inject(DI.userProfilesRepository)
+ private userProfilesRepository: UserProfilesRepository,
+
+ @Inject(DI.mutedNotesRepository)
+ private mutedNotesRepository: MutedNotesRepository,
+
+ @Inject(DI.channelsRepository)
+ private channelsRepository: ChannelsRepository,
+
+ @Inject(DI.noteThreadMutingsRepository)
+ private noteThreadMutingsRepository: NoteThreadMutingsRepository,
+
+ @Inject(DI.noteEditRepository)
+ private noteEditRepository: NoteEditRepository,
+
+ private userEntityService: UserEntityService,
+ private noteEntityService: NoteEntityService,
+ private idService: IdService,
+ private globalEventService: GlobalEventService,
+ private queueService: QueueService,
+ private noteReadService: NoteReadService,
+ private notificationService: NotificationService,
+ private relayService: RelayService,
+ private federatedInstanceService: FederatedInstanceService,
+ private hashtagService: HashtagService,
+ private webhookService: WebhookService,
+ private remoteUserResolveService: RemoteUserResolveService,
+ private apDeliverManagerService: ApDeliverManagerService,
+ private apRendererService: ApRendererService,
+ private roleService: RoleService,
+ private metaService: MetaService,
+ private searchService: SearchService,
+ private activeUsersChart: ActiveUsersChart,
+ private instanceChart: InstanceChart,
+ ) { }
+
+ @bindThis
+ public async edit(user: {
+ id: MiUser['id'];
+ username: MiUser['username'];
+ host: MiUser['host'];
+ createdAt: MiUser['createdAt'];
+ isBot: MiUser['isBot'];
+ }, editid: MiNote['id'], data: Option, silent = false): Promise<MiNote> {
+
+ if (!editid) {
+ throw new Error('fail');
+ };
+
+ let oldnote = await this.notesRepository.findOneBy({
+ id: editid,
+ });
+
+ if (oldnote == null) {
+ throw new Error('no such note');
+ };
+
+ if (oldnote.userId !== user.id) {
+ throw new Error('not the author');
+ };
+
+ // チャンネル外にリプライしたら対象のスコープに合わせる
+ // (クライアントサイドでやっても良い処理だと思うけどとりあえずサーバーサイドで)
+ if (data.reply && data.channel && data.reply.channelId !== data.channel.id) {
+ if (data.reply.channelId) {
+ data.channel = await this.channelsRepository.findOneBy({ id: data.reply.channelId });
+ } else {
+ data.channel = null;
+ }
+ }
+
+ // チャンネル内にリプライしたら対象のスコープに合わせる
+ // (クライアントサイドでやっても良い処理だと思うけどとりあえずサーバーサイドで)
+ if (data.reply && (data.channel == null) && data.reply.channelId) {
+ data.channel = await this.channelsRepository.findOneBy({ id: data.reply.channelId });
+ }
+
+ if (data.visibility == null) data.visibility = 'public';
+ if (data.localOnly == null) data.localOnly = false;
+ if (data.channel != null) data.visibility = 'public';
+ if (data.channel != null) data.visibleUsers = [];
+ if (data.channel != null) data.localOnly = true;
+ if (data.updatedAt == null) data.updatedAt = new Date();
+
+ if (data.visibility === 'public' && data.channel == null) {
+ const sensitiveWords = (await this.metaService.fetch()).sensitiveWords;
+ if (this.isSensitive(data, sensitiveWords)) {
+ data.visibility = 'home';
+ } else if ((await this.roleService.getUserPolicies(user.id)).canPublicNote === false) {
+ data.visibility = 'home';
+ }
+ }
+
+ // Renote対象が「ホームまたは全体」以外の公開範囲ならreject
+ if (data.renote && data.renote.visibility !== 'public' && data.renote.visibility !== 'home' && data.renote.userId !== user.id) {
+ throw new Error('Renote target is not public or home');
+ }
+
+ // Renote対象がpublicではないならhomeにする
+ if (data.renote && data.renote.visibility !== 'public' && data.visibility === 'public') {
+ data.visibility = 'home';
+ }
+
+ // Renote対象がfollowersならfollowersにする
+ if (data.renote && data.renote.visibility === 'followers') {
+ data.visibility = 'followers';
+ }
+
+ // 返信対象がpublicではないならhomeにする
+ if (data.reply && data.reply.visibility !== 'public' && data.visibility === 'public') {
+ data.visibility = 'home';
+ }
+
+ // ローカルのみをRenoteしたらローカルのみにする
+ if (data.renote && data.renote.localOnly && data.channel == null) {
+ data.localOnly = true;
+ }
+
+ // ローカルのみにリプライしたらローカルのみにする
+ if (data.reply && data.reply.localOnly && data.channel == null) {
+ data.localOnly = true;
+ }
+
+ if (data.text) {
+ if (data.text.length > DB_MAX_NOTE_TEXT_LENGTH) {
+ data.text = data.text.slice(0, DB_MAX_NOTE_TEXT_LENGTH);
+ }
+ data.text = data.text.trim();
+ } else {
+ data.text = null;
+ }
+
+ let tags = data.apHashtags;
+ let emojis = data.apEmojis;
+ let mentionedUsers = data.apMentions;
+
+ // Parse MFM if needed
+ if (!tags || !emojis || !mentionedUsers) {
+ const tokens = data.text ? mfm.parse(data.text)! : [];
+ const cwTokens = data.cw ? mfm.parse(data.cw)! : [];
+ const choiceTokens = data.poll && data.poll.choices
+ ? concat(data.poll.choices.map(choice => mfm.parse(choice)!))
+ : [];
+
+ const combinedTokens = tokens.concat(cwTokens).concat(choiceTokens);
+
+ tags = data.apHashtags ?? extractHashtags(combinedTokens);
+
+ emojis = data.apEmojis ?? extractCustomEmojisFromMfm(combinedTokens);
+
+ mentionedUsers = data.apMentions ?? await this.extractMentionedUsers(user, combinedTokens);
+ }
+
+ tags = tags.filter(tag => Array.from(tag ?? '').length <= 128).splice(0, 32);
+
+ if (data.reply && (user.id !== data.reply.userId) && !mentionedUsers.some(u => u.id === data.reply!.userId)) {
+ mentionedUsers.push(await this.usersRepository.findOneByOrFail({ id: data.reply!.userId }));
+ }
+
+ if (data.visibility === 'specified') {
+ if (data.visibleUsers == null) throw new Error('invalid param');
+
+ for (const u of data.visibleUsers) {
+ if (!mentionedUsers.some(x => x.id === u.id)) {
+ mentionedUsers.push(u);
+ }
+ }
+
+ if (data.reply && !data.visibleUsers.some(x => x.id === data.reply!.userId)) {
+ data.visibleUsers.push(await this.usersRepository.findOneByOrFail({ id: data.reply!.userId }));
+ }
+ }
+
+ const update: Partial<MiNote> = {};
+ if (data.text !== oldnote.text) {
+ update.text = data.text;
+ }
+ if (data.cw !== oldnote.cw) {
+ update.cw = data.cw;
+ }
+ if (data.localOnly !== oldnote.localOnly) {
+ update.localOnly = data.localOnly;
+ }
+ if (oldnote.hasPoll !== !!data.poll) {
+ update.hasPoll = !!data.poll;
+ }
+
+ await this.noteEditRepository.insert({
+ id: this.idService.genId(),
+ noteId: oldnote.id,
+ text: data.text || undefined,
+ cw: data.cw,
+ fileIds: undefined,
+ updatedAt: new Date(),
+ });
+
+ const note = new MiNote({
+ id: oldnote.id,
+ createdAt: new Date(oldnote.createdAt!),
+ updatedAt: data.updatedAt ? data.updatedAt : new Date(),
+ fileIds: data.files ? data.files.map(file => file.id) : [],
+ replyId: data.reply ? data.reply.id : null,
+ renoteId: data.renote ? data.renote.id : null,
+ channelId: data.channel ? data.channel.id : null,
+ threadId: data.reply
+ ? data.reply.threadId
+ ? data.reply.threadId
+ : data.reply.id
+ : null,
+ name: data.name,
+ text: data.text,
+ hasPoll: data.poll != null,
+ cw: data.cw ?? null,
+ tags: tags.map(tag => normalizeForSearch(tag)),
+ emojis,
+ reactions: oldnote.reactions,
+ userId: user.id,
+ localOnly: data.localOnly!,
+ reactionAcceptance: data.reactionAcceptance,
+ visibility: data.visibility as any,
+ visibleUserIds: data.visibility === 'specified'
+ ? data.visibleUsers
+ ? data.visibleUsers.map(u => u.id)
+ : []
+ : [],
+
+ attachedFileTypes: data.files ? data.files.map(file => file.type) : [],
+
+ // 以下非正規化データ
+ replyUserId: data.reply ? data.reply.userId : null,
+ replyUserHost: data.reply ? data.reply.userHost : null,
+ renoteUserId: data.renote ? data.renote.userId : null,
+ renoteUserHost: data.renote ? data.renote.userHost : null,
+ userHost: user.host,
+ });
+
+ if (data.uri != null) note.uri = data.uri;
+ if (data.url != null) note.url = data.url;
+
+ if (mentionedUsers.length > 0) {
+ note.mentions = mentionedUsers.map(u => u.id);
+ const profiles = await this.userProfilesRepository.findBy({ userId: In(note.mentions) });
+ note.mentionedRemoteUsers = JSON.stringify(mentionedUsers.filter(u => this.userEntityService.isRemoteUser(u)).map(u => {
+ const profile = profiles.find(p => p.userId === u.id);
+ const url = profile != null ? profile.url : null;
+ return {
+ uri: u.uri,
+ url: url ?? undefined,
+ username: u.username,
+ host: u.host,
+ } as IMentionedRemoteUsers[0];
+ }));
+ }
+
+ await this.notesRepository.update(oldnote.id, note);
+
+ if (data.channel) {
+ this.redisClient.xadd(
+ `channelTimeline:${data.channel.id}`,
+ 'MAXLEN', '~', this.config.perChannelMaxNoteCacheCount.toString(),
+ '*',
+ 'note', note.id);
+ }
+
+ setImmediate('post edited', { signal: this.#shutdownController.signal }).then(
+ () => this.postNoteEdited(note, user, data, silent, tags!, mentionedUsers!),
+ () => { /* aborted, ignore this */ },
+ );
+
+ return note;
+ }
+
+ @bindThis
+ private async postNoteEdited(note: MiNote, user: {
+ id: MiUser['id'];
+ username: MiUser['username'];
+ host: MiUser['host'];
+ createdAt: MiUser['createdAt'];
+ isBot: MiUser['isBot'];
+ }, data: Option, silent: boolean, tags: string[], mentionedUsers: MinimumUser[]) {
+
+ // Register host
+ if (this.userEntityService.isRemoteUser(user)) {
+ this.federatedInstanceService.fetch(user.host).then(async i => {
+ this.instancesRepository.increment({ id: i.id }, 'notesCount', 1);
+ if ((await this.metaService.fetch()).enableChartsForFederatedInstances) {
+ this.instanceChart.updateNote(i.host, note, true);
+ }
+ });
+ }
+
+ // ハッシュタグ更新
+ if (data.visibility === 'public' || data.visibility === 'home') {
+ this.hashtagService.updateHashtags(user, tags);
+ }
+
+ // Word mute
+ mutedWordsCache.fetch(() => this.userProfilesRepository.find({
+ where: {
+ enableWordMute: true,
+ },
+ select: ['userId', 'mutedWords'],
+ })).then(us => {
+ for (const u of us) {
+ checkWordMute(note, { id: u.userId }, u.mutedWords).then(shouldMute => {
+ if (shouldMute) {
+ this.mutedNotesRepository.insert({
+ id: this.idService.genId(),
+ userId: u.userId,
+ noteId: note.id,
+ reason: 'word',
+ });
+ }
+ });
+ }
+ });
+
+ if (data.poll && data.poll.expiresAt) {
+ const delay = data.poll.expiresAt.getTime() - Date.now();
+ this.queueService.endedPollNotificationQueue.add(note.id, {
+ noteId: note.id,
+ }, {
+ delay,
+ removeOnComplete: true,
+ });
+ }
+
+ if (!silent) {
+ if (this.userEntityService.isLocalUser(user)) this.activeUsersChart.write(user);
+
+ // 未読通知を作成
+ if (data.visibility === 'specified') {
+ if (data.visibleUsers == null) throw new Error('invalid param');
+
+ for (const u of data.visibleUsers) {
+ // ローカルユーザーのみ
+ if (!this.userEntityService.isLocalUser(u)) continue;
+
+ this.noteReadService.insertNoteUnread(u.id, note, {
+ isSpecified: true,
+ isMentioned: false,
+ });
+ }
+ } else {
+ for (const u of mentionedUsers) {
+ // ローカルユーザーのみ
+ if (!this.userEntityService.isLocalUser(u)) continue;
+
+ this.noteReadService.insertNoteUnread(u.id, note, {
+ isSpecified: false,
+ isMentioned: true,
+ });
+ }
+ }
+
+ // Pack the note
+ const noteObj = await this.noteEntityService.pack(note);
+
+ this.globalEventService.publishNotesStream(noteObj);
+
+ this.roleService.addNoteToRoleTimeline(noteObj);
+
+ this.webhookService.getActiveWebhooks().then(webhooks => {
+ webhooks = webhooks.filter(x => x.userId === user.id && x.on.includes('note'));
+ for (const webhook of webhooks) {
+ this.queueService.webhookDeliver(webhook, 'note', {
+ note: noteObj,
+ });
+ }
+ });
+
+ const nm = new NotificationManager(this.mutingsRepository, this.notificationService, user, note);
+
+ await this.createMentionedEvents(mentionedUsers, note, nm);
+
+ // If has in reply to note
+ if (data.reply) {
+ // 通知
+ if (data.reply.userHost === null) {
+ const isThreadMuted = await this.noteThreadMutingsRepository.exist({
+ where: {
+ userId: data.reply.userId,
+ threadId: data.reply.threadId ?? data.reply.id,
+ },
+ });
+
+ if (!isThreadMuted) {
+ nm.push(data.reply.userId, 'reply');
+ this.globalEventService.publishMainStream(data.reply.userId, 'reply', noteObj);
+
+ const webhooks = (await this.webhookService.getActiveWebhooks()).filter(x => x.userId === data.reply!.userId && x.on.includes('reply'));
+ for (const webhook of webhooks) {
+ this.queueService.webhookDeliver(webhook, 'reply', {
+ note: noteObj,
+ });
+ }
+ }
+ }
+ }
+
+ // If it is renote
+ if (data.renote) {
+ const type = data.text ? 'quote' : 'renote';
+
+ // Notify
+ if (data.renote.userHost === null) {
+ nm.push(data.renote.userId, type);
+ }
+
+ // Publish event
+ if ((user.id !== data.renote.userId) && data.renote.userHost === null) {
+ this.globalEventService.publishMainStream(data.renote.userId, 'renote', noteObj);
+
+ const webhooks = (await this.webhookService.getActiveWebhooks()).filter(x => x.userId === data.renote!.userId && x.on.includes('renote'));
+ for (const webhook of webhooks) {
+ this.queueService.webhookDeliver(webhook, 'renote', {
+ note: noteObj,
+ });
+ }
+ }
+ }
+
+ nm.deliver();
+
+ //#region AP deliver
+ if (this.userEntityService.isLocalUser(user)) {
+ (async () => {
+ const noteActivity = await this.renderNoteOrRenoteActivity(data, note);
+ const dm = this.apDeliverManagerService.createDeliverManager(user, noteActivity);
+
+ // メンションされたリモートユーザーに配送
+ for (const u of mentionedUsers.filter(u => this.userEntityService.isRemoteUser(u))) {
+ dm.addDirectRecipe(u as MiRemoteUser);
+ }
+
+ // 投稿がリプライかつ投稿者がローカルユーザーかつリプライ先の投稿の投稿者がリモートユーザーなら配送
+ if (data.reply && data.reply.userHost !== null) {
+ const u = await this.usersRepository.findOneBy({ id: data.reply.userId });
+ if (u && this.userEntityService.isRemoteUser(u)) dm.addDirectRecipe(u);
+ }
+
+ // 投稿がRenoteかつ投稿者がローカルユーザーかつRenote元の投稿の投稿者がリモートユーザーなら配送
+ if (data.renote && data.renote.userHost !== null) {
+ const u = await this.usersRepository.findOneBy({ id: data.renote.userId });
+ if (u && this.userEntityService.isRemoteUser(u)) dm.addDirectRecipe(u);
+ }
+
+ // フォロワーに配送
+ if (['public', 'home', 'followers'].includes(note.visibility)) {
+ dm.addFollowersRecipe();
+ }
+
+ if (['public'].includes(note.visibility)) {
+ this.relayService.deliverToRelays(user, noteActivity);
+ }
+
+ dm.execute();
+ })();
+ }
+ //#endregion
+ }
+
+ if (data.channel) {
+ this.channelsRepository.increment({ id: data.channel.id }, 'notesCount', 1);
+ this.channelsRepository.update(data.channel.id, {
+ lastNotedAt: new Date(),
+ });
+
+ this.notesRepository.countBy({
+ userId: user.id,
+ channelId: data.channel.id,
+ }).then(count => {
+ // この処理が行われるのはノート作成後なので、ノートが一つしかなかったら最初の投稿だと判断できる
+ // TODO: とはいえノートを削除して何回も投稿すればその分だけインクリメントされる雑さもあるのでどうにかしたい
+ if (count === 1) {
+ this.channelsRepository.increment({ id: data.channel!.id }, 'usersCount', 1);
+ }
+ });
+ }
+
+ // Register to search database
+ this.index(note);
+ }
+
+ @bindThis
+ private isSensitive(note: Option, sensitiveWord: string[]): boolean {
+ if (sensitiveWord.length > 0) {
+ const text = note.cw ?? note.text ?? '';
+ if (text === '') return false;
+ const matched = sensitiveWord.some(filter => {
+ // represents RegExp
+ const regexp = filter.match(/^\/(.+)\/(.*)$/);
+ // This should never happen due to input sanitisation.
+ if (!regexp) {
+ const words = filter.split(' ');
+ return words.every(keyword => text.includes(keyword));
+ }
+ try {
+ return new RE2(regexp[1], regexp[2]).test(text);
+ } catch (err) {
+ // This should never happen due to input sanitisation.
+ return false;
+ }
+ });
+ if (matched) return true;
+ }
+ return false;
+ }
+
+ @bindThis
+ private async createMentionedEvents(mentionedUsers: MinimumUser[], note: MiNote, nm: NotificationManager) {
+ for (const u of mentionedUsers.filter(u => this.userEntityService.isLocalUser(u))) {
+ const isThreadMuted = await this.noteThreadMutingsRepository.exist({
+ where: {
+ userId: u.id,
+ threadId: note.threadId ?? note.id,
+ },
+ });
+
+ if (isThreadMuted) {
+ continue;
+ }
+
+ const detailPackedNote = await this.noteEntityService.pack(note, u, {
+ detail: true,
+ });
+
+ this.globalEventService.publishMainStream(u.id, 'mention', detailPackedNote);
+
+ const webhooks = (await this.webhookService.getActiveWebhooks()).filter(x => x.userId === u.id && x.on.includes('mention'));
+ for (const webhook of webhooks) {
+ this.queueService.webhookDeliver(webhook, 'mention', {
+ note: detailPackedNote,
+ });
+ }
+
+ // Create notification
+ nm.push(u.id, 'mention');
+ }
+ }
+
+ @bindThis
+ private async renderNoteOrRenoteActivity(data: Option, note: MiNote) {
+ if (data.localOnly) return null;
+
+ const content = data.renote && data.text == null && data.poll == null && (data.files == null || data.files.length === 0)
+ ? this.apRendererService.renderAnnounce(data.renote.uri ? data.renote.uri : `${this.config.url}/notes/${data.renote.id}`, note)
+ : this.apRendererService.renderCreate(await this.apRendererService.renderNote(note, false), note);
+
+ return this.apRendererService.addContext(content);
+ }
+
+ @bindThis
+ private index(note: MiNote) {
+ if (note.text == null && note.cw == null) return;
+
+ this.searchService.indexNote(note);
+ }
+
+ @bindThis
+ private async extractMentionedUsers(user: { host: MiUser['host']; }, tokens: mfm.MfmNode[]): Promise<MiUser[]> {
+ if (tokens == null) return [];
+
+ const mentions = extractMentions(tokens);
+ let mentionedUsers = (await Promise.all(mentions.map(m =>
+ this.remoteUserResolveService.resolveUser(m.username, m.host ?? user.host).catch(() => null),
+ ))).filter(x => x != null) as MiUser[];
+
+ // Drop duplicate users
+ mentionedUsers = mentionedUsers.filter((u, i, self) =>
+ i === self.findIndex(u2 => u.id === u2.id),
+ );
+
+ return mentionedUsers;
+ }
+
+ @bindThis
+ public dispose(): void {
+ this.#shutdownController.abort();
+ }
+
+ @bindThis
+ public onApplicationShutdown(signal?: string | undefined): void {
+ this.dispose();
+ }
+}
diff --git a/packages/backend/src/core/activitypub/ApMfmService.ts b/packages/backend/src/core/activitypub/ApMfmService.ts
index 60868627a2..c45c27f863 100644
--- a/packages/backend/src/core/activitypub/ApMfmService.ts
+++ b/packages/backend/src/core/activitypub/ApMfmService.ts
@@ -27,6 +27,6 @@ export class ApMfmService {
@bindThis
public getNoteHtml(note: MiNote): string | null {
if (!note.text) return '';
- return this.mfmService.toHtml(mfm.parse(note.text), JSON.parse(note.mentionedRemoteUsers));
+ return this.mfmService.toHtml(mfm.parse(note.text), note.mentionedRemoteUsers ? JSON.parse(note.mentionedRemoteUsers) : []);
}
}
diff --git a/packages/backend/src/core/activitypub/ApRendererService.ts b/packages/backend/src/core/activitypub/ApRendererService.ts
index 7a9d2e21d8..b772f3efd5 100644
--- a/packages/backend/src/core/activitypub/ApRendererService.ts
+++ b/packages/backend/src/core/activitypub/ApRendererService.ts
@@ -353,7 +353,7 @@ export class ApRendererService {
const attributedTo = this.userEntityService.genLocalUserUri(note.userId);
- const mentions = (JSON.parse(note.mentionedRemoteUsers) as IMentionedRemoteUsers).map(x => x.uri);
+ const mentions = note.mentionedRemoteUsers ? (JSON.parse(note.mentionedRemoteUsers) as IMentionedRemoteUsers).map(x => x.uri) : [];
let to: string[] = [];
let cc: string[] = [];
@@ -371,7 +371,7 @@ export class ApRendererService {
to = mentions;
}
- const mentionedUsers = note.mentions.length > 0 ? await this.usersRepository.findBy({
+ const mentionedUsers = note.mentions && note.mentions.length > 0 ? await this.usersRepository.findBy({
id: In(note.mentions),
}) : [];
diff --git a/packages/backend/src/core/entities/NoteEntityService.ts b/packages/backend/src/core/entities/NoteEntityService.ts
index bf42e98ce0..7b54ce5c6d 100644
--- a/packages/backend/src/core/entities/NoteEntityService.ts
+++ b/packages/backend/src/core/entities/NoteEntityService.ts
@@ -335,9 +335,10 @@ export class NoteEntityService implements OnModuleInit {
color: channel.color,
isSensitive: channel.isSensitive,
} : undefined,
- mentions: note.mentions.length > 0 ? note.mentions : undefined,
+ mentions: note.mentions && note.mentions.length > 0 ? note.mentions : undefined,
uri: note.uri ?? undefined,
url: note.url ?? undefined,
+ updatedAt: note.updatedAt != null ? note.updatedAt.toISOString() : undefined,
...(opts.detail ? {
clippedCount: note.clippedCount,