Acknowledge channel notes and chat changes when views are active

Co-authored-by: Mikayla <mikayla@zed.dev>
This commit is contained in:
Max Brunsfeld 2023-10-03 17:40:10 -07:00
parent af09861f5c
commit 61e0289014
19 changed files with 478 additions and 209 deletions

2
Cargo.lock generated
View file

@ -1217,6 +1217,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"client", "client",
"clock",
"collections", "collections",
"db", "db",
"feature_flags", "feature_flags",
@ -1530,6 +1531,7 @@ dependencies = [
"tracing-subscriber", "tracing-subscriber",
"unindent", "unindent",
"util", "util",
"uuid 1.4.1",
"workspace", "workspace",
] ]

View file

@ -2,14 +2,17 @@ use crate::Channel;
use anyhow::Result; use anyhow::Result;
use client::{Client, Collaborator, UserStore}; use client::{Client, Collaborator, UserStore};
use collections::HashMap; use collections::HashMap;
use gpui::{AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle}; use gpui::{AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, Task};
use language::proto::serialize_version;
use rpc::{ use rpc::{
proto::{self, PeerId}, proto::{self, PeerId},
TypedEnvelope, TypedEnvelope,
}; };
use std::sync::Arc; use std::{sync::Arc, time::Duration};
use util::ResultExt; use util::ResultExt;
const ACKNOWLEDGE_DEBOUNCE_INTERVAL: Duration = Duration::from_millis(250);
pub(crate) fn init(client: &Arc<Client>) { pub(crate) fn init(client: &Arc<Client>) {
client.add_model_message_handler(ChannelBuffer::handle_update_channel_buffer); client.add_model_message_handler(ChannelBuffer::handle_update_channel_buffer);
client.add_model_message_handler(ChannelBuffer::handle_update_channel_buffer_collaborators); client.add_model_message_handler(ChannelBuffer::handle_update_channel_buffer_collaborators);
@ -24,11 +27,13 @@ pub struct ChannelBuffer {
buffer_epoch: u64, buffer_epoch: u64,
client: Arc<Client>, client: Arc<Client>,
subscription: Option<client::Subscription>, subscription: Option<client::Subscription>,
acknowledge_task: Option<Task<Result<()>>>,
} }
pub enum ChannelBufferEvent { pub enum ChannelBufferEvent {
CollaboratorsChanged, CollaboratorsChanged,
Disconnected, Disconnected,
BufferEdited,
} }
impl Entity for ChannelBuffer { impl Entity for ChannelBuffer {
@ -36,6 +41,9 @@ impl Entity for ChannelBuffer {
fn release(&mut self, _: &mut AppContext) { fn release(&mut self, _: &mut AppContext) {
if self.connected { if self.connected {
if let Some(task) = self.acknowledge_task.take() {
task.detach();
}
self.client self.client
.send(proto::LeaveChannelBuffer { .send(proto::LeaveChannelBuffer {
channel_id: self.channel.id, channel_id: self.channel.id,
@ -81,6 +89,7 @@ impl ChannelBuffer {
client, client,
connected: true, connected: true,
collaborators: Default::default(), collaborators: Default::default(),
acknowledge_task: None,
channel, channel,
subscription: Some(subscription.set_model(&cx.handle(), &mut cx.to_async())), subscription: Some(subscription.set_model(&cx.handle(), &mut cx.to_async())),
user_store, user_store,
@ -159,19 +168,45 @@ impl ChannelBuffer {
&mut self, &mut self,
_: ModelHandle<language::Buffer>, _: ModelHandle<language::Buffer>,
event: &language::Event, event: &language::Event,
_: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) { ) {
if let language::Event::Operation(operation) = event { match event {
let operation = language::proto::serialize_operation(operation); language::Event::Operation(operation) => {
self.client let operation = language::proto::serialize_operation(operation);
.send(proto::UpdateChannelBuffer { self.client
channel_id: self.channel.id, .send(proto::UpdateChannelBuffer {
operations: vec![operation], channel_id: self.channel.id,
}) operations: vec![operation],
.log_err(); })
.log_err();
}
language::Event::Edited => {
cx.emit(ChannelBufferEvent::BufferEdited);
}
_ => {}
} }
} }
pub fn acknowledge_buffer_version(&mut self, cx: &mut ModelContext<'_, ChannelBuffer>) {
let buffer = self.buffer.read(cx);
let version = buffer.version();
let buffer_id = buffer.remote_id();
let client = self.client.clone();
let epoch = self.epoch();
self.acknowledge_task = Some(cx.spawn_weak(|_, cx| async move {
cx.background().timer(ACKNOWLEDGE_DEBOUNCE_INTERVAL).await;
client
.send(proto::AckBufferOperation {
buffer_id,
epoch,
version: serialize_version(&version),
})
.ok();
Ok(())
}));
}
pub fn epoch(&self) -> u64 { pub fn epoch(&self) -> u64 {
self.buffer_epoch self.buffer_epoch
} }

View file

@ -1,4 +1,4 @@
use crate::Channel; use crate::{Channel, ChannelStore};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use client::{ use client::{
proto, proto,
@ -16,7 +16,9 @@ use util::{post_inc, ResultExt as _, TryFutureExt};
pub struct ChannelChat { pub struct ChannelChat {
channel: Arc<Channel>, channel: Arc<Channel>,
messages: SumTree<ChannelMessage>, messages: SumTree<ChannelMessage>,
channel_store: ModelHandle<ChannelStore>,
loaded_all_messages: bool, loaded_all_messages: bool,
last_acknowledged_id: Option<u64>,
next_pending_message_id: usize, next_pending_message_id: usize,
user_store: ModelHandle<UserStore>, user_store: ModelHandle<UserStore>,
rpc: Arc<Client>, rpc: Arc<Client>,
@ -77,6 +79,7 @@ impl Entity for ChannelChat {
impl ChannelChat { impl ChannelChat {
pub async fn new( pub async fn new(
channel: Arc<Channel>, channel: Arc<Channel>,
channel_store: ModelHandle<ChannelStore>,
user_store: ModelHandle<UserStore>, user_store: ModelHandle<UserStore>,
client: Arc<Client>, client: Arc<Client>,
mut cx: AsyncAppContext, mut cx: AsyncAppContext,
@ -94,11 +97,13 @@ impl ChannelChat {
let mut this = Self { let mut this = Self {
channel, channel,
user_store, user_store,
channel_store,
rpc: client, rpc: client,
outgoing_messages_lock: Default::default(), outgoing_messages_lock: Default::default(),
messages: Default::default(), messages: Default::default(),
loaded_all_messages, loaded_all_messages,
next_pending_message_id: 0, next_pending_message_id: 0,
last_acknowledged_id: None,
rng: StdRng::from_entropy(), rng: StdRng::from_entropy(),
_subscription: subscription.set_model(&cx.handle(), &mut cx.to_async()), _subscription: subscription.set_model(&cx.handle(), &mut cx.to_async()),
}; };
@ -219,6 +224,26 @@ impl ChannelChat {
false false
} }
pub fn acknowledge_last_message(&mut self, cx: &mut ModelContext<Self>) {
if let ChannelMessageId::Saved(latest_message_id) = self.messages.summary().max_id {
if self
.last_acknowledged_id
.map_or(true, |acknowledged_id| acknowledged_id < latest_message_id)
{
self.rpc
.send(proto::AckChannelMessage {
channel_id: self.channel.id,
message_id: latest_message_id,
})
.ok();
self.last_acknowledged_id = Some(latest_message_id);
self.channel_store.update(cx, |store, cx| {
store.acknowledge_message_id(self.channel.id, latest_message_id, cx);
});
}
}
}
pub fn rejoin(&mut self, cx: &mut ModelContext<Self>) { pub fn rejoin(&mut self, cx: &mut ModelContext<Self>) {
let user_store = self.user_store.clone(); let user_store = self.user_store.clone();
let rpc = self.rpc.clone(); let rpc = self.rpc.clone();

View file

@ -43,8 +43,8 @@ pub type ChannelData = (Channel, ChannelPath);
pub struct Channel { pub struct Channel {
pub id: ChannelId, pub id: ChannelId,
pub name: String, pub name: String,
pub has_note_changed: bool, pub unseen_note_version: Option<(u64, clock::Global)>,
pub has_new_messages: bool, pub unseen_message_id: Option<u64>,
} }
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize)]
@ -201,34 +201,60 @@ impl ChannelStore {
) -> Task<Result<ModelHandle<ChannelBuffer>>> { ) -> Task<Result<ModelHandle<ChannelBuffer>>> {
let client = self.client.clone(); let client = self.client.clone();
let user_store = self.user_store.clone(); let user_store = self.user_store.clone();
let open_channel_buffer = self.open_channel_resource( self.open_channel_resource(
channel_id, channel_id,
|this| &mut this.opened_buffers, |this| &mut this.opened_buffers,
|channel, cx| ChannelBuffer::new(channel, client, user_store, cx), |channel, cx| ChannelBuffer::new(channel, client, user_store, cx),
cx, cx,
); )
cx.spawn(|this, mut cx| async move {
let buffer = open_channel_buffer.await?;
this.update(&mut cx, |this, cx| {
this.channel_index.clear_note_changed(channel_id);
cx.notify();
});
Ok(buffer)
})
} }
pub fn has_channel_buffer_changed(&self, channel_id: ChannelId) -> Option<bool> { pub fn has_channel_buffer_changed(&self, channel_id: ChannelId) -> Option<bool> {
self.channel_index self.channel_index
.by_id() .by_id()
.get(&channel_id) .get(&channel_id)
.map(|channel| channel.has_note_changed) .map(|channel| channel.unseen_note_version.is_some())
} }
pub fn has_new_messages(&self, channel_id: ChannelId) -> Option<bool> { pub fn has_new_messages(&self, channel_id: ChannelId) -> Option<bool> {
self.channel_index self.channel_index
.by_id() .by_id()
.get(&channel_id) .get(&channel_id)
.map(|channel| channel.has_new_messages) .map(|channel| channel.unseen_message_id.is_some())
}
pub fn notes_changed(
&mut self,
channel_id: ChannelId,
epoch: u64,
version: &clock::Global,
cx: &mut ModelContext<Self>,
) {
self.channel_index.note_changed(channel_id, epoch, version);
cx.notify();
}
pub fn acknowledge_message_id(
&mut self,
channel_id: ChannelId,
message_id: u64,
cx: &mut ModelContext<Self>,
) {
self.channel_index
.acknowledge_message_id(channel_id, message_id);
cx.notify();
}
pub fn acknowledge_notes_version(
&mut self,
channel_id: ChannelId,
epoch: u64,
version: &clock::Global,
cx: &mut ModelContext<Self>,
) {
self.channel_index
.acknowledge_note_version(channel_id, epoch, version);
cx.notify();
} }
pub fn open_channel_chat( pub fn open_channel_chat(
@ -238,20 +264,13 @@ impl ChannelStore {
) -> Task<Result<ModelHandle<ChannelChat>>> { ) -> Task<Result<ModelHandle<ChannelChat>>> {
let client = self.client.clone(); let client = self.client.clone();
let user_store = self.user_store.clone(); let user_store = self.user_store.clone();
let open_channel_chat = self.open_channel_resource( let this = cx.handle();
self.open_channel_resource(
channel_id, channel_id,
|this| &mut this.opened_chats, |this| &mut this.opened_chats,
|channel, cx| ChannelChat::new(channel, user_store, client, cx), |channel, cx| ChannelChat::new(channel, this, user_store, client, cx),
cx, cx,
); )
cx.spawn(|this, mut cx| async move {
let chat = open_channel_chat.await?;
this.update(&mut cx, |this, cx| {
this.channel_index.clear_message_changed(channel_id);
cx.notify();
});
Ok(chat)
})
} }
/// Asynchronously open a given resource associated with a channel. /// Asynchronously open a given resource associated with a channel.
@ -811,8 +830,8 @@ impl ChannelStore {
Arc::new(Channel { Arc::new(Channel {
id: channel.id, id: channel.id,
name: channel.name, name: channel.name,
has_note_changed: false, unseen_note_version: None,
has_new_messages: false, unseen_message_id: None,
}), }),
), ),
} }
@ -822,8 +841,8 @@ impl ChannelStore {
|| !payload.delete_channels.is_empty() || !payload.delete_channels.is_empty()
|| !payload.insert_edge.is_empty() || !payload.insert_edge.is_empty()
|| !payload.delete_edge.is_empty() || !payload.delete_edge.is_empty()
|| !payload.notes_changed.is_empty() || !payload.unseen_channel_messages.is_empty()
|| !payload.new_messages.is_empty(); || !payload.unseen_channel_buffer_changes.is_empty();
if channels_changed { if channels_changed {
if !payload.delete_channels.is_empty() { if !payload.delete_channels.is_empty() {
@ -850,12 +869,20 @@ impl ChannelStore {
index.insert(channel) index.insert(channel)
} }
for id_changed in payload.notes_changed { for unseen_buffer_change in payload.unseen_channel_buffer_changes {
index.note_changed(id_changed); let version = language::proto::deserialize_version(&unseen_buffer_change.version);
index.note_changed(
unseen_buffer_change.channel_id,
unseen_buffer_change.epoch,
&version,
);
} }
for id_changed in payload.new_messages { for unseen_channel_message in payload.unseen_channel_messages {
index.new_messages(id_changed); index.new_messages(
unseen_channel_message.channel_id,
unseen_channel_message.message_id,
);
} }
for edge in payload.insert_edge { for edge in payload.insert_edge {

View file

@ -39,17 +39,38 @@ impl ChannelIndex {
} }
} }
pub fn clear_note_changed(&mut self, channel_id: ChannelId) { pub fn acknowledge_note_version(
&mut self,
channel_id: ChannelId,
epoch: u64,
version: &clock::Global,
) {
if let Some(channel) = self.channels_by_id.get_mut(&channel_id) { if let Some(channel) = self.channels_by_id.get_mut(&channel_id) {
Arc::make_mut(channel).has_note_changed = false; let channel = Arc::make_mut(channel);
if let Some((unseen_epoch, unseen_version)) = &channel.unseen_note_version {
if epoch > *unseen_epoch
|| epoch == *unseen_epoch && version.observed_all(unseen_version)
{
channel.unseen_note_version = None;
}
}
} }
} }
pub fn clear_message_changed(&mut self, channel_id: ChannelId) { pub fn acknowledge_message_id(&mut self, channel_id: ChannelId, message_id: u64) {
if let Some(channel) = self.channels_by_id.get_mut(&channel_id) { if let Some(channel) = self.channels_by_id.get_mut(&channel_id) {
Arc::make_mut(channel).has_new_messages = false; let channel = Arc::make_mut(channel);
if let Some(unseen_message_id) = channel.unseen_message_id {
if message_id >= unseen_message_id {
channel.unseen_message_id = None;
}
}
} }
} }
pub fn note_changed(&mut self, channel_id: ChannelId, epoch: u64, version: &clock::Global) {
insert_note_changed(&mut self.channels_by_id, channel_id, epoch, version);
}
} }
impl Deref for ChannelIndex { impl Deref for ChannelIndex {
@ -88,15 +109,14 @@ impl<'a> ChannelPathsInsertGuard<'a> {
} }
} }
pub fn note_changed(&mut self, channel_id: ChannelId) { pub fn note_changed(&mut self, channel_id: ChannelId, epoch: u64, version: &clock::Global) {
if let Some(channel) = self.channels_by_id.get_mut(&channel_id) { insert_note_changed(&mut self.channels_by_id, channel_id, epoch, &version);
Arc::make_mut(channel).has_note_changed = true;
}
} }
pub fn new_messages(&mut self, channel_id: ChannelId) { pub fn new_messages(&mut self, channel_id: ChannelId, message_id: u64) {
if let Some(channel) = self.channels_by_id.get_mut(&channel_id) { if let Some(channel) = self.channels_by_id.get_mut(&channel_id) {
Arc::make_mut(channel).has_new_messages = true; let unseen_message_id = Arc::make_mut(channel).unseen_message_id.get_or_insert(0);
*unseen_message_id = message_id.max(*unseen_message_id);
} }
} }
@ -109,8 +129,8 @@ impl<'a> ChannelPathsInsertGuard<'a> {
Arc::new(Channel { Arc::new(Channel {
id: channel_proto.id, id: channel_proto.id,
name: channel_proto.name, name: channel_proto.name,
has_note_changed: false, unseen_note_version: None,
has_new_messages: false, unseen_message_id: None,
}), }),
); );
self.insert_root(channel_proto.id); self.insert_root(channel_proto.id);
@ -186,3 +206,21 @@ fn channel_path_sorting_key<'a>(
path.iter() path.iter()
.map(|id| Some(channels_by_id.get(id)?.name.as_str())) .map(|id| Some(channels_by_id.get(id)?.name.as_str()))
} }
fn insert_note_changed(
channels_by_id: &mut BTreeMap<ChannelId, Arc<Channel>>,
channel_id: u64,
epoch: u64,
version: &clock::Global,
) {
if let Some(channel) = channels_by_id.get_mut(&channel_id) {
let unseen_version = Arc::make_mut(channel)
.unseen_note_version
.get_or_insert((0, clock::Global::new()));
if epoch > unseen_version.0 {
*unseen_version = (epoch, version.clone());
} else {
unseen_version.1.join(&version);
}
}
}

View file

@ -34,7 +34,7 @@ use std::{
future::Future, future::Future,
marker::PhantomData, marker::PhantomData,
path::PathBuf, path::PathBuf,
sync::{Arc, Weak}, sync::{atomic::AtomicU64, Arc, Weak},
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use telemetry::Telemetry; use telemetry::Telemetry;
@ -105,7 +105,7 @@ pub fn init(client: &Arc<Client>, cx: &mut AppContext) {
} }
pub struct Client { pub struct Client {
id: usize, id: AtomicU64,
peer: Arc<Peer>, peer: Arc<Peer>,
http: Arc<dyn HttpClient>, http: Arc<dyn HttpClient>,
telemetry: Arc<Telemetry>, telemetry: Arc<Telemetry>,
@ -374,7 +374,7 @@ impl settings::Setting for TelemetrySettings {
impl Client { impl Client {
pub fn new(http: Arc<dyn HttpClient>, cx: &AppContext) -> Arc<Self> { pub fn new(http: Arc<dyn HttpClient>, cx: &AppContext) -> Arc<Self> {
Arc::new(Self { Arc::new(Self {
id: 0, id: AtomicU64::new(0),
peer: Peer::new(0), peer: Peer::new(0),
telemetry: Telemetry::new(http.clone(), cx), telemetry: Telemetry::new(http.clone(), cx),
http, http,
@ -387,17 +387,16 @@ impl Client {
}) })
} }
pub fn id(&self) -> usize { pub fn id(&self) -> u64 {
self.id self.id.load(std::sync::atomic::Ordering::SeqCst)
} }
pub fn http_client(&self) -> Arc<dyn HttpClient> { pub fn http_client(&self) -> Arc<dyn HttpClient> {
self.http.clone() self.http.clone()
} }
#[cfg(any(test, feature = "test-support"))] pub fn set_id(&self, id: u64) -> &Self {
pub fn set_id(&mut self, id: usize) -> &Self { self.id.store(id, std::sync::atomic::Ordering::SeqCst);
self.id = id;
self self
} }
@ -454,7 +453,7 @@ impl Client {
} }
fn set_status(self: &Arc<Self>, status: Status, cx: &AsyncAppContext) { fn set_status(self: &Arc<Self>, status: Status, cx: &AsyncAppContext) {
log::info!("set status on client {}: {:?}", self.id, status); log::info!("set status on client {}: {:?}", self.id(), status);
let mut state = self.state.write(); let mut state = self.state.write();
*state.status.0.borrow_mut() = status; *state.status.0.borrow_mut() = status;
@ -805,6 +804,7 @@ impl Client {
} }
} }
let credentials = credentials.unwrap(); let credentials = credentials.unwrap();
self.set_id(credentials.user_id);
if was_disconnected { if was_disconnected {
self.set_status(Status::Connecting, cx); self.set_status(Status::Connecting, cx);
@ -1221,7 +1221,7 @@ impl Client {
} }
pub fn send<T: EnvelopedMessage>(&self, message: T) -> Result<()> { pub fn send<T: EnvelopedMessage>(&self, message: T) -> Result<()> {
log::debug!("rpc send. client_id:{}, name:{}", self.id, T::NAME); log::debug!("rpc send. client_id:{}, name:{}", self.id(), T::NAME);
self.peer.send(self.connection_id()?, message) self.peer.send(self.connection_id()?, message)
} }
@ -1237,7 +1237,7 @@ impl Client {
&self, &self,
request: T, request: T,
) -> impl Future<Output = Result<TypedEnvelope<T::Response>>> { ) -> impl Future<Output = Result<TypedEnvelope<T::Response>>> {
let client_id = self.id; let client_id = self.id();
log::debug!( log::debug!(
"rpc request start. client_id:{}. name:{}", "rpc request start. client_id:{}. name:{}",
client_id, client_id,
@ -1258,7 +1258,7 @@ impl Client {
} }
fn respond<T: RequestMessage>(&self, receipt: Receipt<T>, response: T::Response) -> Result<()> { fn respond<T: RequestMessage>(&self, receipt: Receipt<T>, response: T::Response) -> Result<()> {
log::debug!("rpc respond. client_id:{}. name:{}", self.id, T::NAME); log::debug!("rpc respond. client_id:{}. name:{}", self.id(), T::NAME);
self.peer.respond(receipt, response) self.peer.respond(receipt, response)
} }
@ -1267,7 +1267,7 @@ impl Client {
receipt: Receipt<T>, receipt: Receipt<T>,
error: proto::Error, error: proto::Error,
) -> Result<()> { ) -> Result<()> {
log::debug!("rpc respond. client_id:{}. name:{}", self.id, T::NAME); log::debug!("rpc respond. client_id:{}. name:{}", self.id(), T::NAME);
self.peer.respond_with_error(receipt, error) self.peer.respond_with_error(receipt, error)
} }
@ -1336,7 +1336,7 @@ impl Client {
if let Some(handler) = handler { if let Some(handler) = handler {
let future = handler(subscriber, message, &self, cx.clone()); let future = handler(subscriber, message, &self, cx.clone());
let client_id = self.id; let client_id = self.id();
log::debug!( log::debug!(
"rpc message received. client_id:{}, sender_id:{:?}, type:{}", "rpc message received. client_id:{}, sender_id:{:?}, type:{}",
client_id, client_id,

View file

@ -439,8 +439,8 @@ pub struct ChannelsForUser {
pub channels: ChannelGraph, pub channels: ChannelGraph,
pub channel_participants: HashMap<ChannelId, Vec<UserId>>, pub channel_participants: HashMap<ChannelId, Vec<UserId>>,
pub channels_with_admin_privileges: HashSet<ChannelId>, pub channels_with_admin_privileges: HashSet<ChannelId>,
pub channels_with_changed_notes: HashSet<ChannelId>, pub unseen_buffer_changes: Vec<proto::UnseenChannelBufferChange>,
pub channels_with_new_messages: HashSet<ChannelId>, pub channel_messages: Vec<proto::UnseenChannelMessage>,
} }
#[derive(Debug)] #[derive(Debug)]

View file

@ -432,7 +432,12 @@ impl Database {
channel_id: ChannelId, channel_id: ChannelId,
user: UserId, user: UserId,
operations: &[proto::Operation], operations: &[proto::Operation],
) -> Result<(Vec<ConnectionId>, Vec<UserId>)> { ) -> Result<(
Vec<ConnectionId>,
Vec<UserId>,
i32,
Vec<proto::VectorClockEntry>,
)> {
self.transaction(move |tx| async move { self.transaction(move |tx| async move {
self.check_user_is_channel_member(channel_id, user, &*tx) self.check_user_is_channel_member(channel_id, user, &*tx)
.await?; .await?;
@ -453,6 +458,7 @@ impl Database {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let mut channel_members; let mut channel_members;
let max_version;
if !operations.is_empty() { if !operations.is_empty() {
let max_operation = operations let max_operation = operations
@ -460,6 +466,11 @@ impl Database {
.max_by_key(|op| (op.lamport_timestamp.as_ref(), op.replica_id.as_ref())) .max_by_key(|op| (op.lamport_timestamp.as_ref(), op.replica_id.as_ref()))
.unwrap(); .unwrap();
max_version = vec![proto::VectorClockEntry {
replica_id: *max_operation.replica_id.as_ref() as u32,
timestamp: *max_operation.lamport_timestamp.as_ref() as u32,
}];
// get current channel participants and save the max operation above // get current channel participants and save the max operation above
self.save_max_operation( self.save_max_operation(
user, user,
@ -492,6 +503,7 @@ impl Database {
.await?; .await?;
} else { } else {
channel_members = Vec::new(); channel_members = Vec::new();
max_version = Vec::new();
} }
let mut connections = Vec::new(); let mut connections = Vec::new();
@ -510,7 +522,7 @@ impl Database {
}); });
} }
Ok((connections, channel_members)) Ok((connections, channel_members, buffer.epoch, max_version))
}) })
.await .await
} }
@ -712,12 +724,12 @@ impl Database {
.await .await
} }
pub async fn channels_with_changed_notes( pub async fn unseen_channel_buffer_changes(
&self, &self,
user_id: UserId, user_id: UserId,
channel_ids: &[ChannelId], channel_ids: &[ChannelId],
tx: &DatabaseTransaction, tx: &DatabaseTransaction,
) -> Result<HashSet<ChannelId>> { ) -> Result<Vec<proto::UnseenChannelBufferChange>> {
#[derive(Debug, Clone, Copy, EnumIter, DeriveColumn)] #[derive(Debug, Clone, Copy, EnumIter, DeriveColumn)]
enum QueryIds { enum QueryIds {
ChannelId, ChannelId,
@ -750,37 +762,45 @@ impl Database {
} }
drop(rows); drop(rows);
let last_operations = self let latest_operations = self
.get_last_operations_for_buffers(channel_ids_by_buffer_id.keys().copied(), &*tx) .get_latest_operations_for_buffers(channel_ids_by_buffer_id.keys().copied(), &*tx)
.await?; .await?;
let mut channels_with_new_changes = HashSet::default(); let mut changes = Vec::default();
for last_operation in last_operations { for latest in latest_operations {
if let Some(observed_edit) = observed_edits_by_buffer_id.get(&last_operation.buffer_id) if let Some(observed) = observed_edits_by_buffer_id.get(&latest.buffer_id) {
{ if (
if observed_edit.epoch == last_operation.epoch observed.epoch,
&& observed_edit.lamport_timestamp == last_operation.lamport_timestamp observed.lamport_timestamp,
&& observed_edit.replica_id == last_operation.replica_id observed.replica_id,
) >= (latest.epoch, latest.lamport_timestamp, latest.replica_id)
{ {
continue; continue;
} }
} }
if let Some(channel_id) = channel_ids_by_buffer_id.get(&last_operation.buffer_id) { if let Some(channel_id) = channel_ids_by_buffer_id.get(&latest.buffer_id) {
channels_with_new_changes.insert(*channel_id); changes.push(proto::UnseenChannelBufferChange {
channel_id: channel_id.to_proto(),
epoch: latest.epoch as u64,
version: vec![proto::VectorClockEntry {
replica_id: latest.replica_id as u32,
timestamp: latest.lamport_timestamp as u32,
}],
});
} }
} }
Ok(channels_with_new_changes) Ok(changes)
} }
pub async fn get_last_operations_for_buffers( pub async fn get_latest_operations_for_buffers(
&self, &self,
channel_ids: impl IntoIterator<Item = BufferId>, buffer_ids: impl IntoIterator<Item = BufferId>,
tx: &DatabaseTransaction, tx: &DatabaseTransaction,
) -> Result<Vec<buffer_operation::Model>> { ) -> Result<Vec<buffer_operation::Model>> {
let mut values = String::new(); let mut values = String::new();
for id in channel_ids { for id in buffer_ids {
if !values.is_empty() { if !values.is_empty() {
values.push_str(", "); values.push_str(", ");
} }
@ -795,13 +815,10 @@ impl Database {
r#" r#"
SELECT SELECT
* *
FROM ( FROM
(
SELECT SELECT
buffer_id, *,
epoch,
lamport_timestamp,
replica_id,
value,
row_number() OVER ( row_number() OVER (
PARTITION BY buffer_id PARTITION BY buffer_id
ORDER BY ORDER BY
@ -812,17 +829,17 @@ impl Database {
FROM buffer_operations FROM buffer_operations
WHERE WHERE
buffer_id in ({values}) buffer_id in ({values})
) AS operations ) AS last_operations
WHERE WHERE
row_number = 1 row_number = 1
"#, "#,
); );
let stmt = Statement::from_string(self.pool.get_database_backend(), sql); let stmt = Statement::from_string(self.pool.get_database_backend(), sql);
let operations = buffer_operation::Model::find_by_statement(stmt) Ok(buffer_operation::Entity::find()
.from_raw_sql(stmt)
.all(&*tx) .all(&*tx)
.await?; .await?)
Ok(operations)
} }
} }

View file

@ -463,20 +463,20 @@ impl Database {
} }
let channel_ids = graph.channels.iter().map(|c| c.id).collect::<Vec<_>>(); let channel_ids = graph.channels.iter().map(|c| c.id).collect::<Vec<_>>();
let channels_with_changed_notes = self let channel_buffer_changes = self
.channels_with_changed_notes(user_id, &channel_ids, &*tx) .unseen_channel_buffer_changes(user_id, &channel_ids, &*tx)
.await?; .await?;
let channels_with_new_messages = self let unseen_messages = self
.channels_with_new_messages(user_id, &channel_ids, &*tx) .unseen_channel_messages(user_id, &channel_ids, &*tx)
.await?; .await?;
Ok(ChannelsForUser { Ok(ChannelsForUser {
channels: graph, channels: graph,
channel_participants, channel_participants,
channels_with_admin_privileges, channels_with_admin_privileges,
channels_with_changed_notes, unseen_buffer_changes: channel_buffer_changes,
channels_with_new_messages, channel_messages: unseen_messages,
}) })
} }

View file

@ -279,12 +279,12 @@ impl Database {
Ok(()) Ok(())
} }
pub async fn channels_with_new_messages( pub async fn unseen_channel_messages(
&self, &self,
user_id: UserId, user_id: UserId,
channel_ids: &[ChannelId], channel_ids: &[ChannelId],
tx: &DatabaseTransaction, tx: &DatabaseTransaction,
) -> Result<collections::HashSet<ChannelId>> { ) -> Result<Vec<proto::UnseenChannelMessage>> {
let mut observed_messages_by_channel_id = HashMap::default(); let mut observed_messages_by_channel_id = HashMap::default();
let mut rows = observed_channel_messages::Entity::find() let mut rows = observed_channel_messages::Entity::find()
.filter(observed_channel_messages::Column::UserId.eq(user_id)) .filter(observed_channel_messages::Column::UserId.eq(user_id))
@ -334,7 +334,7 @@ impl Database {
.all(&*tx) .all(&*tx)
.await?; .await?;
let mut channels_with_new_changes = HashSet::default(); let mut changes = Vec::new();
for last_message in last_messages { for last_message in last_messages {
if let Some(observed_message) = if let Some(observed_message) =
observed_messages_by_channel_id.get(&last_message.channel_id) observed_messages_by_channel_id.get(&last_message.channel_id)
@ -343,10 +343,13 @@ impl Database {
continue; continue;
} }
} }
channels_with_new_changes.insert(last_message.channel_id); changes.push(proto::UnseenChannelMessage {
channel_id: last_message.channel_id.to_proto(),
message_id: last_message.id.to_proto(),
});
} }
Ok(channels_with_new_changes) Ok(changes)
} }
pub async fn remove_channel_message( pub async fn remove_channel_message(

View file

@ -235,7 +235,7 @@ async fn test_channel_buffers_last_operations(db: &Database) {
.transaction(|tx| { .transaction(|tx| {
let buffers = &buffers; let buffers = &buffers;
async move { async move {
db.get_last_operations_for_buffers([buffers[0].id, buffers[2].id], &*tx) db.get_latest_operations_for_buffers([buffers[0].id, buffers[2].id], &*tx)
.await .await
} }
}) })
@ -299,7 +299,7 @@ async fn test_channel_buffers_last_operations(db: &Database) {
.transaction(|tx| { .transaction(|tx| {
let buffers = &buffers; let buffers = &buffers;
async move { async move {
db.get_last_operations_for_buffers([buffers[1].id, buffers[2].id], &*tx) db.get_latest_operations_for_buffers([buffers[1].id, buffers[2].id], &*tx)
.await .await
} }
}) })
@ -317,7 +317,7 @@ async fn test_channel_buffers_last_operations(db: &Database) {
.transaction(|tx| { .transaction(|tx| {
let buffers = &buffers; let buffers = &buffers;
async move { async move {
db.get_last_operations_for_buffers([buffers[0].id, buffers[1].id], &*tx) db.get_latest_operations_for_buffers([buffers[0].id, buffers[1].id], &*tx)
.await .await
} }
}) })
@ -331,11 +331,11 @@ async fn test_channel_buffers_last_operations(db: &Database) {
], ],
); );
let changed_channels = db let buffer_changes = db
.transaction(|tx| { .transaction(|tx| {
let buffers = &buffers; let buffers = &buffers;
async move { async move {
db.channels_with_changed_notes( db.unseen_channel_buffer_changes(
observer_id, observer_id,
&[ &[
buffers[0].channel_id, buffers[0].channel_id,
@ -349,31 +349,42 @@ async fn test_channel_buffers_last_operations(db: &Database) {
}) })
.await .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
changed_channels, buffer_changes,
[ [
buffers[0].channel_id, rpc::proto::UnseenChannelBufferChange {
buffers[1].channel_id, channel_id: buffers[0].channel_id.to_proto(),
buffers[2].channel_id, epoch: 0,
version: serialize_version(&text_buffers[0].version()),
},
rpc::proto::UnseenChannelBufferChange {
channel_id: buffers[1].channel_id.to_proto(),
epoch: 1,
version: serialize_version(&text_buffers[1].version()),
},
rpc::proto::UnseenChannelBufferChange {
channel_id: buffers[2].channel_id.to_proto(),
epoch: 0,
version: serialize_version(&text_buffers[2].version()),
},
] ]
.into_iter()
.collect::<HashSet<_>>()
); );
db.observe_buffer_version( db.observe_buffer_version(
buffers[1].id, buffers[1].id,
observer_id, observer_id,
1, 1,
&serialize_version(&text_buffers[1].version()), serialize_version(&text_buffers[1].version()).as_slice(),
) )
.await .await
.unwrap(); .unwrap();
let changed_channels = db let buffer_changes = db
.transaction(|tx| { .transaction(|tx| {
let buffers = &buffers; let buffers = &buffers;
async move { async move {
db.channels_with_changed_notes( db.unseen_channel_buffer_changes(
observer_id, observer_id,
&[ &[
buffers[0].channel_id, buffers[0].channel_id,
@ -387,11 +398,21 @@ async fn test_channel_buffers_last_operations(db: &Database) {
}) })
.await .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
changed_channels, buffer_changes,
[buffers[0].channel_id, buffers[2].channel_id,] [
.into_iter() rpc::proto::UnseenChannelBufferChange {
.collect::<HashSet<_>>() channel_id: buffers[0].channel_id.to_proto(),
epoch: 0,
version: serialize_version(&text_buffers[0].version()),
},
rpc::proto::UnseenChannelBufferChange {
channel_id: buffers[2].channel_id.to_proto(),
epoch: 0,
version: serialize_version(&text_buffers[2].version()),
},
]
); );
// Observe an earlier version of the buffer. // Observe an earlier version of the buffer.
@ -407,11 +428,11 @@ async fn test_channel_buffers_last_operations(db: &Database) {
.await .await
.unwrap(); .unwrap();
let changed_channels = db let buffer_changes = db
.transaction(|tx| { .transaction(|tx| {
let buffers = &buffers; let buffers = &buffers;
async move { async move {
db.channels_with_changed_notes( db.unseen_channel_buffer_changes(
observer_id, observer_id,
&[ &[
buffers[0].channel_id, buffers[0].channel_id,
@ -425,11 +446,21 @@ async fn test_channel_buffers_last_operations(db: &Database) {
}) })
.await .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
changed_channels, buffer_changes,
[buffers[0].channel_id, buffers[2].channel_id,] [
.into_iter() rpc::proto::UnseenChannelBufferChange {
.collect::<HashSet<_>>() channel_id: buffers[0].channel_id.to_proto(),
epoch: 0,
version: serialize_version(&text_buffers[0].version()),
},
rpc::proto::UnseenChannelBufferChange {
channel_id: buffers[2].channel_id.to_proto(),
epoch: 0,
version: serialize_version(&text_buffers[2].version()),
},
]
); );
} }

View file

@ -144,25 +144,32 @@ async fn test_channel_message_new_notification(db: &Arc<Database>) {
.await .await
.unwrap(); .unwrap();
let _ = db let (fourth_message, _, _) = db
.create_channel_message(channel_2, user, "2_1", OffsetDateTime::now_utc(), 4) .create_channel_message(channel_2, user, "2_1", OffsetDateTime::now_utc(), 4)
.await .await
.unwrap(); .unwrap();
// Check that observer has new messages // Check that observer has new messages
let channels_with_new_messages = db let unseen_messages = db
.transaction(|tx| async move { .transaction(|tx| async move {
db.channels_with_new_messages(observer, &[channel_1, channel_2], &*tx) db.unseen_channel_messages(observer, &[channel_1, channel_2], &*tx)
.await .await
}) })
.await .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
channels_with_new_messages, unseen_messages,
[channel_1, channel_2] [
.into_iter() rpc::proto::UnseenChannelMessage {
.collect::<collections::HashSet<_>>() channel_id: channel_1.to_proto(),
message_id: third_message.to_proto(),
},
rpc::proto::UnseenChannelMessage {
channel_id: channel_2.to_proto(),
message_id: fourth_message.to_proto(),
},
]
); );
// Observe the second message // Observe the second message
@ -171,18 +178,25 @@ async fn test_channel_message_new_notification(db: &Arc<Database>) {
.unwrap(); .unwrap();
// Make sure the observer still has a new message // Make sure the observer still has a new message
let channels_with_new_messages = db let unseen_messages = db
.transaction(|tx| async move { .transaction(|tx| async move {
db.channels_with_new_messages(observer, &[channel_1, channel_2], &*tx) db.unseen_channel_messages(observer, &[channel_1, channel_2], &*tx)
.await .await
}) })
.await .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
channels_with_new_messages, unseen_messages,
[channel_1, channel_2] [
.into_iter() rpc::proto::UnseenChannelMessage {
.collect::<collections::HashSet<_>>() channel_id: channel_1.to_proto(),
message_id: third_message.to_proto(),
},
rpc::proto::UnseenChannelMessage {
channel_id: channel_2.to_proto(),
message_id: fourth_message.to_proto(),
},
]
); );
// Observe the third message, // Observe the third message,
@ -191,16 +205,20 @@ async fn test_channel_message_new_notification(db: &Arc<Database>) {
.unwrap(); .unwrap();
// Make sure the observer does not have a new method // Make sure the observer does not have a new method
let channels_with_new_messages = db let unseen_messages = db
.transaction(|tx| async move { .transaction(|tx| async move {
db.channels_with_new_messages(observer, &[channel_1, channel_2], &*tx) db.unseen_channel_messages(observer, &[channel_1, channel_2], &*tx)
.await .await
}) })
.await .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
channels_with_new_messages, unseen_messages,
[channel_2].into_iter().collect::<collections::HashSet<_>>() [rpc::proto::UnseenChannelMessage {
channel_id: channel_2.to_proto(),
message_id: fourth_message.to_proto(),
}]
); );
// Observe the second message again, should not regress our observed state // Observe the second message again, should not regress our observed state
@ -208,16 +226,19 @@ async fn test_channel_message_new_notification(db: &Arc<Database>) {
.await .await
.unwrap(); .unwrap();
// Make sure the observer does not have a new method // Make sure the observer does not have a new message
let channels_with_new_messages = db let unseen_messages = db
.transaction(|tx| async move { .transaction(|tx| async move {
db.channels_with_new_messages(observer, &[channel_1, channel_2], &*tx) db.unseen_channel_messages(observer, &[channel_1, channel_2], &*tx)
.await .await
}) })
.await .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
channels_with_new_messages, unseen_messages,
[channel_2].into_iter().collect::<collections::HashSet<_>>() [rpc::proto::UnseenChannelMessage {
channel_id: channel_2.to_proto(),
message_id: fourth_message.to_proto(),
}]
); );
} }

View file

@ -274,7 +274,8 @@ impl Server {
.add_message_handler(unfollow) .add_message_handler(unfollow)
.add_message_handler(update_followers) .add_message_handler(update_followers)
.add_message_handler(update_diff_base) .add_message_handler(update_diff_base)
.add_request_handler(get_private_user_info); .add_request_handler(get_private_user_info)
.add_message_handler(acknowledge_channel_message);
Arc::new(server) Arc::new(server)
} }
@ -2568,16 +2569,8 @@ async fn respond_to_channel_invite(
name: channel.name, name: channel.name,
}), }),
); );
update.notes_changed = result update.unseen_channel_messages = result.channel_messages;
.channels_with_changed_notes update.unseen_channel_buffer_changes = result.unseen_buffer_changes;
.iter()
.map(|id| id.to_proto())
.collect();
update.new_messages = result
.channels_with_new_messages
.iter()
.map(|id| id.to_proto())
.collect();
update.insert_edge = result.channels.edges; update.insert_edge = result.channels.edges;
update update
.channel_participants .channel_participants
@ -2701,7 +2694,7 @@ async fn update_channel_buffer(
let db = session.db().await; let db = session.db().await;
let channel_id = ChannelId::from_proto(request.channel_id); let channel_id = ChannelId::from_proto(request.channel_id);
let (collaborators, non_collaborators) = db let (collaborators, non_collaborators, epoch, version) = db
.update_channel_buffer(channel_id, session.user_id, &request.operations) .update_channel_buffer(channel_id, session.user_id, &request.operations)
.await?; .await?;
@ -2726,7 +2719,11 @@ async fn update_channel_buffer(
session.peer.send( session.peer.send(
peer_id.into(), peer_id.into(),
proto::UpdateChannels { proto::UpdateChannels {
notes_changed: vec![channel_id.to_proto()], unseen_channel_buffer_changes: vec![proto::UnseenChannelBufferChange {
channel_id: channel_id.to_proto(),
epoch: epoch as u64,
version: version.clone(),
}],
..Default::default() ..Default::default()
}, },
) )
@ -2859,9 +2856,7 @@ async fn send_channel_message(
message: Some(message), message: Some(message),
})?; })?;
dbg!(&non_participants);
let pool = &*session.connection_pool().await; let pool = &*session.connection_pool().await;
broadcast( broadcast(
None, None,
non_participants non_participants
@ -2871,7 +2866,10 @@ async fn send_channel_message(
session.peer.send( session.peer.send(
peer_id.into(), peer_id.into(),
proto::UpdateChannels { proto::UpdateChannels {
new_messages: vec![channel_id.to_proto()], unseen_channel_messages: vec![proto::UnseenChannelMessage {
channel_id: channel_id.to_proto(),
message_id: message_id.to_proto(),
}],
..Default::default() ..Default::default()
}, },
) )
@ -2900,6 +2898,20 @@ async fn remove_channel_message(
Ok(()) Ok(())
} }
async fn acknowledge_channel_message(
request: proto::AckChannelMessage,
session: Session,
) -> Result<()> {
let channel_id = ChannelId::from_proto(request.channel_id);
let message_id = MessageId::from_proto(request.message_id);
session
.db()
.await
.observe_channel_message(channel_id, session.user_id, message_id)
.await?;
Ok(())
}
async fn join_channel_chat( async fn join_channel_chat(
request: proto::JoinChannelChat, request: proto::JoinChannelChat,
response: Response<proto::JoinChannelChat>, response: Response<proto::JoinChannelChat>,
@ -3035,18 +3047,8 @@ fn build_initial_channels_update(
}); });
} }
update.notes_changed = channels update.unseen_channel_buffer_changes = channels.unseen_buffer_changes;
.channels_with_changed_notes update.unseen_channel_messages = channels.channel_messages;
.iter()
.map(|channel_id| channel_id.to_proto())
.collect();
update.new_messages = channels
.channels_with_new_messages
.iter()
.map(|channel_id| channel_id.to_proto())
.collect();
update.insert_edge = channels.channels.edges; update.insert_edge = channels.channels.edges;
for (channel_id, participants) in channels.channel_participants { for (channel_id, participants) in channels.channel_participants {

View file

@ -445,8 +445,8 @@ fn channel(id: u64, name: &'static str) -> Channel {
Channel { Channel {
id, id,
name: name.to_string(), name: name.to_string(),
has_note_changed: false, unseen_note_version: None,
has_new_messages: false, unseen_message_id: None,
} }
} }

View file

@ -151,12 +151,12 @@ impl TestServer {
Arc::get_mut(&mut client) Arc::get_mut(&mut client)
.unwrap() .unwrap()
.set_id(user_id.0 as usize) .set_id(user_id.to_proto())
.override_authenticate(move |cx| { .override_authenticate(move |cx| {
cx.spawn(|_| async move { cx.spawn(|_| async move {
let access_token = "the-token".to_string(); let access_token = "the-token".to_string();
Ok(Credentials { Ok(Credentials {
user_id: user_id.0 as u64, user_id: user_id.to_proto(),
access_token, access_token,
}) })
}) })

View file

@ -1,6 +1,6 @@
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use call::report_call_event_for_channel; use call::report_call_event_for_channel;
use channel::{Channel, ChannelBuffer, ChannelBufferEvent, ChannelId}; use channel::{Channel, ChannelBuffer, ChannelBufferEvent, ChannelId, ChannelStore};
use client::{ use client::{
proto::{self, PeerId}, proto::{self, PeerId},
Collaborator, ParticipantIndex, Collaborator, ParticipantIndex,
@ -36,6 +36,7 @@ pub fn init(cx: &mut AppContext) {
pub struct ChannelView { pub struct ChannelView {
pub editor: ViewHandle<Editor>, pub editor: ViewHandle<Editor>,
project: ModelHandle<Project>, project: ModelHandle<Project>,
channel_store: ModelHandle<ChannelStore>,
channel_buffer: ModelHandle<ChannelBuffer>, channel_buffer: ModelHandle<ChannelBuffer>,
remote_id: Option<ViewId>, remote_id: Option<ViewId>,
_editor_event_subscription: Subscription, _editor_event_subscription: Subscription,
@ -94,7 +95,13 @@ impl ChannelView {
pane.update(&mut cx, |pane, cx| { pane.update(&mut cx, |pane, cx| {
pane.items_of_type::<Self>() pane.items_of_type::<Self>()
.find(|channel_view| channel_view.read(cx).channel_buffer == channel_buffer) .find(|channel_view| channel_view.read(cx).channel_buffer == channel_buffer)
.unwrap_or_else(|| cx.add_view(|cx| Self::new(project, channel_buffer, cx))) .unwrap_or_else(|| {
cx.add_view(|cx| {
let mut this = Self::new(project, channel_store, channel_buffer, cx);
this.acknowledge_buffer_version(cx);
this
})
})
}) })
.ok_or_else(|| anyhow!("pane was dropped")) .ok_or_else(|| anyhow!("pane was dropped"))
}) })
@ -102,6 +109,7 @@ impl ChannelView {
pub fn new( pub fn new(
project: ModelHandle<Project>, project: ModelHandle<Project>,
channel_store: ModelHandle<ChannelStore>,
channel_buffer: ModelHandle<ChannelBuffer>, channel_buffer: ModelHandle<ChannelBuffer>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Self { ) -> Self {
@ -121,6 +129,7 @@ impl ChannelView {
Self { Self {
editor, editor,
project, project,
channel_store,
channel_buffer, channel_buffer,
remote_id: None, remote_id: None,
_editor_event_subscription, _editor_event_subscription,
@ -137,13 +146,44 @@ impl ChannelView {
event: &ChannelBufferEvent, event: &ChannelBufferEvent,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
if let ChannelBufferEvent::Disconnected = event { match event {
self.editor.update(cx, |editor, cx| { ChannelBufferEvent::Disconnected => self.editor.update(cx, |editor, cx| {
editor.set_read_only(true); editor.set_read_only(true);
cx.notify(); cx.notify();
}) }),
ChannelBufferEvent::BufferEdited => {
if cx.is_self_focused() || self.editor.is_focused(cx) {
self.acknowledge_buffer_version(cx);
} else {
self.channel_store.update(cx, |store, cx| {
let channel_buffer = self.channel_buffer.read(cx);
store.notes_changed(
channel_buffer.channel().id,
channel_buffer.epoch(),
&channel_buffer.buffer().read(cx).version(),
cx,
)
});
}
}
_ => {}
} }
} }
fn acknowledge_buffer_version(&mut self, cx: &mut ViewContext<'_, '_, ChannelView>) {
self.channel_store.update(cx, |store, cx| {
let channel_buffer = self.channel_buffer.read(cx);
store.acknowledge_notes_version(
channel_buffer.channel().id,
channel_buffer.epoch(),
&channel_buffer.buffer().read(cx).version(),
cx,
)
});
self.channel_buffer.update(cx, |buffer, cx| {
buffer.acknowledge_buffer_version(cx);
});
}
} }
impl Entity for ChannelView { impl Entity for ChannelView {
@ -161,6 +201,7 @@ impl View for ChannelView {
fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) { fn focus_in(&mut self, _: AnyViewHandle, cx: &mut ViewContext<Self>) {
if cx.is_self_focused() { if cx.is_self_focused() {
self.acknowledge_buffer_version(cx);
cx.focus(self.editor.as_any()) cx.focus(self.editor.as_any())
} }
} }
@ -200,6 +241,7 @@ impl Item for ChannelView {
fn clone_on_split(&self, _: WorkspaceId, cx: &mut ViewContext<Self>) -> Option<Self> { fn clone_on_split(&self, _: WorkspaceId, cx: &mut ViewContext<Self>) -> Option<Self> {
Some(Self::new( Some(Self::new(
self.project.clone(), self.project.clone(),
self.channel_store.clone(),
self.channel_buffer.clone(), self.channel_buffer.clone(),
cx, cx,
)) ))

View file

@ -42,6 +42,7 @@ pub struct ChatPanel {
local_timezone: UtcOffset, local_timezone: UtcOffset,
fs: Arc<dyn Fs>, fs: Arc<dyn Fs>,
width: Option<f32>, width: Option<f32>,
active: bool,
pending_serialization: Task<Option<()>>, pending_serialization: Task<Option<()>>,
subscriptions: Vec<gpui::Subscription>, subscriptions: Vec<gpui::Subscription>,
workspace: WeakViewHandle<Workspace>, workspace: WeakViewHandle<Workspace>,
@ -138,6 +139,7 @@ impl ChatPanel {
has_focus: false, has_focus: false,
subscriptions: Vec::new(), subscriptions: Vec::new(),
workspace: workspace_handle, workspace: workspace_handle,
active: false,
width: None, width: None,
}; };
@ -154,9 +156,9 @@ impl ChatPanel {
}), }),
); );
this.init_active_channel(cx); this.update_channel_count(cx);
cx.observe(&this.channel_store, |this, _, cx| { cx.observe(&this.channel_store, |this, _, cx| {
this.init_active_channel(cx); this.update_channel_count(cx)
}) })
.detach(); .detach();
@ -225,10 +227,8 @@ impl ChatPanel {
); );
} }
fn init_active_channel(&mut self, cx: &mut ViewContext<Self>) { fn update_channel_count(&mut self, cx: &mut ViewContext<Self>) {
let channel_count = self.channel_store.read(cx).channel_count(); let channel_count = self.channel_store.read(cx).channel_count();
self.message_list.reset(0);
self.active_chat = None;
self.channel_select.update(cx, |select, cx| { self.channel_select.update(cx, |select, cx| {
select.set_item_count(channel_count, cx); select.set_item_count(channel_count, cx);
}); });
@ -247,6 +247,7 @@ impl ChatPanel {
} }
let subscription = cx.subscribe(&chat, Self::channel_did_change); let subscription = cx.subscribe(&chat, Self::channel_did_change);
self.active_chat = Some((chat, subscription)); self.active_chat = Some((chat, subscription));
self.acknowledge_last_message(cx);
self.channel_select.update(cx, |select, cx| { self.channel_select.update(cx, |select, cx| {
if let Some(ix) = self.channel_store.read(cx).index_of_channel(id) { if let Some(ix) = self.channel_store.read(cx).index_of_channel(id) {
select.set_selected_index(ix, cx); select.set_selected_index(ix, cx);
@ -268,11 +269,22 @@ impl ChatPanel {
new_count, new_count,
} => { } => {
self.message_list.splice(old_range.clone(), *new_count); self.message_list.splice(old_range.clone(), *new_count);
self.acknowledge_last_message(cx);
} }
} }
cx.notify(); cx.notify();
} }
fn acknowledge_last_message(&mut self, cx: &mut ViewContext<'_, '_, ChatPanel>) {
if self.active {
if let Some((chat, _)) = &self.active_chat {
chat.update(cx, |chat, cx| {
chat.acknowledge_last_message(cx);
});
}
}
}
fn render_channel(&self, cx: &mut ViewContext<Self>) -> AnyElement<Self> { fn render_channel(&self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
let theme = theme::current(cx); let theme = theme::current(cx);
Flex::column() Flex::column()
@ -627,8 +639,12 @@ impl Panel for ChatPanel {
} }
fn set_active(&mut self, active: bool, cx: &mut ViewContext<Self>) { fn set_active(&mut self, active: bool, cx: &mut ViewContext<Self>) {
if active && !is_chat_feature_enabled(cx) { self.active = active;
cx.emit(Event::Dismissed); if active {
self.acknowledge_last_message(cx);
if !is_chat_feature_enabled(cx) {
cx.emit(Event::Dismissed);
}
} }
} }

View file

@ -1821,7 +1821,7 @@ impl CollabPanel {
channel.name.clone(), channel.name.clone(),
theme theme
.channel_name .channel_name
.in_state(channel.has_new_messages) .in_state(channel.unseen_message_id.is_some())
.text .text
.clone(), .clone(),
) )
@ -1880,7 +1880,7 @@ impl CollabPanel {
let participants = let participants =
self.channel_store.read(cx).channel_participants(channel_id); self.channel_store.read(cx).channel_participants(channel_id);
if participants.is_empty() { if participants.is_empty() {
if channel.has_note_changed { if channel.unseen_note_version.is_some() {
Svg::new("icons/terminal.svg") Svg::new("icons/terminal.svg")
.with_color(theme.channel_note_active_color) .with_color(theme.channel_note_active_color)
.constrained() .constrained()

View file

@ -957,8 +957,19 @@ message UpdateChannels {
repeated uint64 remove_channel_invitations = 6; repeated uint64 remove_channel_invitations = 6;
repeated ChannelParticipants channel_participants = 7; repeated ChannelParticipants channel_participants = 7;
repeated ChannelPermission channel_permissions = 8; repeated ChannelPermission channel_permissions = 8;
repeated uint64 notes_changed = 9; repeated UnseenChannelMessage unseen_channel_messages = 9;
repeated uint64 new_messages = 10; repeated UnseenChannelBufferChange unseen_channel_buffer_changes = 10;
}
message UnseenChannelMessage {
uint64 channel_id = 1;
uint64 message_id = 2;
}
message UnseenChannelBufferChange {
uint64 channel_id = 1;
uint64 epoch = 2;
repeated VectorClockEntry version = 3;
} }
message ChannelEdge { message ChannelEdge {
@ -1127,8 +1138,7 @@ message RejoinChannelBuffersResponse {
message AckBufferOperation { message AckBufferOperation {
uint64 buffer_id = 1; uint64 buffer_id = 1;
uint64 epoch = 2; uint64 epoch = 2;
uint64 lamport_timestamp = 3; repeated VectorClockEntry version = 3;
uint64 replica_id = 4;
} }
message JoinChannelBufferResponse { message JoinChannelBufferResponse {