Skip to content

Commit

Permalink
Move chats to status-go
Browse files Browse the repository at this point in the history
This commit moves chats to status-go.

I have changed the logic to load all chats in one go for simplicity and
while that might have a performance impact, I think it's premature to
  optimize this flow as there will be more changes to the login flow.

Also currently this is likely to be slower as we need to wait for the
 status-service to be initialized, as well as realm.

No migration is provided as we are past the point of no return, so by
installing this version you will lose your chats.

Signed-off-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
  • Loading branch information
cammellos committed Aug 5, 2019
1 parent f464269 commit 6fa482a
Show file tree
Hide file tree
Showing 24 changed files with 347 additions and 297 deletions.
34 changes: 19 additions & 15 deletions src/status_im/chat/models.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@
(or (get (:chats db) chat-id)
(create-new-chat chat-id cofx))
chat-props)]
{:db (update-in db [:chats chat-id] merge chat)
:data-store/tx [(chats-store/save-chat-tx chat)]}))
(fx/merge cofx
{:db (update-in db [:chats chat-id] merge chat)}
(chats-store/save-chat-rpc chat))))

(fx/defn add-public-chat
"Adds new public group chat to db & realm"
Expand Down Expand Up @@ -138,22 +139,25 @@
:clock-value)
deleted-at-clock-value
(utils.clocks/send 0))]
{:db (update-in db [:chats chat-id] merge
{:messages empty-message-map
:message-groups {}
:last-message-content nil
:last-message-content-type nil
:unviewed-messages-count 0
:deleted-at-clock-value last-message-clock-value})
:data-store/tx [(chats-store/clear-history-tx chat-id last-message-clock-value)
(messages-store/delete-chat-messages-tx chat-id)]}))
(fx/merge
cofx
{:db (update-in db [:chats chat-id] merge
{:messages empty-message-map
:message-groups {}
:last-message-content nil
:last-message-content-type nil
:unviewed-messages-count 0
:deleted-at-clock-value last-message-clock-value})
:data-store/tx [(messages-store/delete-chat-messages-tx chat-id)]}
#(chats-store/save-chat-rpc % (get-in % [:db :chats chat-id])))))

(fx/defn deactivate-chat
[{:keys [db now] :as cofx} chat-id]
{:db (-> db
(assoc-in [:chats chat-id :is-active] false)
(assoc-in [:current-chat-id] nil))
:data-store/tx [(chats-store/deactivate-chat-tx chat-id now)]})
(fx/merge cofx
{:db (-> db
(assoc-in [:chats chat-id :is-active] false)
(assoc-in [:current-chat-id] nil))}
#(chats-store/save-chat-rpc % (get-in % [:db :chats chat-id]))))

(fx/defn remove-chat
"Removes chat completely from app, producing all necessary effects for that"
Expand Down
51 changes: 17 additions & 34 deletions src/status_im/chat/models/loading.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
(ns status-im.chat.models.loading
(:require [re-frame.core :as re-frame]
[status-im.data-store.chats :as data-store.chats]
[status-im.chat.commands.core :as commands]
[status-im.transport.filters.core :as filters]
[status-im.chat.models :as chat-model]
[status-im.ethereum.json-rpc :as json-rpc]
[status-im.mailserver.core :as mailserver]
Expand Down Expand Up @@ -55,35 +57,7 @@

(fx/defn update-chats-in-app-db
{:events [:chats-list/load-success]}
[{:keys [db] :as cofx} chats]
(fx/merge cofx
{:db (assoc db :chats chats)}
(commands/load-commands commands/register)))

(defn- unkeywordize-chat-names
[chats]
(reduce-kv
(fn [acc chat-keyword chat]
(assoc acc (name chat-keyword) chat))
{}
chats))

(defn load-chats-from-rpc
[cofx]
(fx/merge cofx
{::json-rpc/call [{:method "status_chats"
:params []
:on-error
#(log/error "can't retrieve chats list from status-go:" %)
:on-success
#(re-frame/dispatch
[:chats-list/load-success
(unkeywordize-chat-names (:chats %))])}]}))

(defn initialize-chats-legacy
"Use realm + clojure to manage chats"
[{:keys [db get-all-stored-chats] :as cofx}
from to]
[{:keys [db] :as cofx} new-chats]
(let [old-chats (:chats db)
chats (reduce (fn [acc {:keys [chat-id] :as chat}]
(assoc acc chat-id
Expand All @@ -92,17 +66,26 @@
:referenced-messages {}
:messages empty-message-map)))
{}
(get-all-stored-chats from to))
new-chats)
chats (merge old-chats chats)]
(update-chats-in-app-db cofx chats)))
(fx/merge cofx
{:db (assoc db :chats chats
:chats/loading? false)}
(filters/load-filters)
(commands/load-commands commands/register))))

(defn load-chats-from-rpc
[cofx from to]
(data-store.chats/fetch-chats-rpc cofx {:from 0
:to 10
:on-success
#(re-frame/dispatch
[:chats-list/load-success %])}))
(fx/defn initialize-chats
"Initialize persisted chats on startup"
[cofx
{:keys [from to] :or {from 0 to nil}}]
(if config/use-status-go-protocol?
(load-chats-from-rpc cofx)
(initialize-chats-legacy cofx from to)))
(load-chats-from-rpc cofx from -1))

(defn load-more-messages
"Loads more messages for current chat"
Expand Down
7 changes: 5 additions & 2 deletions src/status_im/contact/block.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
[{:keys [db] :as cofx} chat-id removed-chat-messages]
(let [removed-messages-ids (map :message-id removed-chat-messages)
removed-unseen-count (count (remove :seen removed-chat-messages))
unviewed-messages-count (- (get-in db [:chats chat-id :unviewed-messages-count])
removed-unseen-count)
db (-> db
;; remove messages
(update-in [:chats chat-id :messages]
Expand All @@ -32,8 +34,9 @@
(chat.models/upsert-chat
{:chat-id chat-id
:unviewed-messages-count
(- (get-in db [:chats chat-id :unviewed-messages-count])
removed-unseen-count)})
(if (pos? unviewed-messages-count)
unviewed-messages-count
0)})
;; recompute message group
(chat.models.loading/group-chat-messages
chat-id
Expand Down
Loading

0 comments on commit 6fa482a

Please sign in to comment.