From 6cf3a6a98e5fff01551c975096323cb23799efcd Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Wed, 30 Sep 2020 14:02:44 +0300 Subject: [PATCH] [#11212] Mention suggestions based on names parts --- src/status_im/chat/models/input.cljs | 14 +++--- src/status_im/chat/models/loading.cljs | 15 +++--- src/status_im/chat/models/mentions.cljs | 54 +++++++++++++++------ src/status_im/chat/models/message.cljs | 14 +++--- src/status_im/chat/models/message_test.cljs | 22 +++++---- src/status_im/subs.cljs | 23 +++++---- 6 files changed, 88 insertions(+), 54 deletions(-) diff --git a/src/status_im/chat/models/input.cljs b/src/status_im/chat/models/input.cljs index 07486e57b4..f5c81c856f 100644 --- a/src/status_im/chat/models/input.cljs +++ b/src/status_im/chat/models/input.cljs @@ -51,12 +51,14 @@ ;; NOTE(roman): on-text-input event is not dispatched when we change input ;; programmatically, so we have to call `on-text-input` manually (mentions/on-text-input - (let [match-len (count match) - searched-text-len (count searched-text)] - {:new-text (subs match searched-text-len) - :previous-text "" - :start (+ at-sign-idx searched-text-len 1) - :end (+ at-sign-idx match-len 1)})) + (let [match-len (count match) + searched-text-len (count searched-text) + start (inc at-sign-idx) + end (+ start match-len)] + {:new-text match + :previous-text searched-text + :start start + :end end})) (mentions/recheck-at-idxs {alias user})))) (defn- start-cooldown [{:keys [db]} cooldowns] diff --git a/src/status_im/chat/models/loading.cljs b/src/status_im/chat/models/loading.cljs index b831acd406..55d196339c 100644 --- a/src/status_im/chat/models/loading.cljs +++ b/src/status_im/chat/models/loading.cljs @@ -10,7 +10,8 @@ [status-im.chat.models.reactions :as reactions] [status-im.chat.models.message-list :as message-list] [taoensso.timbre :as log] - [status-im.chat.models.message-seen :as message-seen])) + [status-im.chat.models.message-seen :as message-seen] + [status-im.chat.models.mentions :as mentions])) (defn cursor->clock-value [^js cursor] @@ -95,11 +96,13 @@ (let [nickname (get-in db [:contacts/contacts from :nickname])] (cond-> acc (and alias (not= alias "")) - (update :users assoc from {:alias alias - :name (or name alias) - :identicon identicon - :public-key from - :nickname nickname}) + (update :users assoc from + (mentions/add-searchable-phrases + {:alias alias + :name (or name alias) + :identicon identicon + :public-key from + :nickname nickname})) (or (nil? last-clock-value) (> last-clock-value clock-value)) (assoc :last-clock-value clock-value) diff --git a/src/status_im/chat/models/mentions.cljs b/src/status_im/chat/models/mentions.cljs index d59df27dd3..1a52ed8b7c 100644 --- a/src/status_im/chat/models/mentions.cljs +++ b/src/status_im/chat/models/mentions.cljs @@ -76,25 +76,33 @@ (defn get-suggestions [users searched-text] (reduce - (fn [acc [k {:keys [alias name nickname] :as user}]] + (fn [acc [k {:keys [alias name nickname searchable-phrases] :as user}]] (if-let [match - (cond - (and nickname - (string/starts-with? - (string/lower-case nickname) - searched-text)) - (or alias name) + (if (seq searchable-phrases) + (when (some + (fn [s] + (string/starts-with? + (string/lower-case s) + searched-text)) + searchable-phrases) + (or name alias)) + (cond + (and nickname + (string/starts-with? + (string/lower-case nickname) + searched-text)) + (or name alias) - (and alias - (string/starts-with? - (string/lower-case alias) - searched-text)) - alias + (and alias + (string/starts-with? + (string/lower-case alias) + searched-text)) + alias - (string/starts-with? - (string/lower-case name) - searched-text) - name)] + (string/starts-with? + (string/lower-case name) + searched-text) + name))] (assoc acc k (assoc user :match match :searched-text searched-text)) @@ -465,3 +473,17 @@ (fx/defn reset-text-input-cursor [_ ref cursor] {::reset-text-input-cursor [ref cursor]}) + +(defn add-searchable-phrases + [{:keys [alias name nickname] :as user}] + (reduce + (fn [user s] + (if (nil? s) + user + (let [new-words (concat + [s] + (rest (string/split s " ")))] + (update user :searchable-phrases (fnil concat []) new-words)))) + user + [alias name nickname])) + diff --git a/src/status_im/chat/models/message.cljs b/src/status_im/chat/models/message.cljs index 16f6676e22..a2cb5795d0 100644 --- a/src/status_im/chat/models/message.cljs +++ b/src/status_im/chat/models/message.cljs @@ -10,7 +10,8 @@ [status-im.transport.message.protocol :as protocol] [status-im.ui.screens.chat.state :as view.state] [status-im.utils.fx :as fx] - [taoensso.timbre :as log])) + [taoensso.timbre :as log] + [status-im.chat.models.mentions :as mentions])) (defn- prepare-message [message current-chat?] @@ -67,11 +68,12 @@ (let [nickname (get-in db [:contacts/contacts from :nickname])] {:db (update-in db [:chats chat-id :users] assoc from - {:alias alias - :name (or name alias) - :identicon identicon - :public-key from - :nickname nickname})}))) + (mentions/add-searchable-phrases + {:alias alias + :name (or name alias) + :identicon identicon + :public-key from + :nickname nickname}))}))) (fx/defn add-received-message [{:keys [db] :as cofx} diff --git a/src/status_im/chat/models/message_test.cljs b/src/status_im/chat/models/message_test.cljs index a48047facc..d9ab26dcd8 100644 --- a/src/status_im/chat/models/message_test.cljs +++ b/src/status_im/chat/models/message_test.cljs @@ -45,11 +45,12 @@ "00000000000000000000000000000000000000000000000000090x0000000000000000000000000000000000000000000000000000000000000000", :cursor-clock-value 9, :users - {"from" {:alias "alias", - :name "name", - :identicon "identicon", - :public-key "from" - :nickname nil}}}}}} + {"from" {:alias "alias", + :name "name", + :identicon "identicon", + :public-key "from" + :nickname nil + :searchable-phrases ["alias" "name"]}}}}}} (message/add-received-message cofx message))))) @@ -68,11 +69,12 @@ "00000000000000000000000000000000000000000000000000090x0000000000000000000000000000000000000000000000000000000000000000", :cursor-clock-value 9, :users - {"from" {:alias "alias", - :name "name", - :identicon "identicon", - :public-key "from" - :nickname nil}}}}}} + {"from" {:alias "alias", + :name "name", + :identicon "identicon", + :public-key "from" + :nickname nil + :searchable-phrases ["alias" "name"]}}}}}} (message/add-received-message cofx message))))) diff --git a/src/status_im/subs.cljs b/src/status_im/subs.cljs index ffdc8e17c0..ce6c5ea961 100644 --- a/src/status_im/subs.cljs +++ b/src/status_im/subs.cljs @@ -42,7 +42,8 @@ status-im.ui.screens.keycard.subs status-im.ui.screens.keycard.settings.subs status-im.ui.screens.keycard.pin.subs - status-im.ui.screens.keycard.setup.subs)) + status-im.ui.screens.keycard.setup.subs + [status-im.chat.models.mentions :as mentions])) ;; TOP LEVEL =========================================================================================================== @@ -890,11 +891,12 @@ (not (contact.db/blocked? contact))) (let [name (utils/safe-replace name ".stateofus.eth" "")] (assoc acc public-key - {:alias alias - :name (or name alias) - :identicon identicon - :nickname nickname - :public-key key})) + (mentions/add-searchable-phrases + {:alias alias + :name (or name alias) + :identicon identicon + :nickname nickname + :public-key key}))) acc)) {} contacts))) @@ -909,10 +911,11 @@ (apply dissoc (-> users (merge contacts) - (assoc public-key {:alias name - :name (or preferred-name name) - :identicon photo-path - :public-key public-key})) + (assoc public-key (mentions/add-searchable-phrases + {:alias name + :name (or preferred-name name) + :identicon photo-path + :public-key public-key}))) blocked))) (re-frame/reg-sub