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

feat(multiverse): add support to toggle a reaction on the last message #4135

Closed
wants to merge 1 commit into from
Closed
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
37 changes: 37 additions & 0 deletions labs/multiverse/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,41 @@ impl App {
}
}

async fn toggle_reaction_to_latest_msg(&mut self) {
let selected = self.get_selected_room_id(None);

if let Some((sdk_timeline, items)) = selected.and_then(|room_id| {
self.timelines
.lock()
.unwrap()
.get(&room_id)
.map(|timeline| (timeline.timeline.clone(), timeline.items.clone()))
}) {
// Look for the latest (most recent) room message.
let item_id = {
let items = items.lock().unwrap();
items.iter().rev().find_map(|it| {
it.as_event()
.and_then(|ev| ev.content().as_message().is_some().then(|| ev.identifier()))
})
};

// If found, send a reaction.
if let Some(item_id) = item_id {
match sdk_timeline.toggle_reaction(&item_id, "🥰").await {
Ok(_) => {
self.set_status_message("reaction sent!".to_owned());
}
Err(err) => self.set_status_message(format!("error when reacting: {err}")),
}
} else {
self.set_status_message("no item to react to".to_owned());
}
} else {
self.set_status_message("missing timeline for room".to_owned());
};
}

/// Run a small back-pagination (expect a batch of 20 events, continue until
/// we get 10 timeline items or hit the timeline start).
fn back_paginate(&mut self) {
Expand Down Expand Up @@ -484,6 +519,8 @@ impl App {
};
}

Char('l') => self.toggle_reaction_to_latest_msg().await,

Char('r') => self.details_mode = DetailsMode::ReadReceipts,
Char('t') => self.details_mode = DetailsMode::TimelineItems,
Char('e') => self.details_mode = DetailsMode::Events,
Expand Down
Loading