| 1 | use serde_json::Value; |
| 2 | use session::Request; |
| 3 | |
| 4 | use crate::error::Result; |
| 5 | use crate::messages::{MessageTransport, Messages, ensure_success}; |
| 6 | use crate::window::WindowDirection; |
| 7 | |
| 8 | const ORGID_PREFIX: &str = "8:orgid:"; |
| 9 | const ANNOTATION_KEYS: [&str; 3] = ["follow", "star", "acks"]; |
| 10 | |
| 11 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 12 | pub struct EmotionReaction { |
| 13 | pub key: String, |
| 14 | pub user_id: Option<String>, |
| 15 | pub time_ms: Option<i64>, |
| 16 | } |
| 17 | |
| 18 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 19 | pub struct MessageEmotions { |
| 20 | pub message_id: String, |
| 21 | pub reactions: Vec<EmotionReaction>, |
| 22 | } |
| 23 | |
| 24 | pub fn user_id_from_mri(mri: &str) -> &str { |
| 25 | mri.strip_prefix(ORGID_PREFIX).unwrap_or(mri) |
| 26 | } |
| 27 | |
| 28 | fn reactions(emotions: Option<&Value>) -> Vec<EmotionReaction> { |
| 29 | emotions |
| 30 | .and_then(Value::as_array) |
| 31 | .into_iter() |
| 32 | .flatten() |
| 33 | .flat_map(|entry| { |
| 34 | let key = entry |
| 35 | .get("key") |
| 36 | .and_then(Value::as_str) |
| 37 | .filter(|key| !ANNOTATION_KEYS.contains(key)) |
| 38 | .unwrap_or_default(); |
| 39 | entry |
| 40 | .get("users") |
| 41 | .and_then(Value::as_array) |
| 42 | .into_iter() |
| 43 | .flatten() |
| 44 | .filter(move |_| !key.is_empty()) |
| 45 | .map(move |user| EmotionReaction { |
| 46 | key: key.to_owned(), |
| 47 | user_id: user |
| 48 | .get("mri") |
| 49 | .and_then(Value::as_str) |
| 50 | .map(|mri| user_id_from_mri(mri).to_owned()), |
| 51 | time_ms: user.get("time").and_then(Value::as_i64), |
| 52 | }) |
| 53 | }) |
| 54 | .collect() |
| 55 | } |
| 56 | |
| 57 | /// Every listed message, including those without an `emotions` property: the chat service |
| 58 | /// leaves it out when nobody reacted, and an entry whose `users` is empty means removed. |
| 59 | pub fn parse_message_emotions(body: &Value) -> Vec<MessageEmotions> { |
| 60 | body.get("messages") |
| 61 | .and_then(Value::as_array) |
| 62 | .into_iter() |
| 63 | .flatten() |
| 64 | .filter_map(|message| { |
| 65 | let message_id = crate::json_id::id_text(message.get("id")?)?; |
| 66 | Some(MessageEmotions { |
| 67 | message_id, |
| 68 | reactions: reactions(message.pointer("/properties/emotions")), |
| 69 | }) |
| 70 | }) |
| 71 | .collect() |
| 72 | } |
| 73 | |
| 74 | impl<T: MessageTransport> Messages<T> { |
| 75 | pub async fn message_emotions( |
| 76 | &self, |
| 77 | conversation_id: &str, |
| 78 | newest_message_id: &str, |
| 79 | page_size: usize, |
| 80 | ) -> Result<Vec<MessageEmotions>> { |
| 81 | let url = self.message_window_url( |
| 82 | conversation_id, |
| 83 | newest_message_id, |
| 84 | WindowDirection::Backward, |
| 85 | page_size, |
| 86 | ); |
| 87 | let answer = self.transport.send(Request::get(url)).await?; |
| 88 | ensure_success(&answer)?; |
| 89 | Ok(parse_message_emotions(&answer.body)) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[cfg(test)] |
| 94 | mod tests { |
| 95 | use serde_json::json; |
| 96 | |
| 97 | use super::*; |
| 98 | |
| 99 | #[test] |
| 100 | fn emotions_expand_per_user_and_skip_removed_entries() { |
| 101 | let body = json!({"messages": [ |
| 102 | {"id": "1", "properties": {"emotions": [ |
| 103 | {"key": "yes-tone2", "users": [ |
| 104 | {"mri": "8:orgid:aaa", "time": 1792135855841_i64, "value": "1792135855835"}, |
| 105 | {"mri": "8:orgid:bbb", "time": 1792135855900_i64, "value": "1"} |
| 106 | ]}, |
| 107 | {"key": "heart", "users": []} |
| 108 | ]}}, |
| 109 | {"id": "2", "properties": {}} |
| 110 | ]}); |
| 111 | let parsed = parse_message_emotions(&body); |
| 112 | assert_eq!(parsed.len(), 2); |
| 113 | assert_eq!(parsed[0].reactions.len(), 2); |
| 114 | assert_eq!(parsed[0].reactions[0].key, "yes-tone2"); |
| 115 | assert_eq!(parsed[0].reactions[0].user_id.as_deref(), Some("aaa")); |
| 116 | assert_eq!(parsed[0].reactions[1].time_ms, Some(1792135855900)); |
| 117 | assert!(parsed[1].reactions.is_empty()); |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn follow_star_and_acks_annotations_are_not_reactions() { |
| 122 | let body = json!({"messages": [ |
| 123 | {"id": "1", "properties": {"emotions": [ |
| 124 | {"key": "follow", "users": [{"mri": "8:orgid:aaa", "time": 1_i64, "value": "1"}]}, |
| 125 | {"key": "star", "users": [{"mri": "8:orgid:aaa", "time": 2_i64, "value": "1"}]}, |
| 126 | {"key": "acks", "users": [{"mri": "8:orgid:bbb", "time": 3_i64, "value": "1"}]}, |
| 127 | {"key": "like", "users": [{"mri": "8:orgid:bbb", "time": 4_i64, "value": "1"}]} |
| 128 | ]}} |
| 129 | ]}); |
| 130 | let parsed = parse_message_emotions(&body); |
| 131 | let keys: Vec<&str> = parsed[0] |
| 132 | .reactions |
| 133 | .iter() |
| 134 | .map(|reaction| reaction.key.as_str()) |
| 135 | .collect(); |
| 136 | assert_eq!(keys, ["like"]); |
| 137 | } |
| 138 | } |