hide mailman contact

inject mailman's commands

ensure that mailman's commands are injected after loading
prevent loading of default contacts at account creation
This commit is contained in:
Roman Volosovskyi 2017-05-02 17:39:02 +03:00 committed by Roman Volosovskyi
parent 1eaea101dd
commit 3607032a4e
16 changed files with 166 additions and 85 deletions

View File

@ -5,6 +5,12 @@ status.command({
description: I18n.t('location_description'), description: I18n.t('location_description'),
color: "#a187d5", color: "#a187d5",
sequentialParams: true, sequentialParams: true,
registeredOnly: true,
params: [{
name: "address",
type: status.types.TEXT,
placeholder: I18n.t('location_address')
}],
preview: function (params) { preview: function (params) {
var text = status.components.text( var text = status.components.text(
{ {
@ -42,8 +48,4 @@ status.command({
) )
}; };
} }
}).param({
name: "address",
type: status.types.TEXT,
placeholder: I18n.t('location_address')
}); });

View File

@ -0,0 +1,7 @@
(ns status-im.bots.constants)
(def mailman-bot "mailman")
(defn mailman-bot? [bot-name]
(= mailman-bot bot-name))
(def hidden-bots #{mailman-bot})

View File

@ -122,7 +122,7 @@
:params :params
parameter-index parameter-index
:suggestions] :suggestions]
args (-> (get-in db [:chats jail-id :input-text]) args (-> (get-in db [:chats current-chat-id :input-text])
(input-model/split-command-args) (input-model/split-command-args)
(rest)) (rest))
params {:parameters {:args args} params {:parameters {:args args}
@ -221,6 +221,7 @@
(dispatch [::request-command-data (dispatch [::request-command-data
{:command command {:command command
:chat-id chat-id :chat-id chat-id
:jail-id (get-in command [:command :bot])
:data-type :preview :data-type :preview
:after #(dispatch [::send-message % chat-id])}]))))) :after #(dispatch [::send-message % chat-id])}])))))

View File

@ -31,7 +31,8 @@
:timestamp (time/now-ms) :timestamp (time/now-ms)
:content (assoc content :handler-data handler-data :content (assoc content :handler-data handler-data
:type (name (:type command)) :type (name (:type command))
:content-command (:name command)) :content-command (:name command)
:bot (:bot command))
:content-type (or content-type :content-type (or content-type
(if request (if request
content-type-command-request content-type-command-request

View File

@ -6,7 +6,8 @@
[status-im.i18n :as i18n] [status-im.i18n :as i18n]
[status-im.utils.phone-number :as phone-number] [status-im.utils.phone-number :as phone-number]
[taoensso.timbre :as log] [taoensso.timbre :as log]
[status-im.chat.utils :as chat-utils])) [status-im.chat.utils :as chat-utils]
[status-im.bots.constants :as bots-constants]))
(defn text-ends-with-space? [text] (defn text-ends-with-space? [text]
(when text (when text
@ -63,8 +64,10 @@
command-args (split-command-args input-text) command-args (split-command-args input-text)
command-name (first command-args)] command-name (first command-args)]
(when (chat-utils/starts-as-command? (or command-name "")) (when (chat-utils/starts-as-command? (or command-name ""))
(when-let [[command to-message-id] (-> (filter (fn [[{:keys [name bot]} message-id]] (when-let [[command to-message-id]
(= (or bot name) (subs command-name 1))) (-> (filter (fn [[{:keys [name bot]} message-id]]
(= (or (when-not (bots-constants/mailman-bot? bot) bot) name)
(subs command-name 1)))
possible-actions) possible-actions)
(first))] (first))]
{:command command {:command command

View File

@ -2,7 +2,8 @@
(:require [status-im.constants :refer [console-chat-id (:require [status-im.constants :refer [console-chat-id
wallet-chat-id]] wallet-chat-id]]
[clojure.string :as str] [clojure.string :as str]
[status-im.chat.constants :as const])) [status-im.chat.constants :as const]
[status-im.bots.constants :as bots-constants]))
(defn console? [s] (defn console? [s]
(= console-chat-id s)) (= console-chat-id s))
@ -48,9 +49,13 @@
(pos? (count message)))) (pos? (count message))))
(defn command-name [{:keys [bot name]}] (defn command-name [{:keys [bot name]}]
(if bot (cond
(str const/bot-char bot) (bots-constants/mailman-bot? bot)
(str const/command-char name))) (str const/command-char name)
bot (str const/bot-char bot)
:else (str const/command-char name)))
(defn starts-as-command? [text] (defn starts-as-command? [text]
(and (not (nil? text)) (and (not (nil? text))

View File

@ -127,10 +127,12 @@
:get-responses :get-responses
:get-commands) :get-commands)
chat-id] chat-id]
global-commands [:get :global-commands]
current-chat-id [:get-current-chat-id] current-chat-id [:get-current-chat-id]
contact-chat [:get-in [:chats (if outgoing to from)]] contact-chat [:get-in [:chats (if outgoing to from)]]
preview [:get-in [:message-data :preview message-id :markup]]] preview [:get-in [:message-data :preview message-id :markup]]]
(let [{:keys [command params]} (parse-command-message-content commands content) (let [{:keys [command params]}
(parse-command-message-content commands global-commands content)
{:keys [name type] {:keys [name type]
icon-path :icon} command] icon-path :icon} command]
[view st/content-command-view [view st/content-command-view
@ -376,9 +378,10 @@
(r/create-class (r/create-class
{:component-will-mount {:component-will-mount
(fn [] (fn []
(when (and (get-in message [:content :command]) (let [{:keys [bot] :as command} (get-in message [:content])
(not @preview)) message' (assoc message :jail-id bot)]
(dispatch [:request-command-data message :preview]))) (when (and command (not @preview))
(dispatch [:request-command-data message' :preview]))))
:component-did-mount :component-did-mount
(fn [] (fn []

View File

@ -5,7 +5,6 @@
[status-im.components.react :refer [view image icon text]] [status-im.components.react :refer [view image icon text]]
[status-im.components.chat-icon.screen :refer [chat-icon-view-chat-list]] [status-im.components.chat-icon.screen :refer [chat-icon-view-chat-list]]
[status-im.components.context-menu :refer [context-menu]] [status-im.components.context-menu :refer [context-menu]]
[status-im.models.commands :refer [parse-command-message-content]]
[status-im.chats-list.styles :as st] [status-im.chats-list.styles :as st]
[status-im.utils.utils :refer [truncate-str]] [status-im.utils.utils :refer [truncate-str]]
[status-im.i18n :refer [get-contact-translated label label-pluralize]] [status-im.i18n :refer [get-contact-translated label label-pluralize]]

View File

@ -14,22 +14,27 @@
[status-im.utils.homoglyph :as h] [status-im.utils.homoglyph :as h]
[status-im.utils.js-resources :as js-res] [status-im.utils.js-resources :as js-res]
[status-im.utils.random :as random] [status-im.utils.random :as random]
[status-im.chat.sign-up :as sign-up])) [status-im.chat.sign-up :as sign-up]
[status-im.bots.constants :as bots-constants]))
(def commands-js "commands.js") (def commands-js "commands.js")
(defn load-commands! (defn load-commands!
[{:keys [current-chat-id contacts]} [identity callback]] [{:keys [current-chat-id contacts]} [contact callback]]
(let [identity' (or identity current-chat-id) (let [whole-contact? (map? contact)
contact (or (get contacts identity') {:keys [whisper-identity]} contact
sign-up/console-contact)] identity' (or whisper-identity contact current-chat-id)
contact' (if whole-contact?
contact
(or (get contacts identity')
sign-up/console-contact))]
(when identity' (when identity'
(dispatch [::fetch-commands! {:contact contact (dispatch [::fetch-commands! {:contact contact'
:callback callback}]))) :callback callback}])))
;; todo uncomment ;; todo uncomment
#_(if-let [{:keys [file]} (commands/get-by-chat-id identity)] #_(if-let [{:keys [file]} (commands/get-by-chat-id contact)]
(dispatch [::parse-commands! identity file]) (dispatch [::parse-commands! contact file])
(dispatch [::fetch-commands! identity]))) (dispatch [::fetch-commands! contact])))
(defn http-get-commands [params url] (defn http-get-commands [params url]
@ -103,8 +108,9 @@
(defn filter-forbidden-names [account id commands] (defn filter-forbidden-names [account id commands]
(->> commands (->> commands
(remove (fn [[_ {:keys [registered-only]}]] (remove (fn [[_ {:keys [registered-only name]}]]
(and (not (:address account)) (and (not (:address account))
(not= name "global")
registered-only))) registered-only)))
(remove (fn [[n]] (remove (fn [[n]]
(and (and
@ -112,37 +118,63 @@
(h/matches (name n) "password")))) (h/matches (name n) "password"))))
(into {}))) (into {})))
(defn get-mailmans-commands [db]
(->> (get-in db [:contacts bots-constants/mailman-bot :commands])
(map
(fn [[k v :as com]]
[k (-> v
(update :params (fn [p]
(if (map? p)
((comp vec vals) p)
p)))
(assoc :bot bots-constants/mailman-bot
:type :command))]))
(into {})))
(defn add-commands (defn add-commands
[db [id _ {:keys [commands responses subscriptions]}]] [db [id _ {:keys [commands responses subscriptions]}]]
(let [account @(subscribe [:get-current-account]) (let [account @(subscribe [:get-current-account])
commands' (filter-forbidden-names account id commands) commands' (filter-forbidden-names account id commands)
global-command (:global commands') global-command (:global commands')
commands'' (apply dissoc commands' [:init :global]) commands'' (apply dissoc commands' [:init :global])
responses' (filter-forbidden-names account id responses)] responses' (filter-forbidden-names account id responses)
mailman-commands (get-mailmans-commands db)]
(cond-> db (cond-> db
true true
(update-in [:contacts id] assoc (update-in [:contacts id] assoc
:commands-loaded true :commands-loaded true
:commands (mark-as :command commands'') :commands (mark-as :command (merge mailman-commands commands''))
:responses (mark-as :response responses') :responses (mark-as :response responses')
:subscriptions subscriptions) :subscriptions subscriptions)
global-command global-command
(update :global-commands assoc (keyword id) (update :global-commands assoc (keyword id)
(assoc global-command :bot id (assoc global-command :bot id
:type :command))))) :type :command))
(= id bots-constants/mailman-bot)
(update db :contacts (fn [contacts]
(reduce (fn [contacts [k _]]
(update-in contacts [k :commands]
(fn [c]
(merge mailman-commands c))))
contacts
contacts))))))
(defn save-commands-js! (defn save-commands-js!
[_ [id file]] [_ [id file]]
#_(commands/save {:chat-id id :file file})) #_(commands/save {:chat-id id :file file}))
(defn save-global-command! (defn save-commands!
[{:keys [global-commands]} [id]] [{:keys [global-commands contacts]} [id]]
(let [command (get global-commands (keyword id))] (let [command (get global-commands (keyword id))
(when command commands (get-in contacts [id :commands])
responses (get-in contacts [id :commands])]
(contacts/save {:whisper-identity id (contacts/save {:whisper-identity id
:global-command command})))) :global-command command
:commands (vals commands)
:responses (vals responses)})))
(defn loading-failed! (defn loading-failed!
[db [id reason details]] [db [id reason details]]
@ -173,7 +205,7 @@
(reg-handler ::add-commands (reg-handler ::add-commands
[(after save-commands-js!) [(after save-commands-js!)
(after save-global-command!) (after save-commands!)
(after #(dispatch [:check-and-open-dapp!])) (after #(dispatch [:check-and-open-dapp!]))
(after #(dispatch [:update-suggestions])) (after #(dispatch [:update-suggestions]))
(after (fn [_ [id]] (after (fn [_ [id]]

View File

@ -223,7 +223,7 @@
(register-handler :load-default-contacts! (register-handler :load-default-contacts!
(u/side-effect! (u/side-effect!
(fn [{:keys [chats groups]}] (fn [{:keys [contacts groups]}]
(let [default-contacts js-res/default-contacts (let [default-contacts js-res/default-contacts
default-groups js-res/default-contact-groups] default-groups js-res/default-contact-groups]
(dispatch [:add-groups (mapv (dispatch [:add-groups (mapv
@ -237,10 +237,11 @@
(doseq [[id {:keys [name photo-path public-key add-chat? has-global-command? (doseq [[id {:keys [name photo-path public-key add-chat? has-global-command?
dapp? dapp-url dapp-hash bot-url]}] default-contacts] dapp? dapp-url dapp-hash bot-url]}] default-contacts]
(let [id' (clojure.core/name id)] (let [id' (clojure.core/name id)]
(when-not (chats id') (when-not (get contacts id')
(when add-chat? (when add-chat?
(dispatch [:add-chat id' {:name (:en name)}])) (dispatch [:add-chat id' {:name (:en name)}]))
(dispatch [:add-contacts [{:whisper-identity id' (let [contact
{:whisper-identity id'
:address (public-key->address id') :address (public-key->address id')
:name (:en name) :name (:en name)
:photo-path photo-path :photo-path photo-path
@ -249,9 +250,10 @@
:dapp-url (:en dapp-url) :dapp-url (:en dapp-url)
:bot-url bot-url :bot-url bot-url
:has-global-command? has-global-command? :has-global-command? has-global-command?
:dapp-hash dapp-hash}]]) :dapp-hash dapp-hash}]
(dispatch [:add-contacts [contact]])
(when bot-url (when bot-url
(dispatch [:load-commands! id']))))))))) (dispatch [:load-commands! contact]))))))))))
(register-handler :add-contacts (register-handler :add-contacts

View File

@ -2,7 +2,8 @@
(:require-macros [reagent.ratom :refer [reaction]]) (:require-macros [reagent.ratom :refer [reaction]])
(:require [re-frame.core :refer [register-sub subscribe]] (:require [re-frame.core :refer [register-sub subscribe]]
[status-im.utils.identicon :refer [identicon]] [status-im.utils.identicon :refer [identicon]]
[clojure.string :as str])) [clojure.string :as str]
[status-im.bots.constants :as bots-constants]))
(register-sub :current-contact (register-sub :current-contact
(fn [db [_ k]] (fn [db [_ k]]
@ -23,10 +24,13 @@
(clojure.string/lower-case name2)))) (clojure.string/lower-case name2))))
(vals contacts))) (vals contacts)))
(register-sub :all-added-contacts (register-sub :all-added-contacts
(fn [db _] (fn [db _]
(let [contacts (reaction (:contacts @db))] (let [contacts (reaction (:contacts @db))]
(->> (remove #(true? (:pending? (second %))) @contacts) (->> (remove (fn [[_ {:keys [pending? whisper-identity]}]]
(or (true? pending?)
(bots-constants/hidden-bots whisper-identity))) @contacts)
(sort-contacts) (sort-contacts)
(reaction))))) (reaction)))))

View File

@ -2,9 +2,18 @@
(:require [status-im.data-store.realm.contacts :as data-store]) (:require [status-im.data-store.realm.contacts :as data-store])
(:refer-clojure :exclude [exists?])) (:refer-clojure :exclude [exists?]))
(defn- command->map-item
[[_ {:keys [name] :as command}]]
[(keyword name) command])
(defn get-all (defn get-all
[] []
(data-store/get-all-as-list)) (map
(fn [{:keys [commands responses] :as contact}]
(assoc contact
:commands (into {} (map command->map-item commands))
:responses (into {} (map command->map-item responses))))
(data-store/get-all-as-list)))
(defn get-by-id (defn get-by-id
[whisper-identity] [whisper-identity]

View File

@ -7,6 +7,8 @@
:color {:type :string :color {:type :string
:optional true} :optional true}
:name {:type :string} :name {:type :string}
:icon {:type :string
:optional true}
:params {:type :list :params {:type :list
:objectType :command-parameter} :objectType :command-parameter}
:title {:type :string :title {:type :string
@ -16,7 +18,9 @@
:fullscreen {:type :bool :fullscreen {:type :bool
:default true} :default true}
:suggestions-trigger {:type :string :suggestions-trigger {:type :string
:default "on-change"}}}) :default "on-change"}
:sequential-params {:type :bool
:default false}}})
(defn migration [old-realm new-realm] (defn migration [old-realm new-realm]
(log/debug "migrating chat-contact schema v6")) (log/debug "migrating chat-contact schema v6"))

View File

@ -23,6 +23,10 @@
:optional true} :optional true}
:global-command {:type :command :global-command {:type :command
:optional true} :optional true}
:commands {:type :list
:objectType :command}
:responses {:type :list
:objectType :command}
:dapp-hash {:type :int :dapp-hash {:type :int
:optional true} :optional true}
:debug? {:type :bool :debug? {:type :bool

View File

@ -49,6 +49,7 @@
network-status network first-run]} _] network-status network first-run]} _]
(data-store/init) (data-store/init)
(assoc app-db :current-account-id nil (assoc app-db :current-account-id nil
:contacts {}
:network-status network-status :network-status network-status
:status-module-initialized? (or p/ios? js/goog.DEBUG status-module-initialized?) :status-module-initialized? (or p/ios? js/goog.DEBUG status-module-initialized?)
:status-node-started? status-node-started? :status-node-started? status-node-started?
@ -97,7 +98,6 @@
(dispatch [:set :first-run false])) (dispatch [:set :first-run false]))
(when (or (not first-run) (empty? accounts)) (when (or (not first-run) (empty? accounts))
(dispatch [:init-console-chat]) (dispatch [:init-console-chat])
(dispatch [:load-default-contacts!])
(dispatch [:load-commands!]) (dispatch [:load-commands!])
(when callback (callback)))))) (when callback (callback))))))

View File

@ -1,10 +1,15 @@
(ns status-im.models.commands (ns status-im.models.commands
(:require [status-im.db :as db] (:require [status-im.db :as db]
[tailrecursion.priority-map :refer [priority-map-by]])) [tailrecursion.priority-map :refer [priority-map-by]]
[status-im.bots.constants :as bots-constants]))
(defn parse-command-message-content [commands content] (defn parse-command-message-content
[commands global-commands content]
(if (map? content) (if (map? content)
(update content :command #((keyword %) commands)) (let [{:keys [command bot]} content]
(if (and bot (not (bots-constants/mailman-bot? bot)))
(update content :command #((keyword bot) global-commands))
(update content :command #((keyword command) commands))))
content)) content))
(defn parse-command-request [commands content] (defn parse-command-request [commands content]