From dfcf7cd47e63a0eaad02df0bc05ff561bafa0f72 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Tue, 14 Jun 2016 18:09:24 +0300 Subject: [PATCH] store preview --- src/status_im/chat/handlers.cljs | 29 +++++++++++++++------------ src/status_im/chat/views/message.cljs | 17 ++++++++-------- src/status_im/models/messages.cljs | 15 +++++++++----- src/status_im/persistence/realm.cljs | 4 +++- 4 files changed, 37 insertions(+), 28 deletions(-) diff --git a/src/status_im/chat/handlers.cljs b/src/status_im/chat/handlers.cljs index 2c5892a579..d7d6b4cc60 100644 --- a/src/status_im/chat/handlers.cljs +++ b/src/status_im/chat/handlers.cljs @@ -17,7 +17,8 @@ [status-im.handlers.server :as server] [status-im.utils.phone-number :refer [format-phone-number]] [status-im.utils.datetime :as time] - [status-im.components.jail :as j])) + [status-im.components.jail :as j] + [status-im.commands.utils :refer [generate-hiccup]])) (register-handler :set-show-actions (fn [db [_ show-actions]] @@ -149,17 +150,18 @@ (commands/set-chat-command db command) (assoc db :new-message (when-not (str/blank? text) message))))) -(defn prepare-command [identity chat-id staged-command] - (let [command-name (get-in staged-command [:command :name]) - content {:command command-name - :content (:content staged-command)}] - {:msg-id (random/id) - :from identity - :to chat-id - :content content - :content-type content-type-command - :outgoing true - :handler (:handler staged-command)})) +(defn prepare-command + [identity chat-id {:keys [preview preview-string content command]}] + (let [content {:command (command :name) + :content content}] + {:msg-id (random/id) + :from identity + :to chat-id + :content content + :content-type content-type-command + :outgoing true + :preview preview-string + :rendered-preview preview})) (defn prepare-staged-commans [{:keys [current-chat-id identity] :as db} _] @@ -212,7 +214,8 @@ (defn save-commands-to-realm! [{:keys [new-commands current-chat-id]} _] (doseq [new-command new-commands] - (messages/save-message current-chat-id (dissoc new-command :handler)))) + (messages/save-message current-chat-id + (dissoc new-command :rendered-preview)))) (defn invoke-commands-handlers! [{:keys [new-commands current-chat-id] :as db}] diff --git a/src/status_im/chat/views/message.cljs b/src/status_im/chat/views/message.cljs index 93c0cec826..8f3ed5cb27 100644 --- a/src/status_im/chat/views/message.cljs +++ b/src/status_im/chat/views/message.cljs @@ -51,9 +51,9 @@ [view st/track-mark] [text {:style st/track-duration-text} "03:39"]]]) -(defn message-content-command [content] +(defn message-content-command [content preview] (let [commands-atom (subscribe [:get-commands-and-responses])] - (fn [content] + (fn [content preview] (let [commands @commands-atom {:keys [command content]} (parse-command-msg-content commands content)] @@ -66,11 +66,10 @@ [view (st/command-image-view command) [image {:source {:uri (:icon command)} :style st/command-image}]] - [text {:style st/command-text} - ;; TODO isn't smart - (if (= (:name command) "keypair-password") - "******" - content)]])))) + (if preview + preview + [text {:style st/command-text} + content])])))) (defn set-chat-command [msg-id command] (dispatch [:set-response-chat-command msg-id (keyword (:name command))])) @@ -126,9 +125,9 @@ [message-content-status message]) (defmethod message-content content-type-command - [wrapper {:keys [content] :as message}] + [wrapper {:keys [content rendered-preview] :as message}] [wrapper message - [message-view message [message-content-command content]]]) + [message-view message [message-content-command content rendered-preview]]]) (defmethod message-content :default [wrapper {:keys [content-type content] :as message}] diff --git a/src/status_im/models/messages.cljs b/src/status_im/models/messages.cljs index 5c8c17fa5f..0193217439 100644 --- a/src/status_im/models/messages.cljs +++ b/src/status_im/models/messages.cljs @@ -3,11 +3,11 @@ [re-frame.core :refer [dispatch]] [cljs.reader :refer [read-string]] [status-im.utils.random :refer [timestamp]] - [status-im.db :as db] [status-im.utils.logging :as log] [clojure.string :refer [join split]] [clojure.walk :refer [stringify-keys keywordize-keys]] - [status-im.constants :as c])) + [status-im.constants :as c] + [status-im.commands.utils :refer [generate-hiccup]])) (defn- map-to-str [m] @@ -21,7 +21,8 @@ {:outgoing false :to nil :same-author false - :same-direction false}) + :same-direction false + :preview nil}) (defn save-message ;; todo remove chat-id parameter @@ -51,9 +52,13 @@ (r/sorted :timestamp :asc) (r/collection->map)) (into '()) - (map (fn [{:keys [content-type] :as message}] + (map (fn [{:keys [content-type preview] :as message}] (if (command-type? content-type) - (update message :content str-to-map) + (-> message + (update :content str-to-map) + (assoc :rendered-preview (generate-hiccup + (read-string preview))) + (dissoc :preview)) message))))) (defn update-message! [{:keys [msg-id] :as msg}] diff --git a/src/status_im/persistence/realm.cljs b/src/status_im/persistence/realm.cljs index fd6e9857ad..8ed342537a 100644 --- a/src/status_im/persistence/realm.cljs +++ b/src/status_im/persistence/realm.cljs @@ -33,7 +33,9 @@ :delivery-status {:type "string" :optional true} :same-author "bool" - :same-direction "bool"}} + :same-direction "bool" + :preview {:type :string + :optional true}}} {:name :chat-contact :properties {:identity "string" :is-in-chat {:type "bool"