2023-07-27 07:31:52 +02:00
|
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2023-03-03 03:13:12 +01:00
|
|
|
|
import { setImmediate } from 'node:timers/promises';
|
2024-02-03 15:01:09 +01:00
|
|
|
|
import * as mfm from '@transfem-org/sfm-js';
|
2023-10-03 13:26:11 +02:00
|
|
|
|
import { In, DataSource, IsNull, LessThan } from 'typeorm';
|
2023-04-14 06:50:05 +02:00
|
|
|
|
import * as Redis from 'ioredis';
|
2023-03-03 03:13:12 +01:00
|
|
|
|
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
|
2023-05-10 11:02:41 +02:00
|
|
|
|
import RE2 from 're2';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
import { extractMentions } from '@/misc/extract-mentions.js';
|
|
|
|
|
import { extractCustomEmojisFromMfm } from '@/misc/extract-custom-emojis-from-mfm.js';
|
|
|
|
|
import { extractHashtags } from '@/misc/extract-hashtags.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
|
import type { IMentionedRemoteUsers } from '@/models/Note.js';
|
|
|
|
|
import { MiNote } from '@/models/Note.js';
|
2024-09-30 05:24:22 +02:00
|
|
|
|
import { LatestNote } from '@/models/LatestNote.js';
|
|
|
|
|
import type { ChannelFollowingsRepository, ChannelsRepository, FollowingsRepository, InstancesRepository, LatestNotesRepository, MiFollowing, MutingsRepository, NotesRepository, NoteThreadMutingsRepository, UserListMembershipsRepository, UserProfilesRepository, UsersRepository } from '@/models/_.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
|
import type { MiDriveFile } from '@/models/DriveFile.js';
|
|
|
|
|
import type { MiApp } from '@/models/App.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
import { concat } from '@/misc/prelude/array.js';
|
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
|
import type { MiUser, MiLocalUser, MiRemoteUser } from '@/models/User.js';
|
|
|
|
|
import type { IPoll } from '@/models/Poll.js';
|
|
|
|
|
import { MiPoll } from '@/models/Poll.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
import { isDuplicateKeyValueError } from '@/misc/is-duplicate-key-value-error.js';
|
|
|
|
|
import { checkWordMute } from '@/misc/check-word-mute.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
|
import type { MiChannel } from '@/models/Channel.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
import { normalizeForSearch } from '@/misc/normalize-for-search.js';
|
2023-04-06 04:14:43 +02:00
|
|
|
|
import { MemorySingleCache } from '@/misc/cache.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
|
import type { MiUserProfile } from '@/models/UserProfile.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
import { RelayService } from '@/core/RelayService.js';
|
|
|
|
|
import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
|
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
import NotesChart from '@/core/chart/charts/notes.js';
|
|
|
|
|
import PerUserNotesChart from '@/core/chart/charts/per-user-notes.js';
|
|
|
|
|
import InstanceChart from '@/core/chart/charts/instance.js';
|
|
|
|
|
import ActiveUsersChart from '@/core/chart/charts/active-users.js';
|
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
2023-03-16 06:24:11 +01:00
|
|
|
|
import { NotificationService } from '@/core/NotificationService.js';
|
2024-06-08 08:34:19 +02:00
|
|
|
|
import { UserWebhookService } from '@/core/UserWebhookService.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
import { HashtagService } from '@/core/HashtagService.js';
|
|
|
|
|
import { AntennaService } from '@/core/AntennaService.js';
|
|
|
|
|
import { QueueService } from '@/core/QueueService.js';
|
2022-12-04 02:16:03 +01:00
|
|
|
|
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';
|
2022-12-04 09:05:32 +01:00
|
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-12-25 06:28:51 +01:00
|
|
|
|
import { DB_MAX_NOTE_TEXT_LENGTH } from '@/const.js';
|
2023-01-12 13:02:26 +01:00
|
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
2023-03-13 09:37:22 +01:00
|
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2023-05-05 01:52:14 +02:00
|
|
|
|
import { SearchService } from '@/core/SearchService.js';
|
2023-10-06 07:24:25 +02:00
|
|
|
|
import { FeaturedService } from '@/core/FeaturedService.js';
|
2023-11-26 02:02:22 +01:00
|
|
|
|
import { FanoutTimelineService } from '@/core/FanoutTimelineService.js';
|
2023-10-16 13:11:27 +02:00
|
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
2023-10-23 09:42:26 +02:00
|
|
|
|
import { UserBlockingService } from '@/core/UserBlockingService.js';
|
2024-01-26 01:24:26 +01:00
|
|
|
|
import { CacheService } from '@/core/CacheService.js';
|
2023-12-04 06:38:21 +01:00
|
|
|
|
import { isReply } from '@/misc/is-reply.js';
|
2024-01-08 04:28:13 +01:00
|
|
|
|
import { trackPromise } from '@/misc/promise-tracker.js';
|
2024-01-26 01:24:26 +01:00
|
|
|
|
import { isUserRelated } from '@/misc/is-user-related.js';
|
2024-02-21 16:59:59 +01:00
|
|
|
|
import { IdentifiableError } from '@/misc/identifiable-error.js';
|
2024-09-30 19:29:15 +02:00
|
|
|
|
import { isQuote, isRenote } from '@/misc/is-renote.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
|
|
type NotificationType = 'reply' | 'renote' | 'quote' | 'mention';
|
|
|
|
|
|
|
|
|
|
class NotificationManager {
|
2023-08-16 10:51:28 +02:00
|
|
|
|
private notifier: { id: MiUser['id']; };
|
|
|
|
|
private note: MiNote;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
private queue: {
|
2023-08-16 10:51:28 +02:00
|
|
|
|
target: MiLocalUser['id'];
|
2022-09-17 20:27:08 +02:00
|
|
|
|
reason: NotificationType;
|
|
|
|
|
}[];
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
private mutingsRepository: MutingsRepository,
|
2023-03-16 06:24:11 +01:00
|
|
|
|
private notificationService: NotificationService,
|
2023-08-16 10:51:28 +02:00
|
|
|
|
notifier: { id: MiUser['id']; },
|
|
|
|
|
note: MiNote,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
) {
|
|
|
|
|
this.notifier = notifier;
|
|
|
|
|
this.note = note;
|
|
|
|
|
this.queue = [];
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
|
public push(notifiee: MiLocalUser['id'], reason: NotificationType) {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
// 自分自身へは通知しない
|
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2023-11-02 07:57:55 +01:00
|
|
|
|
public async notify() {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
for (const x of this.queue) {
|
2023-11-02 07:57:55 +01:00
|
|
|
|
if (x.reason === 'renote') {
|
|
|
|
|
this.notificationService.createNotification(x.target, 'renote', {
|
|
|
|
|
noteId: this.note.id,
|
|
|
|
|
targetNoteId: this.note.renoteId!,
|
|
|
|
|
}, this.notifier.id);
|
|
|
|
|
} else {
|
2023-03-16 06:24:11 +01:00
|
|
|
|
this.notificationService.createNotification(x.target, x.reason, {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
noteId: this.note.id,
|
2023-09-29 04:29:54 +02:00
|
|
|
|
}, this.notifier.id);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type MinimumUser = {
|
2023-08-16 10:51:28 +02:00
|
|
|
|
id: MiUser['id'];
|
|
|
|
|
host: MiUser['host'];
|
|
|
|
|
username: MiUser['username'];
|
|
|
|
|
uri: MiUser['uri'];
|
2022-09-17 20:27:08 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type Option = {
|
|
|
|
|
createdAt?: Date | null;
|
|
|
|
|
name?: string | null;
|
|
|
|
|
text?: string | null;
|
2023-08-16 10:51:28 +02:00
|
|
|
|
reply?: MiNote | null;
|
|
|
|
|
renote?: MiNote | null;
|
|
|
|
|
files?: MiDriveFile[] | null;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
poll?: IPoll | null;
|
|
|
|
|
localOnly?: boolean | null;
|
2023-08-16 10:51:28 +02:00
|
|
|
|
reactionAcceptance?: MiNote['reactionAcceptance'];
|
2022-09-17 20:27:08 +02:00
|
|
|
|
cw?: string | null;
|
|
|
|
|
visibility?: string;
|
|
|
|
|
visibleUsers?: MinimumUser[] | null;
|
2023-08-16 10:51:28 +02:00
|
|
|
|
channel?: MiChannel | null;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
apMentions?: MinimumUser[] | null;
|
|
|
|
|
apHashtags?: string[] | null;
|
|
|
|
|
apEmojis?: string[] | null;
|
|
|
|
|
uri?: string | null;
|
|
|
|
|
url?: string | null;
|
2023-08-16 10:51:28 +02:00
|
|
|
|
app?: MiApp | null;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
2023-03-03 03:13:12 +01:00
|
|
|
|
export class NoteCreateService implements OnApplicationShutdown {
|
|
|
|
|
#shutdownController = new AbortController();
|
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
|
constructor(
|
|
|
|
|
@Inject(DI.config)
|
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
|
|
@Inject(DI.db)
|
|
|
|
|
private db: DataSource,
|
|
|
|
|
|
2023-10-03 13:26:11 +02:00
|
|
|
|
@Inject(DI.redisForTimelines)
|
|
|
|
|
private redisForTimelines: Redis.Redis,
|
2023-04-03 04:49:58 +02:00
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
2024-09-30 05:24:22 +02:00
|
|
|
|
@Inject(DI.latestNotesRepository)
|
|
|
|
|
private latestNotesRepository: LatestNotesRepository,
|
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
|
@Inject(DI.mutingsRepository)
|
|
|
|
|
private mutingsRepository: MutingsRepository,
|
|
|
|
|
|
|
|
|
|
@Inject(DI.instancesRepository)
|
|
|
|
|
private instancesRepository: InstancesRepository,
|
|
|
|
|
|
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
|
|
2023-10-03 13:26:11 +02:00
|
|
|
|
@Inject(DI.userListMembershipsRepository)
|
|
|
|
|
private userListMembershipsRepository: UserListMembershipsRepository,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
|
|
@Inject(DI.channelsRepository)
|
|
|
|
|
private channelsRepository: ChannelsRepository,
|
|
|
|
|
|
|
|
|
|
@Inject(DI.noteThreadMutingsRepository)
|
|
|
|
|
private noteThreadMutingsRepository: NoteThreadMutingsRepository,
|
|
|
|
|
|
2023-09-21 11:48:15 +02:00
|
|
|
|
@Inject(DI.followingsRepository)
|
|
|
|
|
private followingsRepository: FollowingsRepository,
|
|
|
|
|
|
2023-10-03 13:26:11 +02:00
|
|
|
|
@Inject(DI.channelFollowingsRepository)
|
|
|
|
|
private channelFollowingsRepository: ChannelFollowingsRepository,
|
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
|
private noteEntityService: NoteEntityService,
|
|
|
|
|
private idService: IdService,
|
2023-02-04 02:02:03 +01:00
|
|
|
|
private globalEventService: GlobalEventService,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
private queueService: QueueService,
|
2023-11-26 02:02:22 +01:00
|
|
|
|
private fanoutTimelineService: FanoutTimelineService,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
private noteReadService: NoteReadService,
|
2023-03-16 06:24:11 +01:00
|
|
|
|
private notificationService: NotificationService,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
private relayService: RelayService,
|
|
|
|
|
private federatedInstanceService: FederatedInstanceService,
|
|
|
|
|
private hashtagService: HashtagService,
|
|
|
|
|
private antennaService: AntennaService,
|
2024-06-08 08:34:19 +02:00
|
|
|
|
private webhookService: UserWebhookService,
|
2023-10-06 07:24:25 +02:00
|
|
|
|
private featuredService: FeaturedService,
|
2022-12-04 02:16:03 +01:00
|
|
|
|
private remoteUserResolveService: RemoteUserResolveService,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
private apDeliverManagerService: ApDeliverManagerService,
|
|
|
|
|
private apRendererService: ApRendererService,
|
2023-01-12 13:02:26 +01:00
|
|
|
|
private roleService: RoleService,
|
2023-03-13 09:37:22 +01:00
|
|
|
|
private metaService: MetaService,
|
2023-05-05 01:52:14 +02:00
|
|
|
|
private searchService: SearchService,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
private notesChart: NotesChart,
|
|
|
|
|
private perUserNotesChart: PerUserNotesChart,
|
|
|
|
|
private activeUsersChart: ActiveUsersChart,
|
|
|
|
|
private instanceChart: InstanceChart,
|
2023-10-16 13:11:27 +02:00
|
|
|
|
private utilityService: UtilityService,
|
2023-10-23 09:42:26 +02:00
|
|
|
|
private userBlockingService: UserBlockingService,
|
2024-01-26 01:24:26 +01:00
|
|
|
|
private cacheService: CacheService,
|
2023-03-16 06:24:11 +01:00
|
|
|
|
) { }
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
|
public async create(user: {
|
2023-08-16 10:51:28 +02:00
|
|
|
|
id: MiUser['id'];
|
|
|
|
|
username: MiUser['username'];
|
|
|
|
|
host: MiUser['host'];
|
|
|
|
|
isBot: MiUser['isBot'];
|
2023-11-17 15:05:58 +01:00
|
|
|
|
noindex: MiUser['noindex'];
|
2023-08-16 10:51:28 +02:00
|
|
|
|
}, data: Option, silent = false): Promise<MiNote> {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
// チャンネル外にリプライしたら対象のスコープに合わせる
|
|
|
|
|
// (クライアントサイドでやっても良い処理だと思うけどとりあえずサーバーサイドで)
|
|
|
|
|
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.createdAt == null) data.createdAt = new Date();
|
|
|
|
|
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;
|
|
|
|
|
|
2023-10-17 02:00:42 +02:00
|
|
|
|
const meta = await this.metaService.fetch();
|
|
|
|
|
|
2023-01-13 03:14:23 +01:00
|
|
|
|
if (data.visibility === 'public' && data.channel == null) {
|
2023-10-17 02:00:42 +02:00
|
|
|
|
const sensitiveWords = meta.sensitiveWords;
|
2024-02-09 19:51:41 +01:00
|
|
|
|
if (this.utilityService.isKeyWordIncluded(data.cw ?? data.text ?? '', sensitiveWords)) {
|
2023-03-13 09:37:22 +01:00
|
|
|
|
data.visibility = 'home';
|
|
|
|
|
} else if ((await this.roleService.getUserPolicies(user.id)).canPublicNote === false) {
|
2023-01-12 13:02:26 +01:00
|
|
|
|
data.visibility = 'home';
|
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-05 17:52:05 +01:00
|
|
|
|
const hasProhibitedWords = await this.checkProhibitedWordsContain({
|
|
|
|
|
cw: data.cw,
|
|
|
|
|
text: data.text,
|
|
|
|
|
pollChoices: data.poll?.choices,
|
|
|
|
|
}, meta.prohibitedWords);
|
|
|
|
|
|
|
|
|
|
if (hasProhibitedWords) {
|
|
|
|
|
throw new IdentifiableError('689ee33f-f97c-479a-ac49-1b9f8140af99', 'Note contains prohibited words');
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-17 02:00:42 +02:00
|
|
|
|
const inSilencedInstance = this.utilityService.isSilencedHost(meta.silencedHosts, user.host);
|
2023-10-16 13:11:27 +02:00
|
|
|
|
|
|
|
|
|
if (data.visibility === 'public' && inSilencedInstance && user.host !== null) {
|
|
|
|
|
data.visibility = 'home';
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-30 14:23:55 +02:00
|
|
|
|
if (data.renote) {
|
2023-10-05 10:03:50 +02:00
|
|
|
|
switch (data.renote.visibility) {
|
|
|
|
|
case 'public':
|
|
|
|
|
// public noteは無条件にrenote可能
|
|
|
|
|
break;
|
|
|
|
|
case 'home':
|
|
|
|
|
// home noteはhome以下にrenote可能
|
|
|
|
|
if (data.visibility === 'public') {
|
|
|
|
|
data.visibility = 'home';
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'followers':
|
|
|
|
|
// 他人のfollowers noteはreject
|
|
|
|
|
if (data.renote.userId !== user.id) {
|
|
|
|
|
throw new Error('Renote target is not public or home');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Renote対象がfollowersならfollowersにする
|
|
|
|
|
data.visibility = 'followers';
|
|
|
|
|
break;
|
|
|
|
|
case 'specified':
|
|
|
|
|
// specified / direct noteはreject
|
|
|
|
|
throw new Error('Renote target is not public or home');
|
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 09:42:26 +02:00
|
|
|
|
// Check blocking
|
2024-04-25 12:37:04 +02:00
|
|
|
|
if (this.isRenote(data) && !this.isQuote(data)) {
|
2023-10-23 09:42:26 +02:00
|
|
|
|
if (data.renote.userHost === null) {
|
|
|
|
|
if (data.renote.userId !== user.id) {
|
|
|
|
|
const blocked = await this.userBlockingService.checkBlocked(data.renote.userId, user.id);
|
|
|
|
|
if (blocked) {
|
|
|
|
|
throw new Error('blocked');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
|
// 返信対象が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) {
|
2022-12-25 06:28:51 +01:00
|
|
|
|
if (data.text.length > DB_MAX_NOTE_TEXT_LENGTH) {
|
|
|
|
|
data.text = data.text.slice(0, DB_MAX_NOTE_TEXT_LENGTH);
|
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
data.text = data.text.trim();
|
2024-01-26 19:07:49 +01:00
|
|
|
|
if (data.text === '') {
|
|
|
|
|
data.text = null;
|
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
} else {
|
|
|
|
|
data.text = null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-04 02:05:15 +02:00
|
|
|
|
if (data.cw) {
|
|
|
|
|
if (data.cw.length > DB_MAX_NOTE_TEXT_LENGTH) {
|
|
|
|
|
data.cw = data.cw.slice(0, DB_MAX_NOTE_TEXT_LENGTH);
|
|
|
|
|
}
|
|
|
|
|
data.cw = data.cw.trim();
|
|
|
|
|
if (data.cw === '') {
|
|
|
|
|
data.cw = null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
data.cw = null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
|
let tags = data.apHashtags;
|
|
|
|
|
let emojis = data.apEmojis;
|
|
|
|
|
let mentionedUsers = data.apMentions;
|
|
|
|
|
|
|
|
|
|
// Parse MFM if needed
|
|
|
|
|
if (!tags || !emojis || !mentionedUsers) {
|
2023-10-19 04:42:17 +02:00
|
|
|
|
const tokens = (data.text ? mfm.parse(data.text)! : []);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
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);
|
|
|
|
|
|
2022-09-18 20:11:50 +02:00
|
|
|
|
mentionedUsers = data.apMentions ?? await this.extractMentionedUsers(user, combinedTokens);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-30 14:23:55 +02:00
|
|
|
|
// if the host is media-silenced, custom emojis are not allowed
|
|
|
|
|
if (this.utilityService.isMediaSilencedHost(meta.mediaSilencedHosts, user.host)) emojis = [];
|
|
|
|
|
|
2023-10-18 02:54:18 +02:00
|
|
|
|
tags = tags.filter(tag => Array.from(tag).length <= 128).splice(0, 32);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
|
|
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 }));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-21 23:15:03 +01:00
|
|
|
|
if (user.host && !data.cw) {
|
|
|
|
|
await this.federatedInstanceService.fetch(user.host).then(async i => {
|
|
|
|
|
if (i.isNSFW) {
|
|
|
|
|
data.cw = 'Instance is marked as NSFW';
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-02 17:36:49 +01:00
|
|
|
|
if (mentionedUsers.length > 0 && mentionedUsers.length > (await this.roleService.getUserPolicies(user.id)).mentionLimit) {
|
|
|
|
|
throw new IdentifiableError('9f466dab-c856-48cd-9e65-ff90ff750580', 'Note contains too many mentions');
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-18 20:11:50 +02:00
|
|
|
|
const note = await this.insertNote(user, data, tags, emojis, mentionedUsers);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
2023-03-03 03:13:12 +01:00
|
|
|
|
setImmediate('post created', { signal: this.#shutdownController.signal }).then(
|
|
|
|
|
() => this.postNoteCreated(note, user, data, silent, tags!, mentionedUsers!),
|
|
|
|
|
() => { /* aborted, ignore this */ },
|
|
|
|
|
);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
|
|
return note;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-12 15:07:32 +01:00
|
|
|
|
@bindThis
|
|
|
|
|
public async import(user: {
|
|
|
|
|
id: MiUser['id'];
|
|
|
|
|
username: MiUser['username'];
|
|
|
|
|
host: MiUser['host'];
|
|
|
|
|
isBot: MiUser['isBot'];
|
2023-11-17 15:05:58 +01:00
|
|
|
|
noindex: MiUser['noindex'];
|
2024-08-30 18:19:51 +02:00
|
|
|
|
}, data: Option): Promise<MiNote> {
|
|
|
|
|
return this.create(user, data, true);
|
2023-11-12 15:07:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
|
private async insertNote(user: { id: MiUser['id']; host: MiUser['host']; }, data: Option, tags: string[], emojis: string[], mentionedUsers: MinimumUser[]) {
|
|
|
|
|
const insert = new MiNote({
|
2023-10-16 03:45:22 +02:00
|
|
|
|
id: this.idService.gen(data.createdAt?.getTime()),
|
2022-09-17 20:27:08 +02:00
|
|
|
|
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,
|
2023-07-31 12:14:20 +02:00
|
|
|
|
cw: data.cw ?? null,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
tags: tags.map(tag => normalizeForSearch(tag)),
|
|
|
|
|
emojis,
|
|
|
|
|
userId: user.id,
|
|
|
|
|
localOnly: data.localOnly!,
|
2023-03-08 00:56:47 +01:00
|
|
|
|
reactionAcceptance: data.reactionAcceptance,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-14 17:58:06 +02:00
|
|
|
|
// should really not happen, but better safe than sorry
|
|
|
|
|
if (data.reply?.id === insert.id) {
|
|
|
|
|
throw new Error("A note can't reply to itself");
|
|
|
|
|
}
|
|
|
|
|
if (data.renote?.id === insert.id) {
|
|
|
|
|
throw new Error("A note can't renote itself");
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
|
if (data.uri != null) insert.uri = data.uri;
|
|
|
|
|
if (data.url != null) insert.url = data.url;
|
|
|
|
|
|
|
|
|
|
// Append mentions data
|
|
|
|
|
if (mentionedUsers.length > 0) {
|
|
|
|
|
insert.mentions = mentionedUsers.map(u => u.id);
|
|
|
|
|
const profiles = await this.userProfilesRepository.findBy({ userId: In(insert.mentions) });
|
|
|
|
|
insert.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,
|
2023-07-31 12:14:20 +02:00
|
|
|
|
url: url ?? undefined,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
username: u.username,
|
|
|
|
|
host: u.host,
|
|
|
|
|
} as IMentionedRemoteUsers[0];
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 投稿を作成
|
|
|
|
|
try {
|
|
|
|
|
if (insert.hasPoll) {
|
2023-03-16 06:24:11 +01:00
|
|
|
|
// Start transaction
|
2022-09-17 20:27:08 +02:00
|
|
|
|
await this.db.transaction(async transactionalEntityManager => {
|
2023-08-16 10:51:28 +02:00
|
|
|
|
await transactionalEntityManager.insert(MiNote, insert);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
|
const poll = new MiPoll({
|
2022-09-17 20:27:08 +02:00
|
|
|
|
noteId: insert.id,
|
|
|
|
|
choices: data.poll!.choices,
|
|
|
|
|
expiresAt: data.poll!.expiresAt,
|
|
|
|
|
multiple: data.poll!.multiple,
|
|
|
|
|
votes: new Array(data.poll!.choices.length).fill(0),
|
|
|
|
|
noteVisibility: insert.visibility,
|
|
|
|
|
userId: user.id,
|
|
|
|
|
userHost: user.host,
|
2024-05-20 11:08:20 +02:00
|
|
|
|
channelId: insert.channelId,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
});
|
|
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
|
await transactionalEntityManager.insert(MiPoll, poll);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
await this.notesRepository.insert(insert);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-30 05:24:22 +02:00
|
|
|
|
await this.updateLatestNote(insert);
|
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
|
return insert;
|
|
|
|
|
} catch (e) {
|
2023-03-16 06:24:11 +01:00
|
|
|
|
// duplicate key error
|
2022-09-17 20:27:08 +02:00
|
|
|
|
if (isDuplicateKeyValueError(e)) {
|
|
|
|
|
const err = new Error('Duplicated note');
|
|
|
|
|
err.name = 'duplicated';
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.error(e);
|
|
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
|
private async postNoteCreated(note: MiNote, user: {
|
|
|
|
|
id: MiUser['id'];
|
|
|
|
|
username: MiUser['username'];
|
|
|
|
|
host: MiUser['host'];
|
|
|
|
|
isBot: MiUser['isBot'];
|
2023-11-17 15:05:58 +01:00
|
|
|
|
noindex: MiUser['noindex'];
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}, data: Option, silent: boolean, tags: string[], mentionedUsers: MinimumUser[]) {
|
2023-03-24 07:43:36 +01:00
|
|
|
|
const meta = await this.metaService.fetch();
|
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
|
this.notesChart.update(note, true);
|
2024-08-11 09:27:08 +02:00
|
|
|
|
if (note.visibility !== 'specified' && (meta.enableChartsForRemoteUser || (user.host == null))) {
|
2023-03-24 07:43:36 +01:00
|
|
|
|
this.perUserNotesChart.update(user, note, true);
|
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
|
|
// Register host
|
|
|
|
|
if (this.userEntityService.isRemoteUser(user)) {
|
2023-03-24 11:08:08 +01:00
|
|
|
|
this.federatedInstanceService.fetch(user.host).then(async i => {
|
2023-10-21 12:40:08 +02:00
|
|
|
|
if (note.renote && note.text) {
|
|
|
|
|
this.instancesRepository.increment({ id: i.id }, 'notesCount', 1);
|
|
|
|
|
} else if (!note.renote) {
|
|
|
|
|
this.instancesRepository.increment({ id: i.id }, 'notesCount', 1);
|
|
|
|
|
}
|
2023-03-24 11:08:08 +01:00
|
|
|
|
if ((await this.metaService.fetch()).enableChartsForFederatedInstances) {
|
|
|
|
|
this.instanceChart.updateNote(i.host, note, true);
|
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ハッシュタグ更新
|
|
|
|
|
if (data.visibility === 'public' || data.visibility === 'home') {
|
2023-10-18 07:29:16 +02:00
|
|
|
|
if (user.isBot && meta.enableBotTrending) {
|
|
|
|
|
this.hashtagService.updateHashtags(user, tags);
|
|
|
|
|
} else if (!user.isBot) {
|
|
|
|
|
this.hashtagService.updateHashtags(user, tags);
|
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 12:40:08 +02:00
|
|
|
|
if (data.renote && data.text) {
|
|
|
|
|
// Increment notes count (user)
|
|
|
|
|
this.incNotesCountOfUser(user);
|
|
|
|
|
} else if (!data.renote) {
|
|
|
|
|
// Increment notes count (user)
|
|
|
|
|
this.incNotesCountOfUser(user);
|
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
2023-10-09 05:36:25 +02:00
|
|
|
|
this.pushToTl(note, user);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
2023-04-12 03:07:14 +02:00
|
|
|
|
this.antennaService.addNoteToAntennas(note, user);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
|
|
if (data.reply) {
|
2022-09-18 20:11:50 +02:00
|
|
|
|
this.saveReply(data.reply, note);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-21 11:48:15 +02:00
|
|
|
|
if (data.reply == null) {
|
2023-10-03 13:26:11 +02:00
|
|
|
|
// TODO: キャッシュ
|
2023-09-21 11:48:15 +02:00
|
|
|
|
this.followingsRepository.findBy({
|
|
|
|
|
followeeId: user.id,
|
|
|
|
|
notify: 'normal',
|
|
|
|
|
}).then(followings => {
|
2023-11-09 13:35:07 +01:00
|
|
|
|
if (note.visibility !== 'specified') {
|
|
|
|
|
for (const following of followings) {
|
|
|
|
|
// TODO: ワードミュート考慮
|
|
|
|
|
this.notificationService.createNotification(following.followerId, 'note', {
|
|
|
|
|
noteId: note.id,
|
|
|
|
|
}, user.id);
|
|
|
|
|
}
|
2023-09-21 11:48:15 +02:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-15 02:16:02 +02:00
|
|
|
|
if (data.renote && data.text == null && data.renote.userId !== user.id && !user.isBot) {
|
2023-10-06 09:17:29 +02:00
|
|
|
|
this.incRenoteCount(data.renote);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.poll && data.poll.expiresAt) {
|
|
|
|
|
const delay = data.poll.expiresAt.getTime() - Date.now();
|
2023-05-29 04:54:49 +02:00
|
|
|
|
this.queueService.endedPollNotificationQueue.add(note.id, {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
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
|
2023-10-19 02:20:19 +02:00
|
|
|
|
const noteObj = await this.noteEntityService.pack(note, null, { skipHide: true, withReactionAndUserPairCache: true });
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
2023-02-04 02:02:03 +01:00
|
|
|
|
this.globalEventService.publishNotesStream(noteObj);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
2023-04-12 04:40:08 +02:00
|
|
|
|
this.roleService.addNoteToRoleTimeline(noteObj);
|
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
|
this.webhookService.getActiveWebhooks().then(webhooks => {
|
|
|
|
|
webhooks = webhooks.filter(x => x.userId === user.id && x.on.includes('note'));
|
|
|
|
|
for (const webhook of webhooks) {
|
2024-06-08 08:34:19 +02:00
|
|
|
|
this.queueService.userWebhookDeliver(webhook, 'note', {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
note: noteObj,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-03-16 06:24:11 +01:00
|
|
|
|
const nm = new NotificationManager(this.mutingsRepository, this.notificationService, user, note);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
2022-09-18 20:11:50 +02:00
|
|
|
|
await this.createMentionedEvents(mentionedUsers, note, nm);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
|
|
// If has in reply to note
|
|
|
|
|
if (data.reply) {
|
2023-12-19 10:07:32 +01:00
|
|
|
|
this.globalEventService.publishNoteStream(data.reply.id, 'replied', {
|
|
|
|
|
id: note.id,
|
2024-07-06 12:35:52 +02:00
|
|
|
|
userId: user.id,
|
2023-12-19 10:07:32 +01:00
|
|
|
|
});
|
2022-09-17 20:27:08 +02:00
|
|
|
|
// 通知
|
|
|
|
|
if (data.reply.userHost === null) {
|
2024-02-08 08:04:41 +01:00
|
|
|
|
const isThreadMuted = await this.noteThreadMutingsRepository.exists({
|
2023-07-11 07:58:58 +02:00
|
|
|
|
where: {
|
|
|
|
|
userId: data.reply.userId,
|
|
|
|
|
threadId: data.reply.threadId ?? data.reply.id,
|
2023-07-19 04:27:50 +02:00
|
|
|
|
},
|
2022-09-17 20:27:08 +02:00
|
|
|
|
});
|
|
|
|
|
|
2024-01-26 01:24:26 +01:00
|
|
|
|
const [
|
|
|
|
|
userIdsWhoMeMuting,
|
|
|
|
|
] = data.reply.userId ? await Promise.all([
|
|
|
|
|
this.cacheService.userMutingsCache.fetch(data.reply.userId),
|
|
|
|
|
]) : [new Set<string>()];
|
|
|
|
|
|
|
|
|
|
const muted = isUserRelated(note, userIdsWhoMeMuting);
|
|
|
|
|
|
2024-01-26 19:09:25 +01:00
|
|
|
|
if (!isThreadMuted && !muted) {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
nm.push(data.reply.userId, 'reply');
|
2023-02-04 02:02:03 +01:00
|
|
|
|
this.globalEventService.publishMainStream(data.reply.userId, 'reply', noteObj);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
|
|
const webhooks = (await this.webhookService.getActiveWebhooks()).filter(x => x.userId === data.reply!.userId && x.on.includes('reply'));
|
|
|
|
|
for (const webhook of webhooks) {
|
2024-06-08 08:34:19 +02:00
|
|
|
|
this.queueService.userWebhookDeliver(webhook, 'reply', {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
note: noteObj,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If it is renote
|
2024-04-14 03:23:48 +02:00
|
|
|
|
if (this.isRenote(data)) {
|
2023-12-18 11:49:19 +01:00
|
|
|
|
const type = this.isQuote(data) ? 'quote' : 'renote';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
|
|
// Notify
|
|
|
|
|
if (data.renote.userHost === null) {
|
2024-02-09 19:22:06 +01:00
|
|
|
|
const isThreadMuted = await this.noteThreadMutingsRepository.exists({
|
2024-01-25 19:39:41 +01:00
|
|
|
|
where: {
|
|
|
|
|
userId: data.renote.userId,
|
|
|
|
|
threadId: data.renote.threadId ?? data.renote.id,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-01-26 01:24:26 +01:00
|
|
|
|
const [
|
|
|
|
|
userIdsWhoMeMuting,
|
|
|
|
|
] = data.renote.userId ? await Promise.all([
|
|
|
|
|
this.cacheService.userMutingsCache.fetch(data.renote.userId),
|
|
|
|
|
]) : [new Set<string>()];
|
|
|
|
|
|
|
|
|
|
const muted = isUserRelated(note, userIdsWhoMeMuting);
|
|
|
|
|
|
2024-01-26 19:09:25 +01:00
|
|
|
|
if (!isThreadMuted && !muted) {
|
2024-01-25 19:39:41 +01:00
|
|
|
|
nm.push(data.renote.userId, type);
|
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Publish event
|
|
|
|
|
if ((user.id !== data.renote.userId) && data.renote.userHost === null) {
|
2023-02-04 02:02:03 +01:00
|
|
|
|
this.globalEventService.publishMainStream(data.renote.userId, 'renote', noteObj);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
|
|
const webhooks = (await this.webhookService.getActiveWebhooks()).filter(x => x.userId === data.renote!.userId && x.on.includes('renote'));
|
|
|
|
|
for (const webhook of webhooks) {
|
2024-06-08 08:34:19 +02:00
|
|
|
|
this.queueService.userWebhookDeliver(webhook, 'renote', {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
note: noteObj,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-02 07:57:55 +01:00
|
|
|
|
nm.notify();
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
|
|
//#region AP deliver
|
|
|
|
|
if (this.userEntityService.isLocalUser(user)) {
|
|
|
|
|
(async () => {
|
2022-09-18 20:11:50 +02:00
|
|
|
|
const noteActivity = await this.renderNoteOrRenoteActivity(data, note);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
const dm = this.apDeliverManagerService.createDeliverManager(user, noteActivity);
|
|
|
|
|
|
|
|
|
|
// メンションされたリモートユーザーに配送
|
|
|
|
|
for (const u of mentionedUsers.filter(u => this.userEntityService.isRemoteUser(u))) {
|
2023-08-16 10:51:28 +02:00
|
|
|
|
dm.addDirectRecipe(u as MiRemoteUser);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 投稿がリプライかつ投稿者がローカルユーザーかつリプライ先の投稿の投稿者がリモートユーザーなら配送
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-08 04:28:13 +01:00
|
|
|
|
trackPromise(dm.execute());
|
2022-09-17 20:27:08 +02:00
|
|
|
|
})();
|
|
|
|
|
}
|
|
|
|
|
//#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 => {
|
|
|
|
|
// この処理が行われるのはノート作成後なので、ノートが一つしかなかったら最初の投稿だと判断できる
|
2023-11-12 15:07:32 +01:00
|
|
|
|
// TODO: とはいえノートを削除して何回も投稿すればその分だけインクリメントされる雑さもあるのでどうにかしたい
|
|
|
|
|
if (count === 1) {
|
|
|
|
|
this.channelsRepository.increment({ id: data.channel!.id }, 'usersCount', 1);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register to search database
|
2023-11-17 15:05:58 +01:00
|
|
|
|
if (!user.noindex) this.index(note);
|
2023-11-12 15:07:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-18 11:49:19 +01:00
|
|
|
|
@bindThis
|
2024-04-14 03:23:48 +02:00
|
|
|
|
private isRenote(note: Option): note is Option & { renote: MiNote } {
|
|
|
|
|
return note.renote != null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
private isQuote(note: Option & { renote: MiNote }): note is Option & { renote: MiNote } & (
|
|
|
|
|
{ text: string } | { cw: string } | { reply: MiNote } | { poll: IPoll } | { files: MiDriveFile[] }
|
|
|
|
|
) {
|
|
|
|
|
// NOTE: SYNC WITH misc/is-quote.ts
|
|
|
|
|
return note.text != null ||
|
|
|
|
|
note.reply != null ||
|
|
|
|
|
note.cw != null ||
|
|
|
|
|
note.poll != null ||
|
|
|
|
|
(note.files != null && note.files.length > 0);
|
2023-12-18 11:49:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
|
private incRenoteCount(renote: MiNote) {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
this.notesRepository.createQueryBuilder().update()
|
|
|
|
|
.set({
|
|
|
|
|
renoteCount: () => '"renoteCount" + 1',
|
|
|
|
|
})
|
|
|
|
|
.where('id = :id', { id: renote.id })
|
|
|
|
|
.execute();
|
2023-10-06 07:24:25 +02:00
|
|
|
|
|
2023-10-07 00:59:46 +02:00
|
|
|
|
// 30%の確率、3日以内に投稿されたノートの場合ハイライト用ランキング更新
|
|
|
|
|
if (Math.random() < 0.3 && (Date.now() - this.idService.parse(renote.id).date.getTime()) < 1000 * 60 * 60 * 24 * 3) {
|
2023-10-06 07:24:25 +02:00
|
|
|
|
if (renote.channelId != null) {
|
2023-10-07 00:53:14 +02:00
|
|
|
|
if (renote.replyId == null) {
|
|
|
|
|
this.featuredService.updateInChannelNotesRanking(renote.channelId, renote.id, 5);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (renote.visibility === 'public' && renote.userHost == null && renote.replyId == null) {
|
|
|
|
|
this.featuredService.updateGlobalNotesRanking(renote.id, 5);
|
|
|
|
|
this.featuredService.updatePerUserNotesRanking(renote.userId, renote.id, 5);
|
|
|
|
|
}
|
2023-10-06 07:24:25 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
|
private async createMentionedEvents(mentionedUsers: MinimumUser[], note: MiNote, nm: NotificationManager) {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
for (const u of mentionedUsers.filter(u => this.userEntityService.isLocalUser(u))) {
|
2024-02-08 08:04:41 +01:00
|
|
|
|
const isThreadMuted = await this.noteThreadMutingsRepository.exists({
|
2023-07-11 07:58:58 +02:00
|
|
|
|
where: {
|
|
|
|
|
userId: u.id,
|
|
|
|
|
threadId: note.threadId ?? note.id,
|
|
|
|
|
},
|
2022-09-17 20:27:08 +02:00
|
|
|
|
});
|
|
|
|
|
|
2024-01-26 01:24:26 +01:00
|
|
|
|
const [
|
|
|
|
|
userIdsWhoMeMuting,
|
|
|
|
|
] = u.id ? await Promise.all([
|
|
|
|
|
this.cacheService.userMutingsCache.fetch(u.id),
|
|
|
|
|
]) : [new Set<string>()];
|
|
|
|
|
|
|
|
|
|
const muted = isUserRelated(note, userIdsWhoMeMuting);
|
|
|
|
|
|
|
|
|
|
if (isThreadMuted || muted) {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const detailPackedNote = await this.noteEntityService.pack(note, u, {
|
|
|
|
|
detail: true,
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-04 02:02:03 +01:00
|
|
|
|
this.globalEventService.publishMainStream(u.id, 'mention', detailPackedNote);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
|
|
const webhooks = (await this.webhookService.getActiveWebhooks()).filter(x => x.userId === u.id && x.on.includes('mention'));
|
|
|
|
|
for (const webhook of webhooks) {
|
2024-06-08 08:34:19 +02:00
|
|
|
|
this.queueService.userWebhookDeliver(webhook, 'mention', {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
note: detailPackedNote,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create notification
|
|
|
|
|
nm.push(u.id, 'mention');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
|
private saveReply(reply: MiNote, note: MiNote) {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
this.notesRepository.increment({ id: reply.id }, 'repliesCount', 1);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
|
private async renderNoteOrRenoteActivity(data: Option, note: MiNote) {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
if (data.localOnly) return null;
|
|
|
|
|
|
2024-04-14 03:23:48 +02:00
|
|
|
|
const content = this.isRenote(data) && !this.isQuote(data)
|
2022-09-17 20:27:08 +02:00
|
|
|
|
? 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);
|
|
|
|
|
|
2023-02-12 10:47:30 +01:00
|
|
|
|
return this.apRendererService.addContext(content);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
|
private index(note: MiNote) {
|
2023-05-05 01:52:14 +02:00
|
|
|
|
if (note.text == null && note.cw == null) return;
|
2023-07-08 00:08:16 +02:00
|
|
|
|
|
2023-05-05 01:52:14 +02:00
|
|
|
|
this.searchService.indexNote(note);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
|
private incNotesCountOfUser(user: { id: MiUser['id']; }) {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
this.usersRepository.createQueryBuilder().update()
|
|
|
|
|
.set({
|
|
|
|
|
updatedAt: new Date(),
|
|
|
|
|
notesCount: () => '"notesCount" + 1',
|
|
|
|
|
})
|
|
|
|
|
.where('id = :id', { id: user.id })
|
|
|
|
|
.execute();
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
|
private async extractMentionedUsers(user: { host: MiUser['host']; }, tokens: mfm.MfmNode[]): Promise<MiUser[]> {
|
2022-09-17 20:27:08 +02:00
|
|
|
|
if (tokens == null) return [];
|
|
|
|
|
|
|
|
|
|
const mentions = extractMentions(tokens);
|
|
|
|
|
let mentionedUsers = (await Promise.all(mentions.map(m =>
|
2022-12-04 02:16:03 +01:00
|
|
|
|
this.remoteUserResolveService.resolveUser(m.username, m.host ?? user.host).catch(() => null),
|
2024-06-21 06:03:51 +02:00
|
|
|
|
))).filter(x => x != null);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
|
|
// Drop duplicate users
|
|
|
|
|
mentionedUsers = mentionedUsers.filter((u, i, self) =>
|
|
|
|
|
i === self.findIndex(u2 => u.id === u2.id),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return mentionedUsers;
|
|
|
|
|
}
|
2023-03-03 03:13:12 +01:00
|
|
|
|
|
2023-10-03 13:26:11 +02:00
|
|
|
|
@bindThis
|
|
|
|
|
private async pushToTl(note: MiNote, user: { id: MiUser['id']; host: MiUser['host']; }) {
|
2023-10-04 01:46:27 +02:00
|
|
|
|
const meta = await this.metaService.fetch();
|
2023-10-23 08:17:25 +02:00
|
|
|
|
if (!meta.enableFanoutTimeline) return;
|
2023-10-04 01:46:27 +02:00
|
|
|
|
|
2023-10-09 13:31:39 +02:00
|
|
|
|
const r = this.redisForTimelines.pipeline();
|
2023-10-03 13:26:11 +02:00
|
|
|
|
|
|
|
|
|
if (note.channelId) {
|
2023-11-26 02:02:22 +01:00
|
|
|
|
this.fanoutTimelineService.push(`channelTimeline:${note.channelId}`, note.id, this.config.perChannelMaxNoteCacheCount, r);
|
2023-10-09 13:31:39 +02:00
|
|
|
|
|
2023-11-26 02:02:22 +01:00
|
|
|
|
this.fanoutTimelineService.push(`userTimelineWithChannel:${user.id}`, note.id, note.userHost == null ? meta.perLocalUserUserTimelineCacheMax : meta.perRemoteUserUserTimelineCacheMax, r);
|
2023-10-05 02:48:45 +02:00
|
|
|
|
|
2023-10-03 13:26:11 +02:00
|
|
|
|
const channelFollowings = await this.channelFollowingsRepository.find({
|
|
|
|
|
where: {
|
|
|
|
|
followeeId: note.channelId,
|
|
|
|
|
},
|
|
|
|
|
select: ['followerId'],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (const channelFollowing of channelFollowings) {
|
2023-11-26 02:02:22 +01:00
|
|
|
|
this.fanoutTimelineService.push(`homeTimeline:${channelFollowing.followerId}`, note.id, meta.perUserHomeTimelineCacheMax, r);
|
2023-10-03 13:26:11 +02:00
|
|
|
|
if (note.fileIds.length > 0) {
|
2023-11-26 02:02:22 +01:00
|
|
|
|
this.fanoutTimelineService.push(`homeTimelineWithFiles:${channelFollowing.followerId}`, note.id, meta.perUserHomeTimelineCacheMax / 2, r);
|
2023-10-03 13:26:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// TODO: キャッシュ?
|
2023-10-09 05:36:25 +02:00
|
|
|
|
// eslint-disable-next-line prefer-const
|
|
|
|
|
let [followings, userListMemberships] = await Promise.all([
|
|
|
|
|
this.followingsRepository.find({
|
|
|
|
|
where: {
|
|
|
|
|
followeeId: user.id,
|
|
|
|
|
followerHost: IsNull(),
|
|
|
|
|
isFollowerHibernated: false,
|
|
|
|
|
},
|
|
|
|
|
select: ['followerId', 'withReplies'],
|
|
|
|
|
}),
|
|
|
|
|
this.userListMembershipsRepository.find({
|
|
|
|
|
where: {
|
|
|
|
|
userId: user.id,
|
|
|
|
|
},
|
|
|
|
|
select: ['userListId', 'userListUserId', 'withReplies'],
|
|
|
|
|
}),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (note.visibility === 'followers') {
|
|
|
|
|
// TODO: 重そうだから何とかしたい Set 使う?
|
2023-10-22 03:40:53 +02:00
|
|
|
|
userListMemberships = userListMemberships.filter(x => x.userListUserId === user.id || followings.some(f => f.followerId === x.userListUserId));
|
2023-10-09 05:36:25 +02:00
|
|
|
|
}
|
2023-10-03 13:26:11 +02:00
|
|
|
|
|
|
|
|
|
// TODO: あまりにも数が多いと redisPipeline.exec に失敗する(理由は不明)ため、3万件程度を目安に分割して実行するようにする
|
|
|
|
|
for (const following of followings) {
|
2023-10-09 05:36:25 +02:00
|
|
|
|
// 基本的にvisibleUserIdsには自身のidが含まれている前提であること
|
|
|
|
|
if (note.visibility === 'specified' && !note.visibleUserIds.some(v => v === following.followerId)) continue;
|
|
|
|
|
|
2023-10-11 01:18:00 +02:00
|
|
|
|
// 「自分自身への返信 or そのフォロワーへの返信」のどちらでもない場合
|
2023-12-04 06:38:21 +01:00
|
|
|
|
if (isReply(note, following.followerId)) {
|
2023-10-03 13:26:11 +02:00
|
|
|
|
if (!following.withReplies) continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-26 02:02:22 +01:00
|
|
|
|
this.fanoutTimelineService.push(`homeTimeline:${following.followerId}`, note.id, meta.perUserHomeTimelineCacheMax, r);
|
2023-10-03 13:26:11 +02:00
|
|
|
|
if (note.fileIds.length > 0) {
|
2023-11-26 02:02:22 +01:00
|
|
|
|
this.fanoutTimelineService.push(`homeTimelineWithFiles:${following.followerId}`, note.id, meta.perUserHomeTimelineCacheMax / 2, r);
|
2023-10-03 13:26:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const userListMembership of userListMemberships) {
|
2023-10-09 05:36:25 +02:00
|
|
|
|
// ダイレクトのとき、そのリストが対象外のユーザーの場合
|
|
|
|
|
if (
|
|
|
|
|
note.visibility === 'specified' &&
|
2023-12-25 03:56:00 +01:00
|
|
|
|
note.userId !== userListMembership.userListUserId &&
|
2023-10-09 05:36:25 +02:00
|
|
|
|
!note.visibleUserIds.some(v => v === userListMembership.userListUserId)
|
|
|
|
|
) continue;
|
|
|
|
|
|
2023-10-11 01:18:00 +02:00
|
|
|
|
// 「自分自身への返信 or そのリストの作成者への返信」のどちらでもない場合
|
2023-12-04 06:38:21 +01:00
|
|
|
|
if (isReply(note, userListMembership.userListUserId)) {
|
2023-10-03 13:26:11 +02:00
|
|
|
|
if (!userListMembership.withReplies) continue;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-26 02:02:22 +01:00
|
|
|
|
this.fanoutTimelineService.push(`userListTimeline:${userListMembership.userListId}`, note.id, meta.perUserListTimelineCacheMax, r);
|
2023-10-03 13:26:11 +02:00
|
|
|
|
if (note.fileIds.length > 0) {
|
2023-11-26 02:02:22 +01:00
|
|
|
|
this.fanoutTimelineService.push(`userListTimelineWithFiles:${userListMembership.userListId}`, note.id, meta.perUserListTimelineCacheMax / 2, r);
|
2023-10-03 13:26:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-25 09:37:46 +02:00
|
|
|
|
// 自分自身のHTL
|
|
|
|
|
if (note.userHost == null) {
|
|
|
|
|
if (note.visibility !== 'specified' || !note.visibleUserIds.some(v => v === user.id)) {
|
|
|
|
|
this.fanoutTimelineService.push(`homeTimeline:${user.id}`, note.id, meta.perUserHomeTimelineCacheMax, r);
|
|
|
|
|
if (note.fileIds.length > 0) {
|
|
|
|
|
this.fanoutTimelineService.push(`homeTimelineWithFiles:${user.id}`, note.id, meta.perUserHomeTimelineCacheMax / 2, r);
|
|
|
|
|
}
|
2023-10-03 13:26:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-04 04:48:51 +02:00
|
|
|
|
// 自分自身以外への返信
|
2023-12-04 06:38:21 +01:00
|
|
|
|
if (isReply(note)) {
|
2023-11-26 02:02:22 +01:00
|
|
|
|
this.fanoutTimelineService.push(`userTimelineWithReplies:${user.id}`, note.id, note.userHost == null ? meta.perLocalUserUserTimelineCacheMax : meta.perRemoteUserUserTimelineCacheMax, r);
|
2023-10-11 03:15:44 +02:00
|
|
|
|
|
|
|
|
|
if (note.visibility === 'public' && note.userHost == null) {
|
2023-11-26 02:02:22 +01:00
|
|
|
|
this.fanoutTimelineService.push('localTimelineWithReplies', note.id, 300, r);
|
2023-12-04 09:56:48 +01:00
|
|
|
|
if (note.replyUserHost == null) {
|
|
|
|
|
this.fanoutTimelineService.push(`localTimelineWithReplyTo:${note.replyUserId}`, note.id, 300 / 10, r);
|
|
|
|
|
}
|
2023-10-11 03:15:44 +02:00
|
|
|
|
}
|
2023-10-04 04:48:51 +02:00
|
|
|
|
} else {
|
2023-11-26 02:02:22 +01:00
|
|
|
|
this.fanoutTimelineService.push(`userTimeline:${user.id}`, note.id, note.userHost == null ? meta.perLocalUserUserTimelineCacheMax : meta.perRemoteUserUserTimelineCacheMax, r);
|
2023-10-04 04:48:51 +02:00
|
|
|
|
if (note.fileIds.length > 0) {
|
2023-11-26 02:02:22 +01:00
|
|
|
|
this.fanoutTimelineService.push(`userTimelineWithFiles:${user.id}`, note.id, note.userHost == null ? meta.perLocalUserUserTimelineCacheMax / 2 : meta.perRemoteUserUserTimelineCacheMax / 2, r);
|
2023-10-04 04:48:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (note.visibility === 'public' && note.userHost == null) {
|
2023-11-26 02:02:22 +01:00
|
|
|
|
this.fanoutTimelineService.push('localTimeline', note.id, 1000, r);
|
2023-10-03 13:26:11 +02:00
|
|
|
|
if (note.fileIds.length > 0) {
|
2023-11-26 02:02:22 +01:00
|
|
|
|
this.fanoutTimelineService.push('localTimelineWithFiles', note.id, 500, r);
|
2023-10-03 13:26:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Math.random() < 0.1) {
|
|
|
|
|
process.nextTick(() => {
|
|
|
|
|
this.checkHibernation(followings);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-09 13:31:39 +02:00
|
|
|
|
r.exec();
|
2023-10-03 13:26:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
public async checkHibernation(followings: MiFollowing[]) {
|
|
|
|
|
if (followings.length === 0) return;
|
|
|
|
|
|
|
|
|
|
const shuffle = (array: MiFollowing[]) => {
|
|
|
|
|
for (let i = array.length - 1; i > 0; i--) {
|
|
|
|
|
const j = Math.floor(Math.random() * (i + 1));
|
|
|
|
|
[array[i], array[j]] = [array[j], array[i]];
|
|
|
|
|
}
|
|
|
|
|
return array;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ランダムに最大1000件サンプリング
|
|
|
|
|
const samples = shuffle(followings).slice(0, Math.min(followings.length, 1000));
|
|
|
|
|
|
|
|
|
|
const hibernatedUsers = await this.usersRepository.find({
|
|
|
|
|
where: {
|
|
|
|
|
id: In(samples.map(x => x.followerId)),
|
|
|
|
|
lastActiveDate: LessThan(new Date(Date.now() - (1000 * 60 * 60 * 24 * 50))),
|
|
|
|
|
},
|
|
|
|
|
select: ['id'],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (hibernatedUsers.length > 0) {
|
|
|
|
|
this.usersRepository.update({
|
|
|
|
|
id: In(hibernatedUsers.map(x => x.id)),
|
|
|
|
|
}, {
|
|
|
|
|
isHibernated: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.followingsRepository.update({
|
|
|
|
|
followerId: In(hibernatedUsers.map(x => x.id)),
|
|
|
|
|
}, {
|
|
|
|
|
isFollowerHibernated: true,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-02 17:36:49 +01:00
|
|
|
|
public async checkProhibitedWordsContain(content: Parameters<UtilityService['concatNoteContentsForKeyWordCheck']>[0], prohibitedWords?: string[]) {
|
|
|
|
|
if (prohibitedWords == null) {
|
|
|
|
|
prohibitedWords = (await this.metaService.fetch()).prohibitedWords;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
this.utilityService.isKeyWordIncluded(
|
|
|
|
|
this.utilityService.concatNoteContentsForKeyWordCheck(content),
|
|
|
|
|
prohibitedWords,
|
|
|
|
|
)
|
|
|
|
|
) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 06:21:26 +02:00
|
|
|
|
@bindThis
|
|
|
|
|
public dispose(): void {
|
2023-03-03 03:13:12 +01:00
|
|
|
|
this.#shutdownController.abort();
|
|
|
|
|
}
|
2023-05-29 06:21:26 +02:00
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
public onApplicationShutdown(signal?: string | undefined): void {
|
|
|
|
|
this.dispose();
|
|
|
|
|
}
|
2024-09-30 05:24:22 +02:00
|
|
|
|
|
|
|
|
|
private async updateLatestNote(note: MiNote) {
|
2024-10-02 17:38:21 +02:00
|
|
|
|
// Ignore DMs.
|
|
|
|
|
// Followers-only posts are *included*, as this table is used to back the "following" feed.
|
2024-09-30 05:24:22 +02:00
|
|
|
|
if (note.visibility === 'specified') return;
|
|
|
|
|
|
2024-09-30 19:29:15 +02:00
|
|
|
|
// Ignore pure renotes
|
|
|
|
|
if (isRenote(note) && !isQuote(note)) return;
|
|
|
|
|
|
2024-09-30 05:24:22 +02:00
|
|
|
|
// Make sure that this isn't an *older* post.
|
|
|
|
|
// We can get older posts through replies, lookups, etc.
|
|
|
|
|
const currentLatest = await this.latestNotesRepository.findOneBy({ userId: note.userId });
|
2024-10-02 17:38:21 +02:00
|
|
|
|
if (currentLatest != null && currentLatest.noteId >= note.id) return;
|
2024-09-30 05:24:22 +02:00
|
|
|
|
|
|
|
|
|
// Record this as the latest note for the given user
|
|
|
|
|
const latestNote = new LatestNote({
|
|
|
|
|
userId: note.userId,
|
|
|
|
|
noteId: note.id,
|
|
|
|
|
});
|
|
|
|
|
await this.latestNotesRepository.upsert(latestNote, ['userId']);
|
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
}
|