Changeset View
Changeset View
Standalone View
Standalone View
lib/shared/user-utils.js
// @flow | // @flow | ||||
import { memberHasAdminPowers } from './thread-utils.js'; | import { memberHasAdminPowers } from './thread-utils.js'; | ||||
import bots from '../facts/bots.js'; | |||||
import staff from '../facts/staff.js'; | |||||
import { useENSNames } from '../hooks/ens-cache.js'; | import { useENSNames } from '../hooks/ens-cache.js'; | ||||
import type { | import type { | ||||
ServerThreadInfo, | |||||
RawThreadInfo, | RawThreadInfo, | ||||
ServerThreadInfo, | |||||
ThreadInfo, | ThreadInfo, | ||||
} from '../types/thread-types.js'; | } from '../types/thread-types.js'; | ||||
import type { UserInfo } from '../types/user-types.js'; | import type { UserInfo } from '../types/user-types.js'; | ||||
import { useSelector } from '../utils/redux-utils.js'; | import { useSelector } from '../utils/redux-utils.js'; | ||||
function stringForUser(user: { | function stringForUser(user: { | ||||
+username?: ?string, | +username?: ?string, | ||||
+isViewer?: ?boolean, | +isViewer?: ?boolean, | ||||
Show All 9 Lines | |||||
function stringForUserExplicit(user: { +username: ?string, ... }): string { | function stringForUserExplicit(user: { +username: ?string, ... }): string { | ||||
if (user.username) { | if (user.username) { | ||||
return user.username; | return user.username; | ||||
} else { | } else { | ||||
return 'anonymous'; | return 'anonymous'; | ||||
} | } | ||||
} | } | ||||
function isStaff(userID: string): boolean { | |||||
if (staff.includes(userID)) { | |||||
return true; | |||||
} | |||||
for (const key in bots) { | |||||
const bot = bots[key]; | |||||
if (userID === bot.userID) { | |||||
return true; | |||||
} | |||||
} | |||||
return false; | |||||
} | |||||
function useKeyserverAdmin( | function useKeyserverAdmin( | ||||
community: ThreadInfo | RawThreadInfo | ServerThreadInfo, | community: ThreadInfo | RawThreadInfo | ServerThreadInfo, | ||||
): ?UserInfo { | ): ?UserInfo { | ||||
const userInfos = useSelector(state => state.userStore.userInfos); | const userInfos = useSelector(state => state.userStore.userInfos); | ||||
// This hack only works as long as there is only one admin | // This hack only works as long as there is only one admin | ||||
// Linear task to revert this: | // Linear task to revert this: | ||||
// https://linear.app/comm/issue/ENG-1707/revert-fix-getting-the-keyserver-admin-info | // https://linear.app/comm/issue/ENG-1707/revert-fix-getting-the-keyserver-admin-info | ||||
const admin = community.members.find(memberHasAdminPowers); | const admin = community.members.find(memberHasAdminPowers); | ||||
const adminUserInfo = admin ? userInfos[admin.id] : undefined; | const adminUserInfo = admin ? userInfos[admin.id] : undefined; | ||||
const [adminUserInfoWithENSName] = useENSNames([adminUserInfo]); | const [adminUserInfoWithENSName] = useENSNames([adminUserInfo]); | ||||
return adminUserInfoWithENSName; | return adminUserInfoWithENSName; | ||||
} | } | ||||
export { stringForUser, stringForUserExplicit, isStaff, useKeyserverAdmin }; | export { stringForUser, stringForUserExplicit, useKeyserverAdmin }; |