Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/split commands #103

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ tauri-plugin-clipboard-manager = "2.2.1"
tauri-plugin-notification = "2.2.1"
nwc = { version = "0.38" }
lightning-invoice = "0.33.1"
async-trait = "0.1.86"

[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies]
nostr-sdk = { version = "0.38", features = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,10 @@
use crate::accounts::Account;
use crate::groups::{Group, GroupType};
use crate::invites::{Invite, InviteState, ProcessedInvite};
use crate::invites::{Invite, InviteState};
use crate::whitenoise::Whitenoise;
use nostr_sdk::prelude::*;
use serde::{Deserialize, Serialize};
use tauri::Emitter;

#[derive(Debug, Serialize, Deserialize)]
pub struct InvitesWithFailures {
invites: Vec<Invite>,
failures: Vec<(EventId, String)>,
}

/// Fetches invites from the database for the active user
#[tauri::command]
pub async fn get_invites(wn: tauri::State<'_, Whitenoise>) -> Result<InvitesWithFailures, String> {
let pending_invites = Invite::pending(wn.clone())
.await
.map_err(|e| e.to_string())?;

let failed_invites: Vec<(EventId, String)> = ProcessedInvite::failed_with_reason(wn.clone())
.await
.map_err(|e| e.to_string())?;

Ok(InvitesWithFailures {
invites: pending_invites,
failures: failed_invites,
})
}

/// Gets a specific invite by its ID.
///
/// # Arguments
/// * `invite_id` - The ID of the invite to retrieve
/// * `wn` - The Whitenoise state
///
/// # Returns
/// * `Ok(Invite)` if the invite was found
/// * `Err(String)` if there was an error retrieving the invite or it wasn't found
#[tauri::command]
pub async fn get_invite(
active_account: String,
invite_id: String,
wn: tauri::State<'_, Whitenoise>,
) -> Result<Invite, String> {
Invite::find_by_id(&active_account, &invite_id, wn.clone())
.await
.map_err(|e| e.to_string())
}

/// Accepts a group invite and joins the corresponding group.
///
/// # Arguments
Expand Down Expand Up @@ -143,34 +99,3 @@ pub async fn accept_invite(

Ok(())
}

/// Declines a group invite.
///
/// # Arguments
/// * `invite` - The invite to decline
/// * `wn` - The Whitenoise state
/// * `app_handle` - The Tauri app handle
///
/// # Returns
/// * `Ok(())` if the invite was successfully declined
/// * `Err(String)` if there was an error declining the invite
///
/// # Events Emitted
/// * `invite_declined` - Emitted with the updated invite after it is declined
#[tauri::command]
pub async fn decline_invite(
mut invite: Invite,
wn: tauri::State<'_, Whitenoise>,
app_handle: tauri::AppHandle,
) -> Result<(), String> {
tracing::debug!(target: "whitenoise::invites::decline_invite", "Declining invite {:?}", invite.event.id.unwrap());

invite.state = InviteState::Declined;
invite.save(wn.clone()).await.map_err(|e| e.to_string())?;

app_handle
.emit("invite_declined", invite)
.map_err(|e| e.to_string())?;

Ok(())
}
35 changes: 35 additions & 0 deletions src-tauri/src/commands/invites/decline_invite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::invites::{Invite, InviteState};
use crate::whitenoise::Whitenoise;
use nostr_sdk::prelude::*;
use tauri::Emitter;

/// Declines a group invite.
///
/// # Arguments
/// * `invite` - The invite to decline
/// * `wn` - The Whitenoise state
/// * `app_handle` - The Tauri app handle
///
/// # Returns
/// * `Ok(())` if the invite was successfully declined
/// * `Err(String)` if there was an error declining the invite
///
/// # Events Emitted
/// * `invite_declined` - Emitted with the updated invite after it is declined
#[tauri::command]
pub async fn decline_invite(
mut invite: Invite,
wn: tauri::State<'_, Whitenoise>,
app_handle: tauri::AppHandle,
) -> Result<(), String> {
tracing::debug!(target: "whitenoise::invites::decline_invite", "Declining invite {:?}", invite.event.id.unwrap());

invite.state = InviteState::Declined;
invite.save(wn.clone()).await.map_err(|e| e.to_string())?;

app_handle
.emit("invite_declined", invite)
.map_err(|e| e.to_string())?;

Ok(())
}
23 changes: 23 additions & 0 deletions src-tauri/src/commands/invites/get_invite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::invites::Invite;
use crate::whitenoise::Whitenoise;
use nostr_sdk::prelude::*;

/// Gets a specific invite by its ID.
///
/// # Arguments
/// * `invite_id` - The ID of the invite to retrieve
/// * `wn` - The Whitenoise state
///
/// # Returns
/// * `Ok(Invite)` if the invite was found
/// * `Err(String)` if there was an error retrieving the invite or it wasn't found
#[tauri::command]
pub async fn get_invite(
active_account: String,
invite_id: String,
wn: tauri::State<'_, Whitenoise>,
) -> Result<Invite, String> {
Invite::find_by_id(&active_account, &invite_id, wn.clone())
.await
.map_err(|e| e.to_string())
}
27 changes: 27 additions & 0 deletions src-tauri/src/commands/invites/get_invites.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::invites::{Invite, ProcessedInvite};
use crate::whitenoise::Whitenoise;
use nostr_sdk::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct InvitesWithFailures {
invites: Vec<Invite>,
failures: Vec<(EventId, String)>,
}

/// Fetches invites from the database for the active user
#[tauri::command]
pub async fn get_invites(wn: tauri::State<'_, Whitenoise>) -> Result<InvitesWithFailures, String> {
let pending_invites = Invite::pending(wn.clone())
.await
.map_err(|e| e.to_string())?;

let failed_invites: Vec<(EventId, String)> = ProcessedInvite::failed_with_reason(wn.clone())
.await
.map_err(|e| e.to_string())?;

Ok(InvitesWithFailures {
invites: pending_invites,
failures: failed_invites,
})
}
9 changes: 9 additions & 0 deletions src-tauri/src/commands/invites/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
mod accept_invite;
mod decline_invite;
mod get_invite;
mod get_invites;

pub use accept_invite::accept_invite;
pub use decline_invite::decline_invite;
pub use get_invite::get_invite;
pub use get_invites::get_invites;
111 changes: 0 additions & 111 deletions src-tauri/src/commands/key_packages.rs

This file was deleted.

52 changes: 52 additions & 0 deletions src-tauri/src/commands/key_packages/delete_all_key_packages.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::accounts::Account;
use crate::relays::RelayType;
use crate::Whitenoise;
use nostr_sdk::event::EventBuilder;

#[tauri::command]
pub async fn delete_all_key_packages(wn: tauri::State<'_, Whitenoise>) -> Result<(), String> {
let pubkey = wn
.nostr
.client
.signer()
.await
.map_err(|e| e.to_string())?
.get_public_key()
.await
.map_err(|e| e.to_string())?;

let active_account = Account::get_active(wn.clone())
.await
.map_err(|e| e.to_string())?;

let key_package_relays: Vec<String> = if cfg!(dev) {
vec!["ws://localhost:8080".to_string()]
} else {
active_account
.relays(RelayType::KeyPackage, wn.clone())
.await
.map_err(|e| e.to_string())?
};

let key_package_events = wn
.nostr
.query_user_key_packages(pubkey)
.await
.map_err(|e| e.to_string())?;

if !key_package_events.is_empty() {
let delete_event = EventBuilder::delete_with_reason(
key_package_events.iter().map(|e| e.id),
"Delete own key package",
);
tracing::debug!(target: "whitenoise::commands::key_packages::delete_all_key_packages", "Deleting key packages: {:?}", delete_event);
wn.nostr
.client
.send_event_builder_to(key_package_relays, delete_event)
.await
.map_err(|e| e.to_string())?;
} else {
tracing::debug!(target: "whitenoise::commands::key_packages::delete_all_key_packages", "No key packages to delete");
}
Ok(())
}
7 changes: 7 additions & 0 deletions src-tauri/src/commands/key_packages/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
mod delete_all_key_packages;
mod publish_new_key_package;
mod valid_key_package_exists_for_user;

pub use delete_all_key_packages::delete_all_key_packages;
pub use publish_new_key_package::publish_new_key_package;
pub use valid_key_package_exists_for_user::valid_key_package_exists_for_user;
Loading
Loading