Commit d44961c461

d44961c46164cd4d04d35f494a3ed561c39d33b3

parent: 6d80b516f8

Unregistered key

Ulyssa <git@ulyssa.dev> · 2024-08-13 06:21 UTC
committer: <noreply@github.com>

Support reacting literally with non-Emojis (#320)

Layout: unified · split

src/base.rs +10 −2
@@ -152,7 +152,11 @@ pub enum MessageAction {
152 Edit, 152 Edit,
153 153
154 /// React to a message with an Emoji. 154 /// React to a message with an Emoji.
155 React(String), 155 ///
156 /// `:react` will by default try to convert the [String] argument to an Emoji, and error when
157 /// it doesn't recognize it. The second [bool] argument forces it to be interpreted literally
158 /// when it is `true`.
159 React(String, bool),
156 160
157 /// Redact a message, with an optional reason. 161 /// Redact a message, with an optional reason.
158 /// 162 ///
@@ -166,7 +170,11 @@ pub enum MessageAction {
166 /// 170 ///
167 /// If no specific Emoji to remove to is specified, then all reactions from the user on the 171 /// If no specific Emoji to remove to is specified, then all reactions from the user on the
168 /// message are removed. 172 /// message are removed.
169 Unreact(Option<String>), 173 ///
174 /// Like `:react`, `:unreact` will by default try to convert the [String] argument to an Emoji,
175 /// and error when it doesn't recognize it. The second [bool] argument forces it to be
176 /// interpreted literally when it is `true`.
177 Unreact(Option<String>, bool),
170} 178}
171 179
172/// The type of room being created. 180/// The type of room being created.
src/commands.rs +7 −26
@@ -221,24 +221,17 @@ fn iamb_edit(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
221} 221}
222 222
223fn iamb_react(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { 223fn iamb_react(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
224 let args = desc.arg.strings()?; 224 let mut args = desc.arg.strings()?;
225 225
226 if args.len() != 1 { 226 if args.len() != 1 {
227 return Result::Err(CommandError::InvalidArgument); 227 return Result::Err(CommandError::InvalidArgument);
228 } 228 }
229 229
230 let k = args[0].as_str(); 230 let react = args.remove(0);
231 231 let mact = IambAction::from(MessageAction::React(react, desc.bang));
232 if let Some(emoji) = emojis::get(k).or_else(|| emojis::get_by_shortcode(k)) { 232 let step = CommandStep::Continue(mact.into(), ctx.context.clone());
233 let mact = IambAction::from(MessageAction::React(emoji.to_string()));
234 let step = CommandStep::Continue(mact.into(), ctx.context.clone());
235
236 return Ok(step);
237 } else {
238 let msg = format!("Invalid Emoji or shortcode: {k}");
239 233
240 return Result::Err(CommandError::Error(msg)); 234 return Ok(step);
241 }
242} 235}
243 236
244fn iamb_unreact(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { 237fn iamb_unreact(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
@@ -248,20 +241,8 @@ fn iamb_unreact(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
248 return Result::Err(CommandError::InvalidArgument); 241 return Result::Err(CommandError::InvalidArgument);
249 } 242 }
250 243
251 let mact = if let Some(k) = args.pop() { 244 let reaction = args.pop();
252 let k = k.as_str(); 245 let mact = IambAction::from(MessageAction::Unreact(reaction, desc.bang));
253
254 if let Some(emoji) = emojis::get(k).or_else(|| emojis::get_by_shortcode(k)) {
255 IambAction::from(MessageAction::Unreact(Some(emoji.to_string())))
256 } else {
257 let msg = format!("Invalid Emoji or shortcode: {k}");
258
259 return Result::Err(CommandError::Error(msg));
260 }
261 } else {
262 IambAction::from(MessageAction::Unreact(None))
263 };
264
265 let step = CommandStep::Continue(mact.into(), ctx.context.clone()); 246 let step = CommandStep::Continue(mact.into(), ctx.context.clone());
266 247
267 return Ok(step); 248 return Ok(step);
src/windows/room/chat.rs +37 −2
@@ -358,7 +358,22 @@ impl ChatState {
358 358
359 Ok(None) 359 Ok(None)
360 }, 360 },
361 MessageAction::React(emoji) => { 361 MessageAction::React(reaction, literal) => {
362 let emoji = if literal {
363 reaction
364 } else if let Some(emoji) =
365 emojis::get(&reaction).or_else(|| emojis::get_by_shortcode(&reaction))
366 {
367 emoji.to_string()
368 } else {
369 let msg = format!("{reaction:?} is not a known Emoji shortcode; do you want to react with exactly {reaction:?}?");
370 let act = IambAction::Message(MessageAction::React(reaction, true));
371 let prompt = PromptYesNo::new(msg, vec![Action::from(act)]);
372 let prompt = Box::new(prompt);
373
374 return Err(UIError::NeedConfirm(prompt));
375 };
376
362 let room = self.get_joined(&store.application.worker)?; 377 let room = self.get_joined(&store.application.worker)?;
363 let event_id = match &msg.event { 378 let event_id = match &msg.event {
364 MessageEvent::EncryptedOriginal(ev) => ev.event_id.clone(), 379 MessageEvent::EncryptedOriginal(ev) => ev.event_id.clone(),
@@ -422,7 +437,27 @@ impl ChatState {
422 437
423 Ok(None) 438 Ok(None)
424 }, 439 },
425 MessageAction::Unreact(emoji) => { 440 MessageAction::Unreact(reaction, literal) => {
441 let emoji = match reaction {
442 reaction if literal => reaction,
443 Some(reaction) => {
444 if let Some(emoji) =
445 emojis::get(&reaction).or_else(|| emojis::get_by_shortcode(&reaction))
446 {
447 Some(emoji.to_string())
448 } else {
449 let msg = format!("{reaction:?} is not a known Emoji shortcode; do you want to remove exactly {reaction:?}?");
450 let act =
451 IambAction::Message(MessageAction::Unreact(Some(reaction), true));
452 let prompt = PromptYesNo::new(msg, vec![Action::from(act)]);
453 let prompt = Box::new(prompt);
454
455 return Err(UIError::NeedConfirm(prompt));
456 }
457 },
458 None => None,
459 };
460
426 let room = self.get_joined(&store.application.worker)?; 461 let room = self.get_joined(&store.application.worker)?;
427 let event_id = match &msg.event { 462 let event_id = match &msg.event {
428 MessageEvent::EncryptedOriginal(ev) => ev.event_id.clone(), 463 MessageEvent::EncryptedOriginal(ev) => ev.event_id.clone(),