feat: add feature disableAddFriend which can disable add friends

pull/105/head
moonrailgun 2 years ago
parent 2e489351c1
commit 36061f6b74

@ -45,6 +45,11 @@ export interface GlobalConfig {
* *
*/ */
disablePluginStore?: boolean; disablePluginStore?: boolean;
/**
*
*/
disableAddFriend?: boolean;
} }
export function getGlobalConfig(): GlobalConfig { export function getGlobalConfig(): GlobalConfig {

@ -26,4 +26,5 @@ export const defaultGlobalConfig: GlobalConfig = {
disableGuestLogin: false, disableGuestLogin: false,
disableCreateGroup: false, disableCreateGroup: false,
disablePluginStore: false, disablePluginStore: false,
disableAddFriend: false,
}; };

@ -1,7 +1,7 @@
import React, { useCallback, useState } from 'react'; import React, { useCallback, useState } from 'react';
import { PillTabPane, PillTabs } from '@/components/PillTabs'; import { PillTabPane, PillTabs } from '@/components/PillTabs';
import { AddFriend } from './AddFriend'; import { AddFriend } from './AddFriend';
import { t, useAppSelector } from 'tailchat-shared'; import { t, useAppSelector, useGlobalConfigStore } from 'tailchat-shared';
import { RequestSend } from './RequestSend'; import { RequestSend } from './RequestSend';
import { RequestReceived } from './RequestReceived'; import { RequestReceived } from './RequestReceived';
import { FriendList } from './FriendList'; import { FriendList } from './FriendList';
@ -14,6 +14,9 @@ export const FriendPanel: React.FC = React.memo(() => {
const friendRequests = useAppSelector((state) => state.user.friendRequests); const friendRequests = useAppSelector((state) => state.user.friendRequests);
const userId = useAppSelector((state) => state.user.info?._id); const userId = useAppSelector((state) => state.user.info?._id);
const [activeKey, setActiveKey] = useState('1'); const [activeKey, setActiveKey] = useState('1');
const disableAddFriend = useGlobalConfigStore(
(state) => state.disableAddFriend
);
const send = friendRequests.filter((item) => item.from === userId); const send = friendRequests.filter((item) => item.from === userId);
const received = friendRequests.filter((item) => item.to === userId); const received = friendRequests.filter((item) => item.to === userId);
@ -28,6 +31,9 @@ export const FriendPanel: React.FC = React.memo(() => {
<PillTabPane tab={t('全部')} key="1"> <PillTabPane tab={t('全部')} key="1">
<FriendList onSwitchToAddFriend={handleSwitchToAddFriend} /> <FriendList onSwitchToAddFriend={handleSwitchToAddFriend} />
</PillTabPane> </PillTabPane>
{!disableAddFriend && (
<>
<PillTabPane <PillTabPane
tab={ tab={
<Badge <Badge
@ -62,6 +68,8 @@ export const FriendPanel: React.FC = React.memo(() => {
> >
<AddFriend /> <AddFriend />
</PillTabPane> </PillTabPane>
</>
)}
</PillTabs> </PillTabs>
</div> </div>
); );

@ -17,7 +17,9 @@
"k429851b9": "No operation authority", "k429851b9": "No operation authority",
"k42cdd273": "Username already exists!", "k42cdd273": "Username already exists!",
"k45c8d1bf": "Claimed user does not exist", "k45c8d1bf": "Claimed user does not exist",
"k493e44f1": "Can't add myself as a friend",
"k4fd701fe": "Email does not exist", "k4fd701fe": "Email does not exist",
"k5191530a": "Cannot send duplicate friend requests",
"k590cb8b6": "Account does not exist", "k590cb8b6": "Account does not exist",
"k5f7a33eb": "The server does not allow guests to login", "k5f7a33eb": "The server does not allow guests to login",
"k64a3c830": "User does not exist", "k64a3c830": "User does not exist",
@ -32,6 +34,7 @@
"k986040de": "No group found", "k986040de": "No group found",
"k9891f980": "{{nickname}} joined the group through the public community", "k9891f980": "{{nickname}} joined the group through the public community",
"k996e99cb": "This group is not a public group and cannot be joined directly", "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", "ka3eb52f8": "Call ended, duration: {{num}} minutes",
"ka5b432c0": "Too frequent requests can share the same OTP within 10 minutes", "ka5b432c0": "Too frequent requests can share the same OTP within 10 minutes",
"ka8b712f7": "Email already exists!", "ka8b712f7": "Email already exists!",
@ -53,6 +56,7 @@
"kd389d15d": "This message was not found", "kd389d15d": "This message was not found",
"kd470bc32": "The robot service of this application has not been activated", "kd470bc32": "The robot service of this application has not been activated",
"ke050bc7a": "Username does not exist", "ke050bc7a": "Username does not exist",
"ke0d53ced": "The other party is already your friend and cannot be added again",
"ke19c80a5": "User has no upload permission", "ke19c80a5": "User has no upload permission",
"ke5849544": "Wrong password", "ke5849544": "Wrong password",
"ke82b4383": "You cannot send messages because you are banned", "ke82b4383": "You cannot send messages because you are banned",

@ -17,7 +17,9 @@
"k429851b9": "没有操作权限", "k429851b9": "没有操作权限",
"k42cdd273": "用户名已存在!", "k42cdd273": "用户名已存在!",
"k45c8d1bf": "认领用户不存在", "k45c8d1bf": "认领用户不存在",
"k493e44f1": "不能添加自己为好友",
"k4fd701fe": "邮箱不存在", "k4fd701fe": "邮箱不存在",
"k5191530a": "不能发送重复的好友请求",
"k590cb8b6": "账号不存在", "k590cb8b6": "账号不存在",
"k5f7a33eb": "服务器不允许游客登录", "k5f7a33eb": "服务器不允许游客登录",
"k64a3c830": "用户不存在", "k64a3c830": "用户不存在",
@ -32,6 +34,7 @@
"k986040de": "没有找到群组", "k986040de": "没有找到群组",
"k9891f980": "{{nickname}} 通过公共社区加入群组", "k9891f980": "{{nickname}} 通过公共社区加入群组",
"k996e99cb": "该群组并非公开群组, 无法直接加入", "k996e99cb": "该群组并非公开群组, 无法直接加入",
"k9e276b5": "管理员禁止添加好友功能",
"ka3eb52f8": "通话已结束, 时长: {{num}}分钟", "ka3eb52f8": "通话已结束, 时长: {{num}}分钟",
"ka5b432c0": "过于频繁的请求10 分钟内可以共用同一OTP", "ka5b432c0": "过于频繁的请求10 分钟内可以共用同一OTP",
"ka8b712f7": "邮箱已存在!", "ka8b712f7": "邮箱已存在!",
@ -53,6 +56,7 @@
"kd389d15d": "该消息未找到", "kd389d15d": "该消息未找到",
"kd470bc32": "该应用的机器人服务尚未开通", "kd470bc32": "该应用的机器人服务尚未开通",
"ke050bc7a": "用户名不存在", "ke050bc7a": "用户名不存在",
"ke0d53ced": "对方已经是您的好友, 不能再次添加",
"ke19c80a5": "用户无上传权限", "ke19c80a5": "用户无上传权限",
"ke5849544": "密码错误", "ke5849544": "密码错误",
"ke82b4383": "您因为被禁言无法发送消息", "ke82b4383": "您因为被禁言无法发送消息",

@ -15,7 +15,7 @@
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/msgbyte/tailchat-server.git" "url": "git+https://github.com/msgbyte/tailchat.git"
}, },
"keywords": [ "keywords": [
"msgbyte", "msgbyte",
@ -25,9 +25,9 @@
"author": "moonrailgun <moonrailgun@gmail.com>", "author": "moonrailgun <moonrailgun@gmail.com>",
"license": "MIT", "license": "MIT",
"bugs": { "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": { "devDependencies": {
"@types/jest": "^29.5.1", "@types/jest": "^29.5.1",
"typescript": "^4.3.3" "typescript": "^4.3.3"

@ -51,7 +51,8 @@ export const config = {
disableUserRegister: checkEnvTrusty(process.env.DISABLE_USER_REGISTER), // 是否关闭用户注册功能 disableUserRegister: checkEnvTrusty(process.env.DISABLE_USER_REGISTER), // 是否关闭用户注册功能
disableGuestLogin: checkEnvTrusty(process.env.DISABLE_GUEST_LOGIN), // 是否关闭用户游客登录功能 disableGuestLogin: checkEnvTrusty(process.env.DISABLE_GUEST_LOGIN), // 是否关闭用户游客登录功能
disableCreateGroup: checkEnvTrusty(process.env.DISABLE_CREATE_GROUP), // 是否禁用用户创建群组功能 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), // 是否禁用用户添加好友功能
}, },
}; };

@ -184,7 +184,6 @@ class MessageService extends TcService {
const member = groupInfo.members.find((m) => String(m.userId) === userId); const member = groupInfo.members.find((m) => String(m.userId) === userId);
if (member) { if (member) {
// 因为有机器人,所以如果没有在成员列表中找到不报错 // 因为有机器人,所以如果没有在成员列表中找到不报错
if (new Date(member.muteUntil).valueOf() > new Date().valueOf()) { if (new Date(member.muteUntil).valueOf() > new Date().valueOf()) {
throw new Error(t('您因为被禁言无法发送消息')); throw new Error(t('您因为被禁言无法发送消息'));
} }

@ -83,6 +83,7 @@ class ConfigService extends TcService {
disableGuestLogin: config.feature.disableGuestLogin, disableGuestLogin: config.feature.disableGuestLogin,
disableCreateGroup: config.feature.disableCreateGroup, disableCreateGroup: config.feature.disableCreateGroup,
disablePluginStore: config.feature.disablePluginStore, disablePluginStore: config.feature.disablePluginStore,
disableAddFriend: config.feature.disableAddFriend,
...persistConfig, ...persistConfig,
}; };
} }

@ -5,6 +5,7 @@ import {
Errors, Errors,
DataNotFoundError, DataNotFoundError,
NoPermissionError, NoPermissionError,
config,
} from 'tailchat-server-sdk'; } from 'tailchat-server-sdk';
import _ from 'lodash'; import _ from 'lodash';
import type { FriendRequest } from '../../../models/user/friendRequest'; import type { FriendRequest } from '../../../models/user/friendRequest';
@ -47,11 +48,16 @@ class FriendService extends TcService {
*/ */
async add(ctx: TcContext<{ to: string; message?: string }>) { async add(ctx: TcContext<{ to: string; message?: string }>) {
const from = ctx.meta.userId; const from = ctx.meta.userId;
const t = ctx.meta.t;
const { to, message } = ctx.params; const { to, message } = ctx.params;
if (config.feature.disableAddFriend === true) {
throw new NoPermissionError(t('管理员禁止添加好友功能'));
}
if (from === to) { if (from === to) {
throw new Errors.ValidationError('不能添加自己为好友'); throw new Errors.ValidationError(t('不能添加自己为好友'));
} }
const exist = await this.adapter.findOne({ const exist = await this.adapter.findOne({
@ -59,12 +65,12 @@ class FriendService extends TcService {
to, to,
}); });
if (exist) { if (exist) {
throw new Errors.MoleculerError('不能发送重复的好友请求'); throw new Errors.MoleculerError(t('不能发送重复的好友请求'));
} }
const isFriend = await ctx.call('friend.checkIsFriend', { targetId: to }); const isFriend = await ctx.call('friend.checkIsFriend', { targetId: to });
if (isFriend) { if (isFriend) {
throw new Error('对方已经是您的好友, 不能再次添加'); throw new Error(t('对方已经是您的好友, 不能再次添加'));
} }
const doc = await this.adapter.insert({ const doc = await this.adapter.insert({

Loading…
Cancel
Save