Skip to content

Commit

Permalink
Fix: Strip off fallback quotes from HTML bodies if rich reply event
Browse files Browse the repository at this point in the history
* ement-room.el (ement-room--format-message-body)
(ement-room--format-quotation-html): Strip off fallback quotes from
HTML body, and use the quoted event's body always.  Currently,
stripping off is not done for plain text bodies since it is fragile:
it is not a given that all clients will follow the spec for the
fallback quote in plain text bodies. So more harm than good will be
done by a poor attempt at stripping fallback quotes.
  • Loading branch information
Visuwesh committed Apr 18, 2024
1 parent d6a6bdf commit e1b02bc
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions ement-room.el
Original file line number Diff line number Diff line change
Expand Up @@ -3529,8 +3529,6 @@ If FORMATTED-P, return the formatted body content, when available."
(body (or new-body main-body))
(formatted-body (or new-formatted-body formatted-body))
(quote-in-body-p (and formatted-body
;; FIXME: What if the message has no formatted body
;; but has a plain-text quoted reply?
(string-match-p "<mx-reply>" formatted-body)))
(appendix (pcase msgtype
;; TODO: Face for m.notices.
Expand All @@ -3543,10 +3541,8 @@ If FORMATTED-P, return the formatted body content, when available."
nil
(format "[unsupported msgtype: %s]" msgtype)))))
(event-replied-to))
(when (and replied-to-event-id (not quote-in-body-p))
;; Message is a reply, but event being replied to is not quoted in the body and a
;; reference to it has not already been stored in this event: find or fetch the
;; event being replied to.
(when replied-to-event-id
;; Message is a reply, find or fetch the event being replied to.
(if-let ((replied-to-event (gethash replied-to-event-id (ement-session-events ement-session))))
;; Found event in session's events table: use it.
(setf event-replied-to replied-to-event)
Expand All @@ -3566,6 +3562,8 @@ If FORMATTED-P, return the formatted body content, when available."
(lambda (data) (eq data event)))))
(ewoc-invalidate ement-ewoc node))))))))))
(setf body (if (or (not formatted-p) (not formatted-body))
;; FIXME: This should check if the quote is the plain text body but
;; that is not easy...
(if (and event-replied-to (not quote-in-body-p))
(concat (ement-room--format-quotation-text event-replied-to)
"\n" body)
Expand All @@ -3575,9 +3573,8 @@ If FORMATTED-P, return the formatted body content, when available."
("org.matrix.custom.html"
(save-match-data
(ement-room--render-html
(if (and event-replied-to (not quote-in-body-p))
(concat (ement-room--format-quotation-html event-replied-to ement-room)
"\n" formatted-body)
(if event-replied-to
(ement-room--format-quotation-html event-replied-to formatted-body ement-room)
formatted-body))))
(_ (format "[unknown body format: %s] %s"
(or new-content-format content-format) body)))))
Expand Down Expand Up @@ -3616,23 +3613,39 @@ If FORMATTED-P, return the formatted body content, when available."
(insert "\n")
(buffer-string))))

(defun ement-room--format-quotation-html (quoted-event room)
"Return HTML for QUOTED-EVENT in ROOM."
(defun ement-room--format-quotation-html (quoted-event reply-body room)
"Include QUOTED-EVENT in formatted REPLY-BODY in ROOM."
(pcase-let* (((cl-struct ement-room (id room-id)) room)
((cl-struct ement-event content (id event-id) sender) quoted-event)
((cl-struct ement-user (id sender-id)) sender)
((map format body ('formatted_body formatted-body)) content))
(format "<mx-reply><blockquote>
<a href=\"https://matrix.to/#/%s/%s\">In reply to</a>
((map format ('body quoted-body) ('formatted_body formatted-quoted-body)) content)
(new-quote (format "<mx-reply><blockquote>
<a href=\"https://matrix.to/#/%s/%s\">In reply to TESTY TEST</a>
<a href=\"https://matrix.to/#/%s\">%s</a>
<br />
%s
</blockquote></mx-reply>"
room-id event-id sender-id
(ement--user-displayname-in room sender)
(pcase format
("org.matrix.custom.html" formatted-body)
(_ body)))))
room-id event-id sender-id
(ement--user-displayname-in room sender)
(pcase format
("org.matrix.custom.html" formatted-quoted-body)
(_ quoted-body)))))
(if (string-match-p "<mx-reply>" reply-body)
(let (new-quote-dom reply-body-dom)
(with-current-buffer (get-buffer-create " *ement-room--rich-reply*")
(erase-buffer)
(insert new-quote)
(setq new-quote-dom
(dom-by-tag (libxml-parse-html-region (point-min) (point-max)) 'blockquote))
(erase-buffer)
(insert reply-body)
(setq reply-body-dom (libxml-parse-html-region (point-min) (point-max)))
(setcar (cddar (dom-by-tag reply-body-dom 'mx-reply))
(car new-quote-dom))
(erase-buffer)
(dom-print (dom-by-tag reply-body-dom 'body))
(buffer-string)))
(concat new-quote "\n" reply-body))))

(defun ement-room--render-html (string)
"Return rendered version of HTML STRING.
Expand Down

0 comments on commit e1b02bc

Please sign in to comment.