Skip to content

Commit

Permalink
Add chat commands
Browse files Browse the repository at this point in the history
  • Loading branch information
yenda committed Jan 10, 2020
1 parent bdcb34f commit c4e8012
Show file tree
Hide file tree
Showing 34 changed files with 944 additions and 225 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions ios/StatusIm/Images.xcassets/tiny-warning.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "tiny-warning.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "tiny-warning@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "tiny-warning@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 1 addition & 4 deletions src/status_im/chat/models.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
[status-im.i18n :as i18n]
[status-im.mailserver.core :as mailserver]
[status-im.transport.message.protocol :as transport.protocol]
[status-im.tribute-to-talk.core :as tribute-to-talk]
[status-im.ui.components.colors :as colors]
[status-im.ui.components.react :as react]
[status-im.ui.screens.navigation :as navigation]
Expand Down Expand Up @@ -227,8 +226,7 @@
(when platform/desktop?
(mark-messages-seen chat-id))
(when (and (one-to-one-chat? cofx chat-id) (not (contact.db/contact-exists? db chat-id)))
(contact.core/create-contact chat-id))
(tribute-to-talk/check-tribute chat-id)))
(contact.core/create-contact chat-id))))

(fx/defn navigate-to-chat
"Takes coeffects map and chat-id, returns effects necessary for navigation and preloading data"
Expand Down Expand Up @@ -294,5 +292,4 @@
[cofx identity]
(fx/merge (assoc-in cofx [:db :contacts/identity] identity)
(contact.core/create-contact identity)
(tribute-to-talk/check-tribute identity)
(navigation/navigate-to-cofx :profile nil)))
60 changes: 37 additions & 23 deletions src/status_im/chat/models/message.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
[status-im.transport.message.protocol :as protocol]
[status-im.transport.message.transit :as transit]
[status-im.transport.utils :as transport.utils]
[status-im.tribute-to-talk.core :as tribute-to-talk]
[status-im.ui.components.react :as react]
[status-im.utils.clocks :as utils.clocks]
[status-im.utils.datetime :as time]
Expand Down Expand Up @@ -58,11 +57,31 @@
:body (str body-first-line (:text content))
:prioritary? (not (chat-model/multi-user-chat? cofx chat-id))}))

(fx/defn rebuild-message-list
[{:keys [db]} chat-id]
{:db (assoc-in db [:chats chat-id :message-list]
(message-list/add-many nil (vals (get-in db [:chats chat-id :messages]))))})

(fx/defn hide-message
"Hide chat message, rebuild message-list"
[{:keys [db] :as cofx} chat-id {:keys [seen message-id]}]
(fx/merge cofx
{:db (update-in db [:chats chat-id :messages] dissoc message-id)}
#(when (not seen)
(fx/merge %
{:db (update-in db [:chats chat-id]
update
:unviewed-messages-count dec)}
(data-store.messages/mark-messages-seen chat-id [message-id])))
(rebuild-message-list chat-id)))

(fx/defn add-message
[{:keys [db] :as cofx}
{{:keys [chat-id message-id timestamp from] :as message} :message
{{:keys [chat-id message-id replace timestamp from] :as message} :message
:keys [current-chat?]}]
(let [current-public-key (multiaccounts.model/current-public-key cofx)
message-to-be-removed (when replace
(get-in db [:chats chat-id :messages replace]))
prepared-message (prepare-message message chat-id current-chat?)]
(when (and platform/desktop?
(not= from current-public-key)
Expand All @@ -71,20 +90,23 @@
(let [{:keys [title body prioritary?]} (build-desktop-notification cofx message)]
(.displayNotification react/desktop-notification title body prioritary?)))
(fx/merge cofx
{:db (cond->
(-> db
;; We should not be always adding to the list, as it does not make sense
;; if the chat has not been initialized, but run into
;; some troubles disabling it, so next time
(update-in [:chats chat-id :messages] assoc message-id prepared-message)
(update-in [:chats chat-id :message-list] message-list/add prepared-message))
(and (not current-chat?)
(not= from current-public-key))
(update-in [:chats chat-id :loaded-unviewed-messages-ids]
(fnil conj #{}) message-id))}
(when message-to-be-removed
(hide-message chat-id message-to-be-removed))
(fn [{:keys [db]}]
{:db (cond->
(-> db
;; We should not be always adding to the list, as it does not make sense
;; if the chat has not been initialized, but run into
;; some troubles disabling it, so next time
(update-in [:chats chat-id :messages] assoc message-id prepared-message)
(update-in [:chats chat-id :message-list] message-list/add prepared-message))

(and (not current-chat?)
(not= from current-public-key))
(update-in [:chats chat-id :loaded-unviewed-messages-ids]
(fnil conj #{}) message-id))})
(when (and platform/desktop?
(not (system-message? prepared-message)))

(chat-model/update-dock-badge-label)))))

(fx/defn add-received-message
Expand Down Expand Up @@ -175,11 +197,6 @@
:on-error #(log/error "failed to re-send message" %)}]}
(update-message-status chat-id message-id :sending)))

(fx/defn rebuild-message-list
[{:keys [db]} chat-id]
{:db (assoc-in db [:chats chat-id :message-list]
(message-list/add-many nil (vals (get-in db [:chats chat-id :messages]))))})

(fx/defn delete-message
"Deletes chat message, rebuild message-list"
[{:keys [db] :as cofx} chat-id message-id]
Expand All @@ -200,10 +217,7 @@

(fx/defn send-message
[{:keys [db now] :as cofx} {:keys [chat-id] :as message}]
(let [{:keys [chats]} db
message-data (-> message
(tribute-to-talk/add-transaction-hash db))]
(protocol/send-chat-message cofx message-data)))
(protocol/send-chat-message cofx message))

(fx/defn toggle-expand-message
[{:keys [db]} chat-id message-id]
Expand Down
36 changes: 27 additions & 9 deletions src/status_im/chat/models/message_list.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,15 @@
(.prev iter)
e))

(defn insert-message
"Insert a message in the list, pull it's left and right messages, calculate
its positional metadata, and update the left & right messages if necessary,
this operation is O(logN) for insertion, and O(logN) for the updates, as
we need to re-find (there's probably a better way)"
[old-message-list {:keys [key] :as prepared-message}]
(let [tree (.insert old-message-list prepared-message prepared-message)
iter (.find tree prepared-message)
(defn update-message
"Update the message and siblings with positional info"
[tree message]
(let [iter (.find tree message)
previous-message (when (.-hasPrev iter)
(get-prev-element iter))
next-message (when (.-hasNext iter)
(get-next-element iter))
message-with-pos-data (add-group-info prepared-message previous-message next-message)]
message-with-pos-data (add-group-info message previous-message next-message)]
(cond->
(.update iter message-with-pos-data)

Expand All @@ -156,6 +152,28 @@
(-> (.find previous-message)
(.update (update-previous-message message-with-pos-data previous-message))))))

(defn remove-message
"Remove a message in the list"
[tree prepared-message]
(let [iter (.find tree prepared-message)]
(if (not iter)
tree
(let [new-tree (.remove iter)
next-message (when (.-hasNext iter)
(get-next-element iter))]
(if (not next-message)
new-tree
(update-message new-tree next-message))))))

(defn insert-message
"Insert a message in the list, pull it's left and right messages, calculate
its positional metadata, and update the left & right messages if necessary,
this operation is O(logN) for insertion, and O(logN) for the updates, as
we need to re-find (there's probably a better way)"
[old-message-list prepared-message]
(let [tree (.insert old-message-list prepared-message prepared-message)]
(update-message tree prepared-message)))

(defn add [message-list message]
(insert-message (or message-list (dependencies/rb-tree compare-fn))
(prepare-message message)))
Expand Down
42 changes: 42 additions & 0 deletions src/status_im/commands/core.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(ns status-im.commands.core
(:require
[re-frame.core :as re-frame]
[status-im.ethereum.core :as ethereum]
[status-im.ethereum.json-rpc :as json-rpc]
[status-im.utils.fx :as fx]))

(fx/defn handle-prepare-accept-request-address-for-transaction
{:events [::prepare-accept-request-address-for-transaction]}
[{:keys [db]} message]
{:db (assoc db
:commands/select-account
{:message message
:from (ethereum/get-default-account (:multiaccount/accounts db))})})

(fx/defn set-selected-account
{:events [::set-selected-account]}
[{:keys [db]} _ account]
{:db (-> (assoc-in db [:commands/select-account :from] account)
(assoc :bottom-sheet/show? false))})

(fx/defn handle-accept-request-address-for-transaction
{:events [::accept-request-address-for-transaction]}
[{:keys [db]} message-id address]
{:db (dissoc db :commands/select-account)
::json-rpc/call [{:method "shhext_acceptRequestAddressForTransaction"
:params [message-id address]
:on-success #(re-frame/dispatch [:transport/message-sent % 1])}]})

(fx/defn handle-decline-request-address-for-transaction
{:events [::decline-request-address-for-transaction]}
[cofx message-id]
{::json-rpc/call [{:method "shhext_declineRequestAddressForTransaction"
:params [message-id]
:on-success #(re-frame/dispatch [:transport/message-sent % 1])}]})

(fx/defn handle-decline-request-transaction
{:events [::decline-request-transaction]}
[cofx message-id]
{::json-rpc/call [{:method "shhext_declineRequestTransaction"
:params [message-id]
:on-success #(re-frame/dispatch [:transport/message-sent % 1])}]})
27 changes: 18 additions & 9 deletions src/status_im/constants.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@

(def ms-in-bg-for-require-bioauth 5000)

(def content-type-text 1)
(def content-type-sticker 2)
(def content-type-status 3)
(def content-type-emoji 4)

(def message-type-one-to-one 1)
(def message-type-public-group 2)
(def message-type-private-group 3)
(def message-type-private-group-system-message 4)
(def ^:const content-type-text 1)
(def ^:const content-type-sticker 2)
(def ^:const content-type-status 3)
(def ^:const content-type-emoji 4)
(def ^:const content-type-command 5)

(def ^:const message-type-one-to-one 1)
(def ^:const message-type-public-group 2)
(def ^:const message-type-private-group 3)
(def ^:const message-type-private-group-system-message 4)

(def ^:const command-state-request-address-for-transaction 1)
(def ^:const command-state-request-address-for-transaction-declined 2)
(def ^:const command-state-request-address-for-transaction-accepted 3)
(def ^:const command-state-request-transaction 4)
(def ^:const command-state-request-transaction-declined 5)
(def ^:const command-state-transaction-pending 6)
(def ^:const command-state-transaction-sent 7)

(def desktop-content-types
#{content-type-text content-type-emoji content-type-status})
Expand Down
11 changes: 6 additions & 5 deletions src/status_im/contact/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
(fx/defn load-contacts
{:events [::contacts-loaded]}
[{:keys [db] :as cofx} all-contacts]
(let [contacts-list (map #(vector (:public-key %) %) all-contacts)
(let [contacts-list (map #(vector (:public-key %) (if (empty? (:address %))
(dissoc % :address)
%))
all-contacts)
contacts (into {} contacts-list)
tr-to-talk-enabled? (-> db tribute-to-talk/get-settings tribute-to-talk/enabled?)]
(fx/merge cofx
Expand Down Expand Up @@ -123,12 +126,10 @@
(cond-> {:public-key public-key
:photo-path profile-image
:name name
:address (or address
(:address contact)
(ethereum/public-key->address public-key))
:last-updated timestamp-ms
:system-tags (conj (get contact :system-tags #{})
:contact/request-received)})]
:contact/request-received)}
address (assoc :address address))]
(upsert-contact cofx contact-props)))))

(fx/defn initialize-contacts [cofx]
Expand Down
5 changes: 2 additions & 3 deletions src/status_im/contact/db.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
(spec/def :contact/tribute (spec/nilable int?))
(spec/def :contact/tribute-transaction (spec/nilable string?))

(spec/def :contact/contact (spec/keys :req-un [:contact/address
:contact/public-key
(spec/def :contact/contact (spec/keys :req-un [:contact/public-key
:contact/system-tags]
:opt-un [:contact/name
:contact/address
:contact/photo-path
:contact/last-online
:contact/last-updated
Expand Down Expand Up @@ -66,7 +66,6 @@
(let [alias (gfycat/generate-gfy public-key)]
{:alias alias
:name alias
:address (ethereum/public-key->address public-key)
:identicon (identicon/identicon public-key)
:public-key public-key
:system-tags #{}}))
Expand Down
3 changes: 3 additions & 0 deletions src/status_im/data_store/messages.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
(-> message
(clojure.set/rename-keys {:id :message-id
:whisperTimestamp :whisper-timestamp
:commandParameters :command-parameters
:messageType :message-type
:localChatId :chat-id
:contentType :content-type
Expand All @@ -31,6 +32,8 @@
:outgoingStatus :outgoing-status})

(update :outgoing-status keyword)
(update :command-parameters clojure.set/rename-keys {:transactionHash :transaction-hash
:commandState :command-state})
(assoc :content {:chat-id (:chatId message)
:text (:text message)
:sticker (:sticker message)
Expand Down
7 changes: 7 additions & 0 deletions src/status_im/ethereum/json_rpc.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@
"shhext_deleteChat" {}
"shhext_saveContact" {}
"shhext_verifyENSNames" {}
"shhext_requestAddressForTransaction" {}
"shhext_requestTransaction" {}
"shhext_acceptRequestAddressForTransaction" {}
"shhext_declineRequestAddressForTransaction" {}
"shhext_declineRequestTransaction" {}
"shhext_sendTransaction" {}
"shhext_acceptRequestTransaction" {}
"status_chats" {}
"wallet_getTransfers" {}
"wallet_getTokensBalances" {}
Expand Down
6 changes: 4 additions & 2 deletions src/status_im/node/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,17 @@
:BrowsersConfig {:Enabled true}
:PermissionsConfig {:Enabled true}
:MailserversConfig {:Enabled true}
:EnableNTPSync true
:WhisperConfig {:Enabled true
:LightClient true
:MinimumPoW 0.001
:EnableNTPSync true}
:MinimumPoW 0.001}
:ShhextConfig
{:BackupDisabledDataDir (utils.platform/no-backup-directory)
:InstallationID installation-id
:MaxMessageDeliveryAttempts config/max-message-delivery-attempts
:MailServerConfirmations config/mailserver-confirmations-enabled?
:VerifyTransactionURL "https://mainnet.infura.io/v3/f315575765b14720b32382a61a89341a"
:VerifyTransactionChainID 1
:DataSyncEnabled true
:PFSEnabled true}
:RequireTopics (get-topics current-network)
Expand Down
Loading

0 comments on commit c4e8012

Please sign in to comment.