2022-09-17 21:27:08 +03:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-09-20 23:33:11 +03:00
|
|
|
import type { UserGroupJoiningsRepository, UsersRepository, MessagingMessagesRepository } from '@/models/index.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import type { User, ILocalUser, IRemoteUser } from '@/models/entities/User.js';
|
|
|
|
import type { UserGroup } from '@/models/entities/UserGroup.js';
|
|
|
|
import { MessagingService } from '@/core/MessagingService.js';
|
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-12-04 08:03:09 +02:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-02-27 04:07:39 +02:00
|
|
|
import Channel from '../channel.js';
|
2022-09-17 21:27:08 +03:00
|
|
|
import type { StreamMessages } from '../types.js';
|
2018-10-07 05:06:17 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
class MessagingChannel extends Channel {
|
2018-10-11 17:01:57 +03:00
|
|
|
public readonly chName = 'messaging';
|
2018-10-11 17:07:20 +03:00
|
|
|
public static shouldShare = false;
|
2018-11-10 19:22:34 +02:00
|
|
|
public static requireCredential = true;
|
2018-10-11 17:01:57 +03:00
|
|
|
|
2019-05-18 14:36:33 +03:00
|
|
|
private otherpartyId: string | null;
|
2021-06-30 17:33:50 +03:00
|
|
|
private otherparty: User | null;
|
2019-05-18 14:36:33 +03:00
|
|
|
private groupId: string | null;
|
2021-10-20 19:04:10 +03:00
|
|
|
private subCh: `messagingStream:${User['id']}-${User['id']}` | `messagingStream:${UserGroup['id']}`;
|
2021-02-21 05:26:49 +02:00
|
|
|
private typers: Record<User['id'], Date> = {};
|
|
|
|
private emitTypersIntervalId: ReturnType<typeof setInterval>;
|
2018-10-07 05:06:17 +03:00
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
constructor(
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
private userGroupJoiningsRepository: UserGroupJoiningsRepository,
|
|
|
|
private messagingMessagesRepository: MessagingMessagesRepository,
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private messagingService: MessagingService,
|
|
|
|
|
|
|
|
id: string,
|
|
|
|
connection: Channel['connection'],
|
|
|
|
) {
|
2022-02-27 04:07:39 +02:00
|
|
|
super(id, connection);
|
2022-12-04 08:03:09 +02:00
|
|
|
//this.onEvent = this.onEvent.bind(this);
|
|
|
|
//this.onMessage = this.onMessage.bind(this);
|
|
|
|
//this.emitTypers = this.emitTypers.bind(this);
|
2022-02-27 04:07:39 +02:00
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2018-10-07 05:06:17 +03:00
|
|
|
public async init(params: any) {
|
2021-06-30 17:33:50 +03:00
|
|
|
this.otherpartyId = params.otherparty;
|
2022-09-17 21:27:08 +03:00
|
|
|
this.otherparty = this.otherpartyId ? await this.usersRepository.findOneByOrFail({ id: this.otherpartyId }) : null;
|
2021-06-30 17:33:50 +03:00
|
|
|
this.groupId = params.group;
|
2019-05-18 14:36:33 +03:00
|
|
|
|
|
|
|
// Check joining
|
|
|
|
if (this.groupId) {
|
2022-09-17 21:27:08 +03:00
|
|
|
const joining = await this.userGroupJoiningsRepository.findOneBy({
|
2019-05-18 14:36:33 +03:00
|
|
|
userId: this.user!.id,
|
2021-12-09 16:58:30 +02:00
|
|
|
userGroupId: this.groupId,
|
2019-05-18 14:36:33 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (joining == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-21 05:26:49 +02:00
|
|
|
this.emitTypersIntervalId = setInterval(this.emitTypers, 5000);
|
|
|
|
|
|
|
|
this.subCh = this.otherpartyId
|
2019-05-18 14:36:33 +03:00
|
|
|
? `messagingStream:${this.user!.id}-${this.otherpartyId}`
|
|
|
|
: `messagingStream:${this.groupId}`;
|
2018-10-07 05:06:17 +03:00
|
|
|
|
|
|
|
// Subscribe messaging stream
|
2021-02-21 05:26:49 +02:00
|
|
|
this.subscriber.on(this.subCh, this.onEvent);
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2021-10-20 19:04:10 +03:00
|
|
|
private onEvent(data: StreamMessages['messaging']['payload'] | StreamMessages['groupMessaging']['payload']) {
|
2021-02-21 05:26:49 +02:00
|
|
|
if (data.type === 'typing') {
|
|
|
|
const id = data.body;
|
|
|
|
const begin = this.typers[id] == null;
|
|
|
|
this.typers[id] = new Date();
|
|
|
|
if (begin) {
|
|
|
|
this.emitTypers();
|
|
|
|
}
|
|
|
|
} else {
|
2018-10-07 05:06:17 +03:00
|
|
|
this.send(data);
|
2021-02-21 05:26:49 +02:00
|
|
|
}
|
2018-10-07 05:06:17 +03:00
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2018-10-07 05:06:17 +03:00
|
|
|
public onMessage(type: string, body: any) {
|
|
|
|
switch (type) {
|
|
|
|
case 'read':
|
2019-05-18 14:36:33 +03:00
|
|
|
if (this.otherpartyId) {
|
2022-09-17 21:27:08 +03:00
|
|
|
this.messagingService.readUserMessagingMessage(this.user!.id, this.otherpartyId, [body.id]);
|
2019-12-14 20:37:19 +02:00
|
|
|
|
|
|
|
// リモートユーザーからのメッセージだったら既読配信
|
2022-09-17 21:27:08 +03:00
|
|
|
if (this.userEntityService.isLocalUser(this.user!) && this.userEntityService.isRemoteUser(this.otherparty!)) {
|
|
|
|
this.messagingMessagesRepository.findOneBy({ id: body.id }).then(message => {
|
|
|
|
if (message) this.messagingService.deliverReadActivity(this.user as ILocalUser, this.otherparty as IRemoteUser, message);
|
2019-12-14 20:37:19 +02:00
|
|
|
});
|
|
|
|
}
|
2019-05-18 14:36:33 +03:00
|
|
|
} else if (this.groupId) {
|
2022-09-17 21:27:08 +03:00
|
|
|
this.messagingService.readGroupMessagingMessage(this.user!.id, this.groupId, [body.id]);
|
2019-05-18 14:36:33 +03:00
|
|
|
}
|
2018-10-07 05:06:17 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-02-21 05:26:49 +02:00
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2021-02-21 05:26:49 +02:00
|
|
|
private async emitTypers() {
|
|
|
|
const now = new Date();
|
|
|
|
|
|
|
|
// Remove not typing users
|
|
|
|
for (const [userId, date] of Object.entries(this.typers)) {
|
|
|
|
if (now.getTime() - date.getTime() > 5000) delete this.typers[userId];
|
|
|
|
}
|
|
|
|
|
2022-09-17 21:27:08 +03:00
|
|
|
const users = await this.userEntityService.packMany(Object.keys(this.typers), null, { detail: false });
|
2021-02-21 05:26:49 +02:00
|
|
|
|
|
|
|
this.send({
|
|
|
|
type: 'typers',
|
|
|
|
body: users,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2021-02-21 05:26:49 +02:00
|
|
|
public dispose() {
|
|
|
|
this.subscriber.off(this.subCh, this.onEvent);
|
2021-02-21 06:38:03 +02:00
|
|
|
|
2021-02-21 05:26:49 +02:00
|
|
|
clearInterval(this.emitTypersIntervalId);
|
|
|
|
}
|
2018-10-07 05:06:17 +03:00
|
|
|
}
|
2022-09-17 21:27:08 +03:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class MessagingChannelService {
|
|
|
|
public readonly shouldShare = MessagingChannel.shouldShare;
|
|
|
|
public readonly requireCredential = MessagingChannel.requireCredential;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userGroupJoiningsRepository)
|
|
|
|
private userGroupJoiningsRepository: UserGroupJoiningsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.messagingMessagesRepository)
|
|
|
|
private messagingMessagesRepository: MessagingMessagesRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private messagingService: MessagingService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 08:03:09 +02:00
|
|
|
@bindThis
|
2022-09-17 21:27:08 +03:00
|
|
|
public create(id: string, connection: Channel['connection']): MessagingChannel {
|
|
|
|
return new MessagingChannel(
|
|
|
|
this.usersRepository,
|
|
|
|
this.userGroupJoiningsRepository,
|
|
|
|
this.messagingMessagesRepository,
|
|
|
|
this.userEntityService,
|
|
|
|
this.messagingService,
|
|
|
|
id,
|
|
|
|
connection,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|