Faster msg status updates

This commit is contained in:
janherich 2018-01-04 20:19:16 +01:00
parent 1ec7053aee
commit 201484f37f
No known key found for this signature in database
GPG Key ID: C23B473AFBE94D13
5 changed files with 31 additions and 21 deletions

View File

@ -1,5 +1,7 @@
(ns status-im.chat.events (ns status-im.chat.events
(:require [re-frame.core :as re-frame] (:require-macros [cljs.core.async.macros :as async])
(:require [cljs.core.async :as async]
[re-frame.core :as re-frame]
[taoensso.timbre :as log] [taoensso.timbre :as log]
[status-im.utils.handlers :as handlers] [status-im.utils.handlers :as handlers]
[status-im.utils.gfycat.core :as gfycat] [status-im.utils.gfycat.core :as gfycat]
@ -54,13 +56,19 @@
(fn [cofx _] (fn [cofx _]
(assoc cofx :get-stored-chat chats-store/get-by-id))) (assoc cofx :get-stored-chat chats-store/get-by-id)))
;;;; Effects ;;;; Effects
(def ^:private update-message-queue (async/chan 100))
(async/go-loop [message (async/<! update-message-queue)]
(when message
(messages-store/update-message message)
(recur (async/<! update-message-queue))))
(re-frame/reg-fx (re-frame/reg-fx
:update-message :update-message
(fn [message] (fn [message]
(messages-store/update-message message))) (async/put! update-message-queue message)))
(re-frame/reg-fx (re-frame/reg-fx
:save-message :save-message

View File

@ -64,7 +64,8 @@
(utils/update-if-present message (utils/update-if-present message
:user-statuses :user-statuses
(partial map (fn [[whisper-identity status]] (partial map (fn [[whisper-identity status]]
{:whisper-identity whisper-identity {:status-id (str message-id "-" whisper-identity)
:whisper-identity whisper-identity
:status status :status status
:chat-id chat-id :chat-id chat-id
:message-id message-id})))) :message-id message-id}))))

View File

@ -112,9 +112,11 @@
statuses (aget msg "user-statuses")] statuses (aget msg "user-statuses")]
(when statuses (when statuses
(.map statuses (fn [status _ _] (.map statuses (fn [status _ _]
(aset status "status-id" (str message-id "-" from))
(aset status "message-id" message-id) (aset status "message-id" message-id)
(aset status "chat-id" chat-id))) (aset status "chat-id" chat-id)))
(.push statuses (clj->js {"message-id" message-id (.push statuses (clj->js {"status-id" (str message-id "-anonymous")
"message-id" message-id
"chat-id" chat-id "chat-id" chat-id
"status" (or msg-status "received") "status" (or msg-status "received")
"whisper-identity" (or from "anonymous")})))))))) "whisper-identity" (or from "anonymous")}))))))))

View File

@ -1,7 +1,12 @@
(ns status-im.data-store.realm.schemas.account.v19.user-status) (ns status-im.data-store.realm.schemas.account.v19.user-status)
(def schema {:name :user-status (def schema {:name :user-status
:properties {:message-id :string :primaryKey :status-id
:properties {;; Unfortunately, realm doesn't support composite primary keys,
;; so we have to keep separate `:status-id` property, which is just
;; `:message-id`-`:whisper-identity` concatenated
:status-id :string
:message-id :string
:chat-id :string :chat-id :string
:whisper-identity :string :whisper-identity :string
:status :string}}) :status :string}})

View File

@ -61,13 +61,6 @@
(and (chats/is-active? group-id) (and (chats/is-active? group-id)
(> timestamp (chats/get-property group-id :timestamp))))))) (> timestamp (chats/get-property group-id :timestamp)))))))
(re-frame/reg-cofx
::chats-is-active?
(fn [coeffects _]
(let [[{{:keys [group-id]} :payload from :from}] (:event coeffects)]
(assoc coeffects :chats-is-active?
(chats/is-active? (or group-id from))))))
(re-frame/reg-cofx (re-frame/reg-cofx
::has-contact? ::has-contact?
(fn [coeffects _] (fn [coeffects _]
@ -347,16 +340,17 @@
(handlers/register-handler-fx (handlers/register-handler-fx
:update-message-status :update-message-status
[re-frame/trim-v [re-frame/trim-v (re-frame/inject-cofx :get-stored-message)]
(re-frame/inject-cofx :get-stored-message) (fn [{:keys [db get-stored-message]} [{:keys [from sent-from payload]} status]]
(re-frame/inject-cofx ::chats-is-active?)]
(fn [{:keys [db chats-is-active? get-stored-message]} [{:keys [from sent-from payload]} status]]
(let [message-identifier (get-message-id payload) (let [message-identifier (get-message-id payload)
message-db-path [:chats (or (:group-id payload) from) :messages message-identifier] chat-identifier (or (:group-id payload) from)
message-db-path [:chats chat-identifier :messages message-identifier]
from-id (or sent-from from) from-id (or sent-from from)
message (get-stored-message message-identifier)] message (get-stored-message message-identifier)]
;; proceed with updating status if chat is active, and message was not already seen ;; proceed with updating status if chat is in db, status is not the same and message was not already seen
(when (and chats-is-active? (not (chat.utils/message-seen-by? message from-id))) (when (and (get-in db [:chats chat-identifier])
(not= status (get-in message [:user-statuses from-id]))
(not (chat.utils/message-seen-by? message from-id)))
(let [statuses (assoc (:user-statuses message) from-id status)] (let [statuses (assoc (:user-statuses message) from-id status)]
(cond-> {:update-message {:message-id message-identifier (cond-> {:update-message {:message-id message-identifier
:user-statuses statuses}} :user-statuses statuses}}