diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index 1b0e6f5f..2773dc77 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -151,6 +151,7 @@ import { usePowerLevelsContext } from '$hooks/usePowerLevels'; import { useRoomCreators } from '$hooks/useRoomCreators'; import { useRoomPermissions } from '$hooks/useRoomPermissions'; import { AutocompleteNotice } from '$components/editor/autocomplete/AutocompleteNotice'; +import { getCurrentlyUsedPerMessageProfileForRoom } from '$hooks/usePerMessageProfile'; import { SchedulePickerDialog } from './schedule-send'; import * as css from './schedule-send/SchedulePickerDialog.css'; import { @@ -269,6 +270,12 @@ export const RoomInput = forwardRef( room ); + const permessageprofile = useCallback(async () => { + const profile = await getCurrentlyUsedPerMessageProfileForRoom(mx, roomId); + console.debug('Fetched per-message profile:', { profile, roomId }); + return profile; + }, [mx, roomId]); + const [uploadBoard, setUploadBoard] = useState(true); const [selectedFiles, setSelectedFiles] = useAtom(roomIdToUploadItemsAtomFamily(draftKey)); const uploadFamilyObserverAtom = createUploadFamilyObserverAtom( @@ -624,11 +631,27 @@ export const RoomInput = forwardRef( const formattedBody = customHtml; const mentionData = getMentions(mx, roomId, editor); + const perMessageProfile = await permessageprofile(); + const content: IContent = { msgtype: msgType, body, }; + if (perMessageProfile) { + console.warn( + 'Using per-message profile:', + perMessageProfile.id, + perMessageProfile.name, + perMessageProfile.avatarUrl + ); + content['com.beeper.per_message_profile'] = { + id: perMessageProfile.id, + displayname: perMessageProfile.name, + avatar_url: perMessageProfile.avatarUrl, + }; + } + if (replyDraft && !silentReply) { mentionData.users.add(replyDraft.userId); } @@ -723,7 +746,7 @@ export const RoomInput = forwardRef( canSendReaction, mx, roomId, - threadRootId, + permessageprofile, replyDraft, silentReply, scheduledTime, @@ -731,12 +754,13 @@ export const RoomInput = forwardRef( handleQuickReact, commands, sendTypingStatus, + room, queryClient, + threadRootId, setReplyDraft, isEncrypted, setEditingScheduledDelayId, setScheduledTime, - room, ]); const handleKeyDown: KeyboardEventHandler = useCallback( diff --git a/src/app/hooks/usePerMessageProfile.ts b/src/app/hooks/usePerMessageProfile.ts index 67818674..8107940a 100644 --- a/src/app/hooks/usePerMessageProfile.ts +++ b/src/app/hooks/usePerMessageProfile.ts @@ -44,20 +44,19 @@ type PerMessageProfileRoomAssociationWrapper = { associations: PerMessageProfileRoomAssociation[]; }; -export function getPerMessageProfileById( +export async function getPerMessageProfileById( mx: MatrixClient, id: string -): PerMessageProfile | undefined { - const profile = mx.getAccountData(`fyi.cisnt.permessageprofile.${id}` as any); - return profile ? (profile as unknown as PerMessageProfile) : undefined; +): Promise { + const profile = await mx.getAccountData(`fyi.cisnt.permessageprofile.${id}` as any); + return profile ? (profile.getContent() as unknown as PerMessageProfile) : undefined; } -export function getAllPerMessageProfiles(mx: MatrixClient): PerMessageProfile[] { - const profileData = mx.getAccountData('fyi.cisnt.permessageprofile.index' as any); +export async function getAllPerMessageProfiles(mx: MatrixClient): Promise { + const profileData = await mx.getAccountData('fyi.cisnt.permessageprofile.index' as any); const profileIds = (profileData?.getContent() as PerMessageProfileIndex)?.profileIds || []; - return profileIds - .map((id) => getPerMessageProfileById(mx, id)) - .filter((profile): profile is PerMessageProfile => profile !== undefined); + const profiles = await Promise.all(profileIds.map((id) => getPerMessageProfileById(mx, id))); + return profiles.filter((profile): profile is PerMessageProfile => profile !== undefined); } export function addOrUpdatePerMessageProfile(mx: MatrixClient, profile: PerMessageProfile) { @@ -87,10 +86,10 @@ export function deletePerMessageProfile(mx: MatrixClient, id: string) { * @param roomId the room id you are querying for * @returns the profile to be used */ -export function getCurrentlyUsedPerMessageProfileIdForRoom( +export async function getCurrentlyUsedPerMessageProfileForRoom( mx: MatrixClient, roomId: string -): PerMessageProfile | undefined { +): Promise { const accountData = mx.getAccountData(`fyi.cisnt.permessageprofile.roomassociation` as any); const content = accountData?.getContent()?.associations as | PerMessageProfileRoomAssociation[] @@ -108,8 +107,15 @@ export function getCurrentlyUsedPerMessageProfileIdForRoom( ) .find((assoc: PerMessageProfileRoomAssociation) => assoc.roomId === roomId)?.profileId; - console.warn('getCurrentlyUsedPerMessageProfileIdForRoom', { profileId, roomId }); - return profileId ? getPerMessageProfileById(mx, profileId) : undefined; + const pmp = profileId ? await getPerMessageProfileById(mx, profileId) : undefined; + console.warn('getCurrentlyUsedPerMessageProfileIdForRoom', { + accountData, + content, + roomId, + profileId, + pmp, + }); + return profileId ? pmp : undefined; } /**