feat: 增加group topic创建

pull/56/head
moonrailgun 3 years ago
parent ce3c119364
commit 1f8a9391d0

@ -10,15 +10,9 @@ import {
import { Base, TimeStamps } from '@typegoose/typegoose/lib/defaultClasses';
import _ from 'lodash';
import { Types } from 'mongoose';
import { allPermission } from 'tailchat-server-sdk';
import { allPermission, GroupPanelType } from 'tailchat-server-sdk';
import { User } from '../user/user';
export enum GroupPanelType {
TEXT = 0,
GROUP = 1,
PLUGIN = 2,
}
class GroupMember {
@prop({
type: () => String,
@ -60,8 +54,10 @@ export class GroupPanel {
*
* Reference: https://discord.com/developers/docs/resources/channel#channel-object-channel-types
*/
@prop()
type: number;
@prop({
type: () => Number,
})
type: GroupPanelType;
@prop()
provider?: string; // 面板提供者,为插件的标识,仅面板类型为插件时有效

@ -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;

@ -30,6 +30,7 @@ export type {
GroupRoleStruct,
GroupPanelStruct,
} from './structs/group';
export { GroupPanelType } from './structs/group';
export type { UserStruct } from './structs/user';
// db

@ -4,6 +4,14 @@ export enum GroupPanelType {
PLUGIN = 2,
}
export interface GroupPanelMeta {
/**
*
* topic
*/
variant?: 'topic';
}
interface GroupMemberStruct {
roles?: string[]; // 角色
@ -17,7 +25,7 @@ export interface GroupPanelStruct {
parentId?: string; // 父节点id
type: number; // 面板类型: Reference: https://discord.com/developers/docs/resources/channel#channel-object-channel-types
type: GroupPanelType; // 面板类型: Reference: https://discord.com/developers/docs/resources/channel#channel-object-channel-types
provider?: string; // 面板提供者,为插件的标识,仅面板类型为插件时有效
@ -26,7 +34,7 @@ export interface GroupPanelStruct {
/**
*
*/
meta?: object;
meta?: GroupPanelMeta;
}
/**

@ -1,12 +1,11 @@
import _ from 'lodash';
import { Types } from 'mongoose';
import { isValidStr } from '../../../lib/utils';
import {
import type {
Group,
GroupDocument,
GroupModel,
GroupPanel,
GroupPanelType,
} from '../../../models/group/group';
import {
TcService,
@ -19,6 +18,7 @@ import {
EntityError,
NoPermissionError,
PERMISSION,
GroupPanelType,
} from 'tailchat-server-sdk';
import moment from 'moment';

@ -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…
Cancel
Save