Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ui): Rewrite a bit TimelineEventHandler::add #3527

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 46 additions & 32 deletions crates/matrix-sdk-ui/src/timeline/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,14 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
self.handle_room_message_edit(re);
}
AnyMessageLikeEventContent::RoomMessage(c) => {
self.add(should_add, TimelineItemContent::message(c, relations, self.items));
if should_add {
self.add_item(TimelineItemContent::message(c, relations, self.items));
}
}
AnyMessageLikeEventContent::RoomEncrypted(c) => {
// TODO: Handle replacements if the replaced event is also UTD
let cause = UtdCause::determine(raw_event);
self.add(true, TimelineItemContent::unable_to_decrypt(c, cause));
self.add_item(TimelineItemContent::unable_to_decrypt(c, cause));

// Let the hook know that we ran into an unable-to-decrypt that is added to the
// timeline.
Expand All @@ -370,7 +372,9 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
}
}
AnyMessageLikeEventContent::Sticker(content) => {
self.add(should_add, TimelineItemContent::Sticker(Sticker { content }));
if should_add {
self.add_item(TimelineItemContent::Sticker(Sticker { content }));
}
}
AnyMessageLikeEventContent::UnstablePollStart(
UnstablePollStartEventContent::Replacement(c),
Expand All @@ -381,10 +385,14 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
AnyMessageLikeEventContent::UnstablePollResponse(c) => self.handle_poll_response(c),
AnyMessageLikeEventContent::UnstablePollEnd(c) => self.handle_poll_end(c),
AnyMessageLikeEventContent::CallInvite(_) => {
self.add(should_add, TimelineItemContent::CallInvite);
if should_add {
self.add_item(TimelineItemContent::CallInvite);
}
}
AnyMessageLikeEventContent::CallNotify(_) => {
self.add(should_add, TimelineItemContent::CallNotify)
if should_add {
self.add_item(TimelineItemContent::CallNotify)
}
}

// TODO
Expand All @@ -397,8 +405,8 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
},

TimelineEventKind::RedactedMessage { event_type } => {
if event_type != MessageLikeEventType::Reaction {
self.add(should_add, TimelineItemContent::RedactedMessage);
if event_type != MessageLikeEventType::Reaction && should_add {
self.add_item(TimelineItemContent::RedactedMessage);
}
}

Expand All @@ -410,28 +418,37 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
}

TimelineEventKind::RoomMember { user_id, content, sender } => {
self.add(should_add, TimelineItemContent::room_member(user_id, content, sender));
if should_add {
self.add_item(TimelineItemContent::room_member(user_id, content, sender));
}
}

TimelineEventKind::OtherState { state_key, content } => {
self.add(
should_add,
TimelineItemContent::OtherState(OtherState { state_key, content }),
);
if should_add {
self.add_item(TimelineItemContent::OtherState(OtherState {
state_key,
content,
}));
}
}

TimelineEventKind::FailedToParseMessageLike { event_type, error } => {
self.add(
should_add,
TimelineItemContent::FailedToParseMessageLike { event_type, error },
);
if should_add {
self.add_item(TimelineItemContent::FailedToParseMessageLike {
event_type,
error,
});
}
}

TimelineEventKind::FailedToParseState { event_type, state_key, error } => {
self.add(
should_add,
TimelineItemContent::FailedToParseState { event_type, state_key, error },
);
if should_add {
self.add_item(TimelineItemContent::FailedToParseState {
event_type,
state_key,
error,
});
}
}
}

Expand Down Expand Up @@ -636,7 +653,10 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
// don't have an event ID that could be referenced by responses yet.
self.meta.poll_pending_events.apply(&event_id, &mut poll_state);
}
self.add(should_add, TimelineItemContent::Poll(poll_state));

if should_add {
self.add_item(TimelineItemContent::Poll(poll_state));
}
}

fn handle_poll_response(&mut self, c: UnstablePollResponseEventContent) {
Expand Down Expand Up @@ -846,11 +866,7 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
}

/// Add a new event item in the timeline.
fn add(&mut self, should_add: bool, content: TimelineItemContent) {
if !should_add {
return;
}
Hywan marked this conversation as resolved.
Show resolved Hide resolved

fn add_item(&mut self, content: TimelineItemContent) {
self.result.item_added = true;

let sender = self.ctx.sender.to_owned();
Expand All @@ -859,12 +875,10 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
let mut reactions = self.pending_reactions().unwrap_or_default();

let kind: EventTimelineItemKind = match &self.ctx.flow {
Flow::Local { txn_id, abort_handle } => {
LocalEventTimelineItem {
send_state: EventSendState::NotSentYet,
transaction_id: txn_id.to_owned(),
abort_handle: abort_handle.clone(),
}
Flow::Local { txn_id, abort_handle } => LocalEventTimelineItem {
send_state: EventSendState::NotSentYet,
transaction_id: txn_id.to_owned(),
abort_handle: abort_handle.clone(),
}
.into(),

Expand Down
Loading