feat: improve splash screen and notification service
This commit is contained in:
@@ -40,7 +40,6 @@ fn main() {
|
||||
nostr::keys::user_to_bech32,
|
||||
nostr::keys::to_npub,
|
||||
nostr::keys::verify_nip05,
|
||||
nostr::metadata::run_notification,
|
||||
nostr::metadata::get_activities,
|
||||
nostr::metadata::get_current_user_profile,
|
||||
nostr::metadata::get_profile,
|
||||
|
||||
@@ -6,7 +6,7 @@ use serde::Serialize;
|
||||
use specta::Type;
|
||||
use std::str::FromStr;
|
||||
use std::time::Duration;
|
||||
use tauri::State;
|
||||
use tauri::{Manager, State};
|
||||
|
||||
#[derive(Serialize, Type)]
|
||||
pub struct Account {
|
||||
@@ -16,12 +16,26 @@ pub struct Account {
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn get_accounts() -> Result<String, String> {
|
||||
pub fn get_accounts() -> Result<Vec<String>, String> {
|
||||
let search = Search::new().unwrap();
|
||||
let results = search.by("Account", "nostr_secret");
|
||||
|
||||
match List::list_credentials(results, Limit::All) {
|
||||
Ok(list) => Ok(list),
|
||||
Ok(list) => {
|
||||
let search: Vec<String> = list
|
||||
.split_whitespace()
|
||||
.map(|v| v.to_string())
|
||||
.filter(|v| v.starts_with("npub1"))
|
||||
.collect();
|
||||
|
||||
let accounts: Vec<String> = search
|
||||
.into_iter()
|
||||
.collect::<std::collections::HashSet<_>>()
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
Ok(accounts)
|
||||
}
|
||||
Err(_) => Err("Empty.".into()),
|
||||
}
|
||||
}
|
||||
@@ -86,7 +100,11 @@ pub async fn save_account(
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn load_account(npub: &str, state: State<'_, Nostr>) -> Result<bool, String> {
|
||||
pub async fn load_account(
|
||||
npub: &str,
|
||||
state: State<'_, Nostr>,
|
||||
app: tauri::AppHandle,
|
||||
) -> Result<bool, String> {
|
||||
let client = &state.client;
|
||||
let keyring = Entry::new(npub, "nostr_secret").unwrap();
|
||||
|
||||
@@ -122,12 +140,13 @@ pub async fn load_account(npub: &str, state: State<'_, Nostr>) -> Result<bool, S
|
||||
let signer = client.signer().await.unwrap();
|
||||
let public_key = signer.public_key().await.unwrap();
|
||||
|
||||
// Connect to user's relay
|
||||
let filter = Filter::new()
|
||||
.author(public_key)
|
||||
.kind(Kind::RelayList)
|
||||
.limit(1);
|
||||
|
||||
// Connect to user's relay (NIP-65)
|
||||
// #TODO: Let rust-nostr handle it
|
||||
match client
|
||||
.get_events_of(vec![filter], Some(Duration::from_secs(10)))
|
||||
.await
|
||||
@@ -164,6 +183,40 @@ pub async fn load_account(npub: &str, state: State<'_, Nostr>) -> Result<bool, S
|
||||
Err(_) => todo!(),
|
||||
};
|
||||
|
||||
// Run notification service
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let window = app.get_window("main").unwrap();
|
||||
let state = window.state::<Nostr>();
|
||||
let client = &state.client;
|
||||
let subscription = Filter::new()
|
||||
.pubkey(public_key)
|
||||
.kinds(vec![Kind::TextNote, Kind::Repost, Kind::ZapReceipt])
|
||||
.since(Timestamp::now());
|
||||
let activity_id = SubscriptionId::new("activity");
|
||||
|
||||
// Create a subscription for activity
|
||||
client
|
||||
.subscribe_with_id(activity_id.clone(), vec![subscription], None)
|
||||
.await;
|
||||
|
||||
// Handle notifications
|
||||
let _ = client
|
||||
.handle_notifications(|notification| async {
|
||||
if let RelayPoolNotification::Event {
|
||||
subscription_id,
|
||||
event,
|
||||
..
|
||||
} = notification
|
||||
{
|
||||
if subscription_id == activity_id {
|
||||
let _ = app.emit("activity", event.as_json());
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
})
|
||||
.await;
|
||||
});
|
||||
|
||||
Ok(true)
|
||||
}
|
||||
Err(err) => Err(err.to_string()),
|
||||
|
||||
@@ -2,53 +2,9 @@ use crate::Nostr;
|
||||
use keyring::Entry;
|
||||
use nostr_sdk::prelude::*;
|
||||
use std::{str::FromStr, time::Duration};
|
||||
use tauri::{Manager, State};
|
||||
use tauri::State;
|
||||
use url::Url;
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub fn run_notification(accounts: Vec<String>, app: tauri::AppHandle) -> Result<(), ()> {
|
||||
tauri::async_runtime::spawn(async move {
|
||||
let window = app.get_window("main").unwrap();
|
||||
let state = window.state::<Nostr>();
|
||||
let client = &state.client;
|
||||
let pubkeys: Vec<PublicKey> = accounts
|
||||
.into_iter()
|
||||
.map(|f| PublicKey::from_bech32(f).unwrap())
|
||||
.collect();
|
||||
let subscription = Filter::new()
|
||||
.pubkeys(pubkeys)
|
||||
.kinds(vec![Kind::TextNote, Kind::Repost, Kind::ZapReceipt])
|
||||
.since(Timestamp::now());
|
||||
let activity_id = SubscriptionId::new("activity");
|
||||
|
||||
// Create a subscription for activity
|
||||
client
|
||||
.subscribe_with_id(activity_id.clone(), vec![subscription], None)
|
||||
.await;
|
||||
|
||||
// Handle notifications
|
||||
let _ = client
|
||||
.handle_notifications(|notification| async {
|
||||
if let RelayPoolNotification::Event {
|
||||
subscription_id,
|
||||
event,
|
||||
..
|
||||
} = notification
|
||||
{
|
||||
if subscription_id == activity_id {
|
||||
println!("new notification: {}", event.as_json());
|
||||
let _ = app.emit("activity", event.as_json());
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
})
|
||||
.await;
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
pub async fn get_activities(
|
||||
|
||||
Reference in New Issue
Block a user