update
This commit is contained in:
@@ -84,28 +84,6 @@ pub async fn get_replies(id: String, state: State<'_, Nostr>) -> Result<Vec<Rich
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn subscribe_to(id: String, state: State<'_, Nostr>) -> Result<(), String> {
|
||||
let client = &state.client;
|
||||
|
||||
let subscription_id = SubscriptionId::new(&id);
|
||||
let event_id = EventId::parse(&id).map_err(|err| err.to_string())?;
|
||||
|
||||
let filter = Filter::new()
|
||||
.kinds(vec![Kind::TextNote])
|
||||
.event(event_id)
|
||||
.since(Timestamp::now());
|
||||
|
||||
match client
|
||||
.subscribe_with_id(subscription_id, vec![filter], None)
|
||||
.await
|
||||
{
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn get_all_events_by_author(
|
||||
@@ -178,10 +156,7 @@ pub async fn get_all_events_by_hashtags(
|
||||
.until(as_of)
|
||||
.hashtags(hashtags);
|
||||
|
||||
match client
|
||||
.fetch_events(vec![filter], Some(Duration::from_secs(5)))
|
||||
.await
|
||||
{
|
||||
match client.database().query(vec![filter]).await {
|
||||
Ok(events) => Ok(process_event(client, events, false).await),
|
||||
Err(err) => Err(err.to_string()),
|
||||
}
|
||||
|
||||
@@ -1,126 +1,9 @@
|
||||
use nostr_sdk::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
fs::{self, File},
|
||||
str::FromStr,
|
||||
};
|
||||
use tauri::{ipc::Channel, AppHandle, Manager, State};
|
||||
use tauri_specta::Event as TauriEvent;
|
||||
use std::fs::{self, File};
|
||||
use tauri::{ipc::Channel, Manager, State};
|
||||
|
||||
use crate::Nostr;
|
||||
|
||||
#[derive(Clone, Serialize, Type, TauriEvent)]
|
||||
pub struct NegentropyEvent {
|
||||
kind: NegentropyKind,
|
||||
total_event: i32,
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize, Type)]
|
||||
pub enum NegentropyKind {
|
||||
Profile,
|
||||
Metadata,
|
||||
Events,
|
||||
EventIds,
|
||||
Global,
|
||||
Notification,
|
||||
Others,
|
||||
}
|
||||
|
||||
pub fn sync_all(accounts: Vec<String>, app_handle: AppHandle) {
|
||||
if accounts.is_empty() {
|
||||
return;
|
||||
};
|
||||
|
||||
let public_keys: Vec<PublicKey> = accounts
|
||||
.iter()
|
||||
.filter_map(|acc| {
|
||||
if let Ok(pk) = PublicKey::from_str(acc) {
|
||||
Some(pk)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let state = app_handle.state::<Nostr>();
|
||||
let client = &state.client;
|
||||
let bootstrap_relays = state.bootstrap_relays.lock().unwrap().clone();
|
||||
|
||||
// NEG: Sync events for all pubkeys in local database
|
||||
//
|
||||
if let Ok(events) = client
|
||||
.database()
|
||||
.query(vec![Filter::new()
|
||||
.authors(public_keys)
|
||||
.kinds(vec![Kind::ContactList, Kind::FollowSet])])
|
||||
.await
|
||||
{
|
||||
let set: HashSet<PublicKey> = events
|
||||
.iter()
|
||||
.flat_map(|ev| ev.tags.public_keys().copied())
|
||||
.collect();
|
||||
|
||||
let pubkeys: Vec<PublicKey> = set.into_iter().collect();
|
||||
|
||||
for chunk in pubkeys.chunks(500) {
|
||||
if chunk.is_empty() {
|
||||
break;
|
||||
}
|
||||
|
||||
let authors = chunk.to_owned();
|
||||
|
||||
// NEG: Sync event
|
||||
//
|
||||
let events = Filter::new()
|
||||
.authors(authors.clone())
|
||||
.kinds(vec![Kind::TextNote, Kind::Repost])
|
||||
.limit(500);
|
||||
|
||||
if let Ok(output) = client
|
||||
.sync_with(&bootstrap_relays, events, &SyncOptions::default())
|
||||
.await
|
||||
{
|
||||
NegentropyEvent {
|
||||
kind: NegentropyKind::Events,
|
||||
total_event: output.received.len() as i32,
|
||||
}
|
||||
.emit(&app_handle)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
// NEG: Sync metadata
|
||||
//
|
||||
let events = Filter::new()
|
||||
.authors(authors.clone())
|
||||
.kinds(vec![
|
||||
Kind::Metadata,
|
||||
Kind::InterestSet,
|
||||
Kind::Interests,
|
||||
Kind::FollowSet,
|
||||
Kind::EventDeletion,
|
||||
Kind::Custom(30315),
|
||||
])
|
||||
.limit(500);
|
||||
|
||||
if let Ok(output) = client
|
||||
.sync_with(&bootstrap_relays, events, &SyncOptions::default())
|
||||
.await
|
||||
{
|
||||
NegentropyEvent {
|
||||
kind: NegentropyKind::Metadata,
|
||||
total_event: output.received.len() as i32,
|
||||
}
|
||||
.emit(&app_handle)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn is_account_sync(id: String, app_handle: tauri::AppHandle) -> Result<bool, String> {
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
use border::WebviewWindowExt as BorderWebviewWindowExt;
|
||||
use nostr_sdk::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use tauri::utils::config::WindowEffectsConfig;
|
||||
@@ -14,6 +16,9 @@ use tauri::{WebviewBuilder, WebviewWindowBuilder};
|
||||
#[cfg(target_os = "windows")]
|
||||
use tauri_plugin_decorum::WebviewWindowExt;
|
||||
|
||||
use crate::common::get_last_segment;
|
||||
use crate::Nostr;
|
||||
|
||||
#[derive(Serialize, Deserialize, Type)]
|
||||
pub struct NewWindow {
|
||||
label: String,
|
||||
@@ -55,8 +60,101 @@ pub async fn create_column(
|
||||
.transparent(true)
|
||||
.on_page_load(|webview, payload| match payload.event() {
|
||||
PageLoadEvent::Started => {
|
||||
let url = payload.url();
|
||||
println!("#TODO, preload: {}", url)
|
||||
if let Ok(id) = get_last_segment(payload.url()) {
|
||||
if let Ok(public_key) = PublicKey::from_str(&id) {
|
||||
let is_newsfeed = payload.url().to_string().contains("newsfeed");
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let state = webview.state::<Nostr>();
|
||||
let client = &state.client;
|
||||
let relays = &state.bootstrap_relays.lock().unwrap().clone();
|
||||
|
||||
if is_newsfeed {
|
||||
if let Ok(contact_list) =
|
||||
client.database().contacts_public_keys(public_key).await
|
||||
{
|
||||
let opts = SyncOptions::default();
|
||||
let subscription_id =
|
||||
SubscriptionId::new(webview.label());
|
||||
let filter =
|
||||
Filter::new().authors(contact_list).kinds(vec![
|
||||
Kind::TextNote,
|
||||
Kind::Repost,
|
||||
Kind::EventDeletion,
|
||||
]);
|
||||
|
||||
if let Err(e) = client
|
||||
.subscribe_with_id(
|
||||
subscription_id,
|
||||
vec![filter.clone().since(Timestamp::now())],
|
||||
None,
|
||||
)
|
||||
.await
|
||||
{
|
||||
println!("Subscription error: {}", e);
|
||||
}
|
||||
|
||||
if let Ok(output) = client
|
||||
.sync_with(relays, filter.limit(1000), &opts)
|
||||
.await
|
||||
{
|
||||
println!("Success: {:?}", output.success.len());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let opts = SyncOptions::default();
|
||||
let filter = Filter::new()
|
||||
.author(public_key)
|
||||
.kinds(vec![
|
||||
Kind::Interests,
|
||||
Kind::InterestSet,
|
||||
Kind::FollowSet,
|
||||
Kind::Bookmarks,
|
||||
Kind::BookmarkSet,
|
||||
Kind::TextNote,
|
||||
Kind::Repost,
|
||||
Kind::Custom(30315),
|
||||
])
|
||||
.limit(500);
|
||||
|
||||
if let Ok(output) =
|
||||
client.sync_with(relays, filter, &opts).await
|
||||
{
|
||||
println!("Success: {:?}", output.success.len());
|
||||
}
|
||||
}
|
||||
});
|
||||
} else if let Ok(event_id) = EventId::from_str(&id) {
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let state = webview.state::<Nostr>();
|
||||
let client = &state.client;
|
||||
let relays = &state.bootstrap_relays.lock().unwrap().clone();
|
||||
|
||||
let opts = SyncOptions::default();
|
||||
let subscription_id = SubscriptionId::new(webview.label());
|
||||
let filter = Filter::new()
|
||||
.event(event_id)
|
||||
.kinds(vec![Kind::TextNote, Kind::Custom(1111)]);
|
||||
|
||||
if let Err(e) = client
|
||||
.subscribe_with_id(
|
||||
subscription_id,
|
||||
vec![filter.clone().since(Timestamp::now())],
|
||||
None,
|
||||
)
|
||||
.await
|
||||
{
|
||||
println!("Subscription error: {}", e);
|
||||
}
|
||||
|
||||
if let Ok(output) =
|
||||
client.sync_with(relays, filter, &opts).await
|
||||
{
|
||||
println!("Success: {:?}", output.success.len());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
PageLoadEvent::Finished => {
|
||||
println!("{} finished loading", payload.url());
|
||||
@@ -80,7 +178,7 @@ pub async fn create_column(
|
||||
pub async fn close_column(label: String, app_handle: tauri::AppHandle) -> Result<bool, String> {
|
||||
match app_handle.get_webview(&label) {
|
||||
Some(webview) => Ok(webview.close().is_ok()),
|
||||
None => Err("Cannot close, column not found.".into()),
|
||||
None => Err(format!("Cannot close, column not found: {}", label)),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -143,6 +143,14 @@ pub fn get_all_accounts() -> Vec<String> {
|
||||
accounts.into_iter().collect()
|
||||
}
|
||||
|
||||
pub fn get_last_segment(url: &Url) -> Result<String, String> {
|
||||
url.path_segments()
|
||||
.ok_or("No segments".to_string())?
|
||||
.last()
|
||||
.ok_or("No items".into())
|
||||
.map(String::from)
|
||||
}
|
||||
|
||||
pub async fn process_event(client: &Client, events: Events, is_reply: bool) -> Vec<RichEvent> {
|
||||
// Remove event thread if event is TextNote
|
||||
let events: Vec<Event> = if !is_reply {
|
||||
|
||||
@@ -12,7 +12,6 @@ use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use specta_typescript::Typescript;
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
fs,
|
||||
io::{self, BufRead},
|
||||
str::FromStr,
|
||||
@@ -22,7 +21,7 @@ use std::{
|
||||
use tauri::{path::BaseDirectory, Emitter, EventTarget, Manager};
|
||||
use tauri_plugin_decorum::WebviewWindowExt;
|
||||
use tauri_plugin_notification::{NotificationExt, PermissionState};
|
||||
use tauri_specta::{collect_commands, collect_events, Builder, Event as TauriEvent};
|
||||
use tauri_specta::{collect_commands, Builder, Event as TauriEvent};
|
||||
|
||||
pub mod commands;
|
||||
pub mod common;
|
||||
@@ -32,7 +31,6 @@ pub struct Nostr {
|
||||
settings: Mutex<Settings>,
|
||||
accounts: Mutex<Vec<String>>,
|
||||
bootstrap_relays: Mutex<Vec<Url>>,
|
||||
subscriptions: Mutex<HashSet<SubscriptionId>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize, Type)]
|
||||
@@ -66,20 +64,6 @@ impl Default for Settings {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Type)]
|
||||
enum SubscriptionMethod {
|
||||
Subscribe,
|
||||
Unsubscribe,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Type, TauriEvent)]
|
||||
struct Subscription {
|
||||
label: String,
|
||||
kind: SubscriptionMethod,
|
||||
event_id: Option<String>,
|
||||
contacts: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Type, TauriEvent)]
|
||||
struct Sync {
|
||||
id: String,
|
||||
@@ -92,72 +76,69 @@ pub const NOTIFICATION_SUB_ID: &str = "lume_notification";
|
||||
fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let builder = Builder::<tauri::Wry>::new()
|
||||
.commands(collect_commands![
|
||||
sync_account,
|
||||
is_account_sync,
|
||||
get_relays,
|
||||
connect_relay,
|
||||
remove_relay,
|
||||
get_bootstrap_relays,
|
||||
save_bootstrap_relays,
|
||||
get_accounts,
|
||||
watch_account,
|
||||
import_account,
|
||||
connect_account,
|
||||
get_private_key,
|
||||
delete_account,
|
||||
reset_password,
|
||||
has_signer,
|
||||
set_signer,
|
||||
get_profile,
|
||||
set_profile,
|
||||
get_contact_list,
|
||||
set_contact_list,
|
||||
is_contact,
|
||||
toggle_contact,
|
||||
get_all_profiles,
|
||||
set_group,
|
||||
get_group,
|
||||
get_all_groups,
|
||||
set_interest,
|
||||
get_interest,
|
||||
get_all_interests,
|
||||
set_wallet,
|
||||
load_wallet,
|
||||
remove_wallet,
|
||||
zap_profile,
|
||||
zap_event,
|
||||
copy_friend,
|
||||
get_notifications,
|
||||
get_user_settings,
|
||||
set_user_settings,
|
||||
verify_nip05,
|
||||
get_meta_from_event,
|
||||
get_event,
|
||||
get_replies,
|
||||
subscribe_to,
|
||||
get_all_events_by_author,
|
||||
get_all_events_by_authors,
|
||||
get_all_events_by_hashtags,
|
||||
get_local_events,
|
||||
get_global_events,
|
||||
search,
|
||||
publish,
|
||||
reply,
|
||||
repost,
|
||||
is_reposted,
|
||||
request_delete,
|
||||
is_deleted_event,
|
||||
event_to_bech32,
|
||||
user_to_bech32,
|
||||
create_column,
|
||||
update_column,
|
||||
reload_column,
|
||||
close_column,
|
||||
open_window,
|
||||
])
|
||||
.events(collect_events![Subscription, NegentropyEvent]);
|
||||
let builder = Builder::<tauri::Wry>::new().commands(collect_commands![
|
||||
sync_account,
|
||||
is_account_sync,
|
||||
get_relays,
|
||||
connect_relay,
|
||||
remove_relay,
|
||||
get_bootstrap_relays,
|
||||
save_bootstrap_relays,
|
||||
get_accounts,
|
||||
watch_account,
|
||||
import_account,
|
||||
connect_account,
|
||||
get_private_key,
|
||||
delete_account,
|
||||
reset_password,
|
||||
has_signer,
|
||||
set_signer,
|
||||
get_profile,
|
||||
set_profile,
|
||||
get_contact_list,
|
||||
set_contact_list,
|
||||
is_contact,
|
||||
toggle_contact,
|
||||
get_all_profiles,
|
||||
set_group,
|
||||
get_group,
|
||||
get_all_groups,
|
||||
set_interest,
|
||||
get_interest,
|
||||
get_all_interests,
|
||||
set_wallet,
|
||||
load_wallet,
|
||||
remove_wallet,
|
||||
zap_profile,
|
||||
zap_event,
|
||||
copy_friend,
|
||||
get_notifications,
|
||||
get_user_settings,
|
||||
set_user_settings,
|
||||
verify_nip05,
|
||||
get_meta_from_event,
|
||||
get_event,
|
||||
get_replies,
|
||||
get_all_events_by_author,
|
||||
get_all_events_by_authors,
|
||||
get_all_events_by_hashtags,
|
||||
get_local_events,
|
||||
get_global_events,
|
||||
search,
|
||||
publish,
|
||||
reply,
|
||||
repost,
|
||||
is_reposted,
|
||||
request_delete,
|
||||
is_deleted_event,
|
||||
event_to_bech32,
|
||||
user_to_bech32,
|
||||
create_column,
|
||||
update_column,
|
||||
reload_column,
|
||||
close_column,
|
||||
open_window,
|
||||
]);
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
builder
|
||||
@@ -170,12 +151,8 @@ fn main() {
|
||||
tauri_builder
|
||||
.invoke_handler(builder.invoke_handler())
|
||||
.setup(move |app| {
|
||||
builder.mount_events(app);
|
||||
|
||||
let handle = app.handle();
|
||||
let handle_clone = handle.clone();
|
||||
let handle_clone_child = handle_clone.clone();
|
||||
let handle_clone_child_child = handle_clone_child.clone();
|
||||
let main_window = app.get_webview_window("main").unwrap();
|
||||
|
||||
let config_dir = handle
|
||||
@@ -261,8 +238,6 @@ fn main() {
|
||||
});
|
||||
|
||||
let accounts = get_all_accounts();
|
||||
// Run sync for all accounts
|
||||
sync_all(accounts.clone(), handle_clone_child_child);
|
||||
|
||||
// Create global state
|
||||
app.manage(Nostr {
|
||||
@@ -270,98 +245,6 @@ fn main() {
|
||||
accounts: Mutex::new(accounts),
|
||||
settings: Mutex::new(Settings::default()),
|
||||
bootstrap_relays: Mutex::new(bootstrap_relays),
|
||||
subscriptions: Mutex::new(HashSet::new()),
|
||||
});
|
||||
|
||||
// Handle subscription request
|
||||
Subscription::listen_any(app, move |event| {
|
||||
let handle = handle_clone_child.to_owned();
|
||||
let payload = event.payload;
|
||||
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let state = handle.state::<Nostr>();
|
||||
let client = &state.client;
|
||||
|
||||
match payload.kind {
|
||||
SubscriptionMethod::Subscribe => {
|
||||
let subscription_id = SubscriptionId::new(payload.label);
|
||||
|
||||
if !client
|
||||
.pool()
|
||||
.subscriptions()
|
||||
.await
|
||||
.contains_key(&subscription_id)
|
||||
{
|
||||
// Update state
|
||||
state
|
||||
.subscriptions
|
||||
.lock()
|
||||
.unwrap()
|
||||
.insert(subscription_id.clone());
|
||||
|
||||
println!(
|
||||
"Total subscriptions: {}",
|
||||
state.subscriptions.lock().unwrap().len()
|
||||
);
|
||||
|
||||
if let Some(id) = payload.event_id {
|
||||
let event_id = EventId::from_str(&id).unwrap();
|
||||
let filter =
|
||||
Filter::new().event(event_id).since(Timestamp::now());
|
||||
|
||||
if let Err(e) = client
|
||||
.subscribe_with_id(
|
||||
subscription_id.clone(),
|
||||
vec![filter],
|
||||
None,
|
||||
)
|
||||
.await
|
||||
{
|
||||
println!("Subscription error: {}", e)
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ids) = payload.contacts {
|
||||
let authors: Vec<PublicKey> = ids
|
||||
.iter()
|
||||
.filter_map(|item| {
|
||||
if let Ok(pk) = PublicKey::from_str(item) {
|
||||
Some(pk)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
if let Err(e) = client
|
||||
.subscribe_with_id(
|
||||
subscription_id,
|
||||
vec![Filter::new()
|
||||
.kinds(vec![Kind::TextNote, Kind::Repost])
|
||||
.authors(authors)
|
||||
.since(Timestamp::now())],
|
||||
None,
|
||||
)
|
||||
.await
|
||||
{
|
||||
println!("Subscription error: {}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
SubscriptionMethod::Unsubscribe => {
|
||||
let subscription_id = SubscriptionId::new(payload.label);
|
||||
|
||||
println!(
|
||||
"Total subscriptions: {}",
|
||||
state.subscriptions.lock().unwrap().len()
|
||||
);
|
||||
|
||||
state.subscriptions.lock().unwrap().remove(&subscription_id);
|
||||
client.unsubscribe(subscription_id).await;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Run notification thread
|
||||
|
||||
Reference in New Issue
Block a user