wip: nip96 upload
This commit is contained in:
@@ -5,3 +5,4 @@ pub const NEW_MESSAGE_SUB_ID: &str = "listen_new_giftwrap";
|
|||||||
pub const ALL_MESSAGES_SUB_ID: &str = "listen_all_giftwraps";
|
pub const ALL_MESSAGES_SUB_ID: &str = "listen_all_giftwraps";
|
||||||
pub const METADATA_DELAY: u64 = 200;
|
pub const METADATA_DELAY: u64 = 200;
|
||||||
pub const IMAGE_SERVICE: &str = "https://wsrv.nl";
|
pub const IMAGE_SERVICE: &str = "https://wsrv.nl";
|
||||||
|
pub const NIP96_SERVER: &str = "https://nostrcheck.me";
|
||||||
|
|||||||
@@ -5,6 +5,19 @@ use std::{
|
|||||||
hash::{DefaultHasher, Hash, Hasher},
|
hash::{DefaultHasher, Hash, Hasher},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::{constants::NIP96_SERVER, get_client};
|
||||||
|
|
||||||
|
pub async fn nip96_upload(file: Vec<u8>) -> anyhow::Result<Url, anyhow::Error> {
|
||||||
|
let client = get_client();
|
||||||
|
let signer = client.signer().await?;
|
||||||
|
let server_url = Url::parse(NIP96_SERVER)?;
|
||||||
|
|
||||||
|
let config: ServerConfig = nip96::get_server_config(server_url, None).await?;
|
||||||
|
let url = nip96::upload_data(&signer, &config, file, None, None).await?;
|
||||||
|
|
||||||
|
Ok(url)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn room_hash(tags: &Tags) -> u64 {
|
pub fn room_hash(tags: &Tags) -> u64 {
|
||||||
let pubkeys: Vec<PublicKey> = tags.public_keys().copied().collect();
|
let pubkeys: Vec<PublicKey> = tags.public_keys().copied().collect();
|
||||||
let mut hasher = DefaultHasher::new();
|
let mut hasher = DefaultHasher::new();
|
||||||
|
|||||||
@@ -1,18 +1,21 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
get_client,
|
get_client,
|
||||||
states::chat::room::Room,
|
states::chat::room::Room,
|
||||||
utils::{ago, compare},
|
utils::{ago, compare, nip96_upload},
|
||||||
};
|
};
|
||||||
|
use async_utility::task::spawn;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
div, list, px, AnyElement, AppContext, Context, EventEmitter, Flatten, FocusHandle,
|
div, img, list, px, AnyElement, AppContext, Context, EventEmitter, Flatten, FocusHandle,
|
||||||
FocusableView, IntoElement, ListAlignment, ListState, Model, ParentElement, PathPromptOptions,
|
FocusableView, InteractiveElement, IntoElement, ListAlignment, ListState, Model, ObjectFit,
|
||||||
Pixels, Render, SharedString, Styled, View, ViewContext, VisualContext, WeakModel, WeakView,
|
ParentElement, PathPromptOptions, Pixels, Render, SharedString, StatefulInteractiveElement,
|
||||||
WindowContext,
|
Styled, StyledImage, View, ViewContext, VisualContext, WeakModel, WeakView, WindowContext,
|
||||||
};
|
};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use message::Message;
|
use message::Message;
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
|
use smol::fs;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use tokio::sync::oneshot;
|
||||||
use ui::{
|
use ui::{
|
||||||
button::{Button, ButtonVariants},
|
button::{Button, ButtonVariants},
|
||||||
dock_area::{
|
dock_area::{
|
||||||
@@ -21,6 +24,7 @@ use ui::{
|
|||||||
},
|
},
|
||||||
input::{InputEvent, TextInput},
|
input::{InputEvent, TextInput},
|
||||||
popup_menu::PopupMenu,
|
popup_menu::PopupMenu,
|
||||||
|
prelude::FluentBuilder,
|
||||||
theme::{scale::ColorScaleStep, ActiveTheme},
|
theme::{scale::ColorScaleStep, ActiveTheme},
|
||||||
v_flex, Icon, IconName,
|
v_flex, Icon, IconName,
|
||||||
};
|
};
|
||||||
@@ -44,7 +48,11 @@ pub struct ChatPanel {
|
|||||||
room: Model<Room>,
|
room: Model<Room>,
|
||||||
state: Model<State>,
|
state: Model<State>,
|
||||||
list: ListState,
|
list: ListState,
|
||||||
|
// New Message
|
||||||
input: View<TextInput>,
|
input: View<TextInput>,
|
||||||
|
// Media
|
||||||
|
attaches: Model<Option<Vec<Url>>>,
|
||||||
|
is_uploading: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ChatPanel {
|
impl ChatPanel {
|
||||||
@@ -68,6 +76,7 @@ impl ChatPanel {
|
|||||||
.cleanable()
|
.cleanable()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// List
|
||||||
let state = cx.new_model(|_| State {
|
let state = cx.new_model(|_| State {
|
||||||
count: 0,
|
count: 0,
|
||||||
items: vec![],
|
items: vec![],
|
||||||
@@ -107,6 +116,8 @@ impl ChatPanel {
|
|||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
|
|
||||||
|
let attaches = cx.new_model(|_| None);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
closeable: true,
|
closeable: true,
|
||||||
zoomable: true,
|
zoomable: true,
|
||||||
@@ -115,10 +126,12 @@ impl ChatPanel {
|
|||||||
list: ListState::new(0, ListAlignment::Bottom, Pixels(256.), move |_, _| {
|
list: ListState::new(0, ListAlignment::Bottom, Pixels(256.), move |_, _| {
|
||||||
div().into_any_element()
|
div().into_any_element()
|
||||||
}),
|
}),
|
||||||
|
is_uploading: false,
|
||||||
id,
|
id,
|
||||||
name,
|
name,
|
||||||
input,
|
input,
|
||||||
state,
|
state,
|
||||||
|
attaches,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -291,6 +304,52 @@ impl ChatPanel {
|
|||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn upload(&mut self, cx: &mut ViewContext<Self>) {
|
||||||
|
let attaches = self.attaches.clone();
|
||||||
|
|
||||||
|
let paths = cx.prompt_for_paths(PathPromptOptions {
|
||||||
|
files: true,
|
||||||
|
directories: false,
|
||||||
|
multiple: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
cx.spawn(move |_, mut async_cx| async move {
|
||||||
|
match Flatten::flatten(paths.await.map_err(|e| e.into())) {
|
||||||
|
Ok(Some(mut paths)) => {
|
||||||
|
let path = paths.pop().unwrap();
|
||||||
|
|
||||||
|
if let Ok(file_data) = fs::read(path).await {
|
||||||
|
let (tx, rx) = oneshot::channel::<Url>();
|
||||||
|
|
||||||
|
spawn(async move {
|
||||||
|
if let Ok(url) = nip96_upload(file_data).await {
|
||||||
|
_ = tx.send(url);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if let Ok(url) = rx.await {
|
||||||
|
_ = async_cx.update_model(&attaches, |model, cx| {
|
||||||
|
if let Some(model) = model.as_mut() {
|
||||||
|
model.push(url);
|
||||||
|
} else {
|
||||||
|
*model = Some(vec![url]);
|
||||||
|
}
|
||||||
|
cx.notify();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(None) => {}
|
||||||
|
Err(_) => {}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove(&mut self, cx: &mut ViewContext<Self>) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Panel for ChatPanel {
|
impl Panel for ChatPanel {
|
||||||
@@ -343,44 +402,56 @@ impl Render for ChatPanel {
|
|||||||
.child(
|
.child(
|
||||||
div()
|
div()
|
||||||
.flex_shrink_0()
|
.flex_shrink_0()
|
||||||
.w_full()
|
|
||||||
.h_12()
|
|
||||||
.flex()
|
.flex()
|
||||||
.items_center()
|
.flex_col()
|
||||||
.gap_2()
|
.gap_1()
|
||||||
.px_2()
|
.when_some(self.attaches.read(cx).as_ref(), |this, attaches| {
|
||||||
.child(
|
this.flex()
|
||||||
Button::new("upload")
|
.items_center()
|
||||||
.icon(Icon::new(IconName::Upload))
|
.gap_1p5()
|
||||||
.ghost()
|
.px_2()
|
||||||
.on_click(|_, cx| {
|
.children(attaches.iter().map(|url| {
|
||||||
let paths = cx.prompt_for_paths(PathPromptOptions {
|
let path: SharedString = url.to_string().into();
|
||||||
files: true,
|
|
||||||
directories: false,
|
|
||||||
multiple: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
cx.spawn(move |_async_cx| async move {
|
div()
|
||||||
match Flatten::flatten(paths.await.map_err(|e| e.into())) {
|
.id(path.clone())
|
||||||
Ok(Some(paths)) => {
|
.child(
|
||||||
// TODO: upload file to blossom server
|
img(path)
|
||||||
println!("Paths: {:?}", paths)
|
.h_16()
|
||||||
}
|
.rounded(px(cx.theme().radius))
|
||||||
Ok(None) => {}
|
.object_fit(ObjectFit::ScaleDown),
|
||||||
Err(_) => {}
|
)
|
||||||
}
|
.on_click(cx.listener(move |this, _, cx| {
|
||||||
})
|
this.remove(cx);
|
||||||
.detach();
|
}))
|
||||||
}),
|
}))
|
||||||
)
|
})
|
||||||
.child(
|
.child(
|
||||||
div()
|
div()
|
||||||
.flex_1()
|
.w_full()
|
||||||
|
.h_12()
|
||||||
.flex()
|
.flex()
|
||||||
.bg(cx.theme().base.step(cx, ColorScaleStep::FOUR))
|
.items_center()
|
||||||
.rounded(px(cx.theme().radius))
|
.gap_2()
|
||||||
.px_2()
|
.px_2()
|
||||||
.child(self.input.clone()),
|
.child(
|
||||||
|
Button::new("upload")
|
||||||
|
.icon(Icon::new(IconName::Upload))
|
||||||
|
.ghost()
|
||||||
|
.on_click(cx.listener(move |this, _, cx| {
|
||||||
|
this.upload(cx);
|
||||||
|
}))
|
||||||
|
.loading(self.is_uploading),
|
||||||
|
)
|
||||||
|
.child(
|
||||||
|
div()
|
||||||
|
.flex_1()
|
||||||
|
.flex()
|
||||||
|
.bg(cx.theme().base.step(cx, ColorScaleStep::FOUR))
|
||||||
|
.rounded(px(cx.theme().radius))
|
||||||
|
.px_2()
|
||||||
|
.child(self.input.clone()),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user