Refactor timestamp

Signed-off-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
This commit is contained in:
Itoh, Masaru 2018-02-20 23:47:56 +09:00 committed by Andrea Maria Piana
parent b447b80ee4
commit 98a0c2081f
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
18 changed files with 56 additions and 50 deletions

View File

@ -1,4 +1,4 @@
(ns status-im.chat.handlers (ns status-im.chat.handlers
(:require [clojure.string :as string] (:require [clojure.string :as string]
[re-frame.core :as re-frame] [re-frame.core :as re-frame]
[status-im.chat.models :as models] [status-im.chat.models :as models]
@ -7,7 +7,8 @@
[status-im.ui.components.styles :as components.styles] [status-im.ui.components.styles :as components.styles]
[status-im.utils.handlers :as handlers] [status-im.utils.handlers :as handlers]
[status-im.utils.random :as random] [status-im.utils.random :as random]
status-im.chat.events)) status-im.chat.events
[status-im.utils.datetime :as datetime]))
(handlers/register-handler (handlers/register-handler
:leave-group-chat :leave-group-chat
@ -71,7 +72,8 @@
(handlers/register-handler-fx (handlers/register-handler-fx
:create-new-public-chat :create-new-public-chat
(fn [{:keys [db]} [_ topic]] [(re-frame/inject-cofx :now)]
(fn [{:keys [db now]} [_ topic]]
(let [exists? (boolean (get-in db [:chats topic])) (let [exists? (boolean (get-in db [:chats topic]))
chat {:chat-id topic chat {:chat-id topic
:name topic :name topic
@ -79,7 +81,7 @@
:group-chat true :group-chat true
:public? true :public? true
:is-active true :is-active true
:timestamp (random/timestamp) :timestamp now
:last-to-clock-value 0 :last-to-clock-value 0
:last-from-clock-value 0}] :last-from-clock-value 0}]
(merge (merge
@ -125,7 +127,7 @@
(defn prepare-group-chat (defn prepare-group-chat
[{:keys [current-public-key username] [{:keys [current-public-key username]
:group/keys [selected-contacts] :group/keys [selected-contacts]
:contacts/keys [contacts]} group-name] :contacts/keys [contacts]} group-name timestamp]
(let [selected-contacts' (mapv #(hash-map :identity %) selected-contacts) (let [selected-contacts' (mapv #(hash-map :identity %) selected-contacts)
chat-name (if-not (string/blank? group-name) chat-name (if-not (string/blank? group-name)
group-name group-name
@ -139,17 +141,19 @@
:group-chat true :group-chat true
:group-admin current-public-key :group-admin current-public-key
:is-active true :is-active true
:timestamp (random/timestamp) :timestamp timestamp
:contacts selected-contacts' :contacts selected-contacts'
:last-to-clock-value 0 :last-to-clock-value 0
:last-from-clock-value 0})) :last-from-clock-value 0}))
(handlers/register-handler-fx (handlers/register-handler-fx
:create-new-group-chat-and-open :create-new-group-chat-and-open
(fn [{:keys [db]} [_ group-name]] [(re-frame/inject-cofx :now)]
(fn [{:keys [db now]} [_ group-name]]
(let [new-chat (prepare-group-chat (select-keys db [:group/selected-contacts :current-public-key :username (let [new-chat (prepare-group-chat (select-keys db [:group/selected-contacts :current-public-key :username
:contacts/contacts]) :contacts/contacts])
group-name)] group-name
now)]
{:db (-> db {:db (-> db
(assoc-in [:chats (:chat-id new-chat)] new-chat) (assoc-in [:chats (:chat-id new-chat)] new-chat)
(assoc :group/selected-contacts #{})) (assoc :group/selected-contacts #{}))

View File

@ -4,7 +4,8 @@
[status-im.constants :as constants] [status-im.constants :as constants]
[status-im.data-store.realm.messages :as data-store] [status-im.data-store.realm.messages :as data-store]
[status-im.utils.random :as random] [status-im.utils.random :as random]
[status-im.utils.core :as utils])) [status-im.utils.core :as utils]
[status-im.utils.datetime :as datetime]))
(defn- command-type? (defn- command-type?
[type] [type]
@ -76,7 +77,7 @@
(data-store/save (prepare-message (merge default-values (data-store/save (prepare-message (merge default-values
message message
{:from (or from "anonymous") {:from (or from "anonymous")
:timestamp (random/timestamp)}))))) :timestamp (datetime/timestamp)})))))
(defn update-message (defn update-message
[{:keys [message-id] :as message}] [{:keys [message-id] :as message}]

View File

@ -1,6 +1,5 @@
(ns status-im.data-store.realm.browser (ns status-im.data-store.realm.browser
(:require [status-im.data-store.realm.core :as realm] (:require [status-im.data-store.realm.core :as realm])
[status-im.utils.random :refer [timestamp]])
(:refer-clojure :exclude [exists?])) (:refer-clojure :exclude [exists?]))
(defn get-all (defn get-all

View File

@ -2,7 +2,7 @@
(:require [goog.object :as object] (:require [goog.object :as object]
[status-im.data-store.realm.core :as realm] [status-im.data-store.realm.core :as realm]
[status-im.data-store.realm.messages :as messages] [status-im.data-store.realm.messages :as messages]
[status-im.utils.random :refer [timestamp]] [status-im.utils.datetime :as datetime]
[taoensso.timbre :as log]) [taoensso.timbre :as log])
(:refer-clojure :exclude [exists?])) (:refer-clojure :exclude [exists?]))
@ -78,7 +78,7 @@
(fn [] (fn []
(doto chat (doto chat
(aset "is-active" false) (aset "is-active" false)
(aset "removed-at" (timestamp))))))) (aset "removed-at" (datetime/timestamp)))))))
(defn get-contacts (defn get-contacts
[chat-id] [chat-id]
@ -100,7 +100,7 @@
(defn add-contacts (defn add-contacts
[chat-id identities] [chat-id identities]
(let [contacts (get-contacts chat-id) (let [contacts (get-contacts chat-id)
added-at (timestamp)] added-at (datetime/timestamp)]
(realm/write @realm/account-realm (realm/write @realm/account-realm
#(save-contacts identities contacts added-at)))) #(save-contacts identities contacts added-at))))

View File

@ -1,7 +1,6 @@
(ns status-im.data-store.realm.contact-groups (ns status-im.data-store.realm.contact-groups
(:require [goog.object :as object] (:require [goog.object :as object]
[status-im.data-store.realm.core :as realm] [status-im.data-store.realm.core :as realm])
[status-im.utils.random :refer [timestamp]])
(:refer-clojure :exclude [exists?])) (:refer-clojure :exclude [exists?]))
(defn get-all (defn get-all

View File

@ -8,7 +8,8 @@
[cljs.spec.alpha :as s] [cljs.spec.alpha :as s]
[status-im.protocol.validation :refer-macros [valid?]] [status-im.protocol.validation :refer-macros [valid?]]
[status-im.utils.random :as random] [status-im.utils.random :as random]
[status-im.protocol.web3.keys :as shh-keys])) [status-im.protocol.web3.keys :as shh-keys]
[status-im.utils.datetime :as datetime]))
(def discover-topic-prefix "status-discover-") (def discover-topic-prefix "status-discover-")
(def discover-topic "0xbeefdead") (def discover-topic "0xbeefdead")
@ -33,7 +34,7 @@
{:requires-ack? false {:requires-ack? false
:type :online :type :online
:key-password discovery-key-password :key-password discovery-key-password
:payload {:content {:timestamp (u/timestamp)}} :payload {:content {:timestamp (datetime/timestamp)}}
:topics [f/status-topic]})] :topics [f/status-topic]})]
(d/add-pending-message! web3 message'))) (d/add-pending-message! web3 message')))
@ -116,7 +117,7 @@
(assoc :type :profile (assoc :type :profile
:topics [f/status-topic] :topics [f/status-topic]
:key-password discovery-key-password) :key-password discovery-key-password)
(assoc-in [:payload :timestamp] (u/timestamp)) (assoc-in [:payload :timestamp] (datetime/timestamp))
(assoc-in [:payload :content :profile] (assoc-in [:payload :content :profile]
(get-in message [:payload :profile])) (get-in message [:payload :profile]))
(update :payload dissoc :profile)))) (update :payload dissoc :profile))))
@ -139,7 +140,7 @@
:requires-ack? false :requires-ack? false
:key-password discovery-key-password :key-password discovery-key-password
:topics [f/status-topic]) :topics [f/status-topic])
(assoc-in [:payload :timestamp] (u/timestamp)))] (assoc-in [:payload :timestamp] (datetime/timestamp)))]
(d/add-pending-message! web3 message))) (d/add-pending-message! web3 message)))
(s/def :status/payload (s/def :status/payload

View File

@ -9,7 +9,8 @@
[status-im.protocol.web3.filtering :as f] [status-im.protocol.web3.filtering :as f]
[status-im.protocol.listeners :as l] [status-im.protocol.listeners :as l]
[clojure.string :as str] [clojure.string :as str]
[status-im.protocol.web3.keys :as shh-keys])) [status-im.protocol.web3.keys :as shh-keys]
[status-im.utils.datetime :as datetime]))
(defn prepare-mesage (defn prepare-mesage
[{:keys [message group-id keypair new-keypair type username requires-ack?]}] [{:keys [message group-id keypair new-keypair type username requires-ack?]}]
@ -18,7 +19,7 @@
:username username :username username
:group-id group-id :group-id group-id
:type type :type type
:timestamp (u/timestamp)) :timestamp (datetime/timestamp))
(assoc :topics [f/status-topic] (assoc :topics [f/status-topic]
:key-password group-id :key-password group-id
:requires-ack? (or (nil? requires-ack?) requires-ack?) :requires-ack? (or (nil? requires-ack?) requires-ack?)
@ -97,7 +98,7 @@
:requires-ack? true :requires-ack? true
:type type) :type type)
(update :payload assoc (update :payload assoc
:timestamp (u/timestamp) :timestamp (datetime/timestamp)
:group-id id :group-id id
:group-admin admin :group-admin admin
:group-name name :group-name name

View File

@ -185,7 +185,7 @@
(re-frame/reg-fx (re-frame/reg-fx
::load-processed-messages! ::load-processed-messages!
(fn [] (fn []
(let [now (datetime/now-ms) (let [now (datetime/timestamp)
messages (processed-messages/get-filtered (str "ttl > " now))] messages (processed-messages/get-filtered (str "ttl > " now))]
(cache/init! messages) (cache/init! messages)
(processed-messages/delete (str "ttl <=" now))))) (processed-messages/delete (str "ttl <=" now)))))
@ -465,7 +465,7 @@
processed-message {:id (random/id) processed-message {:id (random/id)
:message-id message-id :message-id message-id
:type type :type type
:ttl (+ (datetime/now-ms) ttl-s)} :ttl (+ (datetime/timestamp) ttl-s)}
chat-message (#{:message :group-message} (:type payload)) chat-message (#{:message :group-message} (:type payload))
route-fx (case type route-fx (case type
(:message (:message

View File

@ -8,7 +8,8 @@
[status-im.protocol.validation :refer-macros [valid?]] [status-im.protocol.validation :refer-macros [valid?]]
[clojure.set :as set] [clojure.set :as set]
[status-im.protocol.web3.keys :as shh-keys] [status-im.protocol.web3.keys :as shh-keys]
[status-im.utils.async :refer [timeout]])) [status-im.utils.async :refer [timeout]]
[status-im.utils.datetime :as datetime]))
(defonce loop-state (atom nil)) (defonce loop-state (atom nil))
(defonce messages (atom {})) (defonce messages (atom {}))
@ -162,7 +163,7 @@
(swap! messages update-in [web3 id to] (swap! messages update-in [web3 id to]
(fn [{:keys [attempts] :as data}] (fn [{:keys [attempts] :as data}]
(assoc data :attempts (inc attempts) (assoc data :attempts (inc attempts)
:last-attempt (u/timestamp))))) :last-attempt (datetime/timestamp)))))
(defn delivery-callback (defn delivery-callback
[web3 post-error-callback {:keys [id requires-ack? to]} message] [web3 post-error-callback {:keys [id requires-ack? to]} message]
@ -206,7 +207,7 @@
;; continue attempts ;; continue attempts
(<= attempts max-attempts-number) (<= attempts max-attempts-number)
;; check retransmission interval ;; check retransmission interval
(<= (+ last-attempt (* 1000 ack-not-received-s-interval)) (u/timestamp))))) (<= (+ last-attempt (* 1000 ack-not-received-s-interval)) (datetime/timestamp)))))
(defn- check-ttl (defn- check-ttl
[message message-type ttl-config default-ttl] [message message-type ttl-config default-ttl]

View File

@ -16,9 +16,6 @@
(defn shh [web3] (defn shh [web3]
(.-shh web3)) (.-shh web3))
(defn timestamp []
(to-long (now)))
(defn extract-enode-id [enode] (defn extract-enode-id [enode]
(-> enode (-> enode
(string/split #"/") (string/split #"/")

View File

@ -22,7 +22,8 @@
[cljs.spec.alpha :as spec] [cljs.spec.alpha :as spec]
[status-im.protocol.web3.utils :as web3.utils] [status-im.protocol.web3.utils :as web3.utils]
[status-im.ui.screens.add-new.new-chat.db :as new-chat.db] [status-im.ui.screens.add-new.new-chat.db :as new-chat.db]
[clojure.string :as string])) [clojure.string :as string]
[status-im.utils.datetime :as datetime]))
;;;; COFX ;;;; COFX
(reg-cofx (reg-cofx
@ -71,7 +72,7 @@
:fcm-token fcm-token} :fcm-token fcm-token}
:keypair {:public updates-public-key :keypair {:public updates-public-key
:private updates-private-key} :private updates-private-key}
:timestamp (web3.utils/timestamp)}}}))) :timestamp (datetime/timestamp)}}})))
(reg-fx (reg-fx
::reset-pending-messages ::reset-pending-messages
@ -146,7 +147,7 @@
{:group-id id' {:group-id id'
:name (:en name) :name (:en name)
:order 0 :order 0
:timestamp (random/timestamp) :timestamp (datetime/timestamp)
:contacts (mapv #(hash-map :identity %) contacts)})]]) :contacts (mapv #(hash-map :identity %) contacts)})]])
;; NOTE(oskarth): We now overwrite default contacts upon upgrade with default_contacts.json data. ;; NOTE(oskarth): We now overwrite default contacts upon upgrade with default_contacts.json data.

View File

@ -29,7 +29,7 @@
:<- [:get-contacts] :<- [:get-contacts]
:<- [:get :current-public-key] :<- [:get :current-public-key]
(fn [[discoveries chats contacts current-public-key]] (fn [[discoveries chats contacts current-public-key]]
(let [now-ms (time/now-ms)] (let [now-ms (time/timestamp)]
(map #(assoc % :priority (calculate-priority now-ms chats current-public-key contacts %)) (vals discoveries))))) (map #(assoc % :priority (calculate-priority now-ms chats current-public-key contacts %)) (vals discoveries)))))
(reg-sub :discover/search-tags :discover-search-tags) (reg-sub :discover/search-tags :discover-search-tags)

View File

@ -69,7 +69,7 @@
(re-frame/reg-cofx (re-frame/reg-cofx
:now :now
(fn [coeffects _] (fn [coeffects _]
(assoc coeffects :now (time/now-ms)))) (assoc coeffects :now (time/timestamp))))
(re-frame/reg-cofx (re-frame/reg-cofx
:random-id :random-id

View File

@ -6,7 +6,9 @@
[status-im.data-store.contact-groups :as groups] [status-im.data-store.contact-groups :as groups]
[clojure.string :as string] [clojure.string :as string]
[status-im.utils.random :as random] [status-im.utils.random :as random]
[status-im.ui.screens.group.navigation])) [status-im.ui.screens.group.navigation]
[status-im.utils.datetime :as datetime]
[re-frame.core :as re-frame]))
;;;; COFX ;;;; COFX
@ -65,12 +67,14 @@
(register-handler-fx (register-handler-fx
:create-new-contact-group :create-new-contact-group
(fn [{{:group/keys [contact-groups selected-contacts] :as db} :db} [_ group-name]] [(re-frame/inject-cofx :now)]
(fn [{{:group/keys [contact-groups selected-contacts] :as db} :db
now :now} [_ group-name]]
(let [selected-contacts' (mapv #(hash-map :identity %) selected-contacts) (let [selected-contacts' (mapv #(hash-map :identity %) selected-contacts)
new-group {:group-id (random/id) new-group {:group-id (random/id)
:name group-name :name group-name
:order (count contact-groups) :order (count contact-groups)
:timestamp (random/timestamp) :timestamp now
:contacts selected-contacts'}] :contacts selected-contacts'}]
{:db (update db :group/contact-groups merge {(:group-id new-group) new-group}) {:db (update db :group/contact-groups merge {(:group-id new-group) new-group})
::save-contact-group new-group}))) ::save-contact-group new-group})))

View File

@ -108,8 +108,8 @@
(defn to-date [ms] (defn to-date [ms]
(from-long ms)) (from-long ms))
(defn now-ms [] (defn timestamp []
(to-long (now))) (inst-ms (js/Date.)))
(defn format-date [format date] (defn format-date [format date]
(let [local (plus (from-date date) time-zone-offset)] (let [local (plus (from-date date) time-zone-offset)]

View File

@ -3,7 +3,7 @@
[status-im.utils.gfycat.adjectives :as adjectives] [status-im.utils.gfycat.adjectives :as adjectives]
[clojure.string :as str] [clojure.string :as str]
[status-im.utils.random :as rnd] [status-im.utils.random :as rnd]
[status-im.utils.datetime :refer [now-ms]])) [status-im.utils.datetime :as datetime]))
(defn- pick-random (defn- pick-random
[gen vector] [gen vector]
@ -25,4 +25,4 @@
nil unknown-gfy nil unknown-gfy
"0" unknown-gfy "0" unknown-gfy
(build-gfy public-key))) (build-gfy public-key)))
([] (generate-gfy (now-ms)))) ([] (generate-gfy (datetime/timestamp))))

View File

@ -1,13 +1,11 @@
(ns status-im.utils.random (ns status-im.utils.random
(:require [status-im.js-dependencies :as dependencies])) (:require [status-im.js-dependencies :as dependencies]
[status-im.utils.datetime :as datetime]))
(defn timestamp []
(.getTime (js/Date.)))
(def chance (dependencies/Chance.)) (def chance (dependencies/Chance.))
(defn id [] (defn id []
(str (timestamp) "-" (.guid chance))) (str (datetime/timestamp) "-" (.guid chance)))
(defn rand-gen (defn rand-gen
[seed] [seed]

View File

@ -308,7 +308,7 @@
;; ;;
;; :update-contact! ;; :update-contact!
;;TODO :update-chat! ;;TODO :update-chat!
(let [timestamp (datetime/now-ms)] (let [timestamp (datetime/timestamp)]
(def received-contact2 (assoc received-contact1 (def received-contact2 (assoc received-contact1
:last-updated timestamp :last-updated timestamp
:status "new status" :status "new status"