|
| 1 | +/** |
| 2 | +Copyright 2026 Element Creations Ltd. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | +Please see LICENSE files in the repository root for full details. |
| 6 | +*/ |
| 7 | + |
| 8 | +import { useEffect, useState } from "react"; |
| 9 | +import { ClientEvent, MatrixError } from "matrix-js-sdk/src/matrix"; |
| 10 | +import { logger as rootLogger } from "matrix-js-sdk/src/logger"; |
| 11 | + |
| 12 | +import { useMatrixClientContext } from "../contexts/MatrixClientContext"; |
| 13 | +import { useTypedEventEmitter } from "./useEventEmitter"; |
| 14 | +import { useFeatureEnabled } from "./useSettings"; |
| 15 | + |
| 16 | +const logger = rootLogger.getChild("useUserStatus"); |
| 17 | + |
| 18 | +export interface UserStatus { |
| 19 | + emoji: string; |
| 20 | + text: string; |
| 21 | +} |
| 22 | + |
| 23 | +const MAX_STATUS_TEXT_BYTES = 256; |
| 24 | + |
| 25 | +export function userStatusTextWithinMaxLength(text: string): boolean { |
| 26 | + const textEncoder = new TextEncoder(); |
| 27 | + return textEncoder.encode(text).length <= MAX_STATUS_TEXT_BYTES; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Hook to get the MSC4426 user status for a given user ID. Returns undefined if the feature is disabled, |
| 32 | + * the user does not have a status, or if there was an error fetching the status. |
| 33 | + * |
| 34 | + * @param userId The ID of the user whose status is being fetched. |
| 35 | + * @returns The user's status, or undefined if not available. |
| 36 | + */ |
| 37 | +export function useUserStatus(userId: string | undefined): UserStatus | undefined { |
| 38 | + const isEnabled = useFeatureEnabled("feature_user_status"); |
| 39 | + const matrixClient = useMatrixClientContext(); |
| 40 | + const [rawUserStatus, setRawUserStatus] = useState<unknown>(); |
| 41 | + |
| 42 | + useTypedEventEmitter(matrixClient, ClientEvent.UserProfileUpdate, (syncedUserId, syncProfile) => { |
| 43 | + if (syncedUserId !== userId) { |
| 44 | + return; |
| 45 | + } |
| 46 | + if (syncProfile["org.matrix.msc4426.status"]) { |
| 47 | + setRawUserStatus(syncProfile["org.matrix.msc4426.status"]); |
| 48 | + } |
| 49 | + }); |
| 50 | + useEffect(() => { |
| 51 | + (async () => { |
| 52 | + if (!isEnabled) { |
| 53 | + return; |
| 54 | + } |
| 55 | + if (!userId) { |
| 56 | + setRawUserStatus(undefined); |
| 57 | + return; |
| 58 | + } |
| 59 | + if ((await matrixClient.doesServerSupportExtendedProfiles()) === false) { |
| 60 | + setRawUserStatus(undefined); |
| 61 | + return; |
| 62 | + } |
| 63 | + try { |
| 64 | + const result = await matrixClient.getExtendedProfileProperty(userId, "org.matrix.msc4426.status"); |
| 65 | + setRawUserStatus(result); |
| 66 | + } catch (ex) { |
| 67 | + if (ex instanceof MatrixError && ex.errcode === "M_NOT_FOUND") { |
| 68 | + setRawUserStatus(undefined); |
| 69 | + } else { |
| 70 | + logger.warn(`Failed to get userStatus for ${userId}`, ex); |
| 71 | + } |
| 72 | + } |
| 73 | + })(); |
| 74 | + }, [isEnabled, userId, matrixClient]); |
| 75 | + if (!isEnabled) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + if (typeof rawUserStatus !== "object" || rawUserStatus === null) { |
| 80 | + logger.warn(`value of "org.matrix.msc4426.status" was not an object for ${userId}`); |
| 81 | + return; |
| 82 | + } |
| 83 | + if ("emoji" in rawUserStatus === false || typeof rawUserStatus.emoji !== "string" || !rawUserStatus.emoji) { |
| 84 | + logger.warn(`"emoji" property was not a valid string for ${userId}`); |
| 85 | + return; |
| 86 | + } |
| 87 | + if ("text" in rawUserStatus === false || typeof rawUserStatus.text !== "string" || !rawUserStatus.text) { |
| 88 | + logger.warn(`"text" property was not a valid string for ${userId}`); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + return { |
| 93 | + emoji: rawUserStatus.emoji, |
| 94 | + text: userStatusTextWithinMaxLength(rawUserStatus.text) |
| 95 | + ? rawUserStatus.text |
| 96 | + : `${rawUserStatus.text.slice(0, MAX_STATUS_TEXT_BYTES)}…`, |
| 97 | + }; |
| 98 | +} |
0 commit comments