Skip to content

Commit

Permalink
Merge pull request #64 from TicClick/update-url
Browse files Browse the repository at this point in the history
Allow to change the update URL
  • Loading branch information
TicClick authored Jan 13, 2024
2 parents 4affa0d + 1d96abb commit e6500e4
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "steel"
version = "0.7.2"
version = "0.7.3"
edition = "2021"
build = "src/build.rs"

Expand Down Expand Up @@ -41,6 +41,7 @@ png = "0.17.10"

libloading = "0.8.1"
winresource = "0.1.17"
serde_json = "1.0.111"

[features]
default = []
Expand Down
2 changes: 2 additions & 0 deletions crates/steel_core/src/settings/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ pub struct Application {
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct AutoUpdate {
pub enabled: bool,
#[serde(default)]
pub url: String,
}
7 changes: 6 additions & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use steel_core::chat::irc::IRCError;
use steel_core::chat::{ChatLike, ChatState, ConnectionStatus, Message};

use crate::core::irc::IRCActorHandle;
use crate::core::settings;
use crate::core::{settings, updater};
use steel_core::ipc::{server::AppMessageIn, ui::UIMessageIn};

const EVENT_QUEUE_SIZE: usize = 1000;
Expand Down Expand Up @@ -120,6 +120,11 @@ impl Application {

pub fn load_settings(&mut self, source: settings::Source, fallback: bool) {
self.state.settings = settings::Settings::from_file(&source, fallback);

if self.state.settings.application.autoupdate.url.is_empty() {
self.state.settings.application.autoupdate.url = updater::default_update_url();
}

self.handle_chat_moderator_added("BanchoBot".into());
self.ui_handle_settings_requested();
}
Expand Down
60 changes: 56 additions & 4 deletions src/core/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@ use serde::{Deserialize, Serialize};
use steel_core::VersionString;
use tokio::sync::mpsc::{channel, Receiver, Sender};

pub const RECENT_RELEASES_METADATA_URL: &str =
"https://api.github.com/repos/TicClick/steel/releases";
const GIST_METADATA_FILENAME: &str = "releases.json";

pub fn default_update_url() -> String {
#[cfg(feature = "glass")]
{
glass::ROOT_RELEASES_URL.to_owned()
}
#[cfg(not(feature = "glass"))]
{
RECENT_RELEASES_METADATA_URL.to_owned()
}
}

#[derive(Debug, Default, Clone)]
pub struct UpdateState {
pub when: Option<chrono::DateTime<chrono::Local>>,
pub state: State,
pub url_test_result: Option<Result<(), String>>,
}

#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -52,14 +68,12 @@ pub enum UpdateSource {
pub enum BackendRequest {
InitiateAutoupdate,
SetAutoupdateStatus(bool),
ChangeURL(String),
FetchMetadata,
FetchRelease,
Quit,
}

const RECENT_RELEASES_METADATA_URL: &str = "https://api.github.com/repos/TicClick/steel/releases";
const GIST_METADATA_FILENAME: &str = "releases.json";

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ReleaseMetadataGitHub {
pub tag_name: String,
Expand Down Expand Up @@ -140,6 +154,22 @@ impl ReleaseAsset {
}
}

pub fn test_update_url(url: &str) -> Result<UpdateSource, String> {
match ureq::request("GET", url).call() {
Err(e) => Err(e.to_string()),
Ok(payload) => {
let v = payload.into_string().unwrap();
match serde_json::from_str::<Vec<ReleaseMetadataGitHub>>(&v) {
Ok(_) => Ok(UpdateSource::GitHub(url.to_owned())),
Err(_) => match serde_json::from_str::<ReleaseMetadataGist>(&v) {
Ok(_) => Ok(UpdateSource::Gist(url.to_owned())),
Err(e) => Err(e.to_string()),
},
}
}
}
}

#[derive(Debug)]
pub struct Updater {
state: Arc<Mutex<UpdateState>>,
Expand Down Expand Up @@ -187,6 +217,12 @@ impl Updater {
}
}

pub fn change_url(&self, url: &str) {
self.channel
.blocking_send(BackendRequest::ChangeURL(url.to_owned()))
.unwrap();
}

pub fn check_version(&self) {
self.channel
.blocking_send(BackendRequest::FetchMetadata)
Expand Down Expand Up @@ -259,6 +295,20 @@ impl UpdaterBackend {
BackendRequest::SetAutoupdateStatus(enabled) => {
self.autoupdate = enabled;
}
BackendRequest::ChangeURL(url) => self.change_url(url),
}
}
}

fn change_url(&mut self, url: String) {
match test_update_url(&url) {
Ok(src) => {
log::debug!("updater: change url {:?} -> {:?}", self.src, src);
self.src = src;
self.state.lock().unwrap().url_test_result = Some(Ok(()));
}
Err(e) => {
self.state.lock().unwrap().url_test_result = Some(Err(e));
}
}
}
Expand Down Expand Up @@ -310,9 +360,11 @@ impl UpdaterBackend {
if let State::UpdateError(ref text) = state {
log::error!("failed to perform the update: {}", text);
}
*self.state.lock().unwrap() = UpdateState {
let mut guard = self.state.lock().unwrap();
*guard = UpdateState {
state,
when: Some(chrono::Local::now()),
url_test_result: guard.url_test_result.clone(),
};
}

Expand Down
31 changes: 30 additions & 1 deletion src/gui/settings/application.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use eframe::egui;

use super::SettingsWindow;
use crate::gui::state::UIState;
use crate::{core::updater, gui::state::UIState};

impl SettingsWindow {
pub(super) fn show_application_tab(
Expand All @@ -23,6 +23,35 @@ impl SettingsWindow {
"enable automatic updates",
)
.on_hover_text_at_pointer(hint);

ui.label("update URL");
let url = egui::TextEdit::multiline(&mut state.settings.application.autoupdate.url)
.hint_text("should point to release metadata");
ui.add(url);
ui.horizontal(|ui| {
if ui.button("test").clicked() {
state
.updater
.change_url(&state.settings.application.autoupdate.url);
}
if ui.button("revert").clicked() {
state.settings.application.autoupdate.url = updater::default_update_url();
state
.updater
.change_url(&state.settings.application.autoupdate.url);
}
});

if let Some(test_result) = state.updater.state().url_test_result {
match test_result {
Ok(_) => {
ui.label("test result: OK");
}
Err(why) => {
ui.label(format!("test result: FAIL ({})", why));
}
}
}
});

if autoupdate != state.settings.application.autoupdate.enabled {
Expand Down
9 changes: 2 additions & 7 deletions src/gui/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use eframe::egui;
use tokio::sync::mpsc::Sender;

use crate::core::settings::Settings;
use crate::core::updater::{UpdateSource, Updater};
use crate::core::updater::Updater;

use crate::gui::highlights;

Expand Down Expand Up @@ -55,13 +55,7 @@ impl UIState {
active_chat_tab_name: String::new(),
core: CoreClient::new(app_queue_handle),
highlights: highlights::HighlightTracker::new(),

#[cfg(feature = "glass")]
updater: Updater::new(UpdateSource::Gist(glass::ROOT_RELEASES_URL.to_owned())),

#[cfg(not(feature = "glass"))]
updater: Updater::default(),

sound_player: crate::core::sound::SoundPlayer::new(),

#[cfg(feature = "glass")]
Expand All @@ -77,6 +71,7 @@ impl UIState {
.set_username(&self.settings.chat.irc.username);
self.highlights
.set_highlights(&self.settings.notifications.highlights.words);

self.updater
.enable_autoupdate(self.settings.application.autoupdate.enabled);
}
Expand Down
1 change: 1 addition & 0 deletions src/gui/update_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl UpdateWindow {
let UpdateState {
state: last_action,
when,
..
} = state.updater.state();
match last_action {
State::Idle => {
Expand Down

0 comments on commit e6500e4

Please sign in to comment.