2016-03-23 16:48:26 +02:00
|
|
|
(ns syng-im.handlers
|
|
|
|
(:require
|
2016-05-06 14:06:58 +03:00
|
|
|
[re-frame.core :refer [register-handler after dispatch debug enrich]]
|
2016-04-29 16:24:03 +03:00
|
|
|
[schema.core :as s :include-macros true]
|
2016-05-10 12:31:48 +03:00
|
|
|
[syng-im.persistence.simple-kv-store :as kv]
|
|
|
|
[syng-im.protocol.state.storage :as storage]
|
2016-04-29 16:24:03 +03:00
|
|
|
[syng-im.db :as db :refer [app-db schema]]
|
|
|
|
[syng-im.protocol.api :refer [init-protocol]]
|
|
|
|
[syng-im.protocol.protocol-handler :refer [make-handler]]
|
|
|
|
[syng-im.models.protocol :refer [update-identity
|
|
|
|
set-initialized]]
|
|
|
|
[syng-im.models.user-data :as user-data]
|
|
|
|
[syng-im.models.contacts :as contacts]
|
|
|
|
[syng-im.models.messages :refer [save-message
|
|
|
|
update-message!
|
2016-05-06 14:06:58 +03:00
|
|
|
message-by-id
|
2016-05-16 15:19:13 +03:00
|
|
|
get-messages
|
|
|
|
clear-history]]
|
2016-04-29 16:24:03 +03:00
|
|
|
[syng-im.models.commands :as commands :refer [set-chat-command
|
|
|
|
set-response-chat-command
|
|
|
|
set-chat-command-content
|
|
|
|
set-chat-command-request
|
|
|
|
stage-command
|
|
|
|
unstage-command
|
|
|
|
set-commands]]
|
|
|
|
[syng-im.handlers.server :as server]
|
|
|
|
[syng-im.handlers.contacts :as contacts-service]
|
|
|
|
[syng-im.handlers.suggestions :refer [get-command
|
|
|
|
handle-command
|
|
|
|
get-command-handler
|
|
|
|
load-commands
|
|
|
|
apply-staged-commands
|
2016-05-10 13:12:49 +03:00
|
|
|
check-suggestion
|
|
|
|
switch-command-suggestions]]
|
2016-04-29 16:24:03 +03:00
|
|
|
[syng-im.handlers.sign-up :as sign-up-service]
|
2016-05-07 18:04:19 +03:00
|
|
|
[syng-im.components.discovery.handlers :as discovery]
|
2016-04-29 16:24:03 +03:00
|
|
|
[syng-im.models.chats :refer [chat-exists?
|
|
|
|
create-chat
|
2016-05-13 15:15:40 +03:00
|
|
|
set-group-chat-name
|
2016-05-16 13:47:34 +03:00
|
|
|
set-chat-color
|
2016-05-13 14:17:03 +03:00
|
|
|
chat-remove-member
|
2016-04-29 16:24:03 +03:00
|
|
|
chat-add-participants
|
|
|
|
chat-remove-participants
|
|
|
|
set-chat-active
|
2016-05-16 15:19:13 +03:00
|
|
|
delete-chat
|
2016-05-06 14:06:58 +03:00
|
|
|
re-join-group-chat
|
|
|
|
chat-by-id2] :as chats]
|
2016-04-29 16:24:03 +03:00
|
|
|
[syng-im.models.chat :refer [signal-chat-updated
|
|
|
|
set-current-chat-id
|
|
|
|
current-chat-id
|
|
|
|
update-new-group-selection
|
|
|
|
update-new-participants-selection
|
|
|
|
clear-new-group
|
|
|
|
clear-new-participants
|
|
|
|
new-group-selection
|
|
|
|
set-chat-input-text
|
|
|
|
new-participants-selection]]
|
|
|
|
[syng-im.utils.logging :as log]
|
|
|
|
[syng-im.protocol.api :as api]
|
|
|
|
[syng-im.constants :refer [text-content-type
|
|
|
|
content-type-command]]
|
|
|
|
[syng-im.navigation :refer [nav-push
|
|
|
|
nav-replace
|
|
|
|
nav-pop]]
|
|
|
|
[syng-im.utils.crypt :refer [gen-random-bytes]]
|
2016-05-06 14:06:58 +03:00
|
|
|
[syng-im.utils.random :as random]
|
|
|
|
[clojure.string :as str]
|
|
|
|
[syng-im.components.react :as r]))
|
2016-03-23 16:48:26 +02:00
|
|
|
|
|
|
|
;; -- Middleware ------------------------------------------------------------
|
|
|
|
;;
|
|
|
|
;; See https://github.com/Day8/re-frame/wiki/Using-Handler-Middleware
|
|
|
|
;;
|
|
|
|
(defn check-and-throw
|
|
|
|
"throw an exception if db doesn't match the schema."
|
|
|
|
[a-schema db]
|
2016-03-23 21:05:42 +02:00
|
|
|
(if-let [problems (s/check a-schema db)]
|
|
|
|
(throw (js/Error. (str "schema check failed: " problems)))))
|
2016-03-23 16:48:26 +02:00
|
|
|
|
|
|
|
(def validate-schema-mw
|
|
|
|
(after (partial check-and-throw schema)))
|
|
|
|
|
2016-03-29 23:45:31 +03:00
|
|
|
|
|
|
|
;; -- Common --------------------------------------------------------------
|
2016-03-23 16:48:26 +02:00
|
|
|
|
2016-03-23 21:05:42 +02:00
|
|
|
(register-handler :initialize-db
|
2016-05-10 12:31:48 +03:00
|
|
|
(fn [_ _]
|
|
|
|
(assoc app-db
|
|
|
|
:signed-up (storage/get kv/kv-store :signed-up))))
|
2016-03-23 16:48:26 +02:00
|
|
|
|
2016-03-28 15:14:57 +03:00
|
|
|
(register-handler :set-loading
|
|
|
|
(fn [db [_ value]]
|
|
|
|
(assoc db :loading value)))
|
|
|
|
|
2016-04-03 19:00:40 +03:00
|
|
|
(register-handler :initialize-crypt
|
|
|
|
(fn [db _]
|
|
|
|
(log/debug "initializing crypt")
|
|
|
|
(gen-random-bytes 1024 (fn [{:keys [error buffer]}]
|
|
|
|
(if error
|
|
|
|
(do
|
|
|
|
(log/error "Failed to generate random bytes to initialize sjcl crypto")
|
|
|
|
(dispatch [:notify-user {:type :error
|
|
|
|
:error error}]))
|
|
|
|
(do
|
|
|
|
(->> (.toString buffer "hex")
|
|
|
|
(.toBits (.. js/ecc -sjcl -codec -hex))
|
|
|
|
(.addEntropy (.. js/ecc -sjcl -random)))
|
|
|
|
(dispatch [:crypt-initialized])))))
|
|
|
|
db))
|
|
|
|
|
|
|
|
(register-handler :crypt-initialized
|
|
|
|
(fn [db _]
|
|
|
|
(log/debug "crypt initialized")
|
|
|
|
db))
|
|
|
|
|
2016-04-14 12:49:50 +03:00
|
|
|
(register-handler :load-commands
|
|
|
|
(fn [db [action]]
|
|
|
|
(log/debug action)
|
|
|
|
(load-commands)
|
|
|
|
db))
|
|
|
|
|
|
|
|
(register-handler :set-commands
|
2016-04-29 16:24:03 +03:00
|
|
|
(fn [db [action commands]]
|
|
|
|
(log/debug action commands)
|
|
|
|
(set-commands db commands)))
|
2016-04-14 12:49:50 +03:00
|
|
|
|
2016-05-03 16:39:08 +03:00
|
|
|
(register-handler :set-show-actions
|
|
|
|
(fn [db [action show-actions]]
|
|
|
|
(log/debug action)
|
|
|
|
(assoc-in db db/show-actions-path show-actions)))
|
|
|
|
|
2016-03-23 21:05:42 +02:00
|
|
|
;; -- Protocol --------------------------------------------------------------
|
|
|
|
|
|
|
|
(register-handler :initialize-protocol
|
|
|
|
(fn [db [_]]
|
|
|
|
(init-protocol (make-handler db))
|
|
|
|
db))
|
|
|
|
|
|
|
|
(register-handler :protocol-initialized
|
|
|
|
(fn [db [_ identity]]
|
|
|
|
(-> db
|
|
|
|
(update-identity identity)
|
|
|
|
(set-initialized true))))
|
|
|
|
|
2016-05-06 14:06:58 +03:00
|
|
|
(defn gen-messages [n]
|
|
|
|
(mapv (fn [_]
|
|
|
|
(let [id (random-uuid)]
|
|
|
|
{:msg-id id
|
|
|
|
:content (str id
|
|
|
|
"ooops sdfg dsfg"
|
|
|
|
"s dfg\ndsfg dfg\ndsfgdsfgdsfg")
|
|
|
|
:content-type text-content-type
|
|
|
|
:outgoing false
|
|
|
|
:from "console"
|
|
|
|
:to "me"})) (range n)))
|
|
|
|
|
|
|
|
(defn store-message!
|
2016-05-08 23:06:38 +03:00
|
|
|
[{:keys [new-message]} [_ {chat-id :from}]]
|
|
|
|
(save-message chat-id new-message))
|
2016-05-06 14:06:58 +03:00
|
|
|
|
2016-05-06 16:11:45 +03:00
|
|
|
(defn add-message-to-db
|
2016-05-08 23:06:38 +03:00
|
|
|
[db chat-id message]
|
|
|
|
(let [messages [:chats chat-id :messages]]
|
2016-05-06 16:11:45 +03:00
|
|
|
(update-in db messages conj message)))
|
|
|
|
|
2016-05-08 23:06:38 +03:00
|
|
|
(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)})))
|
|
|
|
|
2016-05-06 14:06:58 +03:00
|
|
|
(defn receive-message
|
2016-05-08 23:06:38 +03:00
|
|
|
[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'))))
|
2016-05-06 14:06:58 +03:00
|
|
|
|
2016-03-23 21:05:42 +02:00
|
|
|
(register-handler :received-msg
|
2016-05-06 14:06:58 +03:00
|
|
|
(-> receive-message
|
|
|
|
((after store-message!))))
|
2016-03-28 19:00:07 +03:00
|
|
|
|
2016-04-04 17:19:21 +03:00
|
|
|
(register-handler :group-received-msg
|
|
|
|
(fn [db [action {chat-id :group-id :as msg}]]
|
|
|
|
(log/debug action "msg" msg)
|
|
|
|
(save-message chat-id msg)
|
|
|
|
(signal-chat-updated db chat-id)))
|
|
|
|
|
2016-05-06 14:06:58 +03:00
|
|
|
(defn system-message [msg-id content]
|
|
|
|
{:from "system"
|
|
|
|
:msg-id msg-id
|
|
|
|
:content content
|
|
|
|
:content-type text-content-type})
|
|
|
|
|
2016-04-07 14:01:41 +03:00
|
|
|
(defn joined-chat-msg [chat-id from msg-id]
|
2016-04-11 16:45:01 +03:00
|
|
|
(let [contact-name (:name (contacts/contact-by-identity from))]
|
2016-04-07 14:01:41 +03:00
|
|
|
(save-message chat-id {:from "system"
|
2016-04-20 13:12:12 +03:00
|
|
|
:msg-id (str msg-id "_" from)
|
2016-04-07 14:01:41 +03:00
|
|
|
:content (str (or contact-name from) " received chat invitation")
|
|
|
|
:content-type text-content-type})))
|
|
|
|
|
2016-04-11 16:45:01 +03:00
|
|
|
(defn participant-invited-to-group-msg [chat-id identity from msg-id]
|
|
|
|
(let [inviter-name (:name (contacts/contact-by-identity from))
|
2016-04-29 16:24:03 +03:00
|
|
|
invitee-name (if (= identity (api/my-identity))
|
|
|
|
"You"
|
|
|
|
(:name (contacts/contact-by-identity identity)))]
|
2016-04-11 16:45:01 +03:00
|
|
|
(save-message chat-id {:from "system"
|
|
|
|
:msg-id msg-id
|
|
|
|
:content (str (or inviter-name from) " invited " (or invitee-name identity))
|
|
|
|
:content-type text-content-type})))
|
|
|
|
|
2016-04-11 19:44:02 +03:00
|
|
|
(defn participant-removed-from-group-msg [chat-id identity from msg-id]
|
|
|
|
(let [remover-name (:name (contacts/contact-by-identity from))
|
|
|
|
removed-name (:name (contacts/contact-by-identity identity))]
|
2016-05-06 14:06:58 +03:00
|
|
|
(->> (str (or remover-name from) " removed " (or removed-name identity))
|
|
|
|
(system-message msg-id)
|
|
|
|
(save-message chat-id))))
|
2016-04-11 19:44:02 +03:00
|
|
|
|
2016-04-11 22:57:20 +03:00
|
|
|
(defn you-removed-from-group-msg [chat-id from msg-id]
|
|
|
|
(let [remover-name (:name (contacts/contact-by-identity from))]
|
2016-05-06 14:06:58 +03:00
|
|
|
(->> (str (or remover-name from) " removed you from group chat")
|
|
|
|
(system-message msg-id)
|
|
|
|
(save-message chat-id))))
|
2016-04-11 22:57:20 +03:00
|
|
|
|
2016-04-11 23:12:47 +03:00
|
|
|
(defn participant-left-group-msg [chat-id from msg-id]
|
|
|
|
(let [left-name (:name (contacts/contact-by-identity from))]
|
2016-05-06 14:06:58 +03:00
|
|
|
(->> (str (or left-name from) " left")
|
|
|
|
(system-message msg-id)
|
|
|
|
(save-message chat-id))))
|
2016-04-11 23:12:47 +03:00
|
|
|
|
2016-04-11 19:20:58 +03:00
|
|
|
(defn removed-participant-msg [chat-id identity]
|
|
|
|
(let [contact-name (:name (contacts/contact-by-identity identity))]
|
2016-05-06 14:06:58 +03:00
|
|
|
(->> (str "You've removed " (or contact-name identity))
|
|
|
|
(system-message (random/id))
|
|
|
|
(save-message chat-id))))
|
2016-04-11 19:20:58 +03:00
|
|
|
|
2016-04-11 22:57:20 +03:00
|
|
|
(defn left-chat-msg [chat-id]
|
|
|
|
(save-message chat-id {:from "system"
|
|
|
|
:msg-id (random/id)
|
|
|
|
:content "You left this chat"
|
|
|
|
:content-type text-content-type}))
|
|
|
|
|
2016-04-07 14:01:41 +03:00
|
|
|
(register-handler :group-chat-invite-acked
|
|
|
|
(fn [db [action from group-id ack-msg-id]]
|
|
|
|
(log/debug action from group-id ack-msg-id)
|
|
|
|
(joined-chat-msg group-id from ack-msg-id)
|
|
|
|
(signal-chat-updated db group-id)))
|
|
|
|
|
2016-04-11 19:44:02 +03:00
|
|
|
(register-handler :participant-removed-from-group
|
|
|
|
(fn [db [action from group-id identity msg-id]]
|
|
|
|
(log/debug action msg-id from group-id identity)
|
|
|
|
(chat-remove-participants group-id [identity])
|
|
|
|
(participant-removed-from-group-msg group-id identity from msg-id)
|
|
|
|
(signal-chat-updated db group-id)))
|
|
|
|
|
2016-04-11 22:57:20 +03:00
|
|
|
(register-handler :you-removed-from-group
|
|
|
|
(fn [db [action from group-id msg-id]]
|
|
|
|
(log/debug action msg-id from group-id)
|
|
|
|
(you-removed-from-group-msg group-id from msg-id)
|
|
|
|
(set-chat-active group-id false)
|
|
|
|
(signal-chat-updated db group-id)))
|
|
|
|
|
2016-04-11 23:12:47 +03:00
|
|
|
(register-handler :participant-left-group
|
|
|
|
(fn [db [action from group-id msg-id]]
|
|
|
|
(log/debug action msg-id from group-id)
|
2016-04-29 16:24:03 +03:00
|
|
|
(if (= (api/my-identity) from)
|
|
|
|
db
|
|
|
|
(do (participant-left-group-msg group-id from msg-id)
|
|
|
|
(signal-chat-updated db group-id)))))
|
2016-04-11 23:12:47 +03:00
|
|
|
|
2016-04-11 16:45:01 +03:00
|
|
|
(register-handler :participant-invited-to-group
|
|
|
|
(fn [db [action from group-id identity msg-id]]
|
|
|
|
(log/debug action msg-id from group-id identity)
|
|
|
|
(participant-invited-to-group-msg group-id identity from msg-id)
|
|
|
|
(signal-chat-updated db group-id)))
|
|
|
|
|
2016-03-28 19:00:07 +03:00
|
|
|
(register-handler :acked-msg
|
2016-04-07 14:21:02 +03:00
|
|
|
(fn [db [action from msg-id]]
|
|
|
|
(log/debug action from msg-id)
|
2016-03-28 19:00:07 +03:00
|
|
|
(update-message! {:msg-id msg-id
|
|
|
|
:delivery-status :delivered})
|
|
|
|
(signal-chat-updated db from)))
|
|
|
|
|
|
|
|
(register-handler :msg-delivery-failed
|
2016-04-07 14:21:02 +03:00
|
|
|
(fn [db [action msg-id]]
|
2016-04-11 16:45:01 +03:00
|
|
|
(log/debug action msg-id)
|
2016-03-28 19:00:07 +03:00
|
|
|
(update-message! {:msg-id msg-id
|
|
|
|
:delivery-status :failed})
|
|
|
|
(let [{:keys [chat-id]} (message-by-id msg-id)]
|
|
|
|
(signal-chat-updated db chat-id))))
|
2016-03-25 17:22:13 +03:00
|
|
|
|
2016-05-06 14:06:58 +03:00
|
|
|
(defn console? [s]
|
|
|
|
(= "console" s))
|
|
|
|
|
|
|
|
(def not-console?
|
|
|
|
(complement console?))
|
|
|
|
|
|
|
|
(defn prepare-message
|
|
|
|
[{:keys [identity current-chat-id] :as db} _]
|
2016-05-08 23:06:38 +03:00
|
|
|
(let [text (get-in db [:chats current-chat-id :input-text])
|
|
|
|
{:keys [command]} (check-suggestion db (str text " "))
|
|
|
|
message (check-author-direction
|
|
|
|
db current-chat-id
|
|
|
|
{:msg-id (random/id)
|
|
|
|
:chat-id current-chat-id
|
|
|
|
:content text
|
|
|
|
:to current-chat-id
|
|
|
|
:from identity
|
|
|
|
:content-type text-content-type
|
|
|
|
:outgoing true})]
|
2016-05-06 14:06:58 +03:00
|
|
|
(if command
|
|
|
|
(set-chat-command db command)
|
2016-05-08 23:06:38 +03:00
|
|
|
(assoc db :new-message (when-not (str/blank? text) message)))))
|
2016-05-06 14:06:58 +03:00
|
|
|
|
|
|
|
(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)}]
|
2016-05-08 23:06:38 +03:00
|
|
|
{:msg-id (random/id)
|
|
|
|
:from identity
|
|
|
|
:to chat-id
|
|
|
|
:content content
|
|
|
|
:content-type content-type-command
|
|
|
|
:outgoing true
|
|
|
|
:handler (:handler staged-command)}))
|
2016-05-06 14:06:58 +03:00
|
|
|
|
|
|
|
(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 %))
|
2016-05-08 23:06:38 +03:00
|
|
|
;todo this is wrong :(
|
|
|
|
(map #(check-author-direction db current-chat-id %))
|
2016-05-06 14:06:58 +03:00
|
|
|
(assoc db :new-commands))))
|
|
|
|
|
|
|
|
(defn add-message
|
|
|
|
[{:keys [new-message current-chat-id] :as db}]
|
|
|
|
(if new-message
|
2016-05-06 16:11:45 +03:00
|
|
|
(add-message-to-db db current-chat-id new-message)
|
2016-04-21 15:33:53 +03:00
|
|
|
db))
|
|
|
|
|
2016-05-06 14:06:58 +03:00
|
|
|
(defn add-commands
|
|
|
|
[{:keys [new-commands current-chat-id] :as db}]
|
|
|
|
(reduce
|
2016-05-06 16:11:45 +03:00
|
|
|
#(add-message-to-db %1 current-chat-id %2)
|
2016-05-06 14:06:58 +03:00
|
|
|
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!
|
|
|
|
[{:keys [new-message current-chat-id]} _]
|
|
|
|
(when (and new-message (not-console? current-chat-id))
|
|
|
|
(api/send-user-msg {:to current-chat-id
|
|
|
|
:content (:content new-message)})))
|
|
|
|
|
|
|
|
(defn save-message-to-realm!
|
|
|
|
[{:keys [new-message current-chat-id]} _]
|
|
|
|
(when new-message
|
|
|
|
(save-message current-chat-id new-message)))
|
|
|
|
|
|
|
|
(defn save-commands-to-realm!
|
|
|
|
[{:keys [new-commands current-chat-id]} _]
|
|
|
|
(doseq [new-command new-commands]
|
|
|
|
(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))))
|
|
|
|
|
2016-03-25 17:22:13 +03:00
|
|
|
(register-handler :send-chat-msg
|
2016-05-06 14:06:58 +03:00
|
|
|
(-> prepare-message
|
|
|
|
((enrich prepare-staged-commans))
|
|
|
|
((enrich add-message))
|
|
|
|
((enrich add-commands))
|
|
|
|
((enrich clear-input))
|
|
|
|
((enrich clear-staged-commands))
|
|
|
|
((after (fn [_ _] (r/dismiss-keyboard!))))
|
|
|
|
((after send-message!))
|
|
|
|
((after save-message-to-realm!))
|
|
|
|
((after save-commands-to-realm!))
|
|
|
|
((after handle-commands))))
|
2016-03-23 21:05:42 +02:00
|
|
|
|
2016-05-16 15:19:13 +03:00
|
|
|
(register-handler :clear-history
|
|
|
|
(fn [db [action]]
|
|
|
|
(log/debug action)
|
|
|
|
(let [chat-id (current-chat-id db)]
|
|
|
|
(clear-history chat-id)
|
|
|
|
(signal-chat-updated db chat-id))))
|
|
|
|
|
2016-04-11 22:57:20 +03:00
|
|
|
(register-handler :leave-group-chat
|
2016-05-12 12:13:09 +03:00
|
|
|
(fn [db [action]]
|
2016-04-11 22:57:20 +03:00
|
|
|
(log/debug action)
|
|
|
|
(let [chat-id (current-chat-id db)]
|
|
|
|
(api/leave-group-chat chat-id)
|
|
|
|
(set-chat-active chat-id false)
|
|
|
|
(left-chat-msg chat-id)
|
2016-05-16 15:19:13 +03:00
|
|
|
(delete-chat chat-id)
|
|
|
|
(dispatch [:navigate-back])
|
2016-04-11 22:57:20 +03:00
|
|
|
(signal-chat-updated db chat-id))))
|
|
|
|
|
2016-04-03 19:00:40 +03:00
|
|
|
(register-handler :send-group-chat-msg
|
|
|
|
(fn [db [action chat-id text]]
|
|
|
|
(log/debug action "chat-id" chat-id "text" text)
|
2016-04-04 17:19:21 +03:00
|
|
|
(let [{msg-id :msg-id
|
2016-04-03 19:00:40 +03:00
|
|
|
{from :from} :msg} (api/send-group-user-msg {:group-id chat-id
|
2016-04-04 17:19:21 +03:00
|
|
|
:content text})
|
2016-04-03 19:00:40 +03:00
|
|
|
msg {:msg-id msg-id
|
|
|
|
:from from
|
|
|
|
:to nil
|
|
|
|
:content text
|
|
|
|
:content-type text-content-type
|
|
|
|
:outgoing true}]
|
|
|
|
(save-message chat-id msg)
|
|
|
|
(signal-chat-updated db chat-id))))
|
|
|
|
|
2016-03-28 15:14:57 +03:00
|
|
|
;; -- User data --------------------------------------------------------------
|
|
|
|
|
|
|
|
(register-handler :set-user-phone-number
|
|
|
|
(fn [db [_ value]]
|
|
|
|
(assoc db :user-phone-number value)))
|
|
|
|
|
|
|
|
(register-handler :load-user-phone-number
|
|
|
|
(fn [db [_]]
|
|
|
|
(user-data/load-phone-number)
|
|
|
|
db))
|
|
|
|
|
|
|
|
;; -- Sign up --------------------------------------------------------------
|
|
|
|
|
|
|
|
(register-handler :sign-up
|
2016-05-06 14:06:58 +03:00
|
|
|
(-> (fn [db [_ phone-number]]
|
|
|
|
(assoc db :user-phone-number phone-number))
|
|
|
|
((after (fn [& _] (sign-up-service/on-sign-up-response))))))
|
2016-03-28 15:14:57 +03:00
|
|
|
|
2016-03-28 18:27:09 +03:00
|
|
|
(register-handler :sign-up-confirm
|
2016-05-06 14:06:58 +03:00
|
|
|
(fn [db [_ confirmation-code]]
|
|
|
|
(sign-up-service/on-send-code-response confirmation-code)
|
2016-03-28 18:27:09 +03:00
|
|
|
db))
|
|
|
|
|
|
|
|
(register-handler :sync-contacts
|
|
|
|
(fn [db [_ handler]]
|
|
|
|
(contacts-service/sync-contacts handler)
|
|
|
|
db))
|
|
|
|
|
2016-03-27 17:59:03 +03:00
|
|
|
;; -- Contacts --------------------------------------------------------------
|
|
|
|
|
|
|
|
(register-handler :load-syng-contacts
|
|
|
|
(fn [db [_ value]]
|
|
|
|
(contacts/load-syng-contacts db)))
|
|
|
|
|
2016-05-05 17:03:34 +03:00
|
|
|
(register-handler :show-profile
|
2016-05-09 17:33:41 +03:00
|
|
|
(fn [db [action identity]]
|
2016-05-05 17:03:34 +03:00
|
|
|
(log/debug action)
|
2016-05-06 17:12:24 +03:00
|
|
|
(let [db (contacts/set-contact-identity db identity)]
|
2016-05-09 17:33:41 +03:00
|
|
|
(dispatch [:navigate-to :profile])
|
2016-05-06 17:12:24 +03:00
|
|
|
db)))
|
|
|
|
|
2016-05-11 13:05:33 +03:00
|
|
|
(register-handler :show-my-profile
|
|
|
|
(fn [db [action]]
|
|
|
|
(log/debug action)
|
|
|
|
(dispatch [:navigate-to :my-profile])
|
|
|
|
db))
|
|
|
|
|
2016-05-06 17:12:24 +03:00
|
|
|
;; -- Chats --------------------------------------------------------------
|
2016-05-05 17:03:34 +03:00
|
|
|
|
2016-03-29 23:45:31 +03:00
|
|
|
(register-handler :show-chat
|
2016-04-11 19:20:58 +03:00
|
|
|
(fn [db [action chat-id navigator nav-type]]
|
2016-03-29 23:45:31 +03:00
|
|
|
(log/debug action "chat-id" chat-id)
|
2016-05-06 17:12:24 +03:00
|
|
|
(let [db (-> db
|
|
|
|
(create-chat chat-id [chat-id] false)
|
|
|
|
(set-current-chat-id chat-id))]
|
2016-05-09 17:33:41 +03:00
|
|
|
;; (dispatch [:navigate-to navigator {:view-id :chat} nav-type])
|
|
|
|
(dispatch [:navigate-to :chat])
|
2016-04-03 19:00:40 +03:00
|
|
|
db)))
|
2016-03-30 15:23:37 +03:00
|
|
|
|
2016-04-08 12:17:10 +03:00
|
|
|
(register-handler :init-console-chat
|
2016-04-04 16:13:36 +03:00
|
|
|
(fn [db [_]]
|
2016-04-08 12:17:10 +03:00
|
|
|
(sign-up-service/init db)))
|
|
|
|
|
|
|
|
(register-handler :set-signed-up
|
|
|
|
(fn [db [_ signed-up]]
|
|
|
|
(sign-up-service/set-signed-up db signed-up)))
|
2016-04-04 16:13:36 +03:00
|
|
|
|
2016-03-30 15:23:37 +03:00
|
|
|
;; -- Chat --------------------------------------------------------------
|
|
|
|
|
2016-05-06 14:06:58 +03:00
|
|
|
(defn update-text [db [_ text]]
|
|
|
|
(set-chat-input-text db text))
|
|
|
|
|
|
|
|
(defn update-command [db [_ text]]
|
|
|
|
(let [{:keys [command]} (check-suggestion db text)]
|
|
|
|
(set-chat-command db command)))
|
|
|
|
|
2016-04-05 17:00:49 +03:00
|
|
|
(register-handler :set-chat-input-text
|
2016-05-06 14:06:58 +03:00
|
|
|
((enrich update-command) update-text))
|
2016-03-30 16:40:08 +03:00
|
|
|
|
2016-05-10 13:12:49 +03:00
|
|
|
(register-handler :switch-command-suggestions
|
|
|
|
(fn [db [_]]
|
|
|
|
(switch-command-suggestions db)))
|
|
|
|
|
2016-04-05 17:00:49 +03:00
|
|
|
(register-handler :set-chat-command
|
|
|
|
(fn [db [_ command-key]]
|
2016-05-06 14:06:58 +03:00
|
|
|
;; todo what is going on there?!
|
2016-04-05 17:00:49 +03:00
|
|
|
(set-chat-command db command-key)))
|
|
|
|
|
2016-04-21 12:26:05 +03:00
|
|
|
(register-handler :stage-command
|
2016-05-06 14:06:58 +03:00
|
|
|
(fn [{:keys [current-chat-id] :as db} _]
|
2016-04-29 16:24:03 +03:00
|
|
|
(let [db (set-chat-input-text db nil)
|
2016-05-06 14:06:58 +03:00
|
|
|
{:keys [command content]}
|
|
|
|
(get-in db [:chats current-chat-id :command-input])
|
2016-04-21 12:26:05 +03:00
|
|
|
command-info {:command command
|
|
|
|
:content content
|
2016-05-06 14:06:58 +03:00
|
|
|
:handler (:handler command)}]
|
2016-04-21 12:26:05 +03:00
|
|
|
(stage-command db command-info))))
|
|
|
|
|
|
|
|
(register-handler :unstage-command
|
2016-05-06 14:06:58 +03:00
|
|
|
(fn [db [_ staged-command]]
|
2016-04-21 12:26:05 +03:00
|
|
|
(let []
|
|
|
|
(unstage-command db staged-command))))
|
|
|
|
|
2016-04-08 20:08:40 +03:00
|
|
|
(register-handler :set-response-chat-command
|
|
|
|
(fn [db [_ to-msg-id command-key]]
|
|
|
|
(set-response-chat-command db to-msg-id command-key)))
|
|
|
|
|
2016-04-05 17:00:49 +03:00
|
|
|
(register-handler :set-chat-command-content
|
|
|
|
(fn [db [_ content]]
|
|
|
|
(set-chat-command-content db content)))
|
2016-04-01 18:05:55 +03:00
|
|
|
|
2016-04-07 20:50:53 +03:00
|
|
|
(register-handler :set-chat-command-request
|
2016-04-08 20:08:40 +03:00
|
|
|
(fn [db [_ msg-id handler]]
|
|
|
|
(set-chat-command-request db msg-id handler)))
|
2016-04-07 20:50:53 +03:00
|
|
|
|
2016-04-01 18:05:55 +03:00
|
|
|
(register-handler :show-contacts
|
|
|
|
(fn [db [action navigator]]
|
|
|
|
(nav-push navigator {:view-id :contact-list})
|
|
|
|
db))
|
|
|
|
|
2016-04-07 14:21:02 +03:00
|
|
|
(register-handler :select-new-participant
|
|
|
|
(fn [db [action identity add?]]
|
|
|
|
(log/debug action identity add?)
|
|
|
|
(update-new-participants-selection db identity add?)))
|
|
|
|
|
2016-04-11 19:20:58 +03:00
|
|
|
(register-handler :show-remove-participants
|
2016-05-12 12:13:09 +03:00
|
|
|
(fn [db [action]]
|
2016-04-11 19:20:58 +03:00
|
|
|
(log/debug action)
|
2016-05-12 12:13:09 +03:00
|
|
|
(dispatch [:navigate-to :remove-participants])
|
2016-04-11 19:20:58 +03:00
|
|
|
(clear-new-participants db)))
|
|
|
|
|
|
|
|
(register-handler :remove-selected-participants
|
|
|
|
(fn [db [action navigator]]
|
|
|
|
(log/debug action)
|
2016-04-26 12:42:08 +03:00
|
|
|
(let [identities (vec (new-participants-selection db))
|
2016-04-11 19:20:58 +03:00
|
|
|
chat-id (current-chat-id db)]
|
|
|
|
(chat-remove-participants chat-id identities)
|
2016-05-13 14:17:03 +03:00
|
|
|
(dispatch [:navigate-back])
|
2016-04-11 19:20:58 +03:00
|
|
|
(doseq [ident identities]
|
|
|
|
(api/group-remove-participant chat-id ident)
|
|
|
|
(removed-participant-msg chat-id ident))
|
|
|
|
(signal-chat-updated db chat-id))))
|
|
|
|
|
2016-04-07 14:21:02 +03:00
|
|
|
(register-handler :show-add-participants
|
2016-05-12 12:13:09 +03:00
|
|
|
(fn [db [action]]
|
2016-04-07 14:21:02 +03:00
|
|
|
(log/debug action)
|
2016-05-12 12:13:09 +03:00
|
|
|
(dispatch [:navigate-to :add-participants])
|
2016-04-07 14:21:02 +03:00
|
|
|
(clear-new-participants db)))
|
|
|
|
|
|
|
|
(register-handler :add-new-participants
|
|
|
|
(fn [db [action navigator]]
|
|
|
|
(log/debug action)
|
2016-04-26 12:42:08 +03:00
|
|
|
(let [identities (vec (new-participants-selection db))
|
2016-04-07 14:21:02 +03:00
|
|
|
chat-id (current-chat-id db)]
|
|
|
|
(chat-add-participants chat-id identities)
|
2016-05-13 14:17:03 +03:00
|
|
|
(dispatch [:navigate-back])
|
2016-04-07 14:21:02 +03:00
|
|
|
(doseq [ident identities]
|
|
|
|
(api/group-add-participant chat-id ident))
|
|
|
|
db)))
|
|
|
|
|
2016-04-01 18:05:55 +03:00
|
|
|
(register-handler :show-group-new
|
2016-05-10 10:59:38 +03:00
|
|
|
(fn [db [action]]
|
2016-04-01 18:05:55 +03:00
|
|
|
(log/debug action)
|
2016-05-10 10:59:38 +03:00
|
|
|
(dispatch [:navigate-to :new-group])
|
2016-04-01 18:05:55 +03:00
|
|
|
(clear-new-group db)))
|
|
|
|
|
|
|
|
(register-handler :select-for-new-group
|
|
|
|
(fn [db [action identity add?]]
|
|
|
|
(log/debug action identity add?)
|
|
|
|
(update-new-group-selection db identity add?)))
|
|
|
|
|
|
|
|
(register-handler :create-new-group
|
2016-04-03 19:00:40 +03:00
|
|
|
(fn [db [action group-name navigator]]
|
2016-04-01 18:05:55 +03:00
|
|
|
(log/debug action)
|
2016-04-26 12:42:08 +03:00
|
|
|
(let [identities (vec (new-group-selection db))
|
2016-04-04 17:03:40 +03:00
|
|
|
group-id (api/start-group-chat identities group-name)
|
|
|
|
db (create-chat db group-id identities true group-name)]
|
2016-04-11 19:20:58 +03:00
|
|
|
(dispatch [:show-chat group-id navigator :replace])
|
2016-04-01 18:05:55 +03:00
|
|
|
db)))
|
|
|
|
|
2016-05-12 12:13:09 +03:00
|
|
|
(register-handler :show-group-settings
|
|
|
|
(fn [db [action]]
|
|
|
|
(log/debug action)
|
2016-05-13 15:15:40 +03:00
|
|
|
(dispatch [:navigate-to :group-settings])
|
|
|
|
db))
|
2016-05-12 12:13:09 +03:00
|
|
|
|
2016-05-13 15:15:40 +03:00
|
|
|
(register-handler :set-group-chat-name
|
2016-05-12 12:13:09 +03:00
|
|
|
(fn [db [action chat-name]]
|
|
|
|
(log/debug action)
|
2016-05-13 15:15:40 +03:00
|
|
|
(set-group-chat-name db chat-name)))
|
2016-05-12 12:13:09 +03:00
|
|
|
|
2016-05-16 13:47:34 +03:00
|
|
|
(register-handler :set-chat-color
|
|
|
|
(fn [db [action color]]
|
|
|
|
(log/debug action)
|
|
|
|
(set-chat-color db color)))
|
|
|
|
|
2016-05-13 14:17:03 +03:00
|
|
|
(register-handler :select-group-chat-member
|
|
|
|
(fn [db [action identity]]
|
|
|
|
(log/debug action)
|
|
|
|
(assoc-in db db/group-settings-selected-member-path identity)))
|
|
|
|
|
2016-05-16 13:47:34 +03:00
|
|
|
(register-handler :show-group-settings-color-picker
|
|
|
|
(fn [db [action show?]]
|
|
|
|
(log/debug action)
|
|
|
|
(assoc-in db db/group-settings-show-color-picker show?)))
|
|
|
|
|
2016-05-13 14:17:03 +03:00
|
|
|
(register-handler :chat-remove-member
|
|
|
|
(fn [db [action identity]]
|
|
|
|
(log/debug action)
|
|
|
|
(let [chat-id (current-chat-id db)
|
|
|
|
db (chat-remove-member db identity)]
|
|
|
|
(dispatch [:select-group-chat-member nil])
|
2016-05-13 15:15:40 +03:00
|
|
|
;; TODO fix and uncomment
|
2016-05-13 14:17:03 +03:00
|
|
|
;; (api/group-remove-participant chat-id identity)
|
|
|
|
;; (removed-participant-msg chat-id identity)
|
|
|
|
(signal-chat-updated db chat-id))))
|
|
|
|
|
2016-04-04 17:03:40 +03:00
|
|
|
(register-handler :group-chat-invite-received
|
|
|
|
(fn [db [action from group-id identities group-name]]
|
|
|
|
(log/debug action from group-id identities)
|
2016-04-29 02:06:15 +03:00
|
|
|
(if (chat-exists? group-id)
|
|
|
|
(re-join-group-chat db group-id identities group-name)
|
|
|
|
(create-chat db group-id identities true group-name))))
|
2016-04-01 18:05:55 +03:00
|
|
|
|
2016-05-06 14:06:58 +03:00
|
|
|
(register-handler :navigate-to
|
|
|
|
(fn [db [_ view-id]]
|
|
|
|
(-> db
|
|
|
|
(assoc :view-id view-id)
|
|
|
|
(update :navigation-stack conj view-id))))
|
|
|
|
|
|
|
|
(register-handler :navigate-back
|
|
|
|
(fn [{:keys [navigation-stack] :as db} _]
|
|
|
|
(log/debug :navigate-back)
|
|
|
|
(if (>= 1 (count navigation-stack))
|
|
|
|
db
|
|
|
|
(let [[view-id :as navigation-stack'] (pop navigation-stack)]
|
|
|
|
(-> db
|
|
|
|
(assoc :view-id view-id)
|
|
|
|
(assoc :navigation-stack navigation-stack'))))))
|
|
|
|
|
|
|
|
(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 load-messages!
|
|
|
|
[db _]
|
|
|
|
db
|
|
|
|
(->> (current-chat-id db)
|
|
|
|
get-messages
|
|
|
|
(assoc db :messages)))
|
|
|
|
|
|
|
|
(defn init-chat
|
|
|
|
[{:keys [messages] :as db} _]
|
|
|
|
(let [id (current-chat-id db)]
|
|
|
|
(assoc-in db [:chats id :messages] messages)))
|
|
|
|
|
|
|
|
(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]))
|
|
|
|
(into {}))]
|
|
|
|
(-> db
|
|
|
|
(assoc :chats chats)
|
|
|
|
(dissoc :loaded-chats))))
|
|
|
|
|
|
|
|
(defn load-chats!
|
|
|
|
[db _]
|
|
|
|
(assoc db :loaded-chats (chats/chats-list)))
|
|
|
|
|
|
|
|
(register-handler :initialize-chats
|
|
|
|
((enrich initialize-chats) load-chats!))
|
|
|
|
|
|
|
|
(defn safe-trim [s]
|
|
|
|
(when (string? s)
|
|
|
|
(str/trim s)))
|
|
|
|
|
|
|
|
(register-handler :cancel-command
|
|
|
|
(fn [{:keys [current-chat-id] :as db} _]
|
|
|
|
(-> db
|
|
|
|
(assoc-in [:chats current-chat-id :command-input] {})
|
|
|
|
(update-in [:chats current-chat-id :input-text] safe-trim))))
|
|
|
|
|
|
|
|
(register-handler :save-password
|
|
|
|
(fn [db [_ password]]
|
|
|
|
(sign-up-service/save-password password)
|
|
|
|
(assoc db :password-saved true)))
|