2018-10-07 05:06:17 +03:00
|
|
|
import autobind from 'autobind-decorator';
|
2019-12-14 20:37:19 +02:00
|
|
|
import { readUserMessagingMessage, readGroupMessagingMessage, deliverReadActivity } from '../../common/read-messaging-message';
|
2018-10-07 05:06:17 +03:00
|
|
|
import Channel from '../channel';
|
2019-12-14 20:37:19 +02:00
|
|
|
import { UserGroupJoinings, Users, MessagingMessages } from '../../../../models';
|
|
|
|
import { User, ILocalUser, IRemoteUser } from '../../../../models/entities/user';
|
2018-10-07 05:06:17 +03:00
|
|
|
|
|
|
|
export default class 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-02-21 05:26:49 +02:00
|
|
|
private subCh: string;
|
|
|
|
private typers: Record<User['id'], Date> = {};
|
|
|
|
private emitTypersIntervalId: ReturnType<typeof setInterval>;
|
2018-10-07 05:06:17 +03:00
|
|
|
|
|
|
|
@autobind
|
|
|
|
public async init(params: any) {
|
2021-06-30 17:33:50 +03:00
|
|
|
this.otherpartyId = params.otherparty;
|
|
|
|
this.otherparty = this.otherpartyId ? await Users.findOneOrFail({ id: this.otherpartyId }) : null;
|
|
|
|
this.groupId = params.group;
|
2019-05-18 14:36:33 +03:00
|
|
|
|
|
|
|
// Check joining
|
|
|
|
if (this.groupId) {
|
|
|
|
const joining = await UserGroupJoinings.findOne({
|
|
|
|
userId: this.user!.id,
|
|
|
|
userGroupId: this.groupId
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
private onEvent(data: any) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public onMessage(type: string, body: any) {
|
|
|
|
switch (type) {
|
|
|
|
case 'read':
|
2019-05-18 14:36:33 +03:00
|
|
|
if (this.otherpartyId) {
|
|
|
|
readUserMessagingMessage(this.user!.id, this.otherpartyId, [body.id]);
|
2019-12-14 20:37:19 +02:00
|
|
|
|
|
|
|
// リモートユーザーからのメッセージだったら既読配信
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2019-05-18 14:36:33 +03:00
|
|
|
} else if (this.groupId) {
|
|
|
|
readGroupMessagingMessage(this.user!.id, this.groupId, [body.id]);
|
|
|
|
}
|
2018-10-07 05:06:17 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-02-21 05:26:49 +02:00
|
|
|
|
|
|
|
@autobind
|
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
const users = await Users.packMany(Object.keys(this.typers), null, { detail: false });
|
|
|
|
|
|
|
|
this.send({
|
|
|
|
type: 'typers',
|
|
|
|
body: users,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
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
|
|
|
}
|