feat: use upstream rust nostr

This commit is contained in:
2024-10-15 10:37:07 +07:00
parent e158f2e4d7
commit ba9c81a10a
5 changed files with 30 additions and 74 deletions

22
src-tauri/Cargo.lock generated
View File

@@ -2965,7 +2965,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4" checksum = "4979f22fdb869068da03c9f7528f8297c6fd2606bc3a4affe42e6a823fdb8da4"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"windows-targets 0.52.6", "windows-targets 0.48.5",
] ]
[[package]] [[package]]
@@ -3362,7 +3362,7 @@ checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8"
[[package]] [[package]]
name = "nostr" name = "nostr"
version = "0.35.0" version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#82f39b5655bd58f045194b14a2cda5ef119acef1" source = "git+https://github.com/rust-nostr/nostr#55c4014bc2ba301a91ba14b8363bbe44f5ee6cb6"
dependencies = [ dependencies = [
"aes", "aes",
"base64 0.22.1", "base64 0.22.1",
@@ -3392,7 +3392,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-database" name = "nostr-database"
version = "0.35.0" version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#82f39b5655bd58f045194b14a2cda5ef119acef1" source = "git+https://github.com/rust-nostr/nostr#55c4014bc2ba301a91ba14b8363bbe44f5ee6cb6"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"flatbuffers", "flatbuffers",
@@ -3406,7 +3406,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-lmdb" name = "nostr-lmdb"
version = "0.35.0" version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#82f39b5655bd58f045194b14a2cda5ef119acef1" source = "git+https://github.com/rust-nostr/nostr#55c4014bc2ba301a91ba14b8363bbe44f5ee6cb6"
dependencies = [ dependencies = [
"heed", "heed",
"nostr", "nostr",
@@ -3419,7 +3419,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-relay-builder" name = "nostr-relay-builder"
version = "0.35.0" version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#82f39b5655bd58f045194b14a2cda5ef119acef1" source = "git+https://github.com/rust-nostr/nostr#55c4014bc2ba301a91ba14b8363bbe44f5ee6cb6"
dependencies = [ dependencies = [
"async-utility", "async-utility",
"async-wsocket", "async-wsocket",
@@ -3434,7 +3434,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-relay-pool" name = "nostr-relay-pool"
version = "0.35.0" version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#82f39b5655bd58f045194b14a2cda5ef119acef1" source = "git+https://github.com/rust-nostr/nostr#55c4014bc2ba301a91ba14b8363bbe44f5ee6cb6"
dependencies = [ dependencies = [
"async-utility", "async-utility",
"async-wsocket", "async-wsocket",
@@ -3452,7 +3452,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-sdk" name = "nostr-sdk"
version = "0.35.0" version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#82f39b5655bd58f045194b14a2cda5ef119acef1" source = "git+https://github.com/rust-nostr/nostr#55c4014bc2ba301a91ba14b8363bbe44f5ee6cb6"
dependencies = [ dependencies = [
"async-utility", "async-utility",
"atomic-destructor", "atomic-destructor",
@@ -3472,7 +3472,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-signer" name = "nostr-signer"
version = "0.35.0" version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#82f39b5655bd58f045194b14a2cda5ef119acef1" source = "git+https://github.com/rust-nostr/nostr#55c4014bc2ba301a91ba14b8363bbe44f5ee6cb6"
dependencies = [ dependencies = [
"async-utility", "async-utility",
"nostr", "nostr",
@@ -3485,7 +3485,7 @@ dependencies = [
[[package]] [[package]]
name = "nostr-zapper" name = "nostr-zapper"
version = "0.35.0" version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#82f39b5655bd58f045194b14a2cda5ef119acef1" source = "git+https://github.com/rust-nostr/nostr#55c4014bc2ba301a91ba14b8363bbe44f5ee6cb6"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"nostr", "nostr",
@@ -3629,7 +3629,7 @@ dependencies = [
[[package]] [[package]]
name = "nwc" name = "nwc"
version = "0.35.0" version = "0.35.0"
source = "git+https://github.com/rust-nostr/nostr#82f39b5655bd58f045194b14a2cda5ef119acef1" source = "git+https://github.com/rust-nostr/nostr#55c4014bc2ba301a91ba14b8363bbe44f5ee6cb6"
dependencies = [ dependencies = [
"async-utility", "async-utility",
"nostr", "nostr",
@@ -7029,7 +7029,7 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [ dependencies = [
"windows-sys 0.59.0", "windows-sys 0.48.0",
] ]
[[package]] [[package]]

View File

@@ -41,10 +41,7 @@ pub async fn get_event(id: String, state: State<'_, Nostr>) -> Result<RichEvent,
Ok(RichEvent { raw, parsed }) Ok(RichEvent { raw, parsed })
} else { } else {
match client match client
.get_events_of( .fetch_events(vec![filter], Some(Duration::from_secs(10)))
vec![filter],
EventSource::relays(Some(Duration::from_secs(10))),
)
.await .await
{ {
Ok(events) => { Ok(events) => {
@@ -81,10 +78,7 @@ pub async fn get_event_from(
let filter = Filter::new().id(event_id); let filter = Filter::new().id(event_id);
match client match client
.get_events_of( .fetch_events(vec![filter], Some(Duration::from_secs(5)))
vec![filter],
EventSource::both(Some(Duration::from_secs(5))),
)
.await .await
{ {
Ok(events) => { Ok(events) => {
@@ -113,10 +107,7 @@ pub async fn get_replies(id: String, state: State<'_, Nostr>) -> Result<Vec<Rich
let filter = Filter::new().kinds(vec![Kind::TextNote]).event(event_id); let filter = Filter::new().kinds(vec![Kind::TextNote]).event(event_id);
match client match client
.get_events_of( .fetch_events(vec![filter], Some(Duration::from_secs(5)))
vec![filter],
EventSource::both(Some(Duration::from_secs(5))),
)
.await .await
{ {
Ok(events) => { Ok(events) => {
@@ -175,10 +166,7 @@ pub async fn get_all_events_by_author(
.author(author) .author(author)
.limit(limit as usize); .limit(limit as usize);
match client match client.database().query(vec![filter]).await {
.get_events_of(vec![filter], EventSource::Database)
.await
{
Ok(events) => Ok(process_event(client, events).await), Ok(events) => Ok(process_event(client, events).await),
Err(err) => Err(err.to_string()), Err(err) => Err(err.to_string()),
} }
@@ -209,10 +197,7 @@ pub async fn get_all_events_by_authors(
.until(as_of) .until(as_of)
.authors(authors); .authors(authors);
match client match client.database().query(vec![filter]).await {
.get_events_of(vec![filter], EventSource::Database)
.await
{
Ok(events) => Ok(process_event(client, events).await), Ok(events) => Ok(process_event(client, events).await),
Err(err) => Err(err.to_string()), Err(err) => Err(err.to_string()),
} }
@@ -239,10 +224,7 @@ pub async fn get_all_events_by_hashtags(
.hashtags(hashtags); .hashtags(hashtags);
match client match client
.get_events_of( .fetch_events(vec![filter], Some(Duration::from_secs(5)))
vec![filter],
EventSource::both(Some(Duration::from_secs(5))),
)
.await .await
{ {
Ok(events) => Ok(process_event(client, events).await), Ok(events) => Ok(process_event(client, events).await),
@@ -293,10 +275,7 @@ pub async fn get_global_events(
.until(as_of); .until(as_of);
match client match client
.get_events_of( .fetch_events(vec![filter], Some(Duration::from_secs(5)))
vec![filter],
EventSource::both(Some(Duration::from_secs(5))),
)
.await .await
{ {
Ok(events) => Ok(process_event(client, events).await), Ok(events) => Ok(process_event(client, events).await),
@@ -477,13 +456,11 @@ pub async fn user_to_bech32(user: String, state: State<'_, Nostr>) -> Result<Str
let public_key = PublicKey::parse(user).map_err(|err| err.to_string())?; let public_key = PublicKey::parse(user).map_err(|err| err.to_string())?;
match client match client
.get_events_of( .database()
vec![Filter::new() .query(vec![Filter::new()
.author(public_key) .author(public_key)
.kind(Kind::RelayList) .kind(Kind::RelayList)
.limit(1)], .limit(1)])
EventSource::both(Some(Duration::from_secs(5))),
)
.await .await
{ {
Ok(events) => match events.first() { Ok(events) => match events.first() {

View File

@@ -59,10 +59,7 @@ pub async fn get_profile(id: Option<String>, state: State<'_, Nostr>) -> Result<
} }
} else { } else {
match client match client
.get_events_of( .fetch_events(vec![filter], Some(Duration::from_secs(10)))
vec![filter],
EventSource::relays(Some(Duration::from_secs(10))),
)
.await .await
{ {
Ok(events) => { Ok(events) => {
@@ -518,10 +515,7 @@ pub async fn copy_friend(npub: &str, state: State<'_, Nostr>) -> Result<bool, St
.limit(1); .limit(1);
if let Ok(contact_list_events) = client if let Ok(contact_list_events) = client
.get_events_of( .fetch_events(vec![contact_list_filter], Some(Duration::from_secs(5)))
vec![contact_list_filter],
EventSource::both(Some(Duration::from_secs(5))),
)
.await .await
{ {
for event in contact_list_events.into_iter() { for event in contact_list_events.into_iter() {

View File

@@ -5,7 +5,6 @@ use specta::Type;
use std::{ use std::{
fs::OpenOptions, fs::OpenOptions,
io::{self, BufRead, Write}, io::{self, BufRead, Write},
time::Duration,
}; };
use tauri::{path::BaseDirectory, Manager, State}; use tauri::{path::BaseDirectory, Manager, State};
@@ -37,16 +36,7 @@ pub async fn get_relays(state: State<'_, Nostr>) -> Result<Relays, String> {
.kind(Kind::RelayList) .kind(Kind::RelayList)
.limit(1); .limit(1);
match client match client.database().query(vec![filter]).await {
.get_events_of(
vec![filter],
EventSource::Relays {
timeout: Some(Duration::from_secs(5)),
specific_relays: None,
},
)
.await
{
Ok(events) => { Ok(events) => {
if let Some(event) = events.first() { if let Some(event) = events.first() {
let nip65_list = nip65::extract_relay_list(event).collect::<Vec<_>>(); let nip65_list = nip65::extract_relay_list(event).collect::<Vec<_>>();

View File

@@ -4,9 +4,7 @@ use nostr_sdk::prelude::*;
use reqwest::Client as ReqClient; use reqwest::Client as ReqClient;
use serde::Serialize; use serde::Serialize;
use specta::Type; use specta::Type;
use std::collections::HashSet; use std::{collections::HashSet, str::FromStr, time::Duration};
use std::str::FromStr;
use std::time::Duration;
use crate::RichEvent; use crate::RichEvent;
@@ -45,7 +43,7 @@ const NOSTR_MENTIONS: [&str; 10] = [
"Nostr:naddr1", "Nostr:naddr1",
]; ];
pub fn get_latest_event(events: &[Event]) -> Option<&Event> { pub fn get_latest_event(events: &Events) -> Option<&Event> {
events.iter().next() events.iter().next()
} }
@@ -130,7 +128,7 @@ pub fn create_tags(content: &str) -> Vec<Tag> {
tags tags
} }
pub async fn process_event(client: &Client, events: Vec<Event>) -> Vec<RichEvent> { pub async fn process_event(client: &Client, events: Events) -> Vec<RichEvent> {
// Remove event thread if event is TextNote // Remove event thread if event is TextNote
let events: Vec<Event> = events let events: Vec<Event> = events
.into_iter() .into_iter()
@@ -211,10 +209,7 @@ pub async fn init_nip65(client: &Client, public_key: &str) {
// client.connect_relay("ws://127.0.0.1:1984").await.unwrap(); // client.connect_relay("ws://127.0.0.1:1984").await.unwrap();
if let Ok(events) = client if let Ok(events) = client
.get_events_of( .fetch_events(vec![filter], Some(Duration::from_secs(5)))
vec![filter],
EventSource::relays(Some(Duration::from_secs(5))),
)
.await .await
{ {
if let Some(event) = events.first() { if let Some(event) = events.first() {