feat(server): 增加面板feature定义,增加subscribe用于给自定义面板增加订阅功能(之前写死文本面板会有这个效果)

pull/70/head
moonrailgun 2 years ago
parent b317731748
commit b938fcb12c

@ -9,6 +9,7 @@ export type {
GroupBaseInfo,
PureServiceSchema,
PureService,
PanelFeature,
} from './services/types';
export { parseLanguageFromHead } from './services/lib/i18n/parser';
export { t } from './services/lib/i18n';

@ -13,7 +13,7 @@ import {
} from 'moleculer';
import { once } from 'lodash';
import { TcDbService } from './mixins/db.mixin';
import type { PureContext, TcPureContext } from './types';
import type { PanelFeature, PureContext, TcPureContext } from './types';
import type { TFunction } from 'i18next';
import { t } from './lib/i18n';
import type { ValidationRuleObject } from 'fastest-validator';
@ -309,6 +309,29 @@ export abstract class TcService extends Service {
});
}
/**
*
* @param panelFeature
*/
async setPanelFeature(panelName: string, panelFeatures: PanelFeature[]) {
await this.setGlobalConfig(`panelFeature.${panelName}`, panelFeatures);
}
/**
*
* @param panelFeature
*/
getPanelNamesWithFeature(panelFeature: PanelFeature) {
const map =
this.getGlobalConfig<Record<string, PanelFeature[]>>('panelFeature');
const matched = Object.entries(map).filter(([panelName, panelFeatures]) =>
panelFeatures.includes(panelFeature)
);
return matched.map((m) => m[0]);
}
/**
*
* @param serviceNames
@ -334,7 +357,7 @@ export abstract class TcService extends Service {
return super.waitForServices(serviceNames, timeout, interval, logger);
}
getGlobalConfig(key: string): any {
getGlobalConfig<T = any>(key: string): T {
return _.get(this.globalConfig, key);
}

@ -53,3 +53,8 @@ export type TcContext<P = {}, M = {}> = TcPureContext<
export type GroupBaseInfo = Pick<GroupStruct, 'name' | 'avatar' | 'owner'> & {
memberCount: number;
};
/**
*
*/
export type PanelFeature = 'subscribe'; // 订阅变更,即用户登录时自动加入面板的房间

@ -43,6 +43,10 @@ class GroupTopicService extends TcService {
});
}
protected onInited(): void {
this.setPanelFeature('com.msgbyte.topic/grouppanel', ['subscribe']);
}
/**
* Topic
*/
@ -121,7 +125,7 @@ class GroupTopicService extends TcService {
const json = await this.transformDocuments(ctx, {}, topic);
this.roomcastNotify(ctx, groupId, 'create', json);
this.roomcastNotify(ctx, panelId, 'create', json);
return json;
}
@ -175,7 +179,9 @@ class GroupTopicService extends TcService {
const json = await this.transformDocuments(ctx, {}, topic);
this.roomcastNotify(ctx, groupId, 'createComment', json);
// TODO: 回复需要添加到收件箱
this.roomcastNotify(ctx, panelId, 'createComment', json);
return true;
}

@ -12,11 +12,6 @@ class ConfigService extends TcService {
}
onInit(): void {
/**
*
*
* 使
*/
this.registerAction('client', this.client);
this.registerAction('all', this.all, {
visibility: 'public',
@ -48,7 +43,9 @@ class ConfigService extends TcService {
/**
*
*
* 使d
* 使
*
* NOTICE:
*/
async client(ctx: TcPureContext) {
return {

@ -19,6 +19,7 @@ import {
NoPermissionError,
PERMISSION,
GroupPanelType,
PanelFeature,
} from 'tailchat-server-sdk';
import moment from 'moment';
@ -210,6 +211,21 @@ class GroupService extends TcService {
});
}
/**
* id
*
* socket
*/
private getSubscribedGroupPanelIds(group: Group): string[] {
const textPanelIds = this.getGroupTextPanelIds(group);
const subscribeFeaturePanelIds = this.getGroupPanelIdsWithFeature(
group,
'subscribe'
);
return _.uniq([...textPanelIds, ...subscribeFeaturePanelIds]);
}
/**
* id
*
@ -223,6 +239,22 @@ class GroupService extends TcService {
return textPanelIds;
}
/**
*
* @param group
*/
private getGroupPanelIdsWithFeature(
group: Group,
feature: PanelFeature
): string[] {
const featureAllPanelNames = this.getPanelNamesWithFeature(feature);
const matchedPanels = group.panels.filter((p) =>
featureAllPanelNames.includes(p.pluginPanelName)
);
return matchedPanels.map((p) => p.id);
}
/**
*
*/
@ -242,7 +274,7 @@ class GroupService extends TcService {
owner: userId,
});
const textPanelIds = this.getGroupTextPanelIds(group);
const textPanelIds = this.getSubscribedGroupPanelIds(group);
await call(ctx).joinSocketIORoom(
[String(group._id), ...textPanelIds],
@ -268,7 +300,9 @@ class GroupService extends TcService {
panelIds: string[];
}> {
const groups = await this.getUserGroups(ctx); // TODO: 应该使用call而不是直接调用为了获取tracer和caching支持。目前moleculer的文档没有显式的声明类似localCall的行为可以花时间看一下
const panelIds = _.flatten(groups.map((g) => this.getGroupTextPanelIds(g)));
const panelIds = _.flatten(
groups.map((g) => this.getSubscribedGroupPanelIds(g))
);
return {
groupIds: groups.map((g) => String(g._id)),
@ -460,7 +494,7 @@ class GroupService extends TcService {
this.notifyGroupInfoUpdate(ctx, group); // 推送变更
this.unicastNotify(ctx, userId, 'add', group);
const textPanelIds = this.getGroupTextPanelIds(group);
const textPanelIds = this.getSubscribedGroupPanelIds(group);
await call(ctx).joinSocketIORoom(
[String(group._id), ...textPanelIds],
@ -658,9 +692,12 @@ class GroupService extends TcService {
)
.exec();
if (type === 0) {
if (
type === GroupPanelType.TEXT ||
this.getPanelNamesWithFeature('subscribe').includes(name)
) {
/**
*
*
*
*/
const groupInfo = await call(ctx).getGroupInfo(groupId);

Loading…
Cancel
Save