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

View File

@ -6,9 +6,10 @@
[status-im.utils.utils :refer [truncate-str]] [status-im.utils.utils :refer [truncate-str]]
[status-im.utils.datetime :as time])) [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 [{:keys [chat-id name color new-messages-count
online group-chat contacts] :as chat}] online group-chat contacts] :as chat}]
[unviewed-messages [:unviewed-messages-count chat-id]]
(let [last-message (first (:messages chat))] (let [last-message (first (:messages chat))]
[view st/chat-container [view st/chat-container
[view st/chat-icon-container [view st/chat-icon-container
@ -43,6 +44,6 @@
(when (:timestamp last-message) (when (:timestamp last-message)
[text {:style st/datetime-text} [text {:style st/datetime-text}
(time/to-short-str (:timestamp last-message))])]) (time/to-short-str (:timestamp last-message))])])
(when (pos? new-messages-count) (when (pos? unviewed-messages)
[view st/new-messages-container [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.utils.handlers :refer [register-handler] :as u]
[status-im.models.protocol :as protocol] [status-im.models.protocol :as protocol]
status-im.chat.handlers status-im.chat.handlers
status-im.chat.handlers.animation
status-im.group-settings.handlers status-im.group-settings.handlers
status-im.navigation.handlers status-im.navigation.handlers
status-im.contacts.handlers status-im.contacts.handlers
@ -22,8 +21,7 @@
status-im.commands.handlers.jail status-im.commands.handlers.jail
status-im.qr-scanner.handlers status-im.qr-scanner.handlers
status-im.accounts.handlers status-im.accounts.handlers
status-im.protocol.handlers status-im.protocol.handlers))
status-im.chat.handlers.requests))
;; -- Middleware ------------------------------------------------------------ ;; -- Middleware ------------------------------------------------------------
;; ;;

View File

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

View File

@ -122,8 +122,8 @@
(after (update-message! :failed)) (after (update-message! :failed))
(update-message-status :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 (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)) (update-message-status :seen))