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

View File

@ -305,13 +305,18 @@
(defonce messages-list-ref (atom nil)) (defonce messages-list-ref (atom nil))
(defn on-viewable-items-changed [e] (defn on-viewable-items-changed [e]
(reset! state/viewable-item (when @messages-list-ref
(let [element (->> (.-viewableItems e) (reset! state/viewable-item
reverse (when-let [last-visible-element (aget (.-viewableItems e) (dec (.-length (.-viewableItems e))))]
(filter (fn [e] (let [index (.-index last-visible-element)
(= :message (:type (.-item e))))) ;; Get first not visible element, if it's a datemark/gap
first)] ;; we might unnecessarely add messages on receiving as
(when element (.-item element)))) ;; 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)) (debounce/debounce-and-dispatch [:chat.ui/message-visibility-changed e] 5000))
(defview messages-view (defview messages-view