From 2c7c29fef9940ca8f823236814a5044eb7fb40e2 Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Sun, 23 Jan 2022 09:45:45 +0000 Subject: [PATCH] Trigger subscription update from manager page --- public/js/subscription-manager.js | 8 +++++--- src/mango.cr | 2 +- src/plugin/plugin.cr | 5 +++++ src/plugin/updater.cr | 4 ++-- src/routes/api.cr | 17 +++++++++++++++++ 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/public/js/subscription-manager.js b/public/js/subscription-manager.js index 6d3f2af..7206c56 100644 --- a/public/js/subscription-manager.js +++ b/public/js/subscription-manager.js @@ -104,24 +104,26 @@ const component = () => { this.loading = true; const id = $(event.currentTarget).closest("tr").attr("sid"); fetch( - `${base_url}api/admin/plugin/subscriptions?${new URLSearchParams( + `${base_url}api/admin/plugin/subscriptions${type === 'update' ? '/update' : ''}?${new URLSearchParams( { plugin: this.pid, subscription: id, } )}`, { - method: "DELETE", + method: type === 'delete' ? "DELETE" : 'POST' } ) .then((response) => response.json()) .then((data) => { if (!data.success) throw new Error(data.error); + if (type === 'update') + alert("success", `Checking updates for subscription ${id}. Check the log for the progress or come back to this page later.`); }) .catch((e) => { alert( "danger", - `Failed to delete subscription. Error: ${e}` + `Failed to ${type} subscription. Error: ${e}` ); }) .finally(() => { diff --git a/src/mango.cr b/src/mango.cr index c3e86cd..b19799f 100644 --- a/src/mango.cr +++ b/src/mango.cr @@ -61,7 +61,7 @@ class CLI < Clim Library.load_instance Library.default Plugin::Downloader.default - Plugin::Updater.new + Plugin::Updater.default spawn do begin diff --git a/src/plugin/plugin.cr b/src/plugin/plugin.cr index 7f44ccc..76f1f1f 100644 --- a/src/plugin/plugin.cr +++ b/src/plugin/plugin.cr @@ -153,6 +153,11 @@ class Plugin list.save end + def check_subscription(id : String) + sub = list_subscriptions_raw.find &.id.== id + Plugin::Updater.default.check_subscription self, sub.not_nil! + end + def initialize(id : String) Plugin.build_info_ary diff --git a/src/plugin/updater.cr b/src/plugin/updater.cr index e490940..d4fc743 100644 --- a/src/plugin/updater.cr +++ b/src/plugin/updater.cr @@ -1,7 +1,7 @@ -require "../config" - class Plugin class Updater + use_default + def initialize interval = Config.current.plugin_update_interval_hours return if interval <= 0 diff --git a/src/routes/api.cr b/src/routes/api.cr index 47e5558..ffed75f 100644 --- a/src/routes/api.cr +++ b/src/routes/api.cr @@ -721,6 +721,23 @@ struct APIRouter end end + post "/api/admin/plugin/subscriptions/update" do |env| + pid = env.params.query["plugin"].as String + sid = env.params.query["subscription"].as String + + Plugin.new(pid).check_subscription sid + + send_json env, { + "success" => true, + }.to_json + rescue e + Logger.error e + send_json env, { + "success" => false, + "error" => e.message, + }.to_json + end + Koa.describe "Lists the chapters in a title from a plugin" Koa.tags ["admin", "downloader"] Koa.query "plugin", schema: String