Correctly load messages when on tab
While on tabs we want a slightly different behavior: Unread counter should increase, message should be loaded in the chat On moving to the chat tab from one of these tabs, it should mark the messages as seen. Signed-off-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
This commit is contained in:
parent
2f412d95ee
commit
947b1c2b2e
|
@ -41,13 +41,16 @@
|
||||||
(fx/defn offload-all-messages
|
(fx/defn offload-all-messages
|
||||||
[{:keys [db] :as cofx}]
|
[{:keys [db] :as cofx}]
|
||||||
(when-let [current-chat-id (:current-chat-id db)]
|
(when-let [current-chat-id (:current-chat-id db)]
|
||||||
{:db (update-in db [:chats current-chat-id]
|
{:db
|
||||||
|
(-> db
|
||||||
|
(dissoc :loaded-chat-id)
|
||||||
|
(update-in [:chats current-chat-id]
|
||||||
assoc
|
assoc
|
||||||
:all-loaded? false
|
:all-loaded? false
|
||||||
:cursor nil
|
:cursor nil
|
||||||
:messages-initialized? false
|
:messages-initialized? false
|
||||||
:messages {}
|
:messages {}
|
||||||
:message-list nil)}))
|
:message-list nil))}))
|
||||||
|
|
||||||
(fx/defn handle-chat-visibility-changed
|
(fx/defn handle-chat-visibility-changed
|
||||||
{:events [:chat.ui/message-visibility-changed]}
|
{:events [:chat.ui/message-visibility-changed]}
|
||||||
|
@ -155,10 +158,18 @@
|
||||||
(fx/defn load-messages
|
(fx/defn load-messages
|
||||||
[{:keys [db now] :as cofx}]
|
[{:keys [db now] :as cofx}]
|
||||||
(when-let [current-chat-id (:current-chat-id db)]
|
(when-let [current-chat-id (:current-chat-id db)]
|
||||||
(when-not (get-in db [:chats current-chat-id :messages-initialized?])
|
(if-not (get-in db [:chats current-chat-id :messages-initialized?])
|
||||||
|
(do
|
||||||
; reset chat viewable-items state
|
; reset chat viewable-items state
|
||||||
(chat.state/reset)
|
(chat.state/reset)
|
||||||
(fx/merge cofx
|
(fx/merge cofx
|
||||||
{:db (assoc-in db [:chats current-chat-id :messages-initialized?] now)}
|
{:db (-> db
|
||||||
(load-more-messages)))))
|
;; We keep track of whether there's a loaded chat
|
||||||
|
;; which will be reset only if we hit home
|
||||||
|
(assoc :loaded-chat-id current-chat-id)
|
||||||
|
(assoc-in [:chats current-chat-id :messages-initialized?] now))}
|
||||||
|
(chat-model/mark-messages-seen current-chat-id)
|
||||||
|
(load-more-messages)))
|
||||||
|
;; We mark messages as seen in case we received them while on a different tab
|
||||||
|
(chat-model/mark-messages-seen cofx current-chat-id))))
|
||||||
|
|
||||||
|
|
|
@ -80,11 +80,11 @@
|
||||||
(fx/defn add-message
|
(fx/defn add-message
|
||||||
[{:keys [db] :as cofx}
|
[{:keys [db] :as cofx}
|
||||||
{{:keys [chat-id message-id replace timestamp from] :as message} :message
|
{{:keys [chat-id message-id replace timestamp from] :as message} :message
|
||||||
:keys [current-chat?]}]
|
:keys [seen-by-user?]}]
|
||||||
(let [current-public-key (multiaccounts.model/current-public-key cofx)
|
(let [current-public-key (multiaccounts.model/current-public-key cofx)
|
||||||
message-to-be-removed (when replace
|
message-to-be-removed (when replace
|
||||||
(get-in db [:chats chat-id :messages replace]))
|
(get-in db [:chats chat-id :messages replace]))
|
||||||
prepared-message (prepare-message message chat-id current-chat?)]
|
prepared-message (prepare-message message chat-id seen-by-user?)]
|
||||||
(fx/merge cofx
|
(fx/merge cofx
|
||||||
(when message-to-be-removed
|
(when message-to-be-removed
|
||||||
(hide-message chat-id message-to-be-removed))
|
(hide-message chat-id message-to-be-removed))
|
||||||
|
@ -97,7 +97,7 @@
|
||||||
(update-in [:chats chat-id :messages] assoc message-id prepared-message)
|
(update-in [:chats chat-id :messages] assoc message-id prepared-message)
|
||||||
(update-in [:chats chat-id :message-list] message-list/add prepared-message))
|
(update-in [:chats chat-id :message-list] message-list/add prepared-message))
|
||||||
|
|
||||||
(and (not current-chat?)
|
(and (not seen-by-user?)
|
||||||
(not= from current-public-key))
|
(not= from current-public-key))
|
||||||
(update-in [:chats chat-id :loaded-unviewed-messages-ids]
|
(update-in [:chats chat-id :loaded-unviewed-messages-ids]
|
||||||
(fnil conj #{}) message-id))}))))
|
(fnil conj #{}) message-id))}))))
|
||||||
|
@ -109,10 +109,11 @@
|
||||||
chat-id
|
chat-id
|
||||||
clock-value
|
clock-value
|
||||||
content] :as message}]
|
content] :as message}]
|
||||||
(let [{:keys [current-chat-id view-id]} db
|
(let [{:keys [loaded-chat-id
|
||||||
|
view-id
|
||||||
|
current-chat-id]} db
|
||||||
cursor-clock-value (get-in db [:chats current-chat-id :cursor-clock-value])
|
cursor-clock-value (get-in db [:chats current-chat-id :cursor-clock-value])
|
||||||
current-chat? (and (= :chat view-id)
|
current-chat? (= chat-id loaded-chat-id)]
|
||||||
(= current-chat-id chat-id))]
|
|
||||||
(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)))
|
||||||
|
@ -122,7 +123,8 @@
|
||||||
(<= (:clock-value @view.state/viewable-item)
|
(<= (:clock-value @view.state/viewable-item)
|
||||||
clock-value))
|
clock-value))
|
||||||
(add-message cofx {:message message
|
(add-message cofx {:message message
|
||||||
:current-chat? current-chat?})
|
:seen-by-user? (and current-chat?
|
||||||
|
(= view-id :chat))})
|
||||||
;; Not in the current view, offload to db and update cursor if necessary
|
;; 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)
|
||||||
|
@ -226,7 +228,7 @@
|
||||||
{:events [:messages/system-messages-saved]}
|
{:events [:messages/system-messages-saved]}
|
||||||
[cofx messages]
|
[cofx messages]
|
||||||
(apply fx/merge cofx (map #(add-message {:message %
|
(apply fx/merge cofx (map #(add-message {:message %
|
||||||
:current-chat? true})
|
:seen-by-user? true})
|
||||||
messages)))
|
messages)))
|
||||||
|
|
||||||
(fx/defn send-message
|
(fx/defn send-message
|
||||||
|
|
|
@ -45,6 +45,7 @@
|
||||||
(deftest add-own-received-message
|
(deftest add-own-received-message
|
||||||
(let [db {:multiaccount {:public-key "me"}
|
(let [db {:multiaccount {:public-key "me"}
|
||||||
:view-id :chat
|
:view-id :chat
|
||||||
|
:loaded-chat-id "chat-id"
|
||||||
:current-chat-id "chat-id"
|
:current-chat-id "chat-id"
|
||||||
:chats {"chat-id" {:messages {}}}}]
|
:chats {"chat-id" {:messages {}}}}]
|
||||||
(testing "a message coming from you!"
|
(testing "a message coming from you!"
|
||||||
|
@ -66,6 +67,7 @@
|
||||||
(let [cofx {:db {:chats {"chat-id" {:contacts #{"present"}
|
(let [cofx {:db {:chats {"chat-id" {:contacts #{"present"}
|
||||||
:members-joined #{"a"}}}
|
:members-joined #{"a"}}}
|
||||||
:multiaccount {:public-key "a"}
|
:multiaccount {:public-key "a"}
|
||||||
|
:loaded-chat-id "chat-id"
|
||||||
:current-chat-id "chat-id"
|
:current-chat-id "chat-id"
|
||||||
:view-id :chat}}
|
:view-id :chat}}
|
||||||
cofx-without-member (update-in cofx [:db :chats "chat-id" :members-joined] disj "a")
|
cofx-without-member (update-in cofx [:db :chats "chat-id" :members-joined] disj "a")
|
||||||
|
@ -102,6 +104,7 @@
|
||||||
(deftest receive-public-chats
|
(deftest receive-public-chats
|
||||||
(let [cofx {:db {:chats {"chat-id" {:public? true}}
|
(let [cofx {:db {:chats {"chat-id" {:public? true}}
|
||||||
:multiaccount {:public-key "a"}
|
:multiaccount {:public-key "a"}
|
||||||
|
:loaded-chat-id "chat-id"
|
||||||
:current-chat-id "chat-id"
|
:current-chat-id "chat-id"
|
||||||
:view-id :chat}}
|
:view-id :chat}}
|
||||||
valid-message {:chat-id "chat-id"
|
valid-message {:chat-id "chat-id"
|
||||||
|
@ -130,6 +133,7 @@
|
||||||
(let [cofx {:db {:chats {"matching" {}}
|
(let [cofx {:db {:chats {"matching" {}}
|
||||||
:multiaccount {:public-key "me"}
|
:multiaccount {:public-key "me"}
|
||||||
:current-chat-id "matching"
|
:current-chat-id "matching"
|
||||||
|
:loaded-chat-id "matching"
|
||||||
:view-id :chat}}
|
:view-id :chat}}
|
||||||
valid-message {:chat-id "matching"
|
valid-message {:chat-id "matching"
|
||||||
:from "matching"
|
:from "matching"
|
||||||
|
|
Loading…
Reference in New Issue