From 620b988e229e5d5f99851f93a731cd79d3a23892 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 19 Aug 2021 13:07:28 -0700 Subject: [PATCH] WIP --- server/src/tests.rs | 29 +++++++++++++++++---------- zed/src/channel.rs | 48 ++++++++++++++++++++++++++++++++++++--------- zed/src/rpc.rs | 14 +++++++------ 3 files changed, 66 insertions(+), 25 deletions(-) diff --git a/server/src/tests.rs b/server/src/tests.rs index 3846f68794..607929327f 100644 --- a/server/src/tests.rs +++ b/server/src/tests.rs @@ -14,6 +14,7 @@ use sqlx::{ }; use std::{path::Path, sync::Arc}; use zed::{ + channel::{ChannelDetails, ChannelList}, editor::Editor, fs::{FakeFs, Fs as _}, language::LanguageRegistry, @@ -514,17 +515,25 @@ async fn test_basic_chat(mut cx_a: TestAppContext, cx_b: TestAppContext) { .await .unwrap(); - // let channels_a = client_a.get_channels().await; - // assert_eq!(channels_a.len(), 1); - // assert_eq!(channels_a[0].read(&cx_a).name(), "test-channel"); + let channels_a = ChannelList::new(client_a, &mut cx_a.to_async()) + .await + .unwrap(); + let channels_b = ChannelList::new(client_b, &mut cx_b.to_async()) + .await + .unwrap(); + channels_a.read_with(&cx_a, |list, _| { + assert_eq!( + list.available_channels(), + &[ChannelDetails { + id: channel_id.to_proto(), + name: "test-channel".to_string() + }] + ) + }); - // assert_eq!( - // db.get_recent_channel_messages(channel_id, 50) - // .await - // .unwrap()[0] - // .body, - // "first message!" - // ); + let channel_a = channels_a.read_with(&cx_a, |this, cx| { + this.get_channel(channel_id.to_proto(), &cx).unwrap() + }); } struct TestServer { diff --git a/zed/src/channel.rs b/zed/src/channel.rs index 5fc21206b2..1c7d576c65 100644 --- a/zed/src/channel.rs +++ b/zed/src/channel.rs @@ -1,11 +1,14 @@ use crate::rpc::{self, Client}; -use anyhow::Result; -use gpui::{Entity, ModelContext, WeakModelHandle}; +use anyhow::{Context, Result}; +use gpui::{AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, WeakModelHandle}; use std::{ collections::{HashMap, VecDeque}, sync::Arc, }; -use zrpc::{proto::ChannelMessageSent, TypedEnvelope}; +use zrpc::{ + proto::{self, ChannelMessageSent}, + TypedEnvelope, +}; pub struct ChannelList { available_channels: Vec, @@ -13,10 +16,12 @@ pub struct ChannelList { rpc: Arc, } +#[derive(Debug, PartialEq)] pub struct ChannelDetails { - id: u64, - name: String, + pub id: u64, + pub name: String, } + pub struct Channel { details: ChannelDetails, first_message_id: Option, @@ -35,12 +40,28 @@ impl Entity for ChannelList { } impl ChannelList { - fn new(rpc: Arc) -> Self { - Self { - available_channels: Default::default(), + pub async fn new(rpc: Arc, cx: &mut AsyncAppContext) -> Result> { + let response = rpc + .request(proto::GetChannels {}) + .await + .context("failed to fetch available channels")?; + + Ok(cx.add_model(|_| Self { + available_channels: response.channels.into_iter().map(Into::into).collect(), channels: Default::default(), rpc, - } + })) + } + + pub fn available_channels(&self) -> &[ChannelDetails] { + &self.available_channels + } + + pub fn get_channel(&self, id: u64, cx: &AppContext) -> Option> { + self.channels + .get(&id) + .cloned() + .and_then(|handle| handle.upgrade(cx)) } } @@ -70,3 +91,12 @@ impl Channel { Ok(()) } } + +impl From for ChannelDetails { + fn from(message: proto::Channel) -> Self { + Self { + id: message.id, + name: message.name, + } + } +} diff --git a/zed/src/rpc.rs b/zed/src/rpc.rs index 902f17eb9e..a65551a7ff 100644 --- a/zed/src/rpc.rs +++ b/zed/src/rpc.rs @@ -46,12 +46,14 @@ pub struct Subscription { impl Drop for Subscription { fn drop(&mut self) { if let Some(client) = self.client.upgrade() { - client - .state - .write() - .model_handlers - .remove(&self.id) - .unwrap(); + drop( + client + .state + .write() + .model_handlers + .remove(&self.id) + .unwrap(), + ); } } }