unviewed messages counter

This commit is contained in:
Roman Volosovskyi 2016-07-06 20:17:52 +03:00
parent 6d4fd4dfda
commit 2e790fb006
8 changed files with 86 additions and 28 deletions

View File

@ -24,7 +24,9 @@
[status-im.utils.logging :as log]
[status-im.components.jail :as j]
[status-im.utils.types :refer [json->clj]]
[status-im.commands.utils :refer [generate-hiccup]]))
[status-im.commands.utils :refer [generate-hiccup]]
status-im.chat.handlers.requests
status-im.chat.handlers.unviewed-messages))
(register-handler :set-show-actions
(fn [db [_ show-actions]]
@ -221,14 +223,15 @@
[command] (suggestions/check-suggestion db (str text " "))
message (check-author-direction
db current-chat-id
{:msg-id (random/id)
:chat-id current-chat-id
:content text
:to current-chat-id
:from identity
:content-type text-content-type
:outgoing true
:timestamp (time/now-ms)})]
{:msg-id (random/id)
:chat-id current-chat-id
:content text
:to current-chat-id
:from identity
:content-type text-content-type
:delivery-status :pending
:outgoing true
:timestamp (time/now-ms)})]
(if command
(commands/set-chat-command db command)
(assoc db :new-message (when-not (str/blank? text) message)))))
@ -424,6 +427,7 @@
(assoc db :loaded-chats (chats/chats-list)))
(register-handler :initialize-chats
(after #(dispatch [:load-unviewed-messages!]))
((enrich initialize-chats) load-chats!))
(defn store-message!
@ -437,14 +441,22 @@
(defn receive-message
[db [_ {chat-id :from :as message}]]
(let [message' (check-author-direction db chat-id message)]
(let [message' (-> db
(check-author-direction chat-id message)
(assoc :delivery-status :pending))]
(-> db
(add-message-to-db chat-id message')
(assoc :new-message message'))))
(defn dispatch-unviewed-message!
[{:keys [new-message]} [_ {chat-id :from}]]
(let [{:keys [msg-id]} new-message]
(dispatch [:add-unviewed-message chat-id msg-id])))
(register-handler :received-msg
[(after store-message!)
(after dispatch-request!)]
(after dispatch-request!)
(after dispatch-unviewed-message!)]
receive-message)
(register-handler :group-received-msg
@ -574,8 +586,8 @@
(register-handler :send-seen!
#_(afetr (fn [_ [_ chat-id message-id]]
(dispatch [:msg-seen chat-id message-id])))
(debug (u/side-effect!
(fn [_ [_ chat-id message-id]]
(api/send-seen chat-id message-id)))))
(after (fn [_ [_ chat-id message-id]]
(dispatch [:msg-seen chat-id message-id])))
(u/side-effect!
(fn [_ [_ chat-id message-id]]
(api/send-seen chat-id message-id))))

View File

@ -0,0 +1,44 @@
(ns status-im.chat.handlers.unviewed-messages
(:require [re-frame.core :refer [after enrich path dispatch]]
[status-im.utils.handlers :refer [register-handler]]
[status-im.persistence.realm :as realm]))
(defn delivered-messages []
(-> (realm/get-by-fields
:msgs
{:delivery-status :delivered
:outgoing false})
(realm/collection->map)))
(defn set-unviewed-messages [db]
(let [messages (->> (::raw-unviewed-messages db)
(group-by :chat-id)
(map (fn [[id messages]]
[id {:messages-ids (map :msg-id messages)
:count (count messages)}]))
(into {}))]
(-> db
(assoc :unviewed-messages messages)
(dissoc ::raw-unviewed-messages))))
(defn load-messages! [db]
(let [messages (delivered-messages)]
(assoc db ::raw-unviewed-messages messages)))
(register-handler ::set-unviewed-messages set-unviewed-messages)
(register-handler :load-unviewed-messages!
(after #(dispatch [::set-unviewed-messages]))
load-messages!)
(register-handler :add-unviewed-message
(path :unviewed-messages)
(fn [db [_ chat-id message-id]]
(-> db
(update-in [chat-id :messages-ids] conj message-id)
(update-in [chat-id :count] inc))))
(register-handler :remove-unviewed-messages
(path :unviewed-messages)
(fn [db [_ chat-id]]
(dissoc db chat-id)))

View File

@ -172,3 +172,7 @@
(fn [_ [_ message-id]]
(let [requests (subscribe [:get-requests])]
(reaction (not (some #(= message-id (:message-id %)) @requests))))))
(register-sub :unviewed-messages-count
(fn [db [_ chat-id]]
(reaction (get-in @db [:unviewed-messages chat-id :count]))))

View File

@ -211,7 +211,7 @@
(r/create-class
{:component-did-mount
(fn []
(when (and outgoing
(when (and (not outgoing)
(not= :seen delivery-status)
(not= :seen @status))
(dispatch [:send-seen! chat-id msg-id])))

View File

@ -6,9 +6,10 @@
[status-im.utils.utils :refer [truncate-str]]
[status-im.utils.datetime :as time]))
(defn chat-list-item-inner-view
(defview chat-list-item-inner-view
[{:keys [chat-id name color new-messages-count
online group-chat contacts] :as chat}]
[unviewed-messages [:unviewed-messages-count chat-id]]
(let [last-message (first (:messages chat))]
[view st/chat-container
[view st/chat-icon-container
@ -43,6 +44,6 @@
(when (:timestamp last-message)
[text {:style st/datetime-text}
(time/to-short-str (:timestamp last-message))])])
(when (pos? new-messages-count)
(when (pos? unviewed-messages)
[view st/new-messages-container
[text {:style st/new-messages-text} new-messages-count]])]]))
[text {:style st/new-messages-text} unviewed-messages]])]]))

View File

@ -11,7 +11,6 @@
[status-im.utils.handlers :refer [register-handler] :as u]
[status-im.models.protocol :as protocol]
status-im.chat.handlers
status-im.chat.handlers.animation
status-im.group-settings.handlers
status-im.navigation.handlers
status-im.contacts.handlers
@ -22,8 +21,7 @@
status-im.commands.handlers.jail
status-im.qr-scanner.handlers
status-im.accounts.handlers
status-im.protocol.handlers
status-im.chat.handlers.requests))
status-im.protocol.handlers))
;; -- Middleware ------------------------------------------------------------
;;

View File

@ -38,8 +38,7 @@
message
{:chat-id chat-id
:content content'
:timestamp (timestamp)
:delivery-status nil})]
:timestamp (timestamp)})]
(r/create :msgs message' true))))))
(defn command-type? [type]

View File

@ -122,8 +122,8 @@
(after (update-message! :failed))
(update-message-status :failed))
;; todo maybe it is fine to treat as "seen" all messages that are older
;; than current
(register-handler :msg-seen
(after (update-message! :seen))
[(after (update-message! :seen))
(after (fn [_ [_ chat-id]]
(dispatch [:remove-unviewed-messages chat-id])))]
(update-message-status :seen))