2016-05-19 18:31:56 +02:00
|
|
|
(ns status-im.discovery.handlers
|
2016-06-30 17:23:38 +03:00
|
|
|
(:require [re-frame.core :refer [after dispatch enrich]]
|
2016-08-04 18:36:13 +03:00
|
|
|
[status-im.utils.utils :refer [first-index]]
|
2016-06-30 17:23:38 +03:00
|
|
|
[status-im.utils.handlers :refer [register-handler]]
|
2016-09-04 18:39:05 +03:00
|
|
|
[status-im.protocol.core :as protocol]
|
2016-05-19 18:31:56 +02:00
|
|
|
[status-im.navigation.handlers :as nav]
|
2016-10-04 14:49:59 +03:00
|
|
|
[status-im.data-store.discovery :as discoveries]
|
2016-08-04 18:36:13 +03:00
|
|
|
[status-im.utils.handlers :as u]
|
2016-09-04 18:39:05 +03:00
|
|
|
[status-im.utils.datetime :as time]
|
|
|
|
[status-im.utils.random :as random]))
|
2016-08-04 18:36:13 +03:00
|
|
|
|
|
|
|
(register-handler :init-discoveries
|
|
|
|
(fn [db _]
|
|
|
|
(-> db
|
|
|
|
(assoc :discoveries []))))
|
|
|
|
|
|
|
|
(defn calculate-priority [{:keys [chats contacts]} from payload]
|
2016-09-04 18:39:05 +03:00
|
|
|
(let [contact (get contacts from)
|
|
|
|
chat (get chats from)
|
2016-08-04 18:36:13 +03:00
|
|
|
seen-online-recently? (< (- (time/now-ms) (get contact :last-online))
|
|
|
|
time/hour)]
|
|
|
|
(+ (time/now-ms) ;; message is newer => priority is higher
|
|
|
|
(if contact time/day 0) ;; user exists in contact list => increase priority
|
|
|
|
(if chat time/day 0) ;; chat with this user exists => increase priority
|
|
|
|
(if seen-online-recently? time/hour 0) ;; the user was online recently => increase priority
|
|
|
|
)))
|
2016-05-13 14:58:58 +03:00
|
|
|
|
|
|
|
(defmethod nav/preload-data! :discovery
|
2016-05-13 16:52:42 +03:00
|
|
|
[{:keys [discoveries] :as db} _]
|
2016-08-04 18:36:13 +03:00
|
|
|
(-> db
|
2016-10-04 14:49:59 +03:00
|
|
|
(assoc :tags (discoveries/get-all-tags))
|
2016-08-04 18:36:13 +03:00
|
|
|
;; todo add limit
|
|
|
|
;; todo hash-map with whisper-id as key and sorted by last-update
|
|
|
|
;; may be more efficient here
|
2016-10-04 14:49:59 +03:00
|
|
|
(assoc :discoveries (discoveries/get-all))))
|
2016-05-13 14:58:58 +03:00
|
|
|
|
|
|
|
(register-handler :discovery-response-received
|
|
|
|
(u/side-effect!
|
2016-09-04 18:39:05 +03:00
|
|
|
(fn [{:keys [current-public-key] :as db} [_ {:keys [from payload]}]]
|
|
|
|
(when-not (= current-public-key from)
|
|
|
|
(let [{:keys [discovery-id profile hashtags]} payload
|
|
|
|
{:keys [name profile-image status]} profile
|
|
|
|
discovery {:message-id discovery-id
|
|
|
|
:name name
|
|
|
|
:photo-path profile-image
|
|
|
|
:status status
|
|
|
|
:whisper-id from
|
|
|
|
:tags (map #(hash-map :name %) hashtags)
|
|
|
|
:last-updated (js/Date.)
|
|
|
|
:priority (calculate-priority db from payload)}]
|
|
|
|
(dispatch [:add-discovery discovery]))))))
|
2016-05-13 14:58:58 +03:00
|
|
|
|
|
|
|
(register-handler :broadcast-status
|
2016-05-13 16:52:42 +03:00
|
|
|
(u/side-effect!
|
2016-09-04 18:39:05 +03:00
|
|
|
(fn [{:keys [current-public-key web3 current-account-id accounts]}
|
|
|
|
[_ status hashtags]]
|
|
|
|
(let [{:keys [name photo-path]} (get accounts current-account-id)]
|
|
|
|
(protocol/broadcats-discoveries!
|
|
|
|
{:web3 web3
|
|
|
|
:hashtags (set hashtags)
|
|
|
|
:message {:from current-public-key
|
|
|
|
:message-id (random/id)
|
|
|
|
:payload {:status status
|
|
|
|
:profile {:name name
|
|
|
|
:status status
|
|
|
|
:profile-image photo-path}}}})
|
|
|
|
(protocol/watch-hashtags! {:web3 web3
|
|
|
|
:hashtags (set hashtags)
|
|
|
|
:callback #(dispatch [:incoming-message %1 %2])})))))
|
2016-05-13 14:58:58 +03:00
|
|
|
|
|
|
|
(register-handler :show-discovery-tag
|
|
|
|
(fn [db [_ tag]]
|
2016-05-13 16:52:42 +03:00
|
|
|
(dispatch [:navigate-to :discovery-tag])
|
|
|
|
(assoc db :current-tag tag)))
|
2016-05-13 14:58:58 +03:00
|
|
|
|
|
|
|
(defn add-discovery
|
2016-08-04 18:36:13 +03:00
|
|
|
[{db-discoveries :discoveries
|
2016-08-24 10:29:40 +03:00
|
|
|
:as db} [_ {:keys [message-id] :as discovery}]]
|
|
|
|
(let [updated-discoveries (if-let [i (first-index #(= (:message-id %) message-id) db-discoveries)]
|
2016-08-04 18:36:13 +03:00
|
|
|
(assoc db-discoveries i discovery)
|
|
|
|
(conj db-discoveries discovery))]
|
|
|
|
(-> db
|
|
|
|
(assoc :new-discovery discovery)
|
|
|
|
(assoc :discoveries updated-discoveries))))
|
2016-05-13 14:58:58 +03:00
|
|
|
|
|
|
|
(defn save-discovery!
|
|
|
|
[{:keys [new-discovery]} _]
|
2016-10-04 14:49:59 +03:00
|
|
|
(discoveries/save new-discovery))
|
2016-05-13 14:58:58 +03:00
|
|
|
|
|
|
|
(defn reload-tags!
|
|
|
|
[db _]
|
2016-10-04 14:49:59 +03:00
|
|
|
(assoc db :tags (discoveries/get-all-tags)))
|
2016-05-13 14:58:58 +03:00
|
|
|
|
|
|
|
(register-handler :add-discovery
|
|
|
|
(-> add-discovery
|
|
|
|
((after save-discovery!))
|
|
|
|
((enrich reload-tags!))))
|
2016-08-04 18:36:13 +03:00
|
|
|
|
|
|
|
(register-handler
|
|
|
|
:remove-old-discoveries!
|
|
|
|
(u/side-effect!
|
|
|
|
(fn [_ _]
|
2016-10-04 14:49:59 +03:00
|
|
|
(discoveries/delete :priority :asc 1000 200))))
|