mirror of https://github.com/msgbyte/tailchat
feat: 增加group topic创建
parent
ce3c119364
commit
1f8a9391d0
@ -0,0 +1,64 @@
|
|||||||
|
import {
|
||||||
|
getModelForClass,
|
||||||
|
DocumentType,
|
||||||
|
Ref,
|
||||||
|
prop,
|
||||||
|
} from '@typegoose/typegoose';
|
||||||
|
import { Base, TimeStamps } from '@typegoose/typegoose/lib/defaultClasses';
|
||||||
|
import type { Types } from 'mongoose';
|
||||||
|
import { nanoid } from 'nanoid';
|
||||||
|
import { User } from '../user/user';
|
||||||
|
import { Group } from './group';
|
||||||
|
|
||||||
|
class GroupTopicComment {
|
||||||
|
@prop({
|
||||||
|
default: () => nanoid(8),
|
||||||
|
})
|
||||||
|
id: string;
|
||||||
|
|
||||||
|
@prop()
|
||||||
|
content: string;
|
||||||
|
|
||||||
|
@prop({ ref: () => User })
|
||||||
|
author: Ref<User>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 回复他人评论的id
|
||||||
|
*/
|
||||||
|
@prop()
|
||||||
|
replyCommentId?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class GroupTopic extends TimeStamps implements Base {
|
||||||
|
_id: Types.ObjectId;
|
||||||
|
id: string;
|
||||||
|
|
||||||
|
@prop()
|
||||||
|
content: string;
|
||||||
|
|
||||||
|
@prop({ ref: () => User })
|
||||||
|
author: Ref<User>;
|
||||||
|
|
||||||
|
@prop({ ref: () => Group })
|
||||||
|
groupId: Ref<Group>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 会话面板id
|
||||||
|
*/
|
||||||
|
@prop()
|
||||||
|
panelId: string;
|
||||||
|
|
||||||
|
@prop({
|
||||||
|
type: () => GroupTopicComment,
|
||||||
|
default: [],
|
||||||
|
})
|
||||||
|
comment: GroupTopicComment[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export type GroupTopicDocument = DocumentType<GroupTopic>;
|
||||||
|
|
||||||
|
const model = getModelForClass(GroupTopic);
|
||||||
|
|
||||||
|
export type GroupTopicModel = typeof model;
|
||||||
|
|
||||||
|
export default model;
|
@ -0,0 +1,84 @@
|
|||||||
|
import type {
|
||||||
|
GroupTopicDocument,
|
||||||
|
GroupTopicModel,
|
||||||
|
} from '../../../models/group/topic';
|
||||||
|
import {
|
||||||
|
TcService,
|
||||||
|
TcDbService,
|
||||||
|
TcContext,
|
||||||
|
call,
|
||||||
|
GroupPanelType,
|
||||||
|
} from 'tailchat-server-sdk';
|
||||||
|
import { Types } from 'mongoose';
|
||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
|
interface GroupTopicService
|
||||||
|
extends TcService,
|
||||||
|
TcDbService<GroupTopicDocument, GroupTopicModel> {}
|
||||||
|
class GroupTopicService extends TcService {
|
||||||
|
get serviceName(): string {
|
||||||
|
return 'group.topic';
|
||||||
|
}
|
||||||
|
|
||||||
|
onInit(): void {
|
||||||
|
this.registerLocalDb(require('../../../models/group/topic').default);
|
||||||
|
|
||||||
|
this.registerAction('create', this.create, {
|
||||||
|
params: {
|
||||||
|
groupId: 'string',
|
||||||
|
panelId: 'string',
|
||||||
|
content: 'string',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建一条Topic
|
||||||
|
*/
|
||||||
|
async create(
|
||||||
|
ctx: TcContext<{
|
||||||
|
groupId: string;
|
||||||
|
panelId: string;
|
||||||
|
content: string;
|
||||||
|
}>
|
||||||
|
) {
|
||||||
|
const { groupId, panelId, content } = ctx.params;
|
||||||
|
const userId = ctx.meta.userId;
|
||||||
|
const t = ctx.meta.t;
|
||||||
|
|
||||||
|
// 鉴权
|
||||||
|
const group = await call(ctx).getGroupInfo(groupId);
|
||||||
|
const isMember = group.members.some((member) => member.userId === userId);
|
||||||
|
if (!isMember) {
|
||||||
|
throw new Error(t('不是该群组成员'));
|
||||||
|
}
|
||||||
|
|
||||||
|
const targetPanel = group.panels.find((p) => p.id === panelId);
|
||||||
|
|
||||||
|
if (!targetPanel) {
|
||||||
|
throw new Error(t('面板不存在'));
|
||||||
|
}
|
||||||
|
const isPanelValid =
|
||||||
|
targetPanel.type === GroupPanelType.TEXT &&
|
||||||
|
_.get(targetPanel, ['meta', 'variant']) === 'topic';
|
||||||
|
if (!isPanelValid) {
|
||||||
|
throw new Error(t('面板类型不合法'));
|
||||||
|
}
|
||||||
|
|
||||||
|
const topic = await this.adapter.model.create({
|
||||||
|
groupId: new Types.ObjectId(groupId),
|
||||||
|
panelId,
|
||||||
|
content,
|
||||||
|
author: new Types.ObjectId(userId),
|
||||||
|
comment: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
const json = await this.transformDocuments(ctx, {}, topic);
|
||||||
|
|
||||||
|
this.roomcastNotify(ctx, groupId, 'create', json);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default GroupTopicService;
|
Loading…
Reference in New Issue