|
7 | 7 | [status-im.protocol.core :as protocol] |
8 | 8 | [status-im.chat.models :as models] |
9 | 9 | [status-im.chat.console :as console] |
10 | | - [status-im.chat.constants :as chat.constants] |
11 | 10 | [status-im.data-store.chats :as chats] |
12 | 11 | [status-im.data-store.messages :as messages] |
13 | 12 | [status-im.data-store.pending-messages :as pending-messages] |
|
29 | 28 | :stored-unviewed-messages |
30 | 29 | (fn [cofx _] |
31 | 30 | (assoc cofx :stored-unviewed-messages |
32 | | - (messages/get-unviewed (-> cofx :db :current-public-key))))) |
| 31 | + (messages/get-unviewed (-> cofx :db :current-public-key))))) |
33 | 32 |
|
34 | 33 | (re-frame/reg-cofx |
35 | 34 | :get-stored-message |
|
129 | 128 | [re-frame/trim-v] |
130 | 129 | (fn [db [details]] |
131 | 130 | (models/set-chat-ui-props db {:show-bottom-info? true |
132 | | - :bottom-info details}))) |
| 131 | + :bottom-info details}))) |
133 | 132 |
|
134 | 133 | (def index-messages (partial into {} (map (juxt :message-id identity)))) |
135 | 134 |
|
|
153 | 152 | (update-in db [:chats chat-id :messages message-id] assoc :appearing? false))) |
154 | 153 |
|
155 | 154 | (defn init-console-chat |
156 | | - [{:keys [chats] :accounts/keys [current-account-id] :as db}] |
| 155 | + [{:keys [chats] :as db}] |
157 | 156 | (if (chats constants/console-chat-id) |
158 | 157 | {:db db} |
159 | | - (cond-> {:db (-> db |
160 | | - (assoc :current-chat-id constants/console-chat-id) |
161 | | - (update :chats assoc constants/console-chat-id console/chat)) |
162 | | - :dispatch-n [[:add-contacts [console/contact]]] |
163 | | - :save-chat console/chat |
164 | | - :save-all-contacts [console/contact]} |
165 | | - |
166 | | - (not current-account-id) |
167 | | - (update :dispatch-n concat [[:chat-received-message/add-when-commands-loaded console/intro-message1]])))) |
| 158 | + {:db (-> db |
| 159 | + (assoc :current-chat-id constants/console-chat-id) |
| 160 | + (update :chats assoc constants/console-chat-id console/chat)) |
| 161 | + :dispatch [:add-contacts [console/contact]] |
| 162 | + :save-chat console/chat |
| 163 | + :save-all-contacts [console/contact]})) |
168 | 164 |
|
169 | 165 | (handlers/register-handler-fx |
170 | 166 | :init-console-chat |
|
186 | 182 | get-stored-messages |
187 | 183 | stored-unviewed-messages |
188 | 184 | stored-message-ids]} _] |
189 | | - (let [{:accounts/keys [account-creation?]} db |
190 | | - load-default-contacts-event [:load-default-contacts!]] |
191 | | - (if account-creation? |
192 | | - {:db db |
193 | | - :dispatch load-default-contacts-event} |
194 | | - (let [chat->message-id->request (reduce (fn [acc {:keys [chat-id message-id] :as request}] |
195 | | - (assoc-in acc [chat-id message-id] request)) |
196 | | - {} |
197 | | - stored-unanswered-requests) |
198 | | - chats (reduce (fn [acc {:keys [chat-id] :as chat}] |
199 | | - (let [chat-messages (index-messages (get-stored-messages chat-id))] |
200 | | - (assoc acc chat-id |
| 185 | + (let [chat->message-id->request (reduce (fn [acc {:keys [chat-id message-id] :as request}] |
| 186 | + (assoc-in acc [chat-id message-id] request)) |
| 187 | + {} |
| 188 | + stored-unanswered-requests) |
| 189 | + chats (reduce (fn [acc {:keys [chat-id] :as chat}] |
| 190 | + (let [chat-messages (index-messages (get-stored-messages chat-id))] |
| 191 | + (assoc acc chat-id |
201 | 192 | (assoc chat |
202 | | - :unviewed-messages (get stored-unviewed-messages chat-id) |
203 | | - :requests (get chat->message-id->request chat-id) |
204 | | - :messages chat-messages |
205 | | - :not-loaded-message-ids (set/difference (get stored-message-ids chat-id) |
206 | | - (-> chat-messages keys set)))))) |
207 | | - {} |
208 | | - all-stored-chats)] |
209 | | - (-> db |
210 | | - (assoc :chats chats |
211 | | - :deleted-chats inactive-chat-ids) |
212 | | - init-console-chat |
213 | | - (update :dispatch-n conj load-default-contacts-event))))))) |
| 193 | + :unviewed-messages (get stored-unviewed-messages chat-id) |
| 194 | + :requests (get chat->message-id->request chat-id) |
| 195 | + :messages chat-messages |
| 196 | + :not-loaded-message-ids (set/difference (get stored-message-ids chat-id) |
| 197 | + (-> chat-messages keys set)))))) |
| 198 | + {} |
| 199 | + all-stored-chats)] |
| 200 | + (-> db |
| 201 | + (assoc :chats chats |
| 202 | + :deleted-chats inactive-chat-ids) |
| 203 | + init-console-chat |
| 204 | + (update :dispatch-n conj [:load-default-contacts!]))))) |
214 | 205 |
|
215 | 206 | (handlers/register-handler-fx |
216 | 207 | :send-seen! |
|
219 | 210 | (let [{:keys [web3 chats] :contacts/keys [contacts]} db |
220 | 211 | {:keys [group-chat public? messages]} (get chats chat-id) |
221 | 212 | statuses (assoc (get-in messages [message-id :user-statuses]) me :seen)] |
222 | | - (cond-> {:db (-> db |
223 | | - (update-in [:chats chat-id :unviewed-messages] disj message-id) |
224 | | - (assoc-in [:chats chat-id :messages message-id :user-statuses] statuses)) |
| 213 | + (cond-> {:db (-> db |
| 214 | + (update-in [:chats chat-id :unviewed-messages] disj message-id) |
| 215 | + (assoc-in [:chats chat-id :messages message-id :user-statuses] statuses)) |
225 | 216 | :update-message {:message-id message-id |
226 | 217 | :user-statuses statuses}} |
227 | | - ;; for public chats and 1-1 bot/dapp chats, it makes no sense to signalise `:seen` msg |
228 | | - (not (or public? (get-in contacts [chat-id :dapp?]))) |
229 | | - (assoc :protocol-send-seen {:web3 web3 |
230 | | - :message (cond-> {:from me |
231 | | - :to from |
232 | | - :message-id message-id} |
233 | | - group-chat (assoc :group-id chat-id))}))))) |
234 | | - |
235 | | -(handlers/register-handler-fx |
236 | | - :show-mnemonic |
237 | | - [re-frame/trim-v] |
238 | | - (fn [{:keys [db]} [mnemonic signing-phrase]] |
239 | | - (let [crazy-math-message? (contains? (get-in db [:chats chat.constants/console-chat-id]) chat.constants/crazy-math-message-id) |
240 | | - messages-events (->> (console/passphrase-messages mnemonic signing-phrase crazy-math-message?) |
241 | | - (mapv #(vector :chat-received-message/add %)))] |
242 | | - {:dispatch-n messages-events}))) |
243 | | - |
244 | | -;; TODO(alwx): can be simplified |
245 | | -(handlers/register-handler-fx |
246 | | - :account-generation-message |
247 | | - (fn [{:keys [db]} _] |
248 | | - (when-not (contains? (get-in db [:chats chat.constants/console-chat-id]) chat.constants/passphrase-message-id) |
249 | | - {:dispatch [:chat-received-message/add console/account-generation-message]}))) |
250 | | - |
251 | | -(handlers/register-handler-fx |
252 | | - :move-to-internal-failure-message |
253 | | - (fn [{:keys [db]} _] |
254 | | - (when-not (contains? (get-in db [:chats chat.constants/console-chat-id]) chat.constants/move-to-internal-failure-message-id) |
255 | | - {:dispatch [:chat-received-message/add console/move-to-internal-failure-message]}))) |
| 218 | + ;; for public chats and 1-1 bot/dapp chats, it makes no sense to signalise `:seen` msg |
| 219 | + (not (or public? (get-in contacts [chat-id :dapp?]))) |
| 220 | + (assoc :protocol-send-seen {:web3 web3 |
| 221 | + :message (cond-> {:from me |
| 222 | + :to from |
| 223 | + :message-id message-id} |
| 224 | + group-chat (assoc :group-id chat-id))}))))) |
256 | 225 |
|
257 | 226 | (handlers/register-handler-fx |
258 | 227 | :browse-link-from-message |
|
269 | 238 | (models/set-chat-ui-props {:validation-messages nil}) |
270 | 239 | (update-in [:chats chat-id] dissoc :chat-loaded-event))} |
271 | 240 |
|
272 | | - chat-loaded-event |
273 | | - (assoc :dispatch chat-loaded-event)))) |
| 241 | + chat-loaded-event |
| 242 | + (assoc :dispatch chat-loaded-event)))) |
274 | 243 |
|
275 | 244 | (handlers/register-handler-fx |
276 | 245 | :add-chat-loaded-event |
277 | 246 | [(re-frame/inject-cofx :get-stored-chat) re-frame/trim-v] |
278 | 247 | (fn [{:keys [db] :as cofx} [chat-id event]] |
279 | 248 | (if (get (:chats db) chat-id) |
280 | 249 | {:db (assoc-in db [:chats chat-id :chat-loaded-event] event)} |
281 | | - (-> (models/add-chat cofx chat-id) ; chat not created yet, we have to create it |
| 250 | + (-> (models/add-chat cofx chat-id) ; chat not created yet, we have to create it |
282 | 251 | (assoc-in [:db :chats chat-id :chat-loaded-event] event))))) |
283 | 252 |
|
284 | 253 | ;; TODO(janherich): remove this unnecessary event in the future (only model function `add-chat` will stay) |
|
309 | 278 | :start-chat |
310 | 279 | [(re-frame/inject-cofx :get-stored-chat) re-frame/trim-v] |
311 | 280 | (fn [{:keys [db] :as cofx} [contact-id {:keys [navigation-replace?]}]] |
312 | | - (when (not= (:current-public-key db) contact-id) ; don't allow to open chat with yourself |
| 281 | + (when (not= (:current-public-key db) contact-id) ; don't allow to open chat with yourself |
313 | 282 | (if (get (:chats db) contact-id) |
314 | 283 | (navigate-to-chat cofx contact-id navigation-replace?) ; existing chat, just preload and displey |
315 | 284 | (let [add-chat-fx (models/add-chat cofx contact-id)] ; new chat, create before preload & display |
|
334 | 303 | (update :chats dissoc chat-id) |
335 | 304 | (update :deleted-chats (fnil conj #{}) chat-id)) |
336 | 305 | :delete-pending-messages chat-id} |
337 | | - (or group-chat debug?) |
338 | | - (assoc :delete-messages chat-id) |
339 | | - debug? |
340 | | - (assoc :delete-chat chat-id) |
341 | | - (not debug?) |
342 | | - (assoc :deactivate-chat chat-id))))) |
| 306 | + (or group-chat debug?) |
| 307 | + (assoc :delete-messages chat-id) |
| 308 | + debug? |
| 309 | + (assoc :delete-chat chat-id) |
| 310 | + (not debug?) |
| 311 | + (assoc :deactivate-chat chat-id))))) |
343 | 312 |
|
344 | 313 | (handlers/register-handler-fx |
345 | 314 | :delete-chat |
|
0 commit comments