remove participants colors, re-join chat, update contacts instead of remove

Former-commit-id: 8f04104cc0
This commit is contained in:
michaelr 2016-04-29 02:06:15 +03:00
parent ff8dc7a4f7
commit de14071c06
6 changed files with 83 additions and 68 deletions

View File

@ -9,22 +9,7 @@
(def content-type-command "command") (def content-type-command "command")
(def content-type-command-request "command-request") (def content-type-command-request "command-request")
(def content-type-status "status") (def content-type-status "status")
(def group-chat-colors [{:background "#AB7967", :text "#FFFFFF"}
{:background "#B48EAD", :text "#FFFFFF"}
{:background "#8FA1B3", :text "#FFFFFF"}
{:background "#96B5B4", :text "#FFFFFF"}
{:background "#A3BE8C", :text "#FFFFFF"}
{:background "#EBCB8B", :text "#FFFFFF"}
{:background "#D08770", :text "#FFFFFF"}
{:background "#BF616A", :text "#FFFFFF"}
{:background "#EFF1F5", :text "#000000"}
{:background "#DFE1E8", :text "#000000"}
{:background "#C0C5CE", :text "#000000"}
{:background "#A7ADBA", :text "#000000"}
{:background "#65737E", :text "#FFFFFF"}
{:background "#4F5B66", :text "#FFFFFF"}
{:background "#343D46", :text "#FFFFFF"}
{:background "#2B303B", :text "#FFFFFF"}])
(comment (comment

View File

@ -29,10 +29,12 @@
check-suggestion]] check-suggestion]]
[syng-im.handlers.sign-up :as sign-up-service] [syng-im.handlers.sign-up :as sign-up-service]
[syng-im.models.chats :refer [create-chat [syng-im.models.chats :refer [chat-exists?
create-chat
chat-add-participants chat-add-participants
chat-remove-participants chat-remove-participants
set-chat-active]] set-chat-active
re-join-group-chat]]
[syng-im.models.chat :refer [signal-chat-updated [syng-im.models.chat :refer [signal-chat-updated
set-current-chat-id set-current-chat-id
current-chat-id current-chat-id
@ -498,8 +500,10 @@
(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)
(create-chat db group-id identities true group-name))) (if (chat-exists? group-id)
(re-join-group-chat db group-id identities group-name)
(create-chat db group-id identities true group-name))))
(comment (comment
(dispatch [:set-signed-up true])
) )

View File

@ -1,11 +1,12 @@
(ns syng-im.models.chats (ns syng-im.models.chats
(:require [syng-im.persistence.realm :as r] (:require [clojure.set :refer [difference]]
[syng-im.persistence.realm :as r]
[syng-im.utils.random :refer [timestamp]] [syng-im.utils.random :refer [timestamp]]
[clojure.string :refer [join blank?]] [clojure.string :refer [join blank?]]
[syng-im.db :as db] [syng-im.db :as db]
[syng-im.utils.logging :as log] [syng-im.utils.logging :as log]
[syng-im.constants :refer [group-chat-colors]] [syng-im.persistence.realm-queries :refer [include-query]]
[syng-im.persistence.realm-queries :refer [include-query]])) [syng-im.models.chat :refer [signal-chat-updated]]))
(defn signal-chats-updated [db] (defn signal-chats-updated [db]
(update-in db db/updated-chats-signal-path (fn [current] (update-in db db/updated-chats-signal-path (fn [current]
@ -31,21 +32,22 @@
(or (chat-name-from-contacts identities) (or (chat-name-from-contacts identities)
chat-id)) chat-id))
(defn chat-exists? [chat-id]
(r/exists? :chats :chat-id chat-id))
(defn create-chat (defn create-chat
([db chat-id identities group-chat?] ([db chat-id identities group-chat?]
(create-chat db chat-id identities group-chat? nil)) (create-chat db chat-id identities group-chat? nil))
([db chat-id identities group-chat? chat-name] ([db chat-id identities group-chat? chat-name]
(if (r/exists? :chats :chat-id chat-id) (if (chat-exists? chat-id)
db db
(let [chat-name (or chat-name (let [chat-name (or chat-name
(get-chat-name chat-id identities)) (get-chat-name chat-id identities))
_ (log/debug "creating chat" chat-name)] _ (log/debug "creating chat" chat-name)]
(r/write (r/write
(fn [] (fn []
(let [contacts (mapv (fn [ident {:keys [background text]}] (let [contacts (mapv (fn [ident]
{:identity ident {:identity ident}) identities)]
:background-color background
:text-color text}) identities group-chat-colors)]
(r/create :chats {:chat-id chat-id (r/create :chats {:chat-id chat-id
:is-active true :is-active true
:name chat-name :name chat-name
@ -54,6 +56,30 @@
:contacts contacts})))) :contacts contacts}))))
(signal-chats-updated db))))) (signal-chats-updated db)))))
(defn chat-contacts [chat-id]
(-> (r/get-by-field :chats :chat-id chat-id)
(r/single)
(aget "contacts")))
(defn re-join-group-chat [db group-id identities group-name]
(r/write
(fn []
(let [new-identities (set identities)
only-old-contacts (->> (chat-contacts group-id)
(r/cljs-list)
(remove (fn [{:keys [identity]}]
(new-identities identity))))
contacts (->> new-identities
(mapv (fn [ident]
{:identity ident}))
(concat only-old-contacts))]
(r/create :chats {:chat-id group-id
:is-active true
:name group-name
:contacts contacts} true))))
(-> (signal-chats-updated db)
(signal-chat-updated group-id)))
(defn chats-list [] (defn chats-list []
(r/sorted (r/get-all :chats) :timestamp :desc)) (r/sorted (r/get-all :chats) :timestamp :desc))
@ -65,20 +91,12 @@
(defn chat-add-participants [chat-id identities] (defn chat-add-participants [chat-id identities]
(r/write (r/write
(fn [] (fn []
(let [contacts (-> (r/get-by-field :chats :chat-id chat-id) (let [contacts (chat-contacts chat-id)]
(r/single) (doseq [contact-identity identities]
(aget "contacts")) (if-let [contact-exists (.find contacts (fn [object index collection]
colors-in-use (set (.map contacts (fn [object index collection] (= contact-identity (aget object "identity"))))]
{:text-color (aget object "text-color") (aset contact-exists "is-in-chat" true)
:background-color (aget object "background-color")}))) (.push contacts (clj->js {:identity contact-identity}))))))))
colors (filter (fn [color]
(not (contains? colors-in-use color))) group-chat-colors)
new-contacts (mapv (fn [ident {:keys [background text]}]
{:identity ident
:background-color background
:text-color text}) identities colors)]
(doseq [contact new-contacts]
(.push contacts (clj->js contact)))))))
(defn chat-remove-participants [chat-id identities] (defn chat-remove-participants [chat-id identities]
(r/write (r/write
@ -87,7 +105,8 @@
chat (r/single (r/get-by-field :chats :chat-id chat-id))] chat (r/single (r/get-by-field :chats :chat-id chat-id))]
(-> (aget chat "contacts") (-> (aget chat "contacts")
(r/filtered query) (r/filtered query)
(r/delete)))))) (.forEach (fn [object index collection]
(aset object "is-in-chat" false))))))))
(defn active-group-chats [] (defn active-group-chats []
(let [results (r/filtered (r/get-all :chats) (let [results (r/filtered (r/get-all :chats)
@ -102,7 +121,7 @@
(r/single) (r/single)
(aset "is-active" active?))))) (aset "is-active" active?)))))
#_(comment (comment
(active-group-chats) (active-group-chats)
@ -119,8 +138,7 @@
(-> (aget (aget (chats-list) 0) "contacts") (-> (aget (aget (chats-list) 0) "contacts")
(js->clj :keywordize-keys true) (r/cljs-list))
)
(r/delete (chats-list)) (r/delete (chats-list))
@ -136,4 +154,8 @@
(swap! re-frame.db/app-db (fn [db] (swap! re-frame.db/app-db (fn [db]
(create-chat db "A group chat"))) (create-chat db "A group chat")))
(-> (chats-list)
(.find (fn [object index collection]
(= "console1" (aget object "chat-id")))))
) )

View File

@ -111,13 +111,13 @@
(comment (comment
(r/write #(create-contact {:phone-number "0543072333" (r/write #(create-contact {:phone-number "0543072333"
:whisper-identity "0x041e1a37a0317a66f8d826e6779d808a9f39b88c2f896c5d3c08c4ded259719be256e4e6d6f49dab2324993ec822b588f2c0591b7a723cb0be659f2eccf48b5fed" :whisper-identity "0x04e43e861a6dd99ad9eee7bd58af89dcaa430188ebec8698de7b7bad54573324fff4ac5cb9bb277af317efd7abfc917b91bf48cc41e40bf70062fd79400016a1f9"
:name "Mr. Bean" :name "Splinter"
:photo-path ""})) :photo-path ""}))
(r/write #(create-contact {:phone-number "0544828649" (r/write #(create-contact {:phone-number "0544828649"
:whisper-identity "0x04dcbf434bbf6925f251b7f43337f66a5a3f943e8983284045af703551cab39684d8c838e73f0234169f26fe126d7ef1ea3b8c1013e7dad1d4c5a82c7a651647fd" :whisper-identity "0x0487954e7fa746d8cf787403c2c491aadad540b9bb1f0f7b8184792e91c33b6a394079295f5777ec6d4af9ad5ba24794b3ff1ec8be9ff6a708c85a163733192665"
:name "Mr. Batman" :name "Exodius"
:photo-path ""})) :photo-path ""}))
(r/write #(create-contact {:phone-number "0522222222" (r/write #(create-contact {:phone-number "0522222222"

View File

@ -35,8 +35,8 @@
:optional true}}} :optional true}}}
{:name :chat-contact {:name :chat-contact
:properties {:identity "string" :properties {:identity "string"
:text-color "string" :is-in-chat {:type "bool"
:background-color "string"}} :default true}}}
{:name :chats {:name :chats
:primaryKey :chat-id :primaryKey :chat-id
:properties {:chat-id "string" :properties {:chat-id "string"
@ -106,6 +106,10 @@
(some-> (aget result 0) (some-> (aget result 0)
(js->clj :keywordize-keys true))) (js->clj :keywordize-keys true)))
(defn cljs-list [results]
(-> (js->clj results :keywordize-keys true)
(vals)))
(defn list-to-array [record list-field] (defn list-to-array [record list-field]
(update-in record [list-field] (comp vec vals))) (update-in record [list-field] (comp vec vals)))