Skip to content

Commit

Permalink
Refactored statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
janherich committed Dec 28, 2017
1 parent 77b966c commit 3111c63
Show file tree
Hide file tree
Showing 18 changed files with 279 additions and 318 deletions.
47 changes: 23 additions & 24 deletions src/status_im/chat/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
[status-im.utils.handlers :as handlers]
[status-im.utils.gfycat.core :as gfycat]
[status-im.chat.models :as model]
[status-im.chat.models.unviewed-messages :as unviewed-messages-model]
[status-im.chat.console :as console-chat]
[status-im.chat.constants :as chat-const]
[status-im.data-store.messages :as msg-store]
Expand All @@ -31,7 +30,8 @@
(re-frame/reg-cofx
:stored-unviewed-messages
(fn [cofx _]
(assoc cofx :stored-unviewed-messages (messages-store/get-unviewed))))
(assoc cofx :stored-unviewed-messages
(msg-store/get-unviewed (-> cofx :db :current-public-key)))))

(re-frame/reg-cofx
:get-stored-message
Expand Down Expand Up @@ -170,16 +170,16 @@
(if account-creation?
{:db db
:dispatch load-default-contacts-event}
(let [chat->unviewed-messages (unviewed-messages-model/index-unviewed-messages stored-unviewed-messages)
chat->message-id->request (reduce (fn [acc {:keys [chat-id message-id] :as request}]
(let [chat->message-id->request (reduce (fn [acc {:keys [chat-id message-id] :as request}]
(assoc-in acc [chat-id message-id] request))
{}
stored-unanswered-requests)
chats (reduce (fn [acc {:keys [chat-id] :as chat}]
(assoc acc chat-id (assoc chat
:unviewed-messages (get chat->unviewed-messages chat-id)
:requests (get chat->message-id->request chat-id)
:messages (index-messages (get-stored-messages chat-id)))))
(assoc acc chat-id
(assoc chat
:unviewed-messages (get stored-unviewed-messages chat-id)
:requests (get chat->message-id->request chat-id)
:messages (index-messages (get-stored-messages chat-id)))))
{}
all-stored-chats)]
(-> db
Expand All @@ -190,23 +190,22 @@
(handlers/register-handler-fx
:send-seen!
[re-frame/trim-v]
(fn [{:keys [db]} [{:keys [message-id chat-id from]}]]
(let [{:keys [web3 current-public-key chats]
:contacts/keys [contacts]} db
{:keys [group-chat public?]} (get chats chat-id)]
(fn [{:keys [db]} [{:keys [chat-id from me message-id]}]]
(let [{:keys [web3 chats] :contacts/keys [contacts]} db
{:keys [group-chat public? messages]} (get chats chat-id)
statuses (assoc (get-in messages [message-id :user-statuses]) me :seen)]
(cond-> {:db (-> db
(unviewed-messages-model/remove-unviewed-message chat-id message-id)
(assoc-in [:chats chat-id :messages message-id :message-status] :seen))
:update-message {:message-id message-id
:message-status :seen}}
(and (not (get-in contacts [chat-id] :dapp?))
(not public?))
(assoc :protocol-send-seen
{:web3 web3
:message (cond-> {:from current-public-key
:to from
:message-id message-id}
group-chat (assoc :group-id chat-id))})))))
(update-in [:chats chat-id :unviewed-messages] disj message-id)
(assoc-in [:chats chat-id :messages message-id :user-statuses] statuses))
:update-message {:message-id message-id
:user-statuses statuses}}
;; for public chats and 1-1 bot/dapp chats, it makes no sense to signalise `:seen` msg
(not (or public? (get-in contacts [chat-id] :dapp?)))
(assoc :protocol-send-seen {:web3 web3
:message (cond-> {:from me
:to from
:message-id message-id}
group-chat (assoc :group-id chat-id))})))))

(handlers/register-handler-fx
:show-mnemonic
Expand Down
11 changes: 7 additions & 4 deletions src/status_im/chat/events/sign_up.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@
(sign-up db phone-number message-id)))

(defn- message-seen [{:keys [db] :as fx} message-id]
(-> fx
(assoc-in [:db :chats const/console-chat-id :messages message-id :message-status] :seen)
(assoc :update-message {:message-id message-id
:message-status :seen})))
(let [statuses-path [:chats const/console-chat-id :messages message-id :user-statuses]
statuses (-> (get-in db statuses-path)
(assoc const/console-chat-id :seen))]
(-> fx
(assoc-in (into [:db] statuses-path) statuses)
(assoc :update-message {:message-id message-id
:user-statuses statuses}))))

(handlers/register-handler-fx
:start-listening-confirmation-code-sms
Expand Down
39 changes: 20 additions & 19 deletions src/status_im/chat/models/message.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
[status-im.constants :as constants]
[status-im.chat.utils :as chat-utils]
[status-im.chat.models :as chat-model]
[status-im.chat.models.commands :as commands-model]
[status-im.chat.models.unviewed-messages :as unviewed-messages-model]
[status-im.chat.models.commands :as commands-model]
[status-im.chat.events.requests :as requests-events]
[taoensso.timbre :as log]))

Expand Down Expand Up @@ -33,8 +32,8 @@
(defn- add-message-to-db
[db {:keys [message-id] :as message} chat-id]
(-> db
(chat-utils/add-message-to-db chat-id chat-id message (:new? message))
(unviewed-messages-model/add-unviewed-message chat-id message-id)))
(update-in [:chats chat-id :unviewed-messages] (fnil conj #{}) message-id)
(chat-utils/add-message-to-db chat-id chat-id message (:new? message))))

(defn receive
[{:keys [db message-exists? pop-up-chat? get-last-clock-value now] :as cofx}
Expand All @@ -57,21 +56,23 @@
command-request? (= content-type constants/content-type-command-request)
command (:command content)
enriched-message (cond-> (assoc message
:chat-id chat-identifier
:timestamp (or timestamp now)
:show? true
:clock-value (clocks/receive
clock-value
(get-last-clock-value chat-identifier)))
(and command command-request?)
(assoc-in [:content :content-command-ref]
(lookup-response-ref access-scope->commands-responses
current-account
(get-in fx [:db :chats chat-identifier])
contacts
command)))]
:chat-id chat-identifier
:timestamp (or timestamp now)
:show? true
:clock-value (clocks/receive
clock-value
(get-last-clock-value chat-identifier)))
public-key
(assoc :user-statuses {public-key :received})
(and command command-request?)
(assoc-in [:content :content-command-ref]
(lookup-response-ref access-scope->commands-responses
current-account
(get-in fx [:db :chats chat-identifier])
contacts
command)))]
(cond-> (-> fx
(update :db add-message-to-db enriched-message chat-identifier)
(assoc :save-message (dissoc enriched-message :new?)))
command-request?
(requests-events/add-request chat-identifier enriched-message))))))
command-request?
(requests-events/add-request chat-identifier enriched-message))))))
13 changes: 0 additions & 13 deletions src/status_im/chat/models/unviewed_messages.cljs

This file was deleted.

4 changes: 1 addition & 3 deletions src/status_im/chat/specs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@
(s/def :chat/chat-list-ui-props (s/nilable map?))
(s/def :chat/layout-height (s/nilable number?)) ; height of chat's view layout
(s/def :chat/expandable-view-height-to-value (s/nilable number?))
(s/def :chat/message-status (s/nilable map?)) ; TODO janherich: remove later
(s/def :chat/selected-participants (s/nilable set?))
(s/def :chat/chat-loaded-callbacks (s/nilable map?))
(s/def :chat/command-hash-valid? (s/nilable boolean?))
(s/def :chat/chat-loaded-callbacks (s/nilable map?))
(s/def :chat/public-group-topic (s/nilable string?))
(s/def :chat/confirmation-code-sms-listener (s/nilable any?)) ; .addListener result object
(s/def :chat/messages (s/nilable map?)) ; messages indexed by message-id
Expand Down
8 changes: 7 additions & 1 deletion src/status_im/chat/subs.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@
(fn [[chats current-chat-id]]
(get chats current-chat-id)))

(reg-sub
:get-current-chat-message
:<- [:get-current-chat]
(fn [{:keys [messages]} [_ message-id]]
(get messages message-id)))

(reg-sub
:chat
:<- [:chats]
Expand Down Expand Up @@ -265,7 +271,7 @@
(:web-view-extra-js current-chat)))

(reg-sub
:photo-path
:get-photo-path
:<- [:get-contacts]
(fn [contacts [_ id]]
(:photo-path (contacts id))))
Expand Down
3 changes: 3 additions & 0 deletions src/status_im/chat/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
:new? (if (nil? new?) true new?))]
(update-in db [:chats add-to-chat-id :messages] assoc message-id prepared-message))))

(defn message-seen-by? [message user-pk]
(= :seen (get-in message [:user-statuses user-pk])))

(defn command-name [{:keys [name]}]
(str chat.constants/command-char name))
73 changes: 32 additions & 41 deletions src/status_im/chat/views/input/suggestions.cljs
Original file line number Diff line number Diff line change
@@ -1,72 +1,63 @@
(ns status-im.chat.views.input.suggestions
(:require-macros [status-im.utils.views :refer [defview]])
(:require [re-frame.core :refer [subscribe dispatch]]
[status-im.ui.components.react :refer [view
scroll-view
touchable-highlight
text
icon]]
[status-im.data-store.messages :as messages]
(:require [re-frame.core :as re-frame]
[status-im.ui.components.react :as react]
[status-im.chat.styles.input.suggestions :as style]
[status-im.chat.constants :as const]
[status-im.chat.views.input.animations.expandable :refer [expandable-view]]
[status-im.chat.views.input.utils :as input-utils]
[status-im.i18n :refer [label]]
[taoensso.timbre :as log]
[status-im.chat.utils :as chat-utils]))
[status-im.chat.views.input.animations.expandable :as expandable]
[status-im.chat.utils :as chat.utils]
[status-im.i18n :as i18n]))

(defn suggestion-item [{:keys [on-press name description last?]}]
[touchable-highlight {:on-press on-press}
[view (style/item-suggestion-container last?)
[view {:style style/item-suggestion-name}
[text {:style style/item-suggestion-name-text
:font :roboto-mono} name]]
[text {:style style/item-suggestion-description
:number-of-lines 2}
[react/touchable-highlight {:on-press on-press}
[react/view (style/item-suggestion-container last?)
[react/view {:style style/item-suggestion-name}
[react/text {:style style/item-suggestion-name-text
:font :roboto-mono} name]]
[react/text {:style style/item-suggestion-description
:number-of-lines 2}
description]]])

(defview response-item [{:keys [name description]
{:keys [type message-id]} :request :as command} last?]
[{:keys [chat-id]} [:get-current-chat]]
{:keys [message-id]} :request :as command} last?]
[{{:keys [params]} :content} [:get-current-chat-message message-id]]
[suggestion-item
{:on-press #(let [{:keys [params]} (messages/get-message-content-by-id message-id)
metadata (assoc params :to-message-id message-id)]
(dispatch [:select-chat-input-command command metadata]))
:name (chat-utils/command-name command)
{:on-press #(let [metadata (assoc params :to-message-id message-id)]
(re-frame/dispatch [:select-chat-input-command command metadata]))
:name (chat.utils/command-name command)
:description description
:last? last?}])

(defn command-item [{:keys [name description bot] :as command} last?]
[suggestion-item
{:on-press #(dispatch [:select-chat-input-command command nil])
:name (chat-utils/command-name command)
{:on-press #(re-frame/dispatch [:select-chat-input-command command nil])
:name (chat.utils/command-name command)
:description description
:last? last?}])

(defn item-title [top-padding? s]
[view (style/item-title-container top-padding?)
[text {:style style/item-title-text}
s]])
(defn item-title [top-padding? title]
[react/view (style/item-title-container top-padding?)
[react/text {:style style/item-title-text}
title]])

(defview suggestions-view []
[show-suggestions? [:show-suggestions?]
responses [:get-available-responses]
commands [:get-available-commands]]
(when show-suggestions?
[expandable-view {:key :suggestions
:draggable? false
:height 212}
[view {:flex 1}
[scroll-view {:keyboardShouldPersistTaps :always}
[expandable/expandable-view {:key :suggestions
:draggable? false
:height 212}
[react/view {:flex 1}
[react/scroll-view {:keyboardShouldPersistTaps :always}
(when (seq responses)
[view
[item-title false (label :t/suggestions-requests)]
[react/view
[item-title false (i18n/label :t/suggestions-requests)]
(for [[i response] (map-indexed vector responses)]
^{:key i}
[response-item response (= i (dec (count responses)))])])
(when (seq commands)
[view
[item-title (seq responses) (label :t/suggestions-commands)]
[react/view
[item-title (seq responses) (i18n/label :t/suggestions-commands)]
(for [[i command] (map-indexed vector commands)]
^{:key i}
[command-item command (= i (dec (count commands)))])])]]]))
Loading

0 comments on commit 3111c63

Please sign in to comment.