summaryrefslogtreecommitdiff
path: root/packages/backend/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'packages/backend/src/core')
-rw-r--r--packages/backend/src/core/AbuseReportService.ts6
-rw-r--r--packages/backend/src/core/AccountMoveService.ts14
-rw-r--r--packages/backend/src/core/CaptchaService.ts6
-rw-r--r--packages/backend/src/core/ChatService.ts780
-rw-r--r--packages/backend/src/core/CoreModule.ts42
-rw-r--r--packages/backend/src/core/CreateSystemUserService.ts95
-rw-r--r--packages/backend/src/core/DeleteAccountService.ts15
-rw-r--r--packages/backend/src/core/DownloadService.ts4
-rw-r--r--packages/backend/src/core/DriveService.ts7
-rw-r--r--packages/backend/src/core/EmailService.ts7
-rw-r--r--packages/backend/src/core/FanoutTimelineService.ts3
-rw-r--r--packages/backend/src/core/GlobalEventService.ts40
-rw-r--r--packages/backend/src/core/HttpRequestService.ts51
-rw-r--r--packages/backend/src/core/InstanceActorService.ts57
-rw-r--r--packages/backend/src/core/MetaService.ts23
-rw-r--r--packages/backend/src/core/MfmService.ts3
-rw-r--r--packages/backend/src/core/NoteCreateService.ts27
-rw-r--r--packages/backend/src/core/NoteDeleteService.ts24
-rw-r--r--packages/backend/src/core/NoteReadService.ts147
-rw-r--r--packages/backend/src/core/ProxyAccountService.ts28
-rw-r--r--packages/backend/src/core/RelayService.ts33
-rw-r--r--packages/backend/src/core/RemoteUserResolveService.ts2
-rw-r--r--packages/backend/src/core/RoleService.ts25
-rw-r--r--packages/backend/src/core/S3Service.ts2
-rw-r--r--packages/backend/src/core/SignupService.ts21
-rw-r--r--packages/backend/src/core/SystemAccountService.ts172
-rw-r--r--packages/backend/src/core/UserFollowingService.ts28
-rw-r--r--packages/backend/src/core/UserListService.ts10
-rw-r--r--packages/backend/src/core/UserWebhookService.ts2
-rw-r--r--packages/backend/src/core/UtilityService.ts8
-rw-r--r--packages/backend/src/core/WebAuthnService.ts18
-rw-r--r--packages/backend/src/core/WebhookTestService.ts334
-rw-r--r--packages/backend/src/core/activitypub/ApDeliverManagerService.ts19
-rw-r--r--packages/backend/src/core/activitypub/ApInboxService.ts11
-rw-r--r--packages/backend/src/core/activitypub/ApRendererService.ts72
-rw-r--r--packages/backend/src/core/activitypub/ApRequestService.ts7
-rw-r--r--packages/backend/src/core/activitypub/ApResolverService.ts34
-rw-r--r--packages/backend/src/core/activitypub/misc/check-against-url.ts38
-rw-r--r--packages/backend/src/core/activitypub/models/ApPersonService.ts8
-rw-r--r--packages/backend/src/core/entities/ChatEntityService.ts376
-rw-r--r--packages/backend/src/core/entities/MetaEntityService.ts13
-rw-r--r--packages/backend/src/core/entities/UserEntityService.ts34
42 files changed, 1915 insertions, 731 deletions
diff --git a/packages/backend/src/core/AbuseReportService.ts b/packages/backend/src/core/AbuseReportService.ts
index 0b022d3b08..846d2c8ebd 100644
--- a/packages/backend/src/core/AbuseReportService.ts
+++ b/packages/backend/src/core/AbuseReportService.ts
@@ -10,9 +10,9 @@ import { bindThis } from '@/decorators.js';
import type { AbuseUserReportsRepository, MiAbuseUserReport, MiUser, UsersRepository } from '@/models/_.js';
import { AbuseReportNotificationService } from '@/core/AbuseReportNotificationService.js';
import { QueueService } from '@/core/QueueService.js';
-import { InstanceActorService } from '@/core/InstanceActorService.js';
import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
import { ModerationLogService } from '@/core/ModerationLogService.js';
+import { SystemAccountService } from '@/core/SystemAccountService.js';
import { IdService } from './IdService.js';
@Injectable()
@@ -27,7 +27,7 @@ export class AbuseReportService {
private idService: IdService,
private abuseReportNotificationService: AbuseReportNotificationService,
private queueService: QueueService,
- private instanceActorService: InstanceActorService,
+ private systemAccountService: SystemAccountService,
private apRendererService: ApRendererService,
private moderationLogService: ModerationLogService,
) {
@@ -136,7 +136,7 @@ export class AbuseReportService {
forwarded: true,
});
- const actor = await this.instanceActorService.getInstanceActor();
+ const actor = await this.systemAccountService.fetch('actor');
const targetUser = await this.usersRepository.findOneByOrFail({ id: report.targetUserId });
const flag = this.apRendererService.renderFlag(actor, targetUser.uri!, report.comment);
diff --git a/packages/backend/src/core/AccountMoveService.ts b/packages/backend/src/core/AccountMoveService.ts
index e24fefb4b5..2dbd16b5fe 100644
--- a/packages/backend/src/core/AccountMoveService.ts
+++ b/packages/backend/src/core/AccountMoveService.ts
@@ -20,10 +20,10 @@ import { ApPersonService } from '@/core/activitypub/models/ApPersonService.js';
import { ApDeliverManagerService } from '@/core/activitypub/ApDeliverManagerService.js';
import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
-import { ProxyAccountService } from '@/core/ProxyAccountService.js';
import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
import InstanceChart from '@/core/chart/charts/instance.js';
import PerUserFollowingChart from '@/core/chart/charts/per-user-following.js';
+import { SystemAccountService } from '@/core/SystemAccountService.js';
@Injectable()
export class AccountMoveService {
@@ -58,12 +58,12 @@ export class AccountMoveService {
private apRendererService: ApRendererService,
private apDeliverManagerService: ApDeliverManagerService,
private globalEventService: GlobalEventService,
- private proxyAccountService: ProxyAccountService,
private perUserFollowingChart: PerUserFollowingChart,
private federatedInstanceService: FederatedInstanceService,
private instanceChart: InstanceChart,
private relayService: RelayService,
private queueService: QueueService,
+ private systemAccountService: SystemAccountService,
) {
}
@@ -130,11 +130,11 @@ export class AccountMoveService {
}
// follow the new account
- const proxy = await this.proxyAccountService.fetch();
+ const proxy = await this.systemAccountService.fetch('proxy');
const followings = await this.followingsRepository.findBy({
followeeId: src.id,
followerHost: IsNull(), // follower is local
- followerId: proxy ? Not(proxy.id) : undefined,
+ followerId: Not(proxy.id),
});
const followJobs = followings.map(following => ({
from: { id: following.followerId },
@@ -269,10 +269,8 @@ export class AccountMoveService {
// Have the proxy account follow the new account in the same way as UserListService.push
if (this.userEntityService.isRemoteUser(dst)) {
- const proxy = await this.proxyAccountService.fetch();
- if (proxy) {
- this.queueService.createFollowJob([{ from: { id: proxy.id }, to: { id: dst.id } }]);
- }
+ const proxy = await this.systemAccountService.fetch('proxy');
+ this.queueService.createFollowJob([{ from: { id: proxy.id }, to: { id: dst.id } }]);
}
}
diff --git a/packages/backend/src/core/CaptchaService.ts b/packages/backend/src/core/CaptchaService.ts
index d17101ac97..5bf0f82163 100644
--- a/packages/backend/src/core/CaptchaService.ts
+++ b/packages/backend/src/core/CaptchaService.ts
@@ -47,7 +47,7 @@ export type CaptchaSetting = {
siteKey: string | null;
secretKey: string | null;
}
-}
+};
export class CaptchaError extends Error {
public readonly code: CaptchaErrorCode;
@@ -63,11 +63,11 @@ export class CaptchaError extends Error {
export type CaptchaSaveSuccess = {
success: true;
-}
+};
export type CaptchaSaveFailure = {
success: false;
error: CaptchaError;
-}
+};
export type CaptchaSaveResult = CaptchaSaveSuccess | CaptchaSaveFailure;
type CaptchaResponse = {
diff --git a/packages/backend/src/core/ChatService.ts b/packages/backend/src/core/ChatService.ts
new file mode 100644
index 0000000000..57e33af107
--- /dev/null
+++ b/packages/backend/src/core/ChatService.ts
@@ -0,0 +1,780 @@
+/*
+ * SPDX-FileCopyrightText: syuilo and misskey-project
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+import { Inject, Injectable } from '@nestjs/common';
+import * as Redis from 'ioredis';
+import { Brackets } from 'typeorm';
+import { DI } from '@/di-symbols.js';
+import type { Config } from '@/config.js';
+import { QueueService } from '@/core/QueueService.js';
+import { IdService } from '@/core/IdService.js';
+import { GlobalEventService } from '@/core/GlobalEventService.js';
+import { UserEntityService } from '@/core/entities/UserEntityService.js';
+import { ChatEntityService } from '@/core/entities/ChatEntityService.js';
+import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
+import { PushNotificationService } from '@/core/PushNotificationService.js';
+import { bindThis } from '@/decorators.js';
+import type { ChatApprovalsRepository, ChatMessagesRepository, ChatRoomInvitationsRepository, ChatRoomMembershipsRepository, ChatRoomsRepository, MiChatMessage, MiChatRoom, MiChatRoomMembership, MiDriveFile, MiUser, MutingsRepository, UsersRepository } from '@/models/_.js';
+import { UserBlockingService } from '@/core/UserBlockingService.js';
+import { QueryService } from '@/core/QueryService.js';
+import { RoleService } from '@/core/RoleService.js';
+import { UserFollowingService } from '@/core/UserFollowingService.js';
+import { MiChatRoomInvitation } from '@/models/ChatRoomInvitation.js';
+import { Packed } from '@/misc/json-schema.js';
+import { sqlLikeEscape } from '@/misc/sql-like-escape.js';
+import { CustomEmojiService } from '@/core/CustomEmojiService.js';
+import { emojiRegex } from '@/misc/emoji-regex.js';
+
+const MAX_ROOM_MEMBERS = 30;
+const MAX_REACTIONS_PER_MESSAGE = 100;
+const isCustomEmojiRegexp = /^:([\w+-]+)(?:@\.)?:$/;
+
+@Injectable()
+export class ChatService {
+ constructor(
+ @Inject(DI.config)
+ private config: Config,
+
+ @Inject(DI.redis)
+ private redisClient: Redis.Redis,
+
+ @Inject(DI.usersRepository)
+ private usersRepository: UsersRepository,
+
+ @Inject(DI.chatMessagesRepository)
+ private chatMessagesRepository: ChatMessagesRepository,
+
+ @Inject(DI.chatApprovalsRepository)
+ private chatApprovalsRepository: ChatApprovalsRepository,
+
+ @Inject(DI.chatRoomsRepository)
+ private chatRoomsRepository: ChatRoomsRepository,
+
+ @Inject(DI.chatRoomInvitationsRepository)
+ private chatRoomInvitationsRepository: ChatRoomInvitationsRepository,
+
+ @Inject(DI.chatRoomMembershipsRepository)
+ private chatRoomMembershipsRepository: ChatRoomMembershipsRepository,
+
+ @Inject(DI.mutingsRepository)
+ private mutingsRepository: MutingsRepository,
+
+ private userEntityService: UserEntityService,
+ private chatEntityService: ChatEntityService,
+ private idService: IdService,
+ private globalEventService: GlobalEventService,
+ private apRendererService: ApRendererService,
+ private queueService: QueueService,
+ private pushNotificationService: PushNotificationService,
+ private userBlockingService: UserBlockingService,
+ private queryService: QueryService,
+ private roleService: RoleService,
+ private userFollowingService: UserFollowingService,
+ private customEmojiService: CustomEmojiService,
+ ) {
+ }
+
+ @bindThis
+ public async createMessageToUser(fromUser: { id: MiUser['id']; host: MiUser['host']; }, toUser: MiUser, params: {
+ text?: string | null;
+ file?: MiDriveFile | null;
+ uri?: string | null;
+ }): Promise<Packed<'ChatMessageLite'>> {
+ if (fromUser.id === toUser.id) {
+ throw new Error('yourself');
+ }
+
+ const approvals = await this.chatApprovalsRepository.createQueryBuilder('approval')
+ .where(new Brackets(qb => { // 自分が相手を許可しているか
+ qb.where('approval.userId = :fromUserId', { fromUserId: fromUser.id })
+ .andWhere('approval.otherId = :toUserId', { toUserId: toUser.id });
+ }))
+ .orWhere(new Brackets(qb => { // 相手が自分を許可しているか
+ qb.where('approval.userId = :toUserId', { toUserId: toUser.id })
+ .andWhere('approval.otherId = :fromUserId', { fromUserId: fromUser.id });
+ }))
+ .take(2)
+ .getMany();
+
+ const otherApprovedMe = approvals.some(approval => approval.userId === toUser.id);
+ const iApprovedOther = approvals.some(approval => approval.userId === fromUser.id);
+
+ if (!otherApprovedMe) {
+ if (toUser.chatScope === 'none') {
+ throw new Error('recipient is cannot chat (none)');
+ } else if (toUser.chatScope === 'followers') {
+ const isFollower = await this.userFollowingService.isFollowing(fromUser.id, toUser.id);
+ if (!isFollower) {
+ throw new Error('recipient is cannot chat (followers)');
+ }
+ } else if (toUser.chatScope === 'following') {
+ const isFollowing = await this.userFollowingService.isFollowing(toUser.id, fromUser.id);
+ if (!isFollowing) {
+ throw new Error('recipient is cannot chat (following)');
+ }
+ } else if (toUser.chatScope === 'mutual') {
+ const isMutual = await this.userFollowingService.isMutual(fromUser.id, toUser.id);
+ if (!isMutual) {
+ throw new Error('recipient is cannot chat (mutual)');
+ }
+ }
+ }
+
+ if (!(await this.roleService.getUserPolicies(toUser.id)).canChat) {
+ throw new Error('recipient is cannot chat (policy)');
+ }
+
+ const blocked = await this.userBlockingService.checkBlocked(toUser.id, fromUser.id);
+ if (blocked) {
+ throw new Error('blocked');
+ }
+
+ const message = {
+ id: this.idService.gen(),
+ fromUserId: fromUser.id,
+ toUserId: toUser.id,
+ text: params.text ? params.text.trim() : null,
+ fileId: params.file ? params.file.id : null,
+ reads: [],
+ uri: params.uri ?? null,
+ } satisfies Partial<MiChatMessage>;
+
+ const inserted = await this.chatMessagesRepository.insertOne(message);
+
+ // 相手を許可しておく
+ if (!iApprovedOther) {
+ this.chatApprovalsRepository.insertOne({
+ id: this.idService.gen(),
+ userId: fromUser.id,
+ otherId: toUser.id,
+ });
+ }
+
+ const packedMessage = await this.chatEntityService.packMessageLiteFor1on1(inserted);
+
+ if (this.userEntityService.isLocalUser(toUser)) {
+ const redisPipeline = this.redisClient.pipeline();
+ redisPipeline.set(`newUserChatMessageExists:${toUser.id}:${fromUser.id}`, message.id);
+ redisPipeline.sadd(`newChatMessagesExists:${toUser.id}`, `user:${fromUser.id}`);
+ redisPipeline.exec();
+ }
+
+ if (this.userEntityService.isLocalUser(fromUser)) {
+ // 自分のストリーム
+ this.globalEventService.publishChatUserStream(fromUser.id, toUser.id, 'message', packedMessage);
+ }
+
+ if (this.userEntityService.isLocalUser(toUser)) {
+ // 相手のストリーム
+ this.globalEventService.publishChatUserStream(toUser.id, fromUser.id, 'message', packedMessage);
+ }
+
+ // 3秒経っても既読にならなかったらイベント発行
+ if (this.userEntityService.isLocalUser(toUser)) {
+ setTimeout(async () => {
+ const marker = await this.redisClient.get(`newUserChatMessageExists:${toUser.id}:${fromUser.id}`);
+
+ if (marker == null) return; // 既読
+
+ const packedMessageForTo = await this.chatEntityService.packMessageDetailed(inserted, toUser);
+ this.globalEventService.publishMainStream(toUser.id, 'newChatMessage', packedMessageForTo);
+ //this.pushNotificationService.pushNotification(toUser.id, 'newChatMessage', packedMessageForTo);
+ }, 3000);
+ }
+
+ return packedMessage;
+ }
+
+ @bindThis
+ public async createMessageToRoom(fromUser: { id: MiUser['id']; host: MiUser['host']; }, toRoom: MiChatRoom, params: {
+ text?: string | null;
+ file?: MiDriveFile | null;
+ uri?: string | null;
+ }): Promise<Packed<'ChatMessageLite'>> {
+ const memberships = await this.chatRoomMembershipsRepository.findBy({ roomId: toRoom.id });
+
+ if (toRoom.ownerId !== fromUser.id && !memberships.some(member => member.userId === fromUser.id)) {
+ throw new Error('you are not a member of the room');
+ }
+
+ const message = {
+ id: this.idService.gen(),
+ fromUserId: fromUser.id,
+ toRoomId: toRoom.id,
+ text: params.text ? params.text.trim() : null,
+ fileId: params.file ? params.file.id : null,
+ reads: [],
+ uri: params.uri ?? null,
+ } satisfies Partial<MiChatMessage>;
+
+ const inserted = await this.chatMessagesRepository.insertOne(message);
+
+ const packedMessage = await this.chatEntityService.packMessageLiteForRoom(inserted);
+
+ this.globalEventService.publishChatRoomStream(toRoom.id, 'message', packedMessage);
+
+ const redisPipeline = this.redisClient.pipeline();
+ for (const membership of memberships) {
+ if (membership.isMuted) continue;
+
+ redisPipeline.set(`newRoomChatMessageExists:${membership.userId}:${toRoom.id}`, message.id);
+ redisPipeline.sadd(`newChatMessagesExists:${membership.userId}`, `room:${toRoom.id}`);
+ }
+ redisPipeline.exec();
+
+ // 3秒経っても既読にならなかったらイベント発行
+ setTimeout(async () => {
+ const redisPipeline = this.redisClient.pipeline();
+ for (const membership of memberships) {
+ redisPipeline.get(`newRoomChatMessageExists:${membership.userId}:${toRoom.id}`);
+ }
+ const markers = await redisPipeline.exec();
+ if (markers == null) throw new Error('redis error');
+
+ if (markers.every(marker => marker[1] == null)) return;
+
+ const packedMessageForTo = await this.chatEntityService.packMessageDetailed(inserted);
+
+ for (let i = 0; i < memberships.length; i++) {
+ const marker = markers[i][1];
+ if (marker == null) continue;
+
+ this.globalEventService.publishMainStream(memberships[i].userId, 'newChatMessage', packedMessageForTo);
+ //this.pushNotificationService.pushNotification(memberships[i].userId, 'newChatMessage', packedMessageForTo);
+ }
+ }, 3000);
+
+ return packedMessage;
+ }
+
+ @bindThis
+ public async readUserChatMessage(
+ readerId: MiUser['id'],
+ senderId: MiUser['id'],
+ ): Promise<void> {
+ const redisPipeline = this.redisClient.pipeline();
+ redisPipeline.del(`newUserChatMessageExists:${readerId}:${senderId}`);
+ redisPipeline.srem(`newChatMessagesExists:${readerId}`, `user:${senderId}`);
+ await redisPipeline.exec();
+ }
+
+ @bindThis
+ public async readRoomChatMessage(
+ readerId: MiUser['id'],
+ roomId: MiChatRoom['id'],
+ ): Promise<void> {
+ const redisPipeline = this.redisClient.pipeline();
+ redisPipeline.del(`newRoomChatMessageExists:${readerId}:${roomId}`);
+ redisPipeline.srem(`newChatMessagesExists:${readerId}`, `room:${roomId}`);
+ await redisPipeline.exec();
+ }
+
+ @bindThis
+ public findMessageById(messageId: MiChatMessage['id']) {
+ return this.chatMessagesRepository.findOneBy({ id: messageId });
+ }
+
+ @bindThis
+ public findMyMessageById(userId: MiUser['id'], messageId: MiChatMessage['id']) {
+ return this.chatMessagesRepository.findOneBy({ id: messageId, fromUserId: userId });
+ }
+
+ @bindThis
+ public async deleteMessage(message: MiChatMessage) {
+ await this.chatMessagesRepository.delete(message.id);
+
+ if (message.toUserId) {
+ const [fromUser, toUser] = await Promise.all([
+ this.usersRepository.findOneByOrFail({ id: message.fromUserId }),
+ this.usersRepository.findOneByOrFail({ id: message.toUserId }),
+ ]);
+
+ if (this.userEntityService.isLocalUser(fromUser)) this.globalEventService.publishChatUserStream(message.fromUserId, message.toUserId, 'deleted', message.id);
+ if (this.userEntityService.isLocalUser(toUser)) this.globalEventService.publishChatUserStream(message.toUserId, message.fromUserId, 'deleted', message.id);
+
+ if (this.userEntityService.isLocalUser(fromUser) && this.userEntityService.isRemoteUser(toUser)) {
+ //const activity = this.apRendererService.addContext(this.apRendererService.renderDelete(this.apRendererService.renderTombstone(`${this.config.url}/notes/${message.id}`), fromUser));
+ //this.queueService.deliver(fromUser, activity, toUser.inbox);
+ }
+ } else if (message.toRoomId) {
+ this.globalEventService.publishChatRoomStream(message.toRoomId, 'deleted', message.id);
+ }
+ }
+
+ @bindThis
+ public async userTimeline(meId: MiUser['id'], otherId: MiUser['id'], limit: number, sinceId?: MiChatMessage['id'] | null, untilId?: MiChatMessage['id'] | null) {
+ const query = this.queryService.makePaginationQuery(this.chatMessagesRepository.createQueryBuilder('message'), sinceId, untilId)
+ .andWhere(new Brackets(qb => {
+ qb
+ .where(new Brackets(qb => {
+ qb
+ .where('message.fromUserId = :meId')
+ .andWhere('message.toUserId = :otherId');
+ }))
+ .orWhere(new Brackets(qb => {
+ qb
+ .where('message.fromUserId = :otherId')
+ .andWhere('message.toUserId = :meId');
+ }));
+ }))
+ .setParameter('meId', meId)
+ .setParameter('otherId', otherId);
+
+ const messages = await query.take(limit).getMany();
+
+ return messages;
+ }
+
+ @bindThis
+ public async roomTimeline(roomId: MiChatRoom['id'], limit: number, sinceId?: MiChatMessage['id'] | null, untilId?: MiChatMessage['id'] | null) {
+ const query = this.queryService.makePaginationQuery(this.chatMessagesRepository.createQueryBuilder('message'), sinceId, untilId)
+ .where('message.toRoomId = :roomId', { roomId })
+ .leftJoinAndSelect('message.file', 'file')
+ .leftJoinAndSelect('message.fromUser', 'fromUser');
+
+ const messages = await query.take(limit).getMany();
+
+ return messages;
+ }
+
+ @bindThis
+ public async userHistory(meId: MiUser['id'], limit: number): Promise<MiChatMessage[]> {
+ const history: MiChatMessage[] = [];
+
+ const mutingQuery = this.mutingsRepository.createQueryBuilder('muting')
+ .select('muting.muteeId')
+ .where('muting.muterId = :muterId', { muterId: meId });
+
+ for (let i = 0; i < limit; i++) {
+ const found = history.map(m => (m.fromUserId === meId) ? m.toUserId! : m.fromUserId!);
+
+ const query = this.chatMessagesRepository.createQueryBuilder('message')
+ .orderBy('message.id', 'DESC')
+ .where(new Brackets(qb => {
+ qb
+ .where('message.fromUserId = :meId', { meId: meId })
+ .orWhere('message.toUserId = :meId', { meId: meId });
+ }))
+ .andWhere('message.toRoomId IS NULL')
+ .andWhere(`message.fromUserId NOT IN (${ mutingQuery.getQuery() })`)
+ .andWhere(`message.toUserId NOT IN (${ mutingQuery.getQuery() })`);
+
+ if (found.length > 0) {
+ query.andWhere('message.fromUserId NOT IN (:...found)', { found: found });
+ query.andWhere('message.toUserId NOT IN (:...found)', { found: found });
+ }
+
+ query.setParameters(mutingQuery.getParameters());
+
+ const message = await query.getOne();
+
+ if (message) {
+ history.push(message);
+ } else {
+ break;
+ }
+ }
+
+ return history;
+ }
+
+ @bindThis
+ public async roomHistory(meId: MiUser['id'], limit: number): Promise<MiChatMessage[]> {
+ // TODO: 一回のクエリにまとめられるかも
+ const [memberRoomIds, ownedRoomIds] = await Promise.all([
+ this.chatRoomMembershipsRepository.findBy({
+ userId: meId,
+ }).then(xs => xs.map(x => x.roomId)),
+ this.chatRoomsRepository.findBy({
+ ownerId: meId,
+ }).then(xs => xs.map(x => x.id)),
+ ]);
+
+ const roomIds = memberRoomIds.concat(ownedRoomIds);
+
+ if (memberRoomIds.length === 0 && ownedRoomIds.length === 0) {
+ return [];
+ }
+
+ const history: MiChatMessage[] = [];
+
+ for (let i = 0; i < limit; i++) {
+ const found = history.map(m => m.toRoomId!);
+
+ const query = this.chatMessagesRepository.createQueryBuilder('message')
+ .orderBy('message.id', 'DESC')
+ .where('message.toRoomId IN (:...roomIds)', { roomIds });
+
+ if (found.length > 0) {
+ query.andWhere('message.toRoomId NOT IN (:...found)', { found: found });
+ }
+
+ const message = await query.getOne();
+
+ if (message) {
+ history.push(message);
+ } else {
+ break;
+ }
+ }
+
+ return history;
+ }
+
+ @bindThis
+ public async getUserReadStateMap(userId: MiUser['id'], otherIds: MiUser['id'][]) {
+ const readStateMap: Record<MiUser['id'], boolean> = {};
+
+ const redisPipeline = this.redisClient.pipeline();
+
+ for (const otherId of otherIds) {
+ redisPipeline.get(`newUserChatMessageExists:${userId}:${otherId}`);
+ }
+
+ const markers = await redisPipeline.exec();
+ if (markers == null) throw new Error('redis error');
+
+ for (let i = 0; i < otherIds.length; i++) {
+ const marker = markers[i][1];
+ readStateMap[otherIds[i]] = marker == null;
+ }
+
+ return readStateMap;
+ }
+
+ @bindThis
+ public async getRoomReadStateMap(userId: MiUser['id'], roomIds: MiChatRoom['id'][]) {
+ const readStateMap: Record<MiChatRoom['id'], boolean> = {};
+
+ const redisPipeline = this.redisClient.pipeline();
+
+ for (const roomId of roomIds) {
+ redisPipeline.get(`newRoomChatMessageExists:${userId}:${roomId}`);
+ }
+
+ const markers = await redisPipeline.exec();
+ if (markers == null) throw new Error('redis error');
+
+ for (let i = 0; i < roomIds.length; i++) {
+ const marker = markers[i][1];
+ readStateMap[roomIds[i]] = marker == null;
+ }
+
+ return readStateMap;
+ }
+
+ @bindThis
+ public async hasUnreadMessages(userId: MiUser['id']) {
+ const card = await this.redisClient.scard(`newChatMessagesExists:${userId}`);
+ return card > 0;
+ }
+
+ @bindThis
+ public async createRoom(owner: MiUser, params: Partial<{
+ name: string;
+ description: string;
+ }>) {
+ const room = {
+ id: this.idService.gen(),
+ name: params.name,
+ description: params.description,
+ ownerId: owner.id,
+ } satisfies Partial<MiChatRoom>;
+
+ const created = await this.chatRoomsRepository.insertOne(room);
+
+ return created;
+ }
+
+ @bindThis
+ public async deleteRoom(room: MiChatRoom) {
+ await this.chatRoomsRepository.delete(room.id);
+ }
+
+ @bindThis
+ public async findMyRoomById(ownerId: MiUser['id'], roomId: MiChatRoom['id']) {
+ return this.chatRoomsRepository.findOneBy({ id: roomId, ownerId: ownerId });
+ }
+
+ @bindThis
+ public async findRoomById(roomId: MiChatRoom['id']) {
+ return this.chatRoomsRepository.findOne({ where: { id: roomId }, relations: ['owner'] });
+ }
+
+ @bindThis
+ public async isRoomMember(room: MiChatRoom, userId: MiUser['id']) {
+ if (room.ownerId === userId) return true;
+ const membership = await this.chatRoomMembershipsRepository.findOneBy({ roomId: room.id, userId });
+ return membership != null;
+ }
+
+ @bindThis
+ public async createRoomInvitation(inviterId: MiUser['id'], roomId: MiChatRoom['id'], inviteeId: MiUser['id']) {
+ if (inviterId === inviteeId) {
+ throw new Error('yourself');
+ }
+
+ const room = await this.chatRoomsRepository.findOneByOrFail({ id: roomId, ownerId: inviterId });
+
+ if (await this.isRoomMember(room, inviteeId)) {
+ throw new Error('already member');
+ }
+
+ const existingInvitation = await this.chatRoomInvitationsRepository.findOneBy({ roomId, userId: inviteeId });
+ if (existingInvitation) {
+ throw new Error('already invited');
+ }
+
+ const membershipsCount = await this.chatRoomMembershipsRepository.countBy({ roomId });
+ if (membershipsCount >= MAX_ROOM_MEMBERS) {
+ throw new Error('room is full');
+ }
+
+ // TODO: cehck block
+
+ const invitation = {
+ id: this.idService.gen(),
+ roomId: room.id,
+ userId: inviteeId,
+ } satisfies Partial<MiChatRoomInvitation>;
+
+ const created = await this.chatRoomInvitationsRepository.insertOne(invitation);
+
+ return created;
+ }
+
+ @bindThis
+ public async getOwnedRoomsWithPagination(ownerId: MiUser['id'], limit: number, sinceId?: MiChatRoom['id'] | null, untilId?: MiChatRoom['id'] | null) {
+ const query = this.queryService.makePaginationQuery(this.chatRoomsRepository.createQueryBuilder('room'), sinceId, untilId)
+ .where('room.ownerId = :ownerId', { ownerId });
+
+ const rooms = await query.take(limit).getMany();
+
+ return rooms;
+ }
+
+ @bindThis
+ public async getReceivedRoomInvitationsWithPagination(userId: MiUser['id'], limit: number, sinceId?: MiChatRoomInvitation['id'] | null, untilId?: MiChatRoomInvitation['id'] | null) {
+ const query = this.queryService.makePaginationQuery(this.chatRoomInvitationsRepository.createQueryBuilder('invitation'), sinceId, untilId)
+ .where('invitation.userId = :userId', { userId })
+ .andWhere('invitation.ignored = FALSE');
+
+ const invitations = await query.take(limit).getMany();
+
+ return invitations;
+ }
+
+ @bindThis
+ public async joinToRoom(userId: MiUser['id'], roomId: MiChatRoom['id']) {
+ const invitation = await this.chatRoomInvitationsRepository.findOneByOrFail({ roomId, userId });
+
+ const membershipsCount = await this.chatRoomMembershipsRepository.countBy({ roomId });
+ if (membershipsCount >= MAX_ROOM_MEMBERS) {
+ throw new Error('room is full');
+ }
+
+ const membership = {
+ id: this.idService.gen(),
+ roomId: roomId,
+ userId: userId,
+ } satisfies Partial<MiChatRoomMembership>;
+
+ // TODO: transaction
+ await this.chatRoomMembershipsRepository.insertOne(membership);
+ await this.chatRoomInvitationsRepository.delete(invitation.id);
+ }
+
+ @bindThis
+ public async ignoreRoomInvitation(userId: MiUser['id'], roomId: MiChatRoom['id']) {
+ const invitation = await this.chatRoomInvitationsRepository.findOneByOrFail({ roomId, userId });
+ await this.chatRoomInvitationsRepository.update(invitation.id, { ignored: true });
+ }
+
+ @bindThis
+ public async leaveRoom(userId: MiUser['id'], roomId: MiChatRoom['id']) {
+ const membership = await this.chatRoomMembershipsRepository.findOneByOrFail({ roomId, userId });
+ await this.chatRoomMembershipsRepository.delete(membership.id);
+ }
+
+ @bindThis
+ public async muteRoom(userId: MiUser['id'], roomId: MiChatRoom['id'], mute: boolean) {
+ const membership = await this.chatRoomMembershipsRepository.findOneByOrFail({ roomId, userId });
+ await this.chatRoomMembershipsRepository.update(membership.id, { isMuted: mute });
+ }
+
+ @bindThis
+ public async updateRoom(room: MiChatRoom, params: {
+ name?: string;
+ description?: string;
+ }): Promise<MiChatRoom> {
+ return this.chatRoomsRepository.createQueryBuilder().update()
+ .set(params)
+ .where('id = :id', { id: room.id })
+ .returning('*')
+ .execute()
+ .then((response) => {
+ return response.raw[0];
+ });
+ }
+
+ @bindThis
+ public async getRoomMembershipsWithPagination(roomId: MiChatRoom['id'], limit: number, sinceId?: MiChatRoomMembership['id'] | null, untilId?: MiChatRoomMembership['id'] | null) {
+ const query = this.queryService.makePaginationQuery(this.chatRoomMembershipsRepository.createQueryBuilder('membership'), sinceId, untilId)
+ .where('membership.roomId = :roomId', { roomId });
+
+ const memberships = await query.take(limit).getMany();
+
+ return memberships;
+ }
+
+ @bindThis
+ public async searchMessages(meId: MiUser['id'], query: string, limit: number, params: {
+ userId?: MiUser['id'] | null;
+ roomId?: MiChatRoom['id'] | null;
+ }) {
+ const q = this.chatMessagesRepository.createQueryBuilder('message');
+
+ if (params.userId) {
+ q.andWhere(new Brackets(qb => {
+ qb
+ .where(new Brackets(qb => {
+ qb
+ .where('message.fromUserId = :meId')
+ .andWhere('message.toUserId = :otherId');
+ }))
+ .orWhere(new Brackets(qb => {
+ qb
+ .where('message.fromUserId = :otherId')
+ .andWhere('message.toUserId = :meId');
+ }));
+ }))
+ .setParameter('meId', meId)
+ .setParameter('otherId', params.userId);
+ } else if (params.roomId) {
+ q.where('message.toRoomId = :roomId', { roomId: params.roomId });
+ } else {
+ const membershipsQuery = this.chatRoomMembershipsRepository.createQueryBuilder('membership')
+ .select('membership.roomId')
+ .where('membership.userId = :meId', { meId: meId });
+
+ const ownedRoomsQuery = this.chatRoomsRepository.createQueryBuilder('room')
+ .select('room.id')
+ .where('room.ownerId = :meId', { meId });
+
+ q.andWhere(new Brackets(qb => {
+ qb
+ .where('message.fromUserId = :meId')
+ .orWhere('message.toUserId = :meId')
+ .orWhere(`message.toRoomId IN (${membershipsQuery.getQuery()})`)
+ .orWhere(`message.toRoomId IN (${ownedRoomsQuery.getQuery()})`);
+ }));
+
+ q.setParameters(membershipsQuery.getParameters());
+ q.setParameters(ownedRoomsQuery.getParameters());
+ }
+
+ q.andWhere('LOWER(message.text) LIKE :q', { q: `%${ sqlLikeEscape(query.toLowerCase()) }%` });
+
+ q.leftJoinAndSelect('message.file', 'file');
+ q.leftJoinAndSelect('message.fromUser', 'fromUser');
+ q.leftJoinAndSelect('message.toUser', 'toUser');
+ q.leftJoinAndSelect('message.toRoom', 'toRoom');
+ q.leftJoinAndSelect('toRoom.owner', 'toRoomOwner');
+
+ const messages = await q.orderBy('message.id', 'DESC').take(limit).getMany();
+
+ return messages;
+ }
+
+ @bindThis
+ public async react(messageId: MiChatMessage['id'], userId: MiUser['id'], reaction_: string) {
+ let reaction;
+
+ // TODO: ReactionServiceのやつと共通化
+ function normalize(x: string) {
+ const match = emojiRegex.exec(x);
+ if (match) {
+ // 合字を含む1つの絵文字
+ const unicode = match[0];
+
+ // 異体字セレクタ除去
+ return unicode.match('\u200d') ? unicode : unicode.replace(/\ufe0f/g, '');
+ } else {
+ throw new Error('invalid emoji');
+ }
+ }
+
+ const custom = reaction_.match(isCustomEmojiRegexp);
+
+ if (custom == null) {
+ reaction = normalize(reaction_);
+ } else {
+ const name = custom[1];
+ const emoji = (await this.customEmojiService.localEmojisCache.fetch()).get(name);
+
+ if (emoji == null) {
+ throw new Error('no such emoji');
+ } else {
+ reaction = `:${name}:`;
+ }
+ }
+
+ const message = await this.chatMessagesRepository.findOneByOrFail({ id: messageId });
+
+ if (message.fromUserId === userId) {
+ throw new Error('cannot react to own message');
+ }
+
+ if (message.toRoomId === null && message.toUserId !== userId) {
+ throw new Error('cannot react to others message');
+ }
+
+ if (message.reactions.length >= MAX_REACTIONS_PER_MESSAGE) {
+ throw new Error('too many reactions');
+ }
+
+ const room = message.toRoomId ? await this.chatRoomsRepository.findOneByOrFail({ id: message.toRoomId }) : null;
+
+ if (room) {
+ if (!await this.isRoomMember(room, userId)) {
+ throw new Error('cannot react to others message');
+ }
+ }
+
+ await this.chatMessagesRepository.createQueryBuilder().update()
+ .set({
+ reactions: () => `array_append("reactions", '${userId}/${reaction}')`,
+ })
+ .where('id = :id', { id: message.id })
+ .execute();
+
+ if (room) {
+ this.globalEventService.publishChatRoomStream(room.id, 'react', {
+ messageId: message.id,
+ user: await this.userEntityService.pack(userId),
+ reaction,
+ });
+ } else {
+ this.globalEventService.publishChatUserStream(message.fromUserId, message.toUserId!, 'react', {
+ messageId: message.id,
+ reaction,
+ });
+ this.globalEventService.publishChatUserStream(message.toUserId!, message.fromUserId, 'react', {
+ messageId: message.id,
+ reaction,
+ });
+ }
+ }
+
+ @bindThis
+ public async getMyMemberships(userId: MiUser['id'], limit: number, sinceId?: MiChatRoomMembership['id'] | null, untilId?: MiChatRoomMembership['id'] | null) {
+ const query = this.queryService.makePaginationQuery(this.chatRoomMembershipsRepository.createQueryBuilder('membership'), sinceId, untilId)
+ .where('membership.userId = :userId', { userId });
+
+ const memberships = await query.take(limit).getMany();
+
+ return memberships;
+ }
+}
diff --git a/packages/backend/src/core/CoreModule.ts b/packages/backend/src/core/CoreModule.ts
index 997d81facc..fcae7d70dc 100644
--- a/packages/backend/src/core/CoreModule.ts
+++ b/packages/backend/src/core/CoreModule.ts
@@ -28,7 +28,6 @@ import { AppLockService } from './AppLockService.js';
import { AchievementService } from './AchievementService.js';
import { AvatarDecorationService } from './AvatarDecorationService.js';
import { CaptchaService } from './CaptchaService.js';
-import { CreateSystemUserService } from './CreateSystemUserService.js';
import { CustomEmojiService } from './CustomEmojiService.js';
import { DeleteAccountService } from './DeleteAccountService.js';
import { DownloadService } from './DownloadService.js';
@@ -41,7 +40,7 @@ import { HashtagService } from './HashtagService.js';
import { HttpRequestService } from './HttpRequestService.js';
import { IdService } from './IdService.js';
import { ImageProcessingService } from './ImageProcessingService.js';
-import { InstanceActorService } from './InstanceActorService.js';
+import { SystemAccountService } from './SystemAccountService.js';
import { InternalStorageService } from './InternalStorageService.js';
import { MetaService } from './MetaService.js';
import { MfmService } from './MfmService.js';
@@ -51,7 +50,6 @@ import { NoteEditService } from './NoteEditService.js';
import { NoteDeleteService } from './NoteDeleteService.js';
import { LatestNoteService } from './LatestNoteService.js';
import { NotePiningService } from './NotePiningService.js';
-import { NoteReadService } from './NoteReadService.js';
import { NotificationService } from './NotificationService.js';
import { PollService } from './PollService.js';
import { PushNotificationService } from './PushNotificationService.js';
@@ -75,7 +73,6 @@ import { UserSuspendService } from './UserSuspendService.js';
import { UserAuthService } from './UserAuthService.js';
import { VideoProcessingService } from './VideoProcessingService.js';
import { UserWebhookService } from './UserWebhookService.js';
-import { ProxyAccountService } from './ProxyAccountService.js';
import { UtilityService } from './UtilityService.js';
import { FileInfoService } from './FileInfoService.js';
import { SearchService } from './SearchService.js';
@@ -83,6 +80,7 @@ import { ClipService } from './ClipService.js';
import { FeaturedService } from './FeaturedService.js';
import { FanoutTimelineService } from './FanoutTimelineService.js';
import { ChannelFollowingService } from './ChannelFollowingService.js';
+import { ChatService } from './ChatService.js';
import { RegistryApiService } from './RegistryApiService.js';
import { ReversiService } from './ReversiService.js';
@@ -108,6 +106,7 @@ import { AppEntityService } from './entities/AppEntityService.js';
import { AuthSessionEntityService } from './entities/AuthSessionEntityService.js';
import { BlockingEntityService } from './entities/BlockingEntityService.js';
import { ChannelEntityService } from './entities/ChannelEntityService.js';
+import { ChatEntityService } from './entities/ChatEntityService.js';
import { ClipEntityService } from './entities/ClipEntityService.js';
import { DriveFileEntityService } from './entities/DriveFileEntityService.js';
import { DriveFolderEntityService } from './entities/DriveFolderEntityService.js';
@@ -174,7 +173,6 @@ const $AppLockService: Provider = { provide: 'AppLockService', useExisting: AppL
const $AchievementService: Provider = { provide: 'AchievementService', useExisting: AchievementService };
const $AvatarDecorationService: Provider = { provide: 'AvatarDecorationService', useExisting: AvatarDecorationService };
const $CaptchaService: Provider = { provide: 'CaptchaService', useExisting: CaptchaService };
-const $CreateSystemUserService: Provider = { provide: 'CreateSystemUserService', useExisting: CreateSystemUserService };
const $CustomEmojiService: Provider = { provide: 'CustomEmojiService', useExisting: CustomEmojiService };
const $DeleteAccountService: Provider = { provide: 'DeleteAccountService', useExisting: DeleteAccountService };
const $DownloadService: Provider = { provide: 'DownloadService', useExisting: DownloadService };
@@ -187,7 +185,6 @@ const $HashtagService: Provider = { provide: 'HashtagService', useExisting: Hash
const $HttpRequestService: Provider = { provide: 'HttpRequestService', useExisting: HttpRequestService };
const $IdService: Provider = { provide: 'IdService', useExisting: IdService };
const $ImageProcessingService: Provider = { provide: 'ImageProcessingService', useExisting: ImageProcessingService };
-const $InstanceActorService: Provider = { provide: 'InstanceActorService', useExisting: InstanceActorService };
const $InternalStorageService: Provider = { provide: 'InternalStorageService', useExisting: InternalStorageService };
const $MetaService: Provider = { provide: 'MetaService', useExisting: MetaService };
const $MfmService: Provider = { provide: 'MfmService', useExisting: MfmService };
@@ -197,10 +194,9 @@ const $NoteEditService: Provider = { provide: 'NoteEditService', useExisting: No
const $NoteDeleteService: Provider = { provide: 'NoteDeleteService', useExisting: NoteDeleteService };
const $LatestNoteService: Provider = { provide: 'LatestNoteService', useExisting: LatestNoteService };
const $NotePiningService: Provider = { provide: 'NotePiningService', useExisting: NotePiningService };
-const $NoteReadService: Provider = { provide: 'NoteReadService', useExisting: NoteReadService };
const $NotificationService: Provider = { provide: 'NotificationService', useExisting: NotificationService };
const $PollService: Provider = { provide: 'PollService', useExisting: PollService };
-const $ProxyAccountService: Provider = { provide: 'ProxyAccountService', useExisting: ProxyAccountService };
+const $SystemAccountService: Provider = { provide: 'SystemAccountService', useExisting: SystemAccountService };
const $PushNotificationService: Provider = { provide: 'PushNotificationService', useExisting: PushNotificationService };
const $QueryService: Provider = { provide: 'QueryService', useExisting: QueryService };
const $ReactionService: Provider = { provide: 'ReactionService', useExisting: ReactionService };
@@ -235,6 +231,7 @@ const $FeaturedService: Provider = { provide: 'FeaturedService', useExisting: Fe
const $FanoutTimelineService: Provider = { provide: 'FanoutTimelineService', useExisting: FanoutTimelineService };
const $FanoutTimelineEndpointService: Provider = { provide: 'FanoutTimelineEndpointService', useExisting: FanoutTimelineEndpointService };
const $ChannelFollowingService: Provider = { provide: 'ChannelFollowingService', useExisting: ChannelFollowingService };
+const $ChatService: Provider = { provide: 'ChatService', useExisting: ChatService };
const $RegistryApiService: Provider = { provide: 'RegistryApiService', useExisting: RegistryApiService };
const $ReversiService: Provider = { provide: 'ReversiService', useExisting: ReversiService };
const $TimeService: Provider = { provide: 'TimeService', useExisting: TimeService };
@@ -263,6 +260,7 @@ const $AppEntityService: Provider = { provide: 'AppEntityService', useExisting:
const $AuthSessionEntityService: Provider = { provide: 'AuthSessionEntityService', useExisting: AuthSessionEntityService };
const $BlockingEntityService: Provider = { provide: 'BlockingEntityService', useExisting: BlockingEntityService };
const $ChannelEntityService: Provider = { provide: 'ChannelEntityService', useExisting: ChannelEntityService };
+const $ChatEntityService: Provider = { provide: 'ChatEntityService', useExisting: ChatEntityService };
const $ClipEntityService: Provider = { provide: 'ClipEntityService', useExisting: ClipEntityService };
const $DriveFileEntityService: Provider = { provide: 'DriveFileEntityService', useExisting: DriveFileEntityService };
const $DriveFolderEntityService: Provider = { provide: 'DriveFolderEntityService', useExisting: DriveFolderEntityService };
@@ -333,7 +331,6 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
AchievementService,
AvatarDecorationService,
CaptchaService,
- CreateSystemUserService,
CustomEmojiService,
DeleteAccountService,
DownloadService,
@@ -346,7 +343,6 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
HttpRequestService,
IdService,
ImageProcessingService,
- InstanceActorService,
InternalStorageService,
MetaService,
MfmService,
@@ -356,10 +352,9 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
NoteDeleteService,
LatestNoteService,
NotePiningService,
- NoteReadService,
NotificationService,
PollService,
- ProxyAccountService,
+ SystemAccountService,
PushNotificationService,
QueryService,
ReactionService,
@@ -394,6 +389,7 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
FanoutTimelineService,
FanoutTimelineEndpointService,
ChannelFollowingService,
+ ChatService,
RegistryApiService,
ReversiService,
TimeService,
@@ -422,6 +418,7 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
AuthSessionEntityService,
BlockingEntityService,
ChannelEntityService,
+ ChatEntityService,
ClipEntityService,
DriveFileEntityService,
DriveFolderEntityService,
@@ -488,7 +485,6 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
$AchievementService,
$AvatarDecorationService,
$CaptchaService,
- $CreateSystemUserService,
$CustomEmojiService,
$DeleteAccountService,
$DownloadService,
@@ -501,7 +497,6 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
$HttpRequestService,
$IdService,
$ImageProcessingService,
- $InstanceActorService,
$InternalStorageService,
$MetaService,
$MfmService,
@@ -511,10 +506,9 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
$NoteDeleteService,
$LatestNoteService,
$NotePiningService,
- $NoteReadService,
$NotificationService,
$PollService,
- $ProxyAccountService,
+ $SystemAccountService,
$PushNotificationService,
$QueryService,
$ReactionService,
@@ -549,6 +543,7 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
$FanoutTimelineService,
$FanoutTimelineEndpointService,
$ChannelFollowingService,
+ $ChatService,
$RegistryApiService,
$ReversiService,
$TimeService,
@@ -577,6 +572,7 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
$AuthSessionEntityService,
$BlockingEntityService,
$ChannelEntityService,
+ $ChatEntityService,
$ClipEntityService,
$DriveFileEntityService,
$DriveFolderEntityService,
@@ -644,7 +640,6 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
AchievementService,
AvatarDecorationService,
CaptchaService,
- CreateSystemUserService,
CustomEmojiService,
DeleteAccountService,
DownloadService,
@@ -657,7 +652,6 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
HttpRequestService,
IdService,
ImageProcessingService,
- InstanceActorService,
InternalStorageService,
MetaService,
MfmService,
@@ -667,10 +661,9 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
NoteDeleteService,
LatestNoteService,
NotePiningService,
- NoteReadService,
NotificationService,
PollService,
- ProxyAccountService,
+ SystemAccountService,
PushNotificationService,
QueryService,
ReactionService,
@@ -705,6 +698,7 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
FanoutTimelineService,
FanoutTimelineEndpointService,
ChannelFollowingService,
+ ChatService,
RegistryApiService,
ReversiService,
TimeService,
@@ -732,6 +726,7 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
AuthSessionEntityService,
BlockingEntityService,
ChannelEntityService,
+ ChatEntityService,
ClipEntityService,
DriveFileEntityService,
DriveFolderEntityService,
@@ -798,7 +793,6 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
$AchievementService,
$AvatarDecorationService,
$CaptchaService,
- $CreateSystemUserService,
$CustomEmojiService,
$DeleteAccountService,
$DownloadService,
@@ -811,7 +805,6 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
$HttpRequestService,
$IdService,
$ImageProcessingService,
- $InstanceActorService,
$InternalStorageService,
$MetaService,
$MfmService,
@@ -821,10 +814,9 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
$NoteDeleteService,
$LatestNoteService,
$NotePiningService,
- $NoteReadService,
$NotificationService,
$PollService,
- $ProxyAccountService,
+ $SystemAccountService,
$PushNotificationService,
$QueryService,
$ReactionService,
@@ -858,6 +850,7 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
$FanoutTimelineService,
$FanoutTimelineEndpointService,
$ChannelFollowingService,
+ $ChatService,
$RegistryApiService,
$ReversiService,
$TimeService,
@@ -885,6 +878,7 @@ const $SponsorsService: Provider = { provide: 'SponsorsService', useExisting: Sp
$AuthSessionEntityService,
$BlockingEntityService,
$ChannelEntityService,
+ $ChatEntityService,
$ClipEntityService,
$DriveFileEntityService,
$DriveFolderEntityService,
diff --git a/packages/backend/src/core/CreateSystemUserService.ts b/packages/backend/src/core/CreateSystemUserService.ts
deleted file mode 100644
index a0aa6bad06..0000000000
--- a/packages/backend/src/core/CreateSystemUserService.ts
+++ /dev/null
@@ -1,95 +0,0 @@
-/*
- * SPDX-FileCopyrightText: syuilo and misskey-project
- * SPDX-License-Identifier: AGPL-3.0-only
- */
-
-import { randomUUID } from 'node:crypto';
-import { Inject, Injectable } from '@nestjs/common';
-import * as argon2 from 'argon2';
-//import bcrypt from 'bcryptjs';
-import { IsNull, DataSource } from 'typeorm';
-import { genRsaKeyPair } from '@/misc/gen-key-pair.js';
-import { MiUser } from '@/models/User.js';
-import { MiUserProfile } from '@/models/UserProfile.js';
-import { IdService } from '@/core/IdService.js';
-import { MiUserKeypair } from '@/models/UserKeypair.js';
-import { MiUsedUsername } from '@/models/UsedUsername.js';
-import { DI } from '@/di-symbols.js';
-import generateNativeUserToken from '@/misc/generate-native-user-token.js';
-import { bindThis } from '@/decorators.js';
-
-@Injectable()
-export class CreateSystemUserService {
- constructor(
- @Inject(DI.db)
- private db: DataSource,
-
- private idService: IdService,
- ) {
- }
-
- @bindThis
- public async createSystemUser(username: string): Promise<MiUser> {
- const password = randomUUID();
-
- // Generate hash of password
- //const salt = await bcrypt.genSalt(8);
- const hash = await argon2.hash(password);
-
- // Generate secret
- const secret = generateNativeUserToken();
-
- const keyPair = await genRsaKeyPair();
-
- let account!: MiUser;
-
- // Start transaction
- await this.db.transaction(async transactionalEntityManager => {
- const exist = await transactionalEntityManager.findOneBy(MiUser, {
- usernameLower: username.toLowerCase(),
- host: IsNull(),
- });
-
- if (exist) throw new Error('the user is already exists');
-
- account = await transactionalEntityManager.insert(MiUser, {
- id: this.idService.gen(),
- username: username,
- usernameLower: username.toLowerCase(),
- host: null,
- token: secret,
- isRoot: false,
- isLocked: true,
- isExplorable: false,
- approved: true,
- isBot: true,
- /* we always allow requests about our instance actor, because when
- a remote instance needs to check our signature on a request we
- sent, it will need to fetch information about the user that
- signed it (which is our instance actor), and if we try to check
- their signature on *that* request, we'll fetch *their* instance
- actor... leading to an infinite recursion */
- allowUnsignedFetch: 'always',
- }).then(x => transactionalEntityManager.findOneByOrFail(MiUser, x.identifiers[0]));
-
- await transactionalEntityManager.insert(MiUserKeypair, {
- publicKey: keyPair.publicKey,
- privateKey: keyPair.privateKey,
- userId: account.id,
- });
-
- await transactionalEntityManager.insert(MiUserProfile, {
- userId: account.id,
- autoAcceptFollowed: false,
- password: hash,
- });
-
- await transactionalEntityManager.insert(MiUsedUsername, {
- createdAt: new Date(),
- username: username.toLowerCase(),
- });
- });
-
- return account;
- }
-}
diff --git a/packages/backend/src/core/DeleteAccountService.ts b/packages/backend/src/core/DeleteAccountService.ts
index 8408e95863..48f27d558e 100644
--- a/packages/backend/src/core/DeleteAccountService.ts
+++ b/packages/backend/src/core/DeleteAccountService.ts
@@ -5,7 +5,7 @@
import { Inject, Injectable } from '@nestjs/common';
import { Not, IsNull } from 'typeorm';
-import type { FollowingsRepository, MiUser, UsersRepository } from '@/models/_.js';
+import type { FollowingsRepository, MiMeta, MiUser, UsersRepository } from '@/models/_.js';
import { QueueService } from '@/core/QueueService.js';
import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js';
@@ -13,11 +13,15 @@ import { GlobalEventService } from '@/core/GlobalEventService.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
import { ModerationLogService } from '@/core/ModerationLogService.js';
+import { SystemAccountService } from '@/core/SystemAccountService.js';
import { isSystemAccount } from '@/misc/is-system-account.js';
@Injectable()
export class DeleteAccountService {
constructor(
+ @Inject(DI.meta)
+ private meta: MiMeta,
+
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
@@ -29,6 +33,7 @@ export class DeleteAccountService {
private queueService: QueueService,
private globalEventService: GlobalEventService,
private moderationLogService: ModerationLogService,
+ private systemAccountService: SystemAccountService,
) {
}
@@ -37,9 +42,13 @@ export class DeleteAccountService {
id: string;
host: string | null;
}, moderator?: MiUser): Promise<void> {
+ if (this.meta.rootUserId === user.id) throw new Error('cannot delete a root account');
+
const _user = await this.usersRepository.findOneByOrFail({ id: user.id });
- if (_user.isRoot) throw new Error('cannot delete a root account');
- if (isSystemAccount(_user)) throw new Error('cannot delete a system account');
+
+ if (isSystemAccount(_user)) {
+ throw new Error('cannot delete a system account');
+ }
if (moderator != null) {
this.moderationLogService.log(moderator, 'deleteAccount', {
diff --git a/packages/backend/src/core/DownloadService.ts b/packages/backend/src/core/DownloadService.ts
index 05b9e64a37..a1a6914130 100644
--- a/packages/backend/src/core/DownloadService.ts
+++ b/packages/backend/src/core/DownloadService.ts
@@ -60,8 +60,8 @@ export class DownloadService {
request: operationTimeout, // whole operation timeout
},
agent: {
- http: this.httpRequestService.httpAgent,
- https: this.httpRequestService.httpsAgent,
+ http: this.httpRequestService.getAgentForHttp(urlObj, true),
+ https: this.httpRequestService.getAgentForHttps(urlObj, true),
},
http2: false, // default
retry: {
diff --git a/packages/backend/src/core/DriveService.ts b/packages/backend/src/core/DriveService.ts
index a65059b417..4be1b0e41b 100644
--- a/packages/backend/src/core/DriveService.ts
+++ b/packages/backend/src/core/DriveService.ts
@@ -177,7 +177,8 @@ export class DriveService {
?? `${ this.meta.objectStorageUseSSL ? 'https' : 'http' }://${ this.meta.objectStorageEndpoint }${ this.meta.objectStoragePort ? `:${this.meta.objectStoragePort}` : '' }/${ this.meta.objectStorageBucket }`;
// for original
- const key = `${this.meta.objectStoragePrefix}/${randomUUID()}${ext}`;
+ const prefix = this.meta.objectStoragePrefix ? `${this.meta.objectStoragePrefix}/` : '';
+ const key = `${prefix}${randomUUID()}${ext}`;
const url = `${ baseUrl }/${ key }`;
// for alts
@@ -194,7 +195,7 @@ export class DriveService {
];
if (alts.webpublic) {
- webpublicKey = `${this.meta.objectStoragePrefix}/webpublic-${randomUUID()}.${alts.webpublic.ext}`;
+ webpublicKey = `${prefix}webpublic-${randomUUID()}.${alts.webpublic.ext}`;
webpublicUrl = `${ baseUrl }/${ webpublicKey }`;
this.registerLogger.info(`uploading webpublic: ${webpublicKey}`);
@@ -202,7 +203,7 @@ export class DriveService {
}
if (alts.thumbnail) {
- thumbnailKey = `${this.meta.objectStoragePrefix}/thumbnail-${randomUUID()}.${alts.thumbnail.ext}`;
+ thumbnailKey = `${prefix}thumbnail-${randomUUID()}.${alts.thumbnail.ext}`;
thumbnailUrl = `${ baseUrl }/${ thumbnailKey }`;
this.registerLogger.info(`uploading thumbnail: ${thumbnailKey}`);
diff --git a/packages/backend/src/core/EmailService.ts b/packages/backend/src/core/EmailService.ts
index da198d0e42..45d7ea11e4 100644
--- a/packages/backend/src/core/EmailService.ts
+++ b/packages/backend/src/core/EmailService.ts
@@ -164,6 +164,13 @@ export class EmailService {
available: boolean;
reason: null | 'used' | 'format' | 'disposable' | 'mx' | 'smtp' | 'banned' | 'network' | 'blacklist';
}> {
+ if (!this.utilityService.validateEmailFormat(emailAddress)) {
+ return {
+ available: false,
+ reason: 'format',
+ };
+ }
+
const exist = await this.userProfilesRepository.countBy({
emailVerified: true,
email: emailAddress,
diff --git a/packages/backend/src/core/FanoutTimelineService.ts b/packages/backend/src/core/FanoutTimelineService.ts
index f6dabfadcd..24999bf4da 100644
--- a/packages/backend/src/core/FanoutTimelineService.ts
+++ b/packages/backend/src/core/FanoutTimelineService.ts
@@ -9,7 +9,7 @@ import { DI } from '@/di-symbols.js';
import { bindThis } from '@/decorators.js';
import { IdService } from '@/core/IdService.js';
-export type FanoutTimelineName =
+export type FanoutTimelineName = (
// home timeline
| `homeTimeline:${string}`
| `homeTimelineWithFiles:${string}` // only notes with files are included
@@ -37,6 +37,7 @@ export type FanoutTimelineName =
// role timelines
| `roleTimeline:${string}` // any notes are included
+);
@Injectable()
export class FanoutTimelineService {
diff --git a/packages/backend/src/core/GlobalEventService.ts b/packages/backend/src/core/GlobalEventService.ts
index 211c22bfaf..94d6311e0d 100644
--- a/packages/backend/src/core/GlobalEventService.ts
+++ b/packages/backend/src/core/GlobalEventService.ts
@@ -20,7 +20,7 @@ import type { MiPage } from '@/models/Page.js';
import type { MiWebhook } from '@/models/Webhook.js';
import type { MiSystemWebhook } from '@/models/SystemWebhook.js';
import type { MiMeta } from '@/models/Meta.js';
-import { MiAvatarDecoration, MiReversiGame, MiRole, MiRoleAssignment } from '@/models/_.js';
+import { MiAvatarDecoration, MiChatMessage, MiChatRoom, MiReversiGame, MiRole, MiRoleAssignment } from '@/models/_.js';
import type { Packed } from '@/misc/json-schema.js';
import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
@@ -72,12 +72,8 @@ export interface MainEventTypes {
readAllNotifications: undefined;
notificationFlushed: undefined;
unreadNotification: Packed<'Notification'>;
- unreadMention: MiNote['id'];
- readAllUnreadMentions: undefined;
- unreadSpecifiedNote: MiNote['id'];
- readAllUnreadSpecifiedNotes: undefined;
- readAllAntennas: undefined;
unreadAntenna: MiAntenna;
+ newChatMessage: Packed<'ChatMessage'>;
readAllAnnouncements: undefined;
myTokenRegenerated: undefined;
signin: {
@@ -168,6 +164,16 @@ export interface AdminEventTypes {
};
}
+export interface ChatEventTypes {
+ message: Packed<'ChatMessageLite'>;
+ deleted: Packed<'ChatMessageLite'>['id'];
+ react: {
+ reaction: string;
+ user?: Packed<'UserLite'>;
+ messageId: MiChatMessage['id'];
+ };
+}
+
export interface ReversiEventTypes {
matched: {
game: Packed<'ReversiGameDetailed'>;
@@ -207,7 +213,7 @@ export interface ReversiGameEventTypes {
type Events<T extends object> = { [K in keyof T]: { type: K; body: T[K]; } };
type EventUnionFromDictionary<
T extends object,
- U = Events<T>
+ U = Events<T>,
> = U[keyof U];
type SerializedAll<T> = {
@@ -216,7 +222,7 @@ type SerializedAll<T> = {
type UndefinedAsNullAll<T> = {
[K in keyof T]: T[K] extends undefined ? null : T[K];
-}
+};
export interface InternalEventTypes {
userChangeSuspendedState: { id: MiUser['id']; isSuspended: MiUser['isSuspended']; };
@@ -300,6 +306,14 @@ export type GlobalEvents = {
name: 'notesStream';
payload: Serialized<Packed<'Note'>>;
};
+ chat: {
+ name: `chatUserStream:${MiUser['id']}-${MiUser['id']}`;
+ payload: EventTypesToEventPayload<ChatEventTypes>;
+ };
+ chatRoom: {
+ name: `chatRoomStream:${MiChatRoom['id']}`;
+ payload: EventTypesToEventPayload<ChatEventTypes>;
+ };
reversi: {
name: `reversiStream:${MiUser['id']}`;
payload: EventTypesToEventPayload<ReversiEventTypes>;
@@ -399,6 +413,16 @@ export class GlobalEventService {
}
@bindThis
+ public publishChatUserStream<K extends keyof ChatEventTypes>(fromUserId: MiUser['id'], toUserId: MiUser['id'], type: K, value?: ChatEventTypes[K]): void {
+ this.publish(`chatUserStream:${fromUserId}-${toUserId}`, type, typeof value === 'undefined' ? null : value);
+ }
+
+ @bindThis
+ public publishChatRoomStream<K extends keyof ChatEventTypes>(toRoomId: MiChatRoom['id'], type: K, value?: ChatEventTypes[K]): void {
+ this.publish(`chatRoomStream:${toRoomId}`, type, typeof value === 'undefined' ? null : value);
+ }
+
+ @bindThis
public publishReversiStream<K extends keyof ReversiEventTypes>(userId: MiUser['id'], type: K, value?: ReversiEventTypes[K]): void {
this.publish(`reversiStream:${userId}`, type, typeof value === 'undefined' ? null : value);
}
diff --git a/packages/backend/src/core/HttpRequestService.ts b/packages/backend/src/core/HttpRequestService.ts
index 1aa62a9879..e8b0326e66 100644
--- a/packages/backend/src/core/HttpRequestService.ts
+++ b/packages/backend/src/core/HttpRequestService.ts
@@ -16,6 +16,7 @@ import type { Config } from '@/config.js';
import { StatusError } from '@/misc/status-error.js';
import { bindThis } from '@/decorators.js';
import { validateContentTypeSetAsActivityPub } from '@/core/activitypub/misc/validator.js';
+import { FetchAllowSoftFailMask } from '@/core/activitypub/misc/check-against-url.js';
import type { IObject, IObjectWithId } from '@/core/activitypub/type.js';
import { ApUtilityService } from './activitypub/ApUtilityService.js';
import type { Response } from 'node-fetch';
@@ -115,32 +116,32 @@ export class HttpRequestService {
/**
* Get http non-proxy agent (without local address filtering)
*/
- private httpNative: http.Agent;
+ private readonly httpNative: http.Agent;
/**
* Get https non-proxy agent (without local address filtering)
*/
- private httpsNative: https.Agent;
+ private readonly httpsNative: https.Agent;
/**
* Get http non-proxy agent
*/
- private http: http.Agent;
+ private readonly http: http.Agent;
/**
* Get https non-proxy agent
*/
- private https: https.Agent;
+ private readonly https: https.Agent;
/**
* Get http proxy or non-proxy agent
*/
- public httpAgent: http.Agent;
+ public readonly httpAgent: http.Agent;
/**
* Get https proxy or non-proxy agent
*/
- public httpsAgent: https.Agent;
+ public readonly httpsAgent: https.Agent;
constructor(
@Inject(DI.config)
@@ -198,7 +199,7 @@ export class HttpRequestService {
/**
* Get agent by URL
* @param url URL
- * @param bypassProxy Allways bypass proxy
+ * @param bypassProxy Always bypass proxy
* @param isLocalAddressAllowed
*/
@bindThis
@@ -216,8 +217,40 @@ export class HttpRequestService {
}
}
+ /**
+ * Get agent for http by URL
+ * @param url URL
+ * @param isLocalAddressAllowed
+ */
+ @bindThis
+ public getAgentForHttp(url: URL, isLocalAddressAllowed = false): http.Agent {
+ if ((this.config.proxyBypassHosts ?? []).includes(url.hostname)) {
+ return isLocalAddressAllowed
+ ? this.httpNative
+ : this.http;
+ } else {
+ return this.httpAgent;
+ }
+ }
+
+ /**
+ * Get agent for https by URL
+ * @param url URL
+ * @param isLocalAddressAllowed
+ */
+ @bindThis
+ public getAgentForHttps(url: URL, isLocalAddressAllowed = false): https.Agent {
+ if ((this.config.proxyBypassHosts ?? []).includes(url.hostname)) {
+ return isLocalAddressAllowed
+ ? this.httpsNative
+ : this.https;
+ } else {
+ return this.httpsAgent;
+ }
+ }
+
@bindThis
- public async getActivityJson(url: string, isLocalAddressAllowed = false): Promise<IObjectWithId> {
+ public async getActivityJson(url: string, isLocalAddressAllowed = false, allowSoftfail: FetchAllowSoftFailMask = FetchAllowSoftFailMask.Strict): Promise<IObjectWithId> {
const res = await this.send(url, {
method: 'GET',
headers: {
@@ -235,7 +268,7 @@ export class HttpRequestService {
// Make sure the object ID matches the final URL (which is where it actually exists).
// The caller (ApResolverService) will verify the ID against the original / entry URL, which ensures that all three match.
- this.apUtilityService.assertIdMatchesUrlAuthority(activity, res.url);
+ this.apUtilityService.assertIdMatchesUrlAuthority(activity, res.url, allowSoftfail);
return activity as IObjectWithId;
}
diff --git a/packages/backend/src/core/InstanceActorService.ts b/packages/backend/src/core/InstanceActorService.ts
deleted file mode 100644
index 22c47297a3..0000000000
--- a/packages/backend/src/core/InstanceActorService.ts
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * SPDX-FileCopyrightText: syuilo and misskey-project
- * SPDX-License-Identifier: AGPL-3.0-only
- */
-
-import { Inject, Injectable } from '@nestjs/common';
-import { IsNull, Not } from 'typeorm';
-import type { MiLocalUser } from '@/models/User.js';
-import type { UsersRepository } from '@/models/_.js';
-import { MemorySingleCache } from '@/misc/cache.js';
-import { DI } from '@/di-symbols.js';
-import { CreateSystemUserService } from '@/core/CreateSystemUserService.js';
-import { bindThis } from '@/decorators.js';
-
-const ACTOR_USERNAME = 'instance.actor' as const;
-
-@Injectable()
-export class InstanceActorService {
- private cache: MemorySingleCache<MiLocalUser>;
-
- constructor(
- @Inject(DI.usersRepository)
- private usersRepository: UsersRepository,
-
- private createSystemUserService: CreateSystemUserService,
- ) {
- this.cache = new MemorySingleCache<MiLocalUser>(Infinity);
- }
-
- @bindThis
- public async realLocalUsersPresent(): Promise<boolean> {
- return await this.usersRepository.existsBy({
- host: IsNull(),
- username: Not(ACTOR_USERNAME),
- });
- }
-
- @bindThis
- public async getInstanceActor(): Promise<MiLocalUser> {
- const cached = this.cache.get();
- if (cached) return cached;
-
- const user = await this.usersRepository.findOneBy({
- host: IsNull(),
- username: ACTOR_USERNAME,
- }) as MiLocalUser | undefined;
-
- if (user) {
- this.cache.set(user);
- return user;
- } else {
- const created = await this.createSystemUserService.createSystemUser(ACTOR_USERNAME) as MiLocalUser;
- this.cache.set(created);
- return created;
- }
- }
-}
diff --git a/packages/backend/src/core/MetaService.ts b/packages/backend/src/core/MetaService.ts
index 3d88d0aefe..40e7439f5f 100644
--- a/packages/backend/src/core/MetaService.ts
+++ b/packages/backend/src/core/MetaService.ts
@@ -53,7 +53,7 @@ export class MetaService implements OnApplicationShutdown {
case 'metaUpdated': {
this.cache = { // TODO: このあたりのデシリアライズ処理は各modelファイル内に関数としてexportしたい
...(body.after),
- proxyAccount: null, // joinなカラムは通常取ってこないので
+ rootUser: null, // joinなカラムは通常取ってこないので
};
break;
}
@@ -113,17 +113,20 @@ export class MetaService implements OnApplicationShutdown {
if (before) {
await transactionalEntityManager.update(MiMeta, before.id, data);
-
- const metas = await transactionalEntityManager.find(MiMeta, {
- order: {
- id: 'DESC',
- },
- });
-
- return metas[0];
} else {
- return await transactionalEntityManager.save(MiMeta, data);
+ await transactionalEntityManager.save(MiMeta, {
+ ...data,
+ id: 'x',
+ });
}
+
+ const afters = await transactionalEntityManager.find(MiMeta, {
+ order: {
+ id: 'DESC',
+ },
+ });
+
+ return afters[0];
});
if (data.hiddenTags) {
diff --git a/packages/backend/src/core/MfmService.ts b/packages/backend/src/core/MfmService.ts
index 6c2f673217..ad646d3ff4 100644
--- a/packages/backend/src/core/MfmService.ts
+++ b/packages/backend/src/core/MfmService.ts
@@ -576,7 +576,8 @@ export class MfmService {
appendChildren(nodes, body);
- const serialized = new XMLSerializer().serializeToString(body);
+ // Remove the unnecessary namespace
+ const serialized = new XMLSerializer().serializeToString(body).replace(/^\s*<p xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">/, '<p>');
happyDOM.close().catch(err => {});
diff --git a/packages/backend/src/core/NoteCreateService.ts b/packages/backend/src/core/NoteCreateService.ts
index df31cb4247..b75b4ecd21 100644
--- a/packages/backend/src/core/NoteCreateService.ts
+++ b/packages/backend/src/core/NoteCreateService.ts
@@ -42,7 +42,6 @@ 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 { RoleService } from '@/core/RoleService.js';
@@ -203,7 +202,6 @@ export class NoteCreateService implements OnApplicationShutdown {
private globalEventService: GlobalEventService,
private queueService: QueueService,
private fanoutTimelineService: FanoutTimelineService,
- private noteReadService: NoteReadService,
private notificationService: NotificationService,
private relayService: RelayService,
private federatedInstanceService: FederatedInstanceService,
@@ -651,31 +649,6 @@ export class NoteCreateService implements OnApplicationShutdown {
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, null, { skipHide: true, withReactionAndUserPairCache: true });
diff --git a/packages/backend/src/core/NoteDeleteService.ts b/packages/backend/src/core/NoteDeleteService.ts
index 1f94e65809..8ec05c88dc 100644
--- a/packages/backend/src/core/NoteDeleteService.ts
+++ b/packages/backend/src/core/NoteDeleteService.ts
@@ -3,7 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
-import { Brackets, In } from 'typeorm';
+import { Brackets, In, IsNull, Not } from 'typeorm';
import { Injectable, Inject } from '@nestjs/common';
import type { MiUser, MiLocalUser, MiRemoteUser } from '@/models/User.js';
import { MiNote, IMentionedRemoteUsers } from '@/models/Note.js';
@@ -232,12 +232,26 @@ export class NoteDeleteService {
}
@bindThis
+ private async getRenotedOrRepliedRemoteUsers(note: MiNote) {
+ const query = this.notesRepository.createQueryBuilder('note')
+ .leftJoinAndSelect('note.user', 'user')
+ .where(new Brackets(qb => {
+ qb.orWhere('note.renoteId = :renoteId', { renoteId: note.id });
+ qb.orWhere('note.replyId = :replyId', { replyId: note.id });
+ }))
+ .andWhere({ userHost: Not(IsNull()) });
+ const notes = await query.getMany() as (MiNote & { user: MiRemoteUser })[];
+ const remoteUsers = notes.map(({ user }) => user);
+ return remoteUsers;
+ }
+
+ @bindThis
private async deliverToConcerned(user: { id: MiLocalUser['id']; host: null; }, note: MiNote, content: any) {
this.apDeliverManagerService.deliverToFollowers(user, content);
this.relayService.deliverToRelays(user, content);
- const remoteUsers = await this.getMentionedRemoteUsers(note);
- for (const remoteUser of remoteUsers) {
- this.apDeliverManagerService.deliverToUser(user, content, remoteUser);
- }
+ this.apDeliverManagerService.deliverToUsers(user, content, [
+ ...await this.getMentionedRemoteUsers(note),
+ ...await this.getRenotedOrRepliedRemoteUsers(note),
+ ]);
}
}
diff --git a/packages/backend/src/core/NoteReadService.ts b/packages/backend/src/core/NoteReadService.ts
deleted file mode 100644
index 320b23cc1a..0000000000
--- a/packages/backend/src/core/NoteReadService.ts
+++ /dev/null
@@ -1,147 +0,0 @@
-/*
- * SPDX-FileCopyrightText: syuilo and misskey-project
- * SPDX-License-Identifier: AGPL-3.0-only
- */
-
-import { setTimeout } from 'node:timers/promises';
-import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
-import { In } from 'typeorm';
-import { DI } from '@/di-symbols.js';
-import type { MiUser } from '@/models/User.js';
-import type { Packed } from '@/misc/json-schema.js';
-import type { MiNote } from '@/models/Note.js';
-import { IdService } from '@/core/IdService.js';
-import { GlobalEventService } from '@/core/GlobalEventService.js';
-import type { NoteUnreadsRepository, MutingsRepository, NoteThreadMutingsRepository } from '@/models/_.js';
-import { bindThis } from '@/decorators.js';
-import { trackPromise } from '@/misc/promise-tracker.js';
-
-@Injectable()
-export class NoteReadService implements OnApplicationShutdown {
- #shutdownController = new AbortController();
-
- constructor(
- @Inject(DI.noteUnreadsRepository)
- private noteUnreadsRepository: NoteUnreadsRepository,
-
- @Inject(DI.mutingsRepository)
- private mutingsRepository: MutingsRepository,
-
- @Inject(DI.noteThreadMutingsRepository)
- private noteThreadMutingsRepository: NoteThreadMutingsRepository,
-
- private idService: IdService,
- private globalEventService: GlobalEventService,
- ) {
- }
-
- @bindThis
- public async insertNoteUnread(userId: MiUser['id'], note: MiNote, params: {
- // NOTE: isSpecifiedがtrueならisMentionedは必ずfalse
- isSpecified: boolean;
- isMentioned: boolean;
- }): Promise<void> {
- //#region ミュートしているなら無視
- const mute = await this.mutingsRepository.findBy({
- muterId: userId,
- });
- if (mute.map(m => m.muteeId).includes(note.userId)) return;
- //#endregion
-
- // スレッドミュート
- const isThreadMuted = await this.noteThreadMutingsRepository.exists({
- where: {
- userId: userId,
- threadId: note.threadId ?? note.id,
- },
- });
- if (isThreadMuted) return;
-
- const unread = {
- id: this.idService.gen(),
- noteId: note.id,
- userId: userId,
- isSpecified: params.isSpecified,
- isMentioned: params.isMentioned,
- noteUserId: note.userId,
- };
-
- /* we may be called from NoteEditService, for a note that's
- already present in the `note_unread` table: `upsert` makes sure
- we don't throw a "duplicate key" error, while still updating
- the other columns if they've changed */
- await this.noteUnreadsRepository.upsert(unread, ['userId', 'noteId']);
-
- // 2秒経っても既読にならなかったら「未読の投稿がありますよ」イベントを発行する
- setTimeout(2000, 'unread note', { signal: this.#shutdownController.signal }).then(async () => {
- const exist = await this.noteUnreadsRepository.exists({ where: { id: unread.id } });
-
- if (!exist) return;
-
- if (params.isMentioned) {
- this.globalEventService.publishMainStream(userId, 'unreadMention', note.id);
- }
- if (params.isSpecified) {
- this.globalEventService.publishMainStream(userId, 'unreadSpecifiedNote', note.id);
- }
- }, () => { /* aborted, ignore it */ });
- }
-
- @bindThis
- public async read(
- userId: MiUser['id'],
- notes: (MiNote | Packed<'Note'>)[],
- ): Promise<void> {
- if (notes.length === 0) return;
-
- const noteIds = new Set<MiNote['id']>();
-
- for (const note of notes) {
- if (note.mentions && note.mentions.includes(userId)) {
- noteIds.add(note.id);
- } else if (note.visibleUserIds && note.visibleUserIds.includes(userId)) {
- noteIds.add(note.id);
- }
- }
-
- if (noteIds.size === 0) return;
-
- // Remove the record
- await this.noteUnreadsRepository.delete({
- userId: userId,
- noteId: In(Array.from(noteIds)),
- });
-
- // TODO: ↓まとめてクエリしたい
-
- trackPromise(this.noteUnreadsRepository.countBy({
- userId: userId,
- isMentioned: true,
- }).then(mentionsCount => {
- if (mentionsCount === 0) {
- // 全て既読になったイベントを発行
- this.globalEventService.publishMainStream(userId, 'readAllUnreadMentions');
- }
- }));
-
- trackPromise(this.noteUnreadsRepository.countBy({
- userId: userId,
- isSpecified: true,
- }).then(specifiedCount => {
- if (specifiedCount === 0) {
- // 全て既読になったイベントを発行
- this.globalEventService.publishMainStream(userId, 'readAllUnreadSpecifiedNotes');
- }
- }));
- }
-
- @bindThis
- public dispose(): void {
- this.#shutdownController.abort();
- }
-
- @bindThis
- public onApplicationShutdown(signal?: string | undefined): void {
- this.dispose();
- }
-}
diff --git a/packages/backend/src/core/ProxyAccountService.ts b/packages/backend/src/core/ProxyAccountService.ts
deleted file mode 100644
index c3ff2a68d3..0000000000
--- a/packages/backend/src/core/ProxyAccountService.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * SPDX-FileCopyrightText: syuilo and misskey-project
- * SPDX-License-Identifier: AGPL-3.0-only
- */
-
-import { Inject, Injectable } from '@nestjs/common';
-import type { MiMeta, UsersRepository } from '@/models/_.js';
-import type { MiLocalUser } from '@/models/User.js';
-import { DI } from '@/di-symbols.js';
-import { bindThis } from '@/decorators.js';
-
-@Injectable()
-export class ProxyAccountService {
- constructor(
- @Inject(DI.meta)
- private meta: MiMeta,
-
- @Inject(DI.usersRepository)
- private usersRepository: UsersRepository,
- ) {
- }
-
- @bindThis
- public async fetch(): Promise<MiLocalUser | null> {
- if (this.meta.proxyAccountId == null) return null;
- return await this.usersRepository.findOneByOrFail({ id: this.meta.proxyAccountId }) as MiLocalUser;
- }
-}
diff --git a/packages/backend/src/core/RelayService.ts b/packages/backend/src/core/RelayService.ts
index db32114346..9120de1f9f 100644
--- a/packages/backend/src/core/RelayService.ts
+++ b/packages/backend/src/core/RelayService.ts
@@ -4,54 +4,35 @@
*/
import { Inject, Injectable } from '@nestjs/common';
-import { IsNull } from 'typeorm';
-import type { MiLocalUser, MiUser } from '@/models/User.js';
-import type { RelaysRepository, UsersRepository } from '@/models/_.js';
+import type { MiUser } from '@/models/User.js';
+import type { RelaysRepository } from '@/models/_.js';
import { IdService } from '@/core/IdService.js';
import { MemorySingleCache } from '@/misc/cache.js';
import type { MiRelay } from '@/models/Relay.js';
import { QueueService } from '@/core/QueueService.js';
-import { CreateSystemUserService } from '@/core/CreateSystemUserService.js';
import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
import { DI } from '@/di-symbols.js';
import { deepClone } from '@/misc/clone.js';
import { bindThis } from '@/decorators.js';
-
-const ACTOR_USERNAME = 'relay.actor' as const;
+import { SystemAccountService } from '@/core/SystemAccountService.js';
@Injectable()
export class RelayService {
private relaysCache: MemorySingleCache<MiRelay[]>;
constructor(
- @Inject(DI.usersRepository)
- private usersRepository: UsersRepository,
-
@Inject(DI.relaysRepository)
private relaysRepository: RelaysRepository,
private idService: IdService,
private queueService: QueueService,
- private createSystemUserService: CreateSystemUserService,
+ private systemAccountService: SystemAccountService,
private apRendererService: ApRendererService,
) {
this.relaysCache = new MemorySingleCache<MiRelay[]>(1000 * 60 * 10); // 10m
}
@bindThis
- private async getRelayActor(): Promise<MiLocalUser> {
- const user = await this.usersRepository.findOneBy({
- host: IsNull(),
- username: ACTOR_USERNAME,
- });
-
- if (user) return user as MiLocalUser;
-
- const created = await this.createSystemUserService.createSystemUser(ACTOR_USERNAME);
- return created as MiLocalUser;
- }
-
- @bindThis
public async addRelay(inbox: string): Promise<MiRelay> {
const relay = await this.relaysRepository.insertOne({
id: this.idService.gen(),
@@ -59,8 +40,8 @@ export class RelayService {
status: 'requesting',
});
- const relayActor = await this.getRelayActor();
- const follow = await this.apRendererService.renderFollowRelay(relay, relayActor);
+ const relayActor = await this.systemAccountService.fetch('relay');
+ const follow = this.apRendererService.renderFollowRelay(relay, relayActor);
const activity = this.apRendererService.addContext(follow);
this.queueService.deliver(relayActor, activity, relay.inbox, false);
@@ -77,7 +58,7 @@ export class RelayService {
throw new Error('relay not found');
}
- const relayActor = await this.getRelayActor();
+ const relayActor = await this.systemAccountService.fetch('relay');
const follow = this.apRendererService.renderFollowRelay(relay, relayActor);
const undo = this.apRendererService.renderUndo(follow, relayActor);
const activity = this.apRendererService.addContext(undo);
diff --git a/packages/backend/src/core/RemoteUserResolveService.ts b/packages/backend/src/core/RemoteUserResolveService.ts
index 098b5e1706..a2f1b73cdb 100644
--- a/packages/backend/src/core/RemoteUserResolveService.ts
+++ b/packages/backend/src/core/RemoteUserResolveService.ts
@@ -74,7 +74,7 @@ export class RemoteUserResolveService {
if (user == null) {
const self = await this.resolveSelf(acctLower);
- if (self.href.startsWith(this.config.url)) {
+ if (this.utilityService.isUriLocal(self.href)) {
const local = this.apDbResolverService.parseUri(self.href);
if (local.local && local.type === 'users') {
// the LR points to local
diff --git a/packages/backend/src/core/RoleService.ts b/packages/backend/src/core/RoleService.ts
index 0bae3af385..6e4346e22d 100644
--- a/packages/backend/src/core/RoleService.ts
+++ b/packages/backend/src/core/RoleService.ts
@@ -66,6 +66,7 @@ export type RolePolicies = {
canImportFollowing: boolean;
canImportMuting: boolean;
canImportUserLists: boolean;
+ canChat: boolean;
};
export const DEFAULT_POLICIES: RolePolicies = {
@@ -103,11 +104,11 @@ export const DEFAULT_POLICIES: RolePolicies = {
canImportFollowing: true,
canImportMuting: true,
canImportUserLists: true,
+ canChat: true,
};
@Injectable()
export class RoleService implements OnApplicationShutdown, OnModuleInit {
- private rootUserIdCache: MemorySingleCache<MiUser['id']>;
private rolesCache: MemorySingleCache<MiRole[]>;
private roleAssignmentByUserIdCache: MemoryKVCache<MiRoleAssignment[]>;
private notificationService: NotificationService;
@@ -143,7 +144,6 @@ export class RoleService implements OnApplicationShutdown, OnModuleInit {
private moderationLogService: ModerationLogService,
private fanoutTimelineService: FanoutTimelineService,
) {
- this.rootUserIdCache = new MemorySingleCache<MiUser['id']>(1000 * 60 * 60 * 24 * 7); // 1week. rootユーザのIDは不変なので長めに
this.rolesCache = new MemorySingleCache<MiRole[]>(1000 * 60 * 60); // 1h
this.roleAssignmentByUserIdCache = new MemoryKVCache<MiRoleAssignment[]>(1000 * 60 * 5); // 5m
@@ -411,19 +411,20 @@ export class RoleService implements OnApplicationShutdown, OnModuleInit {
canImportFollowing: calc('canImportFollowing', vs => vs.some(v => v === true)),
canImportMuting: calc('canImportMuting', vs => vs.some(v => v === true)),
canImportUserLists: calc('canImportUserLists', vs => vs.some(v => v === true)),
+ canChat: calc('canChat', vs => vs.some(v => v === true)),
};
}
@bindThis
- public async isModerator(user: { id: MiUser['id']; isRoot: MiUser['isRoot'] } | null): Promise<boolean> {
+ public async isModerator(user: { id: MiUser['id'] } | null): Promise<boolean> {
if (user == null) return false;
- return user.isRoot || (await this.getUserRoles(user.id)).some(r => r.isModerator || r.isAdministrator);
+ return (this.meta.rootUserId === user.id) || (await this.getUserRoles(user.id)).some(r => r.isModerator || r.isAdministrator);
}
@bindThis
- public async isAdministrator(user: { id: MiUser['id']; isRoot: MiUser['isRoot'] } | null): Promise<boolean> {
+ public async isAdministrator(user: { id: MiUser['id'] } | null): Promise<boolean> {
if (user == null) return false;
- return user.isRoot || (await this.getUserRoles(user.id)).some(r => r.isAdministrator);
+ return (this.meta.rootUserId === user.id) || (await this.getUserRoles(user.id)).some(r => r.isAdministrator);
}
@bindThis
@@ -472,16 +473,8 @@ export class RoleService implements OnApplicationShutdown, OnModuleInit {
.map(a => a.userId),
);
- if (includeRoot) {
- const rootUserId = await this.rootUserIdCache.fetch(async () => {
- const it = await this.usersRepository.createQueryBuilder('users')
- .select('id')
- .where({ isRoot: true })
- .getRawOne<{ id: string }>();
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- return it!.id;
- });
- resultSet.add(rootUserId);
+ if (includeRoot && this.meta.rootUserId) {
+ resultSet.add(this.meta.rootUserId);
}
return [...resultSet].sort((x, y) => x.localeCompare(y));
diff --git a/packages/backend/src/core/S3Service.ts b/packages/backend/src/core/S3Service.ts
index 37721d2bf1..968a5dcc0b 100644
--- a/packages/backend/src/core/S3Service.ts
+++ b/packages/backend/src/core/S3Service.ts
@@ -46,6 +46,8 @@ export class S3Service {
tls: meta.objectStorageUseSSL,
forcePathStyle: meta.objectStorageEndpoint ? meta.objectStorageS3ForcePathStyle : false, // AWS with endPoint omitted
requestHandler: new NodeHttpHandler(handlerOption),
+ requestChecksumCalculation: 'WHEN_REQUIRED',
+ responseChecksumValidation: 'WHEN_REQUIRED',
});
}
diff --git a/packages/backend/src/core/SignupService.ts b/packages/backend/src/core/SignupService.ts
index 9fc0c2b34a..f1dd0f0503 100644
--- a/packages/backend/src/core/SignupService.ts
+++ b/packages/backend/src/core/SignupService.ts
@@ -15,13 +15,14 @@ import { MiUserProfile } from '@/models/UserProfile.js';
import { IdService } from '@/core/IdService.js';
import { MiUserKeypair } from '@/models/UserKeypair.js';
import { MiUsedUsername } from '@/models/UsedUsername.js';
-import generateUserToken from '@/misc/generate-native-user-token.js';
+import { generateNativeUserToken } from '@/misc/token.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
-import { InstanceActorService } from '@/core/InstanceActorService.js';
import { bindThis } from '@/decorators.js';
import UsersChart from '@/core/chart/charts/users.js';
import { UtilityService } from '@/core/UtilityService.js';
import { UserService } from '@/core/UserService.js';
+import { SystemAccountService } from '@/core/SystemAccountService.js';
+import { MetaService } from '@/core/MetaService.js';
@Injectable()
export class SignupService {
@@ -42,7 +43,8 @@ export class SignupService {
private userService: UserService,
private userEntityService: UserEntityService,
private idService: IdService,
- private instanceActorService: InstanceActorService,
+ private systemAccountService: SystemAccountService,
+ private metaService: MetaService,
private usersChart: UsersChart,
) {
}
@@ -77,7 +79,7 @@ export class SignupService {
}
// Generate secret
- const secret = generateUserToken();
+ const secret = generateNativeUserToken();
// Check username duplication
if (await this.usersRepository.exists({ where: { usernameLower: username.toLowerCase(), host: IsNull() } })) {
@@ -89,9 +91,7 @@ export class SignupService {
throw new Error('USED_USERNAME');
}
- const isTheFirstUser = !await this.instanceActorService.realLocalUsersPresent();
-
- if (!opts.ignorePreservedUsernames && !isTheFirstUser) {
+ if (!opts.ignorePreservedUsernames && this.meta.rootUserId != null) {
const isPreserved = this.meta.preservedUsernames.map(x => x.toLowerCase()).includes(username.toLowerCase());
if (isPreserved) {
throw new Error('USED_USERNAME');
@@ -132,8 +132,7 @@ export class SignupService {
usernameLower: username.toLowerCase(),
host: this.utilityService.toPunyNullable(host),
token: secret,
- isRoot: isTheFirstUser,
- approved: isTheFirstUser || (opts.approved ?? !this.meta.approvalRequiredForSignup),
+ approved: opts.approved ?? !this.meta.approvalRequiredForSignup,
signupReason: reason,
enableRss: false,
}));
@@ -159,6 +158,10 @@ export class SignupService {
this.usersChart.update(account, true);
this.userService.notifySystemWebhook(account, 'userCreated');
+ if (this.meta.rootUserId == null) {
+ await this.metaService.update({ rootUserId: account.id });
+ }
+
return { account, secret };
}
}
diff --git a/packages/backend/src/core/SystemAccountService.ts b/packages/backend/src/core/SystemAccountService.ts
new file mode 100644
index 0000000000..1e050c3054
--- /dev/null
+++ b/packages/backend/src/core/SystemAccountService.ts
@@ -0,0 +1,172 @@
+/*
+ * SPDX-FileCopyrightText: syuilo and misskey-project
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+import { randomUUID } from 'node:crypto';
+import { Inject, Injectable } from '@nestjs/common';
+import { DataSource, IsNull } from 'typeorm';
+import bcrypt from 'bcryptjs';
+import { MiLocalUser, MiUser } from '@/models/User.js';
+import { MiSystemAccount, MiUsedUsername, MiUserKeypair, MiUserProfile, type UsersRepository, type SystemAccountsRepository } from '@/models/_.js';
+import type { MiMeta, UserProfilesRepository } from '@/models/_.js';
+import { MemoryKVCache } from '@/misc/cache.js';
+import { DI } from '@/di-symbols.js';
+import { bindThis } from '@/decorators.js';
+import { generateNativeUserToken } from '@/misc/token.js';
+import { IdService } from '@/core/IdService.js';
+import { genRsaKeyPair } from '@/misc/gen-key-pair.js';
+
+export const SYSTEM_ACCOUNT_TYPES = ['actor', 'relay', 'proxy'] as const;
+
+@Injectable()
+export class SystemAccountService {
+ private cache: MemoryKVCache<MiLocalUser>;
+
+ constructor(
+ @Inject(DI.db)
+ private db: DataSource,
+
+ @Inject(DI.meta)
+ private meta: MiMeta,
+
+ @Inject(DI.systemAccountsRepository)
+ private systemAccountsRepository: SystemAccountsRepository,
+
+ @Inject(DI.usersRepository)
+ private usersRepository: UsersRepository,
+
+ @Inject(DI.userProfilesRepository)
+ private userProfilesRepository: UserProfilesRepository,
+
+ private idService: IdService,
+ ) {
+ this.cache = new MemoryKVCache<MiLocalUser>(1000 * 60 * 10); // 10m
+ }
+
+ @bindThis
+ public async list(): Promise<MiSystemAccount[]> {
+ const accounts = await this.systemAccountsRepository.findBy({});
+
+ return accounts;
+ }
+
+ @bindThis
+ public async fetch(type: typeof SYSTEM_ACCOUNT_TYPES[number]): Promise<MiLocalUser> {
+ const cached = this.cache.get(type);
+ if (cached) return cached;
+
+ const systemAccount = await this.systemAccountsRepository.findOne({
+ where: { type: type },
+ relations: ['user'],
+ });
+
+ if (systemAccount) {
+ this.cache.set(type, systemAccount.user as MiLocalUser);
+ return systemAccount.user as MiLocalUser;
+ } else {
+ const created = await this.createCorrespondingUser(type, {
+ username: `system.${type}`, // NOTE: (できれば避けたいが) . が含まれるかどうかでシステムアカウントかどうかを判定している処理もあるので変えないように
+ name: this.meta.name,
+ });
+ this.cache.set(type, created);
+ return created;
+ }
+ }
+
+ @bindThis
+ private async createCorrespondingUser(type: typeof SYSTEM_ACCOUNT_TYPES[number], extra: {
+ username: MiUser['username'];
+ name?: MiUser['name'];
+ }): Promise<MiLocalUser> {
+ const password = randomUUID();
+
+ // Generate hash of password
+ const salt = await bcrypt.genSalt(8);
+ const hash = await bcrypt.hash(password, salt);
+
+ // Generate secret
+ const secret = generateNativeUserToken();
+
+ const keyPair = await genRsaKeyPair();
+
+ let account!: MiUser;
+
+ // Start transaction
+ await this.db.transaction(async transactionalEntityManager => {
+ const exist = await transactionalEntityManager.findOneBy(MiUser, {
+ usernameLower: extra.username.toLowerCase(),
+ host: IsNull(),
+ });
+
+ if (exist) {
+ account = exist;
+ return;
+ }
+
+ account = await transactionalEntityManager.insert(MiUser, {
+ id: this.idService.gen(),
+ username: extra.username,
+ usernameLower: extra.username.toLowerCase(),
+ host: null,
+ token: secret,
+ isLocked: true,
+ isExplorable: false,
+ isBot: true,
+ name: extra.name,
+ }).then(x => transactionalEntityManager.findOneByOrFail(MiUser, x.identifiers[0]));
+
+ await transactionalEntityManager.insert(MiUserKeypair, {
+ publicKey: keyPair.publicKey,
+ privateKey: keyPair.privateKey,
+ userId: account.id,
+ });
+
+ await transactionalEntityManager.insert(MiUserProfile, {
+ userId: account.id,
+ autoAcceptFollowed: false,
+ password: hash,
+ });
+
+ await transactionalEntityManager.insert(MiUsedUsername, {
+ createdAt: new Date(),
+ username: extra.username.toLowerCase(),
+ });
+
+ await transactionalEntityManager.insert(MiSystemAccount, {
+ id: this.idService.gen(),
+ userId: account.id,
+ type: type,
+ });
+ });
+
+ return account as MiLocalUser;
+ }
+
+ @bindThis
+ public async updateCorrespondingUserProfile(type: typeof SYSTEM_ACCOUNT_TYPES[number], extra: {
+ name?: string;
+ description?: MiUserProfile['description'];
+ }): Promise<MiLocalUser> {
+ const user = await this.fetch(type);
+
+ const updates = {} as Partial<MiUser>;
+ if (extra.name !== undefined) updates.name = extra.name;
+
+ if (Object.keys(updates).length > 0) {
+ await this.usersRepository.update(user.id, updates);
+ }
+
+ const profileUpdates = {} as Partial<MiUserProfile>;
+ if (extra.description !== undefined) profileUpdates.description = extra.description;
+
+ if (Object.keys(profileUpdates).length > 0) {
+ await this.userProfilesRepository.update(user.id, profileUpdates);
+ }
+
+ const updated = await this.usersRepository.findOneByOrFail({ id: user.id }) as MiLocalUser;
+ this.cache.set(type, updated);
+
+ return updated;
+ }
+}
diff --git a/packages/backend/src/core/UserFollowingService.ts b/packages/backend/src/core/UserFollowingService.ts
index b98ca97ec9..e7a6be99fb 100644
--- a/packages/backend/src/core/UserFollowingService.ts
+++ b/packages/backend/src/core/UserFollowingService.ts
@@ -5,7 +5,7 @@
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
-import { IsNull } from 'typeorm';
+import { Brackets, IsNull } from 'typeorm';
import type { MiLocalUser, MiPartialLocalUser, MiPartialRemoteUser, MiRemoteUser, MiUser } from '@/models/User.js';
import { IdentifiableError } from '@/misc/identifiable-error.js';
import { QueueService } from '@/core/QueueService.js';
@@ -736,4 +736,30 @@ export class UserFollowingService implements OnModuleInit {
.where('following.followerId = :followerId', { followerId: userId })
.getMany();
}
+
+ @bindThis
+ public isFollowing(followerId: MiUser['id'], followeeId: MiUser['id']) {
+ return this.followingsRepository.exists({
+ where: {
+ followerId,
+ followeeId,
+ },
+ });
+ }
+
+ @bindThis
+ public async isMutual(aUserId: MiUser['id'], bUserId: MiUser['id']) {
+ const count = await this.followingsRepository.createQueryBuilder('following')
+ .where(new Brackets(qb => {
+ qb.where('following.followerId = :aUserId', { aUserId })
+ .andWhere('following.followeeId = :bUserId', { bUserId });
+ }))
+ .orWhere(new Brackets(qb => {
+ qb.where('following.followerId = :bUserId', { bUserId })
+ .andWhere('following.followeeId = :aUserId', { aUserId });
+ }))
+ .getCount();
+
+ return count === 2;
+ }
}
diff --git a/packages/backend/src/core/UserListService.ts b/packages/backend/src/core/UserListService.ts
index 4f4d59a02c..a4c5eb5416 100644
--- a/packages/backend/src/core/UserListService.ts
+++ b/packages/backend/src/core/UserListService.ts
@@ -15,11 +15,11 @@ import type { GlobalEvents } from '@/core/GlobalEventService.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { DI } from '@/di-symbols.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
-import { ProxyAccountService } from '@/core/ProxyAccountService.js';
import { bindThis } from '@/decorators.js';
import { QueueService } from '@/core/QueueService.js';
import { RedisKVCache } from '@/misc/cache.js';
import { RoleService } from '@/core/RoleService.js';
+import { SystemAccountService } from '@/core/SystemAccountService.js';
@Injectable()
export class UserListService implements OnApplicationShutdown, OnModuleInit {
@@ -43,8 +43,8 @@ export class UserListService implements OnApplicationShutdown, OnModuleInit {
private userEntityService: UserEntityService,
private idService: IdService,
private globalEventService: GlobalEventService,
- private proxyAccountService: ProxyAccountService,
private queueService: QueueService,
+ private systemAccountService: SystemAccountService,
) {
this.membersCache = new RedisKVCache<Set<string>>(this.redisClient, 'userListMembers', {
lifetime: 1000 * 60 * 30, // 30m
@@ -111,10 +111,8 @@ export class UserListService implements OnApplicationShutdown, OnModuleInit {
// このインスタンス内にこのリモートユーザーをフォローしているユーザーがいなくても投稿を受け取るためにダミーのユーザーがフォローしたということにする
if (this.userEntityService.isRemoteUser(target)) {
- const proxy = await this.proxyAccountService.fetch();
- if (proxy) {
- this.queueService.createFollowJob([{ from: { id: proxy.id }, to: { id: target.id } }]);
- }
+ const proxy = await this.systemAccountService.fetch('proxy');
+ this.queueService.createFollowJob([{ from: { id: proxy.id }, to: { id: target.id } }]);
}
}
diff --git a/packages/backend/src/core/UserWebhookService.ts b/packages/backend/src/core/UserWebhookService.ts
index 08db4c9afc..2f79eb429a 100644
--- a/packages/backend/src/core/UserWebhookService.ts
+++ b/packages/backend/src/core/UserWebhookService.ts
@@ -15,7 +15,7 @@ import { QueueService } from '@/core/QueueService.js';
import type { OnApplicationShutdown } from '@nestjs/common';
export type UserWebhookPayload<T extends WebhookEventTypes> =
- T extends 'note' | 'reply' | 'renote' |'mention' | 'edited' ? {
+ T extends 'note' | 'reply' | 'renote' | 'mention' | 'edited' ? {
note: Packed<'Note'>,
} :
T extends 'follow' | 'unfollow' ? {
diff --git a/packages/backend/src/core/UtilityService.ts b/packages/backend/src/core/UtilityService.ts
index 81eaa5f95d..cb534a229c 100644
--- a/packages/backend/src/core/UtilityService.ts
+++ b/packages/backend/src/core/UtilityService.ts
@@ -39,6 +39,14 @@ export class UtilityService {
return this.punyHost(uri) === this.toPuny(this.config.host);
}
+ // メールアドレスのバリデーションを行う
+ // https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
+ @bindThis
+ public validateEmailFormat(email: string): boolean {
+ const regexp = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
+ return regexp.test(email);
+ }
+
@bindThis
public isBlockedHost(blockedHosts: string[], host: string | null): boolean {
if (host == null) return false;
diff --git a/packages/backend/src/core/WebAuthnService.ts b/packages/backend/src/core/WebAuthnService.ts
index ed75e4f467..372e1e2ab7 100644
--- a/packages/backend/src/core/WebAuthnService.ts
+++ b/packages/backend/src/core/WebAuthnService.ts
@@ -127,11 +127,11 @@ export class WebAuthnService {
const { registrationInfo } = verification;
return {
- credentialID: registrationInfo.credentialID,
- credentialPublicKey: registrationInfo.credentialPublicKey,
+ credentialID: registrationInfo.credential.id,
+ credentialPublicKey: registrationInfo.credential.publicKey,
attestationObject: registrationInfo.attestationObject,
fmt: registrationInfo.fmt,
- counter: registrationInfo.counter,
+ counter: registrationInfo.credential.counter,
userVerified: registrationInfo.userVerified,
credentialDeviceType: registrationInfo.credentialDeviceType,
credentialBackedUp: registrationInfo.credentialBackedUp,
@@ -212,9 +212,9 @@ export class WebAuthnService {
expectedChallenge: challenge,
expectedOrigin: relyingParty.origin,
expectedRPID: relyingParty.rpId,
- authenticator: {
- credentialID: key.id,
- credentialPublicKey: Buffer.from(key.publicKey, 'base64url'),
+ credential: {
+ id: key.id,
+ publicKey: Buffer.from(key.publicKey, 'base64url'),
counter: key.counter,
transports: key.transports ? key.transports as AuthenticatorTransportFuture[] : undefined,
},
@@ -292,9 +292,9 @@ export class WebAuthnService {
expectedChallenge: challenge,
expectedOrigin: relyingParty.origin,
expectedRPID: relyingParty.rpId,
- authenticator: {
- credentialID: key.id,
- credentialPublicKey: Buffer.from(key.publicKey, 'base64url'),
+ credential: {
+ id: key.id,
+ publicKey: Buffer.from(key.publicKey, 'base64url'),
counter: key.counter,
transports: key.transports ? key.transports as AuthenticatorTransportFuture[] : undefined,
},
diff --git a/packages/backend/src/core/WebhookTestService.ts b/packages/backend/src/core/WebhookTestService.ts
index 891bdf7334..8d2e181aa5 100644
--- a/packages/backend/src/core/WebhookTestService.ts
+++ b/packages/backend/src/core/WebhookTestService.ts
@@ -7,42 +7,16 @@ import { Injectable } from '@nestjs/common';
import { MiAbuseUserReport, MiNote, MiUser, MiWebhook } from '@/models/_.js';
import { bindThis } from '@/decorators.js';
import { MiSystemWebhook, type SystemWebhookEventType } from '@/models/SystemWebhook.js';
-import { AbuseReportPayload, SystemWebhookPayload, SystemWebhookService } from '@/core/SystemWebhookService.js';
-import { Packed } from '@/misc/json-schema.js';
+import { type AbuseReportPayload, SystemWebhookPayload, SystemWebhookService } from '@/core/SystemWebhookService.js';
+import { type Packed } from '@/misc/json-schema.js';
import { type WebhookEventTypes } from '@/models/Webhook.js';
+import { CustomEmojiService } from '@/core/CustomEmojiService.js';
import { type UserWebhookPayload, UserWebhookService } from '@/core/UserWebhookService.js';
import { QueueService } from '@/core/QueueService.js';
import { ModeratorInactivityRemainingTime } from '@/queue/processors/CheckModeratorsActivityProcessorService.js';
const oneDayMillis = 24 * 60 * 60 * 1000;
-function generateAbuseReport(override?: Partial<MiAbuseUserReport>): AbuseReportPayload {
- const result: MiAbuseUserReport = {
- id: 'dummy-abuse-report1',
- targetUserId: 'dummy-target-user',
- targetUser: null,
- reporterId: 'dummy-reporter-user',
- reporter: null,
- assigneeId: null,
- assignee: null,
- resolved: false,
- forwarded: false,
- comment: 'This is a dummy report for testing purposes.',
- targetUserHost: null,
- reporterHost: null,
- resolvedAs: null,
- moderationNote: 'foo',
- ...override,
- };
-
- return {
- ...result,
- targetUser: result.targetUser ? toPackedUserLite(result.targetUser) : null,
- reporter: result.reporter ? toPackedUserLite(result.reporter) : null,
- assignee: result.assignee ? toPackedUserLite(result.assignee) : null,
- };
-}
-
function generateDummyUser(override?: Partial<MiUser>): MiUser {
return {
id: 'dummy-user-1',
@@ -79,13 +53,13 @@ function generateDummyUser(override?: Partial<MiUser>): MiUser {
isBot: false,
isCat: true,
speakAsCat: true,
- isRoot: false,
isExplorable: true,
isHibernated: false,
isDeleted: false,
requireSigninToViewContents: false,
makeNotesFollowersOnlyBefore: null,
makeNotesHiddenBefore: null,
+ chatScope: 'mutual',
emojis: [],
score: 0,
host: null,
@@ -150,135 +124,6 @@ function generateDummyNote(override?: Partial<MiNote>): MiNote {
};
}
-function toPackedNote(note: MiNote, detail = true, override?: Packed<'Note'>): Packed<'Note'> {
- return {
- id: note.id,
- createdAt: new Date().toISOString(),
- deletedAt: null,
- text: note.text,
- cw: note.cw,
- userId: note.userId,
- user: toPackedUserLite(note.user ?? generateDummyUser()),
- replyId: note.replyId,
- renoteId: note.renoteId,
- isHidden: false,
- visibility: note.visibility,
- mentions: note.mentions,
- visibleUserIds: note.visibleUserIds,
- fileIds: note.fileIds,
- files: [],
- tags: note.tags,
- poll: null,
- emojis: note.emojis,
- channelId: note.channelId,
- channel: note.channel,
- localOnly: note.localOnly,
- reactionAcceptance: note.reactionAcceptance,
- reactionEmojis: {},
- reactions: {},
- reactionCount: 0,
- renoteCount: note.renoteCount,
- repliesCount: note.repliesCount,
- uri: note.uri ?? undefined,
- url: note.url ?? undefined,
- reactionAndUserPairCache: note.reactionAndUserPairCache,
- ...(detail ? {
- clippedCount: note.clippedCount,
- reply: note.reply ? toPackedNote(note.reply, false) : null,
- renote: note.renote ? toPackedNote(note.renote, true) : null,
- myReaction: null,
- } : {}),
- ...override,
- };
-}
-
-function toPackedUserLite(user: MiUser, override?: Packed<'UserLite'>): Packed<'UserLite'> {
- return {
- id: user.id,
- name: user.name,
- username: user.username,
- host: user.host,
- avatarUrl: user.avatarUrl,
- avatarBlurhash: user.avatarBlurhash,
- avatarDecorations: user.avatarDecorations.map(it => ({
- id: it.id,
- angle: it.angle,
- flipH: it.flipH,
- url: 'https://example.com/dummy-image001.png',
- offsetX: it.offsetX,
- offsetY: it.offsetY,
- })),
- isBot: user.isBot,
- isCat: user.isCat,
- speakAsCat: user.speakAsCat,
- emojis: user.emojis,
- onlineStatus: 'active',
- badgeRoles: [],
- noindex: user.noindex,
- isModerator: false,
- isAdmin: false,
- isSystem: false,
- isSilenced: user.isSilenced,
- enableRss: true,
- mandatoryCW: null,
- ...override,
- };
-}
-
-function toPackedUserDetailedNotMe(user: MiUser, override?: Packed<'UserDetailedNotMe'>): Packed<'UserDetailedNotMe'> {
- return {
- ...toPackedUserLite(user),
- url: null,
- uri: null,
- movedTo: null,
- alsoKnownAs: [],
- createdAt: new Date().toISOString(),
- updatedAt: user.updatedAt?.toISOString() ?? null,
- lastFetchedAt: user.lastFetchedAt?.toISOString() ?? null,
- bannerUrl: user.bannerUrl,
- bannerBlurhash: user.bannerBlurhash,
- backgroundUrl: user.backgroundUrl,
- backgroundBlurhash: user.backgroundBlurhash,
- isLocked: user.isLocked,
- isSilenced: false,
- isSuspended: user.isSuspended,
- description: null,
- location: null,
- birthday: null,
- lang: null,
- fields: [],
- verifiedLinks: [],
- followersCount: user.followersCount,
- followingCount: user.followingCount,
- notesCount: user.notesCount,
- pinnedNoteIds: [],
- pinnedNotes: [],
- pinnedPageId: null,
- pinnedPage: null,
- publicReactions: true,
- followersVisibility: 'public',
- followingVisibility: 'public',
- twoFactorEnabled: false,
- usePasswordLessLogin: false,
- securityKeys: false,
- roles: [],
- memo: null,
- moderationNote: undefined,
- isFollowing: false,
- isFollowed: false,
- hasPendingFollowRequestFromYou: false,
- hasPendingFollowRequestToYou: false,
- isBlocking: false,
- isBlocked: false,
- isMuted: false,
- isRenoteMuted: false,
- notify: 'none',
- withReplies: true,
- listenbrainz: null,
- ...override,
- };
-}
-
const dummyUser1 = generateDummyUser();
const dummyUser2 = generateDummyUser({
id: 'dummy-user-2',
@@ -311,6 +156,7 @@ export class WebhookTestService {
};
constructor(
+ private customEmojiService: CustomEmojiService,
private userWebhookService: UserWebhookService,
private systemWebhookService: SystemWebhookService,
private queueService: QueueService,
@@ -381,19 +227,19 @@ export class WebhookTestService {
switch (params.type) {
case 'note': {
- send('note', { note: toPackedNote(dummyNote1) });
+ send('note', { note: await this.toPackedNote(dummyNote1) });
break;
}
case 'reply': {
- send('reply', { note: toPackedNote(dummyReply1) });
+ send('reply', { note: await this.toPackedNote(dummyReply1) });
break;
}
case 'renote': {
- send('renote', { note: toPackedNote(dummyRenote1) });
+ send('renote', { note: await this.toPackedNote(dummyRenote1) });
break;
}
case 'mention': {
- send('mention', { note: toPackedNote(dummyMention1) });
+ send('mention', { note: await this.toPackedNote(dummyMention1) });
break;
}
case 'edited': {
@@ -401,15 +247,15 @@ export class WebhookTestService {
break;
}
case 'follow': {
- send('follow', { user: toPackedUserDetailedNotMe(dummyUser1) });
+ send('follow', { user: await this.toPackedUserDetailedNotMe(dummyUser1) });
break;
}
case 'followed': {
- send('followed', { user: toPackedUserLite(dummyUser2) });
+ send('followed', { user: await this.toPackedUserLite(dummyUser2) });
break;
}
case 'unfollow': {
- send('unfollow', { user: toPackedUserDetailedNotMe(dummyUser3) });
+ send('unfollow', { user: await this.toPackedUserDetailedNotMe(dummyUser3) });
break;
}
// まだ実装されていない (#9485)
@@ -458,7 +304,7 @@ export class WebhookTestService {
switch (params.type) {
case 'abuseReport': {
- send('abuseReport', generateAbuseReport({
+ send('abuseReport', await this.generateAbuseReport({
targetUserId: dummyUser1.id,
targetUser: dummyUser1,
reporterId: dummyUser2.id,
@@ -467,7 +313,7 @@ export class WebhookTestService {
break;
}
case 'abuseReportResolved': {
- send('abuseReportResolved', generateAbuseReport({
+ send('abuseReportResolved', await this.generateAbuseReport({
targetUserId: dummyUser1.id,
targetUser: dummyUser1,
reporterId: dummyUser2.id,
@@ -479,7 +325,7 @@ export class WebhookTestService {
break;
}
case 'userCreated': {
- send('userCreated', toPackedUserLite(dummyUser1));
+ send('userCreated', await this.toPackedUserLite(dummyUser1));
break;
}
case 'inactiveModeratorsWarning': {
@@ -505,4 +351,154 @@ export class WebhookTestService {
}
}
}
+
+ @bindThis
+ private async generateAbuseReport(override?: Partial<MiAbuseUserReport>): Promise<AbuseReportPayload> {
+ const result: MiAbuseUserReport = {
+ id: 'dummy-abuse-report1',
+ targetUserId: 'dummy-target-user',
+ targetUser: null,
+ reporterId: 'dummy-reporter-user',
+ reporter: null,
+ assigneeId: null,
+ assignee: null,
+ resolved: false,
+ forwarded: false,
+ comment: 'This is a dummy report for testing purposes.',
+ targetUserHost: null,
+ reporterHost: null,
+ resolvedAs: null,
+ moderationNote: 'foo',
+ ...override,
+ };
+
+ return {
+ ...result,
+ targetUser: result.targetUser ? await this.toPackedUserLite(result.targetUser) : null,
+ reporter: result.reporter ? await this.toPackedUserLite(result.reporter) : null,
+ assignee: result.assignee ? await this.toPackedUserLite(result.assignee) : null,
+ };
+ }
+
+ @bindThis
+ private async toPackedNote(note: MiNote, detail = true, override?: Packed<'Note'>): Promise<Packed<'Note'>> {
+ return {
+ id: note.id,
+ createdAt: new Date().toISOString(),
+ deletedAt: null,
+ text: note.text,
+ cw: note.cw,
+ userId: note.userId,
+ user: await this.toPackedUserLite(note.user ?? generateDummyUser()),
+ replyId: note.replyId,
+ renoteId: note.renoteId,
+ isHidden: false,
+ visibility: note.visibility,
+ mentions: note.mentions,
+ visibleUserIds: note.visibleUserIds,
+ fileIds: note.fileIds,
+ files: [],
+ tags: note.tags,
+ poll: null,
+ emojis: await this.customEmojiService.populateEmojis(note.emojis, note.userHost),
+ channelId: note.channelId,
+ channel: note.channel,
+ localOnly: note.localOnly,
+ reactionAcceptance: note.reactionAcceptance,
+ reactionEmojis: {},
+ reactions: {},
+ reactionCount: 0,
+ renoteCount: note.renoteCount,
+ repliesCount: note.repliesCount,
+ uri: note.uri ?? undefined,
+ url: note.url ?? undefined,
+ reactionAndUserPairCache: note.reactionAndUserPairCache,
+ ...(detail ? {
+ clippedCount: note.clippedCount,
+ reply: note.reply ? await this.toPackedNote(note.reply, false) : null,
+ renote: note.renote ? await this.toPackedNote(note.renote, true) : null,
+ myReaction: null,
+ } : {}),
+ ...override,
+ };
+ }
+
+ @bindThis
+ private async toPackedUserLite(user: MiUser, override?: Packed<'UserLite'>): Promise<Packed<'UserLite'>> {
+ return {
+ id: user.id,
+ name: user.name,
+ username: user.username,
+ host: user.host,
+ avatarUrl: user.avatarUrl,
+ avatarBlurhash: user.avatarBlurhash,
+ avatarDecorations: user.avatarDecorations.map(it => ({
+ id: it.id,
+ angle: it.angle,
+ flipH: it.flipH,
+ url: 'https://example.com/dummy-image001.png',
+ offsetX: it.offsetX,
+ offsetY: it.offsetY,
+ })),
+ isBot: user.isBot,
+ isCat: user.isCat,
+ emojis: await this.customEmojiService.populateEmojis(user.emojis, user.host),
+ onlineStatus: 'active',
+ badgeRoles: [],
+ ...override,
+ };
+ }
+
+ @bindThis
+ private async toPackedUserDetailedNotMe(user: MiUser, override?: Packed<'UserDetailedNotMe'>): Promise<Packed<'UserDetailedNotMe'>> {
+ return {
+ ...await this.toPackedUserLite(user),
+ url: null,
+ uri: null,
+ movedTo: null,
+ alsoKnownAs: [],
+ createdAt: new Date().toISOString(),
+ updatedAt: user.updatedAt?.toISOString() ?? null,
+ lastFetchedAt: user.lastFetchedAt?.toISOString() ?? null,
+ bannerUrl: user.bannerUrl,
+ bannerBlurhash: user.bannerBlurhash,
+ isLocked: user.isLocked,
+ isSilenced: false,
+ isSuspended: user.isSuspended,
+ description: null,
+ location: null,
+ birthday: null,
+ lang: null,
+ fields: [],
+ verifiedLinks: [],
+ followersCount: user.followersCount,
+ followingCount: user.followingCount,
+ notesCount: user.notesCount,
+ pinnedNoteIds: [],
+ pinnedNotes: [],
+ pinnedPageId: null,
+ pinnedPage: null,
+ publicReactions: true,
+ followersVisibility: 'public',
+ followingVisibility: 'public',
+ chatScope: 'mutual',
+ twoFactorEnabled: false,
+ usePasswordLessLogin: false,
+ securityKeys: false,
+ roles: [],
+ memo: null,
+ moderationNote: undefined,
+ isFollowing: false,
+ isFollowed: false,
+ hasPendingFollowRequestFromYou: false,
+ hasPendingFollowRequestToYou: false,
+ isBlocking: false,
+ isBlocked: false,
+ isMuted: false,
+ isRenoteMuted: false,
+ notify: 'none',
+ withReplies: true,
+ ...override,
+ };
+ }
}
diff --git a/packages/backend/src/core/activitypub/ApDeliverManagerService.ts b/packages/backend/src/core/activitypub/ApDeliverManagerService.ts
index f045333d2a..eaa592b9e0 100644
--- a/packages/backend/src/core/activitypub/ApDeliverManagerService.ts
+++ b/packages/backend/src/core/activitypub/ApDeliverManagerService.ts
@@ -197,6 +197,25 @@ export class ApDeliverManagerService {
await manager.execute();
}
+ /**
+ * Deliver activity to users
+ * @param actor
+ * @param activity Activity
+ * @param targets Target users
+ */
+ @bindThis
+ public async deliverToUsers(actor: { id: MiLocalUser['id']; host: null; }, activity: IActivity, targets: MiRemoteUser[]): Promise<void> {
+ const manager = new DeliverManager(
+ this.userEntityService,
+ this.followingsRepository,
+ this.queueService,
+ actor,
+ activity,
+ );
+ for (const to of targets) manager.addDirectRecipe(to);
+ await manager.execute();
+ }
+
@bindThis
public createDeliverManager(actor: { id: MiUser['id']; host: null; }, activity: IActivity | null): DeliverManager {
return new DeliverManager(
diff --git a/packages/backend/src/core/activitypub/ApInboxService.ts b/packages/backend/src/core/activitypub/ApInboxService.ts
index 402d5ab2a4..ac4a408fa6 100644
--- a/packages/backend/src/core/activitypub/ApInboxService.ts
+++ b/packages/backend/src/core/activitypub/ApInboxService.ts
@@ -598,19 +598,12 @@ export class ApInboxService {
return `skip: delete actor ${actor.uri} !== ${uri}`;
}
- const user = await this.usersRepository.findOneBy({ id: actor.id });
- if (user == null) {
- return 'skip: actor not found';
- } else if (user.isDeleted) {
- return 'skip: already deleted';
+ if (!(await this.usersRepository.update({ id: actor.id, isDeleted: false }, { isDeleted: true })).affected) {
+ return 'skip: already deleted or actor not found';
}
const job = await this.queueService.createDeleteAccountJob(actor);
- await this.usersRepository.update(actor.id, {
- isDeleted: true,
- });
-
this.globalEventService.publishInternalEvent('remoteUserUpdated', { id: actor.id });
return `ok: queued ${job.name} ${job.id}`;
diff --git a/packages/backend/src/core/activitypub/ApRendererService.ts b/packages/backend/src/core/activitypub/ApRendererService.ts
index 61878c60e8..6612303196 100644
--- a/packages/backend/src/core/activitypub/ApRendererService.ts
+++ b/packages/backend/src/core/activitypub/ApRendererService.ts
@@ -30,6 +30,7 @@ import { CustomEmojiService } from '@/core/CustomEmojiService.js';
import { IdService } from '@/core/IdService.js';
import { appendContentWarning } from '@/misc/append-content-warning.js';
import { QueryService } from '@/core/QueryService.js';
+import { UtilityService } from '@/core/UtilityService.js';
import { JsonLdService } from './JsonLdService.js';
import { ApMfmService } from './ApMfmService.js';
import { CONTEXT } from './misc/contexts.js';
@@ -72,6 +73,7 @@ export class ApRendererService {
private mfmService: MfmService,
private idService: IdService,
private readonly queryService: QueryService,
+ private utilityService: UtilityService,
) {
}
@@ -267,6 +269,49 @@ export class ApRendererService {
}
@bindThis
+ public renderIdenticon(user: MiLocalUser): IApImage {
+ return {
+ type: 'Image',
+ url: this.userEntityService.getIdenticonUrl(user),
+ sensitive: false,
+ name: null,
+ };
+ }
+
+ @bindThis
+ public renderSystemAvatar(user: MiLocalUser): IApImage {
+ if (this.meta.iconUrl == null) return this.renderIdenticon(user);
+ return {
+ type: 'Image',
+ url: this.meta.iconUrl,
+ sensitive: false,
+ name: null,
+ };
+ }
+
+ @bindThis
+ public renderSystemBanner(): IApImage | null {
+ if (this.meta.bannerUrl == null) return null;
+ return {
+ type: 'Image',
+ url: this.meta.bannerUrl,
+ sensitive: false,
+ name: null,
+ };
+ }
+
+ @bindThis
+ public renderSystemBackground(): IApImage | null {
+ if (this.meta.backgroundImageUrl == null) return null;
+ return {
+ type: 'Image',
+ url: this.meta.backgroundImageUrl,
+ sensitive: false,
+ name: null,
+ };
+ }
+
+ @bindThis
public renderKey(user: MiLocalUser, key: MiUserKeypair, postfix?: string): IKey {
return {
id: `${this.config.url}/users/${user.id}${postfix ?? '/publickey'}`,
@@ -503,11 +548,28 @@ export class ApRendererService {
this.userProfilesRepository.findOneByOrFail({ userId: user.id }),
]);
+ const tryRewriteUrl = (maybeUrl: string) => {
+ const urlSafeRegex = /^(?:http[s]?:\/\/.)?(?:www\.)?[-a-zA-Z0-9@%._\+~#=]{2,256}\.[a-z]{2,6}\b(?:[-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)/;
+ try {
+ const match = maybeUrl.match(urlSafeRegex);
+ if (!match) {
+ return maybeUrl;
+ }
+ const urlPart = match[0];
+ const urlPartParsed = new URL(urlPart);
+ const restPart = maybeUrl.slice(match[0].length);
+
+ return `<a href="${urlPartParsed.href}" rel="me nofollow noopener" target="_blank">${urlPart}</a>${restPart}`;
+ } catch (e) {
+ return maybeUrl;
+ }
+ };
+
const attachment = profile.fields.map(field => ({
type: 'PropertyValue',
name: field.name,
value: (field.value.startsWith('http://') || field.value.startsWith('https://'))
- ? `<a href="${new URL(field.value).href}" rel="me nofollow noopener" target="_blank">${new URL(field.value).href}</a>`
+ ? tryRewriteUrl(field.value)
: field.value,
}));
@@ -542,9 +604,9 @@ export class ApRendererService {
_misskey_requireSigninToViewContents: user.requireSigninToViewContents,
_misskey_makeNotesFollowersOnlyBefore: user.makeNotesFollowersOnlyBefore,
_misskey_makeNotesHiddenBefore: user.makeNotesHiddenBefore,
- icon: avatar ? this.renderImage(avatar) : null,
- image: banner ? this.renderImage(banner) : null,
- backgroundUrl: background ? this.renderImage(background) : null,
+ icon: avatar ? this.renderImage(avatar) : isSystem ? this.renderSystemAvatar(user) : this.renderIdenticon(user),
+ image: banner ? this.renderImage(banner) : isSystem ? this.renderSystemBanner() : null,
+ backgroundUrl: background ? this.renderImage(background) : isSystem ? this.renderSystemBackground() : null,
tag,
manuallyApprovesFollowers: user.isLocked,
discoverable: user.isExplorable,
@@ -660,7 +722,7 @@ export class ApRendererService {
@bindThis
public renderUndo(object: string | IObject, user: { id: MiUser['id'] }): IUndo {
- const id = typeof object !== 'string' && typeof object.id === 'string' && object.id.startsWith(this.config.url) ? `${object.id}/undo` : undefined;
+ const id = typeof object !== 'string' && typeof object.id === 'string' && this.utilityService.isUriLocal(object.id) ? `${object.id}/undo` : undefined;
return {
type: 'Undo',
diff --git a/packages/backend/src/core/activitypub/ApRequestService.ts b/packages/backend/src/core/activitypub/ApRequestService.ts
index 952d1f5219..5caee21610 100644
--- a/packages/backend/src/core/activitypub/ApRequestService.ts
+++ b/packages/backend/src/core/activitypub/ApRequestService.ts
@@ -17,6 +17,7 @@ import { LoggerService } from '@/core/LoggerService.js';
import { bindThis } from '@/decorators.js';
import type Logger from '@/logger.js';
import { validateContentTypeSetAsActivityPub } from '@/core/activitypub/misc/validator.js';
+import { FetchAllowSoftFailMask as FetchAllowSoftFailMask } from '@/core/activitypub/misc/check-against-url.js';
import type { IObject, IObjectWithId } from './type.js';
type Request = {
@@ -185,7 +186,7 @@ export class ApRequestService {
* @param followAlternate
*/
@bindThis
- public async signedGet(url: string, user: { id: MiUser['id'] }, followAlternate?: boolean): Promise<IObjectWithId> {
+ public async signedGet(url: string, user: { id: MiUser['id'] }, allowSoftfail: FetchAllowSoftFailMask = FetchAllowSoftFailMask.Strict, followAlternate?: boolean): Promise<IObjectWithId> {
const _followAlternate = followAlternate ?? true;
const keypair = await this.userKeypairService.getUserKeypair(user.id);
@@ -254,7 +255,7 @@ export class ApRequestService {
if (alternate) {
const href = alternate.getAttribute('href');
if (href && this.apUtilityService.haveSameAuthority(url, href)) {
- return await this.signedGet(href, user, false);
+ return await this.signedGet(href, user, allowSoftfail, false);
}
}
} catch (e) {
@@ -271,7 +272,7 @@ export class ApRequestService {
// Make sure the object ID matches the final URL (which is where it actually exists).
// The caller (ApResolverService) will verify the ID against the original / entry URL, which ensures that all three match.
- this.apUtilityService.assertIdMatchesUrlAuthority(activity, res.url);
+ this.apUtilityService.assertIdMatchesUrlAuthority(activity, res.url, allowSoftfail);
return activity as IObjectWithId;
}
diff --git a/packages/backend/src/core/activitypub/ApResolverService.ts b/packages/backend/src/core/activitypub/ApResolverService.ts
index 12c3202af1..27dcf2372b 100644
--- a/packages/backend/src/core/activitypub/ApResolverService.ts
+++ b/packages/backend/src/core/activitypub/ApResolverService.ts
@@ -6,7 +6,6 @@
import { Inject, Injectable } from '@nestjs/common';
import { IsNull, Not } from 'typeorm';
import type { MiLocalUser, MiRemoteUser } from '@/models/User.js';
-import { InstanceActorService } from '@/core/InstanceActorService.js';
import type { NotesRepository, PollsRepository, NoteReactionsRepository, UsersRepository, FollowRequestsRepository, MiMeta, SkApFetchLog } from '@/models/_.js';
import type { Config } from '@/config.js';
import { HttpRequestService } from '@/core/HttpRequestService.js';
@@ -16,13 +15,15 @@ import { bindThis } from '@/decorators.js';
import { LoggerService } from '@/core/LoggerService.js';
import type Logger from '@/logger.js';
import { fromTuple } from '@/misc/from-tuple.js';
-import { IdentifiableError } from '@/misc/identifiable-error.js';
import { ApLogService, calculateDurationSince, extractObjectContext } from '@/core/ApLogService.js';
import { ApUtilityService } from '@/core/activitypub/ApUtilityService.js';
+import { SystemAccountService } from '@/core/SystemAccountService.js';
+import { IdentifiableError } from '@/misc/identifiable-error.js';
import { getApId, getNullableApId, IObjectWithId, isCollectionOrOrderedCollection } from './type.js';
import { ApDbResolverService } from './ApDbResolverService.js';
import { ApRendererService } from './ApRendererService.js';
import { ApRequestService } from './ApRequestService.js';
+import { FetchAllowSoftFailMask } from './misc/check-against-url.js';
import type { IObject, ICollection, IOrderedCollection, ApObject } from './type.js';
export class Resolver {
@@ -39,7 +40,7 @@ export class Resolver {
private noteReactionsRepository: NoteReactionsRepository,
private followRequestsRepository: FollowRequestsRepository,
private utilityService: UtilityService,
- private instanceActorService: InstanceActorService,
+ private systemAccountService: SystemAccountService,
private apRequestService: ApRequestService,
private httpRequestService: HttpRequestService,
private apRendererService: ApRendererService,
@@ -103,11 +104,10 @@ export class Resolver {
return await this.resolve(id);
}
- public async resolve(value: string | [string]): Promise<IObjectWithId>;
- public async resolve(value: string | IObject | [string | IObject]): Promise<IObject>;
+ public async resolve(value: string | [string], allowSoftfail?: FetchAllowSoftFailMask): Promise<IObjectWithId>;
+ public async resolve(value: string | IObject | [string | IObject], allowSoftfail?: FetchAllowSoftFailMask): Promise<IObject>;
@bindThis
- public async resolve(value: string | IObject | [string | IObject]): Promise<IObject> {
- // eslint-disable-next-line no-param-reassign
+ public async resolve(value: string | IObject | [string | IObject], allowSoftfail: FetchAllowSoftFailMask = FetchAllowSoftFailMask.Strict): Promise<IObject> {
value = fromTuple(value);
if (typeof value !== 'string') {
@@ -116,13 +116,13 @@ export class Resolver {
const host = this.utilityService.extractDbHost(value);
if (this.config.activityLogging.enabled && !this.utilityService.isSelfHost(host)) {
- return await this._resolveLogged(value, host);
+ return await this._resolveLogged(value, host, allowSoftfail);
} else {
- return await this._resolve(value, host);
+ return await this._resolve(value, host, allowSoftfail);
}
}
- private async _resolveLogged(requestUri: string, host: string): Promise<IObjectWithId> {
+ private async _resolveLogged(requestUri: string, host: string, allowSoftfail: FetchAllowSoftFailMask): Promise<IObjectWithId> {
const startTime = process.hrtime.bigint();
const log = await this.apLogService.createFetchLog({
@@ -131,7 +131,7 @@ export class Resolver {
});
try {
- const result = await this._resolve(requestUri, host, log);
+ const result = await this._resolve(requestUri, host, allowSoftfail, log);
log.accepted = true;
log.result = 'ok';
@@ -151,7 +151,7 @@ export class Resolver {
}
}
- private async _resolve(value: string, host: string, log?: SkApFetchLog): Promise<IObjectWithId> {
+ private async _resolve(value: string, host: string, allowSoftfail: FetchAllowSoftFailMask, log?: SkApFetchLog): Promise<IObjectWithId> {
if (value.includes('#')) {
// URLs with fragment parts cannot be resolved correctly because
// the fragment part does not get transmitted over HTTP(S).
@@ -178,12 +178,12 @@ export class Resolver {
}
if (this.config.signToActivityPubGet && !this.user) {
- this.user = await this.instanceActorService.getInstanceActor();
+ this.user = await this.systemAccountService.fetch('actor');
}
const object = (this.user
- ? await this.apRequestService.signedGet(value, this.user)
- : await this.httpRequestService.getActivityJson(value));
+ ? await this.apRequestService.signedGet(value, this.user, allowSoftfail) as IObject
+ : await this.httpRequestService.getActivityJson(value, allowSoftfail)) as IObject;
if (log) {
const { object: objectOnly, context, contextHash } = extractObjectContext(object);
@@ -307,7 +307,7 @@ export class ApResolverService {
private followRequestsRepository: FollowRequestsRepository,
private utilityService: UtilityService,
- private instanceActorService: InstanceActorService,
+ private systemAccountService: SystemAccountService,
private apRequestService: ApRequestService,
private httpRequestService: HttpRequestService,
private apRendererService: ApRendererService,
@@ -332,7 +332,7 @@ export class ApResolverService {
this.noteReactionsRepository,
this.followRequestsRepository,
this.utilityService,
- this.instanceActorService,
+ this.systemAccountService,
this.apRequestService,
this.httpRequestService,
this.apRendererService,
diff --git a/packages/backend/src/core/activitypub/misc/check-against-url.ts b/packages/backend/src/core/activitypub/misc/check-against-url.ts
new file mode 100644
index 0000000000..282859907d
--- /dev/null
+++ b/packages/backend/src/core/activitypub/misc/check-against-url.ts
@@ -0,0 +1,38 @@
+/*
+ * SPDX-FileCopyrightText: dakkar and sharkey-project
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+import type { IObject } from '../type.js';
+
+export enum FetchAllowSoftFailMask {
+ // Allow no softfail flags
+ Strict = 0,
+ // The values in tuple (requestUrl, finalUrl, objectId) are not all identical
+ //
+ // This condition is common for user-initiated lookups but should not be allowed in federation loop
+ //
+ // Allow variations:
+ // good example: https://alice.example.com/@user -> https://alice.example.com/user/:userId
+ // problematic example: https://alice.example.com/redirect?url=https://bad.example.com/ -> https://bad.example.com/ -> https://alice.example.com/somethingElse
+ NonCanonicalId = 1 << 0,
+ // Allow the final object to be at most one subdomain deeper than the request URL, similar to SPF relaxed alignment
+ //
+ // Currently no code path allows this flag to be set, but is kept in case of future use as some niche deployments do this, and we provide a pre-reviewed mechanism to opt-in.
+ //
+ // Allow variations:
+ // good example: https://example.com/@user -> https://activitypub.example.com/@user { id: 'https://activitypub.example.com/@user' }
+ // problematic example: https://example.com/@user -> https://untrusted.example.com/@user { id: 'https://untrusted.example.com/@user' }
+ MisalignedOrigin = 1 << 1,
+ // The requested URL has a different host than the returned object ID, although the final URL is still consistent with the object ID
+ //
+ // This condition is common for user-initiated lookups using an intermediate host but should not be allowed in federation loops
+ //
+ // Allow variations:
+ // good example: https://alice.example.com/@user@bob.example.com -> https://bob.example.com/@user { id: 'https://bob.example.com/@user' }
+ // problematic example: https://alice.example.com/definitelyAlice -> https://bob.example.com/@somebodyElse { id: 'https://bob.example.com/@somebodyElse' }
+ CrossOrigin = 1 << 2 | MisalignedOrigin,
+ // Allow all softfail flags
+ //
+ // do not use this flag on released code
+ Any = ~0,
+}
diff --git a/packages/backend/src/core/activitypub/models/ApPersonService.ts b/packages/backend/src/core/activitypub/models/ApPersonService.ts
index da29a3c527..59f1d73fb0 100644
--- a/packages/backend/src/core/activitypub/models/ApPersonService.ts
+++ b/packages/backend/src/core/activitypub/models/ApPersonService.ts
@@ -568,7 +568,7 @@ export class ApPersonService implements OnModuleInit {
inbox: person.inbox,
sharedInbox: person.sharedInbox ?? person.endpoints?.sharedInbox ?? null,
followersUri: person.followers ? getApId(person.followers) : undefined,
- featured: person.featured,
+ featured: person.featured ? getApId(person.featured) : undefined,
emojis: emojiNames,
name: truncate(person.name, nameLength),
tags,
@@ -608,7 +608,9 @@ export class ApPersonService implements OnModuleInit {
if (moving) updates.movedAt = new Date();
// Update user
- await this.usersRepository.update(exist.id, updates);
+ if (!(await this.usersRepository.update({ id: exist.id, isDeleted: false }, updates)).affected) {
+ return 'skip';
+ }
if (person.publicKey) {
await this.userPublickeysRepository.update({ userId: exist.id }, {
@@ -714,7 +716,7 @@ export class ApPersonService implements OnModuleInit {
@bindThis
public async updateFeatured(userId: MiUser['id'], resolver?: Resolver): Promise<void> {
- const user = await this.usersRepository.findOneByOrFail({ id: userId });
+ const user = await this.usersRepository.findOneByOrFail({ id: userId, isDeleted: false });
if (!this.userEntityService.isRemoteUser(user)) return;
if (!user.featured) return;
diff --git a/packages/backend/src/core/entities/ChatEntityService.ts b/packages/backend/src/core/entities/ChatEntityService.ts
new file mode 100644
index 0000000000..099a9e3ad2
--- /dev/null
+++ b/packages/backend/src/core/entities/ChatEntityService.ts
@@ -0,0 +1,376 @@
+/*
+ * SPDX-FileCopyrightText: syuilo and misskey-project
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+import { Inject, Injectable } from '@nestjs/common';
+import { DI } from '@/di-symbols.js';
+import type { MiUser, ChatMessagesRepository, MiChatMessage, ChatRoomsRepository, MiChatRoom, MiChatRoomInvitation, ChatRoomInvitationsRepository, MiChatRoomMembership, ChatRoomMembershipsRepository } from '@/models/_.js';
+import { awaitAll } from '@/misc/prelude/await-all.js';
+import type { Packed } from '@/misc/json-schema.js';
+import type { } from '@/models/Blocking.js';
+import { bindThis } from '@/decorators.js';
+import { IdService } from '@/core/IdService.js';
+import { UserEntityService } from './UserEntityService.js';
+import { DriveFileEntityService } from './DriveFileEntityService.js';
+import { In } from 'typeorm';
+
+@Injectable()
+export class ChatEntityService {
+ constructor(
+ @Inject(DI.chatMessagesRepository)
+ private chatMessagesRepository: ChatMessagesRepository,
+
+ @Inject(DI.chatRoomsRepository)
+ private chatRoomsRepository: ChatRoomsRepository,
+
+ @Inject(DI.chatRoomInvitationsRepository)
+ private chatRoomInvitationsRepository: ChatRoomInvitationsRepository,
+
+ @Inject(DI.chatRoomMembershipsRepository)
+ private chatRoomMembershipsRepository: ChatRoomMembershipsRepository,
+
+ private userEntityService: UserEntityService,
+ private driveFileEntityService: DriveFileEntityService,
+ private idService: IdService,
+ ) {
+ }
+
+ @bindThis
+ public async packMessageDetailed(
+ src: MiChatMessage['id'] | MiChatMessage,
+ me?: { id: MiUser['id'] },
+ options?: {
+ _hint_?: {
+ packedFiles?: Map<MiChatMessage['fileId'], Packed<'DriveFile'> | null>;
+ packedUsers?: Map<MiChatMessage['id'], Packed<'UserLite'>>;
+ packedRooms?: Map<MiChatMessage['toRoomId'], Packed<'ChatRoom'> | null>;
+ };
+ },
+ ): Promise<Packed<'ChatMessage'>> {
+ const packedUsers = options?._hint_?.packedUsers;
+ const packedFiles = options?._hint_?.packedFiles;
+ const packedRooms = options?._hint_?.packedRooms;
+
+ const message = typeof src === 'object' ? src : await this.chatMessagesRepository.findOneByOrFail({ id: src });
+
+ const reactions: { user: Packed<'UserLite'>; reaction: string; }[] = [];
+
+ for (const record of message.reactions) {
+ const [userId, reaction] = record.split('/');
+ reactions.push({
+ user: packedUsers?.get(userId) ?? await this.userEntityService.pack(userId),
+ reaction,
+ });
+ }
+
+ return {
+ id: message.id,
+ createdAt: this.idService.parse(message.id).date.toISOString(),
+ text: message.text,
+ fromUserId: message.fromUserId,
+ fromUser: packedUsers?.get(message.fromUserId) ?? await this.userEntityService.pack(message.fromUser ?? message.fromUserId, me),
+ toUserId: message.toUserId,
+ toUser: message.toUserId ? (packedUsers?.get(message.toUserId) ?? await this.userEntityService.pack(message.toUser ?? message.toUserId, me)) : undefined,
+ toRoomId: message.toRoomId,
+ toRoom: message.toRoomId ? (packedRooms?.get(message.toRoomId) ?? await this.packRoom(message.toRoom ?? message.toRoomId, me)) : undefined,
+ fileId: message.fileId,
+ file: message.fileId ? (packedFiles?.get(message.fileId) ?? await this.driveFileEntityService.pack(message.file ?? message.fileId)) : null,
+ reactions,
+ };
+ }
+
+ @bindThis
+ public async packMessagesDetailed(
+ messages: MiChatMessage[],
+ me: { id: MiUser['id'] },
+ ) {
+ if (messages.length === 0) return [];
+
+ const excludeMe = (x: MiUser | string) => {
+ if (typeof x === 'string') {
+ return x !== me.id;
+ } else {
+ return x.id !== me.id;
+ }
+ };
+
+ const users = [
+ ...messages.map((m) => m.fromUser ?? m.fromUserId).filter(excludeMe),
+ ...messages.map((m) => m.toUser ?? m.toUserId).filter(x => x != null).filter(excludeMe),
+ ];
+
+ const reactedUserIds = messages.flatMap(x => x.reactions.map(r => r.split('/')[0]));
+
+ for (const reactedUserId of reactedUserIds) {
+ if (!users.some(x => typeof x === 'string' ? x === reactedUserId : x.id === reactedUserId)) {
+ users.push(reactedUserId);
+ }
+ }
+
+ const [packedUsers, packedFiles, packedRooms] = await Promise.all([
+ this.userEntityService.packMany(users, me)
+ .then(users => new Map(users.map(u => [u.id, u]))),
+ this.driveFileEntityService.packMany(messages.map(m => m.file).filter(x => x != null))
+ .then(files => new Map(files.map(f => [f.id, f]))),
+ this.packRooms(messages.map(m => m.toRoom ?? m.toRoomId).filter(x => x != null), me)
+ .then(rooms => new Map(rooms.map(r => [r.id, r]))),
+ ]);
+
+ return Promise.all(messages.map(message => this.packMessageDetailed(message, me, { _hint_: { packedUsers, packedFiles, packedRooms } })));
+ }
+
+ @bindThis
+ public async packMessageLiteFor1on1(
+ src: MiChatMessage['id'] | MiChatMessage,
+ options?: {
+ _hint_?: {
+ packedFiles: Map<MiChatMessage['fileId'], Packed<'DriveFile'> | null>;
+ };
+ },
+ ): Promise<Packed<'ChatMessageLite'>> {
+ const packedFiles = options?._hint_?.packedFiles;
+
+ const message = typeof src === 'object' ? src : await this.chatMessagesRepository.findOneByOrFail({ id: src });
+
+ const reactions: { reaction: string; }[] = [];
+
+ for (const record of message.reactions) {
+ const [userId, reaction] = record.split('/');
+ reactions.push({
+ reaction,
+ });
+ }
+
+ return {
+ id: message.id,
+ createdAt: this.idService.parse(message.id).date.toISOString(),
+ text: message.text,
+ fromUserId: message.fromUserId,
+ toUserId: message.toUserId,
+ fileId: message.fileId,
+ file: message.fileId ? (packedFiles?.get(message.fileId) ?? await this.driveFileEntityService.pack(message.file ?? message.fileId)) : null,
+ reactions,
+ };
+ }
+
+ @bindThis
+ public async packMessagesLiteFor1on1(
+ messages: MiChatMessage[],
+ ) {
+ if (messages.length === 0) return [];
+
+ const [packedFiles] = await Promise.all([
+ this.driveFileEntityService.packMany(messages.map(m => m.file).filter(x => x != null))
+ .then(files => new Map(files.map(f => [f.id, f]))),
+ ]);
+
+ return Promise.all(messages.map(message => this.packMessageLiteFor1on1(message, { _hint_: { packedFiles } })));
+ }
+
+ @bindThis
+ public async packMessageLiteForRoom(
+ src: MiChatMessage['id'] | MiChatMessage,
+ options?: {
+ _hint_?: {
+ packedFiles: Map<MiChatMessage['fileId'], Packed<'DriveFile'> | null>;
+ packedUsers: Map<MiUser['id'], Packed<'UserLite'>>;
+ };
+ },
+ ): Promise<Packed<'ChatMessageLite'>> {
+ const packedFiles = options?._hint_?.packedFiles;
+ const packedUsers = options?._hint_?.packedUsers;
+
+ const message = typeof src === 'object' ? src : await this.chatMessagesRepository.findOneByOrFail({ id: src });
+
+ const reactions: { user: Packed<'UserLite'>; reaction: string; }[] = [];
+
+ for (const record of message.reactions) {
+ const [userId, reaction] = record.split('/');
+ reactions.push({
+ user: packedUsers?.get(userId) ?? await this.userEntityService.pack(userId),
+ reaction,
+ });
+ }
+
+ return {
+ id: message.id,
+ createdAt: this.idService.parse(message.id).date.toISOString(),
+ text: message.text,
+ fromUserId: message.fromUserId,
+ fromUser: packedUsers?.get(message.fromUserId) ?? await this.userEntityService.pack(message.fromUser ?? message.fromUserId),
+ toRoomId: message.toRoomId,
+ fileId: message.fileId,
+ file: message.fileId ? (packedFiles?.get(message.fileId) ?? await this.driveFileEntityService.pack(message.file ?? message.fileId)) : null,
+ reactions,
+ };
+ }
+
+ @bindThis
+ public async packMessagesLiteForRoom(
+ messages: MiChatMessage[],
+ ) {
+ if (messages.length === 0) return [];
+
+ const users = messages.map(x => x.fromUser ?? x.fromUserId);
+ const reactedUserIds = messages.flatMap(x => x.reactions.map(r => r.split('/')[0]));
+
+ for (const reactedUserId of reactedUserIds) {
+ if (!users.some(x => typeof x === 'string' ? x === reactedUserId : x.id === reactedUserId)) {
+ users.push(reactedUserId);
+ }
+ }
+
+ const [packedUsers, packedFiles] = await Promise.all([
+ this.userEntityService.packMany(users)
+ .then(users => new Map(users.map(u => [u.id, u]))),
+ this.driveFileEntityService.packMany(messages.map(m => m.file).filter(x => x != null))
+ .then(files => new Map(files.map(f => [f.id, f]))),
+ ]);
+
+ return Promise.all(messages.map(message => this.packMessageLiteForRoom(message, { _hint_: { packedFiles, packedUsers } })));
+ }
+
+ @bindThis
+ public async packRoom(
+ src: MiChatRoom['id'] | MiChatRoom,
+ me?: { id: MiUser['id'] },
+ options?: {
+ _hint_?: {
+ packedOwners: Map<MiChatRoom['id'], Packed<'UserLite'>>;
+ memberships?: Map<MiChatRoom['id'], MiChatRoomMembership | null | undefined>;
+ };
+ },
+ ): Promise<Packed<'ChatRoom'>> {
+ const room = typeof src === 'object' ? src : await this.chatRoomsRepository.findOneByOrFail({ id: src });
+
+ const membership = me && me.id !== room.ownerId ? (options?._hint_?.memberships?.get(room.id) ?? await this.chatRoomMembershipsRepository.findOneBy({ roomId: room.id, userId: me.id })) : null;
+
+ return {
+ id: room.id,
+ createdAt: this.idService.parse(room.id).date.toISOString(),
+ name: room.name,
+ description: room.description,
+ ownerId: room.ownerId,
+ owner: options?._hint_?.packedOwners.get(room.ownerId) ?? await this.userEntityService.pack(room.owner ?? room.ownerId, me),
+ isMuted: membership != null ? membership.isMuted : false,
+ };
+ }
+
+ @bindThis
+ public async packRooms(
+ rooms: (MiChatRoom | MiChatRoom['id'])[],
+ me: { id: MiUser['id'] },
+ ) {
+ if (rooms.length === 0) return [];
+
+ const _rooms = rooms.filter((room): room is MiChatRoom => typeof room !== 'string');
+ if (_rooms.length !== rooms.length) {
+ _rooms.push(
+ ...await this.chatRoomsRepository.find({
+ where: {
+ id: In(rooms.filter((room): room is string => typeof room === 'string')),
+ },
+ relations: ['owner'],
+ }),
+ );
+ }
+
+ const owners = _rooms.map(x => x.owner ?? x.ownerId);
+
+ const [packedOwners, memberships] = await Promise.all([
+ this.userEntityService.packMany(owners, me)
+ .then(users => new Map(users.map(u => [u.id, u]))),
+ this.chatRoomMembershipsRepository.find({
+ where: {
+ roomId: In(_rooms.map(x => x.id)),
+ userId: me.id,
+ },
+ }).then(memberships => new Map(_rooms.map(r => [r.id, memberships.find(m => m.roomId === r.id)]))),
+ ]);
+
+ return Promise.all(_rooms.map(room => this.packRoom(room, me, { _hint_: { packedOwners, memberships } })));
+ }
+
+ @bindThis
+ public async packRoomInvitation(
+ src: MiChatRoomInvitation['id'] | MiChatRoomInvitation,
+ me: { id: MiUser['id'] },
+ options?: {
+ _hint_?: {
+ packedRooms: Map<MiChatRoomInvitation['roomId'], Packed<'ChatRoom'>>;
+ packedUsers: Map<MiChatRoomInvitation['id'], Packed<'UserLite'>>;
+ };
+ },
+ ): Promise<Packed<'ChatRoomInvitation'>> {
+ const invitation = typeof src === 'object' ? src : await this.chatRoomInvitationsRepository.findOneByOrFail({ id: src });
+
+ return {
+ id: invitation.id,
+ createdAt: this.idService.parse(invitation.id).date.toISOString(),
+ roomId: invitation.roomId,
+ room: options?._hint_?.packedRooms.get(invitation.roomId) ?? await this.packRoom(invitation.room ?? invitation.roomId, me),
+ userId: invitation.userId,
+ user: options?._hint_?.packedUsers.get(invitation.userId) ?? await this.userEntityService.pack(invitation.user ?? invitation.userId, me),
+ };
+ }
+
+ @bindThis
+ public async packRoomInvitations(
+ invitations: MiChatRoomInvitation[],
+ me: { id: MiUser['id'] },
+ ) {
+ if (invitations.length === 0) return [];
+
+ return Promise.all(invitations.map(invitation => this.packRoomInvitation(invitation, me)));
+ }
+
+ @bindThis
+ public async packRoomMembership(
+ src: MiChatRoomMembership['id'] | MiChatRoomMembership,
+ me: { id: MiUser['id'] },
+ options?: {
+ populateUser?: boolean;
+ populateRoom?: boolean;
+ _hint_?: {
+ packedRooms: Map<MiChatRoomMembership['roomId'], Packed<'ChatRoom'>>;
+ packedUsers: Map<MiChatRoomMembership['id'], Packed<'UserLite'>>;
+ };
+ },
+ ): Promise<Packed<'ChatRoomMembership'>> {
+ const membership = typeof src === 'object' ? src : await this.chatRoomMembershipsRepository.findOneByOrFail({ id: src });
+
+ return {
+ id: membership.id,
+ createdAt: this.idService.parse(membership.id).date.toISOString(),
+ userId: membership.userId,
+ user: options?.populateUser ? (options._hint_?.packedUsers.get(membership.userId) ?? await this.userEntityService.pack(membership.user ?? membership.userId, me)) : undefined,
+ roomId: membership.roomId,
+ room: options?.populateRoom ? (options._hint_?.packedRooms.get(membership.roomId) ?? await this.packRoom(membership.room ?? membership.roomId, me)) : undefined,
+ };
+ }
+
+ @bindThis
+ public async packRoomMemberships(
+ memberships: MiChatRoomMembership[],
+ me: { id: MiUser['id'] },
+ options: {
+ populateUser?: boolean;
+ populateRoom?: boolean;
+ } = {},
+ ) {
+ if (memberships.length === 0) return [];
+
+ const users = memberships.map(x => x.user ?? x.userId);
+ const rooms = memberships.map(x => x.room ?? x.roomId);
+
+ const [packedUsers, packedRooms] = await Promise.all([
+ this.userEntityService.packMany(users, me)
+ .then(users => new Map(users.map(u => [u.id, u]))),
+ this.packRooms(rooms, me)
+ .then(rooms => new Map(rooms.map(r => [r.id, r]))),
+ ]);
+
+ return Promise.all(memberships.map(membership => this.packRoomMembership(membership, me, { ...options, _hint_: { packedUsers, packedRooms } })));
+ }
+}
diff --git a/packages/backend/src/core/entities/MetaEntityService.ts b/packages/backend/src/core/entities/MetaEntityService.ts
index 3f3a1bad33..83e28ebfc9 100644
--- a/packages/backend/src/core/entities/MetaEntityService.ts
+++ b/packages/backend/src/core/entities/MetaEntityService.ts
@@ -10,8 +10,7 @@ import type { Packed } from '@/misc/json-schema.js';
import type { MiMeta } from '@/models/Meta.js';
import type { AdsRepository } from '@/models/_.js';
import { bindThis } from '@/decorators.js';
-import { UserEntityService } from '@/core/entities/UserEntityService.js';
-import { InstanceActorService } from '@/core/InstanceActorService.js';
+import { SystemAccountService } from '@/core/SystemAccountService.js';
import type { Config } from '@/config.js';
import { DI } from '@/di-symbols.js';
import { DEFAULT_POLICIES } from '@/core/RoleService.js';
@@ -28,8 +27,7 @@ export class MetaEntityService {
@Inject(DI.adsRepository)
private adsRepository: AdsRepository,
- private userEntityService: UserEntityService,
- private instanceActorService: InstanceActorService,
+ private systemAccountService: SystemAccountService,
) { }
@bindThis
@@ -102,6 +100,7 @@ export class MetaEntityService {
enableFC: instance.enableFC,
fcSiteKey: instance.fcSiteKey,
enableTestcaptcha: instance.enableTestcaptcha,
+ googleAnalyticsMeasurementId: instance.googleAnalyticsMeasurementId,
swPublickey: instance.swPublicKey,
themeColor: instance.themeColor,
mascotImageUrl: instance.mascotImageUrl ?? '/assets/ai.png',
@@ -161,14 +160,14 @@ export class MetaEntityService {
const packed = await this.pack(instance);
- const proxyAccount = instance.proxyAccountId ? await this.userEntityService.pack(instance.proxyAccountId).catch(() => null) : null;
+ const proxyAccount = await this.systemAccountService.fetch('proxy');
const packDetailed: Packed<'MetaDetailed'> = {
...packed,
cacheRemoteFiles: instance.cacheRemoteFiles,
cacheRemoteSensitiveFiles: instance.cacheRemoteSensitiveFiles,
- requireSetup: !await this.instanceActorService.realLocalUsersPresent(),
- proxyAccountName: proxyAccount ? proxyAccount.username : null,
+ requireSetup: this.meta.rootUserId == null,
+ proxyAccountName: proxyAccount.username,
features: {
localTimeline: instance.policies.ltlAvailable,
globalTimeline: instance.policies.gtlAvailable,
diff --git a/packages/backend/src/core/entities/UserEntityService.ts b/packages/backend/src/core/entities/UserEntityService.ts
index f5452baaef..cfcf10a0f4 100644
--- a/packages/backend/src/core/entities/UserEntityService.ts
+++ b/packages/backend/src/core/entities/UserEntityService.ts
@@ -30,10 +30,10 @@ import type {
FollowingsRepository,
FollowRequestsRepository,
MiFollowing,
+ MiMeta,
MiUserNotePining,
MiUserProfile,
MutingsRepository,
- NoteUnreadsRepository,
RenoteMutingsRepository,
UserMemoRepository,
UserNotePiningsRepository,
@@ -49,10 +49,10 @@ import { IdService } from '@/core/IdService.js';
import type { AnnouncementService } from '@/core/AnnouncementService.js';
import type { CustomEmojiService } from '@/core/CustomEmojiService.js';
import { AvatarDecorationService } from '@/core/AvatarDecorationService.js';
+import { ChatService } from '@/core/ChatService.js';
import { isSystemAccount } from '@/misc/is-system-account.js';
import type { OnModuleInit } from '@nestjs/common';
import type { NoteEntityService } from './NoteEntityService.js';
-import type { DriveFileEntityService } from './DriveFileEntityService.js';
import type { PageEntityService } from './PageEntityService.js';
/* eslint-disable @typescript-eslint/no-non-null-assertion */
@@ -62,12 +62,14 @@ const ajv = new Ajv();
function isLocalUser(user: MiUser): user is MiLocalUser;
function isLocalUser<T extends { host: MiUser['host'] }>(user: T): user is (T & { host: null; });
+
function isLocalUser(user: MiUser | { host: MiUser['host'] }): boolean {
return user.host == null;
}
function isRemoteUser(user: MiUser): user is MiRemoteUser;
function isRemoteUser<T extends { host: MiUser['host'] }>(user: T): user is (T & { host: string; });
+
function isRemoteUser(user: MiUser | { host: MiUser['host'] }): boolean {
return !isLocalUser(user);
}
@@ -85,7 +87,7 @@ export type UserRelation = {
isRenoteMuted: boolean
isInstanceMuted?: boolean
memo?: string | null
-}
+};
@Injectable()
export class UserEntityService implements OnModuleInit {
@@ -99,6 +101,7 @@ export class UserEntityService implements OnModuleInit {
private federatedInstanceService: FederatedInstanceService;
private idService: IdService;
private avatarDecorationService: AvatarDecorationService;
+ private chatService: ChatService;
constructor(
private moduleRef: ModuleRef,
@@ -106,6 +109,9 @@ export class UserEntityService implements OnModuleInit {
@Inject(DI.config)
private config: Config,
+ @Inject(DI.meta)
+ private meta: MiMeta,
+
@Inject(DI.redis)
private redisClient: Redis.Redis,
@@ -133,9 +139,6 @@ export class UserEntityService implements OnModuleInit {
@Inject(DI.driveFilesRepository)
private driveFilesRepository: DriveFilesRepository,
- @Inject(DI.noteUnreadsRepository)
- private noteUnreadsRepository: NoteUnreadsRepository,
-
@Inject(DI.userNotePiningsRepository)
private userNotePiningsRepository: UserNotePiningsRepository,
@@ -158,6 +161,7 @@ export class UserEntityService implements OnModuleInit {
this.federatedInstanceService = this.moduleRef.get('FederatedInstanceService');
this.idService = this.moduleRef.get('IdService');
this.avatarDecorationService = this.moduleRef.get('AvatarDecorationService');
+ this.chatService = this.moduleRef.get('ChatService');
}
//#region Validators
@@ -447,7 +451,11 @@ export class UserEntityService implements OnModuleInit {
@bindThis
public getIdenticonUrl(user: MiUser): string {
- return `${this.config.url}/identicon/${user.username.toLowerCase()}@${user.host ?? this.config.host}`;
+ if ((user.host == null || user.host === this.config.host) && user.username.includes('.') && this.meta.iconUrl) { // ローカルのシステムアカウントの場合
+ return this.meta.iconUrl;
+ } else {
+ return `${this.config.url}/identicon/${user.username.toLowerCase()}@${user.host ?? this.config.host}`;
+ }
}
@bindThis
@@ -655,6 +663,7 @@ export class UserEntityService implements OnModuleInit {
publicReactions: this.isLocalUser(user) ? profile!.publicReactions : false, // https://github.com/misskey-dev/misskey/issues/12964
followersVisibility: profile!.followersVisibility,
followingVisibility: profile!.followingVisibility,
+ chatScope: user.chatScope,
roles: this.roleService.getUserRoles(user.id).then(roles => roles.filter(role => role.isPublic).sort((a, b) => b.displayOrder - a.displayOrder).map(role => ({
id: role.id,
name: role.name,
@@ -698,14 +707,9 @@ export class UserEntityService implements OnModuleInit {
isDeleted: user.isDeleted,
twoFactorBackupCodesStock: profile?.twoFactorBackupSecret?.length === 5 ? 'full' : (profile?.twoFactorBackupSecret?.length ?? 0) > 0 ? 'partial' : 'none',
hideOnlineStatus: user.hideOnlineStatus,
- hasUnreadSpecifiedNotes: this.noteUnreadsRepository.count({
- where: { userId: user.id, isSpecified: true },
- take: 1,
- }).then(count => count > 0),
- hasUnreadMentions: this.noteUnreadsRepository.count({
- where: { userId: user.id, isMentioned: true },
- take: 1,
- }).then(count => count > 0),
+ hasUnreadSpecifiedNotes: false, // 後方互換性のため
+ hasUnreadMentions: false, // 後方互換性のため
+ hasUnreadChatMessages: this.chatService.hasUnreadMessages(user.id),
hasUnreadAnnouncement: unreadAnnouncements!.length > 0,
unreadAnnouncements,
hasUnreadAntenna: this.getHasUnreadAntenna(user.id),