Skip to content

Commit

Permalink
[lib] Fix useThreadsWithPermission check
Browse files Browse the repository at this point in the history
Summary:
This check is currently equivalent to a `threadTypeIsCommunityRoot` check. I initially gleaned this by reading the code, but I verified it by comparing the two checks for each thread in my `ThreadStore` on production. The reason is that `communityRootMembersToRole` only contains entries for community roots, and all community roots have at least one admin.

This is probably the wrong check for us to making here. We should probably treat the channels inside the community root the same as we treat the community. Additionally, we should probably carve out an exception for channels inside GENESIS, which we generally treat the same as thick threads (for the purposes of determining whether to freeze the chat due to there being only one other member, and that member has blocked you).

Depends on D13874

Test Plan: I deployed a custom environment to comm.software where I could run React profiling on the production dataset as described [here](https://linear.app/comm/issue/ENG-9862/investigate-performance-issues-when-threadstore-changes#comment-a12a7c6d). I confirmed this change reduced rerender time from 3.0s to 0.5s

Reviewers: tomek, angelika

Reviewed By: tomek

Differential Revision: https://phab.comm.dev/D13886
  • Loading branch information
Ashoat committed Nov 7, 2024
1 parent e17a45b commit caf1601
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions lib/shared/thread-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,29 +175,36 @@ function useCommunityRootMembersToRole(
return communityRootMembersToRole;
}

// This function returns true for all thick threads, as well as all channels
// inside GENESIS. Channels inside GENESIS were used in place of thick threads
// before thick threads were launched, and as such we mirror "freezing" behavior
// between them and thick threads. "Freezing" a thread can occur when a user
// blocks another user, and those two users are the only members of a given
// chat. Note that we exclude the GENESIS community root here, as the root
// itself has never been used in place of thick threads. Also note that
// grandchild channels of GENESIS get this behavior too, even though we don't
// currently support channels inside thick threads.
function threadIsThickOrChannelInsideGenesis(threadInfo: ThreadInfo): boolean {
if (threadTypeIsThick(threadInfo.type)) {
return true;
}
if (getCommunity(threadInfo) !== genesis().id) {
return false;
}
return threadInfo.id !== genesis().id;
}

function useThreadsWithPermission(
threadInfos: $ReadOnlyArray<ThreadInfo>,
permission: ThreadPermission,
): $ReadOnlyArray<ThreadInfo> {
const loggedInUserInfo = useLoggedInUserInfo();
const userInfos = useSelector(state => state.userStore.userInfos);
const allThreadInfos = useSelector(state => state.threadStore.threadInfos);
const allThreadInfosArray = React.useMemo(
() => values(allThreadInfos),
[allThreadInfos],
);

const communityRootMembersToRole =
useCommunityRootMembersToRole(allThreadInfosArray);

return React.useMemo(() => {
return threadInfos.filter((threadInfo: ThreadInfo) => {
const membersToRole = communityRootMembersToRole[threadInfo.id];
const memberHasAdminRole = threadMembersWithoutAddedAdmin(
threadInfo,
).some(member => roleIsAdminRole(membersToRole?.[member.id]));

if (memberHasAdminRole || !loggedInUserInfo) {
const isGroupChat = threadIsThickOrChannelInsideGenesis(threadInfo);
if (!isGroupChat || !loggedInUserInfo) {
return hasPermission(threadInfo.currentUser.permissions, permission);
}

Expand All @@ -214,13 +221,7 @@ function useThreadsWithPermission(

return hasPermission(permissions, permission);
});
}, [
threadInfos,
communityRootMembersToRole,
loggedInUserInfo,
userInfos,
permission,
]);
}, [threadInfos, loggedInUserInfo, userInfos, permission]);
}

function useThreadHasPermission(
Expand Down

0 comments on commit caf1601

Please sign in to comment.