feat: add repo commands to the si cli
This commit is contained in:
@@ -11,7 +11,7 @@ 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_core::{Announcement, Deletions, Filters, RepoAddr};
|
||||
use signed_git::{GitCache, Repo};
|
||||
use signed_nostr::{SignedAuthUrlHandler, UniversalSigner, Update};
|
||||
|
||||
@@ -41,8 +41,6 @@ pub enum BackendEvent {
|
||||
status: PushStepStatus,
|
||||
},
|
||||
Error(String),
|
||||
/// The relay list, profile and grasp list of a new identity were published; `Err` when no relay took them.
|
||||
IdentityPublished(Result<(), String>),
|
||||
}
|
||||
|
||||
impl BackendEvent {
|
||||
@@ -231,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);
|
||||
@@ -239,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 {
|
||||
@@ -552,19 +561,13 @@ impl Backend {
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
cx.spawn(async move |this, cx| {
|
||||
let published = futures::future::try_join_all(publishes)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(|error| {
|
||||
format!("identity created, but publishing it failed: {error}")
|
||||
});
|
||||
|
||||
this.update(cx, |_this, cx| {
|
||||
if let Err(message) = &published {
|
||||
cx.emit(BackendEvent::error(message.clone()));
|
||||
}
|
||||
cx.emit(BackendEvent::IdentityPublished(published));
|
||||
})?;
|
||||
if let Err(error) = futures::future::try_join_all(publishes).await {
|
||||
this.update(cx, |_this, cx| {
|
||||
cx.emit(BackendEvent::error(format!(
|
||||
"identity created, but publishing it failed: {error}"
|
||||
)));
|
||||
})?;
|
||||
}
|
||||
|
||||
Ok::<(), anyhow::Error>(())
|
||||
})
|
||||
@@ -1086,6 +1089,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();
|
||||
|
||||
@@ -1161,8 +1203,9 @@ impl Backend {
|
||||
self.passphrase_required
|
||||
}
|
||||
|
||||
pub fn sign_out(&mut self, cx: &mut Context<Self>) -> Task<Result<(), Error>> {
|
||||
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;
|
||||
@@ -1173,11 +1216,6 @@ impl Backend {
|
||||
});
|
||||
|
||||
let delete = cx.delete_credentials(USER_KEYRING);
|
||||
|
||||
cx.emit(BackendEvent::SignerChanged);
|
||||
cx.emit(BackendEvent::SignerRequired);
|
||||
cx.notify();
|
||||
|
||||
cx.spawn(async move |this, cx| {
|
||||
if let Err(error) = delete.await {
|
||||
this.update(cx, |_this, cx| {
|
||||
@@ -1189,6 +1227,12 @@ impl Backend {
|
||||
|
||||
Ok::<(), Error>(())
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.emit(BackendEvent::SignerChanged);
|
||||
cx.emit(BackendEvent::SignerRequired);
|
||||
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn sync_inbox(&mut self, cx: &mut Context<Self>) {
|
||||
|
||||
Reference in New Issue
Block a user