Skip to content

Commit

Permalink
Be a bit smarter with collections.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkulak committed May 7, 2023
1 parent 212cf83 commit 52c3fb6
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 40 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ open = "4.0"
rand = "0.8.5"
serde = { version = "1.0", features = ["derive"] }
simple-logging = "2.0"
sorted-vec = "0.8"
tempfile = "3"
textwrap = "0.16"
timeago = "0.4"
Expand Down
5 changes: 3 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use matrix_sdk::encryption::verification::SasVerification;
use matrix_sdk::room::{Joined, Room};
use once_cell::sync::OnceCell;
use ruma::events::receipt::ReceiptEventContent;
use std::collections::VecDeque;
use std::sync::mpsc::Sender;
use std::sync::Mutex;

Expand Down Expand Up @@ -41,7 +42,7 @@ pub struct App {
pub sas: Option<SasVerification>,

/// Keep old read receipts around
pub receipts: Vec<(Joined, ReceiptEventContent)>,
pub receipts: VecDeque<(Joined, ReceiptEventContent)>,
}

impl App {
Expand All @@ -61,7 +62,7 @@ impl App {
matrix,
sender: send,
sas: None,
receipts: vec![],
receipts: VecDeque::new(),
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ pub fn handle_app_event(event: MatuiEvent, app: &mut App) {
c.receipt_event(&joined, &content);
}

app.receipts.push((joined, content));
app.receipts.push_back((joined, content));

if app.receipts.len() > 250 {
app.receipts.remove(0);
if app.receipts.len() > 500 {
app.receipts.pop_front();
}
}
MatuiEvent::VerificationStarted(sas, emoji) => {
Expand Down
38 changes: 18 additions & 20 deletions src/widgets/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use ruma::events::room::member::MembershipState;
use ruma::events::room::message::MessageType::Text;
use ruma::events::AnyTimelineEvent;
use ruma::{OwnedEventId, OwnedUserId};
use sorted_vec::SortedVec;
use std::cell::Cell;
use std::cmp::Ordering;
use std::collections::BTreeSet;
use std::ops::Deref;

use tui::buffer::Buffer;
Expand All @@ -40,7 +40,7 @@ use super::receipts::Receipts;
pub struct Chat {
matrix: Matrix,
room: DecoratedRoom,
events: SortedVec<OrderedEvent>,
events: BTreeSet<OrderedEvent>,
receipts: Receipts,
messages: Vec<Message>,
read_to: Option<OwnedEventId>,
Expand Down Expand Up @@ -71,7 +71,7 @@ impl Chat {
Some(Self {
matrix: matrix.clone(),
room: decorated_room,
events: SortedVec::new(),
events: BTreeSet::new(),
receipts: Receipts::new(matrix.me()),
messages: vec![],
read_to: None,
Expand Down Expand Up @@ -343,8 +343,7 @@ impl Chat {
}

self.check_event_sender(&event);
self.events.push(OrderedEvent::new(event));
self.dedupe_events();
self.events.insert(OrderedEvent::new(event));
self.messages = make_message_list(&self.events, &self.members, &self.receipts);
self.set_fully_read();
}
Expand Down Expand Up @@ -397,11 +396,9 @@ impl Chat {

for event in batch.events {
self.check_event_sender(&event);
self.events.push(OrderedEvent::new(event));
self.events.insert(OrderedEvent::new(event));
}

self.dedupe_events();

let reset = self.messages.is_empty();

self.messages = make_message_list(&self.events, &self.members, &self.receipts);
Expand Down Expand Up @@ -509,15 +506,6 @@ impl Chat {
})
}

fn dedupe_events(&mut self) {
let prev = self.messages.len();
self.messages.dedup_by(|left, right| left.id == right.id);

if self.messages.len() < prev {
info!("found at least one duplicate event");
}
}

pub fn room_member_event(&mut self, room: Joined, member: RoomMember) {
if self.room.room_id() != room.room_id() {
return;
Expand Down Expand Up @@ -708,14 +696,24 @@ impl Deref for OrderedEvent {

impl Ord for OrderedEvent {
fn cmp(&self, other: &Self) -> Ordering {
// equal IDs are always equal
if self.event_id() == other.event_id() {
return Ordering::Equal;
}

// if the timestamps are the same, use the id
if self.origin_server_ts() == other.origin_server_ts() {
return self.event_id().cmp(other.event_id());
}

// otherwise, us the timestamp
self.origin_server_ts().cmp(&other.origin_server_ts())
}
}

impl PartialOrd for OrderedEvent {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.origin_server_ts()
.partial_cmp(&other.origin_server_ts())
Some(self.cmp(other))
}
}

Expand Down Expand Up @@ -813,7 +811,7 @@ impl Widget for ChatWidget<'_> {
}

fn make_message_list(
timeline: &SortedVec<OrderedEvent>,
timeline: &BTreeSet<OrderedEvent>,
members: &Vec<RoomMember>,
receipts: &Receipts,
) -> Vec<Message> {
Expand Down
11 changes: 4 additions & 7 deletions src/widgets/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,11 @@ impl Receipts {
usernames.retain(|u| &u.id != user_id)
}

let username = Username::new(user_id.clone());

// add our receipt
if let Some(usernames) = self.events.get_mut(event_id) {
usernames.push(username);
} else {
self.events.insert(event_id.clone(), vec![username]);
}
self.events
.entry(event_id.clone())
.or_insert_with(|| Vec::with_capacity(1))
.push(Username::new(user_id.clone()));

// and clean up any now-empty vectors
self.events.retain(|_, value| !value.is_empty());
Expand Down

0 comments on commit 52c3fb6

Please sign in to comment.