Fix disappearing of the last notification when paired

This commit is contained in:
Roman Volosovskyi 2022-08-15 13:01:30 +02:00
parent 959df25c02
commit 24ef84b890
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
1 changed files with 27 additions and 18 deletions

View File

@ -12,25 +12,34 @@
(fx/defn handle-activities [{:keys [db]} activities] (fx/defn handle-activities [{:keys [db]} activities]
(let [{:keys [unread-count notifications]} (let [{:keys [unread-count notifications]}
(reduce (fn [acc {:keys [read dismissed accepted] :as notification}] (reduce (fn [acc {:keys [read dismissed accepted chat-id] :as notification}]
(let [index-existing (->> (map-indexed vector (:notifications acc)) (if (= "" chat-id)
(filter (fn [[idx {:keys [id]}]] (= id (:id notification)))) ;; TODO(rasom): sometimes messages come with empty `chat-id`s
first ;; (specifically it happens on `SyncActivityCenterRead` message).
first)] ;; In result, if notification is received with notification center
(as-> acc a ;; screen opened, and there is another paired device online, the
(if read ;; last notification disappear from the screen and is shown only
(update a :unread-count dec) ;; after reopening. It likely makes sense to fix it on status-go
(update a :unread-count inc)) ;; side, but I got lost a bit.
acc
(let [index-existing (->> (map-indexed vector (:notifications acc))
(filter (fn [[idx {:keys [id]}]] (= id (:id notification))))
first
first)]
(as-> acc a
(if read
(update a :unread-count dec)
(update a :unread-count inc))
(if index-existing (if index-existing
(if (or dismissed accepted) (if (or dismissed accepted)
;; Remove at specific location ;; Remove at specific location
(assoc a :notifications (assoc a :notifications
(into (subvec (:notifications a) 0 index-existing) (subvec (:notifications a) (inc index-existing)))) (into (subvec (:notifications a) 0 index-existing) (subvec (:notifications a) (inc index-existing))))
;; Replace element ;; Replace element
(do (do
(assoc-in a [:notifications index-existing] notification))) (assoc-in a [:notifications index-existing] notification)))
(update a :notifications conj notification))))) (update a :notifications conj notification))))))
{:unread-count (get db :activity.center/notifications-count 0) {:unread-count (get db :activity.center/notifications-count 0)
:notifications (into [] (get-in db [:activity.center/notifications :notifications]))} :notifications (into [] (get-in db [:activity.center/notifications :notifications]))}
activities)] activities)]