From eb10272bdbeaae0d405f69dd2f6139b35e60e150 Mon Sep 17 00:00:00 2001 From: michaelr Date: Fri, 29 Apr 2016 16:24:03 +0300 Subject: [PATCH] improved system messages for edge cases Former-commit-id: a555c57bcc1fa2e3632e202ef987b83adedd8b0a --- src/syng_im/handlers.cljs | 168 +++++++++++++++++----------------- src/syng_im/models/chats.cljs | 2 +- 2 files changed, 87 insertions(+), 83 deletions(-) diff --git a/src/syng_im/handlers.cljs b/src/syng_im/handlers.cljs index 3a6d1228c1..9febceb3f3 100644 --- a/src/syng_im/handlers.cljs +++ b/src/syng_im/handlers.cljs @@ -1,59 +1,59 @@ (ns syng-im.handlers (:require - [re-frame.core :refer [register-handler after dispatch]] - [schema.core :as s :include-macros true] - [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! - 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 - load-commands - apply-staged-commands - check-suggestion]] - [syng-im.handlers.sign-up :as sign-up-service] + [re-frame.core :refer [register-handler after dispatch]] + [schema.core :as s :include-macros true] + [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! + 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 + 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]] - [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]] - [syng-im.utils.random :as random])) + [syng-im.models.chats :refer [chat-exists? + create-chat + chat-add-participants + chat-remove-participants + set-chat-active + re-join-group-chat]] + [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]] + [syng-im.utils.random :as random])) ;; -- Middleware ------------------------------------------------------------ ;; @@ -115,9 +115,9 @@ db)) (register-handler :set-commands - (fn [db [action commands]] - (log/debug action commands) - (set-commands db commands))) + (fn [db [action commands]] + (log/debug action commands) + (set-commands db commands))) ;; -- Protocol -------------------------------------------------------------- @@ -156,7 +156,9 @@ (defn participant-invited-to-group-msg [chat-id identity from msg-id] (let [inviter-name (:name (contacts/contact-by-identity from)) - invitee-name (:name (contacts/contact-by-identity identity))] + invitee-name (if (= identity (api/my-identity)) + "You" + (:name (contacts/contact-by-identity identity)))] (save-message chat-id {:from "system" :msg-id msg-id :content (str (or inviter-name from) " invited " (or invitee-name identity)) @@ -220,8 +222,10 @@ (register-handler :participant-left-group (fn [db [action from group-id msg-id]] (log/debug action msg-id from group-id) - (participant-left-group-msg group-id from msg-id) - (signal-chat-updated db group-id))) + (if (= (api/my-identity) from) + db + (do (participant-left-group-msg group-id from msg-id) + (signal-chat-updated db group-id))))) (register-handler :participant-invited-to-group (fn [db [action from group-id identity msg-id]] @@ -247,26 +251,26 @@ (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)) + (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)) (register-handler :send-chat-msg @@ -278,7 +282,7 @@ (let [msg (when (pos? (count text)) (if (= chat-id "console") (sign-up-service/send-console-msg text) - (let [{msg-id :msg-id + (let [{msg-id :msg-id {from :from to :to} :msg} (api/send-user-msg {:to chat-id :content text})] @@ -307,7 +311,7 @@ (register-handler :send-chat-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) + (let [db (set-chat-input-text db nil) msg (if (= chat-id "console") (sign-up-service/send-console-command db command content) ;; TODO handle command, now sends as plain message @@ -407,7 +411,7 @@ (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) + (let [db (set-chat-input-text db nil) command-info {:command command :content content :handler (get-command-handler db (:command command) content)}] diff --git a/src/syng_im/models/chats.cljs b/src/syng_im/models/chats.cljs index b3e9a768c4..a8aab3b576 100644 --- a/src/syng_im/models/chats.cljs +++ b/src/syng_im/models/chats.cljs @@ -140,7 +140,7 @@ (-> (aget (aget (chats-list) 0) "contacts") (r/cljs-list)) - (r/delete (chats-list)) + (r/write (fn [] (r/delete (chats-list)))) (swap! re-frame.db/app-db signal-chats-updated)