2016-05-19 18:31:56 +02:00
|
|
|
(ns status-im.chat.handlers
|
2016-05-16 10:45:59 +03:00
|
|
|
(:require [re-frame.core :refer [register-handler enrich after debug dispatch]]
|
2016-05-19 18:31:56 +02:00
|
|
|
[status-im.models.commands :as commands]
|
2016-05-10 13:58:37 +03:00
|
|
|
[clojure.string :as str]
|
2016-06-01 16:36:43 +03:00
|
|
|
[status-im.components.drag-drop :as drag]
|
2016-05-30 16:50:23 +03:00
|
|
|
[status-im.components.animation :as anim]
|
2016-05-23 13:50:46 +03:00
|
|
|
[status-im.components.styles :refer [default-chat-color]]
|
2016-05-31 14:23:30 +03:00
|
|
|
[status-im.chat.styles.response :as response-styles]
|
|
|
|
[status-im.chat.styles.response-suggestions :as response-suggestions-styles]
|
2016-05-19 18:31:56 +02:00
|
|
|
[status-im.chat.suggestions :as suggestions]
|
|
|
|
[status-im.protocol.api :as api]
|
|
|
|
[status-im.models.messages :as messages]
|
|
|
|
[status-im.constants :refer [text-content-type
|
2016-05-30 16:50:23 +03:00
|
|
|
content-type-command
|
|
|
|
response-input-hiding-duration]]
|
2016-05-19 18:31:56 +02:00
|
|
|
[status-im.utils.random :as random]
|
|
|
|
[status-im.chat.sign-up :as sign-up-service]
|
|
|
|
[status-im.models.chats :as chats]
|
|
|
|
[status-im.navigation.handlers :as nav]
|
2016-05-20 14:20:48 +03:00
|
|
|
[status-im.utils.handlers :as u]
|
2016-05-24 11:02:08 +03:00
|
|
|
[status-im.persistence.realm :as r]
|
2016-05-20 14:20:48 +03:00
|
|
|
[status-im.handlers.server :as server]
|
2016-05-30 16:50:23 +03:00
|
|
|
[status-im.handlers.content-suggestions :refer [get-content-suggestions]]
|
2016-05-20 14:20:48 +03:00
|
|
|
[status-im.utils.phone-number :refer [format-phone-number]]
|
|
|
|
[status-im.utils.datetime :as time]))
|
2016-05-10 13:58:37 +03:00
|
|
|
|
2016-05-31 14:23:30 +03:00
|
|
|
(def delta 1)
|
|
|
|
|
2016-05-10 13:58:37 +03:00
|
|
|
(register-handler :set-show-actions
|
|
|
|
(fn [db [_ show-actions]]
|
2016-05-10 17:40:21 +03:00
|
|
|
(assoc db :show-actions show-actions)))
|
2016-05-10 13:58:37 +03:00
|
|
|
|
|
|
|
(register-handler :load-more-messages
|
|
|
|
(fn [db _]
|
|
|
|
db
|
|
|
|
;; TODO implement
|
|
|
|
#_(let [chat-id (get-in db [:chat :current-chat-id])
|
|
|
|
messages [:chats chat-id :messages]
|
|
|
|
new-messages (gen-messages 10)]
|
|
|
|
(update-in db messages concat new-messages))))
|
|
|
|
|
|
|
|
(defn safe-trim [s]
|
|
|
|
(when (string? s)
|
|
|
|
(str/trim s)))
|
|
|
|
|
|
|
|
(register-handler :cancel-command
|
|
|
|
(fn [{:keys [current-chat-id] :as db} _]
|
|
|
|
(-> db
|
2016-05-30 16:50:23 +03:00
|
|
|
(assoc-in [:animations :response-input-is-hiding?] false)
|
2016-05-10 13:58:37 +03:00
|
|
|
(assoc-in [:chats current-chat-id :command-input] {})
|
|
|
|
(update-in [:chats current-chat-id :input-text] safe-trim))))
|
|
|
|
|
2016-06-01 14:09:25 +03:00
|
|
|
(defn animate-cancel-command! [{{:keys [response-suggestions-height
|
|
|
|
message-input-buttons-scale
|
|
|
|
message-input-offset]} :animations}]
|
|
|
|
(let [height-to-value 1]
|
|
|
|
(anim/add-listener response-suggestions-height
|
2016-05-31 14:23:30 +03:00
|
|
|
(fn [val]
|
2016-06-01 14:09:25 +03:00
|
|
|
(when (<= (- height-to-value delta) (anim/value val) (+ height-to-value delta))
|
|
|
|
(anim/remove-all-listeners response-suggestions-height)
|
2016-05-31 14:23:30 +03:00
|
|
|
(dispatch [:cancel-command]))))
|
2016-06-01 14:09:25 +03:00
|
|
|
(anim/start (anim/spring response-suggestions-height {:toValue height-to-value
|
|
|
|
:velocity 1
|
|
|
|
:tension 1
|
|
|
|
:friction 5}))
|
|
|
|
(anim/start (anim/timing message-input-buttons-scale {:toValue 1
|
|
|
|
:duration response-input-hiding-duration}))
|
|
|
|
(anim/start (anim/timing message-input-offset {:toValue 0
|
|
|
|
:duration response-input-hiding-duration}))))
|
2016-05-30 16:50:23 +03:00
|
|
|
|
|
|
|
(register-handler :start-cancel-command
|
2016-05-31 17:05:31 +03:00
|
|
|
(after animate-cancel-command!)
|
|
|
|
(fn [db _]
|
2016-06-01 14:09:25 +03:00
|
|
|
(let [hiding? (get-in db [:animations :response-input-is-hiding?])]
|
2016-05-31 17:05:31 +03:00
|
|
|
(if-not hiding?
|
|
|
|
(assoc-in db [:animations :response-input-is-hiding?] true)
|
|
|
|
db))))
|
2016-05-30 16:50:23 +03:00
|
|
|
|
2016-05-31 17:05:31 +03:00
|
|
|
(defn update-response-suggestions-height! [db]
|
2016-05-31 14:23:30 +03:00
|
|
|
(when (and (not (get-in db [:animations :response-input-is-hiding?]))
|
|
|
|
(commands/get-chat-command-to-msg-id db))
|
|
|
|
(let [command (commands/get-chat-command db)
|
|
|
|
text (commands/get-chat-command-content db)
|
|
|
|
suggestions (get-content-suggestions command text)
|
|
|
|
suggestions-height (min response-suggestions-styles/max-suggestions-height
|
|
|
|
(reduce + 0 (map #(if (:header %)
|
|
|
|
response-suggestions-styles/header-height
|
|
|
|
response-suggestions-styles/suggestion-height)
|
|
|
|
suggestions)))
|
|
|
|
height (+ suggestions-height response-styles/request-info-height)
|
|
|
|
anim-value (get-in db [:animations :response-suggestions-height])]
|
|
|
|
(anim/start
|
|
|
|
(anim/spring anim-value {:toValue height
|
2016-06-01 14:09:25 +03:00
|
|
|
:speed 1
|
|
|
|
:bounciness 0.2})))))
|
2016-05-31 14:23:30 +03:00
|
|
|
|
2016-05-10 13:58:37 +03:00
|
|
|
(register-handler :set-chat-command-content
|
2016-05-31 17:05:31 +03:00
|
|
|
(after update-response-suggestions-height!)
|
2016-05-24 16:34:46 +03:00
|
|
|
(fn [{:keys [current-chat-id] :as db} [_ content]]
|
|
|
|
(-> db
|
|
|
|
(commands/set-chat-command-content content)
|
2016-05-31 17:05:31 +03:00
|
|
|
(assoc-in [:chats current-chat-id :input-text] nil))))
|
2016-05-10 13:58:37 +03:00
|
|
|
|
2016-05-10 17:40:21 +03:00
|
|
|
(defn update-input-text
|
|
|
|
[{:keys [current-chat-id] :as db} text]
|
|
|
|
(assoc-in db [:chats current-chat-id :input-text] text))
|
|
|
|
|
2016-05-10 13:58:37 +03:00
|
|
|
(register-handler :stage-command
|
|
|
|
(fn [{:keys [current-chat-id] :as db} _]
|
2016-05-10 17:40:21 +03:00
|
|
|
(let [db (update-input-text db nil)
|
2016-05-10 13:58:37 +03:00
|
|
|
{:keys [command content]}
|
|
|
|
(get-in db [:chats current-chat-id :command-input])
|
|
|
|
command-info {:command command
|
|
|
|
:content content
|
|
|
|
:handler (:handler command)}]
|
|
|
|
(commands/stage-command db command-info))))
|
|
|
|
|
2016-06-01 14:09:25 +03:00
|
|
|
(register-handler :finish-show-response!
|
|
|
|
(fn [db _]
|
|
|
|
(assoc-in db [:animations :commands-input-is-switching?] false)))
|
|
|
|
|
|
|
|
(defn animate-show-response! [{{scale-anim-value :message-input-buttons-scale
|
|
|
|
offset-anim-value :message-input-offset} :animations}]
|
|
|
|
(let [to-value 0.1
|
|
|
|
delta 0.02]
|
|
|
|
(anim/add-listener scale-anim-value
|
|
|
|
(fn [val]
|
|
|
|
(when (<= (- to-value delta) (anim/value val) (+ to-value delta))
|
|
|
|
(anim/remove-all-listeners scale-anim-value)
|
|
|
|
(dispatch [:finish-show-response!]))))
|
|
|
|
(anim/start (anim/timing scale-anim-value {:toValue to-value
|
|
|
|
:duration response-input-hiding-duration}))
|
|
|
|
(anim/start (anim/timing offset-anim-value {:toValue -40
|
|
|
|
:duration response-input-hiding-duration}))))
|
|
|
|
|
2016-06-01 16:36:43 +03:00
|
|
|
(defn set-response-chat-command [db [_ to-msg-id command-key]]
|
2016-06-01 14:09:25 +03:00
|
|
|
(-> db
|
|
|
|
(commands/set-response-chat-command to-msg-id command-key)
|
|
|
|
(assoc-in [:animations :commands-input-is-switching?] true)))
|
|
|
|
|
2016-05-10 13:58:37 +03:00
|
|
|
(register-handler :set-response-chat-command
|
2016-06-01 16:36:43 +03:00
|
|
|
(-> set-response-chat-command
|
2016-06-01 14:09:25 +03:00
|
|
|
((after animate-show-response!))
|
|
|
|
((after update-response-suggestions-height!))))
|
2016-05-10 13:58:37 +03:00
|
|
|
|
2016-05-10 17:40:21 +03:00
|
|
|
(defn update-text
|
|
|
|
[db [_ text]]
|
|
|
|
(update-input-text db text))
|
2016-05-10 13:58:37 +03:00
|
|
|
|
|
|
|
(defn update-command [db [_ text]]
|
2016-05-26 16:54:18 +03:00
|
|
|
(if-not (commands/get-chat-command db)
|
2016-05-24 16:34:46 +03:00
|
|
|
(let [{:keys [command]} (suggestions/check-suggestion db text)]
|
|
|
|
(if command
|
|
|
|
(commands/set-chat-command db command)
|
|
|
|
db))
|
|
|
|
db))
|
2016-05-10 13:58:37 +03:00
|
|
|
|
|
|
|
(register-handler :set-chat-input-text
|
|
|
|
((enrich update-command) update-text))
|
|
|
|
|
|
|
|
(defn console? [s]
|
|
|
|
(= "console" s))
|
|
|
|
|
|
|
|
(def not-console?
|
|
|
|
(complement console?))
|
|
|
|
|
|
|
|
(defn check-author-direction
|
|
|
|
[db chat-id {:keys [from outgoing] :as message}]
|
|
|
|
(let [previous-message (first (get-in db [:chats chat-id :messages]))]
|
|
|
|
(merge message
|
|
|
|
{:same-author (if previous-message
|
|
|
|
(= (:from previous-message) from)
|
|
|
|
true)
|
|
|
|
:same-direction (if previous-message
|
|
|
|
(= (:outgoing previous-message) outgoing)
|
|
|
|
true)})))
|
|
|
|
|
|
|
|
(defn add-message-to-db
|
|
|
|
[db chat-id message]
|
|
|
|
(let [messages [:chats chat-id :messages]]
|
|
|
|
(update-in db messages conj message)))
|
|
|
|
|
|
|
|
(defn prepare-message
|
|
|
|
[{:keys [identity current-chat-id] :as db} _]
|
|
|
|
(let [text (get-in db [:chats current-chat-id :input-text])
|
|
|
|
{:keys [command]} (suggestions/check-suggestion db (str text " "))
|
|
|
|
message (check-author-direction
|
|
|
|
db current-chat-id
|
2016-05-20 12:41:55 +03:00
|
|
|
{:msg-id (random/id)
|
|
|
|
:chat-id current-chat-id
|
|
|
|
:content text
|
|
|
|
:to current-chat-id
|
|
|
|
:from identity
|
|
|
|
:content-type text-content-type
|
|
|
|
:outgoing true
|
|
|
|
:timestamp (time/now-ms)})]
|
2016-05-10 13:58:37 +03:00
|
|
|
(if command
|
|
|
|
(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-key (get-in staged-command [:command :command])
|
|
|
|
content {:command (name command-key)
|
|
|
|
: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-staged-commans
|
|
|
|
[{:keys [current-chat-id identity] :as db} _]
|
|
|
|
(let [staged-commands (get-in db [:chats current-chat-id :staged-commands])]
|
|
|
|
(->> staged-commands
|
|
|
|
(map #(prepare-command identity current-chat-id %))
|
|
|
|
;todo this is wrong :(
|
|
|
|
(map #(check-author-direction db current-chat-id %))
|
|
|
|
(assoc db :new-commands))))
|
|
|
|
|
|
|
|
(defn add-message
|
|
|
|
[{:keys [new-message current-chat-id] :as db}]
|
|
|
|
(if new-message
|
|
|
|
(add-message-to-db db current-chat-id new-message)
|
|
|
|
db))
|
|
|
|
|
|
|
|
(defn add-commands
|
|
|
|
[{:keys [new-commands current-chat-id] :as db}]
|
|
|
|
(reduce
|
|
|
|
#(add-message-to-db %1 current-chat-id %2)
|
|
|
|
db
|
|
|
|
new-commands))
|
|
|
|
|
|
|
|
(defn clear-input
|
|
|
|
[{:keys [current-chat-id new-message] :as db} _]
|
|
|
|
(if new-message
|
|
|
|
(assoc-in db [:chats current-chat-id :input-text] nil)
|
|
|
|
db))
|
|
|
|
|
|
|
|
(defn clear-staged-commands
|
|
|
|
[{:keys [current-chat-id] :as db} _]
|
|
|
|
(assoc-in db [:chats current-chat-id :staged-commands] []))
|
|
|
|
|
|
|
|
(defn send-message!
|
2016-05-19 18:03:15 +03:00
|
|
|
[{:keys [new-message current-chat-id] :as db} _]
|
2016-05-10 13:58:37 +03:00
|
|
|
(when (and new-message (not-console? current-chat-id))
|
2016-05-19 18:03:15 +03:00
|
|
|
(let [{:keys [group-chat]} (get-in db [:chats current-chat-id])
|
|
|
|
content (:content new-message)]
|
|
|
|
(if group-chat
|
|
|
|
(api/send-group-user-msg {:group-id current-chat-id
|
|
|
|
:content content})
|
|
|
|
(api/send-user-msg {:to current-chat-id
|
|
|
|
:content content})))))
|
2016-05-10 13:58:37 +03:00
|
|
|
|
|
|
|
(defn save-message-to-realm!
|
|
|
|
[{:keys [new-message current-chat-id]} _]
|
|
|
|
(when new-message
|
|
|
|
(messages/save-message current-chat-id new-message)))
|
|
|
|
|
|
|
|
(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))))
|
|
|
|
|
|
|
|
(defn handle-commands
|
|
|
|
[{:keys [new-commands]}]
|
|
|
|
(doseq [{{content :content} :content
|
|
|
|
handler :handler} new-commands]
|
|
|
|
(when handler
|
|
|
|
(handler content))))
|
|
|
|
|
|
|
|
(register-handler :send-chat-msg
|
|
|
|
(-> prepare-message
|
|
|
|
((enrich prepare-staged-commans))
|
|
|
|
((enrich add-message))
|
|
|
|
((enrich add-commands))
|
|
|
|
((enrich clear-input))
|
|
|
|
((enrich clear-staged-commands))
|
2016-05-24 12:05:47 +03:00
|
|
|
((after send-message!))
|
2016-05-10 13:58:37 +03:00
|
|
|
((after save-message-to-realm!))
|
|
|
|
((after save-commands-to-realm!))
|
|
|
|
((after handle-commands))))
|
|
|
|
|
|
|
|
(register-handler :unstage-command
|
|
|
|
(fn [db [_ staged-command]]
|
2016-05-19 18:03:15 +03:00
|
|
|
(commands/unstage-command db staged-command)))
|
2016-05-10 13:58:37 +03:00
|
|
|
|
|
|
|
(register-handler :set-chat-command
|
|
|
|
(fn [db [_ command-key]]
|
|
|
|
;; todo what is going on there?!
|
|
|
|
(commands/set-chat-command db command-key)))
|
|
|
|
|
|
|
|
(register-handler :init-console-chat
|
|
|
|
(fn [db [_]]
|
|
|
|
(sign-up-service/init db)))
|
|
|
|
|
|
|
|
(register-handler :save-password
|
|
|
|
(fn [db [_ password]]
|
|
|
|
(sign-up-service/save-password password)
|
|
|
|
(assoc db :password-saved true)))
|
|
|
|
|
|
|
|
(register-handler :sign-up
|
2016-05-18 16:45:03 +03:00
|
|
|
(fn [db [_ phone-number]]
|
|
|
|
;; todo save phone number to db
|
|
|
|
(let [formatted (format-phone-number phone-number)]
|
|
|
|
(-> db
|
|
|
|
(assoc :user-phone-number formatted)
|
2016-05-18 18:40:01 +03:00
|
|
|
sign-up-service/start-listening-confirmation-code-sms
|
2016-05-18 16:45:03 +03:00
|
|
|
(server/sign-up formatted sign-up-service/on-sign-up-response)))))
|
2016-05-10 13:58:37 +03:00
|
|
|
|
2016-05-18 18:40:01 +03:00
|
|
|
(register-handler :stop-listening-confirmation-code-sms
|
|
|
|
(fn [db [_]]
|
|
|
|
(sign-up-service/stop-listening-confirmation-code-sms db)))
|
|
|
|
|
2016-05-10 13:58:37 +03:00
|
|
|
(register-handler :sign-up-confirm
|
|
|
|
(fn [db [_ confirmation-code]]
|
2016-05-18 16:45:03 +03:00
|
|
|
(server/sign-up-confirm confirmation-code sign-up-service/on-send-code-response)
|
|
|
|
db))
|
2016-05-10 13:58:37 +03:00
|
|
|
|
|
|
|
(register-handler :set-signed-up
|
|
|
|
(fn [db [_ signed-up]]
|
|
|
|
(sign-up-service/set-signed-up db signed-up)))
|
|
|
|
|
|
|
|
(defn load-messages!
|
2016-05-16 10:45:59 +03:00
|
|
|
([db] (load-messages! db nil))
|
|
|
|
([db _]
|
|
|
|
(->> (:current-chat-id db)
|
|
|
|
messages/get-messages
|
|
|
|
(assoc db :messages))))
|
2016-05-10 13:58:37 +03:00
|
|
|
|
2016-06-01 16:36:43 +03:00
|
|
|
(defn create-response-pan-responder [pan]
|
|
|
|
(drag/create-pan-responder
|
|
|
|
{:on-move (anim/event {:dy (anim/y pan)})
|
|
|
|
:on-release (fn [e gesture]
|
|
|
|
(anim/start (anim/spring pan
|
|
|
|
{:toValue {:x 0, :y 0}})))}))
|
|
|
|
|
|
|
|
(defn init-response-dragging [db]
|
|
|
|
(let [pan (anim/create-value-xy 0 0)]
|
|
|
|
(-> db
|
|
|
|
(assoc-in [:animations :response-pan] pan)
|
|
|
|
(assoc-in [:animations :response-pan-responder] (create-response-pan-responder pan)))))
|
|
|
|
|
2016-05-10 13:58:37 +03:00
|
|
|
(defn init-chat
|
2016-05-16 10:45:59 +03:00
|
|
|
([db] (init-chat db nil))
|
|
|
|
([{:keys [messages current-chat-id] :as db} _]
|
2016-06-01 16:36:43 +03:00
|
|
|
(-> db
|
|
|
|
(assoc-in [:chats current-chat-id :messages] messages)
|
|
|
|
(init-response-dragging))))
|
2016-05-10 13:58:37 +03:00
|
|
|
|
|
|
|
(register-handler :init-chat
|
|
|
|
(-> load-messages!
|
|
|
|
((enrich init-chat))
|
|
|
|
debug))
|
|
|
|
|
|
|
|
(defn initialize-chats
|
|
|
|
[{:keys [loaded-chats] :as db} _]
|
|
|
|
(let [chats (->> loaded-chats
|
|
|
|
(map (fn [{:keys [chat-id] :as chat}]
|
|
|
|
[chat-id chat]))
|
2016-05-16 10:45:59 +03:00
|
|
|
(into {}))
|
|
|
|
ids (set (keys chats))]
|
2016-05-10 13:58:37 +03:00
|
|
|
(-> db
|
|
|
|
(assoc :chats chats)
|
2016-05-16 10:45:59 +03:00
|
|
|
(assoc :chats-ids ids)
|
2016-05-10 13:58:37 +03:00
|
|
|
(dissoc :loaded-chats))))
|
|
|
|
|
|
|
|
(defn load-chats!
|
|
|
|
[db _]
|
|
|
|
(assoc db :loaded-chats (chats/chats-list)))
|
|
|
|
|
|
|
|
(register-handler :initialize-chats
|
|
|
|
((enrich initialize-chats) load-chats!))
|
|
|
|
|
|
|
|
(defn store-message!
|
|
|
|
[{:keys [new-message]} [_ {chat-id :from}]]
|
|
|
|
(messages/save-message chat-id new-message))
|
|
|
|
|
|
|
|
(defn receive-message
|
|
|
|
[db [_ {chat-id :from :as message}]]
|
|
|
|
(let [message' (check-author-direction db chat-id message)]
|
|
|
|
(-> db
|
|
|
|
(add-message-to-db chat-id message')
|
|
|
|
(assoc :new-message message'))))
|
|
|
|
|
|
|
|
(register-handler :received-msg
|
|
|
|
(-> receive-message
|
|
|
|
((after store-message!))))
|
|
|
|
|
|
|
|
(register-handler :group-received-msg
|
2016-05-17 13:32:20 +03:00
|
|
|
(u/side-effect!
|
|
|
|
(fn [_ [_ {chat-id :group-id :as msg}]]
|
|
|
|
(messages/save-message chat-id msg))))
|
2016-05-16 10:45:59 +03:00
|
|
|
|
|
|
|
(defmethod nav/preload-data! :chat
|
|
|
|
[{:keys [current-chat-id] :as db} [_ _ id]]
|
2016-05-21 16:32:04 +03:00
|
|
|
(let [chat-id (or id current-chat-id)
|
|
|
|
messages (get-in db [:chats chat-id :messages])
|
|
|
|
db' (assoc db :current-chat-id chat-id)]
|
2016-05-20 19:12:04 +03:00
|
|
|
(if (seq messages)
|
2016-05-21 16:32:04 +03:00
|
|
|
db'
|
|
|
|
(-> db'
|
2016-05-20 19:12:04 +03:00
|
|
|
load-messages!
|
2016-05-21 16:32:04 +03:00
|
|
|
init-chat))))
|
2016-05-16 10:45:59 +03:00
|
|
|
|
|
|
|
(defn prepare-chat
|
|
|
|
[{:keys [contacts] :as db} [_ contcat-id]]
|
|
|
|
(let [name (get-in contacts [contcat-id :name])
|
|
|
|
chat {:chat-id contcat-id
|
|
|
|
:name name
|
2016-05-23 13:50:46 +03:00
|
|
|
:color default-chat-color
|
2016-05-16 10:45:59 +03:00
|
|
|
:group-chat false
|
|
|
|
:is-active true
|
|
|
|
:timestamp (.getTime (js/Date.))
|
2016-05-19 18:03:15 +03:00
|
|
|
:contacts [{:identity contcat-id}]}]
|
2016-05-16 10:45:59 +03:00
|
|
|
(assoc db :new-chat chat)))
|
|
|
|
|
|
|
|
(defn add-chat [{:keys [new-chat] :as db} [_ chat-id]]
|
|
|
|
(-> db
|
|
|
|
(update :chats assoc chat-id new-chat)
|
|
|
|
(update :chats-ids conj chat-id)))
|
|
|
|
|
|
|
|
(defn save-chat!
|
|
|
|
[{:keys [new-chat]} _]
|
|
|
|
(chats/create-chat new-chat))
|
|
|
|
|
|
|
|
(defn open-chat!
|
|
|
|
[_ [_ chat-id]]
|
|
|
|
(dispatch [:navigate-to :chat chat-id]))
|
|
|
|
|
|
|
|
(register-handler :start-chat
|
|
|
|
(-> prepare-chat
|
|
|
|
((enrich add-chat))
|
|
|
|
((after save-chat!))
|
2016-05-19 18:03:15 +03:00
|
|
|
((after open-chat!))))
|
2016-05-16 12:31:08 +03:00
|
|
|
|
|
|
|
(register-handler :switch-command-suggestions
|
|
|
|
(fn [db [_]]
|
|
|
|
(suggestions/switch-command-suggestions db)))
|
2016-05-21 15:23:24 +03:00
|
|
|
|
|
|
|
(defn remove-chat
|
|
|
|
[{:keys [current-chat-id] :as db} _]
|
|
|
|
(update db :chats dissoc current-chat-id))
|
|
|
|
|
|
|
|
(defn notify-about-leaving!
|
|
|
|
[{:keys [current-chat-id]} _]
|
|
|
|
(api/leave-group-chat current-chat-id))
|
|
|
|
|
|
|
|
; todo do we really need this message?
|
|
|
|
(defn leaving-message!
|
|
|
|
[{:keys [current-chat-id]} _]
|
|
|
|
(messages/save-message
|
|
|
|
current-chat-id
|
|
|
|
{:from "system"
|
|
|
|
:msg-id (random/id)
|
|
|
|
:content "You left this chat"
|
|
|
|
:content-type text-content-type}))
|
|
|
|
|
|
|
|
(defn delete-messages!
|
|
|
|
[{:keys [current-chat-id]} _]
|
|
|
|
(r/write
|
|
|
|
(fn []
|
|
|
|
(r/delete (r/get-by-field :msgs :chat-id current-chat-id)))))
|
|
|
|
|
|
|
|
(defn delete-chat!
|
|
|
|
[{:keys [current-chat-id]} _]
|
|
|
|
(r/write
|
|
|
|
(fn []
|
|
|
|
(-> (r/get-by-field :chats :chat-id current-chat-id)
|
|
|
|
(r/single)
|
|
|
|
(r/delete)))))
|
|
|
|
|
|
|
|
(register-handler :leave-group-chat
|
|
|
|
;; todo oreder of operations tbd
|
|
|
|
(after (fn [_ _] (dispatch [:navigation-replace :chat-list])))
|
|
|
|
(-> remove-chat
|
|
|
|
;; todo uncomment
|
|
|
|
;((after notify-about-leaving!))
|
|
|
|
;((after leaving-message!))
|
|
|
|
((after delete-messages!))
|
|
|
|
((after delete-chat!))))
|