Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Tidy checkForPreJoinUISI and add some logging (#8567)
Browse files Browse the repository at this point in the history
* Tidy checkForPreJoinUISI

* Add some logging
  • Loading branch information
t3chguy authored May 12, 2022
1 parent 0951a76 commit 25428b0
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions src/components/structures/TimelinePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1456,25 +1456,24 @@ class TimelinePanel extends React.Component<IProps, IState> {
* such events were found, then it returns 0.
*/
private checkForPreJoinUISI(events: MatrixEvent[]): number {
const cli = MatrixClientPeg.get();
const room = this.props.timelineSet.room;

const isThreadTimeline = [TimelineRenderingType.Thread, TimelineRenderingType.ThreadsList]
.includes(this.context.timelineRenderingType);
if (events.length === 0
|| !room
|| !MatrixClientPeg.get().isRoomEncrypted(room.roomId)
|| isThreadTimeline) {
if (events.length === 0 || !room || !cli.isRoomEncrypted(room.roomId) || isThreadTimeline) {
logger.info("checkForPreJoinUISI: showing all messages, skipping check");
return 0;
}

const userId = MatrixClientPeg.get().credentials.userId;
const userId = cli.credentials.userId;

// get the user's membership at the last event by getting the timeline
// that the event belongs to, and traversing the timeline looking for
// that event, while keeping track of the user's membership
let i;
let i = events.length - 1;
let userMembership = "leave";
for (i = events.length - 1; i >= 0; i--) {
for (; i >= 0; i--) {
const timeline = room.getTimelineForEvent(events[i].getId());
if (!timeline) {
// Somehow, it seems to be possible for live events to not have
Expand All @@ -1486,18 +1485,15 @@ class TimelinePanel extends React.Component<IProps, IState> {
);
continue;
}
const userMembershipEvent =
timeline.getState(EventTimeline.FORWARDS).getMember(userId);
userMembership = userMembershipEvent ? userMembershipEvent.membership : "leave";

userMembership = timeline.getState(EventTimeline.FORWARDS).getMember(userId)?.membership ?? "leave";
const timelineEvents = timeline.getEvents();
for (let j = timelineEvents.length - 1; j >= 0; j--) {
const event = timelineEvents[j];
if (event.getId() === events[i].getId()) {
break;
} else if (event.getStateKey() === userId
&& event.getType() === "m.room.member") {
const prevContent = event.getPrevContent();
userMembership = prevContent.membership || "leave";
} else if (event.getStateKey() === userId && event.getType() === EventType.RoomMember) {
userMembership = event.getPrevContent().membership || "leave";
}
}
break;
Expand All @@ -1507,19 +1503,18 @@ class TimelinePanel extends React.Component<IProps, IState> {
// one that was sent when the user wasn't in the room
for (; i >= 0; i--) {
const event = events[i];
if (event.getStateKey() === userId
&& event.getType() === "m.room.member") {
const prevContent = event.getPrevContent();
userMembership = prevContent.membership || "leave";
} else if (userMembership === "leave" &&
(event.isDecryptionFailure() || event.isBeingDecrypted())) {
// reached an undecryptable message when the user wasn't in
// the room -- don't try to load any more
if (event.getStateKey() === userId && event.getType() === EventType.RoomMember) {
userMembership = event.getPrevContent().membership || "leave";
} else if (userMembership === "leave" && (event.isDecryptionFailure() || event.isBeingDecrypted())) {
// reached an undecryptable message when the user wasn't in the room -- don't try to load any more
// Note: for now, we assume that events that are being decrypted are
// not decryptable
// not decryptable - we will be called once more when it is decrypted.
logger.info("checkForPreJoinUISI: reached a pre-join UISI at index ", i);
return i + 1;
}
}

logger.info("checkForPreJoinUISI: did not find pre-join UISI");
return 0;
}

Expand Down

0 comments on commit 25428b0

Please sign in to comment.