chore: refactor auto updater

This commit is contained in:
2025-08-02 17:28:27 +07:00
parent 86d24ccbd5
commit c8c5a6668d
9 changed files with 320 additions and 380 deletions

View File

@@ -1,14 +1,16 @@
use std::sync::Arc;
use auto_update::AutoUpdater;
use client_keys::ClientKeys;
use common::display::DisplayProfile;
use global::constants::DEFAULT_SIDEBAR_WIDTH;
use gpui::prelude::FluentBuilder;
use gpui::{
actions, div, px, rems, Action, App, AppContext, Axis, Context, Entity, InteractiveElement,
IntoElement, ParentElement, Render, SharedString, Styled, Subscription, Window,
IntoElement, ParentElement, Render, SharedString, StatefulInteractiveElement, Styled,
Subscription, Window,
};
use i18n::t;
use i18n::{shared_t, t};
use identity::Identity;
use nostr_connect::prelude::*;
use nostr_sdk::prelude::*;
@@ -275,7 +277,7 @@ impl ChatSpace {
});
}
pub fn on_settings(&mut self, _ev: &Settings, window: &mut Window, cx: &mut Context<Self>) {
fn on_settings(&mut self, _ev: &Settings, window: &mut Window, cx: &mut Context<Self>) {
let view = preferences::init(window, cx);
let title = SharedString::new(t!("common.preferences"));
@@ -341,8 +343,39 @@ impl ChatSpace {
let need_backup = Identity::read_global(cx).need_backup();
let relay_ready = Identity::read_global(cx).relay_ready();
let updating = AutoUpdater::read_global(cx).status.is_updating();
let updated = AutoUpdater::read_global(cx).status.is_updated();
h_flex()
.gap_1()
.when(updating, |this| {
this.child(
h_flex()
.h_6()
.items_center()
.justify_center()
.text_xs()
.bg(cx.theme().ghost_element_background_alt)
.child(shared_t!("auto_update.updating")),
)
})
.when(updated, |this| {
this.child(
h_flex()
.id("updated")
.h_6()
.items_center()
.justify_center()
.text_xs()
.bg(cx.theme().ghost_element_background_alt)
.hover(|this| this.bg(cx.theme().ghost_element_hover))
.active(|this| this.bg(cx.theme().ghost_element_active))
.child(shared_t!("auto_update.updated"))
.on_click(|_, _window, cx| {
cx.restart(None);
}),
)
})
.when_some(relay_ready, |this, status| {
this.when(!status, |this| this.child(messaging_relays::relay_button()))
})

View File

@@ -4,9 +4,8 @@ use std::time::Duration;
use anyhow::{anyhow, Error};
use assets::Assets;
use auto_update::AutoUpdater;
use global::constants::{
ALL_MESSAGES_SUB_ID, APP_ID, APP_NAME, APP_PUBKEY, BOOTSTRAP_RELAYS, METADATA_BATCH_LIMIT,
ALL_MESSAGES_SUB_ID, APP_ID, APP_NAME, BOOTSTRAP_RELAYS, METADATA_BATCH_LIMIT,
METADATA_BATCH_TIMEOUT, NEW_MESSAGE_SUB_ID, SEARCH_RELAYS,
};
use global::{nostr_client, NostrSignal};
@@ -55,11 +54,6 @@ fn main() {
log::error!("Failed to connect to bootstrap relays: {e}");
}
// Connect to bootstrap relays.
if let Err(e) = subscribe_for_app_updates(client).await {
log::error!("Failed to subscribe for app updates: {e}");
}
// Handle Nostr notifications.
//
// Send the redefined signal back to GPUI via channel.
@@ -250,7 +244,6 @@ fn main() {
while let Ok(signal) = signal_rx.recv().await {
cx.update(|window, cx| {
let registry = Registry::global(cx);
let auto_updater = AutoUpdater::global(cx);
let identity = Identity::read_global(cx);
match signal {
@@ -293,11 +286,6 @@ fn main() {
NostrSignal::Notice(_msg) => {
// window.push_notification(msg, cx);
}
NostrSignal::AppUpdate(event) => {
auto_updater.update(cx, |this, cx| {
this.update(event, cx);
});
}
};
})
.ok();
@@ -431,20 +419,6 @@ async fn handle_nostr_notifications(
)
}
}
Kind::ReleaseArtifactSet => {
let ids = event.tags.event_ids().copied();
let filter = Filter::new().ids(ids).kind(Kind::FileMetadata);
client
.subscribe_to(BOOTSTRAP_RELAYS, filter, Some(opts))
.await
.ok();
signal_tx
.send(NostrSignal::AppUpdate(event.into_owned()))
.await
.ok();
}
_ => {}
}
}
@@ -460,27 +434,6 @@ async fn handle_nostr_notifications(
Ok(())
}
async fn subscribe_for_app_updates(client: &Client) -> Result<(), Error> {
let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE);
let coordinate = Coordinate {
kind: Kind::Custom(32267),
public_key: PublicKey::from_hex(APP_PUBKEY).expect("App Pubkey is invalid"),
identifier: APP_ID.into(),
};
let filter = Filter::new()
.kind(Kind::ReleaseArtifactSet)
.coordinate(&coordinate)
.limit(1);
client
.subscribe_to(BOOTSTRAP_RELAYS, filter, Some(opts))
.await?;
Ok(())
}
async fn check_author(client: &Client, event: &Event) -> Result<bool, Error> {
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;