Second stage of realm transactions

Signed-off-by: Julien Eluard <julien.eluard@gmail.com>
This commit is contained in:
janherich 2018-05-14 19:55:30 +02:00 committed by Julien Eluard
parent 1fc30b798d
commit ae109c6e99
No known key found for this signature in database
GPG Key ID: 6FD7DB5437FCBEF6
21 changed files with 198 additions and 331 deletions

View File

@ -22,6 +22,7 @@
[status-im.transport.message.v1.group-chat :as group-chat] [status-im.transport.message.v1.group-chat :as group-chat]
[status-im.data-store.chats :as chats-store] [status-im.data-store.chats :as chats-store]
[status-im.data-store.messages :as messages-store] [status-im.data-store.messages :as messages-store]
[status-im.data-store.contacts :as contacts-store]
status-im.chat.events.commands status-im.chat.events.commands
status-im.chat.events.requests status-im.chat.events.requests
status-im.chat.events.send-message status-im.chat.events.send-message
@ -167,8 +168,9 @@
contacts-to-add (select-keys new-contacts (set/difference (set (keys new-contacts)) contacts-to-add (select-keys new-contacts (set/difference (set (keys new-contacts))
(set (keys existing-contacts))))] (set (keys existing-contacts))))]
(handlers-macro/merge-fx cofx (handlers-macro/merge-fx cofx
{:db (update db :contacts/contacts merge contacts-to-add) {:db (update db :contacts/contacts merge contacts-to-add)
:data-store/save-contacts (vals contacts-to-add)} :data-store/tx [(contacts-store/save-contacts-tx
(vals contacts-to-add))]}
(events.loading/load-commands)))) (events.loading/load-commands))))
(handlers/register-handler-fx (handlers/register-handler-fx

View File

@ -1,6 +1,6 @@
(ns status-im.chat.events.requests (ns status-im.chat.events.requests
(:require [re-frame.core :as re-frame] (:require [status-im.constants :as constants]
[status-im.constants :as constants])) [status-im.data-store.requests :as requests-store]))
;; Functions ;; Functions
@ -8,9 +8,8 @@
"Takes chat-id, message-id and cofx, returns fx necessary data for marking request as answered" "Takes chat-id, message-id and cofx, returns fx necessary data for marking request as answered"
[chat-id message-id {:keys [db]}] [chat-id message-id {:keys [db]}]
(when message-id (when message-id
{:db (update-in db [:chats chat-id :requests] dissoc message-id) {:db (update-in db [:chats chat-id :requests] dissoc message-id)
:data-store/mark-request-as-answered {:chat-id chat-id :data-store/tx [(requests-store/mark-request-as-answered-tx chat-id message-id)]}))
:message-id message-id}}))
(defn add-request (defn add-request
"Takes chat-id, message-id + cofx and returns fx with necessary data for adding new request" "Takes chat-id, message-id + cofx and returns fx with necessary data for adding new request"
@ -21,5 +20,5 @@
:message-id message-id :message-id message-id
:response (:request-command content) :response (:request-command content)
:status "open"}] :status "open"}]
{:db (assoc-in db [:chats chat-id :requests message-id] request) {:db (assoc-in db [:chats chat-id :requests message-id] request)
:data-store/save-request request})))) :data-store/tx [(requests-store/save-request-tx request)]}))))

View File

@ -7,7 +7,8 @@
[status-im.commands.utils :refer [reg-handler]] [status-im.commands.utils :refer [reg-handler]]
[status-im.constants :refer [console-chat-id]] [status-im.constants :refer [console-chat-id]]
[status-im.i18n :refer [get-contact-translated]] [status-im.i18n :refer [get-contact-translated]]
[taoensso.timbre :as log])) [taoensso.timbre :as log]
[status-im.data-store.local-storage :as local-storage-store]))
(defn command-handler! (defn command-handler!
[_ [chat-id [_ [chat-id
@ -105,5 +106,6 @@
:set-local-storage :set-local-storage
[trim-v] [trim-v]
(fn [_ [{:keys [data chat-id]}]] (fn [_ [{:keys [data chat-id]}]]
{:data-store/set-local-storage-data {:chat-id chat-id {:data-store/tx [(local-storage-store/set-local-storage-data-tx
:data data}})) {:chat-id chat-id
:data data})]}))

View File

@ -1,24 +1,31 @@
(ns status-im.data-store.accounts (ns status-im.data-store.accounts
(:require [cljs.core.async :as async] (:require [re-frame.core :as re-frame]
[re-frame.core :as re-frame] [status-im.data-store.realm.core :as core]))
[status-im.data-store.realm.core :as core]
[status-im.data-store.realm.accounts :as data-store]))
;; TODO janherich: define as cofx once debug handlers are refactored ;; TODO janherich: define as cofx once debug handlers are refactored
(defn get-by-address [address] (defn get-by-address [address]
(data-store/get-by-address address)) (-> @core/base-realm
(core/get-by-field :account :address address)
(core/single-clj :account)
(update :settings core/deserialize)))
(re-frame/reg-cofx (re-frame/reg-cofx
:data-store/get-all-accounts :data-store/get-all-accounts
(fn [coeffects _] (fn [coeffects _]
(assoc coeffects :all-accounts (data-store/get-all-as-list)))) (assoc coeffects :all-accounts (-> @core/base-realm
(core/get-all :account)
(re-frame/reg-fx (core/all-clj :account)
:data-store/save-account (as-> accounts
(fn [{:keys [after-update-event] :as account}] (map #(update % :settings core/deserialize) accounts))))))
(let [account-to-save (dissoc account :after-update-event)]
(async/go (async/>! core/realm-queue #(if after-update-event
(do (data-store/save account-to-save true)
(re-frame/dispatch after-update-event))
(data-store/save account-to-save true)))))))
(defn save-account-tx
"Returns tx function for saving account"
[{:keys [after-update-event] :as account}]
(fn [realm]
(let [account-to-save (-> account
(dissoc :after-update-event)
(update :settings core/serialize)
(update :networks vals))]
(core/create realm :account account-to-save true)
(when after-update-event
(re-frame/dispatch after-update-event)))))

View File

@ -1,21 +1,24 @@
(ns status-im.data-store.browser (ns status-im.data-store.browser
(:require [cljs.core.async :as async] (:require [re-frame.core :as re-frame]
[re-frame.core :as re-frame] [status-im.data-store.realm.core :as core]))
[status-im.data-store.realm.core :as core]
[status-im.data-store.realm.browser :as data-store])
(:refer-clojure :exclude [exists?]))
(re-frame/reg-cofx (re-frame/reg-cofx
:data-store/all-browsers :data-store/all-browsers
(fn [cofx _] (fn [cofx _]
(assoc cofx :all-stored-browsers (data-store/get-all)))) (assoc cofx :all-stored-browsers (-> @core/account-realm
(core/get-all :browser)
(core/sorted :timestamp :desc)
(core/all-clj :browser)))))
(re-frame/reg-fx (defn save-browser-tx
:data-store/save-browser "Returns tx function for saving browser"
(fn [{:keys [browser-id] :as browser}] [{:keys [browser-id] :as browser}]
(async/go (async/>! core/realm-queue #(data-store/save browser (data-store/exists? browser-id)))))) (fn [realm]
(core/create realm :browser browser (core/exists? realm :browser :browser-id browser-id))))
(re-frame/reg-fx (defn remove-browser-tx
:data-store/remove-browser "Returns tx function for removing browser"
(fn [browser-id] [browser-id]
(async/go (async/>! core/realm-queue #(data-store/delete browser-id))))) (fn [realm]
(let [browser (core/single (core/get-by-field realm :browser :browser-id browser-id))]
(core/delete realm browser))))

View File

@ -1,34 +1,35 @@
(ns status-im.data-store.contact-groups (ns status-im.data-store.contact-groups
(:require [cljs.core.async :as async] (:require [goog.object :as object]
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[status-im.data-store.realm.core :as core] [status-im.data-store.realm.core :as core]))
[status-im.data-store.realm.contact-groups :as data-store])
(:refer-clojure :exclude [exists?]))
(re-frame/reg-cofx (re-frame/reg-cofx
:data-store/get-all-contact-groups :data-store/get-all-contact-groups
(fn [cofx _] (fn [cofx _]
(assoc cofx :all-contact-groups (into {} (assoc cofx :all-contact-groups (into {}
(map (juxt :group-id identity)) (map (juxt :group-id identity))
(data-store/get-all-as-list))))) (-> @core/account-realm
(core/get-all :contact-group)
(core/all-clj :contact-group))))))
(re-frame/reg-fx (defn save-contact-group-tx
:data-store/save-contact-group "Returns tx function for saving contact group"
(fn [{:keys [group-id] :as group}] [{:keys [group-id] :as group}]
(async/go (async/>! core/realm-queue #(data-store/save group (data-store/exists? group-id)))))) (fn [realm]
(core/create realm :contact-group group (core/exists? realm :contact-group :group-id group-id))))
(re-frame/reg-fx (defn save-contact-groups-tx
:data-store/save-contact-groups "Returns tx function for saving contact groups"
(fn [groups] [groups]
(doseq [{:keys [group-id] :as group} groups] (fn [realm]
(async/go (async/>! core/realm-queue #(data-store/save group (data-store/exists? group-id))))))) (doseq [group groups]
((save-contact-group-tx group) realm))))
(re-frame/reg-fx (defn add-contacts-to-contact-group-tx
:data-store/save-contact-group-property "Returns tx function for adding contacts to contact group"
(fn [[group-id property-name value]] [group-id contacts]
(async/go (async/>! core/realm-queue #(data-store/save-property group-id property-name value))))) (fn [realm]
(let [group (core/single (core/get-by-field realm :contact-group :group-id group-id))
(re-frame/reg-fx existing-contacts (object/get group "contacts")]
:data-store/add-contacts-to-contact-group (aset group "contacts" (clj->js (into #{} (concat contacts
(fn [[group-id contacts]] (core/list->clj existing-contacts))))))))
(async/go (async/>! core/realm-queue #(data-store/add-contacts group-id contacts)))))

View File

@ -1,42 +1,32 @@
(ns status-im.data-store.contacts (ns status-im.data-store.contacts
(:require [cljs.core.async :as async] (:require [re-frame.core :as re-frame]
[re-frame.core :as re-frame] [status-im.data-store.realm.core :as core]))
[status-im.data-store.realm.core :as core]
[status-im.data-store.realm.contacts :as data-store])
(:refer-clojure :exclude [exists?]))
(re-frame/reg-cofx (re-frame/reg-cofx
:data-store/get-all-contacts :data-store/get-all-contacts
(fn [coeffects _] (fn [coeffects _]
(assoc coeffects :all-contacts (data-store/get-all-as-list)))) (assoc coeffects :all-contacts (-> @core/account-realm
(core/get-all :contact)
(core/all-clj :contact)))))
(defn- get-by-id (defn save-contact-tx
"Returns tx function for saving contact"
[{:keys [whisper-identity] :as contact}]
(fn [realm]
(core/create realm
:contact
(dissoc contact :command :response :subscriptions :jail-loaded-events)
(core/exists? realm :contact :whisper-identity whisper-identity))))
(defn save-contacts-tx
"Returns tx function for saving contacts"
[contacts]
(fn [realm]
(doseq [contact contacts]
((save-contact-tx contact) realm))))
(defn delete-contact-tx
"Returns tx function for deleting contact"
[whisper-identity] [whisper-identity]
(data-store/get-by-id-cljs whisper-identity)) (fn [realm]
(core/delete realm (core/single (core/get-by-field realm :contact :whisper-identity whisper-identity)))))
(defn- save
[{:keys [whisper-identity pending?] :as contact}]
(let [{pending-db? :pending?
:as contact-db} (get-by-id whisper-identity)
contact' (-> contact
(assoc :pending? (boolean (if contact-db
(if (nil? pending?) pending-db? pending?)
pending?)))
(dissoc :command :response :subscriptions :jail-loaded-events))]
(data-store/save contact' (boolean contact-db))))
(re-frame/reg-fx
:data-store/save-contact
(fn [contact]
(async/go (async/>! core/realm-queue #(save contact)))))
(re-frame/reg-fx
:data-store/save-contacts
(fn [contacts]
(doseq [contact contacts]
(async/go (async/>! core/realm-queue #(save contact))))))
(re-frame/reg-fx
:data-store/delete-contact
(fn [contact]
(async/go (async/>! core/realm-queue #(data-store/delete contact)))))

View File

@ -1,15 +1,17 @@
(ns status-im.data-store.local-storage (ns status-im.data-store.local-storage
(:require [cljs.core.async :as async] (:require [re-frame.core :as re-frame]
[re-frame.core :as re-frame] [status-im.data-store.realm.core :as core]))
[status-im.data-store.realm.core :as core]
[status-im.data-store.realm.local-storage :as data-store]))
(re-frame/reg-cofx (re-frame/reg-cofx
:data-store/get-local-storage-data :data-store/get-local-storage-data
(fn [cofx _] (fn [cofx _]
(assoc cofx :get-local-storage-data (comp :data data-store/get-by-chat-id)))) (assoc cofx :get-local-storage-data #(-> @core/account-realm
(core/get-by-field :local-storage :chat-id %)
(core/single-clj :local-storage)
:data))))
(re-frame/reg-fx (defn set-local-storage-data-tx
:data-store/set-local-storage-data "Returns tx function setting local storage data"
(fn [data] [data]
(async/go (async/>! core/realm-queue #(data-store/save data))))) (fn [realm]
(core/create realm :local-storage data true)))

View File

@ -1,22 +0,0 @@
(ns status-im.data-store.realm.accounts
(:require [status-im.data-store.realm.core :as realm]))
(defn get-all-as-list []
(->> (realm/all-clj (realm/get-all @realm/base-realm :account) :account)
(mapv #(update % :settings realm/deserialize))))
(defn get-by-address [address]
(-> @realm/base-realm
(realm/get-by-field :account :address address)
(realm/single-clj :account)
(update :settings realm/deserialize)))
(defn- create-account-fn [account update?]
#(realm/create @realm/base-realm :account account update?))
(defn save [account update?]
(realm/write @realm/base-realm
(-> account
(update :settings realm/serialize)
(update :networks vals)
(create-account-fn update?))))

View File

@ -1,29 +0,0 @@
(ns status-im.data-store.realm.browser
(:require [status-im.data-store.realm.core :as realm])
(:refer-clojure :exclude [exists?]))
(defn get-all
[]
(-> @realm/account-realm
(realm/get-all :browser)
(realm/sorted :timestamp :desc)
(realm/all-clj :browser)))
(defn save
[browser update?]
(realm/save @realm/account-realm :browser browser update?))
(defn delete
[browser-id]
(when-let [browser (realm/single (realm/get-by-field @realm/account-realm :browser :browser-id browser-id))]
(realm/write @realm/account-realm #(realm/delete @realm/account-realm browser))))
(defn exists?
[browser-id]
(realm/exists? @realm/account-realm :browser :browser-id browser-id))
(defn get-by-id
[browser-id]
(-> @realm/account-realm
(realm/get-by-field :browser :browser-id browser-id)
(realm/single-clj :browser)))

View File

@ -1,45 +0,0 @@
(ns status-im.data-store.realm.contact-groups
(:require [goog.object :as object]
[status-im.data-store.realm.core :as realm])
(:refer-clojure :exclude [exists?]))
(defn get-all-as-list
[]
(realm/all-clj (realm/get-all @realm/account-realm :contact-group) :contact-group))
(defn save
[group update?]
(realm/save @realm/account-realm :contact-group group update?))
(defn save-property
[group-id property-name value]
(realm/write @realm/account-realm
(fn []
(-> @realm/account-realm
(realm/get-by-field :contact-group :group-id group-id)
realm/single
(aset (name property-name) value)))))
(defn exists?
[group-id]
(realm/exists? @realm/account-realm :contact-group :group-id group-id))
(defn delete
[group-id]
(when-let [group (-> @realm/account-realm
(realm/get-by-field :contact-group :group-id group-id)
realm/single)]
(realm/write @realm/account-realm #(realm/delete @realm/account-realm group))))
(defn- get-by-id-obj
[group-id]
(realm/single (realm/get-by-field @realm/account-realm :contact-group :group-id group-id)))
(defn add-contacts
[group-id identities]
(let [group (get-by-id-obj group-id)
contacts (object/get group "contacts")]
(realm/write @realm/account-realm
#(aset group "contacts"
(clj->js (into #{} (concat identities
(realm/list->clj contacts))))))))

View File

@ -1,30 +0,0 @@
(ns status-im.data-store.realm.contacts
(:require [status-im.data-store.realm.core :as realm])
(:refer-clojure :exclude [exists?]))
(defn get-all-as-list
[]
(realm/all-clj (realm/get-all @realm/account-realm :contact) :contact))
(defn get-by-id
[whisper-identity]
(realm/single (realm/get-by-field @realm/account-realm :contact :whisper-identity whisper-identity)))
(defn get-by-id-cljs
[whisper-identity]
(-> @realm/account-realm
(realm/get-by-field :contact :whisper-identity whisper-identity)
(realm/single-clj :contact)))
(defn save
[contact update?]
(realm/save @realm/account-realm :contact contact update?))
(defn delete
[{:keys [whisper-identity]}]
(realm/write @realm/account-realm
#(realm/delete @realm/account-realm (get-by-id whisper-identity))))
(defn exists?
[whisper-identity]
(realm/exists? @realm/account-realm :contact :whisper-identity whisper-identity))

View File

@ -1,10 +0,0 @@
(ns status-im.data-store.realm.local-storage
(:require [status-im.data-store.realm.core :as realm]))
(defn get-by-chat-id
[chat-id]
(realm/single-clj (realm/get-by-field @realm/account-realm :local-storage :chat-id chat-id) :local-storage))
(defn save
[local-storage]
(realm/save @realm/account-realm :local-storage local-storage true))

View File

@ -1,27 +0,0 @@
(ns status-im.data-store.realm.requests
(:require [status-im.data-store.realm.core :as realm]))
(defn get-all-unanswered
[]
(-> @realm/account-realm
(realm/get-by-field :request :status "open")
(realm/all-clj :request)))
(defn save
[request]
(realm/save @realm/account-realm :request request true))
(defn- get-by-message-id
[chat-id message-id]
(-> @realm/account-realm
(realm/get-by-fields :request :and [[:chat-id chat-id]
[:message-id message-id]])
realm/single))
(defn mark-as-answered
[chat-id message-id]
(realm/write @realm/account-realm
(fn []
(-> (get-by-message-id chat-id message-id)
(.-status)
(set! "answered")))))

View File

@ -1,20 +1,25 @@
(ns status-im.data-store.requests (ns status-im.data-store.requests
(:require [cljs.core.async :as async] (:require [re-frame.core :as re-frame]
[re-frame.core :as re-frame] [status-im.data-store.realm.core :as core]))
[status-im.data-store.realm.core :as core]
[status-im.data-store.realm.requests :as data-store]))
(re-frame/reg-cofx (re-frame/reg-cofx
:data-store/get-unanswered-requests :data-store/get-unanswered-requests
(fn [cofx _] (fn [cofx _]
(assoc cofx :stored-unanswered-requests (data-store/get-all-unanswered)))) (assoc cofx :stored-unanswered-requests (-> @core/account-realm
(core/get-by-field :request :status "open")
(core/all-clj :request)))))
(re-frame/reg-fx (defn save-request-tx
:data-store/save-request "Returns tx function for saving request"
(fn [request] [request]
(async/go (async/>! core/realm-queue #(data-store/save request))))) (fn [realm]
(core/create realm :request request true)))
(re-frame/reg-fx (defn mark-request-as-answered-tx
:data-store/mark-request-as-answered "Given chat-id and message-id, returns tx function for marking request as answered"
(fn [{:keys [chat-id message-id]}] [chat-id message-id]
(async/go (async/>! core/realm-queue #(data-store/mark-as-answered chat-id message-id))))) (fn [realm]
(some-> (core/get-by-fields realm :request :and [[:chat-id chat-id]
[:message-id message-id]])
core/single
(aset "status" "answered"))))

View File

@ -17,7 +17,8 @@
[status-im.transport.message.core :as transport] [status-im.transport.message.core :as transport]
status-im.ui.screens.accounts.create.navigation status-im.ui.screens.accounts.create.navigation
[status-im.chat.models :as chat.models] [status-im.chat.models :as chat.models]
[status-im.ui.screens.accounts.utils :as accounts.utils])) [status-im.ui.screens.accounts.utils :as accounts.utils]
[status-im.data-store.accounts :as accounts-store]))
;;;; COFX ;;;; COFX
@ -55,8 +56,8 @@
:network network :network network
:networks networks :networks networks
:address address)] :address address)]
{:db (assoc-in db [:accounts/accounts address] enriched-account) {:db (assoc-in db [:accounts/accounts address] enriched-account)
:data-store/save-account enriched-account})) :data-store/base-tx [(accounts-store/save-account-tx enriched-account)]}))
;; TODO(janherich) we have this handler here only because of the tests, refactor/improve tests ASAP ;; TODO(janherich) we have this handler here only because of the tests, refactor/improve tests ASAP
(handlers/register-handler-fx (handlers/register-handler-fx
@ -103,13 +104,13 @@
(fn [{{:accounts/keys [accounts] :networks/keys [networks] :as db} :db} [_ id]] (fn [{{:accounts/keys [accounts] :networks/keys [networks] :as db} :db} [_ id]]
(let [current-account (get accounts id) (let [current-account (get accounts id)
new-account (assoc current-account :networks networks)] new-account (assoc current-account :networks networks)]
{:db (assoc-in db [:accounts/accounts id] new-account) {:db (assoc-in db [:accounts/accounts id] new-account)
:data-store/save-account new-account}))) :data-store/base-tx [(accounts-store/save-account-tx new-account)]})))
(defn update-settings [settings {{:keys [account/account] :as db} :db :as cofx}] (defn update-settings [settings {{:keys [account/account] :as db} :db :as cofx}]
(let [new-account (assoc account :settings settings)] (let [new-account (assoc account :settings settings)]
{:db (assoc db :account/account new-account) {:db (assoc db :account/account new-account)
:data-store/save-account new-account})) :data-store/base-tx [(accounts-store/save-account-tx new-account)]}))
(handlers/register-handler-fx (handlers/register-handler-fx
:send-account-update-if-needed :send-account-update-if-needed

View File

@ -1,7 +1,8 @@
(ns status-im.ui.screens.accounts.utils (ns status-im.ui.screens.accounts.utils
(:require [status-im.transport.message.core :as transport] (:require [status-im.transport.message.core :as transport]
[status-im.utils.handlers-macro :as handlers-macro] [status-im.utils.handlers-macro :as handlers-macro]
[status-im.transport.message.v1.contact :as message.contact])) [status-im.transport.message.v1.contact :as message.contact]
[status-im.data-store.accounts :as accounts-store]))
(defn account-update (defn account-update
"Takes effects (containing :db) + new account fields, adds all effects necessary for account update. "Takes effects (containing :db) + new account fields, adds all effects necessary for account update.
@ -11,8 +12,11 @@
([new-account-fields after-update-event {:keys [db] :as cofx}] ([new-account-fields after-update-event {:keys [db] :as cofx}]
(let [current-account (:account/account db) (let [current-account (:account/account db)
new-account (merge current-account new-account-fields) new-account (merge current-account new-account-fields)
fx {:db (assoc db :account/account new-account) fx {:db (assoc db :account/account new-account)
:data-store/save-account (assoc new-account :after-update-event after-update-event)} :data-store/base-tx [(accounts-store/save-account-tx
(assoc new-account
:after-update-event
after-update-event))]}
{:keys [name photo-path]} new-account] {:keys [name photo-path]} new-account]
(if (or (:name new-account-fields) (:photo-path new-account-fields)) (if (or (:name new-account-fields) (:photo-path new-account-fields))
(handlers-macro/merge-fx cofx fx (transport/send (message.contact/ContactUpdate. name photo-path) nil)) (handlers-macro/merge-fx cofx fx (transport/send (message.contact/ContactUpdate. name photo-path) nil))

View File

@ -3,7 +3,8 @@
[status-im.utils.handlers :as handlers] [status-im.utils.handlers :as handlers]
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[status-im.utils.random :as random] [status-im.utils.random :as random]
[status-im.i18n :as i18n])) [status-im.i18n :as i18n]
[status-im.data-store.browser :as browser-store]))
(handlers/register-handler-fx (handlers/register-handler-fx
:initialize-browsers :initialize-browsers
@ -28,8 +29,9 @@
(defn add-browser-fx [{:keys [db now]} browser] (defn add-browser-fx [{:keys [db now]} browser]
(let [new-browser (get-new-browser browser now)] (let [new-browser (get-new-browser browser now)]
{:db (update-in db [:browser/browsers (:browser-id new-browser)] merge new-browser) {:db (update-in db [:browser/browsers (:browser-id new-browser)]
:data-store/save-browser new-browser})) merge new-browser)
:data-store/tx [(browser-store/save-browser-tx new-browser)]}))
(handlers/register-handler-fx (handlers/register-handler-fx
:open-dapp-in-browser :open-dapp-in-browser
@ -69,5 +71,5 @@
:remove-browser :remove-browser
[re-frame/trim-v] [re-frame/trim-v]
(fn [{:keys [db]} [browser-id]] (fn [{:keys [db]} [browser-id]]
{:db (update-in db [:browser/browsers] dissoc browser-id) {:db (update-in db [:browser/browsers] dissoc browser-id)
:data-store/remove-browser browser-id})) :data-store/tx [(browser-store/remove-browser-tx browser-id)]}))

View File

@ -3,7 +3,8 @@
[status-im.utils.handlers-macro :as handlers-macro] [status-im.utils.handlers-macro :as handlers-macro]
[status-im.data-store.messages :as data-store.messages] [status-im.data-store.messages :as data-store.messages]
[status-im.chat.models :as chat.models] [status-im.chat.models :as chat.models]
[status-im.constants :as constants])) [status-im.constants :as constants]
[status-im.data-store.contacts :as contacts-store]))
(defn receive-contact-request (defn receive-contact-request
[public-key [public-key
@ -21,8 +22,10 @@
:chat-id public-key :chat-id public-key
:contact-info (prn-str contact-props)}] :contact-info (prn-str contact-props)}]
(handlers-macro/merge-fx cofx (handlers-macro/merge-fx cofx
{:db (update-in db [:contacts/contacts public-key] merge contact-props) {:db (update-in db [:contacts/contacts public-key]
:data-store/save-contact contact-props} merge contact-props)
:data-store/tx [(contacts-store/save-contact-tx
contact-props)]}
(chat.models/upsert-chat chat-props))))) (chat.models/upsert-chat chat-props)))))
(defn receive-contact-request-confirmation (defn receive-contact-request-confirmation
@ -38,14 +41,16 @@
chat-props {:name name chat-props {:name name
:chat-id public-key}] :chat-id public-key}]
(handlers-macro/merge-fx cofx (handlers-macro/merge-fx cofx
{:db (update-in db [:contacts/contacts public-key] merge contact-props) {:db (update-in db [:contacts/contacts public-key]
:data-store/save-contact contact-props} merge contact-props)
:data-store/tx [(contacts-store/save-contact-tx
contact-props)]}
(chat.models/upsert-chat chat-props))))) (chat.models/upsert-chat chat-props)))))
(defn- update-contact [{:keys [whisper-identity] :as contact} {:keys [db]}] (defn- update-contact [{:keys [whisper-identity] :as contact} {:keys [db]}]
(when (get-in db [:contacts/contacts whisper-identity]) (when (get-in db [:contacts/contacts whisper-identity])
{:db (update-in db [:contacts/contacts whisper-identity] merge contact) {:db (update-in db [:contacts/contacts whisper-identity] merge contact)
:data-store/save-contact contact})) :data-store/tx [(contacts-store/save-contact-tx contact)]}))
(defn receive-contact-update [chat-id public-key {:keys [name profile-image]} {:keys [db now] :as cofx}] (defn receive-contact-update [chat-id public-key {:keys [name profile-image]} {:keys [db now] :as cofx}]
(let [{:keys [chats current-public-key]} db] (let [{:keys [chats current-public-key]} db]

View File

@ -16,14 +16,15 @@
[status-im.chat.models :as chat.models] [status-im.chat.models :as chat.models]
[status-im.transport.message.core :as transport] [status-im.transport.message.core :as transport]
[status-im.transport.message.v1.contact :as message.v1.contact] [status-im.transport.message.v1.contact :as message.v1.contact]
[status-im.ui.screens.add-new.new-chat.db :as new-chat.db])) [status-im.ui.screens.add-new.new-chat.db :as new-chat.db]
[status-im.data-store.contacts :as contacts-store]))
;;;; Handlers ;;;; Handlers
(defn- update-contact [{:keys [whisper-identity] :as contact} {:keys [db]}] (defn- update-contact [{:keys [whisper-identity] :as contact} {:keys [db]}]
(when (get-in db [:contacts/contacts whisper-identity]) (when (get-in db [:contacts/contacts whisper-identity])
{:db (update-in db [:contacts/contacts whisper-identity] merge contact) {:db (update-in db [:contacts/contacts whisper-identity] merge contact)
:data-store/save-contact contact})) :data-store/tx [(contacts-store/save-contact-tx contact)]}))
(handlers/register-handler-fx (handlers/register-handler-fx
:load-contacts :load-contacts
@ -35,10 +36,11 @@
(defn- add-new-contact [{:keys [whisper-identity] :as contact} {:keys [db]}] (defn- add-new-contact [{:keys [whisper-identity] :as contact} {:keys [db]}]
(let [new-contact (assoc contact :pending? false)] (let [new-contact (assoc contact :pending? false)]
{:db (-> db {:db (-> db
(update-in [:contacts/contacts whisper-identity] merge new-contact) (update-in [:contacts/contacts whisper-identity]
(assoc-in [:contacts/new-identity] "")) merge new-contact)
:data-store/save-contact new-contact})) (assoc-in [:contacts/new-identity] ""))
:data-store/tx [(contacts-store/save-contact-tx new-contact)]}))
(defn- own-info [db] (defn- own-info [db]
(let [{:keys [name photo-path address]} (:account/account db) (let [{:keys [name photo-path address]} (:account/account db)
@ -103,8 +105,8 @@
:remove-contact :remove-contact
(fn [{:keys [db]} [_ whisper-identity]] (fn [{:keys [db]} [_ whisper-identity]]
(when-let [contact (get-in db [:contacts/contacts whisper-identity])] (when-let [contact (get-in db [:contacts/contacts whisper-identity])]
{:db (update db :contacts/contacts dissoc whisper-identity) {:db (update db :contacts/contacts dissoc whisper-identity)
:data-store/delete-contact contact}))) :data-store/tx [(contacts-store/delete-contact-tx whisper-identity)]})))
(handlers/register-handler-db (handlers/register-handler-db
:open-contact-toggle-list :open-contact-toggle-list

View File

@ -3,7 +3,8 @@
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[status-im.utils.js-resources :as js-res] [status-im.utils.js-resources :as js-res]
[status-im.utils.handlers :as handlers] [status-im.utils.handlers :as handlers]
[status-im.ui.screens.group.navigation])) [status-im.ui.screens.group.navigation]
[status-im.data-store.contact-groups :as contact-groups-store]))
;;;; COFX ;;;; COFX
@ -45,8 +46,8 @@
:order (count contact-groups) :order (count contact-groups)
:timestamp now :timestamp now
:contacts selected-contacts}] :contacts selected-contacts}]
{:db (assoc-in db [:group/contact-groups group-id] new-group) {:db (assoc-in db [:group/contact-groups group-id] new-group)
:data-store/save-contact-group new-group}))) :data-store/tx [(contact-groups-store/save-contact-group-tx new-group)]})))
(defn add-default-groups (defn add-default-groups
[{:keys [db now default-groups]}] [{:keys [db now default-groups]}]
@ -62,8 +63,9 @@
existing-groups (:group/contact-groups db) existing-groups (:group/contact-groups db)
groups-to-add (select-keys new-groups (set/difference (set (keys new-groups)) groups-to-add (select-keys new-groups (set/difference (set (keys new-groups))
(set (keys existing-groups))))] (set (keys existing-groups))))]
{:db (update db :group/contact-groups merge groups-to-add) {:db (update db :group/contact-groups merge groups-to-add)
:data-store/save-contact-groups (vals groups-to-add)})) :data-store/tx [(contact-groups-store/save-contact-groups-tx
(vals groups-to-add))]}))
(handlers/register-handler-fx (handlers/register-handler-fx
:load-contact-groups :load-contact-groups
@ -74,18 +76,21 @@
(handlers/register-handler-fx (handlers/register-handler-fx
:set-contact-group-name :set-contact-group-name
(fn [{{:keys [new-chat-name] :group/keys [contact-group-id] :as db} :db} _] (fn [{{:keys [new-chat-name] :group/keys [contact-group-id] :as db} :db} _]
{:db (assoc-in db {:db (assoc-in db
[:group/contact-groups contact-group-id :name] [:group/contact-groups contact-group-id :name]
new-chat-name) new-chat-name)
:data-store/save-contact-group-property [contact-group-id :name new-chat-name]})) :data-store/tx [(contact-groups-store/save-contact-group-tx
{:group-id contact-group-id
:name new-chat-name})]}))
(handlers/register-handler-fx (handlers/register-handler-fx
:add-selected-contacts-to-group :add-selected-contacts-to-group
(fn [{{:group/keys [contact-groups contact-group-id selected-contacts] :as db} :db} _] (fn [{{:group/keys [contact-groups contact-group-id selected-contacts] :as db} :db} _]
{:db (update-in db {:db (update-in db
[:group/contact-groups contact-group-id :contacts] [:group/contact-groups contact-group-id :contacts]
#(into [] (set (concat % selected-contacts)))) #(into [] (set (concat % selected-contacts))))
:data-store/add-contacts-to-contact-group [contact-group-id selected-contacts]})) :data-store/tx [(contact-groups-store/add-contacts-to-contact-group-tx
contact-group-id selected-contacts)]}))
(handlers/register-handler-fx (handlers/register-handler-fx
:remove-contact-from-group :remove-contact-from-group
@ -94,5 +99,5 @@
(let [group (-> db (let [group (-> db
(get-in [:group/contact-groups group-id]) (get-in [:group/contact-groups group-id])
(update :contacts (partial remove #(= whisper-identity %))))] (update :contacts (partial remove #(= whisper-identity %))))]
{:db (assoc-in db [:group/contact-groups group-id] group) {:db (assoc-in db [:group/contact-groups group-id] group)
:data-store/save-contact-group group}))) :data-store/tx [(contact-groups-store/save-contact-group-tx group)]})))