From 90d9d7128d2ccd7767aee6a8ef4053f38174fdd4 Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Fri, 28 Aug 2020 14:56:59 -0600
Subject: [PATCH] Use FlairStore's cache for group naming

Turns out GroupStore doesn't really know much.
---
 src/RoomInvite.js                                |  4 ++--
 src/components/views/dialogs/CreateRoomDialog.js |  5 ++---
 src/components/views/dialogs/InviteDialog.js     |  8 +++-----
 src/stores/CommunityPrototypeStore.ts            | 11 +++++++++++
 src/stores/FlairStore.js                         | 16 ++++++++++++++++
 5 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/src/RoomInvite.js b/src/RoomInvite.js
index cae4501901..7f2eec32f3 100644
--- a/src/RoomInvite.js
+++ b/src/RoomInvite.js
@@ -76,8 +76,8 @@ export function showCommunityInviteDialog(communityId) {
     });
     if (!chat) chat = rooms[0];
     if (chat) {
-        const summary = GroupStore.getSummary(communityId);
-        showCommunityRoomInviteDialog(chat.roomId, summary?.profile?.name || communityId);
+        const name = CommunityPrototypeInviteDialog.instance.getCommunityName(communityId);
+        showCommunityRoomInviteDialog(chat.roomId, name);
     } else {
         throw new Error("Failed to locate appropriate room to start an invite in");
     }
diff --git a/src/components/views/dialogs/CreateRoomDialog.js b/src/components/views/dialogs/CreateRoomDialog.js
index 4890626527..bdd3de07c0 100644
--- a/src/components/views/dialogs/CreateRoomDialog.js
+++ b/src/components/views/dialogs/CreateRoomDialog.js
@@ -26,7 +26,7 @@ import {MatrixClientPeg} from '../../../MatrixClientPeg';
 import {Key} from "../../../Keyboard";
 import {privateShouldBeEncrypted} from "../../../createRoom";
 import TagOrderStore from "../../../stores/TagOrderStore";
-import GroupStore from "../../../stores/GroupStore";
+import {CommunityPrototypeStore} from "../../../stores/CommunityPrototypeStore";
 
 export default createReactClass({
     displayName: 'CreateRoomDialog',
@@ -240,8 +240,7 @@ export default createReactClass({
 
         let title = this.state.isPublic ? _t('Create a public room') : _t('Create a private room');
         if (TagOrderStore.getSelectedPrototypeTag()) {
-            const summary = GroupStore.getSummary(TagOrderStore.getSelectedPrototypeTag());
-            const name = summary?.profile?.name || TagOrderStore.getSelectedPrototypeTag();
+            const name = CommunityPrototypeStore.instance.getSelectedCommunityName();
             title = _t("Create a room in %(communityName)s", {communityName: name});
         }
         return (
diff --git a/src/components/views/dialogs/InviteDialog.js b/src/components/views/dialogs/InviteDialog.js
index 934ed12ac1..c2fd7e5b0e 100644
--- a/src/components/views/dialogs/InviteDialog.js
+++ b/src/components/views/dialogs/InviteDialog.js
@@ -38,7 +38,7 @@ import {Action} from "../../../dispatcher/actions";
 import {DefaultTagID} from "../../../stores/room-list/models";
 import RoomListStore from "../../../stores/room-list/RoomListStore";
 import TagOrderStore from "../../../stores/TagOrderStore";
-import GroupStore from "../../../stores/GroupStore";
+import {CommunityPrototypeStore} from "../../../stores/CommunityPrototypeStore";
 
 // we have a number of types defined from the Matrix spec which can't reasonably be altered here.
 /* eslint-disable camelcase */
@@ -925,8 +925,7 @@ export default class InviteDialog extends React.PureComponent {
         let sectionSubname = null;
 
         if (kind === 'suggestions' && TagOrderStore.getSelectedPrototypeTag()) {
-            const summary = GroupStore.getSummary(TagOrderStore.getSelectedPrototypeTag());
-            const communityName = summary?.profile?.name || TagOrderStore.getSelectedPrototypeTag();
+            const communityName = CommunityPrototypeStore.instance.getCommunityName(TagOrderStore.getSelectedPrototypeTag());
             sectionSubname = _t("May include members not in %(communityName)s", {communityName});
         }
 
@@ -1099,8 +1098,7 @@ export default class InviteDialog extends React.PureComponent {
                 }},
             );
             if (TagOrderStore.getSelectedPrototypeTag()) {
-                const communityId = TagOrderStore.getSelectedPrototypeTag();
-                const communityName = GroupStore.getSummary(communityId)?.profile?.name || communityId;
+                const communityName = CommunityPrototypeStore.instance.getSelectedCommunityName();
                 helpText = _t(
                     "Start a conversation with someone using their name, username (like <userId/>) or email address. " +
                     "This won't invite them to %(communityName)s. To invite someone to %(communityName)s, click " +
diff --git a/src/stores/CommunityPrototypeStore.ts b/src/stores/CommunityPrototypeStore.ts
index 581f8a97c8..eec0a8aab8 100644
--- a/src/stores/CommunityPrototypeStore.ts
+++ b/src/stores/CommunityPrototypeStore.ts
@@ -22,6 +22,8 @@ import { EffectiveMembership, getEffectiveMembership } from "../utils/membership
 import SettingsStore from "../settings/SettingsStore";
 import * as utils from "matrix-js-sdk/src/utils";
 import { UPDATE_EVENT } from "./AsyncStore";
+import FlairStore from "./FlairStore";
+import TagOrderStore from "./TagOrderStore";
 
 interface IState {
     // nothing of value - we use account data
@@ -43,6 +45,15 @@ export class CommunityPrototypeStore extends AsyncStoreWithClient<IState> {
         return CommunityPrototypeStore.internalInstance;
     }
 
+    public getSelectedCommunityName(): string {
+        return CommunityPrototypeStore.instance.getCommunityName(TagOrderStore.getSelectedPrototypeTag());
+    }
+
+    public getCommunityName(communityId: string): string {
+        const profile = FlairStore.getGroupProfileCachedFast(this.matrixClient, communityId);
+        return profile?.name || communityId;
+    }
+
     protected async onAction(payload: ActionPayload): Promise<any> {
         if (!this.matrixClient || !SettingsStore.getValue("feature_communities_v2_prototypes")) {
             return;
diff --git a/src/stores/FlairStore.js b/src/stores/FlairStore.js
index 94b81c1ba5..10a4d96921 100644
--- a/src/stores/FlairStore.js
+++ b/src/stores/FlairStore.js
@@ -148,6 +148,22 @@ class FlairStore extends EventEmitter {
         });
     }
 
+    /**
+     * Gets the profile for the given group if known, otherwise returns null.
+     * This triggers `getGroupProfileCached` if needed, though the result of the
+     * call will not be returned by this function.
+     * @param matrixClient The matrix client to use to fetch the profile, if needed.
+     * @param groupId The group ID to get the profile for.
+     * @returns The profile if known, otherwise null.
+     */
+    getGroupProfileCachedFast(matrixClient, groupId) {
+        if (this._groupProfiles[groupId]) {
+            return this._groupProfiles[groupId];
+        }
+        this.getGroupProfileCached(matrixClient, groupId);
+        return null;
+    }
+
     async getGroupProfileCached(matrixClient, groupId) {
         if (this._groupProfiles[groupId]) {
             return this._groupProfiles[groupId];