parent
3be2f2c06c
commit
8a99e2a614
|
@ -27,7 +27,7 @@
|
|||
;; this listener and handle application's closing
|
||||
;; in handlers
|
||||
(let [stack (subscribe [:navigation-stack])]
|
||||
(when (< 1 (count stack))
|
||||
(when (< 1 (count @stack))
|
||||
(dispatch [:navigate-back])
|
||||
true)))]
|
||||
(add-event-listener "hardwareBackPress" new-listener)))
|
||||
|
|
|
@ -0,0 +1,287 @@
|
|||
(ns syng-im.chat.handlers
|
||||
(:require [syng-im.db :as db]
|
||||
[re-frame.core :refer [register-handler enrich after debug]]
|
||||
[syng-im.models.commands :as commands]
|
||||
[clojure.string :as str]
|
||||
[syng-im.models.chat :as chat]
|
||||
[syng-im.handlers.suggestions :as suggestions]
|
||||
[syng-im.protocol.api :as api]
|
||||
[syng-im.models.messages :as messages]
|
||||
[syng-im.constants :refer [text-content-type
|
||||
content-type-command]]
|
||||
[syng-im.utils.random :as random]
|
||||
[syng-im.components.react :as r]
|
||||
[syng-im.handlers.sign-up :as sign-up-service]
|
||||
[syng-im.models.chats :as chats]))
|
||||
|
||||
(register-handler :set-show-actions
|
||||
(fn [db [_ show-actions]]
|
||||
(assoc-in db db/show-actions-path show-actions)))
|
||||
|
||||
(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
|
||||
(assoc-in [:chats current-chat-id :command-input] {})
|
||||
(update-in [:chats current-chat-id :input-text] safe-trim))))
|
||||
|
||||
(register-handler :set-chat-command-content
|
||||
(fn [db [_ content]]
|
||||
(commands/set-chat-command-content db content)))
|
||||
|
||||
(register-handler :stage-command
|
||||
(fn [{:keys [current-chat-id] :as db} _]
|
||||
(let [db (chat/set-chat-input-text db nil)
|
||||
{: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))))
|
||||
|
||||
(register-handler :set-response-chat-command
|
||||
(fn [db [_ to-msg-id command-key]]
|
||||
(commands/set-response-chat-command db to-msg-id command-key)))
|
||||
|
||||
(defn update-text [db [_ text]]
|
||||
(chat/set-chat-input-text db text))
|
||||
|
||||
(defn update-command [db [_ text]]
|
||||
(let [{:keys [command]} (suggestions/check-suggestion db text)]
|
||||
(commands/set-chat-command db command)))
|
||||
|
||||
(register-handler :set-chat-input-text
|
||||
((enrich update-command) update-text))
|
||||
|
||||
(register-handler :send-group-chat-msg
|
||||
(fn [db [_ chat-id text]]
|
||||
(let [{msg-id :msg-id
|
||||
{from :from} :msg} (api/send-group-user-msg {:group-id chat-id
|
||||
:content text})
|
||||
msg {:msg-id msg-id
|
||||
:from from
|
||||
:to nil
|
||||
:content text
|
||||
:content-type text-content-type
|
||||
:outgoing true}]
|
||||
(messages/save-message chat-id msg)
|
||||
(chat/signal-chat-updated db chat-id))))
|
||||
|
||||
(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
|
||||
{:msg-id (random/id)
|
||||
:chat-id current-chat-id
|
||||
:content text
|
||||
:to current-chat-id
|
||||
:from identity
|
||||
:content-type text-content-type
|
||||
:outgoing true})]
|
||||
(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!
|
||||
[{: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
|
||||
(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))
|
||||
((after (fn [_ _] (r/dismiss-keyboard!))))
|
||||
((after send-message!))
|
||||
((after save-message-to-realm!))
|
||||
((after save-commands-to-realm!))
|
||||
((after handle-commands))))
|
||||
|
||||
(register-handler :unstage-command
|
||||
(fn [db [_ staged-command]]
|
||||
(let []
|
||||
(commands/unstage-command db staged-command))))
|
||||
|
||||
(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
|
||||
(-> (fn [db [_ phone-number]]
|
||||
(assoc db :user-phone-number phone-number))
|
||||
((after (fn [& _] (sign-up-service/on-sign-up-response))))))
|
||||
|
||||
(register-handler :sign-up-confirm
|
||||
(fn [db [_ confirmation-code]]
|
||||
(sign-up-service/on-send-code-response confirmation-code)
|
||||
db))
|
||||
|
||||
(register-handler :set-signed-up
|
||||
(fn [db [_ signed-up]]
|
||||
(sign-up-service/set-signed-up db signed-up)))
|
||||
|
||||
|
||||
(defn load-messages!
|
||||
[db _]
|
||||
db
|
||||
(->> (:current-chat-id db)
|
||||
messages/get-messages
|
||||
(assoc db :messages)))
|
||||
|
||||
(defn init-chat
|
||||
[{:keys [messages current-chat-id] :as db} _]
|
||||
(assoc-in db [:chats current-chat-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 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
|
||||
(fn [db [_ {chat-id :group-id :as msg}]]
|
||||
(messages/save-message chat-id msg)
|
||||
(chat/signal-chat-updated db chat-id)))
|
|
@ -1,22 +1,16 @@
|
|||
(ns syng-im.chat.screen
|
||||
(:require [clojure.string :as s]
|
||||
[re-frame.core :refer [subscribe dispatch dispatch-sync]]
|
||||
[re-frame.core :refer [subscribe dispatch]]
|
||||
[syng-im.components.react :refer [view
|
||||
text
|
||||
image
|
||||
icon
|
||||
navigator
|
||||
touchable-highlight
|
||||
toolbar-android
|
||||
list-view
|
||||
list-item
|
||||
android?]]
|
||||
list-item]]
|
||||
[syng-im.chat.styles.chat :as st]
|
||||
[syng-im.utils.logging :as log]
|
||||
[syng-im.resources :as res]
|
||||
[syng-im.constants :refer [content-type-status]]
|
||||
[syng-im.utils.listview :refer [to-datasource
|
||||
to-datasource2]]
|
||||
[syng-im.utils.listview :refer [to-datasource2]]
|
||||
[syng-im.components.invertible-scroll-view :refer [invertible-scroll-view]]
|
||||
[syng-im.chat.views.message :refer [chat-message]]
|
||||
[syng-im.chat.views.new-message :refer [chat-message-new]]))
|
||||
|
@ -29,8 +23,8 @@
|
|||
|
||||
(defn add-msg-color [{:keys [from] :as msg} contact-by-identity]
|
||||
(if (= "system" from)
|
||||
(assoc msg :text-color "#4A5258"
|
||||
:background-color "#D3EEEF")
|
||||
(assoc msg :text-color :#4A5258
|
||||
:background-color :#D3EEEF)
|
||||
(let [{:keys [text-color background-color]} (get contact-by-identity from)]
|
||||
(assoc msg :text-color text-color
|
||||
:background-color background-color))))
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
(ns syng-im.chat.subs
|
||||
(:require-macros [reagent.ratom :refer [reaction]])
|
||||
(:require [re-frame.core :refer [register-sub]]
|
||||
[syng-im.db :as db]
|
||||
;todo handlers in subs?...
|
||||
[syng-im.handlers.suggestions :refer [get-suggestions]]
|
||||
[syng-im.models.commands :as commands]))
|
||||
|
||||
(register-sub :chat-properties
|
||||
(fn [db [_ properties]]
|
||||
(->> properties
|
||||
(map (fn [k]
|
||||
[k (-> @db
|
||||
(get-in [:chats (:current-chat-id @db) k])
|
||||
(reaction))]))
|
||||
(into {}))))
|
||||
|
||||
(register-sub
|
||||
:show-actions
|
||||
(fn [db _]
|
||||
(reaction (get-in @db db/show-actions-path))))
|
||||
|
||||
(register-sub :chat
|
||||
(fn [db [_ k]]
|
||||
(-> @db
|
||||
(get-in [:chats (:current-chat-id @db) k])
|
||||
(reaction))))
|
||||
|
||||
|
||||
(register-sub :get-chat-messages
|
||||
(fn [db _]
|
||||
(let [chat-id (:current-chat-id @db)]
|
||||
(reaction (get-in @db [:chats chat-id :messages])))))
|
||||
|
||||
(register-sub :get-current-chat-id
|
||||
(fn [db _]
|
||||
(reaction (:current-chat-id @db))))
|
||||
|
||||
(register-sub :get-suggestions
|
||||
(fn [db _]
|
||||
(let [input-text (->> (:current-chat-id @db)
|
||||
db/chat-input-text-path
|
||||
(get-in @db)
|
||||
(reaction))]
|
||||
(reaction (get-suggestions @db @input-text)))))
|
||||
|
||||
(register-sub :get-commands
|
||||
(fn [db _]
|
||||
(reaction (commands/get-commands @db))))
|
||||
|
||||
(register-sub :get-chat-input-text
|
||||
(fn [db _]
|
||||
(->> (db/chat-input-text-path (:current-chat-id @db))
|
||||
(get-in @db)
|
||||
(reaction))))
|
||||
|
||||
(register-sub :get-chat-staged-commands
|
||||
(fn [db _]
|
||||
(->> (db/chat-staged-commands-path (:current-chat-id @db))
|
||||
(get-in @db)
|
||||
(reaction))))
|
||||
|
||||
(register-sub :get-chat-command
|
||||
(fn [db _]
|
||||
(reaction (commands/get-chat-command @db))))
|
||||
|
||||
(register-sub :get-chat-command-content
|
||||
(fn [db _]
|
||||
(reaction (commands/get-chat-command-content @db))))
|
||||
|
||||
(register-sub :chat-command-request
|
||||
(fn [db _]
|
||||
(reaction (commands/get-chat-command-request @db))))
|
||||
|
||||
(register-sub :get-current-chat
|
||||
(fn [db _]
|
||||
(let [current-chat-id (:current-chat-id @db)]
|
||||
(reaction (get-in @db [:chats current-chat-id])))))
|
|
@ -1,5 +1,5 @@
|
|||
(ns syng-im.chat.views.command
|
||||
(:require [re-frame.core :refer [subscribe dispatch dispatch-sync]]
|
||||
(:require [re-frame.core :refer [subscribe dispatch]]
|
||||
[syng-im.components.react :refer [view
|
||||
image
|
||||
icon
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
(ns syng-im.chat.views.plain-input
|
||||
(:require [re-frame.core :refer [subscribe dispatch dispatch-sync]]
|
||||
(:require [re-frame.core :refer [subscribe dispatch]]
|
||||
[syng-im.components.react :refer [view
|
||||
icon
|
||||
touchable-highlight
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
(:require
|
||||
[re-frame.core :refer [register-handler after dispatch debug enrich]]
|
||||
[schema.core :as s :include-macros true]
|
||||
[syng-im.db :as db :refer [app-db schema]]
|
||||
[syng-im.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
|
||||
|
@ -11,15 +11,8 @@
|
|||
[syng-im.models.contacts :as contacts]
|
||||
[syng-im.models.messages :refer [save-message
|
||||
update-message!
|
||||
message-by-id
|
||||
get-messages]]
|
||||
[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]]
|
||||
message-by-id]]
|
||||
[syng-im.models.commands :refer [set-commands]]
|
||||
[syng-im.handlers.server :as server]
|
||||
[syng-im.handlers.contacts :as contacts-service]
|
||||
[syng-im.handlers.suggestions :refer [get-command
|
||||
|
@ -28,14 +21,13 @@
|
|||
load-commands
|
||||
apply-staged-commands
|
||||
check-suggestion]]
|
||||
[syng-im.handlers.sign-up :as sign-up-service]
|
||||
[syng-im.models.chats :refer [chat-exists?
|
||||
create-chat
|
||||
chat-add-participants
|
||||
chat-remove-participants
|
||||
set-chat-active
|
||||
re-join-group-chat
|
||||
chat-by-id2] :as chats]
|
||||
chat-by-id2]]
|
||||
[syng-im.models.chat :refer [signal-chat-updated
|
||||
set-current-chat-id
|
||||
current-chat-id
|
||||
|
@ -55,8 +47,7 @@
|
|||
nav-pop]]
|
||||
[syng-im.utils.crypt :refer [gen-random-bytes]]
|
||||
[syng-im.utils.random :as random]
|
||||
[clojure.string :as str]
|
||||
[syng-im.components.react :as r]))
|
||||
syng-im.chat.handlers))
|
||||
|
||||
;; -- Middleware ------------------------------------------------------------
|
||||
;;
|
||||
|
@ -113,11 +104,6 @@
|
|||
(log/debug action commands)
|
||||
(set-commands db commands)))
|
||||
|
||||
(register-handler :set-show-actions
|
||||
(fn [db [action show-actions]]
|
||||
(log/debug action)
|
||||
(assoc-in db db/show-actions-path show-actions)))
|
||||
|
||||
;; -- Protocol --------------------------------------------------------------
|
||||
|
||||
(register-handler :initialize-protocol
|
||||
|
@ -143,43 +129,6 @@
|
|||
:from "console"
|
||||
:to "me"})) (range n)))
|
||||
|
||||
(defn store-message!
|
||||
[{:keys [new-message]} [_ {chat-id :from}]]
|
||||
(save-message chat-id new-message))
|
||||
|
||||
(defn add-message-to-db
|
||||
[db chat-id message]
|
||||
(let [messages [:chats chat-id :messages]]
|
||||
(update-in db messages conj message)))
|
||||
|
||||
(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 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
|
||||
(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)))
|
||||
|
||||
(defn system-message [msg-id content]
|
||||
{:from "system"
|
||||
:msg-id msg-id
|
||||
|
@ -283,109 +232,6 @@
|
|||
(let [{:keys [chat-id]} (message-by-id msg-id)]
|
||||
(signal-chat-updated db chat-id))))
|
||||
|
||||
(defn console? [s]
|
||||
(= "console" s))
|
||||
|
||||
(def not-console?
|
||||
(complement console?))
|
||||
|
||||
(defn prepare-message
|
||||
[{:keys [identity current-chat-id] :as db} _]
|
||||
(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})]
|
||||
(if command
|
||||
(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!
|
||||
[{: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))))
|
||||
|
||||
(register-handler :send-chat-msg
|
||||
(-> 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))))
|
||||
|
||||
(register-handler :leave-group-chat
|
||||
(fn [db [action navigator]]
|
||||
(log/debug action)
|
||||
|
@ -395,21 +241,6 @@
|
|||
(left-chat-msg chat-id)
|
||||
(signal-chat-updated db chat-id))))
|
||||
|
||||
(register-handler :send-group-chat-msg
|
||||
(fn [db [action chat-id text]]
|
||||
(log/debug action "chat-id" chat-id "text" text)
|
||||
(let [{msg-id :msg-id
|
||||
{from :from} :msg} (api/send-group-user-msg {:group-id chat-id
|
||||
:content text})
|
||||
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))))
|
||||
|
||||
;; -- User data --------------------------------------------------------------
|
||||
|
||||
(register-handler :set-user-phone-number
|
||||
|
@ -423,16 +254,6 @@
|
|||
|
||||
;; -- Sign up --------------------------------------------------------------
|
||||
|
||||
(register-handler :sign-up
|
||||
(-> (fn [db [_ phone-number]]
|
||||
(assoc db :user-phone-number phone-number))
|
||||
((after (fn [& _] (sign-up-service/on-sign-up-response))))))
|
||||
|
||||
(register-handler :sign-up-confirm
|
||||
(fn [db [_ confirmation-code]]
|
||||
(sign-up-service/on-send-code-response confirmation-code)
|
||||
db))
|
||||
|
||||
(register-handler :sync-contacts
|
||||
(fn [db [_ handler]]
|
||||
(contacts-service/sync-contacts handler)
|
||||
|
@ -453,60 +274,8 @@
|
|||
(dispatch [:navigate-to navigator {:view-id :chat} nav-type])
|
||||
db)))
|
||||
|
||||
(register-handler :init-console-chat
|
||||
(fn [db [_]]
|
||||
(sign-up-service/init db)))
|
||||
|
||||
(register-handler :set-signed-up
|
||||
(fn [db [_ signed-up]]
|
||||
(sign-up-service/set-signed-up db signed-up)))
|
||||
|
||||
;; -- Chat --------------------------------------------------------------
|
||||
|
||||
(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)))
|
||||
|
||||
(register-handler :set-chat-input-text
|
||||
((enrich update-command) update-text))
|
||||
|
||||
(register-handler :set-chat-command
|
||||
(fn [db [_ command-key]]
|
||||
;; todo what is going on there?!
|
||||
(set-chat-command db command-key)))
|
||||
|
||||
(register-handler :stage-command
|
||||
(fn [{:keys [current-chat-id] :as db} _]
|
||||
(let [db (set-chat-input-text db nil)
|
||||
{:keys [command content]}
|
||||
(get-in db [:chats current-chat-id :command-input])
|
||||
command-info {:command command
|
||||
:content content
|
||||
:handler (:handler command)}]
|
||||
(stage-command db command-info))))
|
||||
|
||||
(register-handler :unstage-command
|
||||
(fn [db [_ staged-command]]
|
||||
(let []
|
||||
(unstage-command db staged-command))))
|
||||
|
||||
(register-handler :set-response-chat-command
|
||||
(fn [db [_ to-msg-id command-key]]
|
||||
(set-response-chat-command db to-msg-id command-key)))
|
||||
|
||||
(register-handler :set-chat-command-content
|
||||
(fn [db [_ content]]
|
||||
(set-chat-command-content db content)))
|
||||
|
||||
(register-handler :set-chat-command-request
|
||||
(fn [db [_ msg-id handler]]
|
||||
(set-chat-command-request db msg-id handler)))
|
||||
|
||||
(register-handler :show-contacts
|
||||
(fn [db [action navigator]]
|
||||
(fn [db [_ navigator]]
|
||||
(nav-push navigator {:view-id :contact-list})
|
||||
db))
|
||||
|
||||
|
@ -576,77 +345,3 @@
|
|||
(if (chat-exists? group-id)
|
||||
(re-join-group-chat db group-id identities group-name)
|
||||
(create-chat db group-id identities true group-name))))
|
||||
|
||||
(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)))
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
(ns syng-im.navigation.handlers
|
||||
(:require [re-frame.core :refer [register-handler]]))
|
||||
|
||||
(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} _]
|
||||
(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'))))))
|
|
@ -0,0 +1,7 @@
|
|||
(ns syng-im.navigation.subs
|
||||
(:require-macros [reagent.ratom :refer [reaction]])
|
||||
(:require [re-frame.core :refer [register-sub]]))
|
||||
|
||||
(register-sub :navigation-stack
|
||||
(fn [db _]
|
||||
(reaction (:navigation-stack @db))))
|
|
@ -1,65 +1,12 @@
|
|||
(ns syng-im.subs
|
||||
(:require-macros [reagent.ratom :refer [reaction]])
|
||||
(:require [re-frame.core :refer [register-sub]]
|
||||
[syng-im.db :as db]
|
||||
[syng-im.models.chat :refer [current-chat-id
|
||||
chat-updated?]]
|
||||
[syng-im.models.chats :refer [chats-list
|
||||
chats-updated?
|
||||
chat-by-id]]
|
||||
[syng-im.models.messages :refer [get-messages]]
|
||||
[syng-im.models.chat :refer [current-chat-id chat-updated?]]
|
||||
[syng-im.models.chats :refer [chats-list chats-updated? chat-by-id]]
|
||||
[syng-im.models.contacts :refer [contacts-list
|
||||
contacts-list-exclude
|
||||
contacts-list-include]]
|
||||
[syng-im.models.commands :refer [get-commands
|
||||
get-chat-command
|
||||
get-chat-command-content
|
||||
get-chat-command-request
|
||||
parse-command-request]]
|
||||
[syng-im.handlers.suggestions :refer [get-suggestions]]))
|
||||
|
||||
;; -- Chat --------------------------------------------------------------
|
||||
|
||||
(register-sub :get-chat-messages
|
||||
(fn [db _]
|
||||
(let [chat-id (current-chat-id @db)]
|
||||
(reaction (get-in @db [:chats chat-id :messages])))))
|
||||
|
||||
(register-sub :get-current-chat-id
|
||||
(fn [db _]
|
||||
(reaction (current-chat-id @db))))
|
||||
|
||||
(register-sub :get-suggestions
|
||||
(fn [db _]
|
||||
(let [input-text (->> (current-chat-id @db)
|
||||
db/chat-input-text-path
|
||||
(get-in @db)
|
||||
(reaction))]
|
||||
(reaction (get-suggestions @db @input-text)))))
|
||||
|
||||
(register-sub :get-commands
|
||||
(fn [db _]
|
||||
(reaction (get-commands @db))))
|
||||
|
||||
(register-sub :get-chat-input-text
|
||||
(fn [db _]
|
||||
(reaction (get-in @db (db/chat-input-text-path (current-chat-id @db))))))
|
||||
|
||||
(register-sub :get-chat-staged-commands
|
||||
(fn [db _]
|
||||
(reaction (get-in @db (db/chat-staged-commands-path (current-chat-id @db))))))
|
||||
|
||||
(register-sub :get-chat-command
|
||||
(fn [db _]
|
||||
(reaction (get-chat-command @db))))
|
||||
|
||||
(register-sub :get-chat-command-content
|
||||
(fn [db _]
|
||||
(reaction (get-chat-command-content @db))))
|
||||
|
||||
(register-sub :chat-command-request
|
||||
(fn [db _]
|
||||
(reaction (get-chat-command-request @db))))
|
||||
syng-im.chat.subs))
|
||||
|
||||
;; -- Chats list --------------------------------------------------------------
|
||||
|
||||
|
@ -70,11 +17,6 @@
|
|||
(let [_ @chats-updated]
|
||||
(chats-list))))))
|
||||
|
||||
(register-sub :get-current-chat
|
||||
(fn [db _]
|
||||
(let [current-chat-id (current-chat-id @db)]
|
||||
(reaction (get-in @db [:chats current-chat-id])))))
|
||||
|
||||
;; -- User data --------------------------------------------------------------
|
||||
|
||||
;; (register-sub
|
||||
|
@ -101,11 +43,6 @@
|
|||
(reaction
|
||||
(get @db :signed-up))))
|
||||
|
||||
(register-sub
|
||||
:show-actions
|
||||
(fn [db _]
|
||||
(reaction (get-in @db db/show-actions-path))))
|
||||
|
||||
(register-sub
|
||||
:get-contacts
|
||||
(fn [db _]
|
||||
|
@ -145,24 +82,6 @@
|
|||
(fn [db _]
|
||||
(reaction (@db :view-id))))
|
||||
|
||||
(register-sub :chat
|
||||
(fn [db [_ k]]
|
||||
(-> @db
|
||||
(get-in [:chats (current-chat-id @db) k])
|
||||
(reaction))))
|
||||
|
||||
(register-sub :navigation-stack
|
||||
(fn [db _]
|
||||
(reaction (:navigation-stack @db))))
|
||||
|
||||
(register-sub :db
|
||||
(fn [db _] (reaction @db)))
|
||||
|
||||
(register-sub :chat-properties
|
||||
(fn [db [_ properties]]
|
||||
(->> properties
|
||||
(map (fn [k]
|
||||
[k (-> @db
|
||||
(get-in [:chats (:current-chat-id @db) k])
|
||||
(reaction))]))
|
||||
(into {}))))
|
||||
|
|
Loading…
Reference in New Issue