bug #2381 - updated statuses are broadcast

This commit is contained in:
Goran Jovic 2017-11-07 15:13:30 +01:00 committed by Roman Volosovskyi
parent 54209b5190
commit edd4df85f9
4 changed files with 57 additions and 34 deletions

View File

@ -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]

View File

@ -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!

View File

@ -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]])))))

View File

@ -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 {}}))))