integrate per-message profile fetching in RoomInput component
This commit is contained in:
2 files changed
+45
-15
No files matched your search
@@ -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<HTMLDivElement, RoomInputProps>(
|
||||
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<HTMLDivElement, RoomInputProps>(
|
||||
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<HTMLDivElement, RoomInputProps>(
|
||||
canSendReaction,
|
||||
mx,
|
||||
roomId,
|
||||
threadRootId,
|
||||
permessageprofile,
|
||||
replyDraft,
|
||||
silentReply,
|
||||
scheduledTime,
|
||||
@@ -731,12 +754,13 @@ export const RoomInput = forwardRef<HTMLDivElement, RoomInputProps>(
|
||||
handleQuickReact,
|
||||
commands,
|
||||
sendTypingStatus,
|
||||
room,
|
||||
queryClient,
|
||||
threadRootId,
|
||||
setReplyDraft,
|
||||
isEncrypted,
|
||||
setEditingScheduledDelayId,
|
||||
setScheduledTime,
|
||||
room,
|
||||
]);
|
||||
|
||||
const handleKeyDown: KeyboardEventHandler = useCallback(
|
||||
|
||||
@@ -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<PerMessageProfile | undefined> {
|
||||
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<PerMessageProfile[]> {
|
||||
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<PerMessageProfile | undefined> {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user