status-mobile/src/status_im/models/mailserver.cljs

130 lines
5.0 KiB
Plaintext
Raw Normal View History

(ns status-im.models.mailserver
(:require
[clojure.string :as string]
[status-im.utils.handlers-macro :as handlers-macro]
[status-im.utils.ethereum.core :as ethereum]
[status-im.models.network :as models.network]
[status-im.data-store.mailservers :as data-store.mailservers]))
(def enode-address-regex #"enode://[a-zA-Z0-9]+\@\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b:(\d{1,5})")
(def enode-url-regex #"enode://[a-zA-Z0-9]+:(.+)\@\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b:(\d{1,5})")
(defn- extract-address-components [address]
(rest (re-matches #"enode://(.*)@(.*)" address)))
(defn- extract-url-components [address]
(rest (re-matches #"enode://(.*?):(.*)@(.*)" address)))
(defn valid-enode-url? [address]
(re-matches enode-url-regex address))
(defn valid-enode-address? [address]
(re-matches enode-address-regex address))
(defn build-url [address password]
(let [[initial host] (extract-address-components address)]
(str "enode://" initial ":" password "@" host)))
(defn set-input [input-key value {:keys [db]}]
{:db (update
db
:mailservers/manage
assoc
input-key
{:value value
:error (case input-key
:id false
:name (string/blank? value)
:url (not (valid-enode-url? value)))})})
(defn- address->mailserver [address]
(let [[enode password url :as response] (extract-url-components address)]
(cond-> {:address (if (seq response)
(str "enode://" enode "@" url)
address)
:user-defined true}
password (assoc :password password))))
(defn- build [id mailserver-name address]
(assoc (address->mailserver address)
:id (string/replace id "-" "")
:name mailserver-name))
(defn connected? [id {:keys [db]}]
(= (:inbox/current-id db) id))
(defn fetch [id {:keys [db] :as cofx}]
(get-in db [:inbox/wnodes (models.network/get-chain cofx) id]))
(defn fetch-current [{:keys [db] :as cofx}]
(fetch (:inbox/current-id db) cofx))
(defn selected-or-random-id
"Use the preferred mailserver if set & exists, otherwise picks one randomly"
[{:keys [db] :as cofx}]
(let [chain (models.network/get-chain cofx)
preference (get-in db [:account/account :settings :wnode chain])]
(if (and preference
(fetch preference cofx))
preference
(-> db :inbox/wnodes chain keys rand-nth))))
(def default? (comp not :user-defined fetch))
(defn delete [id {:keys [db] :as cofx}]
(when-not (or
(default? id cofx)
(connected? id cofx))
{:db (update-in db [:inbox/wnodes (models.network/get-chain cofx)] dissoc id)
:data-store/tx [(data-store.mailservers/delete-tx id)]}))
(defn set-current-mailserver [{:keys [db] :as cofx}]
{:db (assoc db :inbox/current-id (selected-or-random-id cofx))})
[fixes #4745] Don't query mailserver on restored account When creating a new account / recovery we don't poll the mailserver anymore for historic messages, which solves the immediate issue of fetching only received messages Handle messages sent from a different device in public chat / restore history. The message will be added, shown correctly as sent by the user, and the status will be set as sent ( need to check for seen race condition, as messages will now be added twice). This means that multidevice should now work for public chats. Move contact updates to discovery topic. This is necessary as there is a pre-existing bug whereby contact updates would not work anymore after wallet recovery, as the code relies on the initial contact request being stored on the mailserver, which we cannot guarantee (we only pull 7 days of data). Not pulling history anymore exacerbate the problems but does not introduce it. To make sure that contact updates will work after wallet recovery, we also need to consider a ContactUpdate in the same way we consider a ContactRequest (the other peer has no idea that the user has recovered the wallet). This does not change any behaviour in terms of obscurity/security as ContactRequest are automatically processed (in both case the contact will be set as pending?, not as accepted) At this stage ContactRequest, ContactRequestConfirmed, ContactUpdate have all the same logic, i.e. update the contact information, leave the pending flag alone. Only 1 day of history is fetched for newly joined chats, if catching up 7 days is the cap as before. Signed-off-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2018-06-19 13:43:47 +02:00
(defn set-initial-last-request [{:keys [db now] :as cofx}]
{:db (update-in db [:account/account :last-request] (fnil identity (quot now 1000)))})
(defn add-custom-mailservers [mailservers {:keys [db]}]
{:db (reduce (fn [db {:keys [id chain] :as mailserver}]
(assoc-in db [:inbox/wnodes (keyword chain) id]
(-> mailserver
(dissoc :chain)
(assoc :user-defined true))))
db
mailservers)})
(defn edit [id {:keys [db] :as cofx}]
(let [{:keys [id
address
password
name]} (fetch id cofx)
url (when address (build-url address password))
fxs (handlers-macro/merge-fx
cofx
(set-input :id id)
(set-input :url (str url))
(set-input :name (str name)))]
(assoc fxs :dispatch [:navigate-to :edit-mailserver])))
(defn upsert [{{:mailservers/keys [manage] :account/keys [account] :as db} :db :as cofx}]
(let [{:keys [name url id]} manage
network (get (:networks (:account/account db)) (:network db))
chain (ethereum/network->chain-keyword network)
mailserver (build
(or (:value id)
(string/replace (:random-id cofx) "-" ""))
(:value name)
(:value url))
current (connected? (:id mailserver) cofx)]
{:db (-> db
(dissoc :mailservers/manage)
(assoc-in [:inbox/wnodes chain (:id mailserver)] mailserver))
:data-store/tx [{:transaction
(data-store.mailservers/save-tx (assoc
mailserver
:chain
chain))
;; we naively logout if the user is connected to the edited mailserver
:success-event (when current [:logout])}]
:dispatch [:navigate-back]}))