[#11212] Mention suggestions based on names parts

This commit is contained in:
Roman Volosovskyi 2020-09-30 14:02:44 +03:00
parent c866082cde
commit 6cf3a6a98e
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
6 changed files with 88 additions and 54 deletions

View File

@ -51,12 +51,14 @@
;; NOTE(roman): on-text-input event is not dispatched when we change input ;; NOTE(roman): on-text-input event is not dispatched when we change input
;; programmatically, so we have to call `on-text-input` manually ;; programmatically, so we have to call `on-text-input` manually
(mentions/on-text-input (mentions/on-text-input
(let [match-len (count match) (let [match-len (count match)
searched-text-len (count searched-text)] searched-text-len (count searched-text)
{:new-text (subs match searched-text-len) start (inc at-sign-idx)
:previous-text "" end (+ start match-len)]
:start (+ at-sign-idx searched-text-len 1) {:new-text match
:end (+ at-sign-idx match-len 1)})) :previous-text searched-text
:start start
:end end}))
(mentions/recheck-at-idxs {alias user})))) (mentions/recheck-at-idxs {alias user}))))
(defn- start-cooldown [{:keys [db]} cooldowns] (defn- start-cooldown [{:keys [db]} cooldowns]

View File

@ -10,7 +10,8 @@
[status-im.chat.models.reactions :as reactions] [status-im.chat.models.reactions :as reactions]
[status-im.chat.models.message-list :as message-list] [status-im.chat.models.message-list :as message-list]
[taoensso.timbre :as log] [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 (defn cursor->clock-value
[^js cursor] [^js cursor]
@ -95,11 +96,13 @@
(let [nickname (get-in db [:contacts/contacts from :nickname])] (let [nickname (get-in db [:contacts/contacts from :nickname])]
(cond-> acc (cond-> acc
(and alias (not= alias "")) (and alias (not= alias ""))
(update :users assoc from {:alias alias (update :users assoc from
:name (or name alias) (mentions/add-searchable-phrases
:identicon identicon {:alias alias
:public-key from :name (or name alias)
:nickname nickname}) :identicon identicon
:public-key from
:nickname nickname}))
(or (nil? last-clock-value) (or (nil? last-clock-value)
(> last-clock-value clock-value)) (> last-clock-value clock-value))
(assoc :last-clock-value clock-value) (assoc :last-clock-value clock-value)

View File

@ -76,25 +76,33 @@
(defn get-suggestions [users searched-text] (defn get-suggestions [users searched-text]
(reduce (reduce
(fn [acc [k {:keys [alias name nickname] :as user}]] (fn [acc [k {:keys [alias name nickname searchable-phrases] :as user}]]
(if-let [match (if-let [match
(cond (if (seq searchable-phrases)
(and nickname (when (some
(string/starts-with? (fn [s]
(string/lower-case nickname) (string/starts-with?
searched-text)) (string/lower-case s)
(or alias name) searched-text))
searchable-phrases)
(or name alias))
(cond
(and nickname
(string/starts-with?
(string/lower-case nickname)
searched-text))
(or name alias)
(and alias (and alias
(string/starts-with? (string/starts-with?
(string/lower-case alias) (string/lower-case alias)
searched-text)) searched-text))
alias alias
(string/starts-with? (string/starts-with?
(string/lower-case name) (string/lower-case name)
searched-text) searched-text)
name)] name))]
(assoc acc k (assoc user (assoc acc k (assoc user
:match match :match match
:searched-text searched-text)) :searched-text searched-text))
@ -465,3 +473,17 @@
(fx/defn reset-text-input-cursor (fx/defn reset-text-input-cursor
[_ ref cursor] [_ ref cursor]
{::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]))

View File

@ -10,7 +10,8 @@
[status-im.transport.message.protocol :as protocol] [status-im.transport.message.protocol :as protocol]
[status-im.ui.screens.chat.state :as view.state] [status-im.ui.screens.chat.state :as view.state]
[status-im.utils.fx :as fx] [status-im.utils.fx :as fx]
[taoensso.timbre :as log])) [taoensso.timbre :as log]
[status-im.chat.models.mentions :as mentions]))
(defn- prepare-message (defn- prepare-message
[message current-chat?] [message current-chat?]
@ -67,11 +68,12 @@
(let [nickname (get-in db [:contacts/contacts from :nickname])] (let [nickname (get-in db [:contacts/contacts from :nickname])]
{:db (update-in db [:chats chat-id :users] assoc {:db (update-in db [:chats chat-id :users] assoc
from from
{:alias alias (mentions/add-searchable-phrases
:name (or name alias) {:alias alias
:identicon identicon :name (or name alias)
:public-key from :identicon identicon
:nickname nickname})}))) :public-key from
:nickname nickname}))})))
(fx/defn add-received-message (fx/defn add-received-message
[{:keys [db] :as cofx} [{:keys [db] :as cofx}

View File

@ -45,11 +45,12 @@
"00000000000000000000000000000000000000000000000000090x0000000000000000000000000000000000000000000000000000000000000000", "00000000000000000000000000000000000000000000000000090x0000000000000000000000000000000000000000000000000000000000000000",
:cursor-clock-value 9, :cursor-clock-value 9,
:users :users
{"from" {:alias "alias", {"from" {:alias "alias",
:name "name", :name "name",
:identicon "identicon", :identicon "identicon",
:public-key "from" :public-key "from"
:nickname nil}}}}}} :nickname nil
:searchable-phrases ["alias" "name"]}}}}}}
(message/add-received-message (message/add-received-message
cofx cofx
message))))) message)))))
@ -68,11 +69,12 @@
"00000000000000000000000000000000000000000000000000090x0000000000000000000000000000000000000000000000000000000000000000", "00000000000000000000000000000000000000000000000000090x0000000000000000000000000000000000000000000000000000000000000000",
:cursor-clock-value 9, :cursor-clock-value 9,
:users :users
{"from" {:alias "alias", {"from" {:alias "alias",
:name "name", :name "name",
:identicon "identicon", :identicon "identicon",
:public-key "from" :public-key "from"
:nickname nil}}}}}} :nickname nil
:searchable-phrases ["alias" "name"]}}}}}}
(message/add-received-message (message/add-received-message
cofx cofx
message))))) message)))))

View File

@ -42,7 +42,8 @@
status-im.ui.screens.keycard.subs status-im.ui.screens.keycard.subs
status-im.ui.screens.keycard.settings.subs status-im.ui.screens.keycard.settings.subs
status-im.ui.screens.keycard.pin.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 =========================================================================================================== ;; TOP LEVEL ===========================================================================================================
@ -890,11 +891,12 @@
(not (contact.db/blocked? contact))) (not (contact.db/blocked? contact)))
(let [name (utils/safe-replace name ".stateofus.eth" "")] (let [name (utils/safe-replace name ".stateofus.eth" "")]
(assoc acc public-key (assoc acc public-key
{:alias alias (mentions/add-searchable-phrases
:name (or name alias) {:alias alias
:identicon identicon :name (or name alias)
:nickname nickname :identicon identicon
:public-key key})) :nickname nickname
:public-key key})))
acc)) acc))
{} {}
contacts))) contacts)))
@ -909,10 +911,11 @@
(apply dissoc (apply dissoc
(-> users (-> users
(merge contacts) (merge contacts)
(assoc public-key {:alias name (assoc public-key (mentions/add-searchable-phrases
:name (or preferred-name name) {:alias name
:identicon photo-path :name (or preferred-name name)
:public-key public-key})) :identicon photo-path
:public-key public-key})))
blocked))) blocked)))
(re-frame/reg-sub (re-frame/reg-sub