Effects should be dumb, without any logic

This commit is contained in:
janherich 2018-02-06 13:16:21 +01:00
parent 8b4b083b50
commit 86aea8a623
No known key found for this signature in database
GPG Key ID: C23B473AFBE94D13
8 changed files with 70 additions and 50 deletions

View File

@ -83,19 +83,14 @@
(async/go (async/>! realm-queue #(messages-store/save message)))))
(re-frame/reg-fx
:delete-chat-messages
(fn [{:keys [chat-id group-chat debug?]}]
(when (or group-chat debug?)
(async/go (async/>! realm-queue #(messages-store/delete-by-chat-id chat-id))))
(async/go (async/>! realm-queue #(pending-messages-store/delete-all-by-chat-id chat-id)))))
:delete-messages
(fn [chat-id]
(async/go (async/>! realm-queue #(messages-store/delete-by-chat-id chat-id)))))
(re-frame/reg-fx
:update-message-overhead
(fn [[chat-id network-status]]
(let [update-fn (if (= network-status :offline)
chats-store/inc-message-overhead
chats-store/reset-message-overhead)]
(async/go (async/>! realm-queue #(update-fn chat-id))))))
:delete-pending-messages
(fn [chat-id]
(async/go (async/>! realm-queue #(pending-messages-store/delete-all-by-chat-id chat-id)))))
(re-frame/reg-fx
:save-chat
@ -103,16 +98,14 @@
(async/go (async/>! realm-queue #(chats-store/save chat)))))
(re-frame/reg-fx
:delete-chat
(fn [{:keys [chat-id debug?]}]
(if debug?
(async/go (async/>! realm-queue #(chats-store/delete chat-id)))
(async/go (async/>! realm-queue #(chats-store/set-inactive chat-id))))))
:deactivate-chat
(fn [chat-id]
(async/go (async/>! realm-queue #(chats-store/set-inactive chat-id)))))
(re-frame/reg-fx
:save-all-contacts
(fn [contacts]
(contacts-store/save-all contacts)))
:delete-chat
(fn [chat-id]
(async/go (async/>! realm-queue #(chats-store/delete chat-id)))))
(re-frame/reg-fx
:protocol-send-seen
@ -354,9 +347,14 @@
:remove-chat
[re-frame/trim-v]
(fn [{:keys [db]} [chat-id]]
(let [chat (get-in db [:chats chat-id])]
{:db (-> db
(update :chats dissoc chat-id)
(update :deleted-chats (fnil conj #{}) chat-id))
:delete-chat chat
:delete-chat-messages chat})))
(let [{:keys [chat-id group-chat debug?]} (get-in db [:chats chat-id])]
(cond-> {:db (-> db
(update :chats dissoc chat-id)
(update :deleted-chats (fnil conj #{}) chat-id))
:delete-pending-messages chat-id}
(or group-chat debug?)
(assoc :delete-messages chat-id)
debug?
(assoc :delete-chat chat-id)
(not debug?)
(assoc :deactivate-chat chat-id)))))

View File

@ -219,9 +219,8 @@
(let [chat (get-in db [:chats chat-id])
message (prepare-message params chat)
params' (assoc params :message message)
fx {:db (add-message-to-db db chat-id message true)
:update-message-overhead [chat-id network-status]
:save-message message}]
fx {:db (add-message-to-db db chat-id message true)
:save-message message}]
(-> (merge fx (chat-model/upsert-chat (assoc fx :now now)
{:chat-id chat-id}))
(as-> fx'
@ -283,8 +282,7 @@
params' (assoc params :command command')
fx {:db (-> (merge db (:db result))
(add-message-to-db chat-id command' true))
:update-message-overhead [chat-id network-status]
(add-message-to-db chat-id command' true))
:save-message (-> command'
(assoc :chat-id chat-id)
(update-in [:content :params]

View File

@ -62,10 +62,6 @@
[chat-id]
(get-property chat-id :removed-at))
(defn get-message-overhead
[chat-id]
(get-property chat-id :message-overhead))
(defn get-active-group-chats
[]
(data-store/get-active-group-chats))
@ -74,14 +70,6 @@
[chat-id active?]
(save-property chat-id :is-active active?))
(defn inc-message-overhead
[chat-id]
(save-property chat-id :message-overhead (inc (get-message-overhead chat-id))))
(defn reset-message-overhead
[chat-id]
(save-property chat-id :message-overhead 0))
(defn new-update?
[timestamp chat-id]
(let

View File

@ -0,0 +1,37 @@
(ns status-im.data-store.realm.schemas.account.v21.chat
(:require [status-im.ui.components.styles :refer [default-chat-color]]))
(def schema {:name :chat
:primaryKey :chat-id
:properties {:chat-id :string
:name :string
:color {:type :string
:default default-chat-color}
:group-chat {:type :bool
:indexed true}
:group-admin {:type :string
:optional true}
:is-active :bool
:timestamp :int
:contacts {:type :list
:objectType :chat-contact}
:unremovable? {:type :bool
:default false}
:removed-at {:type :int
:optional true}
:removed-from-at {:type :int
:optional true}
:added-to-at {:type :int
:optional true}
:updated-at {:type :int
:optional true}
:public-key {:type :string
:optional true}
:private-key {:type :string
:optional true}
:contact-info {:type :string
:optional true}
:debug? {:type :bool
:default false}
:public? {:type :bool
:default false}}})

View File

@ -1,5 +1,5 @@
(ns status-im.data-store.realm.schemas.account.v21.core
(:require [status-im.data-store.realm.schemas.account.v19.chat :as chat]
(:require [status-im.data-store.realm.schemas.account.v21.chat :as chat]
[status-im.data-store.realm.schemas.account.v1.chat-contact :as chat-contact]
[status-im.data-store.realm.schemas.account.v19.contact :as contact]
[status-im.data-store.realm.schemas.account.v20.discover :as discover]

View File

@ -84,7 +84,7 @@
(contacts/save contact)))
(reg-fx
::save-contacts!
:save-all-contacts
(fn [new-contacts]
(contacts/save-all new-contacts)))
@ -223,8 +223,8 @@
;;(remove #(identities (:whisper-identity %)))
(map #(vector (:whisper-identity %) %))
(into {}))
fx {:db (update db :contacts/contacts merge new-contacts')
::save-contacts! (vals new-contacts')}]
fx {:db (update db :contacts/contacts merge new-contacts')
:save-all-contacts (vals new-contacts')}]
(transduce (map second)
(completing (partial loading-events/load-commands (assoc cofx :db (:db fx))))
fx

View File

@ -2,8 +2,7 @@
(:require [re-frame.core :refer [dispatch reg-fx]]
[status-im.utils.handlers :refer [register-handler-fx]]
[status-im.protocol.core :as protocol]
[status-im.utils.random :as random]
[status-im.chat.handlers :as chat-events]
[status-im.utils.random :as random]
[status-im.data-store.contacts :as contacts]
[status-im.data-store.messages :as messages]
[status-im.data-store.chats :as chats]
@ -173,4 +172,4 @@
:clear-history
(fn [{{:keys [current-chat-id] :as db} :db} _]
{:db (assoc-in db [:chats current-chat-id :messages] {})
::chat-events/delete-messages current-chat-id}))
:delete-messages current-chat-id}))

View File

@ -64,7 +64,7 @@
(defn test-fixtures []
(rf/reg-fx ::events/init-store #())
(rf/reg-fx ::contacts-events/save-contacts! #())
(rf/reg-fx :save-all-contacts #())
(rf/reg-fx ::contacts-events/save-contact #())
(rf/reg-fx ::contacts-events/watch-contact #())
(rf/reg-fx ::contacts-events/stop-watching-contact #())