summaryrefslogtreecommitdiff
path: root/src/server/api/stream/channels/messaging.ts
blob: 8456871e6a468d16a59b863003dd1264b6e29e17 (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
import autobind from 'autobind-decorator';
import { readUserMessagingMessage, readGroupMessagingMessage, deliverReadActivity } from '../../common/read-messaging-message';
import Channel from '../channel';
import { UserGroupJoinings, Users, MessagingMessages } from '../../../../models';
import { User, ILocalUser, IRemoteUser } from '../../../../models/entities/user';

export default class extends Channel {
	public readonly chName = 'messaging';
	public static shouldShare = false;
	public static requireCredential = true;

	private otherpartyId: string | null;
	private otherparty?: User;
	private groupId: string | null;

	@autobind
	public async init(params: any) {
		this.otherpartyId = params.otherparty as string;
		this.otherparty = await Users.findOne({ id: this.otherpartyId });
		this.groupId = params.group as string;

		// Check joining
		if (this.groupId) {
			const joining = await UserGroupJoinings.findOne({
				userId: this.user!.id,
				userGroupId: this.groupId
			});

			if (joining == null) {
				return;
			}
		}

		const subCh = this.otherpartyId
			? `messagingStream:${this.user!.id}-${this.otherpartyId}`
			: `messagingStream:${this.groupId}`;

		// Subscribe messaging stream
		this.subscriber.on(subCh, data => {
			this.send(data);
		});
	}

	@autobind
	public onMessage(type: string, body: any) {
		switch (type) {
			case 'read':
				if (this.otherpartyId) {
					readUserMessagingMessage(this.user!.id, this.otherpartyId, [body.id]);

					// リモートユーザーからのメッセージだったら既読配信
					if (Users.isLocalUser(this.user!) && Users.isRemoteUser(this.otherparty!)) {
						MessagingMessages.findOne(body.id).then(message => {
							if (message) deliverReadActivity(this.user as ILocalUser, this.otherparty as IRemoteUser, message);
						});
					}
				} else if (this.groupId) {
					readGroupMessagingMessage(this.user!.id, this.groupId, [body.id]);
				}
				break;
		}
	}
}