feat: add si command line interface (#37)
Reviewed-on: #37
This commit was merged in pull request #37.
This commit is contained in:
@@ -8,7 +8,8 @@ publish.workspace = true
|
||||
signed_core = { path = "../signed_core" }
|
||||
signed_git = { path = "../signed_git" }
|
||||
signed_nostr = { path = "../signed_nostr" }
|
||||
settings = { path = "../settings" }
|
||||
settings.workspace = true
|
||||
paths = { path = "../paths" }
|
||||
utils = { path = "../utils" }
|
||||
|
||||
nostr.workspace = true
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::collections::HashSet;
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
@@ -11,8 +11,8 @@ use nostr::event::IntoEventBuilder;
|
||||
use nostr::nips::nip19::Nip19Coordinate;
|
||||
use nostr_connect::prelude::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
use signed_core::{Announcement, Filters, RepoAddr};
|
||||
use signed_git::{GitCache, Repo};
|
||||
use signed_core::{Announcement, Deletions, Filters, RepoAddr};
|
||||
use signed_git::{GitCache, Nip34Metadata, Repo};
|
||||
use signed_nostr::{SignedAuthUrlHandler, UniversalSigner, Update};
|
||||
|
||||
use crate::bootstrap::{
|
||||
@@ -229,7 +229,7 @@ impl Backend {
|
||||
let task: Task<Result<(), Error>> = cx.spawn(async move |this, cx| {
|
||||
let content = match user.await {
|
||||
Ok(Some((_username, secret))) => String::from_utf8(secret)?,
|
||||
_ => {
|
||||
Ok(None) => {
|
||||
this.update(cx, |this, cx| {
|
||||
if this.session == session {
|
||||
cx.emit(BackendEvent::SignerRequired);
|
||||
@@ -237,6 +237,17 @@ impl Backend {
|
||||
})?;
|
||||
return Ok(());
|
||||
}
|
||||
Err(error) => {
|
||||
this.update(cx, |this, cx| {
|
||||
if this.session == session {
|
||||
cx.emit(BackendEvent::error(format!(
|
||||
"failed to read the stored credential: {error}"
|
||||
)));
|
||||
cx.emit(BackendEvent::SignerRequired);
|
||||
}
|
||||
})?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
let result = async {
|
||||
@@ -798,8 +809,6 @@ impl Backend {
|
||||
.await;
|
||||
}
|
||||
|
||||
// The ngit-compatible `nostr.repo` marker makes the next scan
|
||||
// detect the repository instead of offering to publish it again.
|
||||
let coordinate = RepoAddr::new(event.pubkey, repo_id.clone());
|
||||
match Nip19Coordinate::new(coordinate.into(), servers.clone()).to_bech32() {
|
||||
Ok(naddr) => {
|
||||
@@ -814,7 +823,27 @@ impl Backend {
|
||||
Err(error) => log::warn!("failed to encode the repository coordinate: {error}"),
|
||||
}
|
||||
|
||||
Announcement::from_event(&event).ok_or_else(|| anyhow!("failed to parse announcement"))
|
||||
let announcement = Announcement::from_event(&event)
|
||||
.ok_or_else(|| anyhow!("failed to parse announcement"))?;
|
||||
|
||||
Self::write_nip34_json(&path, &announcement, cx).await;
|
||||
|
||||
Ok(announcement)
|
||||
})
|
||||
}
|
||||
|
||||
/// Records the announcement in `nak`'s `nip34.json`, which keeps itself out of the repository.
|
||||
fn write_nip34_json(path: &Path, announcement: &Announcement, cx: &mut AsyncApp) -> Task<()> {
|
||||
let metadata = Nip34Metadata::from_announcement(announcement);
|
||||
let path = path.to_path_buf();
|
||||
|
||||
cx.background_spawn(async move {
|
||||
let written =
|
||||
metadata.and_then(|metadata| Repo::open(&path)?.write_nip34_json(&metadata));
|
||||
|
||||
if let Err(error) = written {
|
||||
log::warn!("failed to write nip34.json: {error:#}");
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1078,6 +1107,45 @@ impl Backend {
|
||||
})
|
||||
}
|
||||
|
||||
/// The latest announcement of `addr` and its latest state's `HEAD`, syncing the bootstrap relays when the database has neither.
|
||||
pub async fn load_announcement(
|
||||
client: &Client,
|
||||
addr: &RepoAddr,
|
||||
) -> Result<Option<(Announcement, Option<String>)>, Error> {
|
||||
if let Some(found) = Self::stored_announcement(client, addr).await? {
|
||||
return Ok(Some(found));
|
||||
}
|
||||
|
||||
sync_bootstrap_only(client, addr.announcement_filter(), SyncOptions::default()).await?;
|
||||
Self::stored_announcement(client, addr).await
|
||||
}
|
||||
|
||||
async fn stored_announcement(
|
||||
client: &Client,
|
||||
addr: &RepoAddr,
|
||||
) -> Result<Option<(Announcement, Option<String>)>, Error> {
|
||||
let db = client.database();
|
||||
let deletions = Deletions::from_events(db.query(Filters::deletions()).await?);
|
||||
|
||||
let announcement = db
|
||||
.query(addr.announcement_filter())
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter(|event| !deletions.is_deleted(event))
|
||||
.max_by_key(|event| event.created_at)
|
||||
.and_then(|event| Announcement::from_event(&event));
|
||||
|
||||
let head = db
|
||||
.query(addr.state_filter())
|
||||
.await?
|
||||
.into_iter()
|
||||
.filter(|event| !deletions.is_deleted(event))
|
||||
.max_by_key(|event| event.created_at)
|
||||
.and_then(|event| SyncState::parse(&event).head);
|
||||
|
||||
Ok(announcement.map(|announcement| (announcement, head)))
|
||||
}
|
||||
|
||||
fn bootstrap_user(&mut self, public_key: PublicKey, cx: &mut Context<Self>) {
|
||||
let client = self.client.clone();
|
||||
|
||||
@@ -1155,6 +1223,7 @@ impl Backend {
|
||||
|
||||
pub fn sign_out(&mut self, cx: &mut Context<Self>) {
|
||||
self.supersede_session();
|
||||
|
||||
// Nothing may be signed with the identity the user just left.
|
||||
self.signer.swap_inner(Keys::generate());
|
||||
self.current_user = None;
|
||||
@@ -1177,8 +1246,10 @@ impl Backend {
|
||||
Ok::<(), Error>(())
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.emit(BackendEvent::SignerChanged);
|
||||
cx.emit(BackendEvent::SignerRequired);
|
||||
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
|
||||
@@ -12,8 +12,6 @@ mod repos;
|
||||
mod state;
|
||||
mod sync_status;
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub use backend::{Backend, BackendEvent};
|
||||
pub use bootstrap::{sync_bootstrap_only, user_grasp_list_servers};
|
||||
pub use checkouts::CheckoutsStore;
|
||||
@@ -26,17 +24,25 @@ pub use push::{GraspServer, PushOutcome};
|
||||
pub use refresh::{RefreshGate, RefreshRequest};
|
||||
pub use repo::RepoStore;
|
||||
pub use repos::RepoListStore;
|
||||
use settings::SettingsStore;
|
||||
pub use signed_git::{Nip34Binding, Nip34Kind};
|
||||
use signed_nostr::NostrBackend;
|
||||
pub use state::{GitPushMode, GitPushPolicy, GitRef, PushStep, PushStepStatus, SyncState};
|
||||
pub use sync_status::{CheckoutSyncStatus, SyncEvent, SyncStatusDirectory, SyncStatusStore};
|
||||
|
||||
pub fn init(
|
||||
db_path: impl AsRef<Path>,
|
||||
repos_root: impl Into<PathBuf>,
|
||||
scan_paths: Vec<PathBuf>,
|
||||
cx: &mut App,
|
||||
) -> anyhow::Result<()> {
|
||||
/// Loads the settings, creates the data directories and installs the stores every frontend shares.
|
||||
pub fn init(cx: &mut App) -> anyhow::Result<()> {
|
||||
SettingsStore::set_global(cx.new(|_| SettingsStore::new(paths::settings_file())), cx);
|
||||
|
||||
let settings = SettingsStore::global(cx);
|
||||
let scan_paths = settings.read(cx).settings().local_repos.scan_paths.clone();
|
||||
|
||||
for dir in [paths::nostr_dir(), paths::repos_dir()] {
|
||||
if let Err(error) = std::fs::create_dir_all(dir) {
|
||||
log::warn!("failed to create {}: {error}", dir.display());
|
||||
}
|
||||
}
|
||||
|
||||
// rustls uses the `aws_lc_rs` provider by default.
|
||||
if rustls::crypto::aws_lc_rs::default_provider()
|
||||
.install_default()
|
||||
@@ -45,7 +51,7 @@ pub fn init(
|
||||
log::debug!("a crypto provider is already installed");
|
||||
}
|
||||
|
||||
let db_path = db_path.as_ref().to_path_buf();
|
||||
let db_path = paths::nostr_dir().to_path_buf();
|
||||
|
||||
let backend = match cx
|
||||
.foreground_executor()
|
||||
@@ -73,9 +79,9 @@ pub fn init(
|
||||
if backend.is_ok() {
|
||||
cx.show_system_notification(gpui::SystemNotification {
|
||||
tag: "signed-nostr-db".into(),
|
||||
title: "Nostr cache unavailable".into(),
|
||||
title: "Nostr Storage unavailable".into(),
|
||||
body: format!(
|
||||
"{} could not be opened; this session uses a temporary cache",
|
||||
"{} could not be opened, this session uses a temporary storage",
|
||||
db_path.display()
|
||||
)
|
||||
.into(),
|
||||
@@ -89,7 +95,7 @@ pub fn init(
|
||||
|
||||
let (client, signer) = (backend.client, backend.signer);
|
||||
|
||||
Mirrors::install(repos_root);
|
||||
Mirrors::install(paths::repos_dir().to_path_buf());
|
||||
|
||||
Backend::set_global(cx.new(|cx| Backend::new(client, signer, cx)), cx);
|
||||
RepoListStore::set_global(cx.new(RepoListStore::new), cx);
|
||||
|
||||
Reference in New Issue
Block a user