From 36061f6b740cf23fa663139ae71fbee2a4e99ee6 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Thu, 13 Jul 2023 14:49:01 +0800 Subject: [PATCH] feat: add feature disableAddFriend which can disable add friends --- client/shared/model/config.ts | 5 ++ client/shared/utils/consts.ts | 1 + .../Main/Content/Personal/Friends/index.tsx | 74 ++++++++++--------- server/locales/en-US/translation.json | 4 + server/locales/zh-CN/translation.json | 4 + server/packages/sdk/package.json | 6 +- .../packages/sdk/src/services/lib/settings.ts | 3 +- server/services/core/chat/message.service.ts | 1 - server/services/core/config.service.ts | 1 + .../core/user/friendRequest.service.ts | 12 ++- 10 files changed, 70 insertions(+), 41 deletions(-) diff --git a/client/shared/model/config.ts b/client/shared/model/config.ts index 7578b146..d98fcfb2 100644 --- a/client/shared/model/config.ts +++ b/client/shared/model/config.ts @@ -45,6 +45,11 @@ export interface GlobalConfig { * 是否禁用插件中心 */ disablePluginStore?: boolean; + + /** + * 是否禁用添加好友功能 + */ + disableAddFriend?: boolean; } export function getGlobalConfig(): GlobalConfig { diff --git a/client/shared/utils/consts.ts b/client/shared/utils/consts.ts index be828de1..f2c102b6 100644 --- a/client/shared/utils/consts.ts +++ b/client/shared/utils/consts.ts @@ -26,4 +26,5 @@ export const defaultGlobalConfig: GlobalConfig = { disableGuestLogin: false, disableCreateGroup: false, disablePluginStore: false, + disableAddFriend: false, }; diff --git a/client/web/src/routes/Main/Content/Personal/Friends/index.tsx b/client/web/src/routes/Main/Content/Personal/Friends/index.tsx index da2d4e9e..9b93cd67 100644 --- a/client/web/src/routes/Main/Content/Personal/Friends/index.tsx +++ b/client/web/src/routes/Main/Content/Personal/Friends/index.tsx @@ -1,7 +1,7 @@ import React, { useCallback, useState } from 'react'; import { PillTabPane, PillTabs } from '@/components/PillTabs'; import { AddFriend } from './AddFriend'; -import { t, useAppSelector } from 'tailchat-shared'; +import { t, useAppSelector, useGlobalConfigStore } from 'tailchat-shared'; import { RequestSend } from './RequestSend'; import { RequestReceived } from './RequestReceived'; import { FriendList } from './FriendList'; @@ -14,6 +14,9 @@ export const FriendPanel: React.FC = React.memo(() => { const friendRequests = useAppSelector((state) => state.user.friendRequests); const userId = useAppSelector((state) => state.user.info?._id); const [activeKey, setActiveKey] = useState('1'); + const disableAddFriend = useGlobalConfigStore( + (state) => state.disableAddFriend + ); const send = friendRequests.filter((item) => item.from === userId); const received = friendRequests.filter((item) => item.to === userId); @@ -28,40 +31,45 @@ export const FriendPanel: React.FC = React.memo(() => { - + + {t('已发送')} + + } + key="2" > - {t('已发送')} - - } - key="2" - > - - - + + + {t('待处理')} + + } + key="3" > - {t('待处理')} - - } - key="3" - > - - - {t('添加好友')}} - key="add" - > - - + + + {t('添加好友')}} + key="add" + > + + + + )} ); diff --git a/server/locales/en-US/translation.json b/server/locales/en-US/translation.json index 6bd54b41..e6f27856 100644 --- a/server/locales/en-US/translation.json +++ b/server/locales/en-US/translation.json @@ -17,7 +17,9 @@ "k429851b9": "No operation authority", "k42cdd273": "Username already exists!", "k45c8d1bf": "Claimed user does not exist", + "k493e44f1": "Can't add myself as a friend", "k4fd701fe": "Email does not exist", + "k5191530a": "Cannot send duplicate friend requests", "k590cb8b6": "Account does not exist", "k5f7a33eb": "The server does not allow guests to login", "k64a3c830": "User does not exist", @@ -32,6 +34,7 @@ "k986040de": "No group found", "k9891f980": "{{nickname}} joined the group through the public community", "k996e99cb": "This group is not a public group and cannot be joined directly", + "k9e276b5": "The administrator forbids the function of adding friends", "ka3eb52f8": "Call ended, duration: {{num}} minutes", "ka5b432c0": "Too frequent requests can share the same OTP within 10 minutes", "ka8b712f7": "Email already exists!", @@ -53,6 +56,7 @@ "kd389d15d": "This message was not found", "kd470bc32": "The robot service of this application has not been activated", "ke050bc7a": "Username does not exist", + "ke0d53ced": "The other party is already your friend and cannot be added again", "ke19c80a5": "User has no upload permission", "ke5849544": "Wrong password", "ke82b4383": "You cannot send messages because you are banned", diff --git a/server/locales/zh-CN/translation.json b/server/locales/zh-CN/translation.json index 3dccdfc5..e96e0712 100644 --- a/server/locales/zh-CN/translation.json +++ b/server/locales/zh-CN/translation.json @@ -17,7 +17,9 @@ "k429851b9": "没有操作权限", "k42cdd273": "用户名已存在!", "k45c8d1bf": "认领用户不存在", + "k493e44f1": "不能添加自己为好友", "k4fd701fe": "邮箱不存在", + "k5191530a": "不能发送重复的好友请求", "k590cb8b6": "账号不存在", "k5f7a33eb": "服务器不允许游客登录", "k64a3c830": "用户不存在", @@ -32,6 +34,7 @@ "k986040de": "没有找到群组", "k9891f980": "{{nickname}} 通过公共社区加入群组", "k996e99cb": "该群组并非公开群组, 无法直接加入", + "k9e276b5": "管理员禁止添加好友功能", "ka3eb52f8": "通话已结束, 时长: {{num}}分钟", "ka5b432c0": "过于频繁的请求,10 分钟内可以共用同一OTP", "ka8b712f7": "邮箱已存在!", @@ -53,6 +56,7 @@ "kd389d15d": "该消息未找到", "kd470bc32": "该应用的机器人服务尚未开通", "ke050bc7a": "用户名不存在", + "ke0d53ced": "对方已经是您的好友, 不能再次添加", "ke19c80a5": "用户无上传权限", "ke5849544": "密码错误", "ke82b4383": "您因为被禁言无法发送消息", diff --git a/server/packages/sdk/package.json b/server/packages/sdk/package.json index 85c6be92..72bd8b4d 100644 --- a/server/packages/sdk/package.json +++ b/server/packages/sdk/package.json @@ -15,7 +15,7 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/msgbyte/tailchat-server.git" + "url": "git+https://github.com/msgbyte/tailchat.git" }, "keywords": [ "msgbyte", @@ -25,9 +25,9 @@ "author": "moonrailgun ", "license": "MIT", "bugs": { - "url": "https://github.com/msgbyte/tailchat-server/issues" + "url": "https://github.com/msgbyte/tailchat/issues" }, - "homepage": "https://github.com/msgbyte/tailchat-server#readme", + "homepage": "https://github.com/msgbyte/tailchat#readme", "devDependencies": { "@types/jest": "^29.5.1", "typescript": "^4.3.3" diff --git a/server/packages/sdk/src/services/lib/settings.ts b/server/packages/sdk/src/services/lib/settings.ts index 3a036c55..03d178bf 100644 --- a/server/packages/sdk/src/services/lib/settings.ts +++ b/server/packages/sdk/src/services/lib/settings.ts @@ -51,7 +51,8 @@ export const config = { disableUserRegister: checkEnvTrusty(process.env.DISABLE_USER_REGISTER), // 是否关闭用户注册功能 disableGuestLogin: checkEnvTrusty(process.env.DISABLE_GUEST_LOGIN), // 是否关闭用户游客登录功能 disableCreateGroup: checkEnvTrusty(process.env.DISABLE_CREATE_GROUP), // 是否禁用用户创建群组功能 - disablePluginStore: checkEnvTrusty(process.env.DISABLE_PLUGIN_STORE), // 是否禁用用户用户插件中心功能 + disablePluginStore: checkEnvTrusty(process.env.DISABLE_PLUGIN_STORE), // 是否禁用用户插件中心功能 + disableAddFriend: checkEnvTrusty(process.env.DISABLE_ADD_FRIEND), // 是否禁用用户添加好友功能 }, }; diff --git a/server/services/core/chat/message.service.ts b/server/services/core/chat/message.service.ts index 32be34b7..d80ecd2d 100644 --- a/server/services/core/chat/message.service.ts +++ b/server/services/core/chat/message.service.ts @@ -184,7 +184,6 @@ class MessageService extends TcService { const member = groupInfo.members.find((m) => String(m.userId) === userId); if (member) { // 因为有机器人,所以如果没有在成员列表中找到不报错 - if (new Date(member.muteUntil).valueOf() > new Date().valueOf()) { throw new Error(t('您因为被禁言无法发送消息')); } diff --git a/server/services/core/config.service.ts b/server/services/core/config.service.ts index 47d7714e..baa43adc 100644 --- a/server/services/core/config.service.ts +++ b/server/services/core/config.service.ts @@ -83,6 +83,7 @@ class ConfigService extends TcService { disableGuestLogin: config.feature.disableGuestLogin, disableCreateGroup: config.feature.disableCreateGroup, disablePluginStore: config.feature.disablePluginStore, + disableAddFriend: config.feature.disableAddFriend, ...persistConfig, }; } diff --git a/server/services/core/user/friendRequest.service.ts b/server/services/core/user/friendRequest.service.ts index 9984146a..6e180eab 100644 --- a/server/services/core/user/friendRequest.service.ts +++ b/server/services/core/user/friendRequest.service.ts @@ -5,6 +5,7 @@ import { Errors, DataNotFoundError, NoPermissionError, + config, } from 'tailchat-server-sdk'; import _ from 'lodash'; import type { FriendRequest } from '../../../models/user/friendRequest'; @@ -47,11 +48,16 @@ class FriendService extends TcService { */ async add(ctx: TcContext<{ to: string; message?: string }>) { const from = ctx.meta.userId; + const t = ctx.meta.t; const { to, message } = ctx.params; + if (config.feature.disableAddFriend === true) { + throw new NoPermissionError(t('管理员禁止添加好友功能')); + } + if (from === to) { - throw new Errors.ValidationError('不能添加自己为好友'); + throw new Errors.ValidationError(t('不能添加自己为好友')); } const exist = await this.adapter.findOne({ @@ -59,12 +65,12 @@ class FriendService extends TcService { to, }); if (exist) { - throw new Errors.MoleculerError('不能发送重复的好友请求'); + throw new Errors.MoleculerError(t('不能发送重复的好友请求')); } const isFriend = await ctx.call('friend.checkIsFriend', { targetId: to }); if (isFriend) { - throw new Error('对方已经是您的好友, 不能再次添加'); + throw new Error(t('对方已经是您的好友, 不能再次添加')); } const doc = await this.adapter.insert({