Skip to content

Commit 3d81cc4

Browse files
committed
chore(ui): Rewrite a bit TimelineEventHandler::add.
This patch renames `TimelineEventHandler::add` to `add_new_item`. This patch also removes the `should_add` argument. The `add_new_item` is called conditionally everytime. I believe this is much cleaner. Otherwise the method should have been called `maybe_add_new_item` with a return type or something that indicates whether the item is added. This patch makes it clear and remove one possible state in `add_new_item`.
1 parent e5487da commit 3d81cc4

File tree

1 file changed

+47
-33
lines changed

1 file changed

+47
-33
lines changed

crates/matrix-sdk-ui/src/timeline/event_handler.rs

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,14 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
354354
self.handle_room_message_edit(re);
355355
}
356356
AnyMessageLikeEventContent::RoomMessage(c) => {
357-
self.add(should_add, TimelineItemContent::message(c, relations, self.items));
357+
if should_add {
358+
self.add_new_item(TimelineItemContent::message(c, relations, self.items));
359+
}
358360
}
359361
AnyMessageLikeEventContent::RoomEncrypted(c) => {
360362
// TODO: Handle replacements if the replaced event is also UTD
361363
let cause = UtdCause::determine(raw_event);
362-
self.add(true, TimelineItemContent::unable_to_decrypt(c, cause));
364+
self.add_new_item(TimelineItemContent::unable_to_decrypt(c, cause));
363365

364366
// Let the hook know that we ran into an unable-to-decrypt that is added to the
365367
// timeline.
@@ -370,7 +372,9 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
370372
}
371373
}
372374
AnyMessageLikeEventContent::Sticker(content) => {
373-
self.add(should_add, TimelineItemContent::Sticker(Sticker { content }));
375+
if should_add {
376+
self.add_new_item(TimelineItemContent::Sticker(Sticker { content }));
377+
}
374378
}
375379
AnyMessageLikeEventContent::UnstablePollStart(
376380
UnstablePollStartEventContent::Replacement(c),
@@ -381,10 +385,14 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
381385
AnyMessageLikeEventContent::UnstablePollResponse(c) => self.handle_poll_response(c),
382386
AnyMessageLikeEventContent::UnstablePollEnd(c) => self.handle_poll_end(c),
383387
AnyMessageLikeEventContent::CallInvite(_) => {
384-
self.add(should_add, TimelineItemContent::CallInvite);
388+
if should_add {
389+
self.add_new_item(TimelineItemContent::CallInvite);
390+
}
385391
}
386392
AnyMessageLikeEventContent::CallNotify(_) => {
387-
self.add(should_add, TimelineItemContent::CallNotify)
393+
if should_add {
394+
self.add_new_item(TimelineItemContent::CallNotify)
395+
}
388396
}
389397

390398
// TODO
@@ -397,8 +405,8 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
397405
},
398406

399407
TimelineEventKind::RedactedMessage { event_type } => {
400-
if event_type != MessageLikeEventType::Reaction {
401-
self.add(should_add, TimelineItemContent::RedactedMessage);
408+
if event_type != MessageLikeEventType::Reaction && should_add {
409+
self.add_new_item(TimelineItemContent::RedactedMessage);
402410
}
403411
}
404412

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

412420
TimelineEventKind::RoomMember { user_id, content, sender } => {
413-
self.add(should_add, TimelineItemContent::room_member(user_id, content, sender));
421+
if should_add {
422+
self.add_new_item(TimelineItemContent::room_member(user_id, content, sender));
423+
}
414424
}
415425

416426
TimelineEventKind::OtherState { state_key, content } => {
417-
self.add(
418-
should_add,
419-
TimelineItemContent::OtherState(OtherState { state_key, content }),
420-
);
427+
if should_add {
428+
self.add_new_item(TimelineItemContent::OtherState(OtherState {
429+
state_key,
430+
content,
431+
}));
432+
}
421433
}
422434

423435
TimelineEventKind::FailedToParseMessageLike { event_type, error } => {
424-
self.add(
425-
should_add,
426-
TimelineItemContent::FailedToParseMessageLike { event_type, error },
427-
);
436+
if should_add {
437+
self.add_new_item(TimelineItemContent::FailedToParseMessageLike {
438+
event_type,
439+
error,
440+
});
441+
}
428442
}
429443

430444
TimelineEventKind::FailedToParseState { event_type, state_key, error } => {
431-
self.add(
432-
should_add,
433-
TimelineItemContent::FailedToParseState { event_type, state_key, error },
434-
);
445+
if should_add {
446+
self.add_new_item(TimelineItemContent::FailedToParseState {
447+
event_type,
448+
state_key,
449+
error,
450+
});
451+
}
435452
}
436453
}
437454

@@ -636,7 +653,10 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
636653
// don't have an event ID that could be referenced by responses yet.
637654
self.meta.poll_pending_events.apply(&event_id, &mut poll_state);
638655
}
639-
self.add(should_add, TimelineItemContent::Poll(poll_state));
656+
657+
if should_add {
658+
self.add_new_item(TimelineItemContent::Poll(poll_state));
659+
}
640660
}
641661

642662
fn handle_poll_response(&mut self, c: UnstablePollResponseEventContent) {
@@ -845,12 +865,8 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
845865
}
846866
}
847867

848-
/// Add a new event item in the timeline.
849-
fn add(&mut self, should_add: bool, content: TimelineItemContent) {
850-
if !should_add {
851-
return;
852-
}
853-
868+
/// Add a new event item in the timeline if `should_add` is true.
869+
fn add_new_item(&mut self, content: TimelineItemContent) {
854870
self.result.item_added = true;
855871

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

861877
let kind: EventTimelineItemKind = match &self.ctx.flow {
862-
Flow::Local { txn_id, abort_handle } => {
863-
LocalEventTimelineItem {
864-
send_state: EventSendState::NotSentYet,
865-
transaction_id: txn_id.to_owned(),
866-
abort_handle: abort_handle.clone(),
867-
}
878+
Flow::Local { txn_id, abort_handle } => LocalEventTimelineItem {
879+
send_state: EventSendState::NotSentYet,
880+
transaction_id: txn_id.to_owned(),
881+
abort_handle: abort_handle.clone(),
868882
}
869883
.into(),
870884

0 commit comments

Comments
 (0)