diff --git a/lib/shared/user-utils.js b/lib/shared/user-utils.js --- a/lib/shared/user-utils.js +++ b/lib/shared/user-utils.js @@ -2,8 +2,13 @@ import bots from '../facts/bots'; import staff from '../facts/staff'; -import type { RelativeMemberInfo } from '../types/thread-types'; -import type { RelativeUserInfo } from '../types/user-types'; +import type { RelativeMemberInfo, ThreadInfo } from '../types/thread-types'; +import type { + RelativeUserInfo, + UserInfo, + UserInfos, +} from '../types/user-types'; +import { memberHasAdminPowers } from './thread-utils'; function stringForUser(user: RelativeUserInfo | RelativeMemberInfo): string { if (user.isViewer) { @@ -28,4 +33,17 @@ return false; } -export { stringForUser, isStaff }; +function getKeyserverAdmin( + community: ThreadInfo, + userInfos: UserInfos, +): ?UserInfo { + for (const member: RelativeMemberInfo of community.members) { + // This only works as long as there is only one admin + if (memberHasAdminPowers(member)) { + return userInfos[member.id]; + } + } + return undefined; +} + +export { stringForUser, isStaff, getKeyserverAdmin }; diff --git a/native/components/community-pill.react.js b/native/components/community-pill.react.js --- a/native/components/community-pill.react.js +++ b/native/components/community-pill.react.js @@ -3,8 +3,8 @@ import * as React from 'react'; import { View, StyleSheet } from 'react-native'; -import { memberHasAdminPowers } from 'lib/shared/thread-utils'; -import type { ThreadInfo, RelativeMemberInfo } from 'lib/types/thread-types'; +import { getKeyserverAdmin } from 'lib/shared/user-utils'; +import type { ThreadInfo } from 'lib/types/thread-types'; import { useSelector } from '../redux/redux-utils'; import { useColors } from '../themes/colors'; @@ -20,11 +20,7 @@ const userInfos = useSelector(state => state.userStore.userInfos); const keyserverOperatorUsername: ?string = React.useMemo(() => { - for (const member: RelativeMemberInfo of community.members) { - if (memberHasAdminPowers(member)) { - return userInfos[member.id].username; - } - } + return getKeyserverAdmin(community, userInfos)?.username; }, [community, userInfos]); const colors = useColors(); diff --git a/web/chat/chat-thread-ancestors.react.js b/web/chat/chat-thread-ancestors.react.js --- a/web/chat/chat-thread-ancestors.react.js +++ b/web/chat/chat-thread-ancestors.react.js @@ -4,7 +4,8 @@ import * as React from 'react'; import { useAncestorThreads } from 'lib/shared/ancestor-threads'; -import { memberHasAdminPowers, colorIsDark } from 'lib/shared/thread-utils'; +import { colorIsDark } from 'lib/shared/thread-utils'; +import { getKeyserverAdmin } from 'lib/shared/user-utils'; import type { ThreadInfo } from 'lib/types/thread-types'; import CommIcon from '../CommIcon.react'; @@ -40,13 +41,8 @@ const userInfos = useSelector(state => state.userStore.userInfos); const community = ancestorThreads[0] ?? threadInfo; const keyserverOwnerUsername: ?string = React.useMemo(() => { - for (const member of community.members) { - if (memberHasAdminPowers(member)) { - return userInfos[member.id].username; - } - } - return undefined; - }, [community.members, userInfos]); + return getKeyserverAdmin(community, userInfos)?.username; + }, [community, userInfos]); const keyserverInfo = React.useMemo( () => (