Skip to content

Commit 3fc5dc6

Browse files
committed
formatting
1 parent 74b9146 commit 3fc5dc6

File tree

8 files changed

+75
-57
lines changed

8 files changed

+75
-57
lines changed

src/chat/src/channel.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ pub trait Channel: Clone {
88
fn get_receiver(&self) -> broadcast::Receiver<Self::Message>;
99

1010
fn send_message(&self, content: String, nonce: String) -> Self::Message;
11-
}
11+
}

src/chat/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
pub mod message;
21
pub mod channel;
2+
pub mod message;

src/chat/src/message.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use gpui::{Element};
1+
use gpui::Element;
22

33
pub trait Message: Clone {
44
fn get_author(&self) -> &impl MessageAuthor;
@@ -10,4 +10,4 @@ pub trait Message: Clone {
1010
pub trait MessageAuthor {
1111
fn get_display_name(&self) -> impl Element;
1212
fn get_icon(&self) -> String;
13-
}
13+
}

src/discord/src/channel/mod.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ use std::sync::Arc;
33
use scope_chat::channel::Channel;
44
use tokio::sync::{broadcast, RwLock};
55

6-
use crate::{client::DiscordClient, message::{author::{DiscordMessageAuthor, DisplayName}, content::DiscordMessageContent, DiscordMessage}, snowflake::Snowflake};
6+
use crate::{
7+
client::DiscordClient,
8+
message::{
9+
author::{DiscordMessageAuthor, DisplayName},
10+
content::DiscordMessageContent,
11+
DiscordMessage,
12+
},
13+
snowflake::Snowflake,
14+
};
715

816
pub struct DiscordChannel {
917
channel_id: Snowflake,
@@ -44,7 +52,10 @@ impl Channel for DiscordChannel {
4452

4553
DiscordMessage {
4654
content: DiscordMessageContent { content, is_pending: true },
47-
author: DiscordMessageAuthor { display_name: DisplayName("Pending".to_owned()), icon: "".to_owned() },
55+
author: DiscordMessageAuthor {
56+
display_name: DisplayName("Pending".to_owned()),
57+
icon: "".to_owned(),
58+
},
4859
id: Snowflake { content: 0 },
4960
nonce: Some(nonce),
5061
}

src/discord/src/client.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
use std::{
2-
collections::HashMap, rc::Rc, sync::{Arc, OnceLock}
2+
collections::HashMap,
3+
fs::File,
4+
rc::Rc,
5+
sync::{Arc, OnceLock},
36
};
47

58
use serenity::{
6-
all::{Context, EventHandler, GatewayIntents, Message},
9+
all::{ChannelId, Context, CreateMessage, Event, EventHandler, GatewayIntents, Message, Nonce, RawEventHandler},
710
async_trait,
811
futures::SinkExt,
912
};
13+
use std::io::Write;
1014
use tokio::sync::{broadcast, Mutex, RwLock};
1115

1216
use crate::{
@@ -28,7 +32,11 @@ impl DiscordClient {
2832
pub async fn new(token: String) -> Arc<DiscordClient> {
2933
let client = Arc::new(DiscordClient::default());
3034

31-
let mut discord = serenity::Client::builder(token, GatewayIntents::all()).event_handler_arc(client.clone()).await.expect("Error creating client");
35+
let mut discord = serenity::Client::builder(token, GatewayIntents::all())
36+
.event_handler_arc(client.clone())
37+
.raw_event_handler(RawClient(client.clone()))
38+
.await
39+
.expect("Error creating client");
3240

3341
if let Err(why) = discord.start().await {
3442
panic!("Client error: {why:?}");
@@ -48,7 +56,6 @@ impl DiscordClient {
4856
}
4957

5058
pub async fn send_message(&self, channel_id: Snowflake, content: String, nonce: String) {
51-
println!("All the way to discord~! {:?} {:?}", channel_id, content);
5259
ChannelId::new(channel_id.content)
5360
.send_message(
5461
self.discord().http.clone(),
@@ -59,23 +66,26 @@ impl DiscordClient {
5966
}
6067
}
6168

69+
struct RawClient(Arc<DiscordClient>);
70+
6271
#[async_trait]
63-
impl EventHandler for DiscordClient {
64-
async fn ready(&self, _: Context, data_about_bot: serenity::model::prelude::Ready) {
65-
println!("Ready! {:?}", data_about_bot);
72+
impl RawEventHandler for RawClient {
73+
async fn raw_event(&self, ctx: Context, ev: serenity::model::prelude::Event) {
74+
if let Event::Unknown(unk) = ev {
75+
writeln!(&mut File::create(unk.kind).unwrap(), "{:#?}", unk.value);
76+
}
6677
}
78+
}
6779

80+
#[async_trait]
81+
impl EventHandler for DiscordClient {
6882
async fn message(&self, _: Context, msg: Message) {
69-
println!("Got message: {:?} {:?}", msg.channel_id, msg.content);
70-
7183
let snowflake = Snowflake {
7284
content: msg.channel_id.get(),
7385
};
7486

7587
if let Some(vec) = self.channel_message_event_handlers.read().await.get(&snowflake) {
7688
for sender in vec {
77-
println!("Sending to sender!");
78-
7989
let _ = sender.send(DiscordMessage {
8090
id: snowflake,
8191
author: DiscordMessageAuthor {

src/ui/src/app_state.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct GlobalAppState();
99
impl Global for GlobalAppState {}
1010

1111
impl AppState {
12-
pub fn set_global(_app_state: Weak<AppState>, cx: &mut AppContext) {
13-
cx.set_global(GlobalAppState());
14-
}
15-
}
12+
pub fn set_global(_app_state: Weak<AppState>, cx: &mut AppContext) {
13+
cx.set_global(GlobalAppState());
14+
}
15+
}

src/ui/src/channel/message_list.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use gpui::{ListState, Pixels};
1+
use gpui::{div, IntoElement, ListAlignment, ListState, ParentElement, Pixels};
22
use scope_backend_discord::{
33
message::{
44
author::{DiscordMessageAuthor, DisplayName},
@@ -46,14 +46,9 @@ impl<M: Message> MessageList<M> {
4646
pub fn create_list_state(&self) -> ListState {
4747
let clone = self.clone();
4848

49-
ListState::new(
50-
clone.length(),
51-
ListAlignment::Bottom,
52-
Pixels(20.),
53-
move |idx, _cx| {
54-
let item = clone.get(idx).unwrap().clone();
55-
div().child(message(item)).into_any_element()
56-
},
57-
)
49+
ListState::new(clone.length(), ListAlignment::Bottom, Pixels(20.), move |idx, _cx| {
50+
let item = clone.get(idx).unwrap().clone();
51+
div().child(message(item)).into_any_element()
52+
})
5853
}
5954
}

src/ui/src/channel/mod.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ pub mod message_list;
44
use std::ops::Deref;
55

66
use components::input::{InputEvent, TextInput};
7-
use gpui::{div, list, rgb, AppContext, Context, IntoElement, ListAlignment, ListState, Model, ParentElement, Pixels, Render, SharedString, Styled, View, VisualContext};
7+
use gpui::{
8+
div, list, rgb, AppContext, Context, IntoElement, ListAlignment, ListState, Model, ParentElement, Pixels, Render, SharedString, Styled, View,
9+
VisualContext,
10+
};
811
use message::message;
912
use message_list::MessageList;
1013
use scope_backend_discord::message::DiscordMessage;
@@ -29,30 +32,35 @@ impl<M: Message + 'static> ChannelView<M> {
2932
.foreground_executor()
3033
.spawn(async move {
3134
loop {
32-
let message = channel.get_receiver().recv().await.unwrap();
35+
let message = channel_listener.get_receiver().recv().await.unwrap();
3336

34-
async_model.update(&mut async_ctx, |data, ctx| {
35-
data.add_external_message(message);
36-
ctx.notify();
37-
}).unwrap();
38-
}
39-
}).detach();
37+
async_model
38+
.update(&mut async_ctx, |data, ctx| {
39+
data.add_external_message(message);
40+
ctx.notify();
41+
})
42+
.unwrap();
43+
}
44+
})
45+
.detach();
4046

41-
ctx.observe(&state_model, |this: &mut ChannelView<M>, model, cx| {
42-
this.list_state = model.read(cx).create_list_state();
43-
cx.notify();
44-
}).detach();
47+
ctx
48+
.observe(&state_model, |this: &mut ChannelView<M>, model, cx| {
49+
this.list_state = model.read(cx).create_list_state();
50+
cx.notify();
51+
})
52+
.detach();
4553

4654
let message_input = ctx.new_view(|cx| {
4755
let mut input = components::input::TextInput::new(cx);
48-
56+
4957
input.set_size(components::Size::Large, cx);
50-
58+
5159
input
5260
});
5361

54-
ctx.subscribe(&message_input, move |channel_view, text_input, input_event, ctx| {
55-
match input_event {
62+
ctx
63+
.subscribe(&message_input, move |channel_view, text_input, input_event, ctx| match input_event {
5664
InputEvent::PressEnter => {
5765
let content = text_input.read(ctx).text().to_string();
5866
let channel_sender = channel.clone();
@@ -69,9 +77,9 @@ impl<M: Message + 'static> ChannelView<M> {
6977
});
7078
}
7179
_ => {}
72-
}
73-
}).detach();
74-
80+
})
81+
.detach();
82+
7583
ChannelView::<M> {
7684
list_state: state_model.read(ctx).create_list_state(),
7785
list_model: state_model,
@@ -85,12 +93,6 @@ impl<M: Message + 'static> ChannelView<M> {
8593

8694
impl<M: Message + 'static> Render for ChannelView<M> {
8795
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl gpui::IntoElement {
88-
div()
89-
.flex()
90-
.flex_col()
91-
.w_full()
92-
.h_full()
93-
.child(list(self.list_state.clone()).w_full().h_full())
94-
.child(self.message_input.clone())
96+
div().flex().flex_col().w_full().h_full().child(list(self.list_state.clone()).w_full().h_full()).child(self.message_input.clone())
9597
}
9698
}

0 commit comments

Comments
 (0)