Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add group preview support #142

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions client/shared/api/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ export class AppSocket {
if (resp.result === true) {
resolve(resp.data);
} else if (resp.result === false) {
reject(
new SocketEventError(
`[${eventName}]: ${resp.message}: \ndata: ${JSON.stringify(
eventData
)}`
)
const error = new SocketEventError(
`[${eventName}]: ${resp.message}: \ndata: ${JSON.stringify(
eventData
)}`
);
console.error(error);
reject(error);
}
});
});
Expand Down
9 changes: 9 additions & 0 deletions client/web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ const InviteRoute = Loadable(
)
);

const PreviewRoute = Loadable(
() =>
import(
/* webpackChunkName: 'preview' */ /* webpackPreload: true */
'./routes/Preview'
)
);

export const TcAntdProvider: React.FC<PropsWithChildren> = React.memo(
(props) => {
const { value: locale } = useAsync(async (): Promise<Locale> => {
Expand Down Expand Up @@ -168,6 +176,7 @@ export const App: React.FC = React.memo(() => {
<Route path="/main/*" element={<MainRoute />} />
<Route path="/panel/*" element={<PanelRoute />} />
<Route path="/invite/:inviteCode" element={<InviteRoute />} />
<Route path="/preview/*" element={<PreviewRoute />} />
<Route
path="/plugin/*"
element={
Expand Down
14 changes: 14 additions & 0 deletions client/web/src/components/GroupPreview/GroupPreview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import { useSocket } from './useSocket';

/**
* A Component for Group Preview Entry
*/
export const GroupPreview: React.FC<{
groupId: string;
}> = React.memo((props) => {
useSocket(props.groupId);

return null;
});
GroupPreview.displayName = 'GroupPreview';
19 changes: 19 additions & 0 deletions client/web/src/components/GroupPreview/MessageList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import { ChatMessageList } from '../ChatBox/ChatMessageList';

interface GroupPreviewMessageListProps {
groupId: string;
converseId: string;
}
export const GroupPreviewMessageList: React.FC<GroupPreviewMessageListProps> =
React.memo(() => {
return (
<ChatMessageList
messages={[]}
isLoadingMore={false}
hasMoreMessage={false}
onLoadMore={async () => {}}
/>
);
});
GroupPreviewMessageList.displayName = 'GroupPreviewMessageList';
3 changes: 3 additions & 0 deletions client/web/src/components/GroupPreview/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Preview Group Message like Discord.

NOTICE: GroupPreview should has independent context because its should can run in non-main page.
1 change: 1 addition & 0 deletions client/web/src/components/GroupPreview/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { GroupPreview } from './GroupPreview';
29 changes: 29 additions & 0 deletions client/web/src/components/GroupPreview/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { create } from 'zustand';
import type { model } from 'tailchat-shared';

interface ChatConverseState extends model.converse.ChatConverseInfo {
messages: model.message.LocalChatMessage[];
}

interface GroupPreviewState {
groupInfo: model.group.GroupInfo | null;
converses: Record<string, ChatConverseState>;
}

function getDefaultState() {
return {
groupInfo: null,
converses: {},
};
}

export const useGroupPreviewStore = create<GroupPreviewState>((get) => ({
...getDefaultState(),
}));

/**
* 重置状态
*/
export function resetGroupPreviewState() {
useGroupPreviewStore.setState(getDefaultState());
}
41 changes: 41 additions & 0 deletions client/web/src/components/GroupPreview/useSocket.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useSocketContext } from '@/context/SocketContext';
import { useEffect } from 'react';
import { GroupInfo, GroupPanelType } from 'tailchat-shared';
import { resetGroupPreviewState, useGroupPreviewStore } from './store';

export function useSocket(groupId: string) {
const socket = useSocketContext();

useEffect(() => {
socket.request('group.preview.joinGroupRooms', {
groupId,
});

socket
.request<GroupInfo>('group.preview.getGroupInfo', {
groupId,
})
.then((groupInfo) => {
console.log('groupInfo', groupInfo);
useGroupPreviewStore.setState({
groupInfo,
});

if (Array.isArray(groupInfo.panels)) {
const textPanels = groupInfo.panels.map(
(p) => p.type === GroupPanelType.TEXT
);

// TODO
}
});

return () => {
socket.request('group.preview.leaveGroupRooms', {
groupId,
});

resetGroupPreviewState();
};
}, [groupId]);
}
34 changes: 34 additions & 0 deletions client/web/src/routes/Preview/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { GroupPreview } from '@/components/GroupPreview';
import { NotFound } from '@/components/NotFound';
import React from 'react';
import { Route, Routes, useParams } from 'react-router';
import { t } from 'tailchat-shared';
import { MainProvider } from '../Main/Provider';

const PreviewRoute: React.FC = React.memo(() => {
return (
<MainProvider>
<Routes>
<Route path="/:groupId" element={<GroupPreviewRoute />} />

<Route path="/*" element={t('未知的页面')} />
</Routes>
</MainProvider>
);
});
PreviewRoute.displayName = 'PreviewRoute';

const GroupPreviewRoute: React.FC = React.memo(() => {
const { groupId } = useParams<{
groupId: string;
}>();

if (!groupId) {
return <NotFound />;
}

return <GroupPreview groupId={groupId} />;
});
GroupPreviewRoute.displayName = 'GroupPreviewRoute';

export default PreviewRoute;
40 changes: 30 additions & 10 deletions server/services/core/group/group.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
db,
} from 'tailchat-server-sdk';
import moment from 'moment';
import type { GroupStruct } from 'tailchat-server-sdk';

interface GroupService
extends TcService,
Expand All @@ -48,6 +49,7 @@ class GroupService extends TcService {
'getJoinedGroupAndPanelIds',
this.getJoinedGroupAndPanelIds
);
this.registerAction('getGroupSocketRooms', this.getGroupSocketRooms);
this.registerAction('getGroupBasicInfo', this.getGroupBasicInfo, {
params: {
groupId: 'string',
Expand Down Expand Up @@ -234,7 +236,7 @@ class GroupService extends TcService {
*
* 订阅即加入socket房间
*/
private getSubscribedGroupPanelIds(group: Group): {
private getSubscribedGroupPanelIds(group: GroupStruct): {
textPanelIds: string[];
subscribeFeaturePanelIds: string[];
} {
Expand All @@ -254,7 +256,7 @@ class GroupService extends TcService {
* 获取群组所有的文字面板id列表
* 用于加入房间
*/
private getGroupTextPanelIds(group: Group): string[] {
private getGroupTextPanelIds(group: GroupStruct): string[] {
// TODO: 先无视权限, 把所有的信息全部显示
const textPanelIds = group.panels
.filter((p) => p.type === GroupPanelType.TEXT)
Expand All @@ -268,7 +270,7 @@ class GroupService extends TcService {
* @param group
*/
private getGroupPanelIdsWithFeature(
group: Group,
group: GroupStruct,
feature: PanelFeature
): string[] {
const featureAllPanelNames = this.getPanelNamesWithFeature(feature);
Expand Down Expand Up @@ -301,12 +303,14 @@ class GroupService extends TcService {
throw new NoPermissionError(t('创建群组功能已被管理员禁用'));
}

const group = await this.adapter.model.createGroup({
const doc = await this.adapter.model.createGroup({
name,
panels,
owner: userId,
});

const group = await this.transformDocuments(ctx, {}, doc);

const { textPanelIds, subscribeFeaturePanelIds } =
this.getSubscribedGroupPanelIds(group);

Expand All @@ -315,10 +319,10 @@ class GroupService extends TcService {
userId
);

return this.transformDocuments(ctx, {}, group);
return group;
}

async getUserGroups(ctx: TcContext): Promise<Group[]> {
async getUserGroups(ctx: TcContext): Promise<GroupStruct[]> {
const userId = ctx.meta.userId;

const groups = await this.adapter.model.getUserGroups(userId);
Expand Down Expand Up @@ -351,6 +355,20 @@ class GroupService extends TcService {
};
}

/**
* 获取所有订阅的群组面板列表
*/
async getGroupSocketRooms(ctx: TcContext<{ groupId: string }>): Promise<{
textPanelIds: string[];
subscribeFeaturePanelIds: string[];
}> {
const groupId = ctx.params.groupId;

const group = await call(ctx).getGroupInfo(groupId);

return this.getSubscribedGroupPanelIds(group);
}

/**
* 获取群组基本信息
*/
Expand Down Expand Up @@ -550,7 +568,7 @@ class GroupService extends TcService {
)
.exec();

const group: Group = await this.transformDocuments(ctx, {}, doc);
const group: GroupStruct = await this.transformDocuments(ctx, {}, doc);

this.notifyGroupInfoUpdate(ctx, group); // 推送变更
this.unicastNotify(ctx, userId, 'add', group);
Expand Down Expand Up @@ -1241,13 +1259,15 @@ class GroupService extends TcService {
*/
private async notifyGroupInfoUpdate(
ctx: TcContext,
group: Group
): Promise<Group> {
group: Group | GroupStruct
): Promise<GroupStruct> {
const groupId = String(group._id);
let json = group;
let json: GroupStruct;
if (_.isPlainObject(group) === false) {
// 当传入的数据为group doc时
json = await this.transformDocuments(ctx, {}, group);
} else {
json = group as any;
}

this.cleanGroupInfoCache(groupId);
Expand Down
78 changes: 78 additions & 0 deletions server/services/core/group/preview.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { TcService, TcContext, call } from 'tailchat-server-sdk';

class GroupPreviewService extends TcService {
get serviceName(): string {
return 'group.preview';
}

onInit(): void {
/**
* TODO: 这里的action都应当判断一下群组是否支持预览
*/
this.registerAction('joinGroupRooms', this.joinGroupRooms, {
params: {
groupId: 'string',
},
});
this.registerAction('leaveGroupRooms', this.leaveGroupRooms, {
params: {
groupId: 'string',
},
});
this.registerAction('getGroupInfo', this.getGroupInfo, {
params: {
groupId: 'string',
},
});
}

async joinGroupRooms(ctx: TcContext<{ groupId: string }>) {
const groupId = ctx.params.groupId;

const { textPanelIds, subscribeFeaturePanelIds } = await ctx.call<
{
textPanelIds: string[];
subscribeFeaturePanelIds: string[];
},
{ groupId: string }
>('group.getGroupSocketRooms', {
groupId,
});

await call(ctx).joinSocketIORoom([
groupId,
...textPanelIds,
...subscribeFeaturePanelIds,
]);
}

async leaveGroupRooms(ctx: TcContext<{ groupId: string }>) {
const groupId = ctx.params.groupId;

const { textPanelIds, subscribeFeaturePanelIds } = await ctx.call<
{
textPanelIds: string[];
subscribeFeaturePanelIds: string[];
},
{ groupId: string }
>('group.getGroupSocketRooms', {
groupId,
});

await call(ctx).leaveSocketIORoom([
groupId,
...textPanelIds,
...subscribeFeaturePanelIds,
]);
}

async getGroupInfo(ctx: TcContext<{ groupId: string }>) {
const groupId = ctx.params.groupId;

const groupInfo = await call(ctx).getGroupInfo(groupId);

return groupInfo;
}
}

export default GroupPreviewService;