Use first-not-visible message instead of last-visible

Using last visible message ignores an edge case:

If we are on top of the list (i.e oldest message), any received message
should be appended (it must be visible).

When using last-visible message for this, a newer message will have <
clock-value, therefore it will be offloaded to the database, which is
not correct.

Instead if we use the first-not-visible message, this should not be a
problem. If we are on top of the list, this will be nil, and therefore
the message will be appended.

Signed-off-by: Andrey Shovkoplyas <motor4ik@gmail.com>
This commit is contained in:
Andrea Maria Piana 2020-03-09 09:43:10 +01:00 committed by Andrey Shovkoplyas
parent 9594bb49e2
commit c2e195979c
No known key found for this signature in database
GPG Key ID: EAAB7C8622D860A4
2 changed files with 14 additions and 9 deletions

View File

@ -116,7 +116,6 @@
(when (and current-chat?
(or (not cursor-clock-value)
(<= cursor-clock-value clock-value)))
;; Not in the current view, offload to db and update cursor if necessary
(if (or (not @view.state/viewable-item)
(not= current-chat-id
(:chat-id @view.state/viewable-item))
@ -124,10 +123,11 @@
clock-value))
(add-message cofx {:message message
:current-chat? current-chat?})
;; Not in the current view, offload to db and update cursor if necessary
(when (and (< clock-value
cursor-clock-value)
(= current-chat-id
(:chat-id (.-item view.state/viewable-item))))
(:chat-id @view.state/viewable-item)))
{:db (assoc-in db [:chats chat-id :cursor] (chat-loading/clock-value->cursor clock-value))})))))
(defn- add-to-chat?

View File

@ -305,13 +305,18 @@
(defonce messages-list-ref (atom nil))
(defn on-viewable-items-changed [e]
(reset! state/viewable-item
(let [element (->> (.-viewableItems e)
reverse
(filter (fn [e]
(= :message (:type (.-item e)))))
first)]
(when element (.-item element))))
(when @messages-list-ref
(reset! state/viewable-item
(when-let [last-visible-element (aget (.-viewableItems e) (dec (.-length (.-viewableItems e))))]
(let [index (.-index last-visible-element)
;; Get first not visible element, if it's a datemark/gap
;; we might unnecessarely add messages on receiving as
;; they do not have a clock value, but most of the times
;; it will be a message
first-not-visible (aget (.-data (.-props @messages-list-ref)) (inc index))]
(when (and first-not-visible
(= :message (:type first-not-visible)))
first-not-visible)))))
(debounce/debounce-and-dispatch [:chat.ui/message-visibility-changed e] 5000))
(defview messages-view