Skip to content

Commit 78fd8a1

Browse files
authored
Merge pull request #449 from SableClient/fix/pmp-rendering
Fix various pmp issues
2 parents 666c28a + 8c34d4a commit 78fd8a1

5 files changed

Lines changed: 52 additions & 10 deletions

File tree

.changeset/fix-pmp-collapsing.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
default: patch
3+
---
4+
5+
Fix per-message profile messages collapsing together when different profiles are used.

.changeset/fix-pmp-edit-render.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
default: patch
3+
---
4+
5+
Fix per-message profiles not updating avatar/name if edit events are recieved.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
default: patch
3+
---
4+
5+
Fix per-message profiles not rendering in encrypted rooms.

src/app/features/room/message/Message.tsx

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import {
4949
Username,
5050
UsernameBold,
5151
} from '$components/message';
52-
import { canEditEvent, getEventEdits, getMemberAvatarMxc } from '$utils/room';
52+
import { canEditEvent, getEditedEvent, getEventEdits, getMemberAvatarMxc } from '$utils/room';
5353
import { mxcUrlToHttp } from '$utils/matrix';
5454
import { getSettings, MessageLayout, MessageSpacing, settingsAtom } from '$state/settings';
5555
import { nicknamesAtom, setNicknameAtom } from '$state/nicknames';
@@ -380,18 +380,38 @@ function MessageInternal(
380380
const mx = useMatrixClient();
381381
const useAuthentication = useMediaAuthentication();
382382

383+
const [editVersion, setEditVersion] = useState(0);
384+
385+
useEffect(() => {
386+
const onReplaced = () => setEditVersion((v) => v + 1);
387+
mEvent.on('Event.replaced' as any, onReplaced);
388+
return () => {
389+
mEvent.off('Event.replaced' as any, onReplaced);
390+
};
391+
}, [mEvent]);
392+
383393
/**
384394
* We read the per-message profile from the event content here.
385395
* We have to do this in the message component because the per-message profile can be different for each message, and we need to read it for each message individually.
386396
* We also want to avoid reading and parsing the per-message profile in a parent component like the timeline, because that would be inefficient and would cause unnecessary re-renders of the entire timeline whenever a per-message profile changes.
387397
*/
388-
const pmp: PerMessageProfileBeeperFormat | undefined = useMemo(
389-
() =>
390-
mEvent.event.content?.['com.beeper.per_message_profile'] as
391-
| PerMessageProfileBeeperFormat
392-
| undefined,
393-
[mEvent]
394-
);
398+
const pmp: PerMessageProfileBeeperFormat | undefined = useMemo(() => {
399+
const evtId = mEvent.getId();
400+
const evtTimeline = evtId ? room.getTimelineForEvent(evtId) : undefined;
401+
const editedEvent =
402+
evtTimeline && evtId
403+
? getEditedEvent(evtId, mEvent, evtTimeline.getTimelineSet())
404+
: undefined;
405+
406+
const resolvedContent = editedEvent
407+
? editedEvent.getContent()['m.new_content']
408+
: mEvent.getContent();
409+
410+
return resolvedContent?.['com.beeper.per_message_profile'] as
411+
| PerMessageProfileBeeperFormat
412+
| undefined;
413+
// eslint-disable-next-line react-hooks/exhaustive-deps
414+
}, [mEvent, room, editVersion]);
395415

396416
/**
397417
* We convert the per-message profile from the Beeper format to our internal format here in the message component

src/app/hooks/timeline/useProcessedTimeline.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,15 @@ export function useProcessedTimeline({
143143
const typeMatch =
144144
normalizeMessageType(getPrevType.call(prevEvent)) === normalizeMessageType(type);
145145
const dividerOk = !newDivider || eventSender === mxUserId;
146-
147-
collapsed = dividerOk && senderMatch && typeMatch && withinTimeThreshold;
146+
const getPmpId = (ev: MatrixEvent): string | null =>
147+
ev.getContent()?.['com.beeper.per_message_profile']?.id ?? null;
148+
149+
collapsed =
150+
dividerOk &&
151+
senderMatch &&
152+
typeMatch &&
153+
withinTimeThreshold &&
154+
getPmpId(prevEvent) === getPmpId(mEvent);
148155
} else {
149156
const prevIsMessageEvent = MESSAGE_EVENT_TYPES.includes(getPrevType.call(prevEvent));
150157
collapsed = !prevIsMessageEvent;

0 commit comments

Comments
 (0)