Skip to content

Commit

Permalink
Handle receipts in replies more accurately.
Browse files Browse the repository at this point in the history
  • Loading branch information
pkulak committed May 8, 2023
1 parent a96cab3 commit c5a74c6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
39 changes: 22 additions & 17 deletions src/widgets/message.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use chrono::TimeZone;
use log::info;
use std::cell::Cell;
use std::collections::BinaryHeap;
use std::iter;
Expand Down Expand Up @@ -332,25 +331,26 @@ impl Message {

/// Given a binary heap (priority queue) of Receipts, run through the
/// the messages, popping off receipts and attaching them. This way we
/// only show a single receipt per user, on the latest message they have
/// read.
/// only show a single receipt per user (per reply chain), on the latest
/// message they have read.
pub fn apply_receipts(messages: &mut [Message], heap: &mut BinaryHeap<Receipt>) {
// hold on to a fresh copy of the heap to clone into each reply chain
let og_heap = heap.clone();

for message in messages.iter_mut().rev() {
Message::apply_receipts(&mut message.replies, heap);

loop {
if let Some(candidate) = heap.peek() {
if candidate.timestamp > &message.sent {
message
.receipts
.push(Username::new(candidate.user_id.clone()));

heap.pop();
} else {
break;
}
if !message.replies.is_empty() {
Message::apply_receipts(&mut message.replies, &mut og_heap.clone());
}

while let Some(candidate) = heap.peek() {
if candidate.timestamp > &message.sent {
message
.receipts
.push(Username::new(candidate.user_id.clone()));

heap.pop();
} else {
return;
break;
}
}
}
Expand Down Expand Up @@ -421,6 +421,11 @@ impl Message {
};

height += 2;

if !self.receipts.is_empty() {
height += 1;
}

height += self.reactions.len();
self.last_height.set(LastHeight { width, height });
height
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Receipts {
}
}

#[derive(Eq, PartialEq, Ord, PartialOrd, Debug)]
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone)]
pub struct Receipt<'a> {
pub timestamp: &'a MilliSecondsSinceUnixEpoch,
pub user_id: &'a OwnedUserId,
Expand Down

0 comments on commit c5a74c6

Please sign in to comment.