feat: topic获取所有列表网络请求

pull/56/head
moonrailgun 3 years ago
parent f350e45d02
commit aa61ff5470

@ -44,13 +44,12 @@ export const declarationCommand: CommandModule = {
content = content =
"declare module '@capital/common';\ndeclare module '@capital/component';\n"; "declare module '@capital/common';\ndeclare module '@capital/component';\n";
} else if (source === 'github') { } else if (source === 'github') {
const spinner = ora('正在从 Github 下载插件类型声明').start(); const url =
'https://raw.githubusercontent.com/msgbyte/tailchat/master/client/web/tailchat.d.ts';
content = await got const spinner = ora(`正在从 Github 下载插件类型声明: ${url}`).start();
.get(
'https://raw.githubusercontent.com/msgbyte/tailchat/master/client/web/tailchat.d.ts' content = await got.get(url).then((res) => res.body);
)
.then((res) => res.body);
spinner.succeed('声明文件下载完毕'); spinner.succeed('声明文件下载完毕');
} }

@ -1,4 +1,4 @@
import React, { PropsWithChildren, useEffect } from 'react'; import React, { PropsWithChildren, useEffect, useMemo } from 'react';
import { t, useGroupPanelInfo } from 'tailchat-shared'; import { t, useGroupPanelInfo } from 'tailchat-shared';
import _isNil from 'lodash/isNil'; import _isNil from 'lodash/isNil';
import { MembersPanel } from './MembersPanel'; import { MembersPanel } from './MembersPanel';
@ -46,26 +46,35 @@ export const GroupPanelWrapper: React.FC<GroupPanelWrapperProps> = React.memo(
const panelInfo = useGroupPanelInfo(props.groupId, props.panelId); const panelInfo = useGroupPanelInfo(props.groupId, props.panelId);
useRecordGroupPanel(props.groupId, props.panelId); useRecordGroupPanel(props.groupId, props.panelId);
const { hasOpenedPanel, openPanelWindow, closePanelWindow } =
usePanelWindow(`/panel/group/${props.groupId}/${props.panelId}`);
const groupPanelContextValue = useMemo(
() => ({
groupId: props.groupId,
panelId: props.panelId,
}),
[props.groupId, props.panelId]
);
if (_isNil(panelInfo)) { if (_isNil(panelInfo)) {
return null; return null;
} }
const { hasOpenedPanel, openPanelWindow, closePanelWindow } =
usePanelWindow(`/panel/group/${props.groupId}/${props.panelId}`);
if (hasOpenedPanel) { if (hasOpenedPanel) {
return <OpenedPanelTip onClosePanelWindow={closePanelWindow} />; return <OpenedPanelTip onClosePanelWindow={closePanelWindow} />;
} }
if (!props.showHeader) { if (!props.showHeader) {
return ( return (
<GroupPanelContext.Provider value={props}> <GroupPanelContext.Provider value={groupPanelContextValue}>
{props.children} {props.children}
</GroupPanelContext.Provider> </GroupPanelContext.Provider>
); );
} }
return ( return (
<GroupPanelContext.Provider value={props}> <GroupPanelContext.Provider value={groupPanelContextValue}>
<CommonPanelWrapper <CommonPanelWrapper
header={panelInfo.name} header={panelInfo.name}
actions={(setRightPanel) => [ actions={(setRightPanel) => [

@ -11,6 +11,7 @@ export {
Switch, Switch,
Tooltip, Tooltip,
notification, notification,
Empty,
} from 'antd'; } from 'antd';
export { Avatar, SensitiveText } from 'tailchat-design'; export { Avatar, SensitiveText } from 'tailchat-design';
export const TextArea = Input.TextArea; export const TextArea = Input.TextArea;

@ -252,6 +252,8 @@ declare module '@capital/component' {
*/ */
export const notification: any; export const notification: any;
export const Empty: any;
export const Avatar: any; export const Avatar: any;
export const SensitiveText: React.FC<{ className?: string; text: string }>; export const SensitiveText: React.FC<{ className?: string; text: string }>;

@ -1,5 +1,5 @@
import { db } from 'tailchat-server-sdk'; import { db } from 'tailchat-server-sdk';
const { getModelForClass, prop, TimeStamps } = db; const { getModelForClass, prop, TimeStamps, modelOptions } = db;
import type { Types } from 'mongoose'; import type { Types } from 'mongoose';
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
@ -22,6 +22,11 @@ class GroupTopicComment {
replyCommentId?: string; replyCommentId?: string;
} }
@modelOptions({
options: {
customName: 'p_topic',
},
})
export class GroupTopic extends TimeStamps implements db.Base { export class GroupTopic extends TimeStamps implements db.Base {
_id: Types.ObjectId; _id: Types.ObjectId;
id: string; id: string;

@ -8,7 +8,6 @@ import {
call, call,
} from 'tailchat-server-sdk'; } from 'tailchat-server-sdk';
import type { GroupTopicDocument, GroupTopicModel } from '../models/topic'; import type { GroupTopicDocument, GroupTopicModel } from '../models/topic';
const { Types } = db;
/** /**
* *
@ -24,6 +23,14 @@ class GroupTopicService extends TcService {
onInit(): void { onInit(): void {
this.registerLocalDb(require('../models/topic').default); this.registerLocalDb(require('../models/topic').default);
this.registerAction('list', this.list, {
params: {
groupId: 'string',
panelId: 'string',
page: { type: 'number', optional: true },
size: { type: 'number', optional: true },
},
});
this.registerAction('create', this.create, { this.registerAction('create', this.create, {
params: { params: {
groupId: 'string', groupId: 'string',
@ -33,6 +40,42 @@ class GroupTopicService extends TcService {
}); });
} }
/**
* Topic
*/
async list(
ctx: TcContext<{
groupId: string;
panelId: string;
page?: number;
size?: number;
}>
) {
const { groupId, panelId, page = 1, size = 20 } = 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 topic = await this.adapter.model
.find({
groupId,
panelId,
})
.limit(size)
.skip((page - 1) * 20)
.exec();
const json = await this.transformDocuments(ctx, {}, topic);
return json;
}
/** /**
* Topic * Topic
*/ */
@ -59,18 +102,12 @@ class GroupTopicService extends TcService {
if (!targetPanel) { if (!targetPanel) {
throw new Error(t('面板不存在')); 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({ const topic = await this.adapter.model.create({
groupId: new Types.ObjectId(groupId), groupId,
panelId, panelId,
content, content,
author: new Types.ObjectId(userId), author: userId,
comment: [], comment: [],
}); });

@ -1,13 +1,39 @@
import React from 'react'; import React, { useEffect } from 'react';
import { TopicCard } from '../components/TopicCard'; import { TopicCard } from '../components/TopicCard';
import { useAsyncRequest, useGroupPanelContext } from '@capital/common';
import { Empty } from '@capital/component';
import { request } from '../request';
import './GroupTopicPanelRender.less'; import './GroupTopicPanelRender.less';
const GroupTopicPanelRender: React.FC = React.memo(() => { const GroupTopicPanelRender: React.FC = React.memo(() => {
const panelInfo = useGroupPanelContext();
const [{ value: list = [] }, fetch] = useAsyncRequest(async () => {
if (!panelInfo) {
return [];
}
const { data } = await request.get('list', {
params: {
groupId: panelInfo.groupId,
panelId: panelInfo.panelId,
},
});
return data;
}, [panelInfo]);
useEffect(() => {
fetch();
}, [fetch]);
return ( return (
<div className="plugin-topic-group-panel"> <div className="plugin-topic-group-panel">
{Array.from({ length: 20 }).map((_, i) => ( {Array.isArray(list) && list.length > 0 ? (
<TopicCard key={i} /> list.map((_, i) => <TopicCard key={i} />)
))} ) : (
<Empty />
)}
</div> </div>
); );
}); });

@ -0,0 +1,3 @@
import { createPluginRequest } from '@capital/common';
export const request = createPluginRequest('com.msgbyte.topic');

@ -1,5 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
/// <reference types="react" />
/** /**
* Tailchat * Tailchat
* *
@ -66,7 +68,7 @@ declare module '@capital/common' {
export const appendUrlSearch: any; export const appendUrlSearch: any;
export const useGroupIdContext: any; export const getServiceWorkerRegistration: any;
export const getServiceUrl: () => string; export const getServiceUrl: () => string;
@ -84,7 +86,14 @@ declare module '@capital/common' {
export const getCachedConverseInfo: any; export const getCachedConverseInfo: any;
export const localTrans: any; /**
*
* @example
* localTrans({'zh-CN': '你好', 'en-US': 'Hello'});
*
* @param trans
*/
export const localTrans: (trans: Record<'zh-CN' | 'en-US', string>) => string;
export const getLanguage: any; export const getLanguage: any;
@ -114,7 +123,7 @@ declare module '@capital/common' {
export const useLocation: any; export const useLocation: any;
export const useHistory: any; export const useNavigate: any;
export const createFastFormSchema: any; export const createFastFormSchema: any;
@ -205,6 +214,15 @@ declare module '@capital/common' {
*/ */
required?: string[]; required?: string[];
}) => void; }) => void;
export const useGroupIdContext: () => string;
export const useGroupPanelContext: () => {
groupId: string;
panelId: string;
} | null;
export const useSocketContext: any;
} }
/** /**
@ -215,6 +233,8 @@ declare module '@capital/component' {
export const Checkbox: any; export const Checkbox: any;
export const Empty: any;
export const Input: any; export const Input: any;
export const Divider: any; export const Divider: any;
@ -301,4 +321,6 @@ declare module '@capital/component' {
userId: string; userId: string;
className?: string; className?: string;
}>; }>;
export const Markdown: any;
} }

Loading…
Cancel
Save