Skip to content

Commit

Permalink
Merge: Fix retrieval of earlier events in a just-joined room
Browse files Browse the repository at this point in the history
Fixes #148.
  • Loading branch information
alphapapa committed Sep 11, 2024
2 parents 3552980 + 16d4878 commit 3f87a95
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ Ement.el doesn't support encrypted rooms natively, but it can be used transparen
+ Replies to edited messages are correctly sent to the original event (whereas previously they were sent to the edit, which caused reactions to not be shown). ([[https://github.com/alphapapa/ement.el/issues/230][#230]], [[https://github.com/alphapapa/ement.el/issues/277][#277]]. Thanks to [[https://github.com/phil-s][Phil Sainty]] for suggesting, and to [[https://github.com/dionisos2][dionisos]] for reporting.)
+ Set ~filter-buffer-substring-function~ in room buffers to prevent undesired text properties from being included in copied text. ([[https://github.com/alphapapa/ement.el/pull/278][#278]]. Thanks to [[https://github.com/phil-s][Phil Sainty]].)
+ Command ~ement-disconnect~ no longer shows an error message. ([[https://github.com/alphapapa/ement.el/issues/208][#208]].)
+ Retrieval of earlier events in a just-joined room. ([[https://github.com/alphapapa/ement.el/issues/148][#148]]. Thanks to [[https://github.com/MagicRB][Richard Brežák]] for reporting, and to [[https://github.com/phil-s][Phil Sainty]] for testing.)

** 0.15.1

Expand Down
35 changes: 25 additions & 10 deletions ement-room.el
Original file line number Diff line number Diff line change
Expand Up @@ -1884,10 +1884,12 @@ see."
;; We use a timeout of 30, because sometimes the server can take a while to
;; respond, especially if loading, e.g. hundreds or thousands of events.
(ement-api session endpoint :timeout 30
:params (list (list "from" prev-batch)
(list "dir" "b")
(list "limit" (number-to-string number))
(list "filter" (json-encode ement-room-messages-filter)))
:params (remq nil
(list (when prev-batch
(list "from" prev-batch))
(list "dir" "b")
(list "limit" (number-to-string number))
(list "filter" (json-encode ement-room-messages-filter))))
:then then
:else (lambda (plz-error)
(when buffer
Expand Down Expand Up @@ -2502,13 +2504,23 @@ before the earliest-seen message)."
(append (ement-room-state room) (append state nil))))
(ement-with-progress-reporter (:reporter ("Ement: Processing earlier events..." 0 progress-max-value))
;; Append timeline events (in the "chunk").
;; NOTE: It's regrettable that we have to turn the chunk vector into a list before
;; appending it to the timeline, but we have to discard events that we've already
;; seen.
;; TODO: Consider looping over the vector and pushing one-by-one instead of using
;; `seq-remove' and `append' (might be faster).
(cl-loop for event across-ref chunk
do (setf event (ement--make-event event))
;; HACK: Put events on events table. See FIXME above about using the event hook.
(ement--put-event event nil session)
do (if (gethash (alist-get 'event_id event) (ement-session-events session))
;; Duplicate event: set to nil to be ignored.
(setf event nil)
;; New event.
(setf event (ement--make-event event))
;; HACK: Put events on events table. See FIXME above about using the event hook.
(ement--put-event event nil session))
(ement-progress-update)
finally do (setf (ement-room-timeline room)
(append (ement-room-timeline room) (append chunk nil))))
finally do
(setf chunk (seq-remove #'null chunk)
(ement-room-timeline room) (append (ement-room-timeline room) chunk)))
(when buffer
;; Insert events into the room's buffer.
(with-current-buffer buffer
Expand All @@ -2518,7 +2530,10 @@ before the earliest-seen message)."
(select-window buffer-window))
;; FIXME: Use retro-loading in event handlers, or in --handle-events, anyway.
(ement-room--process-events chunk)
(when set-prev-batch
;; Don't set the slot if the response doesn't include an "end" token (that
;; would cause subsequent retro requests to fetch events from the end of the
;; timeline, as if we had just joined).
(when (and set-prev-batch end)
;; This feels a little hacky, but maybe not too bad.
(setf (ement-room-prev-batch room) end))
(setf ement-room-retro-loading nil)))))
Expand Down

0 comments on commit 3f87a95

Please sign in to comment.