2018-10-07 11:06:17 +09:00
|
|
|
import autobind from 'autobind-decorator';
|
2019-05-18 20:36:33 +09:00
|
|
|
import { readUserMessagingMessage, readGroupMessagingMessage } from '../../common/read-messaging-message';
|
2018-10-07 11:06:17 +09:00
|
|
|
import Channel from '../channel';
|
2019-05-18 20:36:33 +09:00
|
|
|
import { UserGroupJoinings } from '../../../../models';
|
2018-10-07 11:06:17 +09:00
|
|
|
|
|
|
|
export default class extends Channel {
|
2018-10-11 23:01:57 +09:00
|
|
|
public readonly chName = 'messaging';
|
2018-10-11 23:07:20 +09:00
|
|
|
public static shouldShare = false;
|
2018-11-11 02:22:34 +09:00
|
|
|
public static requireCredential = true;
|
2018-10-11 23:01:57 +09:00
|
|
|
|
2019-05-18 20:36:33 +09:00
|
|
|
private otherpartyId: string | null;
|
|
|
|
private groupId: string | null;
|
2018-10-07 11:06:17 +09:00
|
|
|
|
|
|
|
@autobind
|
|
|
|
public async init(params: any) {
|
|
|
|
this.otherpartyId = params.otherparty as string;
|
2019-05-18 20:36:33 +09:00
|
|
|
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}`;
|
2018-10-07 11:06:17 +09:00
|
|
|
|
|
|
|
// Subscribe messaging stream
|
2019-05-18 20:36:33 +09:00
|
|
|
this.subscriber.on(subCh, data => {
|
2018-10-07 11:06:17 +09:00
|
|
|
this.send(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public onMessage(type: string, body: any) {
|
|
|
|
switch (type) {
|
|
|
|
case 'read':
|
2019-05-18 20:36:33 +09:00
|
|
|
if (this.otherpartyId) {
|
|
|
|
readUserMessagingMessage(this.user!.id, this.otherpartyId, [body.id]);
|
|
|
|
} else if (this.groupId) {
|
|
|
|
readGroupMessagingMessage(this.user!.id, this.groupId, [body.id]);
|
|
|
|
}
|
2018-10-07 11:06:17 +09:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|