Group settings handler/sub

Former-commit-id: 1059c47826
This commit is contained in:
virvar 2016-05-16 19:38:35 +03:00
parent a0b0e1e8c6
commit 2d32a5c507
6 changed files with 108 additions and 119 deletions

View File

@ -16,9 +16,7 @@
[syng-im.components.styles :refer [color-purple [syng-im.components.styles :refer [color-purple
text2-color]] text2-color]]
[syng-im.group-settings.styles.group-settings :as st] [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.group-settings.views.member :refer [member-view]]
[syng-im.utils.logging :as log]
[reagent.core :as r])) [reagent.core :as r]))
(defn remove-member [{:keys [whisper-identity]}] (defn remove-member [{:keys [whisper-identity]}]

View File

@ -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))))

View File

@ -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))))

View File

@ -13,8 +13,6 @@
[syng-im.models.contacts :as contacts] [syng-im.models.contacts :as contacts]
[syng-im.models.messages :refer [save-message [syng-im.models.messages :refer [save-message
update-message! update-message!
message-by-id
get-messages
clear-history]] clear-history]]
[syng-im.models.commands :refer [set-commands]] [syng-im.models.commands :refer [set-commands]]
[syng-im.handlers.server :as server] [syng-im.handlers.server :as server]
@ -22,13 +20,9 @@
[syng-im.handlers.sign-up :as sign-up-service] [syng-im.handlers.sign-up :as sign-up-service]
[syng-im.models.chats :refer [chat-exists? [syng-im.models.chats :refer [chat-exists?
create-chat create-chat
set-group-chat-name
set-chat-color
chat-remove-member
chat-add-participants chat-add-participants
chat-remove-participants chat-remove-participants
set-chat-active set-chat-active
delete-chat
re-join-group-chat re-join-group-chat
chat-by-id2]] chat-by-id2]]
[syng-im.utils.logging :as log] [syng-im.utils.logging :as log]
@ -41,6 +35,7 @@
[syng-im.utils.crypt :refer [gen-random-bytes]] [syng-im.utils.crypt :refer [gen-random-bytes]]
[syng-im.utils.random :as random] [syng-im.utils.random :as random]
syng-im.chat.handlers syng-im.chat.handlers
[syng-im.group-settings.handlers :refer [delete-chat]]
[syng-im.navigation.handlers :as nav] [syng-im.navigation.handlers :as nav]
syng-im.discovery.handlers syng-im.discovery.handlers
syng-im.contacts.handlers)) syng-im.contacts.handlers))
@ -219,13 +214,6 @@
(update-message! {:msg-id msg-id (update-message! {:msg-id msg-id
:delivery-status :failed}))) :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 (register-handler :leave-group-chat
(fn [db [action]] (fn [db [action]]
(log/debug action) (log/debug action)
@ -295,43 +283,6 @@
(dispatch [:show-chat group-id :replace]) (dispatch [:show-chat group-id :replace])
db))) 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 (register-handler :group-chat-invite-received
(fn [db [action from group-id identities group-name]] (fn [db [action from group-id identities group-name]]
(log/debug action from group-id identities) (log/debug action from group-id identities)

View File

@ -7,10 +7,7 @@
[syng-im.utils.logging :as log] [syng-im.utils.logging :as log]
[syng-im.constants :refer [content-type-status]] [syng-im.constants :refer [content-type-status]]
[syng-im.models.messages :refer [save-message]] [syng-im.models.messages :refer [save-message]]
[syng-im.persistence.realm-queries :refer [include-query]] [syng-im.persistence.realm-queries :refer [include-query]]))
[syng-im.models.chat :refer [current-chat-id
current-chat
signal-chat-updated]]))
(defn chat-name-from-contacts [identities] (defn chat-name-from-contacts [identities]
(let [chat-name (->> identities (let [chat-name (->> identities
@ -66,22 +63,6 @@
(add-status-message chat-id) (add-status-message chat-id)
db)))) 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] (defn chat-contacts [chat-id]
(-> (r/get-by-field :chats :chat-id chat-id) (-> (r/get-by-field :chats :chat-id chat-id)
(r/single) (r/single)
@ -148,37 +129,14 @@
(.forEach (fn [object _ _] (.forEach (fn [object _ _]
(aset object "is-in-chat" false)))))))) (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 [] (defn active-group-chats []
(let [results (r/filtered (r/get-all :chats) (let [results (r/filtered (r/get-all :chats)
"group-chat = true && is-active = true")] "group-chat = true && is-active = true")]
(js->clj (.map results (fn [object _ _] (js->clj (.map results (fn [object _ _]
(aget object "chat-id")))))) (aget object "chat-id"))))))
(defn set-chat-active [chat-id active?] (defn set-chat-active [chat-id active?]
(r/write (fn [] (r/write (fn []
(-> (r/get-by-field :chats :chat-id chat-id) (-> (r/get-by-field :chats :chat-id chat-id)
(r/single) (r/single)
(aset "is-active" active?))))) (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]))

View File

@ -1,13 +1,8 @@
(ns syng-im.subs (ns syng-im.subs
(:require-macros [reagent.ratom :refer [reaction]]) (:require-macros [reagent.ratom :refer [reaction]])
(:require [re-frame.core :refer [register-sub]] (: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.chat.subs
syng-im.group-settings.subs
syng-im.navigation.subs syng-im.navigation.subs
syng-im.discovery.subs syng-im.discovery.subs
syng-im.contacts.subs)) syng-im.contacts.subs))
@ -17,14 +12,6 @@
(register-sub :get (register-sub :get
(fn [db [_ k]] (fn [db [_ k]]
(reaction (k @db)))) (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 -------------------------------------------------------------- ;; -- User data --------------------------------------------------------------
(register-sub (register-sub
@ -32,15 +19,5 @@
(fn [db _] (fn [db _]
(reaction (:signed-up @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 (register-sub :db
(fn [db _] (reaction @db))) (fn [db _] (reaction @db)))