You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tailchat/server/plugins/com.msgbyte.meeting/services/meeting.service.dev.ts

104 lines
2.3 KiB
TypeScript

import { TcService, TcDbService, TcContext } from 'tailchat-server-sdk';
import { generateRandomNumStr, isValidStr } from '../../../lib/utils';
import type { MeetingDocument, MeetingModel } from '../models/meeting';
/**
* 任务管理服务
*/
interface MeetingService
extends TcService,
TcDbService<MeetingDocument, MeetingModel> {}
class MeetingService extends TcService {
get serviceName() {
return 'plugin:com.msgbyte.meeting';
}
get tailchatMeetingUrl() {
return process.env.TAILCHAT_MEETING_URL;
}
onInit() {
// this.registerLocalDb(require('../models/meeting').default);
this.registerAction('available', this.available);
if (!isValidStr(this.tailchatMeetingUrl)) {
return;
}
this.registerAction('url', this.url);
this.registerAction('create', this.create);
this.registerAction('getJoinMeetingInfo', this.getJoinMeetingInfo);
this.registerAction(
'inviteUserConverseJoinMeeting',
this.inviteUserConverseJoinMeeting,
{
params: {
meetingId: 'string',
converseId: 'string',
},
}
);
}
available(ctx: TcContext) {
return Boolean(this.tailchatMeetingUrl);
}
/**
* 获取连接地址
*/
url(ctx: TcContext) {
return {
tailchatMeetingUrl: this.tailchatMeetingUrl,
};
}
/**
* 创建房间
*
* TODO: 先手动返回一个随机生成的房间号
*/
create(ctx: TcContext) {
const roomId = generateRandomNumStr(9);
return {
roomId,
url: `${this.tailchatMeetingUrl}/room/${roomId}`,
};
}
/**
* 获取加入会议的基本信息
*/
getJoinMeetingInfo(ctx: TcContext) {
const user = ctx.meta.user;
return {
signalingUrl: this.tailchatMeetingUrl.replace('https', 'wss'),
userId: String(user._id),
nickname: user.nickname,
avatar: user.avatar,
};
}
/**
* 邀请多人会话加入会议
*/
async inviteUserConverseJoinMeeting(
ctx: TcContext<{
meetingId: string;
converseId: string;
}>
) {
const { meetingId, converseId } = ctx.params;
const userId = ctx.meta.userId;
await this.roomcastNotify(ctx, converseId, 'inviteJoinMeeting', {
meetingId,
fromId: userId,
});
}
}
export default MeetingService;