Repository navigation
Expand file tree
/
Copy pathmodels.cljs
More file actions
126 lines (111 loc) · 5.29 KB
/
Copy pathmodels.cljs
File metadata and controls
126 lines (111 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
(ns status-im.chat.models
(:require [status-im.ui.components.styles :as styles]
[status-im.utils.gfycat.core :as gfycat]
[status-im.transport.utils :as transport.utils]
[status-im.utils.clocks :as utils.clocks]
[status-im.transport.message.core :as transport.message]
[status-im.data-store.chats :as chats-store]
[status-im.transport.message.v1.group-chat :as transport.group-chat]
[status-im.utils.handlers-macro :as handlers-macro]
[status-im.data-store.messages :as messages-store]))
(defn multi-user-chat? [chat-id cofx]
(get-in cofx [:db :chats chat-id :group-chat]))
(defn group-chat? [chat-id cofx]
(and (multi-user-chat? chat-id cofx)
(not (get-in cofx [:db :chats chat-id :public?]))))
(defn public-chat? [chat-id cofx]
(get-in cofx [:db :chats chat-id :public?]))
(defn set-chat-ui-props
"Updates ui-props in active chat by merging provided kvs into them"
[{:keys [current-chat-id] :as db} kvs]
(update-in db [:chat-ui-props current-chat-id] merge kvs))
(defn toggle-chat-ui-prop
"Toggles chat ui prop in active chat"
[{:keys [current-chat-id] :as db} ui-element]
(update-in db [:chat-ui-props current-chat-id ui-element] not))
(defn- create-new-chat
[chat-id {:keys [db now]}]
(let [name (get-in db [:contacts/contacts chat-id :name])]
{:chat-id chat-id
:name (or name (gfycat/generate-gfy chat-id))
:color (styles/random-chat-color)
:group-chat false
:is-active true
:timestamp now
:contacts [chat-id]
:last-clock-value 0}))
(defn upsert-chat
"Upsert chat when not deleted"
[{:keys [chat-id] :as chat-props} {:keys [db] :as cofx}]
(let [chat (merge
(or (get (:chats db) chat-id)
(create-new-chat chat-id cofx))
chat-props)]
(if (:is-active chat)
{:db (update-in db [:chats chat-id] merge chat)
:data-store/tx [(chats-store/save-chat-tx chat)]}
;; when chat is deleted, don't change anything
{:db db})))
(defn add-public-chat
"Adds new public group chat to db & realm"
[topic cofx]
(upsert-chat {:chat-id topic
:is-active true
:name topic
:group-chat true
:contacts []
:public? true} cofx))
(defn add-group-chat
"Adds new private group chat to db & realm"
[chat-id chat-name admin participants cofx]
(upsert-chat {:chat-id chat-id
:name chat-name
:is-active true
:group-chat true
:group-admin admin
:contacts participants} cofx))
(defn clear-history [chat-id {:keys [db] :as cofx}]
(let [{:keys [messages
deleted-at-clock-value]} (get-in db [:chats chat-id])
last-message-clock-value (or (->> messages
vals
(sort-by (comp unchecked-negate :clock-value))
first
:clock-value)
deleted-at-clock-value
(utils.clocks/send 0))]
;; Necessary until we adjust merge-fx to cater for :txs
(-> (select-keys cofx [:data-store/tx :db])
(assoc-in [:db :chats chat-id :messages] {})
(assoc-in [:db :chats chat-id :message-groups] {})
(assoc-in [:db :chats chat-id :deleted-at-clock-value] last-message-clock-value)
(update :data-store/tx concat [(chats-store/clear-history-tx chat-id last-message-clock-value)
(messages-store/delete-messages-tx chat-id)]))))
(defn- remove-transport [chat-id {:keys [db] :as cofx}]
;; if this is private group chat, we have to broadcast leave and unsubscribe after that
(if (group-chat? chat-id cofx)
(transport.message/send (transport.group-chat/GroupLeave.) chat-id cofx)
(transport.utils/unsubscribe-from-chat chat-id cofx)))
(defn- deactivate-chat [chat-id {:keys [db now] :as cofx}]
(assoc-in {:db db
:data-store/tx [(chats-store/deactivate-chat-tx chat-id now)]}
[:db :chats chat-id :is-active] false))
;; TODO: There's a race condition here, as the removal of the filter (async)
;; is done at the same time as the removal of the chat, so a message
;; might come between and restore the chat. Multiple way to handle this
;; (remove chat only after the filter has been removed, probably the safest,
;; flag the chat to ignore new messages, change receive method for public/group chats)
;; For now to keep the code simplier and avoid significant changes, best to leave as it is.
(defn remove-chat [chat-id {:keys [db now] :as cofx}]
(letfn [(remove-transport-fx [chat-id cofx]
(when (multi-user-chat? chat-id cofx)
(remove-transport chat-id cofx)))]
(handlers-macro/merge-fx
cofx
(remove-transport-fx chat-id)
(deactivate-chat chat-id)
(clear-history chat-id))))
(defn bot-only-chat? [db chat-id]
(let [{:keys [group-chat contacts]} (get-in db [:chats chat-id])]
(and (not group-chat)
(get-in db [:contacts/contacts (first contacts) :dapp?]))))