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

View File

@ -1,6 +1,6 @@
(ns status-im.chat.events.requests
(:require [re-frame.core :as re-frame]
[status-im.constants :as constants]))
(:require [status-im.constants :as constants]
[status-im.data-store.requests :as requests-store]))
;; Functions
@ -8,9 +8,8 @@
"Takes chat-id, message-id and cofx, returns fx necessary data for marking request as answered"
[chat-id message-id {:keys [db]}]
(when message-id
{:db (update-in db [:chats chat-id :requests] dissoc message-id)
:data-store/mark-request-as-answered {:chat-id chat-id
:message-id message-id}}))
{:db (update-in db [:chats chat-id :requests] dissoc message-id)
:data-store/tx [(requests-store/mark-request-as-answered-tx chat-id message-id)]}))
(defn add-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
:response (:request-command content)
:status "open"}]
{:db (assoc-in db [:chats chat-id :requests message-id] request)
:data-store/save-request request}))))
{:db (assoc-in db [:chats chat-id :requests message-id] 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.constants :refer [console-chat-id]]
[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!
[_ [chat-id
@ -105,5 +106,6 @@
:set-local-storage
[trim-v]
(fn [_ [{:keys [data chat-id]}]]
{:data-store/set-local-storage-data {:chat-id chat-id
:data data}}))
{:data-store/tx [(local-storage-store/set-local-storage-data-tx
{:chat-id chat-id
:data data})]}))

View File

@ -1,24 +1,31 @@
(ns status-im.data-store.accounts
(:require [cljs.core.async :as async]
[re-frame.core :as re-frame]
[status-im.data-store.realm.core :as core]
[status-im.data-store.realm.accounts :as data-store]))
(:require [re-frame.core :as re-frame]
[status-im.data-store.realm.core :as core]))
;; TODO janherich: define as cofx once debug handlers are refactored
(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
:data-store/get-all-accounts
(fn [coeffects _]
(assoc coeffects :all-accounts (data-store/get-all-as-list))))
(re-frame/reg-fx
:data-store/save-account
(fn [{:keys [after-update-event] :as account}]
(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)))))))
(assoc coeffects :all-accounts (-> @core/base-realm
(core/get-all :account)
(core/all-clj :account)
(as-> accounts
(map #(update % :settings core/deserialize) accounts))))))
(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
(:require [cljs.core.async :as async]
[re-frame.core :as re-frame]
[status-im.data-store.realm.core :as core]
[status-im.data-store.realm.browser :as data-store])
(:refer-clojure :exclude [exists?]))
(:require [re-frame.core :as re-frame]
[status-im.data-store.realm.core :as core]))
(re-frame/reg-cofx
:data-store/all-browsers
(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
:data-store/save-browser
(fn [{:keys [browser-id] :as browser}]
(async/go (async/>! core/realm-queue #(data-store/save browser (data-store/exists? browser-id))))))
(defn save-browser-tx
"Returns tx function for saving browser"
[{:keys [browser-id] :as browser}]
(fn [realm]
(core/create realm :browser browser (core/exists? realm :browser :browser-id browser-id))))
(re-frame/reg-fx
:data-store/remove-browser
(fn [browser-id]
(async/go (async/>! core/realm-queue #(data-store/delete browser-id)))))
(defn remove-browser-tx
"Returns tx function for removing browser"
[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
(:require [cljs.core.async :as async]
(:require [goog.object :as object]
[re-frame.core :as re-frame]
[status-im.data-store.realm.core :as core]
[status-im.data-store.realm.contact-groups :as data-store])
(:refer-clojure :exclude [exists?]))
[status-im.data-store.realm.core :as core]))
(re-frame/reg-cofx
:data-store/get-all-contact-groups
(fn [cofx _]
(assoc cofx :all-contact-groups (into {}
(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
:data-store/save-contact-group
(fn [{:keys [group-id] :as group}]
(async/go (async/>! core/realm-queue #(data-store/save group (data-store/exists? group-id))))))
(defn save-contact-group-tx
"Returns tx function for saving contact group"
[{:keys [group-id] :as group}]
(fn [realm]
(core/create realm :contact-group group (core/exists? realm :contact-group :group-id group-id))))
(re-frame/reg-fx
:data-store/save-contact-groups
(fn [groups]
(doseq [{:keys [group-id] :as group} groups]
(async/go (async/>! core/realm-queue #(data-store/save group (data-store/exists? group-id)))))))
(defn save-contact-groups-tx
"Returns tx function for saving contact groups"
[groups]
(fn [realm]
(doseq [group groups]
((save-contact-group-tx group) realm))))
(re-frame/reg-fx
:data-store/save-contact-group-property
(fn [[group-id property-name value]]
(async/go (async/>! core/realm-queue #(data-store/save-property group-id property-name value)))))
(re-frame/reg-fx
:data-store/add-contacts-to-contact-group
(fn [[group-id contacts]]
(async/go (async/>! core/realm-queue #(data-store/add-contacts group-id contacts)))))
(defn add-contacts-to-contact-group-tx
"Returns tx function for adding contacts to contact group"
[group-id contacts]
(fn [realm]
(let [group (core/single (core/get-by-field realm :contact-group :group-id group-id))
existing-contacts (object/get group "contacts")]
(aset group "contacts" (clj->js (into #{} (concat contacts
(core/list->clj existing-contacts))))))))

View File

@ -1,42 +1,32 @@
(ns status-im.data-store.contacts
(:require [cljs.core.async :as async]
[re-frame.core :as re-frame]
[status-im.data-store.realm.core :as core]
[status-im.data-store.realm.contacts :as data-store])
(:refer-clojure :exclude [exists?]))
(:require [re-frame.core :as re-frame]
[status-im.data-store.realm.core :as core]))
(re-frame/reg-cofx
:data-store/get-all-contacts
(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]
(data-store/get-by-id-cljs 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)))))
(fn [realm]
(core/delete realm (core/single (core/get-by-field realm :contact :whisper-identity whisper-identity)))))

View File

@ -1,15 +1,17 @@
(ns status-im.data-store.local-storage
(:require [cljs.core.async :as async]
[re-frame.core :as re-frame]
[status-im.data-store.realm.core :as core]
[status-im.data-store.realm.local-storage :as data-store]))
(:require [re-frame.core :as re-frame]
[status-im.data-store.realm.core :as core]))
(re-frame/reg-cofx
:data-store/get-local-storage-data
(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
:data-store/set-local-storage-data
(fn [data]
(async/go (async/>! core/realm-queue #(data-store/save data)))))
(defn set-local-storage-data-tx
"Returns tx function setting local storage data"
[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
(:require [cljs.core.async :as async]
[re-frame.core :as re-frame]
[status-im.data-store.realm.core :as core]
[status-im.data-store.realm.requests :as data-store]))
(:require [re-frame.core :as re-frame]
[status-im.data-store.realm.core :as core]))
(re-frame/reg-cofx
:data-store/get-unanswered-requests
(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
:data-store/save-request
(fn [request]
(async/go (async/>! core/realm-queue #(data-store/save request)))))
(defn save-request-tx
"Returns tx function for saving request"
[request]
(fn [realm]
(core/create realm :request request true)))
(re-frame/reg-fx
:data-store/mark-request-as-answered
(fn [{:keys [chat-id message-id]}]
(async/go (async/>! core/realm-queue #(data-store/mark-as-answered chat-id message-id)))))
(defn mark-request-as-answered-tx
"Given chat-id and message-id, returns tx function for marking request 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.ui.screens.accounts.create.navigation
[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
@ -55,8 +56,8 @@
:network network
:networks networks
:address address)]
{:db (assoc-in db [:accounts/accounts address] enriched-account)
:data-store/save-account enriched-account}))
{:db (assoc-in db [:accounts/accounts address] 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
(handlers/register-handler-fx
@ -103,13 +104,13 @@
(fn [{{:accounts/keys [accounts] :networks/keys [networks] :as db} :db} [_ id]]
(let [current-account (get accounts id)
new-account (assoc current-account :networks networks)]
{:db (assoc-in db [:accounts/accounts id] new-account)
:data-store/save-account new-account})))
{:db (assoc-in db [:accounts/accounts id] 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}]
(let [new-account (assoc account :settings settings)]
{:db (assoc db :account/account new-account)
:data-store/save-account new-account}))
{:db (assoc db :account/account new-account)
:data-store/base-tx [(accounts-store/save-account-tx new-account)]}))
(handlers/register-handler-fx
:send-account-update-if-needed

View File

@ -1,7 +1,8 @@
(ns status-im.ui.screens.accounts.utils
(:require [status-im.transport.message.core :as transport]
[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
"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}]
(let [current-account (:account/account db)
new-account (merge current-account new-account-fields)
fx {:db (assoc db :account/account new-account)
:data-store/save-account (assoc new-account :after-update-event after-update-event)}
fx {:db (assoc db :account/account new-account)
:data-store/base-tx [(accounts-store/save-account-tx
(assoc new-account
:after-update-event
after-update-event))]}
{:keys [name photo-path]} new-account]
(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))

View File

@ -3,7 +3,8 @@
[status-im.utils.handlers :as handlers]
[re-frame.core :as re-frame]
[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
:initialize-browsers
@ -28,8 +29,9 @@
(defn add-browser-fx [{:keys [db now]} browser]
(let [new-browser (get-new-browser browser now)]
{:db (update-in db [:browser/browsers (:browser-id new-browser)] merge new-browser)
:data-store/save-browser new-browser}))
{:db (update-in db [:browser/browsers (:browser-id new-browser)]
merge new-browser)
:data-store/tx [(browser-store/save-browser-tx new-browser)]}))
(handlers/register-handler-fx
:open-dapp-in-browser
@ -69,5 +71,5 @@
:remove-browser
[re-frame/trim-v]
(fn [{:keys [db]} [browser-id]]
{:db (update-in db [:browser/browsers] dissoc browser-id)
:data-store/remove-browser browser-id}))
{:db (update-in db [:browser/browsers] dissoc 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.data-store.messages :as data-store.messages]
[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
[public-key
@ -21,8 +22,10 @@
:chat-id public-key
:contact-info (prn-str contact-props)}]
(handlers-macro/merge-fx cofx
{:db (update-in db [:contacts/contacts public-key] merge contact-props)
:data-store/save-contact contact-props}
{:db (update-in db [:contacts/contacts public-key]
merge contact-props)
:data-store/tx [(contacts-store/save-contact-tx
contact-props)]}
(chat.models/upsert-chat chat-props)))))
(defn receive-contact-request-confirmation
@ -38,14 +41,16 @@
chat-props {:name name
:chat-id public-key}]
(handlers-macro/merge-fx cofx
{:db (update-in db [:contacts/contacts public-key] merge contact-props)
:data-store/save-contact contact-props}
{:db (update-in db [:contacts/contacts public-key]
merge contact-props)
:data-store/tx [(contacts-store/save-contact-tx
contact-props)]}
(chat.models/upsert-chat chat-props)))))
(defn- update-contact [{:keys [whisper-identity] :as contact} {:keys [db]}]
(when (get-in db [:contacts/contacts whisper-identity])
{:db (update-in db [:contacts/contacts whisper-identity] merge contact)
:data-store/save-contact contact}))
{:db (update-in db [:contacts/contacts whisper-identity] merge 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}]
(let [{:keys [chats current-public-key]} db]

View File

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

View File

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