wip: refactor
This commit is contained in:
@@ -28,3 +28,4 @@ rust-embed.workspace = true
|
||||
smol.workspace = true
|
||||
|
||||
tracing-subscriber = { version = "0.3.18", features = ["fmt"] }
|
||||
random_name_generator = "0.3.6"
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
use gpui::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
use rnglib::{Language, RNG};
|
||||
use serde::Deserialize;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use super::metadata::MetadataRegistry;
|
||||
use crate::utils::get_room_id;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Deserialize)]
|
||||
@@ -12,31 +14,40 @@ pub struct Room {
|
||||
pub members: Vec<PublicKey>,
|
||||
pub last_seen: Timestamp,
|
||||
pub title: Option<SharedString>,
|
||||
pub metadata: Option<Metadata>,
|
||||
}
|
||||
|
||||
impl Room {
|
||||
pub fn new(event: &Event) -> Self {
|
||||
pub fn new(event: &Event, cx: &mut WindowContext<'_>) -> Self {
|
||||
let owner = event.pubkey;
|
||||
let last_seen = event.created_at;
|
||||
|
||||
// Get all members from event's tag
|
||||
let members: Vec<PublicKey> = event.tags.public_keys().copied().collect();
|
||||
|
||||
// Get title from event's tag
|
||||
let title = if let Some(tag) = event.tags.find(TagKind::Title) {
|
||||
tag.content().map(|s| s.to_owned().into())
|
||||
} else {
|
||||
// TODO: create random name?
|
||||
None
|
||||
let rng = RNG::from(&Language::Roman);
|
||||
let name = rng.generate_names(2, true).join("-").to_lowercase();
|
||||
|
||||
Some(name.into())
|
||||
};
|
||||
|
||||
// Get unique id based on members
|
||||
let id = get_room_id(&owner, &members).into();
|
||||
|
||||
// Get metadata for all members if exists
|
||||
let metadata = cx.global::<MetadataRegistry>().get(&owner);
|
||||
|
||||
Self {
|
||||
id,
|
||||
title,
|
||||
members,
|
||||
last_seen,
|
||||
owner,
|
||||
metadata,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,8 +35,8 @@ impl MetadataRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, public_key: PublicKey) -> Option<Metadata> {
|
||||
self.profiles.read().unwrap().get(&public_key).cloned()
|
||||
pub fn get(&self, public_key: &PublicKey) -> Option<Metadata> {
|
||||
self.profiles.read().unwrap().get(public_key).cloned()
|
||||
}
|
||||
|
||||
fn new() -> Self {
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use coop_ui::{
|
||||
button::Button,
|
||||
dock::{Panel, PanelEvent, PanelState},
|
||||
popup_menu::PopupMenu,
|
||||
};
|
||||
use gpui::*;
|
||||
use room::ChatRoom;
|
||||
use std::sync::Arc;
|
||||
use nostr_sdk::prelude::*;
|
||||
use room::RoomPanel;
|
||||
|
||||
use crate::states::chat::Room;
|
||||
|
||||
@@ -20,14 +22,18 @@ pub struct ChatPanel {
|
||||
focus_handle: FocusHandle,
|
||||
// Room
|
||||
id: SharedString,
|
||||
room: View<ChatRoom>,
|
||||
room: View<RoomPanel>,
|
||||
metadata: Option<Metadata>,
|
||||
}
|
||||
|
||||
impl ChatPanel {
|
||||
pub fn new(room: &Arc<Room>, cx: &mut WindowContext) -> View<Self> {
|
||||
let id = room.id.clone();
|
||||
let title = room.title.clone();
|
||||
let metadata = room.metadata.clone();
|
||||
|
||||
let room = cx.new_view(|cx| {
|
||||
let view = ChatRoom::new(room, cx);
|
||||
let view = RoomPanel::new(room, cx);
|
||||
// Load messages
|
||||
view.load(cx);
|
||||
// Subscribe for new messages
|
||||
@@ -37,21 +43,26 @@ impl ChatPanel {
|
||||
});
|
||||
|
||||
cx.new_view(|cx| Self {
|
||||
name: "Chat".into(),
|
||||
name: title.unwrap_or("Untitled".into()),
|
||||
closeable: true,
|
||||
zoomable: true,
|
||||
focus_handle: cx.focus_handle(),
|
||||
id,
|
||||
room,
|
||||
metadata,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Panel for ChatPanel {
|
||||
fn panel_name(&self) -> SharedString {
|
||||
fn panel_id(&self) -> SharedString {
|
||||
self.id.clone()
|
||||
}
|
||||
|
||||
fn panel_metadata(&self) -> Option<Metadata> {
|
||||
self.metadata.clone()
|
||||
}
|
||||
|
||||
fn title(&self, _cx: &WindowContext) -> AnyElement {
|
||||
self.name.clone().into_any_element()
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ pub struct Messages {
|
||||
items: Vec<RoomMessage>,
|
||||
}
|
||||
|
||||
pub struct ChatRoom {
|
||||
pub struct RoomPanel {
|
||||
owner: PublicKey,
|
||||
members: Arc<[PublicKey]>,
|
||||
// Form
|
||||
@@ -34,7 +34,7 @@ pub struct ChatRoom {
|
||||
messages: Model<Messages>,
|
||||
}
|
||||
|
||||
impl ChatRoom {
|
||||
impl RoomPanel {
|
||||
pub fn new(room: &Arc<Room>, cx: &mut ViewContext<'_, Self>) -> Self {
|
||||
let members: Arc<[PublicKey]> = room.members.clone().into();
|
||||
let owner = room.owner;
|
||||
@@ -125,7 +125,7 @@ impl ChatRoom {
|
||||
// Get user's metadata
|
||||
let metadata = async_cx
|
||||
.read_global::<MetadataRegistry, _>(|state, _cx| {
|
||||
state.get(ev.pubkey)
|
||||
state.get(&ev.pubkey)
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
@@ -231,7 +231,7 @@ impl ChatRoom {
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for ChatRoom {
|
||||
impl Render for RoomPanel {
|
||||
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl IntoElement {
|
||||
v_flex()
|
||||
.size_full()
|
||||
|
||||
@@ -70,7 +70,7 @@ impl InboxItem {
|
||||
}
|
||||
|
||||
pub fn action(&self, cx: &mut WindowContext<'_>) {
|
||||
let room = Arc::new(Room::new(&self.event));
|
||||
let room = Arc::new(Room::new(&self.event, cx));
|
||||
|
||||
cx.dispatch_action(Box::new(AddPanel {
|
||||
room,
|
||||
|
||||
@@ -40,7 +40,7 @@ impl LeftDock {
|
||||
}
|
||||
|
||||
impl Panel for LeftDock {
|
||||
fn panel_name(&self) -> SharedString {
|
||||
fn panel_id(&self) -> SharedString {
|
||||
"LeftDock".into()
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ impl WelcomePanel {
|
||||
}
|
||||
|
||||
impl Panel for WelcomePanel {
|
||||
fn panel_name(&self) -> SharedString {
|
||||
fn panel_id(&self) -> SharedString {
|
||||
"WelcomePanel".into()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user