From 2d32a5c507ae9d086c131e30ef70b686640b1740 Mon Sep 17 00:00:00 2001
From: virvar <revalka@gmail.com>
Date: Mon, 16 May 2016 19:38:35 +0300
Subject: [PATCH] Group settings handler/sub

Former-commit-id: 1059c47826df191e5fd6a6c39540d7df7c80e778
---
 .../group_settings/group_settings.cljs        |  2 -
 src/syng_im/group_settings/handlers.cljs      | 77 +++++++++++++++++++
 src/syng_im/group_settings/subs.cljs          | 28 +++++++
 src/syng_im/handlers.cljs                     | 51 +-----------
 src/syng_im/models/chats.cljs                 | 44 +----------
 src/syng_im/subs.cljs                         | 25 +-----
 6 files changed, 108 insertions(+), 119 deletions(-)
 create mode 100644 src/syng_im/group_settings/handlers.cljs
 create mode 100644 src/syng_im/group_settings/subs.cljs

diff --git a/src/syng_im/group_settings/group_settings.cljs b/src/syng_im/group_settings/group_settings.cljs
index baba9bc9b1..5d23a3ea40 100644
--- a/src/syng_im/group_settings/group_settings.cljs
+++ b/src/syng_im/group_settings/group_settings.cljs
@@ -16,9 +16,7 @@
             [syng-im.components.styles :refer [color-purple
                                                text2-color]]
             [syng-im.group-settings.styles.group-settings :as st]
-            [syng-im.utils.listview :refer [to-realm-datasource]]
             [syng-im.group-settings.views.member :refer [member-view]]
-            [syng-im.utils.logging :as log]
             [reagent.core :as r]))
 
 (defn remove-member [{:keys [whisper-identity]}]
diff --git a/src/syng_im/group_settings/handlers.cljs b/src/syng_im/group_settings/handlers.cljs
new file mode 100644
index 0000000000..218ab1c61c
--- /dev/null
+++ b/src/syng_im/group_settings/handlers.cljs
@@ -0,0 +1,77 @@
+(ns syng-im.group-settings.handlers
+  (:require [re-frame.core :refer [register-handler debug dispatch]]
+            [syng-im.db :as db]
+            [syng-im.persistence.realm :as r]))
+
+(defn set-group-chat-name [db name]
+  (let [chat-id (:current-chat-id db)]
+    (r/write (fn []
+               (-> (r/get-by-field :chats :chat-id chat-id)
+                   (r/single)
+                   (aset "name" name))))
+    (assoc-in db (db/chat-name-path chat-id) name)))
+
+(defn set-chat-color [db color]
+  (let [chat-id (:current-chat-id db)]
+    (r/write (fn []
+               (-> (r/get-by-field :chats :chat-id chat-id)
+                   (r/single)
+                   (aset "color" color))))
+    (assoc-in db (db/chat-color-path chat-id) color)))
+
+(defn chat-remove-member [db identity]
+  (let [chat (get-in db [:chats (:current-chat-id db)])]
+    (r/write
+     (fn []
+       (r/create :chats
+                 (update chat :contacts
+                         (fn [members]
+                           (filter #(not= (:identity %) identity) members)))
+                 true)))
+    ;; TODO temp. Update chat in db atom
+    (dispatch [:initialize-chats])
+    db))
+
+(defn delete-chat [chat-id]
+  (r/write
+   (fn []
+     (-> (r/get-by-field :chats :chat-id chat-id)
+         (r/single)
+         (r/delete))))
+  ;; TODO temp. Update chat in db atom
+  (dispatch [:initialize-chats]))
+
+(register-handler :show-group-settings
+  (fn [db [action]]
+    (dispatch [:navigate-to :group-settings])
+    db))
+
+(register-handler :set-group-chat-name
+  (fn [db [action chat-name]]
+    (set-group-chat-name db chat-name)))
+
+(register-handler :set-chat-color
+  (fn [db [action color]]
+    (set-chat-color db color)))
+
+(register-handler :select-group-chat-member
+  (fn [db [action identity]]
+    (assoc-in db db/group-settings-selected-member-path identity)))
+
+(register-handler :show-group-settings-color-picker
+  (fn [db [action show?]]
+    (assoc-in db db/group-settings-show-color-picker show?)))
+
+(register-handler :chat-remove-member
+  (fn [db [action identity]]
+    (let [chat-id (:current-chat-id db)
+          db      (chat-remove-member db identity)]
+      (dispatch [:select-group-chat-member nil])
+      ;; TODO fix and uncomment
+      ;; (api/group-remove-participant chat-id identity)
+      ;; (removed-participant-msg chat-id identity)
+      )))
+
+(register-handler :clear-history
+  (fn [db [action]]
+    (clear-history (:current-chat-id db))))
diff --git a/src/syng_im/group_settings/subs.cljs b/src/syng_im/group_settings/subs.cljs
new file mode 100644
index 0000000000..f544cc3294
--- /dev/null
+++ b/src/syng_im/group_settings/subs.cljs
@@ -0,0 +1,28 @@
+(ns syng-im.group-settings.subs
+  (:require-macros [reagent.ratom :refer [reaction]])
+  (:require [re-frame.core :refer [register-sub]]
+            [syng-im.db :as db]
+            [syng-im.models.chats :refer [chats-list chat-by-id]]
+            [syng-im.models.contacts :refer [get-contacts
+                                             contacts-list-exclude
+                                             contacts-list-include
+                                             contact-by-identity]]
+            [syng-im.handlers.content-suggestions :refer [get-content-suggestions]]))
+
+(register-sub :get-current-chat-name
+  (fn [db _]
+    (reaction (get-in @db (db/chat-name-path (:current-chat-id @db))))))
+
+(register-sub :get-current-chat-color
+  (fn [db _]
+    (reaction (get-in @db (db/chat-color-path (:current-chat-id @db))))))
+
+(register-sub :selected-group-chat-member
+  (fn [db [_]]
+    (reaction
+     (let [identity (get-in @db db/group-settings-selected-member-path)]
+       (contact-by-identity identity)))))
+
+(register-sub :group-settings-show-color-picker
+  (fn [db [_]]
+    (reaction (get-in @db db/group-settings-show-color-picker))))
diff --git a/src/syng_im/handlers.cljs b/src/syng_im/handlers.cljs
index 2da34d7660..16a0cf4855 100644
--- a/src/syng_im/handlers.cljs
+++ b/src/syng_im/handlers.cljs
@@ -13,8 +13,6 @@
     [syng-im.models.contacts :as contacts]
     [syng-im.models.messages :refer [save-message
                                      update-message!
-                                     message-by-id
-                                     get-messages
                                      clear-history]]
     [syng-im.models.commands :refer [set-commands]]
     [syng-im.handlers.server :as server]
@@ -22,13 +20,9 @@
     [syng-im.handlers.sign-up :as sign-up-service]
     [syng-im.models.chats :refer [chat-exists?
                                   create-chat
-                                  set-group-chat-name
-                                  set-chat-color
-                                  chat-remove-member
                                   chat-add-participants
                                   chat-remove-participants
                                   set-chat-active
-                                  delete-chat
                                   re-join-group-chat
                                   chat-by-id2]]
     [syng-im.utils.logging :as log]
@@ -41,6 +35,7 @@
     [syng-im.utils.crypt :refer [gen-random-bytes]]
     [syng-im.utils.random :as random]
     syng-im.chat.handlers
+    [syng-im.group-settings.handlers :refer [delete-chat]]
     [syng-im.navigation.handlers :as nav]
     syng-im.discovery.handlers
     syng-im.contacts.handlers))
@@ -219,13 +214,6 @@
     (update-message! {:msg-id          msg-id
                       :delivery-status :failed})))
 
-(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))))
-
 (register-handler :leave-group-chat
   (fn [db [action]]
     (log/debug action)
@@ -295,43 +283,6 @@
       (dispatch [:show-chat group-id :replace])
       db)))
 
-(register-handler :show-group-settings
-  (fn [db [action]]
-    (log/debug action)
-    (dispatch [:navigate-to :group-settings])
-    db))
-
-(register-handler :set-group-chat-name
-  (fn [db [action chat-name]]
-    (log/debug action)
-    (set-group-chat-name db chat-name)))
-
-(register-handler :set-chat-color
-  (fn [db [action color]]
-    (log/debug action)
-    (set-chat-color db color)))
-
-(register-handler :select-group-chat-member
-  (fn [db [action identity]]
-    (log/debug action)
-    (assoc-in db db/group-settings-selected-member-path identity)))
-
-(register-handler :show-group-settings-color-picker
-  (fn [db [action show?]]
-    (log/debug action)
-    (assoc-in db db/group-settings-show-color-picker show?)))
-
-(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])
-      ;; TODO fix and uncomment
-      ;; (api/group-remove-participant chat-id identity)
-      ;; (removed-participant-msg chat-id identity)
-      (signal-chat-updated db chat-id))))
-
 (register-handler :group-chat-invite-received
   (fn [db [action from group-id identities group-name]]
     (log/debug action from group-id identities)
diff --git a/src/syng_im/models/chats.cljs b/src/syng_im/models/chats.cljs
index c7cc356e2e..de9ea76963 100644
--- a/src/syng_im/models/chats.cljs
+++ b/src/syng_im/models/chats.cljs
@@ -7,10 +7,7 @@
             [syng-im.utils.logging :as log]
             [syng-im.constants :refer [content-type-status]]
             [syng-im.models.messages :refer [save-message]]
-            [syng-im.persistence.realm-queries :refer [include-query]]
-            [syng-im.models.chat :refer [current-chat-id
-                                         current-chat
-                                         signal-chat-updated]]))
+            [syng-im.persistence.realm-queries :refer [include-query]]))
 
 (defn chat-name-from-contacts [identities]
   (let [chat-name (->> identities
@@ -66,22 +63,6 @@
        (add-status-message chat-id)
        db))))
 
-(defn set-group-chat-name [db name]
-  (let [chat-id (current-chat-id db)]
-    (r/write (fn []
-               (-> (r/get-by-field :chats :chat-id chat-id)
-                   (r/single)
-                   (aset "name" name))))
-    (assoc-in db (db/chat-name-path chat-id) name)))
-
-(defn set-chat-color [db color]
-  (let [chat-id (current-chat-id db)]
-    (r/write (fn []
-               (-> (r/get-by-field :chats :chat-id chat-id)
-                   (r/single)
-                   (aset "color" color))))
-    (assoc-in db (db/chat-color-path chat-id) color)))
-
 (defn chat-contacts [chat-id]
   (-> (r/get-by-field :chats :chat-id chat-id)
       (r/single)
@@ -148,37 +129,14 @@
             (.forEach (fn [object _ _]
                         (aset object "is-in-chat" false))))))))
 
-(defn chat-remove-member [db identity]
-  (let [chat (current-chat db)]
-    (r/write
-     (fn []
-       (r/create :chats
-                 (update chat :contacts
-                         (fn [members]
-                           (filter #(not= (:identity %) identity) members)))
-                 true)))
-    ;; TODO temp. Update chat in db atom
-    (dispatch [:initialize-chats])
-    db))
-
 (defn active-group-chats []
   (let [results (r/filtered (r/get-all :chats)
                             "group-chat = true && is-active = true")]
     (js->clj (.map results (fn [object _ _]
                              (aget object "chat-id"))))))
 
-
 (defn set-chat-active [chat-id active?]
   (r/write (fn []
              (-> (r/get-by-field :chats :chat-id chat-id)
                  (r/single)
                  (aset "is-active" active?)))))
-
-(defn delete-chat [chat-id]
-  (r/write
-   (fn []
-     (-> (r/get-by-field :chats :chat-id chat-id)
-         (r/single)
-         (r/delete))))
-  ;; TODO temp. Update chat in db atom
-  (dispatch [:initialize-chats]))
diff --git a/src/syng_im/subs.cljs b/src/syng_im/subs.cljs
index 17adff37ec..6e4c360999 100644
--- a/src/syng_im/subs.cljs
+++ b/src/syng_im/subs.cljs
@@ -1,13 +1,8 @@
 (ns syng-im.subs
   (:require-macros [reagent.ratom :refer [reaction]])
   (:require [re-frame.core :refer [register-sub]]
-            [syng-im.models.chats :refer [chats-list chat-by-id]]
-            [syng-im.models.contacts :refer [get-contacts
-                                         current-chat
-                                             contacts-list-exclude
-                                             contacts-list-include
-                                             contact-by-identity]]
             syng-im.chat.subs
+            syng-im.group-settings.subs
             syng-im.navigation.subs
             syng-im.discovery.subs
             syng-im.contacts.subs))
@@ -17,14 +12,6 @@
 (register-sub :get
   (fn [db [_ k]]
     (reaction (k @db))))
-(register-sub :get-current-chat-name
-  (fn [db _]
-    (let [current-chat-id (current-chat-id @db)]
-      (reaction (get-in @db (db/chat-name-path current-chat-id))))))
-(register-sub :get-current-chat-color
-  (fn [db _]
-    (let [current-chat-id (current-chat-id @db)]
-      (reaction (get-in @db (db/chat-color-path current-chat-id))))))
 
 ;; -- User data --------------------------------------------------------------
 (register-sub
@@ -32,15 +19,5 @@
   (fn [db _]
     (reaction (:signed-up @db))))
 
-(register-sub :selected-group-chat-member
-  (fn [db [_]]
-    (reaction
-     (let [identity (get-in @db db/group-settings-selected-member-path)]
-       (contact-by-identity identity)))))
-
-(register-sub :group-settings-show-color-picker
-  (fn [db [_]]
-    (reaction (get-in @db db/group-settings-show-color-picker))))
-
 (register-sub :db
   (fn [db _] (reaction @db)))