summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/MessagingService.ts
blob: f4a1090658c2de31be9fc1f55b4847f099e3e2a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import { Inject, Injectable } from '@nestjs/common';
import { In, Not } from 'typeorm';
import { DI } from '@/di-symbols.js';
import type { Config } from '@/config.js';
import type { DriveFile } from '@/models/entities/DriveFile.js';
import type { MessagingMessage } from '@/models/entities/MessagingMessage.js';
import type { Note } from '@/models/entities/Note.js';
import type { User, CacheableUser, IRemoteUser } from '@/models/entities/User.js';
import type { UserGroup } from '@/models/entities/UserGroup.js';
import { QueueService } from '@/core/QueueService.js';
import { toArray } from '@/misc/prelude/array.js';
import { IdentifiableError } from '@/misc/identifiable-error.js';
import type { MessagingMessagesRepository, MutingsRepository, UserGroupJoiningsRepository, UsersRepository } from '@/models/index.js';
import { IdService } from '@/core/IdService.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
import { MessagingMessageEntityService } from '@/core/entities/MessagingMessageEntityService.js';
import { PushNotificationService } from '@/core/PushNotificationService.js';
import { bindThis } from '@/decorators.js';

@Injectable()
export class MessagingService {
	constructor(
		@Inject(DI.config)
		private config: Config,

		@Inject(DI.usersRepository)
		private usersRepository: UsersRepository,

		@Inject(DI.messagingMessagesRepository)
		private messagingMessagesRepository: MessagingMessagesRepository,

		@Inject(DI.userGroupJoiningsRepository)
		private userGroupJoiningsRepository: UserGroupJoiningsRepository,

		@Inject(DI.mutingsRepository)
		private mutingsRepository: MutingsRepository,

		private userEntityService: UserEntityService,
		private messagingMessageEntityService: MessagingMessageEntityService,
		private idService: IdService,
		private globalEventService: GlobalEventService,
		private apRendererService: ApRendererService,
		private queueService: QueueService,
		private pushNotificationService: PushNotificationService,
	) {
	}

	@bindThis
	public async createMessage(user: { id: User['id']; host: User['host']; }, recipientUser: CacheableUser | undefined, recipientGroup: UserGroup | undefined, text: string | null | undefined, file: DriveFile | null, uri?: string) {
		const message = {
			id: this.idService.genId(),
			createdAt: new Date(),
			fileId: file ? file.id : null,
			recipientId: recipientUser ? recipientUser.id : null,
			groupId: recipientGroup ? recipientGroup.id : null,
			text: text ? text.trim() : null,
			userId: user.id,
			isRead: false,
			reads: [] as any[],
			uri,
		} as MessagingMessage;
	
		await this.messagingMessagesRepository.insert(message);
	
		const messageObj = await this.messagingMessageEntityService.pack(message);
	
		if (recipientUser) {
			if (this.userEntityService.isLocalUser(user)) {
				// 自分のストリーム
				this.globalEventService.publishMessagingStream(message.userId, recipientUser.id, 'message', messageObj);
				this.globalEventService.publishMessagingIndexStream(message.userId, 'message', messageObj);
				this.globalEventService.publishMainStream(message.userId, 'messagingMessage', messageObj);
			}
	
			if (this.userEntityService.isLocalUser(recipientUser)) {
				// 相手のストリーム
				this.globalEventService.publishMessagingStream(recipientUser.id, message.userId, 'message', messageObj);
				this.globalEventService.publishMessagingIndexStream(recipientUser.id, 'message', messageObj);
				this.globalEventService.publishMainStream(recipientUser.id, 'messagingMessage', messageObj);
			}
		} else if (recipientGroup) {
			// グループのストリーム
			this.globalEventService.publishGroupMessagingStream(recipientGroup.id, 'message', messageObj);
	
			// メンバーのストリーム
			const joinings = await this.userGroupJoiningsRepository.findBy({ userGroupId: recipientGroup.id });
			for (const joining of joinings) {
				this.globalEventService.publishMessagingIndexStream(joining.userId, 'message', messageObj);
				this.globalEventService.publishMainStream(joining.userId, 'messagingMessage', messageObj);
			}
		}
	
		// 2秒経っても(今回作成した)メッセージが既読にならなかったら「未読のメッセージがありますよ」イベントを発行する
		setTimeout(async () => {
			const freshMessage = await this.messagingMessagesRepository.findOneBy({ id: message.id });
			if (freshMessage == null) return; // メッセージが削除されている場合もある
	
			if (recipientUser && this.userEntityService.isLocalUser(recipientUser)) {
				if (freshMessage.isRead) return; // 既読
	
				//#region ただしミュートされているなら発行しない
				const mute = await this.mutingsRepository.findBy({
					muterId: recipientUser.id,
				});
				if (mute.map(m => m.muteeId).includes(user.id)) return;
				//#endregion
	
				this.globalEventService.publishMainStream(recipientUser.id, 'unreadMessagingMessage', messageObj);
				this.pushNotificationService.pushNotification(recipientUser.id, 'unreadMessagingMessage', messageObj);
			} else if (recipientGroup) {
				const joinings = await this.userGroupJoiningsRepository.findBy({ userGroupId: recipientGroup.id, userId: Not(user.id) });
				for (const joining of joinings) {
					if (freshMessage.reads.includes(joining.userId)) return; // 既読
					this.globalEventService.publishMainStream(joining.userId, 'unreadMessagingMessage', messageObj);
					this.pushNotificationService.pushNotification(joining.userId, 'unreadMessagingMessage', messageObj);
				}
			}
		}, 2000);
	
		if (recipientUser && this.userEntityService.isLocalUser(user) && this.userEntityService.isRemoteUser(recipientUser)) {
			const note = {
				id: message.id,
				createdAt: message.createdAt,
				fileIds: message.fileId ? [message.fileId] : [],
				text: message.text,
				userId: message.userId,
				visibility: 'specified',
				mentions: [recipientUser].map(u => u.id),
				mentionedRemoteUsers: JSON.stringify([recipientUser].map(u => ({
					uri: u.uri,
					username: u.username,
					host: u.host,
				}))),
			} as Note;
	
			const activity = this.apRendererService.renderActivity(this.apRendererService.renderCreate(await this.apRendererService.renderNote(note, false, true), note));
	
			this.queueService.deliver(user, activity, recipientUser.inbox);
		}
		return messageObj;
	}

	@bindThis
	public async deleteMessage(message: MessagingMessage) {
		await this.messagingMessagesRepository.delete(message.id);
		this.postDeleteMessage(message);
	}
	
	@bindThis
	private async postDeleteMessage(message: MessagingMessage) {
		if (message.recipientId) {
			const user = await this.usersRepository.findOneByOrFail({ id: message.userId });
			const recipient = await this.usersRepository.findOneByOrFail({ id: message.recipientId });
	
			if (this.userEntityService.isLocalUser(user)) this.globalEventService.publishMessagingStream(message.userId, message.recipientId, 'deleted', message.id);
			if (this.userEntityService.isLocalUser(recipient)) this.globalEventService.publishMessagingStream(message.recipientId, message.userId, 'deleted', message.id);
	
			if (this.userEntityService.isLocalUser(user) && this.userEntityService.isRemoteUser(recipient)) {
				const activity = this.apRendererService.renderActivity(this.apRendererService.renderDelete(this.apRendererService.renderTombstone(`${this.config.url}/notes/${message.id}`), user));
				this.queueService.deliver(user, activity, recipient.inbox);
			}
		} else if (message.groupId) {
			this.globalEventService.publishGroupMessagingStream(message.groupId, 'deleted', message.id);
		}
	}

	/**
	 * Mark messages as read
	 */
	@bindThis
	public async readUserMessagingMessage(
		userId: User['id'],
		otherpartyId: User['id'],
		messageIds: MessagingMessage['id'][],
	) {
		if (messageIds.length === 0) return;

		const messages = await this.messagingMessagesRepository.findBy({
			id: In(messageIds),
		});

		for (const message of messages) {
			if (message.recipientId !== userId) {
				throw new IdentifiableError('e140a4bf-49ce-4fb6-b67c-b78dadf6b52f', 'Access denied (user).');
			}
		}

		// Update documents
		await this.messagingMessagesRepository.update({
			id: In(messageIds),
			userId: otherpartyId,
			recipientId: userId,
			isRead: false,
		}, {
			isRead: true,
		});

		// Publish event
		this.globalEventService.publishMessagingStream(otherpartyId, userId, 'read', messageIds);
		this.globalEventService.publishMessagingIndexStream(userId, 'read', messageIds);

		if (!await this.userEntityService.getHasUnreadMessagingMessage(userId)) {
		// 全ての(いままで未読だった)自分宛てのメッセージを(これで)読みましたよというイベントを発行
			this.globalEventService.publishMainStream(userId, 'readAllMessagingMessages');
			this.pushNotificationService.pushNotification(userId, 'readAllMessagingMessages', undefined);
		} else {
		// そのユーザーとのメッセージで未読がなければイベント発行
			const count = await this.messagingMessagesRepository.count({
				where: {
					userId: otherpartyId,
					recipientId: userId,
					isRead: false,
				},
				take: 1,
			});

			if (!count) {
				this.pushNotificationService.pushNotification(userId, 'readAllMessagingMessagesOfARoom', { userId: otherpartyId });
			}
		}
	}

	/**
	 * Mark messages as read
	 */
	@bindThis
	public async readGroupMessagingMessage(
		userId: User['id'],
		groupId: UserGroup['id'],
		messageIds: MessagingMessage['id'][],
	) {
		if (messageIds.length === 0) return;

		// check joined
		const joining = await this.userGroupJoiningsRepository.findOneBy({
			userId: userId,
			userGroupId: groupId,
		});

		if (joining == null) {
			throw new IdentifiableError('930a270c-714a-46b2-b776-ad27276dc569', 'Access denied (group).');
		}

		const messages = await this.messagingMessagesRepository.findBy({
			id: In(messageIds),
		});

		const reads: MessagingMessage['id'][] = [];

		for (const message of messages) {
			if (message.userId === userId) continue;
			if (message.reads.includes(userId)) continue;

			// Update document
			await this.messagingMessagesRepository.createQueryBuilder().update()
				.set({
					reads: (() => `array_append("reads", '${joining.userId}')`) as any,
				})
				.where('id = :id', { id: message.id })
				.execute();

			reads.push(message.id);
		}

		// Publish event
		this.globalEventService.publishGroupMessagingStream(groupId, 'read', {
			ids: reads,
			userId: userId,
		});
		this.globalEventService.publishMessagingIndexStream(userId, 'read', reads);

		if (!await this.userEntityService.getHasUnreadMessagingMessage(userId)) {
		// 全ての(いままで未読だった)自分宛てのメッセージを(これで)読みましたよというイベントを発行
			this.globalEventService.publishMainStream(userId, 'readAllMessagingMessages');
			this.pushNotificationService.pushNotification(userId, 'readAllMessagingMessages', undefined);
		} else {
		// そのグループにおいて未読がなければイベント発行
			const unreadExist = await this.messagingMessagesRepository.createQueryBuilder('message')
				.where('message.groupId = :groupId', { groupId: groupId })
				.andWhere('message.userId != :userId', { userId: userId })
				.andWhere('NOT (:userId = ANY(message.reads))', { userId: userId })
				.andWhere('message.createdAt > :joinedAt', { joinedAt: joining.createdAt }) // 自分が加入する前の会話については、未読扱いしない
				.getOne().then(x => x != null);

			if (!unreadExist) {
				this.pushNotificationService.pushNotification(userId, 'readAllMessagingMessagesOfARoom', { groupId });
			}
		}
	}

	@bindThis
	public async deliverReadActivity(user: { id: User['id']; host: null; }, recipient: IRemoteUser, messages: MessagingMessage | MessagingMessage[]) {
		messages = toArray(messages).filter(x => x.uri);
		const contents = messages.map(x => this.apRendererService.renderRead(user, x));

		if (contents.length > 1) {
			const collection = this.apRendererService.renderOrderedCollection(null, contents.length, undefined, undefined, contents);
			this.queueService.deliver(user, this.apRendererService.renderActivity(collection), recipient.inbox);
		} else {
			for (const content of contents) {
				this.queueService.deliver(user, this.apRendererService.renderActivity(content), recipient.inbox);
			}
		}
	}
}