Skip to content

Commit

Permalink
Handle reactions and show them as suffix of messages. (cont.)
Browse files Browse the repository at this point in the history
This is a follow-up on #53.

* Fix missing handling of reactions from other users.
* Add notifications for reactions from other users.
  • Loading branch information
boxdot committed May 8, 2021
1 parent d81d2a7 commit 16074bd
Showing 1 changed file with 60 additions and 7 deletions.
67 changes: 60 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use serde::{Deserialize, Serialize};
use unicode_width::UnicodeWidthStr;
use uuid::Uuid;

use std::borrow::Cow;
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use std::fs::File;
Expand Down Expand Up @@ -283,11 +284,11 @@ impl App {
}

pub fn name_by_id(&self, id: Uuid) -> &str {
self.get_name_by_id(id).unwrap_or("Unknown Name")
name_by_id(&self.data.names, id)
}

pub fn get_name_by_id(&self, id: Uuid) -> Option<&str> {
self.data.names.get(&id).map(|s| s.as_ref())
get_name_by_id(&self.data.names, id)
}

pub fn put_char(&mut self, c: char) {
Expand Down Expand Up @@ -721,6 +722,29 @@ impl App {
}),
..
}),
)
| (
Metadata {
sender:
ServiceAddress {
uuid: Some(sender_uuid),
..
},
..
},
ContentBody::DataMessage(DataMessage {
body: None,
group_v2,
reaction:
Some(Reaction {
emoji: Some(emoji),
remove,
target_sent_timestamp: Some(target_sent_timestamp),
target_author_uuid: destination_uuid,
..
}),
..
}),
) => {
let channel_id = if let Some(GroupContextV2 {
master_key: Some(master_key),
Expand Down Expand Up @@ -775,18 +799,39 @@ impl App {
.reactions
.iter()
.position(|(from_id, _)| from_id == &sender_uuid);
if let Some(idx) = reaction_idx {
let is_added = if let Some(idx) = reaction_idx {
if remove {
message.reactions.swap_remove(idx);
self.save().unwrap();
false
} else {
message.reactions[idx].1 = emoji;
self.touch_channel(channel_idx);
message.reactions[idx].1 = emoji.clone();
true
}
} else {
message.reactions.push((sender_uuid, emoji));
message.reactions.push((sender_uuid, emoji.clone()));
true
};

if is_added {
if sender_uuid != self.signal_manager.uuid() {
let sender_name = name_by_id(&self.data.names, sender_uuid);
let summary = if let ChannelId::Group(_) = channel.id {
Cow::from(format!("{} in {}", sender_name, channel.name))
} else {
Cow::from(sender_name)
};
let mut notification = format!("{} reacted {}", summary, emoji);
if let Some(text) = message.message.as_ref() {
notification.push_str(" to: ");
notification.push_str(text);
}
self.notify(&summary, &notification);
}
self.touch_channel(channel_idx);
} else {
self.save().unwrap();
}

Some(())
}

Expand Down Expand Up @@ -985,3 +1030,11 @@ impl App {
}
}
}

pub fn get_name_by_id(names: &HashMap<Uuid, String>, id: Uuid) -> Option<&str> {
names.get(&id).map(|s| s.as_ref())
}

pub fn name_by_id(names: &HashMap<Uuid, String>, id: Uuid) -> &str {
get_name_by_id(names, id).unwrap_or("Unknown Name")
}

0 comments on commit 16074bd

Please sign in to comment.