Yuki Takei 1 год назад
Родитель
Сommit
6c2cb13fd2

+ 34 - 6
apps/app/src/server/service/growi-info/growi-info.integ.ts

@@ -15,7 +15,6 @@ describe('GrowiInfoService', () => {
 
   let growiInfoService: GrowiInfoService;
   let User;
-  let user;
 
   beforeAll(async() => {
     process.env.APP_SITE_URL = 'http://growi.test.jp';
@@ -54,14 +53,10 @@ describe('GrowiInfoService', () => {
     User = userModelFactory(crowiMock);
 
     await User.deleteMany({}); // clear users
-
-    user = await User.create({
-      username: 'growiinfo test user',
-      createdAt: '2000-01-01',
-    });
   });
 
   describe('getGrowiInfo', () => {
+
     test('Should get correct GROWI info', async() => {
       const growiInfo = await growiInfoService.getGrowiInfo();
 
@@ -78,6 +73,39 @@ describe('GrowiInfoService', () => {
       delete (growiInfo as any).appSiteUrlHashed;
       delete growiInfo.osInfo;
 
+      expect(growiInfo).toEqual({
+        version: appVersion,
+        appSiteUrl: 'http://growi.test.jp',
+        type: 'on-premise',
+        wikiType: 'closed',
+        deploymentType: 'growi-docker-compose',
+      });
+    });
+
+    test('Should get correct GROWI info with additionalInfo', async() => {
+      // arrange
+      await User.create({
+        username: 'growiinfo test user',
+        createdAt: '2000-01-01',
+      });
+
+      // act
+      const growiInfo = await growiInfoService.getGrowiInfo(true);
+
+      // assert
+      assert(growiInfo != null);
+
+      expect(growiInfo.appSiteUrlHashed).toBeTruthy();
+      expect(growiInfo.appSiteUrlHashed).not.toBe('http://growi.test.jp');
+      expect(growiInfo.osInfo?.type).toBeTruthy();
+      expect(growiInfo.osInfo?.platform).toBeTruthy();
+      expect(growiInfo.osInfo?.arch).toBeTruthy();
+      expect(growiInfo.osInfo?.totalmem).toBeTruthy();
+
+      // eslint-disable-next-line @typescript-eslint/no-explicit-any
+      delete (growiInfo as any).appSiteUrlHashed;
+      delete growiInfo.osInfo;
+
       expect(growiInfo).toEqual({
         version: appVersion,
         appSiteUrl: 'http://growi.test.jp',

+ 49 - 31
apps/app/src/server/service/growi-info/growi-info.ts

@@ -24,59 +24,77 @@ export class GrowiInfoService {
     this.crowi = crowi;
   }
 
-  async getGrowiInfo(): Promise<IGrowiInfo<IGrowiAppAdditionalInfo>> {
-    // eslint-disable-next-line @typescript-eslint/no-explicit-any
-    const User = mongoose.model<IUser, Model<IUser>>('User');
+  /**
+   * Get GROWI information
+   */
+  getGrowiInfo(): Promise<IGrowiInfo<Record<string, never>>>;
+
+  /**
+   * Get GROWI information with additional information
+   * @param includeAdditionalInfo whether to include additional information
+   */
+  getGrowiInfo(includeAdditionalInfo: true): Promise<IGrowiInfo<IGrowiAppAdditionalInfo>>;
+
+  async getGrowiInfo(includeAdditionalInfo?: boolean): Promise<IGrowiInfo<Record<string, never>> | IGrowiInfo<IGrowiAppAdditionalInfo>> {
 
     const appSiteUrl = this.crowi.appService.getSiteUrl();
     const hasher = crypto.createHash('sha256');
     hasher.update(appSiteUrl);
     const appSiteUrlHashed = hasher.digest('hex');
 
+    const isGuestAllowedToRead = aclService.isGuestAllowedToRead();
+    const wikiType = isGuestAllowedToRead ? GrowiWikiType.open : GrowiWikiType.closed;
+
+    const baseInfo = {
+      version: this.crowi.version,
+      osInfo: {
+        type: os.type(),
+        platform: os.platform(),
+        arch: os.arch(),
+        totalmem: os.totalmem(),
+      },
+      appSiteUrl: configManager.getConfig('questionnaire:isAppSiteUrlHashed') ? undefined : appSiteUrl,
+      appSiteUrlHashed,
+      type: configManager.getConfig('app:serviceType'),
+      wikiType,
+      deploymentType: configManager.getConfig('app:deploymentType'),
+    };
+
+    if (!includeAdditionalInfo) {
+      return baseInfo;
+    }
+
+    return {
+      ...baseInfo,
+      additionalInfo: await this.getAdditionalInfo(),
+    };
+  }
+
+  private async getAdditionalInfo(): Promise<IGrowiAppAdditionalInfo> {
+    const User = mongoose.model<IUser, Model<IUser>>('User');
+
     // Get the oldest user who probably installed this GROWI.
     const user = await User.findOne({ createdAt: { $ne: null } }).sort({ createdAt: 1 });
-
     const installedAtByOldestUser = user ? user.createdAt : null;
 
     const appInstalledConfig = await Config.findOne({ key: 'app:installed' });
     const oldestConfig = await Config.findOne().sort({ createdAt: 1 });
-
-    // oldestConfig must not be null because there is at least one config
-    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
     const installedAt = installedAtByOldestUser ?? appInstalledConfig?.createdAt ?? oldestConfig!.createdAt ?? null;
 
     const currentUsersCount = await User.countDocuments();
-    // eslint-disable-next-line @typescript-eslint/no-explicit-any
     const currentActiveUsersCount = await (User as any).countActiveUsers();
 
-    const isGuestAllowedToRead = aclService.isGuestAllowedToRead();
-    const wikiType = isGuestAllowedToRead ? GrowiWikiType.open : GrowiWikiType.closed;
-
     const activeExternalAccountTypes: IExternalAuthProviderType[] = Object.values(IExternalAuthProviderType).filter((type) => {
       return configManager.getConfig(`security:passport-${type}:isEnabled`);
     });
 
     return {
-      version: this.crowi.version,
-      osInfo: {
-        type: os.type(),
-        platform: os.platform(),
-        arch: os.arch(),
-        totalmem: os.totalmem(),
-      },
-      appSiteUrl: configManager.getConfig('questionnaire:isAppSiteUrlHashed') ? undefined : appSiteUrl,
-      appSiteUrlHashed,
-      type: configManager.getConfig('app:serviceType'),
-      wikiType,
-      deploymentType: configManager.getConfig('app:deploymentType'),
-      additionalInfo: {
-        installedAt,
-        installedAtByOldestUser,
-        currentUsersCount,
-        currentActiveUsersCount,
-        attachmentType: configManager.getConfig('app:fileUploadType'),
-        activeExternalAccountTypes,
-      },
+      installedAt,
+      installedAtByOldestUser,
+      currentUsersCount,
+      currentActiveUsersCount,
+      attachmentType: configManager.getConfig('app:fileUploadType'),
+      activeExternalAccountTypes,
     };
   }
 

+ 1 - 1
packages/core/src/interfaces/growi-app-info.ts

@@ -19,7 +19,7 @@ export interface IGrowiAdditionalInfo {
   currentActiveUsersCount: number
 }
 
-export interface IGrowiInfo<A extends IGrowiAdditionalInfo> {
+export interface IGrowiInfo<A extends object = IGrowiAdditionalInfo> {
   version: string
   appSiteUrl?: string
   appSiteUrlHashed: string