2016-10-04 11:49:59 +00:00
|
|
|
(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]))
|
2016-10-04 11:49:59 +00:00
|
|
|
|
|
|
|
(defn- command-type?
|
|
|
|
[type]
|
|
|
|
(contains?
|
2017-12-05 13:03:25 +00:00
|
|
|
#{constants/content-type-command constants/content-type-command-request}
|
2017-08-15 20:56:44 +00:00
|
|
|
type))
|
2016-10-04 11:49:59 +00:00
|
|
|
|
|
|
|
(def default-values
|
|
|
|
{:outgoing false
|
2017-12-23 20:27:04 +00:00
|
|
|
:to nil})
|
2016-10-04 11:49:59 +00:00
|
|
|
|
2017-10-19 13:58:21 +00:00
|
|
|
(defn exists? [message-id]
|
|
|
|
(data-store/exists? message-id))
|
|
|
|
|
2016-10-04 11:49:59 +00:00
|
|
|
(defn get-by-id
|
|
|
|
[message-id]
|
2017-10-19 13:58:21 +00:00
|
|
|
(data-store/get-by-id message-id))
|
2016-10-04 11:49:59 +00:00
|
|
|
|
|
|
|
(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)
|
2016-10-04 11:49:59 +00:00
|
|
|
(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)
|
2016-10-04 11:49:59 +00:00
|
|
|
message))))))
|
|
|
|
|
2017-05-21 14:52:52 +00:00
|
|
|
(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))
|
2017-05-21 14:52:52 +00:00
|
|
|
(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))
|
2016-10-04 11:49:59 +00:00
|
|
|
|
|
|
|
(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))))
|
2016-10-04 11:49:59 +00:00
|
|
|
|
2017-10-27 13:22:53 +00:00
|
|
|
(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-10-27 13:22:53 +00:00
|
|
|
|
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}]
|
2016-10-04 11:49:59 +00:00
|
|
|
(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)})))))
|
2016-10-04 11:49:59 +00:00
|
|
|
|
2017-10-27 13:22:53 +00:00
|
|
|
(defn update-message
|
2016-10-04 11:49:59 +00:00
|
|
|
[{: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)))))
|
2016-10-04 11:49:59 +00:00
|
|
|
|
|
|
|
(defn delete-by-chat-id [chat-id]
|
|
|
|
(data-store/delete-by-chat-id chat-id))
|