Guest roles (#3140)

Release Notes:

- Added a "guest" role to channels, and made that the default when a new
user joins a public channel.
This commit is contained in:
Max Brunsfeld 2023-10-24 17:29:14 +02:00 committed by GitHub
commit cc9e92857b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 2320 additions and 1711 deletions

1
Cargo.lock generated
View file

@ -1575,6 +1575,7 @@ dependencies = [
"serde", "serde",
"serde_derive", "serde_derive",
"settings", "settings",
"smallvec",
"theme", "theme",
"theme_selector", "theme_selector",
"time", "time",

View file

@ -370,42 +370,15 @@
{ {
"context": "Pane", "context": "Pane",
"bindings": { "bindings": {
"ctrl-1": [ "ctrl-1": ["pane::ActivateItem", 0],
"pane::ActivateItem", "ctrl-2": ["pane::ActivateItem", 1],
0 "ctrl-3": ["pane::ActivateItem", 2],
], "ctrl-4": ["pane::ActivateItem", 3],
"ctrl-2": [ "ctrl-5": ["pane::ActivateItem", 4],
"pane::ActivateItem", "ctrl-6": ["pane::ActivateItem", 5],
1 "ctrl-7": ["pane::ActivateItem", 6],
], "ctrl-8": ["pane::ActivateItem", 7],
"ctrl-3": [ "ctrl-9": ["pane::ActivateItem", 8],
"pane::ActivateItem",
2
],
"ctrl-4": [
"pane::ActivateItem",
3
],
"ctrl-5": [
"pane::ActivateItem",
4
],
"ctrl-6": [
"pane::ActivateItem",
5
],
"ctrl-7": [
"pane::ActivateItem",
6
],
"ctrl-8": [
"pane::ActivateItem",
7
],
"ctrl-9": [
"pane::ActivateItem",
8
],
"ctrl-0": "pane::ActivateLastItem", "ctrl-0": "pane::ActivateLastItem",
"ctrl--": "pane::GoBack", "ctrl--": "pane::GoBack",
"ctrl-_": "pane::GoForward", "ctrl-_": "pane::GoForward",
@ -416,42 +389,15 @@
{ {
"context": "Workspace", "context": "Workspace",
"bindings": { "bindings": {
"cmd-1": [ "cmd-1": ["workspace::ActivatePane", 0],
"workspace::ActivatePane", "cmd-2": ["workspace::ActivatePane", 1],
0 "cmd-3": ["workspace::ActivatePane", 2],
], "cmd-4": ["workspace::ActivatePane", 3],
"cmd-2": [ "cmd-5": ["workspace::ActivatePane", 4],
"workspace::ActivatePane", "cmd-6": ["workspace::ActivatePane", 5],
1 "cmd-7": ["workspace::ActivatePane", 6],
], "cmd-8": ["workspace::ActivatePane", 7],
"cmd-3": [ "cmd-9": ["workspace::ActivatePane", 8],
"workspace::ActivatePane",
2
],
"cmd-4": [
"workspace::ActivatePane",
3
],
"cmd-5": [
"workspace::ActivatePane",
4
],
"cmd-6": [
"workspace::ActivatePane",
5
],
"cmd-7": [
"workspace::ActivatePane",
6
],
"cmd-8": [
"workspace::ActivatePane",
7
],
"cmd-9": [
"workspace::ActivatePane",
8
],
"cmd-b": "workspace::ToggleLeftDock", "cmd-b": "workspace::ToggleLeftDock",
"cmd-r": "workspace::ToggleRightDock", "cmd-r": "workspace::ToggleRightDock",
"cmd-j": "workspace::ToggleBottomDock", "cmd-j": "workspace::ToggleBottomDock",
@ -494,38 +440,14 @@
}, },
{ {
"bindings": { "bindings": {
"cmd-k cmd-left": [ "cmd-k cmd-left": ["workspace::ActivatePaneInDirection", "Left"],
"workspace::ActivatePaneInDirection", "cmd-k cmd-right": ["workspace::ActivatePaneInDirection", "Right"],
"Left" "cmd-k cmd-up": ["workspace::ActivatePaneInDirection", "Up"],
], "cmd-k cmd-down": ["workspace::ActivatePaneInDirection", "Down"],
"cmd-k cmd-right": [ "cmd-k shift-left": ["workspace::SwapPaneInDirection", "Left"],
"workspace::ActivatePaneInDirection", "cmd-k shift-right": ["workspace::SwapPaneInDirection", "Right"],
"Right" "cmd-k shift-up": ["workspace::SwapPaneInDirection", "Up"],
], "cmd-k shift-down": ["workspace::SwapPaneInDirection", "Down"]
"cmd-k cmd-up": [
"workspace::ActivatePaneInDirection",
"Up"
],
"cmd-k cmd-down": [
"workspace::ActivatePaneInDirection",
"Down"
],
"cmd-k shift-left": [
"workspace::SwapPaneInDirection",
"Left"
],
"cmd-k shift-right": [
"workspace::SwapPaneInDirection",
"Right"
],
"cmd-k shift-up": [
"workspace::SwapPaneInDirection",
"Up"
],
"cmd-k shift-down": [
"workspace::SwapPaneInDirection",
"Down"
]
} }
}, },
// Bindings from Atom // Bindings from Atom
@ -627,14 +549,6 @@
"space": "collab_panel::InsertSpace" "space": "collab_panel::InsertSpace"
} }
}, },
{
"context": "(CollabPanel && not_editing) > Editor",
"bindings": {
"cmd-c": "collab_panel::StartLinkChannel",
"cmd-x": "collab_panel::StartMoveChannel",
"cmd-v": "collab_panel::MoveOrLinkToSelected"
}
},
{ {
"context": "ChannelModal", "context": "ChannelModal",
"bindings": { "bindings": {
@ -655,57 +569,21 @@
"cmd-v": "terminal::Paste", "cmd-v": "terminal::Paste",
"cmd-k": "terminal::Clear", "cmd-k": "terminal::Clear",
// Some nice conveniences // Some nice conveniences
"cmd-backspace": [ "cmd-backspace": ["terminal::SendText", "\u0015"],
"terminal::SendText", "cmd-right": ["terminal::SendText", "\u0005"],
"\u0015" "cmd-left": ["terminal::SendText", "\u0001"],
],
"cmd-right": [
"terminal::SendText",
"\u0005"
],
"cmd-left": [
"terminal::SendText",
"\u0001"
],
// Terminal.app compatibility // Terminal.app compatibility
"alt-left": [ "alt-left": ["terminal::SendText", "\u001bb"],
"terminal::SendText", "alt-right": ["terminal::SendText", "\u001bf"],
"\u001bb"
],
"alt-right": [
"terminal::SendText",
"\u001bf"
],
// There are conflicting bindings for these keys in the global context. // There are conflicting bindings for these keys in the global context.
// these bindings override them, remove at your own risk: // these bindings override them, remove at your own risk:
"up": [ "up": ["terminal::SendKeystroke", "up"],
"terminal::SendKeystroke", "pageup": ["terminal::SendKeystroke", "pageup"],
"up" "down": ["terminal::SendKeystroke", "down"],
], "pagedown": ["terminal::SendKeystroke", "pagedown"],
"pageup": [ "escape": ["terminal::SendKeystroke", "escape"],
"terminal::SendKeystroke", "enter": ["terminal::SendKeystroke", "enter"],
"pageup" "ctrl-c": ["terminal::SendKeystroke", "ctrl-c"]
],
"down": [
"terminal::SendKeystroke",
"down"
],
"pagedown": [
"terminal::SendKeystroke",
"pagedown"
],
"escape": [
"terminal::SendKeystroke",
"escape"
],
"enter": [
"terminal::SendKeystroke",
"enter"
],
"ctrl-c": [
"terminal::SendKeystroke",
"ctrl-c"
]
} }
} }
] ]

View file

@ -55,7 +55,7 @@ pub enum Event {
pub struct Room { pub struct Room {
id: u64, id: u64,
channel_id: Option<u64>, pub channel_id: Option<u64>,
live_kit: Option<LiveKitRoom>, live_kit: Option<LiveKitRoom>,
status: RoomStatus, status: RoomStatus,
shared_projects: HashSet<WeakModelHandle<Project>>, shared_projects: HashSet<WeakModelHandle<Project>>,
@ -122,6 +122,10 @@ impl Room {
} }
} }
pub fn can_publish(&self) -> bool {
self.live_kit.as_ref().is_some_and(|room| room.can_publish)
}
fn new( fn new(
id: u64, id: u64,
channel_id: Option<u64>, channel_id: Option<u64>,
@ -181,6 +185,7 @@ impl Room {
}); });
let connect = room.connect(&connection_info.server_url, &connection_info.token); let connect = room.connect(&connection_info.server_url, &connection_info.token);
if connection_info.can_publish {
cx.spawn(|this, mut cx| async move { cx.spawn(|this, mut cx| async move {
connect.await?; connect.await?;
@ -192,9 +197,11 @@ impl Room {
anyhow::Ok(()) anyhow::Ok(())
}) })
.detach_and_log_err(cx); .detach_and_log_err(cx);
}
Some(LiveKitRoom { Some(LiveKitRoom {
room, room,
can_publish: connection_info.can_publish,
screen_track: LocalTrack::None, screen_track: LocalTrack::None,
microphone_track: LocalTrack::None, microphone_track: LocalTrack::None,
next_publish_id: 0, next_publish_id: 0,
@ -1498,6 +1505,7 @@ struct LiveKitRoom {
deafened: bool, deafened: bool,
speaking: bool, speaking: bool,
next_publish_id: usize, next_publish_id: usize,
can_publish: bool,
_maintain_room: Task<()>, _maintain_room: Task<()>,
_maintain_tracks: [Task<()>; 2], _maintain_tracks: [Task<()>; 2],
} }

View file

@ -1,4 +1,4 @@
use crate::Channel; use crate::{Channel, ChannelId, ChannelStore};
use anyhow::Result; use anyhow::Result;
use client::{Client, Collaborator, UserStore}; use client::{Client, Collaborator, UserStore};
use collections::HashMap; use collections::HashMap;
@ -19,10 +19,11 @@ pub(crate) fn init(client: &Arc<Client>) {
} }
pub struct ChannelBuffer { pub struct ChannelBuffer {
pub(crate) channel: Arc<Channel>, pub channel_id: ChannelId,
connected: bool, connected: bool,
collaborators: HashMap<PeerId, Collaborator>, collaborators: HashMap<PeerId, Collaborator>,
user_store: ModelHandle<UserStore>, user_store: ModelHandle<UserStore>,
channel_store: ModelHandle<ChannelStore>,
buffer: ModelHandle<language::Buffer>, buffer: ModelHandle<language::Buffer>,
buffer_epoch: u64, buffer_epoch: u64,
client: Arc<Client>, client: Arc<Client>,
@ -34,6 +35,7 @@ pub enum ChannelBufferEvent {
CollaboratorsChanged, CollaboratorsChanged,
Disconnected, Disconnected,
BufferEdited, BufferEdited,
ChannelChanged,
} }
impl Entity for ChannelBuffer { impl Entity for ChannelBuffer {
@ -46,7 +48,7 @@ impl Entity for ChannelBuffer {
} }
self.client self.client
.send(proto::LeaveChannelBuffer { .send(proto::LeaveChannelBuffer {
channel_id: self.channel.id, channel_id: self.channel_id,
}) })
.log_err(); .log_err();
} }
@ -58,6 +60,7 @@ impl ChannelBuffer {
channel: Arc<Channel>, channel: Arc<Channel>,
client: Arc<Client>, client: Arc<Client>,
user_store: ModelHandle<UserStore>, user_store: ModelHandle<UserStore>,
channel_store: ModelHandle<ChannelStore>,
mut cx: AsyncAppContext, mut cx: AsyncAppContext,
) -> Result<ModelHandle<Self>> { ) -> Result<ModelHandle<Self>> {
let response = client let response = client
@ -90,9 +93,10 @@ impl ChannelBuffer {
connected: true, connected: true,
collaborators: Default::default(), collaborators: Default::default(),
acknowledge_task: None, acknowledge_task: None,
channel, channel_id: channel.id,
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,
channel_store,
}; };
this.replace_collaborators(response.collaborators, cx); this.replace_collaborators(response.collaborators, cx);
this this
@ -179,7 +183,7 @@ impl ChannelBuffer {
let operation = language::proto::serialize_operation(operation); let operation = language::proto::serialize_operation(operation);
self.client self.client
.send(proto::UpdateChannelBuffer { .send(proto::UpdateChannelBuffer {
channel_id: self.channel.id, channel_id: self.channel_id,
operations: vec![operation], operations: vec![operation],
}) })
.log_err(); .log_err();
@ -223,12 +227,15 @@ impl ChannelBuffer {
&self.collaborators &self.collaborators
} }
pub fn channel(&self) -> Arc<Channel> { pub fn channel(&self, cx: &AppContext) -> Option<Arc<Channel>> {
self.channel.clone() self.channel_store
.read(cx)
.channel_for_id(self.channel_id)
.cloned()
} }
pub(crate) fn disconnect(&mut self, cx: &mut ModelContext<Self>) { pub(crate) fn disconnect(&mut self, cx: &mut ModelContext<Self>) {
log::info!("channel buffer {} disconnected", self.channel.id); log::info!("channel buffer {} disconnected", self.channel_id);
if self.connected { if self.connected {
self.connected = false; self.connected = false;
self.subscription.take(); self.subscription.take();
@ -237,6 +244,11 @@ impl ChannelBuffer {
} }
} }
pub(crate) fn channel_changed(&mut self, cx: &mut ModelContext<Self>) {
cx.emit(ChannelBufferEvent::ChannelChanged);
cx.notify()
}
pub fn is_connected(&self) -> bool { pub fn is_connected(&self) -> bool {
self.connected self.connected
} }

View file

@ -19,7 +19,7 @@ use time::OffsetDateTime;
use util::{post_inc, ResultExt as _, TryFutureExt}; use util::{post_inc, ResultExt as _, TryFutureExt};
pub struct ChannelChat { pub struct ChannelChat {
channel: Arc<Channel>, pub channel_id: ChannelId,
messages: SumTree<ChannelMessage>, messages: SumTree<ChannelMessage>,
acknowledged_message_ids: HashSet<u64>, acknowledged_message_ids: HashSet<u64>,
channel_store: ModelHandle<ChannelStore>, channel_store: ModelHandle<ChannelStore>,
@ -87,7 +87,7 @@ impl Entity for ChannelChat {
fn release(&mut self, _: &mut AppContext) { fn release(&mut self, _: &mut AppContext) {
self.rpc self.rpc
.send(proto::LeaveChannelChat { .send(proto::LeaveChannelChat {
channel_id: self.channel.id, channel_id: self.channel_id,
}) })
.log_err(); .log_err();
} }
@ -112,7 +112,7 @@ impl ChannelChat {
Ok(cx.add_model(|cx| { Ok(cx.add_model(|cx| {
let mut this = Self { let mut this = Self {
channel, channel_id: channel.id,
user_store, user_store,
channel_store, channel_store,
rpc: client, rpc: client,
@ -130,8 +130,11 @@ impl ChannelChat {
})) }))
} }
pub fn channel(&self) -> &Arc<Channel> { pub fn channel(&self, cx: &AppContext) -> Option<Arc<Channel>> {
&self.channel self.channel_store
.read(cx)
.channel_for_id(self.channel_id)
.cloned()
} }
pub fn client(&self) -> &Arc<Client> { pub fn client(&self) -> &Arc<Client> {
@ -153,7 +156,7 @@ impl ChannelChat {
.current_user() .current_user()
.ok_or_else(|| anyhow!("current_user is not present"))?; .ok_or_else(|| anyhow!("current_user is not present"))?;
let channel_id = self.channel.id; let channel_id = self.channel_id;
let pending_id = ChannelMessageId::Pending(post_inc(&mut self.next_pending_message_id)); let pending_id = ChannelMessageId::Pending(post_inc(&mut self.next_pending_message_id));
let nonce = self.rng.gen(); let nonce = self.rng.gen();
self.insert_messages( self.insert_messages(
@ -195,7 +198,7 @@ impl ChannelChat {
pub fn remove_message(&mut self, id: u64, cx: &mut ModelContext<Self>) -> Task<Result<()>> { pub fn remove_message(&mut self, id: u64, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
let response = self.rpc.request(proto::RemoveChannelMessage { let response = self.rpc.request(proto::RemoveChannelMessage {
channel_id: self.channel.id, channel_id: self.channel_id,
message_id: id, message_id: id,
}); });
cx.spawn(|this, mut cx| async move { cx.spawn(|this, mut cx| async move {
@ -215,7 +218,7 @@ impl ChannelChat {
let rpc = self.rpc.clone(); let rpc = self.rpc.clone();
let user_store = self.user_store.clone(); let user_store = self.user_store.clone();
let channel_id = self.channel.id; let channel_id = self.channel_id;
let before_message_id = self.first_loaded_message_id()?; let before_message_id = self.first_loaded_message_id()?;
Some(cx.spawn(|this, mut cx| { Some(cx.spawn(|this, mut cx| {
async move { async move {
@ -288,13 +291,13 @@ impl ChannelChat {
{ {
self.rpc self.rpc
.send(proto::AckChannelMessage { .send(proto::AckChannelMessage {
channel_id: self.channel.id, channel_id: self.channel_id,
message_id: latest_message_id, message_id: latest_message_id,
}) })
.ok(); .ok();
self.last_acknowledged_id = Some(latest_message_id); self.last_acknowledged_id = Some(latest_message_id);
self.channel_store.update(cx, |store, cx| { self.channel_store.update(cx, |store, cx| {
store.acknowledge_message_id(self.channel.id, latest_message_id, cx); store.acknowledge_message_id(self.channel_id, latest_message_id, cx);
}); });
} }
} }
@ -303,7 +306,7 @@ impl ChannelChat {
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();
let channel_id = self.channel.id; let channel_id = self.channel_id;
cx.spawn(|this, mut cx| { cx.spawn(|this, mut cx| {
async move { async move {
let response = rpc.request(proto::JoinChannelChat { channel_id }).await?; let response = rpc.request(proto::JoinChannelChat { channel_id }).await?;
@ -376,7 +379,7 @@ impl ChannelChat {
if self.acknowledged_message_ids.insert(id) { if self.acknowledged_message_ids.insert(id) {
self.rpc self.rpc
.send(proto::AckChannelMessage { .send(proto::AckChannelMessage {
channel_id: self.channel.id, channel_id: self.channel_id,
message_id: id, message_id: id,
}) })
.ok(); .ok();
@ -412,7 +415,7 @@ impl ChannelChat {
this.update(&mut cx, |this, cx| { this.update(&mut cx, |this, cx| {
this.insert_messages(SumTree::from_item(message, &()), cx); this.insert_messages(SumTree::from_item(message, &()), cx);
cx.emit(ChannelChatEvent::NewMessage { cx.emit(ChannelChatEvent::NewMessage {
channel_id: this.channel.id, channel_id: this.channel_id,
message_id, message_id,
}) })
}); });

View file

@ -9,7 +9,7 @@ use db::RELEASE_CHANNEL;
use futures::{channel::mpsc, future::Shared, Future, FutureExt, StreamExt}; use futures::{channel::mpsc, future::Shared, Future, FutureExt, StreamExt};
use gpui::{AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, Task, WeakModelHandle}; use gpui::{AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, Task, WeakModelHandle};
use rpc::{ use rpc::{
proto::{self, ChannelEdge, ChannelPermission, ChannelRole, ChannelVisibility}, proto::{self, ChannelEdge, ChannelVisibility},
TypedEnvelope, TypedEnvelope,
}; };
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
@ -30,7 +30,6 @@ pub struct ChannelStore {
channel_index: ChannelIndex, channel_index: ChannelIndex,
channel_invitations: Vec<Arc<Channel>>, channel_invitations: Vec<Arc<Channel>>,
channel_participants: HashMap<ChannelId, Vec<Arc<User>>>, channel_participants: HashMap<ChannelId, Vec<Arc<User>>>,
channels_with_admin_privileges: HashSet<ChannelId>,
outgoing_invites: HashSet<(ChannelId, UserId)>, outgoing_invites: HashSet<(ChannelId, UserId)>,
update_channels_tx: mpsc::UnboundedSender<proto::UpdateChannels>, update_channels_tx: mpsc::UnboundedSender<proto::UpdateChannels>,
opened_buffers: HashMap<ChannelId, OpenedModelHandle<ChannelBuffer>>, opened_buffers: HashMap<ChannelId, OpenedModelHandle<ChannelBuffer>>,
@ -50,6 +49,7 @@ pub struct Channel {
pub id: ChannelId, pub id: ChannelId,
pub name: String, pub name: String,
pub visibility: proto::ChannelVisibility, pub visibility: proto::ChannelVisibility,
pub role: proto::ChannelRole,
pub unseen_note_version: Option<(u64, clock::Global)>, pub unseen_note_version: Option<(u64, clock::Global)>,
pub unseen_message_id: Option<u64>, pub unseen_message_id: Option<u64>,
} }
@ -72,6 +72,10 @@ impl Channel {
slug.trim_matches(|c| c == '-').to_string() slug.trim_matches(|c| c == '-').to_string()
} }
pub fn can_edit_notes(&self) -> bool {
self.role == proto::ChannelRole::Member || self.role == proto::ChannelRole::Admin
}
} }
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize)] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize)]
@ -161,7 +165,6 @@ impl ChannelStore {
channel_invitations: Vec::default(), channel_invitations: Vec::default(),
channel_index: ChannelIndex::default(), channel_index: ChannelIndex::default(),
channel_participants: Default::default(), channel_participants: Default::default(),
channels_with_admin_privileges: Default::default(),
outgoing_invites: Default::default(), outgoing_invites: Default::default(),
opened_buffers: Default::default(), opened_buffers: Default::default(),
opened_chats: Default::default(), opened_chats: Default::default(),
@ -269,10 +272,11 @@ 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 channel_store = cx.handle();
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, channel_store, cx),
cx, cx,
) )
} }
@ -449,16 +453,11 @@ impl ChannelStore {
.spawn(async move { task.await.map_err(|error| anyhow!("{}", error)) }) .spawn(async move { task.await.map_err(|error| anyhow!("{}", error)) })
} }
pub fn is_user_admin(&self, channel_id: ChannelId) -> bool { pub fn is_channel_admin(&self, channel_id: ChannelId) -> bool {
self.channel_index.iter().any(|path| { let Some(channel) = self.channel_for_id(channel_id) else {
if let Some(ix) = path.iter().position(|id| *id == channel_id) { return false;
path[..=ix] };
.iter() channel.role == proto::ChannelRole::Admin
.any(|id| self.channels_with_admin_privileges.contains(id))
} else {
false
}
})
} }
pub fn channel_participants(&self, channel_id: ChannelId) -> &[Arc<User>] { pub fn channel_participants(&self, channel_id: ChannelId) -> &[Arc<User>] {
@ -499,10 +498,6 @@ impl ChannelStore {
proto::UpdateChannels { proto::UpdateChannels {
channels: vec![channel], channels: vec![channel],
insert_edge: parent_edge, insert_edge: parent_edge,
channel_permissions: vec![ChannelPermission {
channel_id,
role: ChannelRole::Admin.into(),
}],
..Default::default() ..Default::default()
}, },
cx, cx,
@ -800,6 +795,11 @@ impl ChannelStore {
} }
fn handle_connect(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> { fn handle_connect(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
self.channel_index.clear();
self.channel_invitations.clear();
self.channel_participants.clear();
self.channel_index.clear();
self.outgoing_invites.clear();
self.disconnect_channel_buffers_task.take(); self.disconnect_channel_buffers_task.take();
for chat in self.opened_chats.values() { for chat in self.opened_chats.values() {
@ -819,7 +819,7 @@ impl ChannelStore {
let channel_buffer = buffer.read(cx); let channel_buffer = buffer.read(cx);
let buffer = channel_buffer.buffer().read(cx); let buffer = channel_buffer.buffer().read(cx);
buffer_versions.push(proto::ChannelBufferVersion { buffer_versions.push(proto::ChannelBufferVersion {
channel_id: channel_buffer.channel().id, channel_id: channel_buffer.channel_id,
epoch: channel_buffer.epoch(), epoch: channel_buffer.epoch(),
version: language::proto::serialize_version(&buffer.version()), version: language::proto::serialize_version(&buffer.version()),
}); });
@ -846,13 +846,13 @@ impl ChannelStore {
}; };
channel_buffer.update(cx, |channel_buffer, cx| { channel_buffer.update(cx, |channel_buffer, cx| {
let channel_id = channel_buffer.channel().id; let channel_id = channel_buffer.channel_id;
if let Some(remote_buffer) = response if let Some(remote_buffer) = response
.buffers .buffers
.iter_mut() .iter_mut()
.find(|buffer| buffer.channel_id == channel_id) .find(|buffer| buffer.channel_id == channel_id)
{ {
let channel_id = channel_buffer.channel().id; let channel_id = channel_buffer.channel_id;
let remote_version = let remote_version =
language::proto::deserialize_version(&remote_buffer.version); language::proto::deserialize_version(&remote_buffer.version);
@ -909,12 +909,6 @@ impl ChannelStore {
} }
fn handle_disconnect(&mut self, wait_for_reconnect: bool, cx: &mut ModelContext<Self>) { fn handle_disconnect(&mut self, wait_for_reconnect: bool, cx: &mut ModelContext<Self>) {
self.channel_index.clear();
self.channel_invitations.clear();
self.channel_participants.clear();
self.channels_with_admin_privileges.clear();
self.channel_index.clear();
self.outgoing_invites.clear();
cx.notify(); cx.notify();
self.disconnect_channel_buffers_task.get_or_insert_with(|| { self.disconnect_channel_buffers_task.get_or_insert_with(|| {
@ -958,6 +952,7 @@ impl ChannelStore {
Arc::new(Channel { Arc::new(Channel {
id: channel.id, id: channel.id,
visibility: channel.visibility(), visibility: channel.visibility(),
role: channel.role(),
name: channel.name, name: channel.name,
unseen_note_version: None, unseen_note_version: None,
unseen_message_id: None, unseen_message_id: None,
@ -977,12 +972,17 @@ impl ChannelStore {
if !payload.delete_channels.is_empty() { if !payload.delete_channels.is_empty() {
self.channel_index.delete_channels(&payload.delete_channels); self.channel_index.delete_channels(&payload.delete_channels);
self.channel_participants self.channel_participants
.retain(|channel_id, _| !payload.delete_channels.contains(channel_id)); .retain(|channel_id, _| !&payload.delete_channels.contains(channel_id));
self.channels_with_admin_privileges
.retain(|channel_id| !payload.delete_channels.contains(channel_id));
for channel_id in &payload.delete_channels { for channel_id in &payload.delete_channels {
let channel_id = *channel_id; let channel_id = *channel_id;
if payload
.channels
.iter()
.any(|channel| channel.id == channel_id)
{
continue;
}
if let Some(OpenedModelHandle::Open(buffer)) = if let Some(OpenedModelHandle::Open(buffer)) =
self.opened_buffers.remove(&channel_id) self.opened_buffers.remove(&channel_id)
{ {
@ -995,7 +995,16 @@ impl ChannelStore {
let mut index = self.channel_index.bulk_insert(); let mut index = self.channel_index.bulk_insert();
for channel in payload.channels { for channel in payload.channels {
index.insert(channel) let id = channel.id;
let channel_changed = index.insert(channel);
if channel_changed {
if let Some(OpenedModelHandle::Open(buffer)) = self.opened_buffers.get(&id) {
if let Some(buffer) = buffer.upgrade(cx) {
buffer.update(cx, ChannelBuffer::channel_changed);
}
}
}
} }
for unseen_buffer_change in payload.unseen_channel_buffer_changes { for unseen_buffer_change in payload.unseen_channel_buffer_changes {
@ -1023,16 +1032,6 @@ impl ChannelStore {
} }
} }
for permission in payload.channel_permissions {
if permission.role() == proto::ChannelRole::Admin {
self.channels_with_admin_privileges
.insert(permission.channel_id);
} else {
self.channels_with_admin_privileges
.remove(&permission.channel_id);
}
}
cx.notify(); cx.notify();
if payload.channel_participants.is_empty() { if payload.channel_participants.is_empty() {
return None; return None;

View file

@ -26,10 +26,8 @@ impl ChannelIndex {
pub fn delete_channels(&mut self, channels: &[ChannelId]) { pub fn delete_channels(&mut self, channels: &[ChannelId]) {
self.channels_by_id self.channels_by_id
.retain(|channel_id, _| !channels.contains(channel_id)); .retain(|channel_id, _| !channels.contains(channel_id));
self.paths.retain(|path| { self.paths
path.iter() .retain(|path| !path.iter().any(|channel_id| channels.contains(channel_id)));
.all(|channel_id| self.channels_by_id.contains_key(channel_id))
});
} }
pub fn bulk_insert(&mut self) -> ChannelPathsInsertGuard { pub fn bulk_insert(&mut self) -> ChannelPathsInsertGuard {
@ -121,10 +119,17 @@ impl<'a> ChannelPathsInsertGuard<'a> {
insert_new_message(&mut self.channels_by_id, channel_id, message_id) insert_new_message(&mut self.channels_by_id, channel_id, message_id)
} }
pub fn insert(&mut self, channel_proto: proto::Channel) { pub fn insert(&mut self, channel_proto: proto::Channel) -> bool {
let mut ret = false;
if let Some(existing_channel) = self.channels_by_id.get_mut(&channel_proto.id) { if let Some(existing_channel) = self.channels_by_id.get_mut(&channel_proto.id) {
let existing_channel = Arc::make_mut(existing_channel); let existing_channel = Arc::make_mut(existing_channel);
ret = existing_channel.visibility != channel_proto.visibility()
|| existing_channel.role != channel_proto.role()
|| existing_channel.name != channel_proto.name;
existing_channel.visibility = channel_proto.visibility(); existing_channel.visibility = channel_proto.visibility();
existing_channel.role = channel_proto.role();
existing_channel.name = channel_proto.name; existing_channel.name = channel_proto.name;
} else { } else {
self.channels_by_id.insert( self.channels_by_id.insert(
@ -132,6 +137,7 @@ impl<'a> ChannelPathsInsertGuard<'a> {
Arc::new(Channel { Arc::new(Channel {
id: channel_proto.id, id: channel_proto.id,
visibility: channel_proto.visibility(), visibility: channel_proto.visibility(),
role: channel_proto.role(),
name: channel_proto.name, name: channel_proto.name,
unseen_note_version: None, unseen_note_version: None,
unseen_message_id: None, unseen_message_id: None,
@ -139,6 +145,7 @@ impl<'a> ChannelPathsInsertGuard<'a> {
); );
self.insert_root(channel_proto.id); self.insert_root(channel_proto.id);
} }
ret
} }
pub fn insert_edge(&mut self, channel_id: ChannelId, parent_id: ChannelId) { pub fn insert_edge(&mut self, channel_id: ChannelId, parent_id: ChannelId) {

View file

@ -19,17 +19,15 @@ fn test_update_channels(cx: &mut AppContext) {
id: 1, id: 1,
name: "b".to_string(), name: "b".to_string(),
visibility: proto::ChannelVisibility::Members as i32, visibility: proto::ChannelVisibility::Members as i32,
role: proto::ChannelRole::Admin.into(),
}, },
proto::Channel { proto::Channel {
id: 2, id: 2,
name: "a".to_string(), name: "a".to_string(),
visibility: proto::ChannelVisibility::Members as i32, visibility: proto::ChannelVisibility::Members as i32,
role: proto::ChannelRole::Member.into(),
}, },
], ],
channel_permissions: vec![proto::ChannelPermission {
channel_id: 1,
role: proto::ChannelRole::Admin.into(),
}],
..Default::default() ..Default::default()
}, },
cx, cx,
@ -38,8 +36,8 @@ fn test_update_channels(cx: &mut AppContext) {
&channel_store, &channel_store,
&[ &[
// //
(0, "a".to_string(), false), (0, "a".to_string(), proto::ChannelRole::Member),
(0, "b".to_string(), true), (0, "b".to_string(), proto::ChannelRole::Admin),
], ],
cx, cx,
); );
@ -52,11 +50,13 @@ fn test_update_channels(cx: &mut AppContext) {
id: 3, id: 3,
name: "x".to_string(), name: "x".to_string(),
visibility: proto::ChannelVisibility::Members as i32, visibility: proto::ChannelVisibility::Members as i32,
role: proto::ChannelRole::Admin.into(),
}, },
proto::Channel { proto::Channel {
id: 4, id: 4,
name: "y".to_string(), name: "y".to_string(),
visibility: proto::ChannelVisibility::Members as i32, visibility: proto::ChannelVisibility::Members as i32,
role: proto::ChannelRole::Member.into(),
}, },
], ],
insert_edge: vec![ insert_edge: vec![
@ -76,10 +76,10 @@ fn test_update_channels(cx: &mut AppContext) {
assert_channels( assert_channels(
&channel_store, &channel_store,
&[ &[
(0, "a".to_string(), false), (0, "a".to_string(), proto::ChannelRole::Member),
(1, "y".to_string(), false), (1, "y".to_string(), proto::ChannelRole::Member),
(0, "b".to_string(), true), (0, "b".to_string(), proto::ChannelRole::Admin),
(1, "x".to_string(), true), (1, "x".to_string(), proto::ChannelRole::Admin),
], ],
cx, cx,
); );
@ -97,16 +97,19 @@ fn test_dangling_channel_paths(cx: &mut AppContext) {
id: 0, id: 0,
name: "a".to_string(), name: "a".to_string(),
visibility: proto::ChannelVisibility::Members as i32, visibility: proto::ChannelVisibility::Members as i32,
role: proto::ChannelRole::Admin.into(),
}, },
proto::Channel { proto::Channel {
id: 1, id: 1,
name: "b".to_string(), name: "b".to_string(),
visibility: proto::ChannelVisibility::Members as i32, visibility: proto::ChannelVisibility::Members as i32,
role: proto::ChannelRole::Admin.into(),
}, },
proto::Channel { proto::Channel {
id: 2, id: 2,
name: "c".to_string(), name: "c".to_string(),
visibility: proto::ChannelVisibility::Members as i32, visibility: proto::ChannelVisibility::Members as i32,
role: proto::ChannelRole::Admin.into(),
}, },
], ],
insert_edge: vec![ insert_edge: vec![
@ -119,10 +122,6 @@ fn test_dangling_channel_paths(cx: &mut AppContext) {
channel_id: 2, channel_id: 2,
}, },
], ],
channel_permissions: vec![proto::ChannelPermission {
channel_id: 0,
role: proto::ChannelRole::Admin.into(),
}],
..Default::default() ..Default::default()
}, },
cx, cx,
@ -132,9 +131,9 @@ fn test_dangling_channel_paths(cx: &mut AppContext) {
&channel_store, &channel_store,
&[ &[
// //
(0, "a".to_string(), true), (0, "a".to_string(), proto::ChannelRole::Admin),
(1, "b".to_string(), true), (1, "b".to_string(), proto::ChannelRole::Admin),
(2, "c".to_string(), true), (2, "c".to_string(), proto::ChannelRole::Admin),
], ],
cx, cx,
); );
@ -149,7 +148,11 @@ fn test_dangling_channel_paths(cx: &mut AppContext) {
); );
// Make sure that the 1/2/3 path is gone // Make sure that the 1/2/3 path is gone
assert_channels(&channel_store, &[(0, "a".to_string(), true)], cx); assert_channels(
&channel_store,
&[(0, "a".to_string(), proto::ChannelRole::Admin)],
cx,
);
} }
#[gpui::test] #[gpui::test]
@ -166,12 +169,17 @@ async fn test_channel_messages(cx: &mut TestAppContext) {
id: channel_id, id: channel_id,
name: "the-channel".to_string(), name: "the-channel".to_string(),
visibility: proto::ChannelVisibility::Members as i32, visibility: proto::ChannelVisibility::Members as i32,
role: proto::ChannelRole::Member.into(),
}], }],
..Default::default() ..Default::default()
}); });
cx.foreground().run_until_parked(); cx.foreground().run_until_parked();
cx.read(|cx| { cx.read(|cx| {
assert_channels(&channel_store, &[(0, "the-channel".to_string(), false)], cx); assert_channels(
&channel_store,
&[(0, "the-channel".to_string(), proto::ChannelRole::Member)],
cx,
);
}); });
let get_users = server.receive::<proto::GetUsers>().await.unwrap(); let get_users = server.receive::<proto::GetUsers>().await.unwrap();
@ -371,19 +379,13 @@ fn update_channels(
#[track_caller] #[track_caller]
fn assert_channels( fn assert_channels(
channel_store: &ModelHandle<ChannelStore>, channel_store: &ModelHandle<ChannelStore>,
expected_channels: &[(usize, String, bool)], expected_channels: &[(usize, String, proto::ChannelRole)],
cx: &AppContext, cx: &AppContext,
) { ) {
let actual = channel_store.read_with(cx, |store, _| { let actual = channel_store.read_with(cx, |store, _| {
store store
.channel_dag_entries() .channel_dag_entries()
.map(|(depth, channel)| { .map(|(depth, channel)| (depth, channel.name.to_string(), channel.role))
(
depth,
channel.name.to_string(),
store.is_user_admin(channel.id),
)
})
.collect::<Vec<_>>() .collect::<Vec<_>>()
}); });
assert_eq!(actual, expected_channels); assert_eq!(actual, expected_channels);

View file

@ -435,18 +435,103 @@ pub struct NewUserResult {
pub signup_device_id: Option<String>, pub signup_device_id: Option<String>,
} }
#[derive(Debug)]
pub struct MoveChannelResult {
pub participants_to_update: HashMap<UserId, ChannelsForUser>,
pub participants_to_remove: HashSet<UserId>,
pub moved_channels: HashSet<ChannelId>,
}
#[derive(Debug)]
pub struct RenameChannelResult {
pub channel: Channel,
pub participants_to_update: HashMap<UserId, Channel>,
}
#[derive(Debug)]
pub struct CreateChannelResult {
pub channel: Channel,
pub participants_to_update: Vec<(UserId, ChannelsForUser)>,
}
#[derive(Debug)]
pub struct SetChannelVisibilityResult {
pub participants_to_update: HashMap<UserId, ChannelsForUser>,
pub participants_to_remove: HashSet<UserId>,
pub channels_to_remove: Vec<ChannelId>,
}
#[derive(Debug)]
pub struct MembershipUpdated {
pub channel_id: ChannelId,
pub new_channels: ChannelsForUser,
pub removed_channels: Vec<ChannelId>,
}
#[derive(Debug)]
pub enum SetMemberRoleResult {
InviteUpdated(Channel),
MembershipUpdated(MembershipUpdated),
}
#[derive(Debug)]
pub struct InviteMemberResult {
pub channel: Channel,
pub notifications: NotificationBatch,
}
#[derive(Debug)]
pub struct RespondToChannelInvite {
pub membership_update: Option<MembershipUpdated>,
pub notifications: NotificationBatch,
}
#[derive(Debug)]
pub struct RemoveChannelMemberResult {
pub membership_update: MembershipUpdated,
pub notification_id: Option<NotificationId>,
}
#[derive(FromQueryResult, Debug, PartialEq, Eq, Hash)] #[derive(FromQueryResult, Debug, PartialEq, Eq, Hash)]
pub struct Channel { pub struct Channel {
pub id: ChannelId, pub id: ChannelId,
pub name: String, pub name: String,
pub visibility: ChannelVisibility, pub visibility: ChannelVisibility,
pub role: ChannelRole,
}
impl Channel {
pub fn to_proto(&self) -> proto::Channel {
proto::Channel {
id: self.id.to_proto(),
name: self.name.clone(),
visibility: self.visibility.into(),
role: self.role.into(),
}
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ChannelMember {
pub role: ChannelRole,
pub user_id: UserId,
pub kind: proto::channel_member::Kind,
}
impl ChannelMember {
pub fn to_proto(&self) -> proto::ChannelMember {
proto::ChannelMember {
role: self.role.into(),
user_id: self.user_id.to_proto(),
kind: self.kind.into(),
}
}
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct ChannelsForUser { 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 unseen_buffer_changes: Vec<proto::UnseenChannelBufferChange>, pub unseen_buffer_changes: Vec<proto::UnseenChannelBufferChange>,
pub channel_messages: Vec<proto::UnseenChannelMessage>, pub channel_messages: Vec<proto::UnseenChannelMessage>,
} }

View file

@ -84,7 +84,7 @@ id_type!(FlagId);
id_type!(NotificationId); id_type!(NotificationId);
id_type!(NotificationKindId); id_type!(NotificationKindId);
#[derive(Eq, PartialEq, Copy, Clone, Debug, EnumIter, DeriveActiveEnum, Default)] #[derive(Eq, PartialEq, Copy, Clone, Debug, EnumIter, DeriveActiveEnum, Default, Hash)]
#[sea_orm(rs_type = "String", db_type = "String(None)")] #[sea_orm(rs_type = "String", db_type = "String(None)")]
pub enum ChannelRole { pub enum ChannelRole {
#[sea_orm(string_value = "admin")] #[sea_orm(string_value = "admin")]
@ -116,6 +116,22 @@ impl ChannelRole {
other other
} }
} }
pub fn can_see_all_descendants(&self) -> bool {
use ChannelRole::*;
match self {
Admin | Member => true,
Guest | Banned => false,
}
}
pub fn can_only_see_public_descendants(&self) -> bool {
use ChannelRole::*;
match self {
Guest => true,
Admin | Member | Banned => false,
}
}
} }
impl From<proto::ChannelRole> for ChannelRole { impl From<proto::ChannelRole> for ChannelRole {

View file

@ -16,7 +16,7 @@ impl Database {
connection: ConnectionId, connection: ConnectionId,
) -> Result<proto::JoinChannelBufferResponse> { ) -> Result<proto::JoinChannelBufferResponse> {
self.transaction(|tx| async move { self.transaction(|tx| async move {
self.check_user_is_channel_member(channel_id, user_id, &tx) self.check_user_is_channel_participant(channel_id, user_id, &tx)
.await?; .await?;
let buffer = channel::Model { let buffer = channel::Model {
@ -131,7 +131,7 @@ impl Database {
for client_buffer in buffers { for client_buffer in buffers {
let channel_id = ChannelId::from_proto(client_buffer.channel_id); let channel_id = ChannelId::from_proto(client_buffer.channel_id);
if self if self
.check_user_is_channel_member(channel_id, user_id, &*tx) .check_user_is_channel_participant(channel_id, user_id, &*tx)
.await .await
.is_err() .is_err()
{ {
@ -482,9 +482,7 @@ impl Database {
) )
.await?; .await?;
channel_members = self channel_members = self.get_channel_participants(channel_id, &*tx).await?;
.get_channel_participants_internal(channel_id, &*tx)
.await?;
let collaborators = self let collaborators = self
.get_channel_buffer_collaborators_internal(channel_id, &*tx) .get_channel_buffer_collaborators_internal(channel_id, &*tx)
.await?; .await?;

File diff suppressed because it is too large Load diff

View file

@ -12,7 +12,7 @@ impl Database {
user_id: UserId, user_id: UserId,
) -> Result<()> { ) -> Result<()> {
self.transaction(|tx| async move { self.transaction(|tx| async move {
self.check_user_is_channel_member(channel_id, user_id, &*tx) self.check_user_is_channel_participant(channel_id, user_id, &*tx)
.await?; .await?;
channel_chat_participant::ActiveModel { channel_chat_participant::ActiveModel {
id: ActiveValue::NotSet, id: ActiveValue::NotSet,
@ -80,7 +80,7 @@ impl Database {
before_message_id: Option<MessageId>, before_message_id: Option<MessageId>,
) -> Result<Vec<proto::ChannelMessage>> { ) -> Result<Vec<proto::ChannelMessage>> {
self.transaction(|tx| async move { self.transaction(|tx| async move {
self.check_user_is_channel_member(channel_id, user_id, &*tx) self.check_user_is_channel_participant(channel_id, user_id, &*tx)
.await?; .await?;
let mut condition = let mut condition =
@ -203,6 +203,9 @@ impl Database {
nonce: u128, nonce: u128,
) -> Result<CreatedChannelMessage> { ) -> Result<CreatedChannelMessage> {
self.transaction(|tx| async move { self.transaction(|tx| async move {
self.check_user_is_channel_participant(channel_id, user_id, &*tx)
.await?;
let mut rows = channel_chat_participant::Entity::find() let mut rows = channel_chat_participant::Entity::find()
.filter(channel_chat_participant::Column::ChannelId.eq(channel_id)) .filter(channel_chat_participant::Column::ChannelId.eq(channel_id))
.stream(&*tx) .stream(&*tx)
@ -307,9 +310,7 @@ impl Database {
} }
} }
let mut channel_members = self let mut channel_members = self.get_channel_participants(channel_id, &*tx).await?;
.get_channel_participants_internal(channel_id, &*tx)
.await?;
channel_members.retain(|member| !participant_user_ids.contains(member)); channel_members.retain(|member| !participant_user_ids.contains(member));
Ok(CreatedChannelMessage { Ok(CreatedChannelMessage {

View file

@ -53,9 +53,7 @@ impl Database {
let (channel_id, room) = self.get_channel_room(room_id, &tx).await?; let (channel_id, room) = self.get_channel_room(room_id, &tx).await?;
let channel_members; let channel_members;
if let Some(channel_id) = channel_id { if let Some(channel_id) = channel_id {
channel_members = self channel_members = self.get_channel_participants(channel_id, &tx).await?;
.get_channel_participants_internal(channel_id, &tx)
.await?;
} else { } else {
channel_members = Vec::new(); channel_members = Vec::new();
@ -423,9 +421,7 @@ impl Database {
.await?; .await?;
let room = self.get_room(room_id, &tx).await?; let room = self.get_room(room_id, &tx).await?;
let channel_members = self let channel_members = self.get_channel_participants(channel_id, &tx).await?;
.get_channel_participants_internal(channel_id, &tx)
.await?;
Ok(JoinRoom { Ok(JoinRoom {
room, room,
channel_id: Some(channel_id), channel_id: Some(channel_id),
@ -724,8 +720,7 @@ impl Database {
let (channel_id, room) = self.get_channel_room(room_id, &tx).await?; let (channel_id, room) = self.get_channel_room(room_id, &tx).await?;
let channel_members = if let Some(channel_id) = channel_id { let channel_members = if let Some(channel_id) = channel_id {
self.get_channel_participants_internal(channel_id, &tx) self.get_channel_participants(channel_id, &tx).await?
.await?
} else { } else {
Vec::new() Vec::new()
}; };
@ -883,8 +878,7 @@ impl Database {
}; };
let channel_members = if let Some(channel_id) = channel_id { let channel_members = if let Some(channel_id) = channel_id {
self.get_channel_participants_internal(channel_id, &tx) self.get_channel_participants(channel_id, &tx).await?
.await?
} else { } else {
Vec::new() Vec::new()
}; };

View file

@ -11,7 +11,7 @@ use rpc::proto::ChannelEdge;
use sea_orm::ConnectionTrait; use sea_orm::ConnectionTrait;
use sqlx::migrate::MigrateDatabase; use sqlx::migrate::MigrateDatabase;
use std::sync::{ use std::sync::{
atomic::{AtomicI32, Ordering::SeqCst}, atomic::{AtomicI32, AtomicU32, Ordering::SeqCst},
Arc, Arc,
}; };
@ -154,17 +154,21 @@ impl Drop for TestDb {
} }
/// The second tuples are (channel_id, parent) /// The second tuples are (channel_id, parent)
fn graph(channels: &[(ChannelId, &'static str)], edges: &[(ChannelId, ChannelId)]) -> ChannelGraph { fn graph(
channels: &[(ChannelId, &'static str, ChannelRole)],
edges: &[(ChannelId, ChannelId)],
) -> ChannelGraph {
let mut graph = ChannelGraph { let mut graph = ChannelGraph {
channels: vec![], channels: vec![],
edges: vec![], edges: vec![],
}; };
for (id, name) in channels { for (id, name, role) in channels {
graph.channels.push(Channel { graph.channels.push(Channel {
id: *id, id: *id,
name: name.to_string(), name: name.to_string(),
visibility: ChannelVisibility::Members, visibility: ChannelVisibility::Members,
role: *role,
}) })
} }
@ -193,3 +197,11 @@ async fn new_test_user(db: &Arc<Database>, email: &str) -> UserId {
.unwrap() .unwrap()
.user_id .user_id
} }
static TEST_CONNECTION_ID: AtomicU32 = AtomicU32::new(1);
fn new_test_connection(server: ServerId) -> ConnectionId {
ConnectionId {
id: TEST_CONNECTION_ID.fetch_add(1, SeqCst),
owner_id: server.0 as u32,
}
}

View file

@ -1,7 +1,9 @@
use std::sync::Arc;
use crate::{ use crate::{
db::{ db::{
queries::channels::ChannelGraph, queries::channels::ChannelGraph,
tests::{graph, new_test_user, TEST_RELEASE_CHANNEL}, tests::{graph, new_test_connection, new_test_user, TEST_RELEASE_CHANNEL},
ChannelId, ChannelRole, Database, NewUserParams, RoomId, ChannelId, ChannelRole, Database, NewUserParams, RoomId,
}, },
test_both_dbs, test_both_dbs,
@ -11,36 +13,12 @@ use rpc::{
proto::{self}, proto::{self},
ConnectionId, ConnectionId,
}; };
use std::sync::Arc;
test_both_dbs!(test_channels, test_channels_postgres, test_channels_sqlite); test_both_dbs!(test_channels, test_channels_postgres, test_channels_sqlite);
async fn test_channels(db: &Arc<Database>) { async fn test_channels(db: &Arc<Database>) {
let a_id = db let a_id = new_test_user(db, "user1@example.com").await;
.create_user( let b_id = new_test_user(db, "user2@example.com").await;
"user1@example.com",
false,
NewUserParams {
github_login: "user1".into(),
github_user_id: 5,
},
)
.await
.unwrap()
.user_id;
let b_id = db
.create_user(
"user2@example.com",
false,
NewUserParams {
github_login: "user2".into(),
github_user_id: 6,
},
)
.await
.unwrap()
.user_id;
let zed_id = db.create_root_channel("zed", a_id).await.unwrap(); let zed_id = db.create_root_channel("zed", a_id).await.unwrap();
@ -55,28 +33,28 @@ async fn test_channels(db: &Arc<Database>) {
.await .await
.unwrap(); .unwrap();
let crdb_id = db.create_channel("crdb", Some(zed_id), a_id).await.unwrap(); let crdb_id = db.create_sub_channel("crdb", zed_id, a_id).await.unwrap();
let livestreaming_id = db let livestreaming_id = db
.create_channel("livestreaming", Some(zed_id), a_id) .create_sub_channel("livestreaming", zed_id, a_id)
.await .await
.unwrap(); .unwrap();
let replace_id = db let replace_id = db
.create_channel("replace", Some(zed_id), a_id) .create_sub_channel("replace", zed_id, a_id)
.await .await
.unwrap(); .unwrap();
let mut members = db.get_channel_members(replace_id).await.unwrap(); let mut members = db
.transaction(|tx| async move { Ok(db.get_channel_participants(replace_id, &*tx).await?) })
.await
.unwrap();
members.sort(); members.sort();
assert_eq!(members, &[a_id, b_id]); assert_eq!(members, &[a_id, b_id]);
let rust_id = db.create_root_channel("rust", a_id).await.unwrap(); let rust_id = db.create_root_channel("rust", a_id).await.unwrap();
let cargo_id = db let cargo_id = db.create_sub_channel("cargo", rust_id, a_id).await.unwrap();
.create_channel("cargo", Some(rust_id), a_id)
.await
.unwrap();
let cargo_ra_id = db let cargo_ra_id = db
.create_channel("cargo-ra", Some(cargo_id), a_id) .create_sub_channel("cargo-ra", cargo_id, a_id)
.await .await
.unwrap(); .unwrap();
@ -85,13 +63,13 @@ async fn test_channels(db: &Arc<Database>) {
result.channels, result.channels,
graph( graph(
&[ &[
(zed_id, "zed"), (zed_id, "zed", ChannelRole::Admin),
(crdb_id, "crdb"), (crdb_id, "crdb", ChannelRole::Admin),
(livestreaming_id, "livestreaming"), (livestreaming_id, "livestreaming", ChannelRole::Admin),
(replace_id, "replace"), (replace_id, "replace", ChannelRole::Admin),
(rust_id, "rust"), (rust_id, "rust", ChannelRole::Admin),
(cargo_id, "cargo"), (cargo_id, "cargo", ChannelRole::Admin),
(cargo_ra_id, "cargo-ra") (cargo_ra_id, "cargo-ra", ChannelRole::Admin)
], ],
&[ &[
(crdb_id, zed_id), (crdb_id, zed_id),
@ -108,10 +86,10 @@ async fn test_channels(db: &Arc<Database>) {
result.channels, result.channels,
graph( graph(
&[ &[
(zed_id, "zed"), (zed_id, "zed", ChannelRole::Member),
(crdb_id, "crdb"), (crdb_id, "crdb", ChannelRole::Member),
(livestreaming_id, "livestreaming"), (livestreaming_id, "livestreaming", ChannelRole::Member),
(replace_id, "replace") (replace_id, "replace", ChannelRole::Member)
], ],
&[ &[
(crdb_id, zed_id), (crdb_id, zed_id),
@ -136,10 +114,10 @@ async fn test_channels(db: &Arc<Database>) {
result.channels, result.channels,
graph( graph(
&[ &[
(zed_id, "zed"), (zed_id, "zed", ChannelRole::Admin),
(crdb_id, "crdb"), (crdb_id, "crdb", ChannelRole::Admin),
(livestreaming_id, "livestreaming"), (livestreaming_id, "livestreaming", ChannelRole::Admin),
(replace_id, "replace") (replace_id, "replace", ChannelRole::Admin)
], ],
&[ &[
(crdb_id, zed_id), (crdb_id, zed_id),
@ -173,35 +151,13 @@ test_both_dbs!(
async fn test_joining_channels(db: &Arc<Database>) { async fn test_joining_channels(db: &Arc<Database>) {
let owner_id = db.create_server("test").await.unwrap().0 as u32; let owner_id = db.create_server("test").await.unwrap().0 as u32;
let user_1 = db let user_1 = new_test_user(db, "user1@example.com").await;
.create_user( let user_2 = new_test_user(db, "user2@example.com").await;
"user1@example.com",
false,
NewUserParams {
github_login: "user1".into(),
github_user_id: 5,
},
)
.await
.unwrap()
.user_id;
let user_2 = db
.create_user(
"user2@example.com",
false,
NewUserParams {
github_login: "user2".into(),
github_user_id: 6,
},
)
.await
.unwrap()
.user_id;
let channel_1 = db.create_root_channel("channel_1", user_1).await.unwrap(); let channel_1 = db.create_root_channel("channel_1", user_1).await.unwrap();
// can join a room with membership to its channel // can join a room with membership to its channel
let (joined_room, _) = db let (joined_room, _, _) = db
.join_channel( .join_channel(
channel_1, channel_1,
user_1, user_1,
@ -305,7 +261,7 @@ async fn test_channel_invites(db: &Arc<Database>) {
.unwrap(); .unwrap();
let channel_1_3 = db let channel_1_3 = db
.create_channel("channel_3", Some(channel_1_1), user_1) .create_sub_channel("channel_3", channel_1_1, user_1)
.await .await
.unwrap(); .unwrap();
@ -318,7 +274,7 @@ async fn test_channel_invites(db: &Arc<Database>) {
&[ &[
proto::ChannelMember { proto::ChannelMember {
user_id: user_1.to_proto(), user_id: user_1.to_proto(),
kind: proto::channel_member::Kind::Member.into(), kind: proto::channel_member::Kind::AncestorMember.into(),
role: proto::ChannelRole::Admin.into(), role: proto::ChannelRole::Admin.into(),
}, },
proto::ChannelMember { proto::ChannelMember {
@ -407,20 +363,17 @@ async fn test_db_channel_moving(db: &Arc<Database>) {
let zed_id = db.create_root_channel("zed", a_id).await.unwrap(); let zed_id = db.create_root_channel("zed", a_id).await.unwrap();
let crdb_id = db.create_channel("crdb", Some(zed_id), a_id).await.unwrap(); let crdb_id = db.create_sub_channel("crdb", zed_id, a_id).await.unwrap();
let gpui2_id = db let gpui2_id = db.create_sub_channel("gpui2", zed_id, a_id).await.unwrap();
.create_channel("gpui2", Some(zed_id), a_id)
.await
.unwrap();
let livestreaming_id = db let livestreaming_id = db
.create_channel("livestreaming", Some(crdb_id), a_id) .create_sub_channel("livestreaming", crdb_id, a_id)
.await .await
.unwrap(); .unwrap();
let livestreaming_dag_id = db let livestreaming_dag_id = db
.create_channel("livestreaming_dag", Some(livestreaming_id), a_id) .create_sub_channel("livestreaming_dag", livestreaming_id, a_id)
.await .await
.unwrap(); .unwrap();
@ -447,299 +400,311 @@ async fn test_db_channel_moving(db: &Arc<Database>) {
.await .await
.is_err()); .is_err());
// ======================================================================== // // ========================================================================
// Make a link // // Make a link
db.link_channel(a_id, livestreaming_id, zed_id) // db.link_channel(a_id, livestreaming_id, zed_id)
.await // .await
.unwrap(); // .unwrap();
// DAG is now: // // DAG is now:
// /- gpui2 // // /- gpui2
// zed -- crdb - livestreaming - livestreaming_dag // // zed -- crdb - livestreaming - livestreaming_dag
// \---------/ // // \---------/
let result = db.get_channels_for_user(a_id).await.unwrap(); // let result = db.get_channels_for_user(a_id).await.unwrap();
assert_dag( // assert_dag(
result.channels, // result.channels,
&[ // &[
(zed_id, None), // (zed_id, None),
(crdb_id, Some(zed_id)), // (crdb_id, Some(zed_id)),
(gpui2_id, Some(zed_id)), // (gpui2_id, Some(zed_id)),
(livestreaming_id, Some(zed_id)), // (livestreaming_id, Some(zed_id)),
(livestreaming_id, Some(crdb_id)), // (livestreaming_id, Some(crdb_id)),
(livestreaming_dag_id, Some(livestreaming_id)), // (livestreaming_dag_id, Some(livestreaming_id)),
], // ],
); // );
// ======================================================================== // // ========================================================================
// Create a new channel below a channel with multiple parents // // Create a new channel below a channel with multiple parents
let livestreaming_dag_sub_id = db // let livestreaming_dag_sub_id = db
.create_channel("livestreaming_dag_sub", Some(livestreaming_dag_id), a_id) // .create_channel("livestreaming_dag_sub", Some(livestreaming_dag_id), a_id)
.await // .await
.unwrap(); // .unwrap();
// DAG is now: // // DAG is now:
// /- gpui2 // // /- gpui2
// zed -- crdb - livestreaming - livestreaming_dag - livestreaming_dag_sub_id // // zed -- crdb - livestreaming - livestreaming_dag - livestreaming_dag_sub_id
// \---------/ // // \---------/
let result = db.get_channels_for_user(a_id).await.unwrap(); // let result = db.get_channels_for_user(a_id).await.unwrap();
assert_dag( // assert_dag(
result.channels, // result.channels,
&[ // &[
(zed_id, None), // (zed_id, None),
(crdb_id, Some(zed_id)), // (crdb_id, Some(zed_id)),
(gpui2_id, Some(zed_id)), // (gpui2_id, Some(zed_id)),
(livestreaming_id, Some(zed_id)), // (livestreaming_id, Some(zed_id)),
(livestreaming_id, Some(crdb_id)), // (livestreaming_id, Some(crdb_id)),
(livestreaming_dag_id, Some(livestreaming_id)), // (livestreaming_dag_id, Some(livestreaming_id)),
(livestreaming_dag_sub_id, Some(livestreaming_dag_id)), // (livestreaming_dag_sub_id, Some(livestreaming_dag_id)),
], // ],
); // );
// ======================================================================== // // ========================================================================
// Test a complex DAG by making another link // // Test a complex DAG by making another link
let returned_channels = db // let returned_channels = db
.link_channel(a_id, livestreaming_dag_sub_id, livestreaming_id) // .link_channel(a_id, livestreaming_dag_sub_id, livestreaming_id)
.await // .await
.unwrap(); // .unwrap();
// DAG is now: // // DAG is now:
// /- gpui2 /---------------------\ // // /- gpui2 /---------------------\
// zed - crdb - livestreaming - livestreaming_dag - livestreaming_dag_sub_id // // zed - crdb - livestreaming - livestreaming_dag - livestreaming_dag_sub_id
// \--------/ // // \--------/
// make sure we're getting just the new link // // make sure we're getting just the new link
// Not using the assert_dag helper because we want to make sure we're returning the full data // // Not using the assert_dag helper because we want to make sure we're returning the full data
pretty_assertions::assert_eq!( // pretty_assertions::assert_eq!(
returned_channels, // returned_channels,
graph( // graph(
&[(livestreaming_dag_sub_id, "livestreaming_dag_sub")], // &[(
&[(livestreaming_dag_sub_id, livestreaming_id)] // livestreaming_dag_sub_id,
) // "livestreaming_dag_sub",
); // ChannelRole::Admin
// )],
// &[(livestreaming_dag_sub_id, livestreaming_id)]
// )
// );
let result = db.get_channels_for_user(a_id).await.unwrap(); // let result = db.get_channels_for_user(a_id).await.unwrap();
assert_dag( // assert_dag(
result.channels, // result.channels,
&[ // &[
(zed_id, None), // (zed_id, None),
(crdb_id, Some(zed_id)), // (crdb_id, Some(zed_id)),
(gpui2_id, Some(zed_id)), // (gpui2_id, Some(zed_id)),
(livestreaming_id, Some(zed_id)), // (livestreaming_id, Some(zed_id)),
(livestreaming_id, Some(crdb_id)), // (livestreaming_id, Some(crdb_id)),
(livestreaming_dag_id, Some(livestreaming_id)), // (livestreaming_dag_id, Some(livestreaming_id)),
(livestreaming_dag_sub_id, Some(livestreaming_id)), // (livestreaming_dag_sub_id, Some(livestreaming_id)),
(livestreaming_dag_sub_id, Some(livestreaming_dag_id)), // (livestreaming_dag_sub_id, Some(livestreaming_dag_id)),
], // ],
); // );
// ======================================================================== // // ========================================================================
// Test a complex DAG by making another link // // Test a complex DAG by making another link
let returned_channels = db // let returned_channels = db
.link_channel(a_id, livestreaming_id, gpui2_id) // .link_channel(a_id, livestreaming_id, gpui2_id)
.await // .await
.unwrap(); // .unwrap();
// DAG is now: // // DAG is now:
// /- gpui2 -\ /---------------------\ // // /- gpui2 -\ /---------------------\
// zed - crdb -- livestreaming - livestreaming_dag - livestreaming_dag_sub_id // // zed - crdb -- livestreaming - livestreaming_dag - livestreaming_dag_sub_id
// \---------/ // // \---------/
// Make sure that we're correctly getting the full sub-dag // // Make sure that we're correctly getting the full sub-dag
pretty_assertions::assert_eq!( // pretty_assertions::assert_eq!(
returned_channels, // returned_channels,
graph( // graph(
&[ // &[
(livestreaming_id, "livestreaming"), // (livestreaming_id, "livestreaming", ChannelRole::Admin),
(livestreaming_dag_id, "livestreaming_dag"), // (
(livestreaming_dag_sub_id, "livestreaming_dag_sub"), // livestreaming_dag_id,
], // "livestreaming_dag",
&[ // ChannelRole::Admin
(livestreaming_id, gpui2_id), // ),
(livestreaming_dag_id, livestreaming_id), // (
(livestreaming_dag_sub_id, livestreaming_id), // livestreaming_dag_sub_id,
(livestreaming_dag_sub_id, livestreaming_dag_id), // "livestreaming_dag_sub",
] // ChannelRole::Admin
) // ),
); // ],
// &[
// (livestreaming_id, gpui2_id),
// (livestreaming_dag_id, livestreaming_id),
// (livestreaming_dag_sub_id, livestreaming_id),
// (livestreaming_dag_sub_id, livestreaming_dag_id),
// ]
// )
// );
let result = db.get_channels_for_user(a_id).await.unwrap(); // let result = db.get_channels_for_user(a_id).await.unwrap();
assert_dag( // assert_dag(
result.channels, // result.channels,
&[ // &[
(zed_id, None), // (zed_id, None),
(crdb_id, Some(zed_id)), // (crdb_id, Some(zed_id)),
(gpui2_id, Some(zed_id)), // (gpui2_id, Some(zed_id)),
(livestreaming_id, Some(zed_id)), // (livestreaming_id, Some(zed_id)),
(livestreaming_id, Some(crdb_id)), // (livestreaming_id, Some(crdb_id)),
(livestreaming_id, Some(gpui2_id)), // (livestreaming_id, Some(gpui2_id)),
(livestreaming_dag_id, Some(livestreaming_id)), // (livestreaming_dag_id, Some(livestreaming_id)),
(livestreaming_dag_sub_id, Some(livestreaming_id)), // (livestreaming_dag_sub_id, Some(livestreaming_id)),
(livestreaming_dag_sub_id, Some(livestreaming_dag_id)), // (livestreaming_dag_sub_id, Some(livestreaming_dag_id)),
], // ],
); // );
// ======================================================================== // // ========================================================================
// Test unlinking in a complex DAG by removing the inner link // // Test unlinking in a complex DAG by removing the inner link
db.unlink_channel(a_id, livestreaming_dag_sub_id, livestreaming_id) // db.unlink_channel(a_id, livestreaming_dag_sub_id, livestreaming_id)
.await // .await
.unwrap(); // .unwrap();
// DAG is now: // // DAG is now:
// /- gpui2 -\ // // /- gpui2 -\
// zed - crdb -- livestreaming - livestreaming_dag - livestreaming_dag_sub // // zed - crdb -- livestreaming - livestreaming_dag - livestreaming_dag_sub
// \---------/ // // \---------/
let result = db.get_channels_for_user(a_id).await.unwrap(); // let result = db.get_channels_for_user(a_id).await.unwrap();
assert_dag( // assert_dag(
result.channels, // result.channels,
&[ // &[
(zed_id, None), // (zed_id, None),
(crdb_id, Some(zed_id)), // (crdb_id, Some(zed_id)),
(gpui2_id, Some(zed_id)), // (gpui2_id, Some(zed_id)),
(livestreaming_id, Some(gpui2_id)), // (livestreaming_id, Some(gpui2_id)),
(livestreaming_id, Some(zed_id)), // (livestreaming_id, Some(zed_id)),
(livestreaming_id, Some(crdb_id)), // (livestreaming_id, Some(crdb_id)),
(livestreaming_dag_id, Some(livestreaming_id)), // (livestreaming_dag_id, Some(livestreaming_id)),
(livestreaming_dag_sub_id, Some(livestreaming_dag_id)), // (livestreaming_dag_sub_id, Some(livestreaming_dag_id)),
], // ],
); // );
// ======================================================================== // // ========================================================================
// Test unlinking in a complex DAG by removing the inner link // // Test unlinking in a complex DAG by removing the inner link
db.unlink_channel(a_id, livestreaming_id, gpui2_id) // db.unlink_channel(a_id, livestreaming_id, gpui2_id)
.await // .await
.unwrap(); // .unwrap();
// DAG is now: // // DAG is now:
// /- gpui2 // // /- gpui2
// zed - crdb -- livestreaming - livestreaming_dag - livestreaming_dag_sub // // zed - crdb -- livestreaming - livestreaming_dag - livestreaming_dag_sub
// \---------/ // // \---------/
let result = db.get_channels_for_user(a_id).await.unwrap(); // let result = db.get_channels_for_user(a_id).await.unwrap();
assert_dag( // assert_dag(
result.channels, // result.channels,
&[ // &[
(zed_id, None), // (zed_id, None),
(crdb_id, Some(zed_id)), // (crdb_id, Some(zed_id)),
(gpui2_id, Some(zed_id)), // (gpui2_id, Some(zed_id)),
(livestreaming_id, Some(zed_id)), // (livestreaming_id, Some(zed_id)),
(livestreaming_id, Some(crdb_id)), // (livestreaming_id, Some(crdb_id)),
(livestreaming_dag_id, Some(livestreaming_id)), // (livestreaming_dag_id, Some(livestreaming_id)),
(livestreaming_dag_sub_id, Some(livestreaming_dag_id)), // (livestreaming_dag_sub_id, Some(livestreaming_dag_id)),
], // ],
); // );
// ======================================================================== // // ========================================================================
// Test moving DAG nodes by moving livestreaming to be below gpui2 // // Test moving DAG nodes by moving livestreaming to be below gpui2
db.move_channel(a_id, livestreaming_id, crdb_id, gpui2_id) // db.move_channel(livestreaming_id, Some(crdb_id), gpui2_id, a_id)
.await // .await
.unwrap(); // .unwrap();
// DAG is now: // // DAG is now:
// /- gpui2 -- livestreaming - livestreaming_dag - livestreaming_dag_sub // // /- gpui2 -- livestreaming - livestreaming_dag - livestreaming_dag_sub
// zed - crdb / // // zed - crdb /
// \---------/ // // \---------/
let result = db.get_channels_for_user(a_id).await.unwrap(); // let result = db.get_channels_for_user(a_id).await.unwrap();
assert_dag( // assert_dag(
result.channels, // result.channels,
&[ // &[
(zed_id, None), // (zed_id, None),
(crdb_id, Some(zed_id)), // (crdb_id, Some(zed_id)),
(gpui2_id, Some(zed_id)), // (gpui2_id, Some(zed_id)),
(livestreaming_id, Some(zed_id)), // (livestreaming_id, Some(zed_id)),
(livestreaming_id, Some(gpui2_id)), // (livestreaming_id, Some(gpui2_id)),
(livestreaming_dag_id, Some(livestreaming_id)), // (livestreaming_dag_id, Some(livestreaming_id)),
(livestreaming_dag_sub_id, Some(livestreaming_dag_id)), // (livestreaming_dag_sub_id, Some(livestreaming_dag_id)),
], // ],
); // );
// ======================================================================== // // ========================================================================
// Deleting a channel should not delete children that still have other parents // // Deleting a channel should not delete children that still have other parents
db.delete_channel(gpui2_id, a_id).await.unwrap(); // db.delete_channel(gpui2_id, a_id).await.unwrap();
// DAG is now: // // DAG is now:
// zed - crdb // // zed - crdb
// \- livestreaming - livestreaming_dag - livestreaming_dag_sub // // \- livestreaming - livestreaming_dag - livestreaming_dag_sub
let result = db.get_channels_for_user(a_id).await.unwrap(); // let result = db.get_channels_for_user(a_id).await.unwrap();
assert_dag( // assert_dag(
result.channels, // result.channels,
&[ // &[
(zed_id, None), // (zed_id, None),
(crdb_id, Some(zed_id)), // (crdb_id, Some(zed_id)),
(livestreaming_id, Some(zed_id)), // (livestreaming_id, Some(zed_id)),
(livestreaming_dag_id, Some(livestreaming_id)), // (livestreaming_dag_id, Some(livestreaming_id)),
(livestreaming_dag_sub_id, Some(livestreaming_dag_id)), // (livestreaming_dag_sub_id, Some(livestreaming_dag_id)),
], // ],
); // );
// ======================================================================== // // ========================================================================
// Unlinking a channel from it's parent should automatically promote it to a root channel // // Unlinking a channel from it's parent should automatically promote it to a root channel
db.unlink_channel(a_id, crdb_id, zed_id).await.unwrap(); // db.unlink_channel(a_id, crdb_id, zed_id).await.unwrap();
// DAG is now: // // DAG is now:
// crdb // // crdb
// zed // // zed
// \- livestreaming - livestreaming_dag - livestreaming_dag_sub // // \- livestreaming - livestreaming_dag - livestreaming_dag_sub
let result = db.get_channels_for_user(a_id).await.unwrap(); // let result = db.get_channels_for_user(a_id).await.unwrap();
assert_dag( // assert_dag(
result.channels, // result.channels,
&[ // &[
(zed_id, None), // (zed_id, None),
(crdb_id, None), // (crdb_id, None),
(livestreaming_id, Some(zed_id)), // (livestreaming_id, Some(zed_id)),
(livestreaming_dag_id, Some(livestreaming_id)), // (livestreaming_dag_id, Some(livestreaming_id)),
(livestreaming_dag_sub_id, Some(livestreaming_dag_id)), // (livestreaming_dag_sub_id, Some(livestreaming_dag_id)),
], // ],
); // );
// ======================================================================== // // ========================================================================
// You should be able to move a root channel into a non-root channel // // You should be able to move a root channel into a non-root channel
db.link_channel(a_id, crdb_id, zed_id).await.unwrap(); // db.link_channel(a_id, crdb_id, zed_id).await.unwrap();
// DAG is now: // // DAG is now:
// zed - crdb // // zed - crdb
// \- livestreaming - livestreaming_dag - livestreaming_dag_sub // // \- livestreaming - livestreaming_dag - livestreaming_dag_sub
let result = db.get_channels_for_user(a_id).await.unwrap(); // let result = db.get_channels_for_user(a_id).await.unwrap();
assert_dag( // assert_dag(
result.channels, // result.channels,
&[ // &[
(zed_id, None), // (zed_id, None),
(crdb_id, Some(zed_id)), // (crdb_id, Some(zed_id)),
(livestreaming_id, Some(zed_id)), // (livestreaming_id, Some(zed_id)),
(livestreaming_dag_id, Some(livestreaming_id)), // (livestreaming_dag_id, Some(livestreaming_id)),
(livestreaming_dag_sub_id, Some(livestreaming_dag_id)), // (livestreaming_dag_sub_id, Some(livestreaming_dag_id)),
], // ],
); // );
// ======================================================================== // // ========================================================================
// Prep for DAG deletion test // // Prep for DAG deletion test
db.link_channel(a_id, livestreaming_id, crdb_id) // db.link_channel(a_id, livestreaming_id, crdb_id)
.await // .await
.unwrap(); // .unwrap();
// DAG is now: // // DAG is now:
// zed - crdb - livestreaming - livestreaming_dag - livestreaming_dag_sub // // zed - crdb - livestreaming - livestreaming_dag - livestreaming_dag_sub
// \--------/ // // \--------/
let result = db.get_channels_for_user(a_id).await.unwrap(); // let result = db.get_channels_for_user(a_id).await.unwrap();
assert_dag( // assert_dag(
result.channels, // result.channels,
&[ // &[
(zed_id, None), // (zed_id, None),
(crdb_id, Some(zed_id)), // (crdb_id, Some(zed_id)),
(livestreaming_id, Some(zed_id)), // (livestreaming_id, Some(zed_id)),
(livestreaming_id, Some(crdb_id)), // (livestreaming_id, Some(crdb_id)),
(livestreaming_dag_id, Some(livestreaming_id)), // (livestreaming_dag_id, Some(livestreaming_id)),
(livestreaming_dag_sub_id, Some(livestreaming_dag_id)), // (livestreaming_dag_sub_id, Some(livestreaming_dag_id)),
], // ],
); // );
// Deleting the parent of a DAG should delete the whole DAG: // // Deleting the parent of a DAG should delete the whole DAG:
db.delete_channel(zed_id, a_id).await.unwrap(); // db.delete_channel(zed_id, a_id).await.unwrap();
let result = db.get_channels_for_user(a_id).await.unwrap(); // let result = db.get_channels_for_user(a_id).await.unwrap();
assert!(result.channels.is_empty()) // assert!(result.channels.is_empty())
} }
test_both_dbs!( test_both_dbs!(
@ -765,12 +730,12 @@ async fn test_db_channel_moving_bugs(db: &Arc<Database>) {
let zed_id = db.create_root_channel("zed", user_id).await.unwrap(); let zed_id = db.create_root_channel("zed", user_id).await.unwrap();
let projects_id = db let projects_id = db
.create_channel("projects", Some(zed_id), user_id) .create_sub_channel("projects", zed_id, user_id)
.await .await
.unwrap(); .unwrap();
let livestreaming_id = db let livestreaming_id = db
.create_channel("livestreaming", Some(projects_id), user_id) .create_sub_channel("livestreaming", projects_id, user_id)
.await .await
.unwrap(); .unwrap();
@ -778,25 +743,37 @@ async fn test_db_channel_moving_bugs(db: &Arc<Database>) {
// Move to same parent should be a no-op // Move to same parent should be a no-op
assert!(db assert!(db
.move_channel(user_id, projects_id, zed_id, zed_id) .move_channel(projects_id, Some(zed_id), zed_id, user_id)
.await .await
.unwrap() .unwrap()
.is_empty()); .is_none());
// Stranding a channel should retain it's sub channels
db.unlink_channel(user_id, projects_id, zed_id)
.await
.unwrap();
let result = db.get_channels_for_user(user_id).await.unwrap(); let result = db.get_channels_for_user(user_id).await.unwrap();
assert_dag( assert_dag(
result.channels, result.channels,
&[ &[
(zed_id, None), (zed_id, None),
(projects_id, None), (projects_id, Some(zed_id)),
(livestreaming_id, Some(projects_id)), (livestreaming_id, Some(projects_id)),
], ],
); );
// Stranding a channel should retain it's sub channels
// Commented out as we don't fix permissions when this happens yet.
//
// db.unlink_channel(user_id, projects_id, zed_id)
// .await
// .unwrap();
// let result = db.get_channels_for_user(user_id).await.unwrap();
// assert_dag(
// result.channels,
// &[
// (zed_id, None),
// (projects_id, None),
// (livestreaming_id, Some(projects_id)),
// ],
// );
} }
test_both_dbs!( test_both_dbs!(
@ -812,11 +789,11 @@ async fn test_user_is_channel_participant(db: &Arc<Database>) {
let zed_channel = db.create_root_channel("zed", admin).await.unwrap(); let zed_channel = db.create_root_channel("zed", admin).await.unwrap();
let active_channel = db let active_channel = db
.create_channel("active", Some(zed_channel), admin) .create_sub_channel("active", zed_channel, admin)
.await .await
.unwrap(); .unwrap();
let vim_channel = db let vim_channel = db
.create_channel("vim", Some(active_channel), admin) .create_sub_channel("vim", active_channel, admin)
.await .await
.unwrap(); .unwrap();
@ -859,7 +836,7 @@ async fn test_user_is_channel_participant(db: &Arc<Database>) {
&[ &[
proto::ChannelMember { proto::ChannelMember {
user_id: admin.to_proto(), user_id: admin.to_proto(),
kind: proto::channel_member::Kind::Member.into(), kind: proto::channel_member::Kind::AncestorMember.into(),
role: proto::ChannelRole::Admin.into(), role: proto::ChannelRole::Admin.into(),
}, },
proto::ChannelMember { proto::ChannelMember {
@ -917,7 +894,7 @@ async fn test_user_is_channel_participant(db: &Arc<Database>) {
&[ &[
proto::ChannelMember { proto::ChannelMember {
user_id: admin.to_proto(), user_id: admin.to_proto(),
kind: proto::channel_member::Kind::Member.into(), kind: proto::channel_member::Kind::AncestorMember.into(),
role: proto::ChannelRole::Admin.into(), role: proto::ChannelRole::Admin.into(),
}, },
proto::ChannelMember { proto::ChannelMember {
@ -958,7 +935,7 @@ async fn test_user_is_channel_participant(db: &Arc<Database>) {
&[ &[
proto::ChannelMember { proto::ChannelMember {
user_id: admin.to_proto(), user_id: admin.to_proto(),
kind: proto::channel_member::Kind::Member.into(), kind: proto::channel_member::Kind::AncestorMember.into(),
role: proto::ChannelRole::Admin.into(), role: proto::ChannelRole::Admin.into(),
}, },
proto::ChannelMember { proto::ChannelMember {
@ -1006,7 +983,7 @@ async fn test_user_is_channel_participant(db: &Arc<Database>) {
&[ &[
proto::ChannelMember { proto::ChannelMember {
user_id: admin.to_proto(), user_id: admin.to_proto(),
kind: proto::channel_member::Kind::Member.into(), kind: proto::channel_member::Kind::AncestorMember.into(),
role: proto::ChannelRole::Admin.into(), role: proto::ChannelRole::Admin.into(),
}, },
proto::ChannelMember { proto::ChannelMember {
@ -1041,17 +1018,17 @@ async fn test_user_joins_correct_channel(db: &Arc<Database>) {
let zed_channel = db.create_root_channel("zed", admin).await.unwrap(); let zed_channel = db.create_root_channel("zed", admin).await.unwrap();
let active_channel = db let active_channel = db
.create_channel("active", Some(zed_channel), admin) .create_sub_channel("active", zed_channel, admin)
.await .await
.unwrap(); .unwrap();
let vim_channel = db let vim_channel = db
.create_channel("vim", Some(active_channel), admin) .create_sub_channel("vim", active_channel, admin)
.await .await
.unwrap(); .unwrap();
let vim2_channel = db let vim2_channel = db
.create_channel("vim2", Some(vim_channel), admin) .create_sub_channel("vim2", vim_channel, admin)
.await .await
.unwrap(); .unwrap();
@ -1068,15 +1045,52 @@ async fn test_user_joins_correct_channel(db: &Arc<Database>) {
.unwrap(); .unwrap();
let most_public = db let most_public = db
.transaction( .transaction(|tx| async move {
|tx| async move { db.most_public_ancestor_for_channel(vim_channel, &*tx).await }, Ok(db
) .public_path_to_channel(vim_channel, &tx)
.await?
.first()
.cloned())
})
.await .await
.unwrap(); .unwrap();
assert_eq!(most_public, Some(zed_channel)) assert_eq!(most_public, Some(zed_channel))
} }
test_both_dbs!(
test_guest_access,
test_guest_access_postgres,
test_guest_access_sqlite
);
async fn test_guest_access(db: &Arc<Database>) {
let server = db.create_server("test").await.unwrap();
let admin = new_test_user(db, "admin@example.com").await;
let guest = new_test_user(db, "guest@example.com").await;
let guest_connection = new_test_connection(server);
let zed_channel = db.create_root_channel("zed", admin).await.unwrap();
db.set_channel_visibility(zed_channel, crate::db::ChannelVisibility::Public, admin)
.await
.unwrap();
assert!(db
.join_channel_chat(zed_channel, guest_connection, guest)
.await
.is_err());
db.join_channel(zed_channel, guest, guest_connection, TEST_RELEASE_CHANNEL)
.await
.unwrap();
assert!(db
.join_channel_chat(zed_channel, guest_connection, guest)
.await
.is_ok())
}
#[track_caller] #[track_caller]
fn assert_dag(actual: ChannelGraph, expected: &[(ChannelId, Option<ChannelId>)]) { fn assert_dag(actual: ChannelGraph, expected: &[(ChannelId, Option<ChannelId>)]) {
let mut actual_map: HashMap<ChannelId, HashSet<ChannelId>> = HashMap::default(); let mut actual_map: HashMap<ChannelId, HashSet<ChannelId>> = HashMap::default();

View file

@ -15,10 +15,14 @@ test_both_dbs!(
async fn test_channel_message_retrieval(db: &Arc<Database>) { async fn test_channel_message_retrieval(db: &Arc<Database>) {
let user = new_test_user(db, "user@example.com").await; let user = new_test_user(db, "user@example.com").await;
let channel = db.create_channel("channel", None, user).await.unwrap(); let result = db.create_channel("channel", None, user).await.unwrap();
let owner_id = db.create_server("test").await.unwrap().0 as u32; let owner_id = db.create_server("test").await.unwrap().0 as u32;
db.join_channel_chat(channel, rpc::ConnectionId { owner_id, id: 0 }, user) db.join_channel_chat(
result.channel.id,
rpc::ConnectionId { owner_id, id: 0 },
user,
)
.await .await
.unwrap(); .unwrap();
@ -26,7 +30,7 @@ async fn test_channel_message_retrieval(db: &Arc<Database>) {
for i in 0..10 { for i in 0..10 {
all_messages.push( all_messages.push(
db.create_channel_message( db.create_channel_message(
channel, result.channel.id,
user, user,
&i.to_string(), &i.to_string(),
&[], &[],
@ -41,7 +45,7 @@ async fn test_channel_message_retrieval(db: &Arc<Database>) {
} }
let messages = db let messages = db
.get_channel_messages(channel, user, 3, None) .get_channel_messages(result.channel.id, user, 3, None)
.await .await
.unwrap() .unwrap()
.into_iter() .into_iter()
@ -51,7 +55,7 @@ async fn test_channel_message_retrieval(db: &Arc<Database>) {
let messages = db let messages = db
.get_channel_messages( .get_channel_messages(
channel, result.channel.id,
user, user,
4, 4,
Some(MessageId::from_proto(all_messages[6])), Some(MessageId::from_proto(all_messages[6])),
@ -74,7 +78,7 @@ async fn test_channel_message_nonces(db: &Arc<Database>) {
let user_a = new_test_user(db, "user_a@example.com").await; let user_a = new_test_user(db, "user_a@example.com").await;
let user_b = new_test_user(db, "user_b@example.com").await; let user_b = new_test_user(db, "user_b@example.com").await;
let user_c = new_test_user(db, "user_c@example.com").await; let user_c = new_test_user(db, "user_c@example.com").await;
let channel = db.create_channel("channel", None, user_a).await.unwrap(); let channel = db.create_root_channel("channel", user_a).await.unwrap();
db.invite_channel_member(channel, user_b, user_a, ChannelRole::Member) db.invite_channel_member(channel, user_b, user_a, ChannelRole::Member)
.await .await
.unwrap(); .unwrap();
@ -206,8 +210,8 @@ async fn test_unseen_channel_messages(db: &Arc<Database>) {
let user = new_test_user(db, "user_a@example.com").await; let user = new_test_user(db, "user_a@example.com").await;
let observer = new_test_user(db, "user_b@example.com").await; let observer = new_test_user(db, "user_b@example.com").await;
let channel_1 = db.create_channel("channel", None, user).await.unwrap(); let channel_1 = db.create_root_channel("channel", user).await.unwrap();
let channel_2 = db.create_channel("channel-2", None, user).await.unwrap(); let channel_2 = db.create_root_channel("channel-2", user).await.unwrap();
db.invite_channel_member(channel_1, observer, user, ChannelRole::Member) db.invite_channel_member(channel_1, observer, user, ChannelRole::Member)
.await .await
@ -362,7 +366,12 @@ async fn test_channel_message_mentions(db: &Arc<Database>) {
let user_b = new_test_user(db, "user_b@example.com").await; let user_b = new_test_user(db, "user_b@example.com").await;
let user_c = new_test_user(db, "user_c@example.com").await; let user_c = new_test_user(db, "user_c@example.com").await;
let channel = db.create_channel("channel", None, user_a).await.unwrap(); let channel = db
.create_channel("channel", None, user_a)
.await
.unwrap()
.channel
.id;
db.invite_channel_member(channel, user_b, user_a, ChannelRole::Member) db.invite_channel_member(channel, user_b, user_a, ChannelRole::Member)
.await .await
.unwrap(); .unwrap();

View file

@ -3,8 +3,11 @@ mod connection_pool;
use crate::{ use crate::{
auth, auth,
db::{ db::{
self, BufferId, ChannelId, ChannelVisibility, ChannelsForUser, CreatedChannelMessage, self, BufferId, ChannelId, ChannelRole, ChannelsForUser, CreateChannelResult,
Database, MessageId, NotificationId, ProjectId, RoomId, ServerId, User, UserId, CreatedChannelMessage, Database, InviteMemberResult, MembershipUpdated, MessageId,
MoveChannelResult, NotificationId, ProjectId, RemoveChannelMemberResult,
RenameChannelResult, RespondToChannelInvite, RoomId, ServerId, SetChannelVisibilityResult,
User, UserId,
}, },
executor::Executor, executor::Executor,
AppState, Result, AppState, Result,
@ -38,8 +41,8 @@ use lazy_static::lazy_static;
use prometheus::{register_int_gauge, IntGauge}; use prometheus::{register_int_gauge, IntGauge};
use rpc::{ use rpc::{
proto::{ proto::{
self, Ack, AnyTypedEnvelope, ChannelEdge, EntityMessage, EnvelopedMessage, self, Ack, AnyTypedEnvelope, EntityMessage, EnvelopedMessage, LiveKitConnectionInfo,
LiveKitConnectionInfo, RequestMessage, UpdateChannelBufferCollaborators, RequestMessage, UpdateChannelBufferCollaborators,
}, },
Connection, ConnectionId, Peer, Receipt, TypedEnvelope, Connection, ConnectionId, Peer, Receipt, TypedEnvelope,
}; };
@ -594,7 +597,7 @@ impl Server {
let mut pool = this.connection_pool.lock(); let mut pool = this.connection_pool.lock();
pool.add_connection(connection_id, user_id, user.admin); pool.add_connection(connection_id, user_id, user.admin);
this.peer.send(connection_id, build_initial_contacts_update(contacts, &pool))?; this.peer.send(connection_id, build_initial_contacts_update(contacts, &pool))?;
this.peer.send(connection_id, build_initial_channels_update( this.peer.send(connection_id, build_channels_update(
channels_for_user, channels_for_user,
channel_invites channel_invites
))?; ))?;
@ -951,6 +954,7 @@ async fn create_room(
Some(proto::LiveKitConnectionInfo { Some(proto::LiveKitConnectionInfo {
server_url: live_kit.url().into(), server_url: live_kit.url().into(),
token, token,
can_publish: true,
}) })
}) })
} }
@ -1031,6 +1035,7 @@ async fn join_room(
Some(proto::LiveKitConnectionInfo { Some(proto::LiveKitConnectionInfo {
server_url: live_kit.url().into(), server_url: live_kit.url().into(),
token, token,
can_publish: true,
}) })
} else { } else {
None None
@ -2217,38 +2222,21 @@ async fn create_channel(
let db = session.db().await; let db = session.db().await;
let parent_id = request.parent_id.map(|id| ChannelId::from_proto(id)); let parent_id = request.parent_id.map(|id| ChannelId::from_proto(id));
let id = db let CreateChannelResult {
channel,
participants_to_update,
} = db
.create_channel(&request.name, parent_id, session.user_id) .create_channel(&request.name, parent_id, session.user_id)
.await?; .await?;
let channel = proto::Channel {
id: id.to_proto(),
name: request.name,
visibility: proto::ChannelVisibility::Members as i32,
};
response.send(proto::CreateChannelResponse { response.send(proto::CreateChannelResponse {
channel: Some(channel.clone()), channel: Some(channel.to_proto()),
parent_id: request.parent_id, parent_id: request.parent_id,
})?; })?;
let Some(parent_id) = parent_id else {
return Ok(());
};
let update = proto::UpdateChannels {
channels: vec![channel],
insert_edge: vec![ChannelEdge {
parent_id: parent_id.to_proto(),
channel_id: id.to_proto(),
}],
..Default::default()
};
let user_ids_to_notify = db.get_channel_members(parent_id).await?;
let connection_pool = session.connection_pool().await; let connection_pool = session.connection_pool().await;
for user_id in user_ids_to_notify { for (user_id, channels) in participants_to_update {
let update = build_channels_update(channels, vec![]);
for connection_id in connection_pool.user_connection_ids(user_id) { for connection_id in connection_pool.user_connection_ids(user_id) {
if user_id == session.user_id { if user_id == session.user_id {
continue; continue;
@ -2297,7 +2285,10 @@ async fn invite_channel_member(
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 invitee_id = UserId::from_proto(request.user_id); let invitee_id = UserId::from_proto(request.user_id);
let notifications = db let InviteMemberResult {
channel,
notifications,
} = db
.invite_channel_member( .invite_channel_member(
channel_id, channel_id,
invitee_id, invitee_id,
@ -2306,21 +2297,17 @@ async fn invite_channel_member(
) )
.await?; .await?;
let channel = db.get_channel(channel_id, session.user_id).await?; let update = proto::UpdateChannels {
channel_invitations: vec![channel.to_proto()],
..Default::default()
};
let mut update = proto::UpdateChannels::default(); let connection_pool = session.connection_pool().await;
update.channel_invitations.push(proto::Channel { for connection_id in connection_pool.user_connection_ids(invitee_id) {
id: channel.id.to_proto(),
visibility: channel.visibility.into(),
name: channel.name,
});
let pool = session.connection_pool().await;
for connection_id in pool.user_connection_ids(invitee_id) {
session.peer.send(connection_id, update.clone())?; session.peer.send(connection_id, update.clone())?;
} }
send_notifications(&*pool, &session.peer, notifications); send_notifications(&*connection_pool, &session.peer, notifications);
response.send(proto::Ack {})?; response.send(proto::Ack {})?;
Ok(()) Ok(())
@ -2335,20 +2322,22 @@ async fn remove_channel_member(
let channel_id = ChannelId::from_proto(request.channel_id); let channel_id = ChannelId::from_proto(request.channel_id);
let member_id = UserId::from_proto(request.user_id); let member_id = UserId::from_proto(request.user_id);
let removed_notification_id = db let RemoveChannelMemberResult {
membership_update,
notification_id,
} = db
.remove_channel_member(channel_id, member_id, session.user_id) .remove_channel_member(channel_id, member_id, session.user_id)
.await?; .await?;
let mut update = proto::UpdateChannels::default(); let connection_pool = &session.connection_pool().await;
update.delete_channels.push(channel_id.to_proto()); notify_membership_updated(
&connection_pool,
for connection_id in session membership_update,
.connection_pool() member_id,
.await &session.peer,
.user_connection_ids(member_id) );
{ for connection_id in connection_pool.user_connection_ids(member_id) {
session.peer.send(connection_id, update.clone()).trace_err(); if let Some(notification_id) = notification_id {
if let Some(notification_id) = removed_notification_id {
session session
.peer .peer
.send( .send(
@ -2374,22 +2363,27 @@ async fn set_channel_visibility(
let channel_id = ChannelId::from_proto(request.channel_id); let channel_id = ChannelId::from_proto(request.channel_id);
let visibility = request.visibility().into(); let visibility = request.visibility().into();
let channel = db let SetChannelVisibilityResult {
participants_to_update,
participants_to_remove,
channels_to_remove,
} = db
.set_channel_visibility(channel_id, visibility, session.user_id) .set_channel_visibility(channel_id, visibility, session.user_id)
.await?; .await?;
let mut update = proto::UpdateChannels::default();
update.channels.push(proto::Channel {
id: channel.id.to_proto(),
name: channel.name,
visibility: channel.visibility.into(),
});
let member_ids = db.get_channel_members(channel_id).await?;
let connection_pool = session.connection_pool().await; let connection_pool = session.connection_pool().await;
for member_id in member_ids { for (user_id, channels) in participants_to_update {
for connection_id in connection_pool.user_connection_ids(member_id) { let update = build_channels_update(channels, vec![]);
for connection_id in connection_pool.user_connection_ids(user_id) {
session.peer.send(connection_id, update.clone())?;
}
}
for user_id in participants_to_remove {
let update = proto::UpdateChannels {
delete_channels: channels_to_remove.iter().map(|id| id.to_proto()).collect(),
..Default::default()
};
for connection_id in connection_pool.user_connection_ids(user_id) {
session.peer.send(connection_id, update.clone())?; session.peer.send(connection_id, update.clone())?;
} }
} }
@ -2406,7 +2400,7 @@ async fn set_channel_member_role(
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 member_id = UserId::from_proto(request.user_id); let member_id = UserId::from_proto(request.user_id);
let channel_member = db let result = db
.set_channel_member_role( .set_channel_member_role(
channel_id, channel_id,
session.user_id, session.user_id,
@ -2415,15 +2409,21 @@ async fn set_channel_member_role(
) )
.await?; .await?;
let channel = db.get_channel(channel_id, session.user_id).await?; match result {
db::SetMemberRoleResult::MembershipUpdated(membership_update) => {
let mut update = proto::UpdateChannels::default(); let connection_pool = session.connection_pool().await;
if channel_member.accepted { notify_membership_updated(
update.channel_permissions.push(proto::ChannelPermission { &connection_pool,
channel_id: channel.id.to_proto(), membership_update,
role: request.role, member_id,
}); &session.peer,
)
} }
db::SetMemberRoleResult::InviteUpdated(channel) => {
let update = proto::UpdateChannels {
channel_invitations: vec![channel.to_proto()],
..Default::default()
};
for connection_id in session for connection_id in session
.connection_pool() .connection_pool()
@ -2432,6 +2432,8 @@ async fn set_channel_member_role(
{ {
session.peer.send(connection_id, update.clone())?; session.peer.send(connection_id, update.clone())?;
} }
}
}
response.send(proto::Ack {})?; response.send(proto::Ack {})?;
Ok(()) Ok(())
@ -2444,26 +2446,25 @@ async fn rename_channel(
) -> Result<()> { ) -> Result<()> {
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 channel = db let RenameChannelResult {
channel,
participants_to_update,
} = db
.rename_channel(channel_id, session.user_id, &request.name) .rename_channel(channel_id, session.user_id, &request.name)
.await?; .await?;
let channel = proto::Channel {
id: channel.id.to_proto(),
name: channel.name,
visibility: channel.visibility.into(),
};
response.send(proto::RenameChannelResponse { response.send(proto::RenameChannelResponse {
channel: Some(channel.clone()), channel: Some(channel.to_proto()),
})?; })?;
let mut update = proto::UpdateChannels::default();
update.channels.push(channel);
let member_ids = db.get_channel_members(channel_id).await?;
let connection_pool = session.connection_pool().await; let connection_pool = session.connection_pool().await;
for member_id in member_ids { for (user_id, channel) in participants_to_update {
for connection_id in connection_pool.user_connection_ids(member_id) { for connection_id in connection_pool.user_connection_ids(user_id) {
let update = proto::UpdateChannels {
channels: vec![channel.to_proto()],
..Default::default()
};
session.peer.send(connection_id, update.clone())?; session.peer.send(connection_id, update.clone())?;
} }
} }
@ -2471,6 +2472,8 @@ async fn rename_channel(
Ok(()) Ok(())
} }
// TODO: Implement in terms of symlinks
// Current behavior of this is more like 'Move root channel'
async fn link_channel( async fn link_channel(
request: proto::LinkChannel, request: proto::LinkChannel,
response: Response<proto::LinkChannel>, response: Response<proto::LinkChannel>,
@ -2479,64 +2482,26 @@ async fn link_channel(
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 to = ChannelId::from_proto(request.to); let to = ChannelId::from_proto(request.to);
let channels_to_send = db.link_channel(session.user_id, channel_id, to).await?;
let members = db.get_channel_members(to).await?; let result = db
let connection_pool = session.connection_pool().await; .move_channel(channel_id, None, to, session.user_id)
let update = proto::UpdateChannels { .await?;
channels: channels_to_send drop(db);
.channels
.into_iter() notify_channel_moved(result, session).await?;
.map(|channel| proto::Channel {
id: channel.id.to_proto(),
visibility: channel.visibility.into(),
name: channel.name,
})
.collect(),
insert_edge: channels_to_send.edges,
..Default::default()
};
for member_id in members {
for connection_id in connection_pool.user_connection_ids(member_id) {
session.peer.send(connection_id, update.clone())?;
}
}
response.send(Ack {})?; response.send(Ack {})?;
Ok(()) Ok(())
} }
// TODO: Implement in terms of symlinks
async fn unlink_channel( async fn unlink_channel(
request: proto::UnlinkChannel, _request: proto::UnlinkChannel,
response: Response<proto::UnlinkChannel>, _response: Response<proto::UnlinkChannel>,
session: Session, _session: Session,
) -> Result<()> { ) -> Result<()> {
let db = session.db().await; Err(anyhow!("unimplemented").into())
let channel_id = ChannelId::from_proto(request.channel_id);
let from = ChannelId::from_proto(request.from);
db.unlink_channel(session.user_id, channel_id, from).await?;
let members = db.get_channel_members(from).await?;
let update = proto::UpdateChannels {
delete_edge: vec![proto::ChannelEdge {
channel_id: channel_id.to_proto(),
parent_id: from.to_proto(),
}],
..Default::default()
};
let connection_pool = session.connection_pool().await;
for member_id in members {
for connection_id in connection_pool.user_connection_ids(member_id) {
session.peer.send(connection_id, update.clone())?;
}
}
response.send(Ack {})?;
Ok(())
} }
async fn move_channel( async fn move_channel(
@ -2549,53 +2514,46 @@ async fn move_channel(
let from_parent = ChannelId::from_proto(request.from); let from_parent = ChannelId::from_proto(request.from);
let to = ChannelId::from_proto(request.to); let to = ChannelId::from_proto(request.to);
let channels_to_send = db let result = db
.move_channel(session.user_id, channel_id, from_parent, to) .move_channel(channel_id, Some(from_parent), to, session.user_id)
.await?; .await?;
drop(db);
notify_channel_moved(result, session).await?;
if channels_to_send.is_empty() {
response.send(Ack {})?; response.send(Ack {})?;
Ok(())
}
async fn notify_channel_moved(result: Option<MoveChannelResult>, session: Session) -> Result<()> {
let Some(MoveChannelResult {
participants_to_remove,
participants_to_update,
moved_channels,
}) = result
else {
return Ok(()); return Ok(());
}
let members_from = db.get_channel_members(from_parent).await?;
let members_to = db.get_channel_members(to).await?;
let update = proto::UpdateChannels {
delete_edge: vec![proto::ChannelEdge {
channel_id: channel_id.to_proto(),
parent_id: from_parent.to_proto(),
}],
..Default::default()
}; };
let moved_channels: Vec<u64> = moved_channels.iter().map(|id| id.to_proto()).collect();
let connection_pool = session.connection_pool().await; let connection_pool = session.connection_pool().await;
for member_id in members_from { for (user_id, channels) in participants_to_update {
for connection_id in connection_pool.user_connection_ids(member_id) { let mut update = build_channels_update(channels, vec![]);
update.delete_channels = moved_channels.clone();
for connection_id in connection_pool.user_connection_ids(user_id) {
session.peer.send(connection_id, update.clone())?; session.peer.send(connection_id, update.clone())?;
} }
} }
for user_id in participants_to_remove {
let update = proto::UpdateChannels { let update = proto::UpdateChannels {
channels: channels_to_send delete_channels: moved_channels.clone(),
.channels
.into_iter()
.map(|channel| proto::Channel {
id: channel.id.to_proto(),
visibility: channel.visibility.into(),
name: channel.name,
})
.collect(),
insert_edge: channels_to_send.edges,
..Default::default() ..Default::default()
}; };
for member_id in members_to { for connection_id in connection_pool.user_connection_ids(user_id) {
for connection_id in connection_pool.user_connection_ids(member_id) {
session.peer.send(connection_id, update.clone())?; session.peer.send(connection_id, update.clone())?;
} }
} }
response.send(Ack {})?;
Ok(()) Ok(())
} }
@ -2620,81 +2578,39 @@ async fn respond_to_channel_invite(
) -> Result<()> { ) -> Result<()> {
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 notifications = db let RespondToChannelInvite {
membership_update,
notifications,
} = db
.respond_to_channel_invite(channel_id, session.user_id, request.accept) .respond_to_channel_invite(channel_id, session.user_id, request.accept)
.await?; .await?;
if request.accept { let connection_pool = session.connection_pool().await;
channel_membership_updated(db, channel_id, &session).await?; if let Some(membership_update) = membership_update {
} else { notify_membership_updated(
let mut update = proto::UpdateChannels::default(); &connection_pool,
update membership_update,
.remove_channel_invitations session.user_id,
.push(channel_id.to_proto());
session.peer.send(session.connection_id, update)?;
}
send_notifications(
&*session.connection_pool().await,
&session.peer, &session.peer,
notifications,
); );
} else {
let update = proto::UpdateChannels {
remove_channel_invitations: vec![channel_id.to_proto()],
..Default::default()
};
for connection_id in connection_pool.user_connection_ids(session.user_id) {
session.peer.send(connection_id, update.clone())?;
}
};
send_notifications(&*connection_pool, &session.peer, notifications);
response.send(proto::Ack {})?; response.send(proto::Ack {})?;
Ok(()) Ok(())
} }
async fn channel_membership_updated(
db: tokio::sync::MutexGuard<'_, DbHandle>,
channel_id: ChannelId,
session: &Session,
) -> Result<(), crate::Error> {
let mut update = proto::UpdateChannels::default();
update
.remove_channel_invitations
.push(channel_id.to_proto());
let result = db.get_channel_for_user(channel_id, session.user_id).await?;
update.channels.extend(
result
.channels
.channels
.into_iter()
.map(|channel| proto::Channel {
id: channel.id.to_proto(),
visibility: channel.visibility.into(),
name: channel.name,
}),
);
update.unseen_channel_messages = result.channel_messages;
update.unseen_channel_buffer_changes = result.unseen_buffer_changes;
update.insert_edge = result.channels.edges;
update
.channel_participants
.extend(
result
.channel_participants
.into_iter()
.map(|(channel_id, user_ids)| proto::ChannelParticipants {
channel_id: channel_id.to_proto(),
participant_user_ids: user_ids.into_iter().map(UserId::to_proto).collect(),
}),
);
update
.channel_permissions
.extend(
result
.channels_with_admin_privileges
.into_iter()
.map(|channel_id| proto::ChannelPermission {
channel_id: channel_id.to_proto(),
role: proto::ChannelRole::Admin.into(),
}),
);
session.peer.send(session.connection_id, update)?;
Ok(())
}
async fn join_channel( async fn join_channel(
request: proto::JoinChannel, request: proto::JoinChannel,
response: Response<proto::JoinChannel>, response: Response<proto::JoinChannel>,
@ -2727,7 +2643,7 @@ async fn join_channel_internal(
leave_room_for_session(&session).await?; leave_room_for_session(&session).await?;
let db = session.db().await; let db = session.db().await;
let (joined_room, joined_channel) = db let (joined_room, membership_updated, role) = db
.join_channel( .join_channel(
channel_id, channel_id,
session.user_id, session.user_id,
@ -2737,16 +2653,32 @@ async fn join_channel_internal(
.await?; .await?;
let live_kit_connection_info = session.live_kit_client.as_ref().and_then(|live_kit| { let live_kit_connection_info = session.live_kit_client.as_ref().and_then(|live_kit| {
let token = live_kit let (can_publish, token) = if role == ChannelRole::Guest {
(
false,
live_kit
.guest_token(
&joined_room.room.live_kit_room,
&session.user_id.to_string(),
)
.trace_err()?,
)
} else {
(
true,
live_kit
.room_token( .room_token(
&joined_room.room.live_kit_room, &joined_room.room.live_kit_room,
&session.user_id.to_string(), &session.user_id.to_string(),
) )
.trace_err()?; .trace_err()?,
)
};
Some(LiveKitConnectionInfo { Some(LiveKitConnectionInfo {
server_url: live_kit.url().into(), server_url: live_kit.url().into(),
token, token,
can_publish,
}) })
}); });
@ -2756,8 +2688,14 @@ async fn join_channel_internal(
live_kit_connection_info, live_kit_connection_info,
})?; })?;
if let Some(joined_channel) = joined_channel { let connection_pool = session.connection_pool().await;
channel_membership_updated(db, joined_channel, &session).await? if let Some(membership_updated) = membership_updated {
notify_membership_updated(
&connection_pool,
membership_updated,
session.user_id,
&session.peer,
);
} }
room_updated(&joined_room.room, &session.peer); room_updated(&joined_room.room, &session.peer);
@ -3281,7 +3219,26 @@ fn to_tungstenite_message(message: AxumMessage) -> TungsteniteMessage {
} }
} }
fn build_initial_channels_update( fn notify_membership_updated(
connection_pool: &ConnectionPool,
result: MembershipUpdated,
user_id: UserId,
peer: &Peer,
) {
let mut update = build_channels_update(result.new_channels, vec![]);
update.delete_channels = result
.removed_channels
.into_iter()
.map(|id| id.to_proto())
.collect();
update.remove_channel_invitations = vec![result.channel_id.to_proto()];
for connection_id in connection_pool.user_connection_ids(user_id) {
peer.send(connection_id, update.clone()).trace_err();
}
}
fn build_channels_update(
channels: ChannelsForUser, channels: ChannelsForUser,
channel_invites: Vec<db::Channel>, channel_invites: Vec<db::Channel>,
) -> proto::UpdateChannels { ) -> proto::UpdateChannels {
@ -3292,6 +3249,7 @@ fn build_initial_channels_update(
id: channel.id.to_proto(), id: channel.id.to_proto(),
name: channel.name, name: channel.name,
visibility: channel.visibility.into(), visibility: channel.visibility.into(),
role: channel.role.into(),
}); });
} }
@ -3308,24 +3266,12 @@ fn build_initial_channels_update(
}); });
} }
update
.channel_permissions
.extend(
channels
.channels_with_admin_privileges
.into_iter()
.map(|id| proto::ChannelPermission {
channel_id: id.to_proto(),
role: proto::ChannelRole::Admin.into(),
}),
);
for channel in channel_invites { for channel in channel_invites {
update.channel_invitations.push(proto::Channel { update.channel_invitations.push(proto::Channel {
id: channel.id.to_proto(), id: channel.id.to_proto(),
name: channel.name, name: channel.name,
// TODO: Visibility visibility: channel.visibility.into(),
visibility: ChannelVisibility::Public.into(), role: channel.role.into(),
}); });
} }

View file

@ -410,10 +410,10 @@ async fn test_channel_buffer_disconnect(
server.disconnect_client(client_a.peer_id().unwrap()); server.disconnect_client(client_a.peer_id().unwrap());
deterministic.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT); deterministic.advance_clock(RECEIVE_TIMEOUT + RECONNECT_TIMEOUT);
channel_buffer_a.update(cx_a, |buffer, _| { channel_buffer_a.update(cx_a, |buffer, cx| {
assert_eq!( assert_eq!(
buffer.channel().as_ref(), buffer.channel(cx).unwrap().as_ref(),
&channel(channel_id, "the-channel") &channel(channel_id, "the-channel", proto::ChannelRole::Admin)
); );
assert!(!buffer.is_connected()); assert!(!buffer.is_connected());
}); });
@ -435,18 +435,16 @@ async fn test_channel_buffer_disconnect(
deterministic.run_until_parked(); deterministic.run_until_parked();
// Channel buffer observed the deletion // Channel buffer observed the deletion
channel_buffer_b.update(cx_b, |buffer, _| { channel_buffer_b.update(cx_b, |buffer, cx| {
assert_eq!( assert!(buffer.channel(cx).is_none());
buffer.channel().as_ref(),
&channel(channel_id, "the-channel")
);
assert!(!buffer.is_connected()); assert!(!buffer.is_connected());
}); });
} }
fn channel(id: u64, name: &'static str) -> Channel { fn channel(id: u64, name: &'static str, role: proto::ChannelRole) -> Channel {
Channel { Channel {
id, id,
role,
name: name.to_string(), name: name.to_string(),
visibility: proto::ChannelVisibility::Members, visibility: proto::ChannelVisibility::Members,
unseen_note_version: None, unseen_note_version: None,
@ -698,7 +696,7 @@ async fn test_following_to_channel_notes_without_a_shared_project(
.await .await
.unwrap(); .unwrap();
channel_view_1_a.update(cx_a, |notes, cx| { channel_view_1_a.update(cx_a, |notes, cx| {
assert_eq!(notes.channel(cx).name, "channel-1"); assert_eq!(notes.channel(cx).unwrap().name, "channel-1");
notes.editor.update(cx, |editor, cx| { notes.editor.update(cx, |editor, cx| {
editor.insert("Hello from A.", cx); editor.insert("Hello from A.", cx);
editor.change_selections(None, cx, |selections| { editor.change_selections(None, cx, |selections| {
@ -730,7 +728,7 @@ async fn test_following_to_channel_notes_without_a_shared_project(
.expect("active item is not a channel view") .expect("active item is not a channel view")
}); });
channel_view_1_b.read_with(cx_b, |notes, cx| { channel_view_1_b.read_with(cx_b, |notes, cx| {
assert_eq!(notes.channel(cx).name, "channel-1"); assert_eq!(notes.channel(cx).unwrap().name, "channel-1");
let editor = notes.editor.read(cx); let editor = notes.editor.read(cx);
assert_eq!(editor.text(cx), "Hello from A."); assert_eq!(editor.text(cx), "Hello from A.");
assert_eq!(editor.selections.ranges::<usize>(cx), &[3..4]); assert_eq!(editor.selections.ranges::<usize>(cx), &[3..4]);
@ -742,7 +740,7 @@ async fn test_following_to_channel_notes_without_a_shared_project(
.await .await
.unwrap(); .unwrap();
channel_view_2_a.read_with(cx_a, |notes, cx| { channel_view_2_a.read_with(cx_a, |notes, cx| {
assert_eq!(notes.channel(cx).name, "channel-2"); assert_eq!(notes.channel(cx).unwrap().name, "channel-2");
}); });
// Client B is taken to the notes for channel 2. // Client B is taken to the notes for channel 2.
@ -759,7 +757,7 @@ async fn test_following_to_channel_notes_without_a_shared_project(
.expect("active item is not a channel view") .expect("active item is not a channel view")
}); });
channel_view_2_b.read_with(cx_b, |notes, cx| { channel_view_2_b.read_with(cx_b, |notes, cx| {
assert_eq!(notes.channel(cx).name, "channel-2"); assert_eq!(notes.channel(cx).unwrap().name, "channel-2");
}); });
} }

File diff suppressed because it is too large Load diff

View file

@ -48,7 +48,7 @@ impl RandomizedTest for RandomChannelBufferTest {
let db = &server.app_state.db; let db = &server.app_state.db;
for ix in 0..CHANNEL_COUNT { for ix in 0..CHANNEL_COUNT {
let id = db let id = db
.create_channel(&format!("channel-{ix}"), None, users[0].user_id) .create_root_channel(&format!("channel-{ix}"), users[0].user_id)
.await .await
.unwrap(); .unwrap();
for user in &users[1..] { for user in &users[1..] {
@ -98,15 +98,16 @@ impl RandomizedTest for RandomChannelBufferTest {
30..=40 => { 30..=40 => {
if let Some(buffer) = channel_buffers.iter().choose(rng) { if let Some(buffer) = channel_buffers.iter().choose(rng) {
let channel_name = buffer.read_with(cx, |b, _| b.channel().name.clone()); let channel_name =
buffer.read_with(cx, |b, cx| b.channel(cx).unwrap().name.clone());
break ChannelBufferOperation::LeaveChannelNotes { channel_name }; break ChannelBufferOperation::LeaveChannelNotes { channel_name };
} }
} }
_ => { _ => {
if let Some(buffer) = channel_buffers.iter().choose(rng) { if let Some(buffer) = channel_buffers.iter().choose(rng) {
break buffer.read_with(cx, |b, _| { break buffer.read_with(cx, |b, cx| {
let channel_name = b.channel().name.clone(); let channel_name = b.channel(cx).unwrap().name.clone();
let edits = b let edits = b
.buffer() .buffer()
.read_with(cx, |buffer, _| buffer.get_random_edits(rng, 3)); .read_with(cx, |buffer, _| buffer.get_random_edits(rng, 3));
@ -153,7 +154,7 @@ impl RandomizedTest for RandomChannelBufferTest {
let buffer = cx.update(|cx| { let buffer = cx.update(|cx| {
let mut left_buffer = Err(TestError::Inapplicable); let mut left_buffer = Err(TestError::Inapplicable);
client.channel_buffers().retain(|buffer| { client.channel_buffers().retain(|buffer| {
if buffer.read(cx).channel().name == channel_name { if buffer.read(cx).channel(cx).unwrap().name == channel_name {
left_buffer = Ok(buffer.clone()); left_buffer = Ok(buffer.clone());
false false
} else { } else {
@ -179,7 +180,9 @@ impl RandomizedTest for RandomChannelBufferTest {
client client
.channel_buffers() .channel_buffers()
.iter() .iter()
.find(|buffer| buffer.read(cx).channel().name == channel_name) .find(|buffer| {
buffer.read(cx).channel(cx).unwrap().name == channel_name
})
.cloned() .cloned()
}) })
.ok_or_else(|| TestError::Inapplicable)?; .ok_or_else(|| TestError::Inapplicable)?;
@ -250,7 +253,7 @@ impl RandomizedTest for RandomChannelBufferTest {
if let Some(channel_buffer) = client if let Some(channel_buffer) = client
.channel_buffers() .channel_buffers()
.iter() .iter()
.find(|b| b.read(cx).channel().id == channel_id.to_proto()) .find(|b| b.read(cx).channel_id == channel_id.to_proto())
{ {
let channel_buffer = channel_buffer.read(cx); let channel_buffer = channel_buffer.read(cx);

View file

@ -611,38 +611,6 @@ impl TestClient {
) -> WindowHandle<Workspace> { ) -> WindowHandle<Workspace> {
cx.add_window(|cx| Workspace::new(0, project.clone(), self.app_state.clone(), cx)) cx.add_window(|cx| Workspace::new(0, project.clone(), self.app_state.clone(), cx))
} }
pub async fn add_admin_to_channel(
&self,
user: (&TestClient, &mut TestAppContext),
channel: u64,
cx_self: &mut TestAppContext,
) {
let (other_client, other_cx) = user;
cx_self
.read(ChannelStore::global)
.update(cx_self, |channel_store, cx| {
channel_store.invite_member(
channel,
other_client.user_id().unwrap(),
ChannelRole::Admin,
cx,
)
})
.await
.unwrap();
cx_self.foreground().run_until_parked();
other_cx
.read(ChannelStore::global)
.update(other_cx, |channel_store, cx| {
channel_store.respond_to_channel_invite(channel, true, cx)
})
.await
.unwrap();
}
} }
impl Drop for TestClient { impl Drop for TestClient {

View file

@ -61,6 +61,7 @@ postage.workspace = true
serde.workspace = true serde.workspace = true
serde_derive.workspace = true serde_derive.workspace = true
time.workspace = true time.workspace = true
smallvec.workspace = true
[dev-dependencies] [dev-dependencies]
call = { path = "../call", features = ["test-support"] } call = { path = "../call", features = ["test-support"] }

View file

@ -15,13 +15,14 @@ use gpui::{
ViewContext, ViewHandle, ViewContext, ViewHandle,
}; };
use project::Project; use project::Project;
use smallvec::SmallVec;
use std::{ use std::{
any::{Any, TypeId}, any::{Any, TypeId},
sync::Arc, sync::Arc,
}; };
use util::ResultExt; use util::ResultExt;
use workspace::{ use workspace::{
item::{FollowableItem, Item, ItemHandle}, item::{FollowableItem, Item, ItemEvent, ItemHandle},
register_followable_item, register_followable_item,
searchable::SearchableItemHandle, searchable::SearchableItemHandle,
ItemNavHistory, Pane, SaveIntent, ViewId, Workspace, WorkspaceId, ItemNavHistory, Pane, SaveIntent, ViewId, Workspace, WorkspaceId,
@ -140,6 +141,12 @@ impl ChannelView {
editor.set_collaboration_hub(Box::new(ChannelBufferCollaborationHub( editor.set_collaboration_hub(Box::new(ChannelBufferCollaborationHub(
channel_buffer.clone(), channel_buffer.clone(),
))); )));
editor.set_read_only(
!channel_buffer
.read(cx)
.channel(cx)
.is_some_and(|c| c.can_edit_notes()),
);
editor editor
}); });
let _editor_event_subscription = cx.subscribe(&editor, |_, _, e, cx| cx.emit(e.clone())); let _editor_event_subscription = cx.subscribe(&editor, |_, _, e, cx| cx.emit(e.clone()));
@ -157,8 +164,8 @@ impl ChannelView {
} }
} }
pub fn channel(&self, cx: &AppContext) -> Arc<Channel> { pub fn channel(&self, cx: &AppContext) -> Option<Arc<Channel>> {
self.channel_buffer.read(cx).channel() self.channel_buffer.read(cx).channel(cx)
} }
fn handle_channel_buffer_event( fn handle_channel_buffer_event(
@ -172,6 +179,13 @@ impl ChannelView {
editor.set_read_only(true); editor.set_read_only(true);
cx.notify(); cx.notify();
}), }),
ChannelBufferEvent::ChannelChanged => {
self.editor.update(cx, |editor, cx| {
editor.set_read_only(!self.channel(cx).is_some_and(|c| c.can_edit_notes()));
cx.emit(editor::Event::TitleChanged);
cx.notify()
});
}
ChannelBufferEvent::BufferEdited => { ChannelBufferEvent::BufferEdited => {
if cx.is_self_focused() || self.editor.is_focused(cx) { if cx.is_self_focused() || self.editor.is_focused(cx) {
self.acknowledge_buffer_version(cx); self.acknowledge_buffer_version(cx);
@ -179,7 +193,7 @@ impl ChannelView {
self.channel_store.update(cx, |store, cx| { self.channel_store.update(cx, |store, cx| {
let channel_buffer = self.channel_buffer.read(cx); let channel_buffer = self.channel_buffer.read(cx);
store.notes_changed( store.notes_changed(
channel_buffer.channel().id, channel_buffer.channel_id,
channel_buffer.epoch(), channel_buffer.epoch(),
&channel_buffer.buffer().read(cx).version(), &channel_buffer.buffer().read(cx).version(),
cx, cx,
@ -187,7 +201,7 @@ impl ChannelView {
}); });
} }
} }
_ => {} ChannelBufferEvent::CollaboratorsChanged => {}
} }
} }
@ -195,7 +209,7 @@ impl ChannelView {
self.channel_store.update(cx, |store, cx| { self.channel_store.update(cx, |store, cx| {
let channel_buffer = self.channel_buffer.read(cx); let channel_buffer = self.channel_buffer.read(cx);
store.acknowledge_notes_version( store.acknowledge_notes_version(
channel_buffer.channel().id, channel_buffer.channel_id,
channel_buffer.epoch(), channel_buffer.epoch(),
&channel_buffer.buffer().read(cx).version(), &channel_buffer.buffer().read(cx).version(),
cx, cx,
@ -250,11 +264,17 @@ impl Item for ChannelView {
style: &theme::Tab, style: &theme::Tab,
cx: &gpui::AppContext, cx: &gpui::AppContext,
) -> AnyElement<V> { ) -> AnyElement<V> {
let channel_name = &self.channel_buffer.read(cx).channel().name; let label = if let Some(channel) = self.channel(cx) {
let label = if self.channel_buffer.read(cx).is_connected() { match (
format!("#{}", channel_name) channel.can_edit_notes(),
self.channel_buffer.read(cx).is_connected(),
) {
(true, true) => format!("#{}", channel.name),
(false, true) => format!("#{} (read-only)", channel.name),
(_, false) => format!("#{} (disconnected)", channel.name),
}
} else { } else {
format!("#{} (disconnected)", channel_name) format!("channel notes (disconnected)")
}; };
Label::new(label, style.label.to_owned()).into_any() Label::new(label, style.label.to_owned()).into_any()
} }
@ -298,6 +318,10 @@ impl Item for ChannelView {
fn pixel_position_of_cursor(&self, cx: &AppContext) -> Option<Vector2F> { fn pixel_position_of_cursor(&self, cx: &AppContext) -> Option<Vector2F> {
self.editor.read(cx).pixel_position_of_cursor(cx) self.editor.read(cx).pixel_position_of_cursor(cx)
} }
fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
editor::Editor::to_item_events(event)
}
} }
impl FollowableItem for ChannelView { impl FollowableItem for ChannelView {
@ -313,7 +337,7 @@ impl FollowableItem for ChannelView {
Some(proto::view::Variant::ChannelView( Some(proto::view::Variant::ChannelView(
proto::view::ChannelView { proto::view::ChannelView {
channel_id: channel_buffer.channel().id, channel_id: channel_buffer.channel_id,
editor: if let Some(proto::view::Variant::Editor(proto)) = editor: if let Some(proto::view::Variant::Editor(proto)) =
self.editor.read(cx).to_state_proto(cx) self.editor.read(cx).to_state_proto(cx)
{ {

View file

@ -263,21 +263,22 @@ impl ChatPanel {
fn set_active_chat(&mut self, chat: ModelHandle<ChannelChat>, cx: &mut ViewContext<Self>) { fn set_active_chat(&mut self, chat: ModelHandle<ChannelChat>, cx: &mut ViewContext<Self>) {
if self.active_chat.as_ref().map(|e| &e.0) != Some(&chat) { if self.active_chat.as_ref().map(|e| &e.0) != Some(&chat) {
let channel_id = chat.read(cx).channel_id;
{
self.markdown_data.clear(); self.markdown_data.clear();
let id = {
let chat = chat.read(cx); let chat = chat.read(cx);
let channel = chat.channel().clone();
self.message_list.reset(chat.message_count()); self.message_list.reset(chat.message_count());
let channel_name = chat.channel(cx).map(|channel| channel.name.clone());
self.input_editor.update(cx, |editor, cx| { self.input_editor.update(cx, |editor, cx| {
editor.set_channel(channel.clone(), cx); editor.set_channel(channel_id, channel_name, cx);
}); });
channel.id
}; };
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.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(channel_id) {
select.set_selected_index(ix, cx); select.set_selected_index(ix, cx);
} }
}); });
@ -361,7 +362,8 @@ impl ChatPanel {
let is_admin = self let is_admin = self
.channel_store .channel_store
.read(cx) .read(cx)
.is_user_admin(active_chat.channel().id); .is_channel_admin(active_chat.channel_id);
let last_message = active_chat.message(ix.saturating_sub(1)); let last_message = active_chat.message(ix.saturating_sub(1));
let this_message = active_chat.message(ix).clone(); let this_message = active_chat.message(ix).clone();
let is_continuation = last_message.id != this_message.id let is_continuation = last_message.id != this_message.id
@ -676,7 +678,7 @@ impl ChatPanel {
.active_chat .active_chat
.as_ref() .as_ref()
.and_then(|(chat, _)| { .and_then(|(chat, _)| {
(chat.read(cx).channel().id == selected_channel_id) (chat.read(cx).channel_id == selected_channel_id)
.then(|| Task::ready(anyhow::Ok(chat.clone()))) .then(|| Task::ready(anyhow::Ok(chat.clone())))
}) })
.unwrap_or_else(|| { .unwrap_or_else(|| {
@ -714,7 +716,7 @@ impl ChatPanel {
fn open_notes(&mut self, _: &OpenChannelNotes, cx: &mut ViewContext<Self>) { fn open_notes(&mut self, _: &OpenChannelNotes, cx: &mut ViewContext<Self>) {
if let Some((chat, _)) = &self.active_chat { if let Some((chat, _)) = &self.active_chat {
let channel_id = chat.read(cx).channel().id; let channel_id = chat.read(cx).channel_id;
if let Some(workspace) = self.workspace.upgrade(cx) { if let Some(workspace) = self.workspace.upgrade(cx) {
ChannelView::open(channel_id, workspace, cx).detach(); ChannelView::open(channel_id, workspace, cx).detach();
} }
@ -723,7 +725,7 @@ impl ChatPanel {
fn join_call(&mut self, _: &JoinCall, cx: &mut ViewContext<Self>) { fn join_call(&mut self, _: &JoinCall, cx: &mut ViewContext<Self>) {
if let Some((chat, _)) = &self.active_chat { if let Some((chat, _)) = &self.active_chat {
let channel_id = chat.read(cx).channel().id; let channel_id = chat.read(cx).channel_id;
ActiveCall::global(cx) ActiveCall::global(cx)
.update(cx, |call, cx| call.join_channel(channel_id, cx)) .update(cx, |call, cx| call.join_channel(channel_id, cx))
.detach_and_log_err(cx); .detach_and_log_err(cx);

View file

@ -1,4 +1,4 @@
use channel::{Channel, ChannelMembership, ChannelStore, MessageParams}; use channel::{ChannelId, ChannelMembership, ChannelStore, MessageParams};
use client::UserId; use client::UserId;
use collections::HashMap; use collections::HashMap;
use editor::{AnchorRangeExt, Editor}; use editor::{AnchorRangeExt, Editor};
@ -30,7 +30,7 @@ pub struct MessageEditor {
users: HashMap<String, UserId>, users: HashMap<String, UserId>,
mentions: Vec<UserId>, mentions: Vec<UserId>,
mentions_task: Option<Task<()>>, mentions_task: Option<Task<()>>,
channel: Option<Arc<Channel>>, channel_id: Option<ChannelId>,
} }
impl MessageEditor { impl MessageEditor {
@ -68,24 +68,33 @@ impl MessageEditor {
editor, editor,
channel_store, channel_store,
users: HashMap::default(), users: HashMap::default(),
channel: None, channel_id: None,
mentions: Vec::new(), mentions: Vec::new(),
mentions_task: None, mentions_task: None,
} }
} }
pub fn set_channel(&mut self, channel: Arc<Channel>, cx: &mut ViewContext<Self>) { pub fn set_channel(
&mut self,
channel_id: u64,
channel_name: Option<String>,
cx: &mut ViewContext<Self>,
) {
self.editor.update(cx, |editor, cx| { self.editor.update(cx, |editor, cx| {
editor.set_placeholder_text(format!("Message #{}", channel.name), cx); if let Some(channel_name) = channel_name {
editor.set_placeholder_text(format!("Message #{}", channel_name), cx);
} else {
editor.set_placeholder_text(format!("Message Channel"), cx);
}
}); });
self.channel = Some(channel); self.channel_id = Some(channel_id);
self.refresh_users(cx); self.refresh_users(cx);
} }
pub fn refresh_users(&mut self, cx: &mut ViewContext<Self>) { pub fn refresh_users(&mut self, cx: &mut ViewContext<Self>) {
if let Some(channel) = &self.channel { if let Some(channel_id) = self.channel_id {
let members = self.channel_store.update(cx, |store, cx| { let members = self.channel_store.update(cx, |store, cx| {
store.get_channel_member_details(channel.id, cx) store.get_channel_member_details(channel_id, cx)
}); });
cx.spawn(|this, mut cx| async move { cx.spawn(|this, mut cx| async move {
let members = members.await?; let members = members.await?;

View file

@ -120,22 +120,11 @@ struct StartLinkChannelFor {
parent_id: Option<ChannelId>, parent_id: Option<ChannelId>,
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct LinkChannel {
to: ChannelId,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct MoveChannel { struct MoveChannel {
to: ChannelId, to: ChannelId,
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
struct UnlinkChannel {
channel_id: ChannelId,
parent_id: ChannelId,
}
type DraggedChannel = (Channel, Option<ChannelId>); type DraggedChannel = (Channel, Option<ChannelId>);
actions!( actions!(
@ -147,8 +136,7 @@ actions!(
CollapseSelectedChannel, CollapseSelectedChannel,
ExpandSelectedChannel, ExpandSelectedChannel,
StartMoveChannel, StartMoveChannel,
StartLinkChannel, MoveSelected,
MoveOrLinkToSelected,
InsertSpace, InsertSpace,
] ]
); );
@ -166,11 +154,8 @@ impl_actions!(
JoinChannelCall, JoinChannelCall,
JoinChannelChat, JoinChannelChat,
CopyChannelLink, CopyChannelLink,
LinkChannel,
StartMoveChannelFor, StartMoveChannelFor,
StartLinkChannelFor,
MoveChannel, MoveChannel,
UnlinkChannel,
ToggleSelectedIx ToggleSelectedIx
] ]
); );
@ -185,7 +170,7 @@ struct ChannelMoveClipboard {
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
enum ClipboardIntent { enum ClipboardIntent {
Move, Move,
Link, // Link,
} }
const COLLABORATION_PANEL_KEY: &'static str = "CollaborationPanel"; const COLLABORATION_PANEL_KEY: &'static str = "CollaborationPanel";
@ -238,18 +223,6 @@ pub fn init(cx: &mut AppContext) {
}, },
); );
cx.add_action(
|panel: &mut CollabPanel,
action: &StartLinkChannelFor,
_: &mut ViewContext<CollabPanel>| {
panel.channel_clipboard = Some(ChannelMoveClipboard {
channel_id: action.channel_id,
parent_id: action.parent_id,
intent: ClipboardIntent::Link,
})
},
);
cx.add_action( cx.add_action(
|panel: &mut CollabPanel, _: &StartMoveChannel, _: &mut ViewContext<CollabPanel>| { |panel: &mut CollabPanel, _: &StartMoveChannel, _: &mut ViewContext<CollabPanel>| {
if let Some((_, path)) = panel.selected_channel() { if let Some((_, path)) = panel.selected_channel() {
@ -263,40 +236,26 @@ pub fn init(cx: &mut AppContext) {
); );
cx.add_action( cx.add_action(
|panel: &mut CollabPanel, _: &StartLinkChannel, _: &mut ViewContext<CollabPanel>| { |panel: &mut CollabPanel, _: &MoveSelected, cx: &mut ViewContext<CollabPanel>| {
if let Some((_, path)) = panel.selected_channel() {
panel.channel_clipboard = Some(ChannelMoveClipboard {
channel_id: path.channel_id(),
parent_id: path.parent_id(),
intent: ClipboardIntent::Link,
})
}
},
);
cx.add_action(
|panel: &mut CollabPanel, _: &MoveOrLinkToSelected, cx: &mut ViewContext<CollabPanel>| {
let clipboard = panel.channel_clipboard.take(); let clipboard = panel.channel_clipboard.take();
if let Some(((selected_channel, _), clipboard)) = if let Some(((selected_channel, _), clipboard)) =
panel.selected_channel().zip(clipboard) panel.selected_channel().zip(clipboard)
{ {
match clipboard.intent { match clipboard.intent {
ClipboardIntent::Move if clipboard.parent_id.is_some() => { ClipboardIntent::Move => panel.channel_store.update(cx, |channel_store, cx| {
let parent_id = clipboard.parent_id.unwrap(); match clipboard.parent_id {
panel.channel_store.update(cx, |channel_store, cx| { Some(parent_id) => channel_store.move_channel(
channel_store
.move_channel(
clipboard.channel_id, clipboard.channel_id,
parent_id, parent_id,
selected_channel.id, selected_channel.id,
cx, cx,
) ),
.detach_and_log_err(cx) None => channel_store.link_channel(
}) clipboard.channel_id,
selected_channel.id,
cx,
),
} }
_ => panel.channel_store.update(cx, |channel_store, cx| {
channel_store
.link_channel(clipboard.channel_id, selected_channel.id, cx)
.detach_and_log_err(cx) .detach_and_log_err(cx)
}), }),
} }
@ -304,45 +263,24 @@ pub fn init(cx: &mut AppContext) {
}, },
); );
cx.add_action(
|panel: &mut CollabPanel, action: &LinkChannel, cx: &mut ViewContext<CollabPanel>| {
if let Some(clipboard) = panel.channel_clipboard.take() {
panel.channel_store.update(cx, |channel_store, cx| {
channel_store
.link_channel(clipboard.channel_id, action.to, cx)
.detach_and_log_err(cx)
})
}
},
);
cx.add_action( cx.add_action(
|panel: &mut CollabPanel, action: &MoveChannel, cx: &mut ViewContext<CollabPanel>| { |panel: &mut CollabPanel, action: &MoveChannel, cx: &mut ViewContext<CollabPanel>| {
if let Some(clipboard) = panel.channel_clipboard.take() { if let Some(clipboard) = panel.channel_clipboard.take() {
panel.channel_store.update(cx, |channel_store, cx| { panel.channel_store.update(cx, |channel_store, cx| {
if let Some(parent) = clipboard.parent_id { match clipboard.parent_id {
channel_store Some(parent_id) => channel_store.move_channel(
.move_channel(clipboard.channel_id, parent, action.to, cx) clipboard.channel_id,
.detach_and_log_err(cx) parent_id,
} else { action.to,
channel_store cx,
.link_channel(clipboard.channel_id, action.to, cx) ),
.detach_and_log_err(cx) None => channel_store.link_channel(clipboard.channel_id, action.to, cx),
} }
.detach_and_log_err(cx)
}) })
} }
}, },
); );
cx.add_action(
|panel: &mut CollabPanel, action: &UnlinkChannel, cx: &mut ViewContext<CollabPanel>| {
panel.channel_store.update(cx, |channel_store, cx| {
channel_store
.unlink_channel(action.channel_id, action.parent_id, cx)
.detach_and_log_err(cx)
})
},
);
} }
#[derive(Debug)] #[derive(Debug)]
@ -2235,18 +2173,11 @@ impl CollabPanel {
this.deploy_channel_context_menu(Some(e.position), &path, ix, cx); this.deploy_channel_context_menu(Some(e.position), &path, ix, cx);
} }
}) })
.on_up(MouseButton::Left, move |e, this, cx| { .on_up(MouseButton::Left, move |_, this, cx| {
if let Some((_, dragged_channel)) = cx if let Some((_, dragged_channel)) = cx
.global::<DragAndDrop<Workspace>>() .global::<DragAndDrop<Workspace>>()
.currently_dragged::<DraggedChannel>(cx.window()) .currently_dragged::<DraggedChannel>(cx.window())
{ {
if e.modifiers.alt {
this.channel_store.update(cx, |channel_store, cx| {
channel_store
.link_channel(dragged_channel.0.id, channel_id, cx)
.detach_and_log_err(cx)
})
} else {
this.channel_store.update(cx, |channel_store, cx| { this.channel_store.update(cx, |channel_store, cx| {
match dragged_channel.1 { match dragged_channel.1 {
Some(parent_id) => channel_store.move_channel( Some(parent_id) => channel_store.move_channel(
@ -2255,14 +2186,11 @@ impl CollabPanel {
channel_id, channel_id,
cx, cx,
), ),
None => { None => channel_store.link_channel(dragged_channel.0.id, channel_id, cx),
channel_store.link_channel(dragged_channel.0.id, channel_id, cx)
}
} }
.detach_and_log_err(cx) .detach_and_log_err(cx)
}) })
} }
}
}) })
.on_move({ .on_move({
let channel = channel.clone(); let channel = channel.clone();
@ -2288,18 +2216,10 @@ impl CollabPanel {
}) })
.as_draggable( .as_draggable(
(channel.clone(), path.parent_id()), (channel.clone(), path.parent_id()),
move |modifiers, (channel, _), cx: &mut ViewContext<Workspace>| { move |_, (channel, _), cx: &mut ViewContext<Workspace>| {
let theme = &theme::current(cx).collab_panel; let theme = &theme::current(cx).collab_panel;
Flex::<Workspace>::row() Flex::<Workspace>::row()
.with_children(modifiers.alt.then(|| {
Svg::new("icons/plus.svg")
.with_color(theme.channel_hash.color)
.constrained()
.with_width(theme.channel_hash.width)
.aligned()
.left()
}))
.with_child( .with_child(
Svg::new("icons/hash.svg") Svg::new("icons/hash.svg")
.with_color(theme.channel_hash.color) .with_color(theme.channel_hash.color)
@ -2721,7 +2641,11 @@ impl CollabPanel {
}, },
)); ));
if self.channel_store.read(cx).is_user_admin(path.channel_id()) { if self
.channel_store
.read(cx)
.is_channel_admin(path.channel_id())
{
let parent_id = path.parent_id(); let parent_id = path.parent_id();
items.extend([ items.extend([
@ -2738,20 +2662,6 @@ impl CollabPanel {
location: path.clone(), location: path.clone(),
}, },
), ),
ContextMenuItem::Separator,
]);
if let Some(parent_id) = parent_id {
items.push(ContextMenuItem::action(
"Unlink from parent",
UnlinkChannel {
channel_id: path.channel_id(),
parent_id,
},
));
}
items.extend([
ContextMenuItem::action( ContextMenuItem::action(
"Move this channel", "Move this channel",
StartMoveChannelFor { StartMoveChannelFor {
@ -2759,13 +2669,6 @@ impl CollabPanel {
parent_id, parent_id,
}, },
), ),
ContextMenuItem::action(
"Link this channel",
StartLinkChannelFor {
channel_id: path.channel_id(),
parent_id,
},
),
]); ]);
if let Some(channel_name) = channel_name { if let Some(channel_name) = channel_name {
@ -2776,12 +2679,6 @@ impl CollabPanel {
to: path.channel_id(), to: path.channel_id(),
}, },
)); ));
items.push(ContextMenuItem::action(
format!("Link '#{}' here", channel_name),
LinkChannel {
to: path.channel_id(),
},
));
} }
items.extend([ items.extend([
@ -3160,7 +3057,7 @@ impl CollabPanel {
fn rename_channel(&mut self, action: &RenameChannel, cx: &mut ViewContext<Self>) { fn rename_channel(&mut self, action: &RenameChannel, cx: &mut ViewContext<Self>) {
let channel_store = self.channel_store.read(cx); let channel_store = self.channel_store.read(cx);
if !channel_store.is_user_admin(action.location.channel_id()) { if !channel_store.is_channel_admin(action.location.channel_id()) {
return; return;
} }
if let Some(channel) = channel_store if let Some(channel) = channel_store

View file

@ -88,8 +88,10 @@ impl View for CollabTitlebarItem {
.zip(peer_id) .zip(peer_id)
.zip(ActiveCall::global(cx).read(cx).room().cloned()) .zip(ActiveCall::global(cx).read(cx).room().cloned())
{ {
if room.read(cx).can_publish() {
right_container right_container
.add_children(self.render_in_call_share_unshare_button(&workspace, &theme, cx)); .add_children(self.render_in_call_share_unshare_button(&workspace, &theme, cx));
}
right_container.add_child(self.render_leave_call(&theme, cx)); right_container.add_child(self.render_leave_call(&theme, cx));
let muted = room.read(cx).is_muted(cx); let muted = room.read(cx).is_muted(cx);
let speaking = room.read(cx).is_speaking(); let speaking = room.read(cx).is_speaking();
@ -97,9 +99,14 @@ impl View for CollabTitlebarItem {
self.render_current_user(&workspace, &theme, &user, peer_id, muted, speaking, cx), self.render_current_user(&workspace, &theme, &user, peer_id, muted, speaking, cx),
); );
left_container.add_children(self.render_collaborators(&workspace, &theme, &room, cx)); left_container.add_children(self.render_collaborators(&workspace, &theme, &room, cx));
if room.read(cx).can_publish() {
right_container.add_child(self.render_toggle_mute(&theme, &room, cx)); right_container.add_child(self.render_toggle_mute(&theme, &room, cx));
}
right_container.add_child(self.render_toggle_deafen(&theme, &room, cx)); right_container.add_child(self.render_toggle_deafen(&theme, &room, cx));
right_container.add_child(self.render_toggle_screen_sharing_button(&theme, &room, cx)); if room.read(cx).can_publish() {
right_container
.add_child(self.render_toggle_screen_sharing_button(&theme, &room, cx));
}
} }
let status = workspace.read(cx).client().status(); let status = workspace.read(cx).client().status();

View file

@ -477,7 +477,7 @@ impl NotificationPanel {
return panel.read_with(cx, |panel, cx| { return panel.read_with(cx, |panel, cx| {
panel.is_scrolled_to_bottom() panel.is_scrolled_to_bottom()
&& panel.active_chat().map_or(false, |chat| { && panel.active_chat().map_or(false, |chat| {
chat.read(cx).channel().id == *channel_id chat.read(cx).channel_id == *channel_id
}) })
}); });
} }

View file

@ -306,6 +306,16 @@ impl live_kit_server::api::Client for TestApiClient {
token::VideoGrant::to_join(room), token::VideoGrant::to_join(room),
) )
} }
fn guest_token(&self, room: &str, identity: &str) -> Result<String> {
let server = TestServer::get(&self.url)?;
token::create(
&server.api_key,
&server.secret_key,
Some(identity),
token::VideoGrant::for_guest(room),
)
}
} }
pub type Sid = String; pub type Sid = String;

View file

@ -12,6 +12,7 @@ pub trait Client: Send + Sync {
async fn delete_room(&self, name: String) -> Result<()>; async fn delete_room(&self, name: String) -> Result<()>;
async fn remove_participant(&self, room: String, identity: String) -> Result<()>; async fn remove_participant(&self, room: String, identity: String) -> Result<()>;
fn room_token(&self, room: &str, identity: &str) -> Result<String>; fn room_token(&self, room: &str, identity: &str) -> Result<String>;
fn guest_token(&self, room: &str, identity: &str) -> Result<String>;
} }
#[derive(Clone)] #[derive(Clone)]
@ -138,4 +139,13 @@ impl Client for LiveKitClient {
token::VideoGrant::to_join(room), token::VideoGrant::to_join(room),
) )
} }
fn guest_token(&self, room: &str, identity: &str) -> Result<String> {
token::create(
&self.key,
&self.secret,
Some(identity),
token::VideoGrant::for_guest(room),
)
}
} }

View file

@ -57,6 +57,15 @@ impl<'a> VideoGrant<'a> {
..Default::default() ..Default::default()
} }
} }
pub fn for_guest(room: &'a str) -> Self {
Self {
room: Some(Cow::Borrowed(room)),
room_join: Some(true),
can_subscribe: Some(true),
..Default::default()
}
}
} }
pub fn create( pub fn create(

View file

@ -342,6 +342,7 @@ message RoomUpdated {
message LiveKitConnectionInfo { message LiveKitConnectionInfo {
string server_url = 1; string server_url = 1;
string token = 2; string token = 2;
bool can_publish = 3;
} }
message ShareProject { message ShareProject {
@ -977,7 +978,6 @@ message UpdateChannels {
repeated Channel channel_invitations = 5; repeated Channel channel_invitations = 5;
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 UnseenChannelMessage unseen_channel_messages = 9; repeated UnseenChannelMessage unseen_channel_messages = 9;
repeated UnseenChannelBufferChange unseen_channel_buffer_changes = 10; repeated UnseenChannelBufferChange unseen_channel_buffer_changes = 10;
} }
@ -1585,6 +1585,7 @@ message Channel {
uint64 id = 1; uint64 id = 1;
string name = 2; string name = 2;
ChannelVisibility visibility = 3; ChannelVisibility visibility = 3;
ChannelRole role = 4;
} }
message Contact { message Contact {