From edbb06a7c355daf0715706a88fc99f1c2f0a93fa Mon Sep 17 00:00:00 2001 From: Parvesh Monu Date: Fri, 24 Dec 2021 20:25:00 +0530 Subject: [PATCH] Add community members to mentionable list (#12926) --- src/status_im/chat/models/mentions.cljs | 73 +++++++++++++++----- src/status_im/notifications_center/core.cljs | 17 ++--- src/status_im/subs.cljs | 35 ++++------ 3 files changed, 75 insertions(+), 50 deletions(-) diff --git a/src/status_im/chat/models/mentions.cljs b/src/status_im/chat/models/mentions.cljs index 5b56a78ffb..7b5b6fd676 100644 --- a/src/status_im/chat/models/mentions.cljs +++ b/src/status_im/chat/models/mentions.cljs @@ -9,7 +9,8 @@ [status-im.utils.utils :as utils] [status-im.native-module.core :as status] [quo.react-native :as rn] - [quo.react :as react])) + [quo.react :as react] + [status-im.multiaccounts.core :as multiaccounts])) (def at-sign "@") @@ -170,11 +171,55 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(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])) + +(defn add-searchable-phrases-to-contact + [{:keys [alias name added? blocked? identicon public-key nickname]} community-chat?] + (when (and alias + (not (string/blank? alias)) + (or name + nickname + added? + community-chat?) + (not blocked?)) + (add-searchable-phrases + {:alias alias + :name (or (utils/safe-replace name ".stateofus.eth" "") alias) + :identicon identicon + :nickname nickname + :public-key public-key}))) + +(defn mentionable-commmunity-members [contacts my-public-key community-members] + (reduce (fn [acc identity] + (let [contact (multiaccounts/contact-by-identity + contacts identity) + contact (if (string/blank? (:alias contact)) + (assoc contact :alias + (get-in contact [:names :three-words-name])) + contact) + mentionable-contact (add-searchable-phrases-to-contact + contact true)] + (if (nil? mentionable-contact) acc + (assoc acc identity mentionable-contact)))) + {} + (keys (dissoc community-members my-public-key)))) + (defn get-mentionable-users [{{:keys [current-chat-id] :contacts/keys [contacts] :as db} :db}] - (let [{:keys [chat-type users] :as chat} - (get-in db [:chats current-chat-id]) + (let [{:keys [chat-type community-id users] :as chat} (get-in db [:chats current-chat-id]) + {:keys [name preferred-name public-key]} (:multiaccount db) chat-specific-suggestions (cond (= chat-type constants/private-group-chat-type) @@ -200,9 +245,12 @@ current-chat-id (contact.db/public-key->contact contacts current-chat-id)) - :else users) - {:keys [name preferred-name public-key]} - (:multiaccount db)] + (= chat-type constants/community-chat-type) + (merge users (mentionable-commmunity-members + contacts public-key + (get-in db [:communities community-id :members]))) + + :else users)] (reduce (fn [acc [key {:keys [alias name identicon]}]] (let [name (utils/safe-replace name ".stateofus.eth" "")] @@ -634,19 +682,6 @@ [_ 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])) - (defn is-valid-terminating-character? [c] (case c "\t" true ; tab diff --git a/src/status_im/notifications_center/core.cljs b/src/status_im/notifications_center/core.cljs index 698c6ebbd1..5f059e4293 100644 --- a/src/status_im/notifications_center/core.cljs +++ b/src/status_im/notifications_center/core.cljs @@ -19,15 +19,16 @@ {:unread-count (get db :activity.center/notifications-count 0) :notifications (get-in db [:activity.center/notifications :notifications])} activities)] - {:db (-> db - (assoc-in [:activity.center/notifications :notifications] notifications) - (assoc :activity.center/notifications-count (max 0 unread-count))) - :dispatch (cond - (= (:view-id db) :notifications-center) - [:mark-all-activity-center-notifications-as-read] + (merge + {:db (-> db + (assoc-in [:activity.center/notifications :notifications] notifications) + (assoc :activity.center/notifications-count (max 0 unread-count)))} + (cond + (= (:view-id db) :notifications-center) + {:dispatch [:mark-all-activity-center-notifications-as-read]} - (= (:view-id db) :chat) - [:accept-all-activity-center-notifications-from-chat (:current-chat-id db)])})) + (= (:view-id db) :chat) + {:dispatch [:accept-all-activity-center-notifications-from-chat (:current-chat-id db)]})))) (fx/defn get-activity-center-notifications-count {:events [:get-activity-center-notifications-count]} diff --git a/src/status_im/subs.cljs b/src/status_im/subs.cljs index 1547465ba8..cfc83ff02e 100644 --- a/src/status_im/subs.cljs +++ b/src/status_im/subs.cljs @@ -34,7 +34,6 @@ [status-im.utils.security :as security] [status-im.wallet.db :as wallet.db] [status-im.wallet.utils :as wallet.utils] - [status-im.utils.utils :as utils] status-im.ui.screens.keycard.subs [status-im.chat.models.mentions :as mentions] [status-im.notifications.core :as notifications] @@ -1400,22 +1399,11 @@ :<- [:contacts/contacts] (fn [contacts] (reduce - (fn [acc [key {:keys [alias name added? blocked? identicon public-key nickname]}]] - (if (and alias - (not= alias "") - (or name - nickname - added?) - (not blocked?)) - (let [name (utils/safe-replace name ".stateofus.eth" "")] - (assoc acc public-key - (mentions/add-searchable-phrases - {:alias alias - :name (or name alias) - :identicon identicon - :nickname nickname - :public-key key}))) - acc)) + (fn [acc [key contact]] + (let [mentionable-contact (mentions/add-searchable-phrases-to-contact + contact false)] + (if (nil? mentionable-contact) acc + (assoc acc key mentionable-contact)))) {} contacts))) @@ -1425,7 +1413,9 @@ :<- [:chats/mentionable-contacts] :<- [:contacts/blocked-set] :<- [:multiaccount] - (fn [[{:keys [chat-id users contacts community-id chat-type] :as chat} mentionable-contacts blocked {:keys [name preferred-name public-key]}]] + :<- [:contacts/contacts] + (fn [[{:keys [chat-id users contacts community-id chat-type] :as chat} + mentionable-contacts blocked {:keys [public-key]} my-contacts]] (let [community-members @(re-frame/subscribe [:communities/community-members community-id]) contacts-with-one-to-one (if (= chat-type constants/one-to-one-chat-type) @@ -1436,14 +1426,13 @@ members-left (into #{} (filter #(group-chat/member-removed? chat %) (keys users))) filtered-contacts (select-keys contacts-with-one-to-one (if (nil? community-id) (distinct (concat (seq contacts) (keys users) [chat-id])) - (keys community-members)))] + (keys community-members))) + mentionable-commmunity-members (mentions/mentionable-commmunity-members + my-contacts public-key community-members)] (apply dissoc (-> users (merge filtered-contacts) - (assoc public-key (mentions/add-searchable-phrases - {:alias name - :name (or preferred-name name) - :public-key public-key}))) + (merge mentionable-commmunity-members)) (conj (concat blocked members-left) public-key))))) (re-frame/reg-sub