Sharkey/src/server/api/stream/channels/messaging.ts

54 lines
1.3 KiB
TypeScript
Raw Normal View History

import autobind from 'autobind-decorator';
2019-05-18 14:36:33 +03:00
import { readUserMessagingMessage, readGroupMessagingMessage } from '../../common/read-messaging-message';
import Channel from '../channel';
2019-05-18 14:36:33 +03:00
import { UserGroupJoinings } from '../../../../models';
export default class extends Channel {
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;
2019-05-18 14:36:33 +03:00
private otherpartyId: string | null;
private groupId: string | null;
@autobind
public async init(params: any) {
this.otherpartyId = params.otherparty as string;
2019-05-18 14:36:33 +03: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}`;
// Subscribe messaging stream
2019-05-18 14:36:33 +03:00
this.subscriber.on(subCh, data => {
this.send(data);
});
}
@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]);
} else if (this.groupId) {
readGroupMessagingMessage(this.user!.id, this.groupId, [body.id]);
}
break;
}
}
}