Commit 56ec90523c

56ec90523ce83a7c8448400f66c7a5649d0cc02b

parent: d13d4b9f7f

Unregistered key

Ulyssa <git@ulyssa.dev> · 2023-01-14 01:53 UTC

Support redacting messages (#5)

Layout: unified · split

README.md +1 −1
@@ -69,7 +69,7 @@ two other TUI clients and Element Web:
69| Send formatted messages (markdown) | :x: ([#10]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | 69| Send formatted messages (markdown) | :x: ([#10]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
70| Rich Text Editor for formatted messages | :x: | :x: | :x: | :heavy_check_mark: | 70| Rich Text Editor for formatted messages | :x: | :x: | :x: | :heavy_check_mark: |
71| Display formatted messages | :x: ([#10]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | 71| Display formatted messages | :x: ([#10]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
72| Redacting | :x: ([#5]) | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | 72| Redacting | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: |
73| Multiple Matrix Accounts | :heavy_check_mark: | :x: | :heavy_check_mark: | :x: | 73| Multiple Matrix Accounts | :heavy_check_mark: | :x: | :heavy_check_mark: | :x: |
74| New user registration | :x: | :x: | :x: | :heavy_check_mark: | 74| New user registration | :x: | :x: | :x: | :heavy_check_mark: |
75| VOIP | :x: | :x: | :x: | :heavy_check_mark: | 75| VOIP | :x: | :x: | :x: | :heavy_check_mark: |
src/base.rs +1
@@ -63,6 +63,7 @@ pub enum VerifyAction {
63pub enum MessageAction { 63pub enum MessageAction {
64 Cancel, 64 Cancel,
65 Download(Option<String>, bool), 65 Download(Option<String>, bool),
66 Redact(Option<String>),
66 Reply, 67 Reply,
67} 68}
68 69
src/commands.rs +35
@@ -144,6 +144,19 @@ fn iamb_cancel(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
144 return Ok(step); 144 return Ok(step);
145} 145}
146 146
147fn iamb_redact(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
148 let args = desc.arg.strings()?;
149
150 if args.len() > 1 {
151 return Result::Err(CommandError::InvalidArgument);
152 }
153
154 let ract = IambAction::from(MessageAction::Redact(args.into_iter().next()));
155 let step = CommandStep::Continue(ract.into(), ctx.context.take());
156
157 return Ok(step);
158}
159
147fn iamb_reply(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { 160fn iamb_reply(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
148 if !desc.arg.text.is_empty() { 161 if !desc.arg.text.is_empty() {
149 return Result::Err(CommandError::InvalidArgument); 162 return Result::Err(CommandError::InvalidArgument);
@@ -259,6 +272,7 @@ fn add_iamb_commands(cmds: &mut ProgramCommands) {
259 cmds.add_command(ProgramCommand { names: vec!["invite".into()], f: iamb_invite }); 272 cmds.add_command(ProgramCommand { names: vec!["invite".into()], f: iamb_invite });
260 cmds.add_command(ProgramCommand { names: vec!["join".into()], f: iamb_join }); 273 cmds.add_command(ProgramCommand { names: vec!["join".into()], f: iamb_join });
261 cmds.add_command(ProgramCommand { names: vec!["members".into()], f: iamb_members }); 274 cmds.add_command(ProgramCommand { names: vec!["members".into()], f: iamb_members });
275 cmds.add_command(ProgramCommand { names: vec!["redact".into()], f: iamb_redact });
262 cmds.add_command(ProgramCommand { names: vec!["reply".into()], f: iamb_reply }); 276 cmds.add_command(ProgramCommand { names: vec!["reply".into()], f: iamb_reply });
263 cmds.add_command(ProgramCommand { names: vec!["rooms".into()], f: iamb_rooms }); 277 cmds.add_command(ProgramCommand { names: vec!["rooms".into()], f: iamb_rooms });
264 cmds.add_command(ProgramCommand { names: vec!["set".into()], f: iamb_set }); 278 cmds.add_command(ProgramCommand { names: vec!["set".into()], f: iamb_set });
@@ -429,4 +443,25 @@ mod tests {
429 let res = cmds.input_cmd("invite @user:example.com", ctx.clone()); 443 let res = cmds.input_cmd("invite @user:example.com", ctx.clone());
430 assert_eq!(res, Err(CommandError::InvalidArgument)); 444 assert_eq!(res, Err(CommandError::InvalidArgument));
431 } 445 }
446
447 #[test]
448 fn test_cmd_redact() {
449 let mut cmds = setup_commands();
450 let ctx = ProgramContext::default();
451
452 let res = cmds.input_cmd("redact", ctx.clone()).unwrap();
453 let act = IambAction::Message(MessageAction::Redact(None));
454 assert_eq!(res, vec![(act.into(), ctx.clone())]);
455
456 let res = cmds.input_cmd("redact Removed", ctx.clone()).unwrap();
457 let act = IambAction::Message(MessageAction::Redact(Some("Removed".into())));
458 assert_eq!(res, vec![(act.into(), ctx.clone())]);
459
460 let res = cmds.input_cmd("redact \"Removed\"", ctx.clone()).unwrap();
461 let act = IambAction::Message(MessageAction::Redact(Some("Removed".into())));
462 assert_eq!(res, vec![(act.into(), ctx.clone())]);
463
464 let res = cmds.input_cmd("redact Removed Removed", ctx.clone());
465 assert_eq!(res, Err(CommandError::InvalidArgument));
466 }
432} 467}
src/message.rs +38 −7
@@ -11,16 +11,23 @@ use unicode_segmentation::UnicodeSegmentation;
11use unicode_width::UnicodeWidthStr; 11use unicode_width::UnicodeWidthStr;
12 12
13use matrix_sdk::ruma::{ 13use matrix_sdk::ruma::{
14 events::room::message::{ 14 events::{
15 MessageType, 15 room::{
16 OriginalRoomMessageEvent, 16 message::{
17 RedactedRoomMessageEvent, 17 MessageType,
18 RoomMessageEvent, 18 OriginalRoomMessageEvent,
19 RoomMessageEventContent, 19 RedactedRoomMessageEvent,
20 RoomMessageEvent,
21 RoomMessageEventContent,
22 },
23 redaction::SyncRoomRedactionEvent,
24 },
25 Redact,
20 }, 26 },
21 MilliSecondsSinceUnixEpoch, 27 MilliSecondsSinceUnixEpoch,
22 OwnedEventId, 28 OwnedEventId,
23 OwnedUserId, 29 OwnedUserId,
30 RoomVersionId,
24 UInt, 31 UInt,
25}; 32};
26 33
@@ -323,10 +330,34 @@ impl MessageEvent {
323 pub fn show(&self) -> Cow<'_, str> { 330 pub fn show(&self) -> Cow<'_, str> {
324 match self { 331 match self {
325 MessageEvent::Original(ev) => show_room_content(&ev.content), 332 MessageEvent::Original(ev) => show_room_content(&ev.content),
326 MessageEvent::Redacted(_) => Cow::Borrowed("[redacted]"), 333 MessageEvent::Redacted(ev) => {
334 let reason = ev
335 .unsigned
336 .redacted_because
337 .as_ref()
338 .and_then(|e| e.as_original())
339 .and_then(|r| r.content.reason.as_ref());
340
341 if let Some(r) = reason {
342 Cow::Owned(format!("[Redacted: {:?}]", r))
343 } else {
344 Cow::Borrowed("[Redacted]")
345 }
346 },
327 MessageEvent::Local(content) => show_room_content(content), 347 MessageEvent::Local(content) => show_room_content(content),
328 } 348 }
329 } 349 }
350
351 pub fn redact(&mut self, redaction: SyncRoomRedactionEvent, version: &RoomVersionId) {
352 match self {
353 MessageEvent::Redacted(_) => return,
354 MessageEvent::Local(_) => return,
355 MessageEvent::Original(ev) => {
356 let redacted = ev.clone().redact(redaction, version);
357 *self = MessageEvent::Redacted(Box::new(redacted));
358 },
359 }
360 }
330} 361}
331 362
332fn show_room_content(content: &RoomMessageEventContent) -> Cow<'_, str> { 363fn show_room_content(content: &RoomMessageEventContent) -> Cow<'_, str> {
src/windows/room/chat.rs +27
@@ -224,6 +224,33 @@ impl ChatState {
224 224
225 Err(IambError::NoAttachment.into()) 225 Err(IambError::NoAttachment.into())
226 }, 226 },
227 MessageAction::Redact(reason) => {
228 let room = store
229 .application
230 .worker
231 .client
232 .get_joined_room(self.id())
233 .ok_or(IambError::NotJoined)?;
234
235 let event_id = match &msg.event {
236 MessageEvent::Original(ev) => ev.event_id.clone(),
237 MessageEvent::Local(_) => {
238 self.scrollback.get_key(info).ok_or(IambError::NoSelectedMessage)?.1
239 },
240 MessageEvent::Redacted(_) => {
241 let msg = "";
242 let err = UIError::Failure(msg.into());
243
244 return Err(err);
245 },
246 };
247
248 let event_id = event_id.as_ref();
249 let reason = reason.as_deref();
250 let _ = room.redact(event_id, reason, None).await.map_err(IambError::from)?;
251
252 Ok(None)
253 },
227 MessageAction::Reply => { 254 MessageAction::Reply => {
228 self.reply_to = self.scrollback.get_key(info); 255 self.reply_to = self.scrollback.get_key(info);
229 self.focus = RoomFocus::MessageBar; 256 self.focus = RoomFocus::MessageBar;
src/worker.rs +30
@@ -35,6 +35,7 @@ use matrix_sdk::{
35 room::{ 35 room::{
36 message::{MessageType, RoomMessageEventContent}, 36 message::{MessageType, RoomMessageEventContent},
37 name::RoomNameEventContent, 37 name::RoomNameEventContent,
38 redaction::{OriginalSyncRoomRedactionEvent, SyncRoomRedactionEvent},
38 topic::RoomTopicEventContent, 39 topic::RoomTopicEventContent,
39 }, 40 },
40 typing::SyncTypingEvent, 41 typing::SyncTypingEvent,
@@ -46,6 +47,7 @@ use matrix_sdk::{
46 OwnedRoomId, 47 OwnedRoomId,
47 OwnedRoomOrAliasId, 48 OwnedRoomOrAliasId,
48 OwnedUserId, 49 OwnedUserId,
50 RoomVersionId,
49 }, 51 },
50 Client, 52 Client,
51 DisplayName, 53 DisplayName,
@@ -547,6 +549,34 @@ impl ClientWorker {
547 }, 549 },
548 ); 550 );
549 551
552 let _ = self.client.add_event_handler(
553 |ev: OriginalSyncRoomRedactionEvent,
554 room: MatrixRoom,
555 store: Ctx<AsyncProgramStore>| {
556 async move {
557 let room_id = room.room_id();
558 let room_info = room.clone_info();
559 let room_version = room_info.room_version().unwrap_or(&RoomVersionId::V1);
560
561 let mut locked = store.lock().await;
562 let info = locked.application.get_room_info(room_id.to_owned());
563
564 // XXX: need to store a mapping of EventId -> MessageKey somewhere
565 // to avoid having to iterate over the messages here.
566 for ((_, id), msg) in info.messages.iter_mut().rev() {
567 if id != &ev.redacts {
568 continue;
569 }
570
571 let ev = SyncRoomRedactionEvent::Original(ev);
572 msg.event.redact(ev, room_version);
573
574 break;
575 }
576 }
577 },
578 );
579
550 let _ = self.client.add_event_handler( 580 let _ = self.client.add_event_handler(
551 |ev: OriginalSyncKeyVerificationStartEvent, 581 |ev: OriginalSyncKeyVerificationStartEvent,
552 client: Client, 582 client: Client,