@ -1,20 +1,39 @@
|
||||
import autobind from 'autobind-decorator';
|
||||
import read from '../../common/read-messaging-message';
|
||||
import { readUserMessagingMessage, readGroupMessagingMessage } from '../../common/read-messaging-message';
|
||||
import Channel from '../channel';
|
||||
import { UserGroupJoinings } from '../../../../models';
|
||||
|
||||
export default class extends Channel {
|
||||
public readonly chName = 'messaging';
|
||||
public static shouldShare = false;
|
||||
public static requireCredential = true;
|
||||
|
||||
private otherpartyId: string;
|
||||
private otherpartyId: string | null;
|
||||
private groupId: string | null;
|
||||
|
||||
@autobind
|
||||
public async init(params: any) {
|
||||
this.otherpartyId = params.otherparty as string;
|
||||
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(`messagingStream:${this.user!.id}-${this.otherpartyId}`, data => {
|
||||
this.subscriber.on(subCh, data => {
|
||||
this.send(data);
|
||||
});
|
||||
}
|
||||
@ -23,7 +42,11 @@ export default class extends Channel {
|
||||
public onMessage(type: string, body: any) {
|
||||
switch (type) {
|
||||
case 'read':
|
||||
read(this.user!.id, this.otherpartyId, [body.id]);
|
||||
if (this.otherpartyId) {
|
||||
readUserMessagingMessage(this.user!.id, this.otherpartyId, [body.id]);
|
||||
} else if (this.groupId) {
|
||||
readGroupMessagingMessage(this.user!.id, this.groupId, [body.id]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user