feat: native context menu

This commit is contained in:
reya
2024-06-17 15:31:59 +07:00
parent 843895d876
commit d01cf8319d
10 changed files with 192 additions and 280 deletions

View File

@@ -59,6 +59,8 @@
"fs:allow-read-file",
"theme:allow-set-theme",
"theme:allow-get-theme",
"menu:allow-new",
"menu:allow-popup",
"http:default",
"shell:allow-open",
{

View File

@@ -1 +1 @@
{"desktop-capability":{"identifier":"desktop-capability","description":"Capability for the desktop","local":true,"windows":["main","panel","splash","settings","search","nwc","activity","zap-*","event-*","user-*","editor-*","column-*"],"permissions":["path:default","event:default","window:default","app:default","resources:default","menu:default","tray:default","notification:allow-is-permission-granted","notification:allow-request-permission","notification:default","os:allow-locale","os:allow-platform","os:allow-os-type","updater:default","updater:allow-check","updater:allow-download-and-install","window:allow-start-dragging","window:allow-create","window:allow-close","window:allow-set-focus","window:allow-center","window:allow-minimize","window:allow-maximize","window:allow-set-size","window:allow-set-focus","window:allow-start-dragging","decorum:allow-show-snap-overlay","clipboard-manager:allow-write-text","clipboard-manager:allow-read-text","webview:allow-create-webview-window","webview:allow-create-webview","webview:allow-set-webview-size","webview:allow-set-webview-position","webview:allow-webview-close","dialog:allow-open","dialog:allow-ask","dialog:allow-message","process:allow-restart","fs:allow-read-file","theme:allow-set-theme","theme:allow-get-theme","http:default","shell:allow-open",{"identifier":"http:default","allow":[{"url":"http://**/"},{"url":"https://**/"}]},{"identifier":"fs:allow-read-text-file","allow":[{"path":"$RESOURCE/locales/*"},{"path":"$RESOURCE/resources/*"}]}],"platforms":["linux","macOS","windows"]}}
{"desktop-capability":{"identifier":"desktop-capability","description":"Capability for the desktop","local":true,"windows":["main","panel","splash","settings","search","nwc","activity","zap-*","event-*","user-*","editor-*","column-*"],"permissions":["path:default","event:default","window:default","app:default","resources:default","menu:default","tray:default","notification:allow-is-permission-granted","notification:allow-request-permission","notification:default","os:allow-locale","os:allow-platform","os:allow-os-type","updater:default","updater:allow-check","updater:allow-download-and-install","window:allow-start-dragging","window:allow-create","window:allow-close","window:allow-set-focus","window:allow-center","window:allow-minimize","window:allow-maximize","window:allow-set-size","window:allow-set-focus","window:allow-start-dragging","decorum:allow-show-snap-overlay","clipboard-manager:allow-write-text","clipboard-manager:allow-read-text","webview:allow-create-webview-window","webview:allow-create-webview","webview:allow-set-webview-size","webview:allow-set-webview-position","webview:allow-webview-close","dialog:allow-open","dialog:allow-ask","dialog:allow-message","process:allow-restart","fs:allow-read-file","theme:allow-set-theme","theme:allow-get-theme","menu:allow-new","menu:allow-popup","http:default","shell:allow-open",{"identifier":"http:default","allow":[{"url":"http://**/"},{"url":"https://**/"}]},{"identifier":"fs:allow-read-text-file","allow":[{"path":"$RESOURCE/locales/*"},{"path":"$RESOURCE/resources/*"}]}],"platforms":["linux","macOS","windows"]}}

View File

@@ -57,8 +57,6 @@ fn main() {
nostr::keys::get_private_key,
nostr::keys::connect_remote_account,
nostr::keys::load_account,
nostr::keys::event_to_bech32,
nostr::keys::user_to_bech32,
nostr::keys::verify_nip05,
nostr::metadata::get_current_user_profile,
nostr::metadata::get_profile,
@@ -89,6 +87,8 @@ fn main() {
nostr::event::publish,
nostr::event::reply,
nostr::event::repost,
nostr::event::event_to_bech32,
nostr::event::user_to_bech32,
commands::folder::show_in_folder,
commands::window::create_column,
commands::window::close_column,

View File

@@ -91,7 +91,7 @@ pub async fn get_event_from(
return Err(err.to_string());
}
if (client.connect_relay(relay_hint).await).is_ok() {
if client.connect_relay(relay_hint).await.is_ok() {
match event_id {
Some(id) => {
match client
@@ -522,3 +522,79 @@ pub async fn repost(raw: &str, state: State<'_, Nostr>) -> Result<String, String
Err(err) => Err(err.to_string()),
}
}
#[tauri::command]
#[specta::specta]
pub async fn event_to_bech32(id: &str, state: State<'_, Nostr>) -> Result<String, String> {
let client = &state.client;
let event_id = match EventId::from_hex(id) {
Ok(id) => id,
Err(_) => return Err("ID is not valid.".into()),
};
let seens = client
.database()
.event_seen_on_relays(event_id)
.await
.unwrap();
match seens {
Some(set) => {
let relays = set.into_iter().collect::<Vec<_>>();
let event = Nip19Event::new(event_id, relays);
match event.to_bech32() {
Ok(id) => Ok(id),
Err(err) => Err(err.to_string()),
}
}
None => match event_id.to_bech32() {
Ok(id) => Ok(id),
Err(err) => Err(err.to_string()),
},
}
}
#[tauri::command]
#[specta::specta]
pub async fn user_to_bech32(user: &str, state: State<'_, Nostr>) -> Result<String, String> {
let client = &state.client;
let public_key = match PublicKey::from_str(user) {
Ok(pk) => pk,
Err(_) => return Err("Public Key is not valid.".into()),
};
match client
.get_events_of(
vec![Filter::new()
.author(public_key)
.kind(Kind::RelayList)
.limit(1)],
Some(Duration::from_secs(10)),
)
.await
{
Ok(events) => match events.first() {
Some(event) => {
let relay_list = nip65::extract_relay_list(event);
let relays = relay_list
.into_iter()
.map(|i| i.0.to_string())
.collect::<Vec<_>>();
let profile = Nip19Profile::new(public_key, relays).unwrap();
Ok(profile.to_bech32().unwrap())
}
None => match public_key.to_bech32() {
Ok(pk) => Ok(pk),
Err(err) => Err(err.to_string()),
},
},
Err(_) => match public_key.to_bech32() {
Ok(pk) => Ok(pk),
Err(err) => Err(err.to_string()),
},
}
}

View File

@@ -383,24 +383,6 @@ pub fn get_private_key(npub: &str) -> Result<String, String> {
}
}
#[tauri::command]
#[specta::specta]
pub fn event_to_bech32(id: &str, relays: Vec<String>) -> Result<String, ()> {
let event_id = EventId::from_hex(id).unwrap();
let event = Nip19Event::new(event_id, relays);
Ok(event.to_bech32().unwrap())
}
#[tauri::command]
#[specta::specta]
pub fn user_to_bech32(key: &str, relays: Vec<String>) -> Result<String, ()> {
let pubkey = PublicKey::from_str(key).unwrap();
let profile = Nip19Profile::new(pubkey, relays).unwrap();
Ok(profile.to_bech32().unwrap())
}
#[tauri::command]
#[specta::specta]
pub async fn verify_nip05(key: &str, nip05: &str) -> Result<bool, String> {