status-react/src/status_im/data_store/messages.cljs

93 lines
3.2 KiB
Plaintext
Raw Normal View History

(ns status-im.data-store.messages
2017-10-19 13:58:21 +00:00
(: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?
2017-12-05 13:03:25 +00:00
#{constants/content-type-command constants/content-type-command-request}
type))
(def default-values
{:outgoing false
2017-12-23 20:27:04 +00:00
:to nil})
2017-10-19 13:58:21 +00:00
(defn exists? [message-id]
(data-store/exists? message-id))
(defn get-by-id
[message-id]
2017-10-19 13:58:21 +00:00
(data-store/get-by-id message-id))
(defn get-by-chat-id
([chat-id]
(get-by-chat-id chat-id 0))
([chat-id from]
2017-10-19 13:58:21 +00:00
(->> (data-store/get-by-chat-id chat-id from constants/default-number-of-messages)
(keep (fn [{:keys [content-type preview] :as message}]
(if (command-type? content-type)
2017-10-19 13:58:21 +00:00
(update message :content reader/read-string)
message))))))
(defn get-log-messages
[chat-id]
(->> (data-store/get-by-chat-id chat-id 0 100)
2017-10-19 13:58:21 +00:00
(filter #(= (:content-type %) constants/content-type-log-message))
(map #(select-keys % [:content :timestamp]))))
2016-12-13 10:56:00 +00:00
(defn get-last-clock-value
[chat-id]
(if-let [message (data-store/get-last-message chat-id)]
(:clock-value message)
0))
(defn get-unviewed
2017-12-23 20:27:04 +00:00
[current-public-key]
(into {}
(map (fn [[chat-id user-statuses]]
[chat-id (into #{} (map :message-id) user-statuses)]))
(group-by :chat-id (data-store/get-unviewed current-public-key))))
(defn- prepare-content [content]
2017-12-05 13:03:25 +00:00
(if (string? content)
content
(pr-str
;; TODO janherich: this is ugly and not systematic, define something like `:not-persisent`
2017-12-23 20:27:04 +00:00
;; option for command params instead
2017-12-05 13:03:25 +00:00
(update content :params dissoc :password :password-confirmation))))
2017-12-23 20:27:04 +00:00
(defn- prepare-statuses [{:keys [chat-id message-id] :as message}]
(utils/update-if-present message
:user-statuses
(partial map (fn [[whisper-identity status]]
2018-01-04 19:19:16 +00:00
{:status-id (str message-id "-" whisper-identity)
:whisper-identity whisper-identity
2017-12-23 20:27:04 +00:00
:status status
:chat-id chat-id
:message-id message-id}))))
(defn- prepare-message [message]
(-> message
prepare-statuses
(utils/update-if-present :content prepare-content)))
(defn save
[{:keys [message-id content from] :as message}]
(when-not (data-store/exists? message-id)
2017-12-23 20:27:04 +00:00
(data-store/save (prepare-message (merge default-values
message
{:from (or from "anonymous")
:timestamp (random/timestamp)})))))
(defn update-message
[{:keys [message-id] :as message}]
2017-12-23 20:27:04 +00:00
(when-let [{:keys [chat-id]} (data-store/get-by-id message-id)]
(data-store/save (prepare-message (assoc message :chat-id chat-id)))))
(defn delete-by-chat-id [chat-id]
(data-store/delete-by-chat-id chat-id))