[refs #3210] replace contacts/get-by-id with app-db read

Signed-off-by: Dmitry Novotochinov <trybeee@gmail.com>
This commit is contained in:
Dmitry Novotochinov 2018-02-05 16:40:28 +03:00
parent d799396851
commit 65a2f440c6
No known key found for this signature in database
GPG Key ID: 267674DCC86628D9
2 changed files with 30 additions and 35 deletions

View File

@ -108,9 +108,9 @@
(re-frame/reg-fx (re-frame/reg-fx
::participant-removed-from-group-message ::participant-removed-from-group-message
(fn [{:keys [identity from message-id timestamp group-id]}] (fn [{:keys [identity from message-id timestamp group-id contacts]}]
(let [remover-name (:name (contacts/get-by-id from)) (let [remover-name (get-in contacts [from :name])
removed-name (:name (contacts/get-by-id identity)) removed-name (get-in contacts [identity :name])
message (->> [(or remover-name from) (i18n/label :t/removed) (or removed-name identity)] message (->> [(or remover-name from) (i18n/label :t/removed) (or removed-name identity)]
(string/join " ") (string/join " ")
(system-message message-id timestamp)) (system-message message-id timestamp))
@ -127,17 +127,10 @@
(fn [[group-id identity]] (fn [[group-id identity]]
(chats/remove-contacts group-id [identity]))) (chats/remove-contacts group-id [identity])))
(defn you-removed-from-group-message
[from {:keys [message-id timestamp]}]
(let [remover-name (:name (contacts/get-by-id from))]
(->> [(or remover-name from) (i18n/label :t/removed-from-chat)]
(string/join " ")
(system-message message-id timestamp))))
(re-frame/reg-fx (re-frame/reg-fx
::you-removed-from-group-message ::you-removed-from-group-message
(fn [{:keys [from message-id timestamp group-id]}] (fn [{:keys [from message-id timestamp group-id contacts]}]
(let [remover-name (:name (contacts/get-by-id from)) (let [remover-name (get-in contacts [from :name])
message (->> [(or remover-name from) (i18n/label :t/removed-from-chat)] message (->> [(or remover-name from) (i18n/label :t/removed-from-chat)]
(string/join " ") (string/join " ")
(system-message message-id timestamp)) (system-message message-id timestamp))
@ -151,8 +144,8 @@
(re-frame/reg-fx (re-frame/reg-fx
::participant-left-group-message ::participant-left-group-message
(fn [{:keys [chat-id from message-id timestamp]}] (fn [{:keys [chat-id from message-id timestamp contacts]}]
(let [left-name (:name (contacts/get-by-id from)) (let [left-name (get-in contacts [from :name])
message-text (str (or left-name from) " " (i18n/label :t/left))] message-text (str (or left-name from) " " (i18n/label :t/left))]
(-> (system-message message-id timestamp message-text) (-> (system-message message-id timestamp message-text)
(assoc :chat-id chat-id) (assoc :chat-id chat-id)
@ -160,11 +153,11 @@
(re-frame/reg-fx (re-frame/reg-fx
::participant-invited-to-group-message ::participant-invited-to-group-message
(fn [{:keys [group-id current-identity identity from message-id timestamp]}] (fn [{:keys [group-id current-identity identity from message-id timestamp contacts]}]
(let [inviter-name (:name (contacts/get-by-id from)) (let [inviter-name (get-in contacts [from :name])
invitee-name (if (= identity current-identity) invitee-name (if (= identity current-identity)
(i18n/label :t/You) (i18n/label :t/You)
(:name (contacts/get-by-id identity)))] (get-in contacts [identity :name]))]
(re-frame/dispatch (re-frame/dispatch
[:chat-received-message/add [:chat-received-message/add
{:from "system" {:from "system"
@ -536,7 +529,7 @@
(handlers/register-handler-fx (handlers/register-handler-fx
:participant-invited-to-group :participant-invited-to-group
[re-frame/trim-v] [re-frame/trim-v]
(fn [{{:keys [current-public-key chats] :as db} :db} (fn [{{:keys [current-public-key chats contacts/contacts] :as db} :db}
[{:keys [from] [{:keys [from]
{:keys [group-id identity message-id timestamp]} :payload}]] {:keys [group-id identity message-id timestamp]} :payload}]]
(let [chat (get-in db [:chats group-id]) (let [chat (get-in db [:chats group-id])
@ -544,7 +537,7 @@
(when (= from admin) (when (= from admin)
(merge {::participant-invited-to-group-message {:group-id group-id :current-public-key current-public-key (merge {::participant-invited-to-group-message {:group-id group-id :current-public-key current-public-key
:identity identity :from from :message-id message-id :identity identity :from from :message-id message-id
:timestamp timestamp}} :timestamp timestamp :contacts contacts}}
(when-not (and (= current-public-key identity) (has-contact? chat identity)) (when-not (and (= current-public-key identity) (has-contact? chat identity))
{:db (update-in db [:chats group-id :contacts] conj {:identity identity}) {:db (update-in db [:chats group-id :contacts] conj {:identity identity})
::chats-add-contact [group-id identity]})))))) ::chats-add-contact [group-id identity]}))))))
@ -552,7 +545,7 @@
(handlers/register-handler-fx (handlers/register-handler-fx
::you-removed-from-group ::you-removed-from-group
[re-frame/trim-v] [re-frame/trim-v]
(fn [{{:keys [web3] :as db} :db} (fn [{{:keys [web3 contacts/contacts] :as db} :db}
[{:keys [from] [{:keys [from]
{:keys [group-id timestamp message-id] :as payload} :payload}]] {:keys [group-id timestamp message-id] :as payload} :payload}]]
(let [chat (get-in db [:chats group-id]) (let [chat (get-in db [:chats group-id])
@ -561,7 +554,8 @@
{::you-removed-from-group-message {:from from {::you-removed-from-group-message {:from from
:message-id message-id :message-id message-id
:timestamp timestamp :timestamp timestamp
:group-id group-id} :group-id group-id
:contacts contacts}
::stop-watching-group! {:web3 web3 ::stop-watching-group! {:web3 web3
:group-id group-id} :group-id group-id}
:dispatch [:update-chat! {:chat-id group-id :dispatch [:update-chat! {:chat-id group-id
@ -572,7 +566,7 @@
:participant-removed-from-group :participant-removed-from-group
[re-frame/trim-v [re-frame/trim-v
(re-frame/inject-cofx ::message-get-by-id)] (re-frame/inject-cofx ::message-get-by-id)]
(fn [{{:keys [current-public-key chats]} :db message-by-id :message-by-id} (fn [{{:keys [current-public-key chats contacts/contacts]} :db message-by-id :message-by-id}
[{:keys [from] [{:keys [from]
{:keys [group-id identity message-id timestamp]} :payload {:keys [group-id identity message-id timestamp]} :payload
:as message}]] :as message}]]
@ -582,13 +576,13 @@
(if (= current-public-key identity) (if (= current-public-key identity)
{:dispatch [::you-removed-from-group message]} {:dispatch [::you-removed-from-group message]}
{::participant-removed-from-group-message {:identity identity :from from :message-id message-id {::participant-removed-from-group-message {:identity identity :from from :message-id message-id
:timestamp timestamp :group-id group-id} :timestamp timestamp :group-id group-id :contacts contacts}
::chats-remove-contact [group-id identity]})))))) ::chats-remove-contact [group-id identity]}))))))
(handlers/register-handler-fx (handlers/register-handler-fx
:participant-left-group :participant-left-group
[re-frame/trim-v] [re-frame/trim-v]
(fn [{{:keys [current-public-key] :as db} :db} (fn [{{:keys [current-public-key contacts/contacts] :as db} :db}
[{:keys [from] [{:keys [from]
{:keys [group-id timestamp message-id]} :payload}]] {:keys [group-id timestamp message-id]} :payload}]]
(let [chat (get-in db [:chats group-id]) (let [chat (get-in db [:chats group-id])
@ -599,7 +593,8 @@
{::participant-left-group-message {:chat-id group-id {::participant-left-group-message {:chat-id group-id
:from from :from from
:message-id message-id :message-id message-id
:timestamp timestamp} :timestamp timestamp
:contacts contacts}
::chats-remove-contact [group-id from] ::chats-remove-contact [group-id from]
:db (update-in db [:chats group-id :contacts] :db (update-in db [:chats group-id :contacts]
#(remove (fn [{:keys [identity]}] #(remove (fn [{:keys [identity]}]

View File

@ -2,8 +2,8 @@
(:require [re-frame.core :refer [dispatch reg-fx]] (:require [re-frame.core :refer [dispatch reg-fx]]
[status-im.utils.handlers :refer [register-handler-fx]] [status-im.utils.handlers :refer [register-handler-fx]]
[status-im.protocol.core :as protocol] [status-im.protocol.core :as protocol]
[status-im.utils.random :as random] [status-im.utils.random :as random]
[status-im.data-store.contacts :as contacts] [status-im.chat.handlers :as chat-events]
[status-im.data-store.messages :as messages] [status-im.data-store.messages :as messages]
[status-im.data-store.chats :as chats] [status-im.data-store.chats :as chats]
[status-im.constants :refer [text-content-type]])) [status-im.constants :refer [text-content-type]]))
@ -33,18 +33,18 @@
:content content :content content
:content-type text-content-type}) :content-type text-content-type})
(defn removed-participant-message [chat-id identity] (defn removed-participant-message [chat-id identity contact-name]
(let [contact-name (:name (contacts/get-by-id identity)) (let [message-text (str "You've removed " (or contact-name identity))]
message-text (str "You've removed " (or contact-name identity))]
(-> (system-message (random/id) message-text) (-> (system-message (random/id) message-text)
(assoc :chat-id chat-id) (assoc :chat-id chat-id)
(messages/save)))) (messages/save))))
(reg-fx (reg-fx
::create-removing-messages ::create-removing-messages
(fn [{:keys [current-chat-id participants]}] (fn [{:keys [current-chat-id participants contacts/contacts]}]
(doseq [participant participants] (doseq [participant participants]
(removed-participant-message current-chat-id participant)))) (let [contact-name (get-in contacts [participant :name])]
(removed-participant-message current-chat-id participant contact-name)))))
(reg-fx (reg-fx
::notify-about-new-members ::notify-about-new-members
@ -160,7 +160,7 @@
::notify-about-removing (merge {:participants participants} ::notify-about-removing (merge {:participants participants}
(select-keys db [:web3 :current-chat-id :chats :current-public-key])) (select-keys db [:web3 :current-chat-id :chats :current-public-key]))
::create-removing-messages (merge {:participants participants} ::create-removing-messages (merge {:participants participants}
(select-keys db [:current-chat-id]))})) (select-keys db [:current-chat-id :contacts/contacts]))}))
(register-handler-fx (register-handler-fx
:set-chat-name :set-chat-name