From c25d3a0b46bd7ec8a3321cd72e7ee7360a218bff Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Tue, 31 Oct 2023 22:14:28 +0800 Subject: [PATCH] feat: add telemetry to aware of the presence of other deployment instances you can disable it by env DISABLE_TELEMETRY=1 --- client/shared/index.tsx | 1 + client/shared/model/config.ts | 5 +++++ client/shared/utils/consts.ts | 1 + client/shared/utils/environment.ts | 2 ++ client/web/src/init.tsx | 19 +++++++++++++++---- .../packages/sdk/src/services/lib/settings.ts | 1 + server/services/core/config.service.ts | 1 + 7 files changed, 26 insertions(+), 4 deletions(-) diff --git a/client/shared/index.tsx b/client/shared/index.tsx index 6e344b2a..05e8b269 100644 --- a/client/shared/index.tsx +++ b/client/shared/index.tsx @@ -250,6 +250,7 @@ export { isBrowser, isNavigator, isDevelopment, + isProduction, version, } from './utils/environment'; export type { PermissionItemType } from './utils/role-helper'; diff --git a/client/shared/model/config.ts b/client/shared/model/config.ts index a5206749..c45805bd 100644 --- a/client/shared/model/config.ts +++ b/client/shared/model/config.ts @@ -51,6 +51,11 @@ export interface GlobalConfig { */ disableAddFriend?: boolean; + /** + * 是否禁用遥测 + */ + disableTelemetry?: boolean; + announcement?: | false | { diff --git a/client/shared/utils/consts.ts b/client/shared/utils/consts.ts index f0a9e8ad..acf5561e 100644 --- a/client/shared/utils/consts.ts +++ b/client/shared/utils/consts.ts @@ -27,5 +27,6 @@ export const defaultGlobalConfig: GlobalConfig = { disableCreateGroup: false, disablePluginStore: false, disableAddFriend: false, + disableTelemetry: false, announcement: false, }; diff --git a/client/shared/utils/environment.ts b/client/shared/utils/environment.ts index 7ec2b17b..996c61c1 100644 --- a/client/shared/utils/environment.ts +++ b/client/shared/utils/environment.ts @@ -4,4 +4,6 @@ export const isNavigator = typeof navigator !== 'undefined'; export const isDevelopment = process.env.NODE_ENV === 'development'; +export const isProduction = process.env.NODE_ENV === 'production'; + export const version = process.env.VERSION || '0.0.0'; diff --git a/client/web/src/init.tsx b/client/web/src/init.tsx index 686b5306..62118286 100644 --- a/client/web/src/init.tsx +++ b/client/web/src/init.tsx @@ -12,10 +12,12 @@ import { t, fetchGlobalClientConfig, isDevelopment, + isProduction, setErrorHook, showToasts, parseUrlStr, onLanguageLoaded, + version, } from 'tailchat-shared'; import { getPopupContainer } from './utils/dom-helper'; import { getUserJWT } from './utils/jwt-helper'; @@ -121,7 +123,16 @@ setErrorHook((err) => { /** * 获取前端配置 */ -fetchGlobalClientConfig().catch((e) => { - showErrorToasts(t('全局配置加载失败')); - console.error('全局配置加载失败', e); -}); +fetchGlobalClientConfig() + .then((config) => { + if (isProduction && !config.disableTelemetry) { + // 发送遥测信息 + fetch( + `https://tianji.moonrailgun.com/telemetry/clnzoxcy10001vy2ohi4obbi0/blank.gif?name=tailchat&url=${window.location.origin}&v=${version}` + ).catch(() => {}); + } + }) + .catch((e) => { + showErrorToasts(t('全局配置加载失败')); + console.error('全局配置加载失败', e); + }); diff --git a/server/packages/sdk/src/services/lib/settings.ts b/server/packages/sdk/src/services/lib/settings.ts index fe56bfdc..2aa41700 100644 --- a/server/packages/sdk/src/services/lib/settings.ts +++ b/server/packages/sdk/src/services/lib/settings.ts @@ -55,6 +55,7 @@ export const config = { disableCreateGroup: checkEnvTrusty(process.env.DISABLE_CREATE_GROUP), // 是否禁用用户创建群组功能 disablePluginStore: checkEnvTrusty(process.env.DISABLE_PLUGIN_STORE), // 是否禁用用户插件中心功能 disableAddFriend: checkEnvTrusty(process.env.DISABLE_ADD_FRIEND), // 是否禁用用户添加好友功能 + disableTelemetry: checkEnvTrusty(process.env.DISABLE_TELEMETRY), // 是否禁用遥测 }, }; diff --git a/server/services/core/config.service.ts b/server/services/core/config.service.ts index 234dc3ad..258ea989 100644 --- a/server/services/core/config.service.ts +++ b/server/services/core/config.service.ts @@ -84,6 +84,7 @@ class ConfigService extends TcService { disableCreateGroup: config.feature.disableCreateGroup, disablePluginStore: config.feature.disablePluginStore, disableAddFriend: config.feature.disableAddFriend, + disableTelemetry: config.feature.disableTelemetry, ...persistConfig, }; }