Skip to content

Commit aba72aa

Browse files
authored
Prevent sending duplicate reaction events (#240)
1 parent 72d3543 commit aba72aa

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/base.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,22 @@ impl RoomInfo {
11491149

11501150
return top;
11511151
}
1152+
1153+
/// Checks if a given user has reacted with the given emoji on the given event
1154+
pub fn user_reactions_contains(
1155+
&mut self,
1156+
user_id: &UserId,
1157+
event_id: &EventId,
1158+
emoji: &str,
1159+
) -> bool {
1160+
if let Some(reactions) = self.reactions.get(event_id) {
1161+
reactions
1162+
.values()
1163+
.any(|(annotation, user)| annotation == emoji && user == user_id)
1164+
} else {
1165+
false
1166+
}
1167+
}
11521168
}
11531169

11541170
/// Generate a [CompletionMap] for Emoji shortcodes.

src/windows/room/chat.rs

+7
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,13 @@ impl ChatState {
372372
},
373373
};
374374

375+
if info.user_reactions_contains(&settings.profile.user_id, &event_id, &emoji) {
376+
let msg = format!("You’ve already reacted to this message with {}", emoji);
377+
let err = UIError::Failure(msg);
378+
379+
return Err(err);
380+
}
381+
375382
let reaction = Annotation::new(event_id, emoji);
376383
let msg = ReactionEventContent::new(reaction);
377384
let _ = room.send(msg).await.map_err(IambError::from)?;

0 commit comments

Comments
 (0)