Start work on allowing mentions for all users in call

Max Brunsfeld created

Change summary

crates/channel/src/channel_store.rs               |  1 
crates/collab_ui/src/chat_panel/message_editor.rs | 37 ++++++++++++----
2 files changed, 28 insertions(+), 10 deletions(-)

Detailed changes

crates/channel/src/channel_store.rs 🔗

@@ -118,6 +118,7 @@ pub struct MembershipSortKey<'a> {
 pub enum ChannelEvent {
     ChannelCreated(ChannelId),
     ChannelRenamed(ChannelId),
+    ChannelParticipantsChanged(ChannelId),
 }
 
 impl EventEmitter<ChannelEvent> for ChannelStore {}

crates/collab_ui/src/chat_panel/message_editor.rs 🔗

@@ -1,7 +1,7 @@
 use anyhow::Result;
 use channel::{ChannelId, ChannelMembership, ChannelStore, MessageParams};
-use client::UserId;
-use collections::HashMap;
+use client::{User, UserId};
+use collections::{HashMap, HashSet};
 use editor::{AnchorRangeExt, CompletionProvider, Editor, EditorElement, EditorStyle};
 use fuzzy::StringMatchCandidate;
 use gpui::{
@@ -30,7 +30,7 @@ lazy_static! {
 pub struct MessageEditor {
     pub editor: View<Editor>,
     channel_store: Model<ChannelStore>,
-    users: HashMap<String, UserId>,
+    channel_members: HashMap<String, UserId>,
     mentions: Vec<UserId>,
     mentions_task: Option<Task<()>>,
     channel_id: Option<ChannelId>,
@@ -108,7 +108,7 @@ impl MessageEditor {
         Self {
             editor,
             channel_store,
-            users: HashMap::default(),
+            channel_members: HashMap::default(),
             channel_id: None,
             mentions: Vec::new(),
             mentions_task: None,
@@ -147,14 +147,22 @@ impl MessageEditor {
     }
 
     pub fn set_members(&mut self, members: Vec<ChannelMembership>, _: &mut ViewContext<Self>) {
-        self.users.clear();
-        self.users.extend(
+        self.channel_members.clear();
+        self.channel_members.extend(
             members
                 .into_iter()
                 .map(|member| (member.user.github_login.clone(), member.user.id)),
         );
     }
 
+    pub fn update_users(&mut self, users: &Vec<Arc<User>>, cx: &mut ViewContext<Self>) {
+        self.channel_members.extend(
+            users
+                .into_iter()
+                .map(|user| (user.github_login.clone(), user.id)),
+        );
+    }
+
     pub fn take_message(&mut self, cx: &mut ViewContext<Self>) -> MessageParams {
         self.editor.update(cx, |editor, cx| {
             let highlights = editor.text_highlights::<Self>(cx);
@@ -221,9 +229,18 @@ impl MessageEditor {
         let start_offset = end_offset - query.len();
         let start_anchor = buffer.read(cx).anchor_before(start_offset);
 
-        let candidates = self
-            .users
-            .keys()
+        let names = HashSet::new();
+        for (github_login, _) in self.channel_members {
+            names.insert(github_login.clone());
+        }
+        if let Some(channel_id) = channel_id {
+            for participant in self.channel_store.read(cx).channel_participants(channel_id) {
+                names.insert(participant.github_login.clone());
+            }
+        }
+
+        let candidates = names
+            .into_iter()
             .map(|user| StringMatchCandidate {
                 id: 0,
                 string: user.clone(),
@@ -283,7 +300,7 @@ impl MessageEditor {
                     text.clear();
                     text.extend(buffer.text_for_range(range.clone()));
                     if let Some(username) = text.strip_prefix("@") {
-                        if let Some(user_id) = this.users.get(username) {
+                        if let Some(user_id) = this.channel_members.get(username) {
                             let start = multi_buffer.anchor_after(range.start);
                             let end = multi_buffer.anchor_after(range.end);