From edd4df85f95b42a459b9b189918c3b3f76102e3f Mon Sep 17 00:00:00 2001 From: Goran Jovic Date: Tue, 7 Nov 2017 15:13:30 +0100 Subject: [PATCH] bug #2381 - updated statuses are broadcast --- src/status_im/ui/screens/accounts/events.cljs | 11 ------ src/status_im/ui/screens/discover/events.cljs | 39 +++++++++++-------- src/status_im/ui/screens/profile/events.cljs | 24 +++++++++--- test/cljs/status_im/test/profile/events.cljs | 17 +++++++- 4 files changed, 57 insertions(+), 34 deletions(-) diff --git a/src/status_im/ui/screens/accounts/events.cljs b/src/status_im/ui/screens/accounts/events.cljs index 2939681a08..e9973c87d6 100644 --- a/src/status_im/ui/screens/accounts/events.cljs +++ b/src/status_im/ui/screens/accounts/events.cljs @@ -150,17 +150,6 @@ {:db (assoc-in db [:accounts/accounts id] new-account) ::save-account new-account}))) -(handlers/register-handler-fx - :check-status-change - (fn [{{:accounts/keys [accounts current-account-id]} :db} [_ status]] - (let [{old-status :status} (get accounts current-account-id) - status-updated? (and (not= status nil) - (not= status old-status))] - (when status-updated? - (let [hashtags (handlers/get-hashtags status)] - (when (seq hashtags) - {:dispatch [:broadcast-status status hashtags]})))))) - (defn account-update "Takes effects (containing :db) + new account fields, adds all effects necessary for account update." [{{:accounts/keys [accounts current-account-id] :as db} :db :as fx} new-account-fields] diff --git a/src/status_im/ui/screens/discover/events.cljs b/src/status_im/ui/screens/discover/events.cljs index d37ef0bef1..0dc05596fa 100644 --- a/src/status_im/ui/screens/discover/events.cljs +++ b/src/status_im/ui/screens/discover/events.cljs @@ -7,7 +7,9 @@ [status-im.data-store.discover :as discoveries] [status-im.utils.handlers :as u] [status-im.utils.datetime :as time] - [status-im.utils.random :as random])) + [status-im.utils.random :as random] + [taoensso.timbre :as log] + [status-im.utils.handlers :as handlers])) (def request-discoveries-interval-s 600) @@ -27,26 +29,31 @@ [message-id discover])) (into {}))))) +;; todo(goranjovic): at the moment we do nothing when a status without hashtags is posted +;; but we probably should post a special "delete" status that removes any previous +;; hashtag statuses in that scenario. In any case, that's the reason why this event +;; gets even the statuses without a hashtag - it may need to do stuff with them as well. (register-handler :broadcast-status (u/side-effect! (fn [{:keys [current-public-key web3] :accounts/keys [accounts current-account-id] :contacts/keys [contacts]} - [_ status hashtags]] - (let [{:keys [name photo-path]} (get accounts current-account-id) - message-id (random/id) - message {:message-id message-id - :from current-public-key - :payload {:message-id message-id - :status status - :hashtags (vec hashtags) - :profile {:name name - :profile-image photo-path}}}] - (doseq [id (u/identities contacts)] - (protocol/send-status! - {:web3 web3 - :message (assoc message :to id)})) - (dispatch [:status-received message]))))) + [_ status]] + (if-let [hashtags (seq (handlers/get-hashtags status))] + (let [{:keys [name photo-path]} (get accounts current-account-id) + message-id (random/id) + message {:message-id message-id + :from current-public-key + :payload {:message-id message-id + :status status + :hashtags (vec hashtags) + :profile {:name name + :profile-image photo-path}}}] + (doseq [id (u/identities contacts)] + (protocol/send-status! + {:web3 web3 + :message (assoc message :to id)})) + (dispatch [:status-received message])))))) (register-handler :status-received (u/side-effect! diff --git a/src/status_im/ui/screens/profile/events.cljs b/src/status_im/ui/screens/profile/events.cljs index 84b6043046..4a8dffbf81 100644 --- a/src/status_im/ui/screens/profile/events.cljs +++ b/src/status_im/ui/screens/profile/events.cljs @@ -145,22 +145,35 @@ (accounts-events/account-update {:name cleaned-name :last-updated now}))))) +(defn status-change + "Takes map containing old status and the updated one and if the status has changed + returns the effects neccessary for status broadcasting" + [fx {:keys [old-status status]}] + (if (and status (not= status old-status)) + (assoc fx :dispatch-n [[:broadcast-status status]]) + fx)) + (handlers/register-handler-fx :my-profile.drawer/save-status (fn [{:keys [db now]} _] (let [status (get-in db [:my-profile/drawer :status]) - new-fx (clear-profile {:db db})] + new-fx (clear-profile {:db db}) + {:accounts/keys [accounts current-account-id]} db + {old-status :status} (get accounts current-account-id)] (if (string/blank? status) new-fx (-> new-fx (accounts-events/account-update {:status status :last-updated now}) - (assoc :dispatch-n [[:check-status-change status]])))))) + (status-change {:old-status old-status + :status status})))))) (handlers/register-handler-fx :my-profile/save-profile (fn [{:keys [db now]} _] - (let [{:keys [status photo-path]} (:my-profile/profile db) + (let [{:accounts/keys [accounts current-account-id]} db + {old-status :status} (get accounts current-account-id) + {:keys [status photo-path]} (:my-profile/profile db) cleaned-name (clean-name db :my-profile/profile) cleaned-edit {:name cleaned-name :status status @@ -168,5 +181,6 @@ :last-updated now}] (-> (clear-profile {:db db}) (accounts-events/account-update cleaned-edit) - (assoc :dispatch-n [[:check-status-change status] - [:navigate-back]]))))) + (status-change {:old-status old-status + :status status}) + (update :dispatch-n concat [[:navigate-back]]))))) diff --git a/test/cljs/status_im/test/profile/events.cljs b/test/cljs/status_im/test/profile/events.cljs index fb5e07aae6..42f6a55e75 100644 --- a/test/cljs/status_im/test/profile/events.cljs +++ b/test/cljs/status_im/test/profile/events.cljs @@ -22,8 +22,7 @@ (defn test-fixtures [] (rf/reg-fx ::events/init-store #()) - (rf/reg-fx ::account-events/save-account #()) - (rf/reg-fx :check-status-change #())) + (rf/reg-fx ::account-events/save-account #())) (deftest profile-edit-events (run-test-sync @@ -54,3 +53,17 @@ (is (= new-status (-> @accounts (get address) :status)))))))) + +(deftest test-status-change + (let [fx {:db {}}] + (is (= (profile-events/status-change fx {:old-status "this is old status" + :status "this is new and CHANGED status"}) + {:db {} + :dispatch-n [[:broadcast-status "this is new and CHANGED status"]]})) + (is (= (profile-events/status-change fx {:old-status "this is old status" + :status "this is new and #changed status"}) + {:db {} + :dispatch-n [[:broadcast-status "this is new and #changed status"]]})) + (is (= (profile-events/status-change fx {:old-status "this is old status" + :status "this is old status"}) + {:db {}}))))