Skip to content

Commit

Permalink
Merge: (ement--original-event-for)
Browse files Browse the repository at this point in the history
See #226, #227, #228.

Reported-by: Phil Sainty <phil@catalyst.net.nz>
  • Loading branch information
alphapapa committed Oct 3, 2023
2 parents 0fad92d + ec518a1 commit 84787ed
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ Ement.el doesn't support encrypted rooms natively, but it can be used transparen
+ Name for direct rooms in directory buffers.
+ Editing a message from the compose buffer would be sent as a reply to the edited message. (Fixes [[https://github.com/alphapapa/ement.el/issues/189][#189]]. Thanks to [[https://github.com/phil-s][Phil Sainty]] for reporting.)
+ Editing an already-edited message. ([[https://github.com/alphapapa/ement.el/issues/226][#226]]. Thanks to [[https://github.com/phil-s][Phil Sainty]] for reporting.)
+ Replying to an already-edited message. ([[https://github.com/alphapapa/ement.el/issues/227][#227]]. Thanks to [[https://github.com/phil-s][Phil Sainty]] for reporting.)
+ Command ~ement-room-flush-colors~ maintains point position.

** 0.12
Expand Down
16 changes: 16 additions & 0 deletions ement-lib.el
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,22 @@ m.replace metadata)."
(ement--event-replaces-p a b)
(ement--event-replaces-p b a)))

(defun ement--original-event-for (event session)
"Return the original of EVENT in SESSION.
If EVENT has metadata indicating that it replaces another event,
return the replaced event; otherwise return EVENT. If a replaced
event can't be found in SESSION's events table, return an ersatz
one that has the expected ID and same sender."
(pcase-let (((cl-struct ement-event sender
(content (map ('m.relates_to
(map ('event_id replaced-event-id)
('rel_type relation-type))))))
event))
(pcase relation-type
("m.replace" (or (gethash replaced-event-id (ement-session-events session))
(make-ement-event :id replaced-event-id :sender sender)))
(_ event))))

(defun ement--format-room (room &optional topic)
"Return ROOM formatted with name, alias, ID, and optionally TOPIC.
Suitable for use in completion, etc."
Expand Down
27 changes: 10 additions & 17 deletions ement-room.el
Original file line number Diff line number Diff line change
Expand Up @@ -1702,23 +1702,17 @@ mentioning the ROOM and CONTENT."

(defun ement-room-edit-message (event room session body)
"Edit EVENT in ROOM on SESSION to have new BODY.
The message must be one sent by the local user. EVENT may be the
`ement-event' or the event's ID string."
The message must be one sent by the local user. If EVENT is
itself an edit of another event, the original event is edited."
(interactive (ement-room-with-highlighted-event-at (point)
(cl-assert ement-session) (cl-assert ement-room)
(pcase-let* ((event (ewoc-data (ewoc-locate ement-ewoc)))
((cl-struct ement-session user) ement-session)
((cl-struct ement-event sender
(content (map body ('m.relates_to
(map ('event_id replaced-event-id)
('rel_type relation-type))))))
event)
(ement-room-editing-event event))
((cl-struct ement-event sender (content (map body))) event)
(ement-room-editing-event event)
(edited-event (ement--original-event-for event ement-session)))
(unless (equal (ement-user-id sender) (ement-user-id user))
(user-error "You may only edit your own messages"))
(pcase relation-type
("m.replace" ;; Editing an already-edited event: use the original event ID.
(setf event replaced-event-id)))
;; Remove any leading asterisk from the plain-text body.
(setf body (replace-regexp-in-string (rx bos "*" (1+ space)) "" body t t))
(ement-room-with-typing
Expand All @@ -1729,7 +1723,7 @@ The message must be one sent by the local user. EVENT may be the
(when (string-empty-p body)
(user-error "To delete a message, use command `ement-room-delete-message'"))
(when (yes-or-no-p (format "Edit message to: %S? " body))
(list event ement-room ement-session body)))))))
(list edited-event ement-room ement-session body)))))))
(let* ((endpoint (format "rooms/%s/send/%s/%s" (url-hexify-string (ement-room-id room))
"m.room.message" (ement--update-transaction-id session)))
(new-content (ement-alist "body" body
Expand All @@ -1741,9 +1735,7 @@ The message must be one sent by the local user. EVENT may be the
"m.new_content" new-content
"m.relates_to" (ement-alist
"rel_type" "m.replace"
"event_id" (cl-typecase event
(string event)
(ement-event (ement-event-id event)))))))
"event_id" (ement-event-id event)))))
;; Prepend the asterisk after the filter may have modified the content. Note that the
;; "m.new_content" body does not get the leading asterisk, only the "content" body,
;; which is intended as a fallback.
Expand Down Expand Up @@ -1778,8 +1770,9 @@ Interactively, to event at point."
(setq-local ement-room-replying-to-event event)))
(body (ement-room-with-typing
(ement-room-read-string prompt nil 'ement-room-message-history
nil 'inherit-input-method))))
(ement-room-send-message room session :body body :replying-to-event event)))))
nil 'inherit-input-method)))
(replying-to-event (ement--original-event-for event ement-session)))
(ement-room-send-message room session :body body :replying-to-event replying-to-event)))))

(defun ement-room-send-reaction (key position)
"Send reaction of KEY to event at POSITION.
Expand Down

0 comments on commit 84787ed

Please sign in to comment.