2016-03-23 16:48:26 +02:00
|
|
|
(ns syng-im.handlers
|
|
|
|
(:require
|
2016-04-21 12:26:05 +03:00
|
|
|
[re-frame.core :refer [register-handler after dispatch]]
|
|
|
|
[schema.core :as s :include-macros true]
|
2016-04-21 15:33:53 +03:00
|
|
|
[syng-im.db :as db :refer [app-db schema]]
|
2016-04-21 12:26:05 +03:00
|
|
|
[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!
|
|
|
|
message-by-id]]
|
|
|
|
[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
|
2016-04-21 15:33:53 +03:00
|
|
|
load-commands
|
|
|
|
apply-staged-commands]]
|
2016-04-21 12:26:05 +03:00
|
|
|
[syng-im.handlers.sign-up :as sign-up-service]
|
|
|
|
|
|
|
|
[syng-im.models.chats :refer [create-chat]]
|
|
|
|
[syng-im.models.chat :refer [signal-chat-updated
|
|
|
|
set-current-chat-id
|
|
|
|
update-new-group-selection
|
|
|
|
clear-new-group
|
|
|
|
new-group-selection
|
|
|
|
set-chat-input-text]]
|
|
|
|
[syng-im.utils.logging :as log]
|
|
|
|
[syng-im.protocol.api :as api]
|
2016-04-21 15:33:53 +03:00
|
|
|
[syng-im.constants :refer [text-content-type
|
|
|
|
content-type-command]]
|
2016-04-21 12:26:05 +03:00
|
|
|
[syng-im.navigation :refer [nav-push]]
|
|
|
|
[syng-im.utils.crypt :refer [gen-random-bytes]]))
|
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-03-23 16:48:26 +02:00
|
|
|
(fn [_ _]
|
|
|
|
app-db))
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
(register-handler :navigate-to
|
|
|
|
(fn [db [action navigator route]]
|
|
|
|
(log/debug action route)
|
|
|
|
(nav-push navigator route)
|
|
|
|
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
|
|
|
|
(fn [db [action commands]]
|
|
|
|
(log/debug action commands)
|
|
|
|
(set-commands db commands)))
|
|
|
|
|
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))))
|
|
|
|
|
|
|
|
(register-handler :received-msg
|
2016-03-30 00:12:27 +03:00
|
|
|
(fn [db [action {chat-id :from
|
|
|
|
msg-id :msg-id :as msg}]]
|
|
|
|
(log/debug action "msg" msg)
|
2016-03-23 21:05:42 +02:00
|
|
|
(save-message chat-id msg)
|
2016-03-29 23:45:31 +03:00
|
|
|
(-> db
|
2016-04-04 17:03:40 +03:00
|
|
|
(create-chat chat-id [chat-id] false)
|
2016-03-29 23:45:31 +03:00
|
|
|
(signal-chat-updated chat-id))))
|
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-03-28 19:00:07 +03:00
|
|
|
(register-handler :acked-msg
|
|
|
|
(fn [db [_ from msg-id]]
|
|
|
|
(update-message! {:msg-id msg-id
|
|
|
|
:delivery-status :delivered})
|
|
|
|
(signal-chat-updated db from)))
|
|
|
|
|
|
|
|
(register-handler :msg-delivery-failed
|
|
|
|
(fn [db [_ msg-id]]
|
|
|
|
(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-04-21 15:33:53 +03:00
|
|
|
(defn send-staged-commands [db chat-id]
|
|
|
|
(let [staged-commands (get-in db (db/chat-staged-commands-path chat-id))]
|
|
|
|
(dorun
|
|
|
|
(map
|
|
|
|
(fn [staged-command]
|
|
|
|
(let [command-key (get-in staged-command [:command :command])
|
|
|
|
content (commands/format-command-msg-content command-key
|
|
|
|
(:content staged-command))
|
|
|
|
msg (if (= chat-id "console")
|
|
|
|
(sign-up-service/send-console-command db command-key content)
|
|
|
|
;; TODO handle command, now sends as plain message
|
|
|
|
(let [{msg-id :msg-id
|
|
|
|
{from :from
|
|
|
|
to :to} :msg} (api/send-user-msg {:to chat-id
|
|
|
|
:content content})]
|
|
|
|
{:msg-id msg-id
|
|
|
|
:from from
|
|
|
|
:to to
|
|
|
|
:content content
|
|
|
|
:content-type content-type-command
|
|
|
|
:outgoing true}))]
|
|
|
|
(save-message chat-id msg)))
|
|
|
|
staged-commands))
|
|
|
|
db))
|
|
|
|
|
2016-03-25 17:22:13 +03:00
|
|
|
(register-handler :send-chat-msg
|
2016-03-29 23:45:31 +03:00
|
|
|
(fn [db [action chat-id text]]
|
|
|
|
(log/debug action "chat-id" chat-id "text" text)
|
2016-04-14 12:49:50 +03:00
|
|
|
(if-let [command (get-command db text)]
|
2016-04-21 15:33:53 +03:00
|
|
|
(do (dispatch [:set-chat-command (:command command)])
|
|
|
|
db)
|
|
|
|
(let [msg (when (< 0 (count text))
|
|
|
|
(if (= chat-id "console")
|
|
|
|
(sign-up-service/send-console-msg text)
|
|
|
|
(let [{msg-id :msg-id
|
|
|
|
{from :from
|
|
|
|
to :to} :msg} (api/send-user-msg {:to chat-id
|
|
|
|
:content text})]
|
|
|
|
{:msg-id msg-id
|
|
|
|
:from from
|
|
|
|
:to to
|
|
|
|
:content text
|
|
|
|
:content-type text-content-type
|
|
|
|
:outgoing true})))]
|
|
|
|
(when msg
|
|
|
|
(save-message chat-id msg))
|
|
|
|
(-> db
|
|
|
|
(send-staged-commands chat-id)
|
|
|
|
(apply-staged-commands)
|
|
|
|
(signal-chat-updated chat-id))))))
|
2016-03-23 21:05:42 +02:00
|
|
|
|
2016-04-04 18:19:52 +03:00
|
|
|
(register-handler :send-chat-command
|
|
|
|
(fn [db [action chat-id command content]]
|
|
|
|
(log/debug action "chat-id" chat-id "command" command "content" content)
|
2016-04-06 16:40:31 +03:00
|
|
|
(let [db (set-chat-input-text db nil)
|
|
|
|
msg (if (= chat-id "console")
|
2016-04-07 20:50:53 +03:00
|
|
|
(sign-up-service/send-console-command db command content)
|
2016-04-04 18:19:52 +03:00
|
|
|
;; TODO handle command, now sends as plain message
|
|
|
|
(let [{msg-id :msg-id
|
|
|
|
{from :from
|
|
|
|
to :to} :msg} (api/send-user-msg {:to chat-id
|
|
|
|
:content content})]
|
|
|
|
{:msg-id msg-id
|
|
|
|
:from from
|
|
|
|
:to to
|
|
|
|
:content content
|
|
|
|
:content-type text-content-type
|
|
|
|
:outgoing true}))]
|
|
|
|
(save-message chat-id msg)
|
2016-04-08 20:08:40 +03:00
|
|
|
(-> db
|
|
|
|
(handle-command command content)
|
|
|
|
(signal-chat-updated chat-id)))))
|
2016-04-04 18:19:52 +03:00
|
|
|
|
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-04-06 18:24:11 +03:00
|
|
|
(fn [db [_ phone-number handler]]
|
|
|
|
(server/sign-up db phone-number handler)))
|
2016-03-28 15:14:57 +03:00
|
|
|
|
2016-03-28 18:27:09 +03:00
|
|
|
(register-handler :sign-up-confirm
|
|
|
|
(fn [db [_ confirmation-code handler]]
|
|
|
|
(server/sign-up-confirm confirmation-code handler)
|
|
|
|
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-03-29 23:45:31 +03:00
|
|
|
;; -- Chats --------------------------------------------------------------
|
2016-03-23 21:05:42 +02:00
|
|
|
|
2016-03-29 23:45:31 +03:00
|
|
|
(register-handler :show-chat
|
|
|
|
(fn [db [action chat-id navigator]]
|
|
|
|
(log/debug action "chat-id" chat-id)
|
2016-04-03 19:00:40 +03:00
|
|
|
(let [db (set-current-chat-id db chat-id)]
|
|
|
|
(dispatch [:navigate-to navigator {:view-id :chat}])
|
|
|
|
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-04-05 17:00:49 +03:00
|
|
|
(register-handler :set-chat-input-text
|
2016-03-30 15:23:37 +03:00
|
|
|
(fn [db [_ text]]
|
2016-04-05 17:00:49 +03:00
|
|
|
(set-chat-input-text db text)))
|
2016-03-30 16:40:08 +03:00
|
|
|
|
2016-04-05 17:00:49 +03:00
|
|
|
(register-handler :set-chat-command
|
|
|
|
(fn [db [_ command-key]]
|
|
|
|
(set-chat-command db command-key)))
|
|
|
|
|
2016-04-21 12:26:05 +03:00
|
|
|
(register-handler :stage-command
|
|
|
|
(fn [db [action chat-id command content]]
|
|
|
|
(log/debug action "chat-id" chat-id "command" command "content" content)
|
|
|
|
(let [db (set-chat-input-text db nil)
|
|
|
|
command-info {:command command
|
|
|
|
:content content
|
2016-04-21 15:33:53 +03:00
|
|
|
:handler (get-command-handler db (:command command) content)}]
|
2016-04-21 12:26:05 +03:00
|
|
|
(stage-command db command-info))))
|
|
|
|
|
|
|
|
(register-handler :unstage-command
|
|
|
|
(fn [db [action chat-id staged-command]]
|
|
|
|
(log/debug action "chat-id" chat-id "staged-command" staged-command)
|
|
|
|
(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]]
|
|
|
|
(log/debug action)
|
|
|
|
(nav-push navigator {:view-id :contact-list})
|
|
|
|
db))
|
|
|
|
|
|
|
|
(register-handler :show-group-new
|
|
|
|
(fn [db [action navigator]]
|
|
|
|
(log/debug action)
|
|
|
|
(nav-push navigator {:view-id :new-group})
|
|
|
|
(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)
|
|
|
|
(let [identities (-> (new-group-selection db)
|
|
|
|
(vec))
|
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-01 18:05:55 +03:00
|
|
|
(dispatch [:show-chat group-id navigator])
|
|
|
|
db)))
|
|
|
|
|
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)
|
|
|
|
(create-chat db group-id identities true group-name)))
|
2016-04-01 18:05:55 +03:00
|
|
|
|
|
|
|
(comment
|
|
|
|
|
|
|
|
)
|