update realm version

This commit is contained in:
Eric Dvorsak 2017-10-19 15:58:21 +02:00 committed by Roman Volosovskyi
parent 12c72ccf9d
commit df83cbb987
17 changed files with 1656 additions and 740 deletions

2111
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -81,7 +81,7 @@
"react-native-vector-icons": "^4.0.1",
"react-native-webview-bridge": "git+https://github.com/status-im/react-native-webview-bridge.git#remove-status-go-2",
"readable-stream": "1.0.33",
"realm": "^0.14.3",
"realm": "^2.0.1",
"stream-browserify": "^1.0.0",
"string_decoder": "^0.10.31",
"timers-browserify": "^1.4.2",

View File

@ -3,13 +3,14 @@
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.9.0-alpha17"]
[org.clojure/clojurescript "1.9.908"]
[org.clojure/clojurescript "1.9.946"]
[org.clojure/core.async "0.3.443"]
[reagent "0.6.0" :exclusions [cljsjs/react cljsjs/react-dom cljsjs/react-dom-server]]
[re-frame "0.10.1"]
[com.andrewmcveigh/cljs-time "0.5.0"]
[com.taoensso/timbre "4.10.0"]
[hickory "0.7.1"]]
[hickory "0.7.1"]
[com.cognitect/transit-cljs "0.8.243"]]
:plugins [[lein-cljsbuild "1.1.7"]
[lein-figwheel "0.5.13"]
[lein-re-frisk "0.5.1"]]

View File

@ -17,6 +17,11 @@
(or (not (chat-store/exists? chat-id))
(chat-store/is-active? chat-id))))))
(re-frame/reg-cofx
:message-exists?
(fn [cofx]
(assoc cofx :message-exists? msg-store/exists?)))
(re-frame/reg-cofx
:get-last-clock-value
(fn [cofx]
@ -33,7 +38,7 @@
(get-in accounts [current-account-id :public-key]))
(defn add-message
[{:keys [db get-stored-message get-last-stored-message pop-up-chat?
[{:keys [db message-exists? get-last-stored-message pop-up-chat?
get-last-clock-value current-timestamp random-id]}
{:keys [from group-id chat-id content-type
message-id timestamp clock-value]
@ -44,7 +49,7 @@
;; proceed with adding message if message is not already stored in realm,
;; it's not from current user (outgoing message) and it's for relevant chat
;; (either current active chat or new chat not existing yet)
(if (and (not (get-stored-message chat-identifier))
(if (and (not (message-exists? message-id))
(not= from current-identity)
(pop-up-chat? chat-identifier))
(let [group-chat? (not (nil? group-id))
@ -78,7 +83,7 @@
{:db db})))
(def ^:private receive-interceptors
[(re-frame/inject-cofx :get-stored-message) (re-frame/inject-cofx :get-last-stored-message)
[(re-frame/inject-cofx :message-exists?) (re-frame/inject-cofx :get-last-stored-message)
(re-frame/inject-cofx :pop-up-chat?) (re-frame/inject-cofx :get-last-clock-value)
(re-frame/inject-cofx :current-timestamp) (re-frame/inject-cofx :random-id)
re-frame/trim-v])

View File

@ -1,24 +1,16 @@
(ns status-im.data-store.messages
(:require [status-im.data-store.realm.messages :as data-store]
[clojure.string :refer [join split]]
[status-im.utils.random :refer [timestamp]]
[status-im.utils.utils :refer [update-if-present]]
[clojure.walk :refer [stringify-keys keywordize-keys]]
[cljs.reader :refer [read-string]]
[status-im.constants :as c]))
(defn- user-statuses-to-map
[user-statuses]
(->> (vals user-statuses)
(mapv (fn [{:keys [whisper-identity] :as status}]
[whisper-identity status]))
(into {})))
(:refer-clojure :exclude [exists?])
(:require [cljs.reader :as reader]
[status-im.constants :as constants]
[status-im.data-store.realm.messages :as data-store]
[status-im.utils.random :as random]
[status-im.utils.utils :as utils]))
(defn- command-type?
[type]
(contains?
#{c/content-type-command c/content-type-command-request
c/content-type-wallet-request c/content-type-wallet-command}
#{constants/content-type-command constants/content-type-command-request
constants/content-type-wallet-request constants/content-type-wallet-command}
type))
(def default-values
@ -28,26 +20,17 @@
:same-direction false
:preview nil})
(defn exists? [message-id]
(data-store/exists? message-id))
(defn get-by-id
[message-id]
(some-> (data-store/get-by-id message-id)
(clojure.core/update :user-statuses user-statuses-to-map)))
(data-store/get-by-id message-id))
(defn get-message-content-by-id [message-id]
(when-let [{:keys [content-type content] :as message} (get-by-id message-id)]
(when (command-type? content-type)
(read-string content))))
(defn get-messages
[messages]
(->> messages
(mapv #(clojure.core/update % :user-statuses user-statuses-to-map))
(into '())
reverse
(keep (fn [{:keys [content-type] :as message}]
(if (command-type? content-type)
(clojure.core/update message :content read-string)
message)))))
(reader/read-string content))))
(defn get-count-by-chat-id
[chat-id]
@ -57,26 +40,24 @@
([chat-id]
(get-by-chat-id chat-id 0))
([chat-id from]
(->> (data-store/get-by-chat-id chat-id from c/default-number-of-messages)
(mapv #(clojure.core/update % :user-statuses user-statuses-to-map))
(into '())
(->> (data-store/get-by-chat-id chat-id from constants/default-number-of-messages)
reverse
(keep (fn [{:keys [content-type preview] :as message}]
(if (command-type? content-type)
(clojure.core/update message :content read-string)
(update message :content reader/read-string)
message))))))
(defn get-log-messages
[chat-id]
(->> (data-store/get-by-chat-id chat-id 0 100)
(filter #(= (:content-type %) c/content-type-log-message))
(filter #(= (:content-type %) constants/content-type-log-message))
(map #(select-keys % [:content :timestamp]))))
(defn get-last-message
[chat-id]
(if-let [{:keys [content-type] :as message} (data-store/get-last-message chat-id)]
(if (command-type? content-type)
(clojure.core/update message :content read-string)
(update message :content reader/read-string)
message)))
(defn get-last-outgoing
@ -101,7 +82,7 @@
(->> (data-store/get-all-as-list)
(filter :preview)
(reduce (fn [acc {:keys [message-id preview]}]
(assoc acc message-id (read-string preview)))
(assoc acc message-id (reader/read-string preview)))
{})))
(defn- prepare-content [content]
@ -119,13 +100,13 @@
message
{:chat-id chat-id
:content content'
:timestamp (timestamp)})]
:timestamp (random/timestamp)})]
(data-store/save message'))))
(defn update-message
[{:keys [message-id] :as message}]
(when (data-store/exists? message-id)
(let [message (update-if-present message :user-statuses vals)]
(let [message (utils/update-if-present message :user-statuses vals)]
(data-store/save message))))
(defn delete-by-chat-id [chat-id]

View File

@ -1,25 +1,19 @@
(ns status-im.data-store.realm.accounts
(:require [status-im.data-store.realm.core :as realm]))
(defn- reformat-networks [account]
(update account :networks
(fn [networks]
(into {}
(map (fn [{:keys [id] :as network}]
[id network])
(vals (js->clj networks :keywordize-keys true)))))))
(defn get-all []
(realm/get-all realm/base-realm :account))
(defn get-all-as-list []
(map reformat-networks (realm/realm-collection->list (get-all))))
(->> (get-all)
realm/js-object->clj
(mapv #(realm/fix-map % :networks :id))))
(defn get-by-address [address]
(reformat-networks
(realm/get-one-by-field-clj realm/base-realm :account :address address)))
(realm/fix-map (realm/get-one-by-field-clj realm/base-realm :account :address address)
:networks :id))
(defn save [account update?]
(realm/write realm/base-realm
(let [account' (update account :networks (comp vec vals))]
(let [account' (realm/fix-map->vec account :networks)]
#(realm/create realm/base-realm :account account' update?))))

View File

@ -12,13 +12,13 @@
(defn get-all-as-list
[]
(realm/realm-collection->list (get-all)))
(realm/js-object->clj (get-all)))
(defn get-all-active
[]
(-> (realm/get-by-field @realm/account-realm :chat :is-active true)
(realm/sorted :timestamp :desc)
realm/realm-collection->list))
realm/js-object->clj))
(defn- groups
[active?]
@ -28,15 +28,14 @@
(defn get-active-group-chats
[]
(map
(fn [{:keys [chat-id public-key private-key public?]}]
(map (fn [{:keys [chat-id public-key private-key public?]}]
(let [group {:group-id chat-id
:public? public?}]
(if (and public-key private-key)
(assoc group :keypair {:private private-key
:public public-key})
group)))
(realm/realm-collection->list (groups true))))
(realm/js-object->clj (groups true))))
(defn- get-by-id-obj
[chat-id]
@ -46,7 +45,7 @@
[chat-id]
(-> @realm/account-realm
(realm/get-one-by-field-clj :chat :chat-id chat-id)
(realm/list->array :contacts)))
(realm/fix-map->vec :contacts)))
(defn save
[chat update?]

View File

@ -10,7 +10,7 @@
(defn get-all-as-list
[]
(realm/realm-collection->list (get-all)))
(realm/js-object->clj (get-all)))
(defn save
[group update?]

View File

@ -10,7 +10,7 @@
(defn get-all-as-list
[]
(realm/realm-collection->list (get-all)))
(realm/js-object->clj (get-all)))
(defn get-by-id
[whisper-identity]
@ -18,8 +18,7 @@
(defn get-by-id-cljs
[whisper-identity]
(some-> (get-by-id whisper-identity)
(js->clj :keywordize-keys true)))
(realm/get-one-by-field-clj @realm/account-realm :contact :whisper-identity whisper-identity))
(defn save
[contact update?]

View File

@ -6,6 +6,8 @@
[status-im.utils.fs :as fs]
[clojure.string :as str]
[goog.string :as gstr]
[cognitect.transit :as transit]
[clojure.walk :as walk]
[status-im.react-native.js-dependencies :as rn-dependencies])
(:refer-clojure :exclude [exists?]))
@ -121,81 +123,46 @@
(defn filtered [results filter-query]
(.filtered results filter-query))
(defn add-js->clj-array
"Extends type with IEncodeClojure and treats it as js array."
[t]
(extend-type t
IEncodeClojure
(-js->clj
([x options]
(vec (map #(apply clj->js % options) x))))))
(def map->vec
(comp vec vals))
(defn add-js->clj-object [t]
"Extends type with IEncodeClojure and treats it as js object."
(extend-type t
IEncodeClojure
(-js->clj
([x options]
(let [{:keys [keywordize-keys]} options
keyfn (if keywordize-keys keyword str)]
(dissoc
(into
(def reader (transit/reader :json))
(defn js-object->clj
"Converts any js type/object into a map recursively
Performs 5 times better than iterating over the object keys
and that would require special care for collections"
[js-object]
(let [o (->> js-object
(.stringify js/JSON)
(transit/read reader))]
(walk/keywordize-keys (if (map? o)
(map->vec o)
o))))
(defn fix-map->vec
"Takes a map m and a keyword k
Updates the value in k, a map representing a list, into a vector
example: {:0 0 :1 1} -> [0 1]"
[m k]
(update m k map->vec))
(defn fix-map
"Takes a map m, a keyword k and an id id
Updates the value in k, a map representing a list, into a map using
the id extracted from the value as a key
example: {:0 {:id 1 :a 2} :1 {:id 2 :a 2}} -> {1 {:id 1 :a 2} 2 {:id 2 :a 2}}"
[m k id]
(update m k #(reduce (fn [acc [_ v]]
(assoc acc (get v id) v))
{}
(for [k (js-keys x)]
;; ignore properties that are added with IEncodeClojure
(if (#{"cljs$core$IEncodeClojure$"
"cljs$core$IEncodeClojure$_js__GT_clj$arity$2"}
k)
[nil nil]
(let [v (aget x k)]
;; check if property is of List type and wasn't succesfully
;; transformed to ClojureScript data structure
(when (and v
(not (string? v))
(not (boolean? v))
(not (number? v))
(not (coll? v))
(not (satisfies? IEncodeClojure v))
(str/includes? (type->str (type v)) "List"))
(add-js->clj-object (type v)))
[(keyfn k) (js->clj v :keywordize-keys keywordize-keys)]))))
nil))))))
(defn check-collection
"Checks if collection was succesfully transformed to ClojureScript,
extends it with IEncodeClojure if necessary"
[coll]
(cond
(not (coll? coll))
(do (add-js->clj-array (type coll))
(check-collection (js->clj coll :keywordize-keys true)))
(let [f (first coll)]
(and f (not (map? f))))
(do (add-js->clj-object (type (first coll)))
(js->clj coll :keywordize-keys true))
:else coll))
(defn realm-collection->list [collection]
(-> collection
(.map (fn [object _ _] object))
(js->clj :keywordize-keys true)
check-collection))
(defn list->array [record list-field]
(update-in record [list-field] (comp vec vals)))
%)))
(defn single [result]
(-> (aget result 0)))
(aget result 0))
(defn single-cljs [result]
(let [res (some-> (aget result 0)
(js->clj :keywordize-keys true))]
(if (and res (not (map? res)))
(do (add-js->clj-object (type res))
(js->clj res :keywordize-keys true))
res)))
(def single-clj
(comp first js-object->clj))
(defn- get-schema-by-name [opts]
(->> opts
@ -229,7 +196,7 @@
(single (get-by-field realm schema-name field value)))
(defn get-one-by-field-clj [realm schema-name field value]
(single-cljs (get-by-field realm schema-name field value)))
(single-clj (get-by-field realm schema-name field value)))
(defn get-by-fields [realm schema-name op fields]
(let [queries (map (fn [[k v]]

View File

@ -11,7 +11,7 @@
(defn get-all-as-list
[ordering]
(realm/realm-collection->list (get-all ordering)))
(realm/js-object->clj (get-all ordering)))
(defn get-tag-by-name [tag]
(log/debug "Getting tag: " tag)
@ -33,8 +33,8 @@
(defn- get-tags
[message-id]
(-> (realm/get-one-by-field-clj @realm/account-realm :discover :message-id message-id)
(:tags)
(vals)))
:tags
vals))
(defn- upsert-discover [{:keys [message-id tags] :as discover}]
(log/debug "Creating/updating discover with tags: " tags)
@ -76,4 +76,4 @@
(defn get-all-tags []
(-> (realm/get-all @realm/account-realm :tag)
(realm/sorted :count :desc)
realm/realm-collection->list))
realm/js-object->clj))

View File

@ -5,7 +5,7 @@
(realm/get-all @realm/account-realm :handler-data))
(defn get-all-as-list []
(realm/realm-collection->list (get-all)))
(realm/js-object->clj (get-all)))
(defn get-by-message-id
[message-id]

View File

@ -9,22 +9,25 @@
(defn get-all-as-list
[]
(realm/realm-collection->list (get-all)))
(realm/js-object->clj (get-all)))
(defn get-by-id
[message-id]
(realm/get-one-by-field-clj @realm/account-realm :message :message-id message-id))
(when-let [message (realm/get-one-by-field-clj @realm/account-realm :message :message-id message-id)]
(realm/fix-map message :user-statuses :whisper-identity)))
(defn get-by-chat-id
"arity-1 returns realm object for queries"
([chat-id]
(realm/get-by-field @realm/account-realm :message :chat-id chat-id))
([chat-id number-of-messages]
(get-by-chat-id chat-id 0 number-of-messages))
([chat-id from number-of-messages]
(-> (realm/get-by-field @realm/account-realm :message :chat-id chat-id)
(let [messages (-> (realm/get-by-field @realm/account-realm :message :chat-id chat-id)
(realm/sorted :timestamp :desc)
(realm/page from (+ from number-of-messages))
(realm/realm-collection->list))))
realm/js-object->clj)]
(mapv #(realm/fix-map % :user-statuses :whisper-identity) messages))))
(defn get-count-by-chat-id
[chat-id]
@ -35,20 +38,20 @@
(-> (realm/get-by-fields @realm/account-realm :message :and fields)
(realm/sorted :timestamp :desc)
(realm/page from (+ from number-of-messages))
(realm/realm-collection->list)))
realm/js-object->clj))
(defn get-last-message
[chat-id]
(-> (realm/get-by-field @realm/account-realm :message :chat-id chat-id)
(realm/sorted :clock-value :desc)
(realm/single-cljs)))
(realm/single-clj)))
(defn get-unviewed
[]
(-> @realm/account-realm
(realm/get-by-fields :message :and {:outgoing false
:message-status nil})
(realm/realm-collection->list)))
realm/js-object->clj))
(defn exists?
[message-id]
@ -56,7 +59,8 @@
(defn save
[message]
(realm/save @realm/account-realm :message message true))
(let [message (update message :user-statuses #(if % % []))]
(realm/save @realm/account-realm :message message true)))
(defn delete-by-chat-id
[chat-id]

View File

@ -9,7 +9,7 @@
(defn get-all-as-list
[]
(realm/realm-collection->list (get-all)))
(realm/js-object->clj (get-all)))
(defn save
[network update?]

View File

@ -8,8 +8,7 @@
(defn get-all-as-list
[]
(->> (get-all)
realm/realm-collection->list))
(realm/js-object->clj (get-all)))
(defn get-by-message-id
[message-id]

View File

@ -14,7 +14,7 @@
(defn get-filtered-as-list
[condition]
(realm/realm-collection->list (get-filtered condition)))
(realm/js-object->clj (get-filtered condition)))
(defn save
[processed-message]

View File

@ -7,14 +7,14 @@
(defn get-all-as-list
[]
(realm/realm-collection->list (get-all)))
(realm/js-object->clj (get-all)))
(defn get-open-by-chat-id
[chat-id]
(-> (realm/get-by-fields @realm/account-realm :request :and [[:chat-id chat-id]
[:status "open"]])
(realm/sorted :added :desc)
(realm/realm-collection->list)))
realm/js-object->clj))
;; NOTE(oskarth): phone command in Console can be answered again, so we want to list this
;; TODO(oskarth): Refactor this, either by extending and/or query or changing status of message
@ -24,7 +24,7 @@
[:type "phone"]
[:status "answered"]])
(realm/sorted :added :desc)
(realm/realm-collection->list)))
realm/js-object->clj))
(defn get-available-by-chat-id
[chat-id]
@ -45,7 +45,7 @@
(-> @realm/account-realm
(realm/get-by-fields :request :and [[:chat-id chat-id]
[:message-id message-id]])
(realm/single)))
realm/single))
(defn mark-as-answered
[chat-id message-id]