alphapapa / ement.el

A Matrix client for GNU Emacs
GNU General Public License v3.0
488 stars 44 forks source link

Optionally remove redacted event content from room buffers #300

Open phil-s opened 1 week ago

phil-s commented 1 week ago

Copying comments from chat

P: After deleting a spam message, what would be a way to remove it (or reduce it to the [redacted] state) from the Ement room buffer? Those telegram spam messages are so big and attention-grabbing, and I'd love to have them just gone from the buffers once they've been dealt with (without having to disconnect and reconnect).

A: Yeah, good point. Well the simplest answer is to C-x C-q and then manually remove it from the buffer. :) But feel free to post an issue or a patch on the repo to discuss more permanent solutions. I decided to just mark redacted messages as strikethrough during a session so that you can still see redacted messages (for the cases where you'd be curious what was redacted; even a room admin might want to know if a user was trying to hide his nasty content to avoid a ban), but obviously in case of spam or bad content, one might not even want to see what's already been redacted. We should probably make an option, e.g. maybe set it to remove content of redacted messages unless you are a mod/admin in the room, something like that.

P: I think the default behaviour is fine and useful generally, but maybe C-u C-k could be a combo to both redact and hide the message -- or just have a separate command to do the hiding.

phil-s commented 1 week ago

Comparing a ~[redacted]~ event with one where I can actually see the redacted content, it looked like the notable difference was that the content of the event was simply nil.

The following then works for emptying the content of the redacted event at point and re-displaying it as simply ~[redacted]~:

(defun my-ement-room-hide-message-content ()
  "Hide the content of the message at point."
  (interactive)
  (when-let* ((node (ewoc-locate ement-ewoc (point)))
              (event (ewoc-data node)))
    (setf (ement-event-content event) nil)
    (ement-room--replace-event event)
    (message "Removed content of event.")))

If I try it on a non-redacted message I see: [message has no body content] [unsupported msgtype: nil] (which is fine, and also nice for differentiating from redacted messages in case of accidental use).

phil-s commented 1 week ago

It also 'works' in the Notifications buffer, albeit with an error "Wrong type argument: ement-room, nil".

phil-s commented 1 week ago

In case anyone else wants this interim solution:


(defun my-ement-room-hide-message-content (&optional event)
  "Hide the content of the message at point."
  (interactive)
  (unless event
    (when-let ((node (ewoc-locate ement-ewoc (point))))
      (setq event (ewoc-data node))))
  (when event
    (setf (ement-event-content event) nil)
    (ement-room--replace-event event)
    (message "Removed content of event.")))

(define-advice ement-room-delete-message
    (:after (event _room _session &optional _reason) my-hide-content)
  "Hide the content of the message after deleting if a prefix arg was given.

This is :after advice for `ement-room-delete-message'.  Remove with:

\(advice-remove \\='ement-room-delete-message
               \\='ement-room-delete-message@my-hide-content)"
  (when current-prefix-arg
    (my-ement-room-hide-message-content event)))