Flow.Launcher/Flow.Launcher.Core/ExternalPlugins/PluginsManifest.cs

60 lines
2.3 KiB
C#
Raw Normal View History

2025-04-04 07:56:18 +00:00
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
2025-04-04 07:56:18 +00:00
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Plugin;
namespace Flow.Launcher.Core.ExternalPlugins
{
public static class PluginsManifest
{
2025-04-13 09:59:39 +00:00
private static readonly string ClassName = nameof(PluginsManifest);
private static readonly CommunityPluginStore mainPluginStore =
new("https://raw.githubusercontent.com/Flow-Launcher/Flow.Launcher.PluginsManifest/plugin_api_v2/plugins.json",
2023-07-04 21:01:13 +00:00
"https://fastly.jsdelivr.net/gh/Flow-Launcher/Flow.Launcher.PluginsManifest@plugin_api_v2/plugins.json",
"https://gcore.jsdelivr.net/gh/Flow-Launcher/Flow.Launcher.PluginsManifest@plugin_api_v2/plugins.json",
"https://cdn.jsdelivr.net/gh/Flow-Launcher/Flow.Launcher.PluginsManifest@plugin_api_v2/plugins.json");
private static readonly SemaphoreSlim manifestUpdateLock = new(1);
private static DateTime lastFetchedAt = DateTime.MinValue;
2025-04-04 07:56:18 +00:00
private static readonly TimeSpan fetchTimeout = TimeSpan.FromMinutes(2);
public static List<UserPlugin> UserPlugins { get; private set; }
2025-04-04 07:56:18 +00:00
public static async Task<bool> UpdateManifestAsync(bool usePrimaryUrlOnly = false, CancellationToken token = default)
{
try
{
await manifestUpdateLock.WaitAsync(token).ConfigureAwait(false);
2025-02-10 03:54:09 +00:00
if (UserPlugins == null || usePrimaryUrlOnly || DateTime.Now.Subtract(lastFetchedAt) >= fetchTimeout)
{
var results = await mainPluginStore.FetchAsync(token, usePrimaryUrlOnly).ConfigureAwait(false);
2025-02-10 03:54:09 +00:00
// If the results are empty, we shouldn't update the manifest because the results are invalid.
if (results.Count != 0)
{
UserPlugins = results;
lastFetchedAt = DateTime.Now;
2025-02-10 04:01:21 +00:00
return true;
2025-02-10 03:54:09 +00:00
}
}
}
catch (Exception e)
{
2025-04-13 09:59:39 +00:00
Ioc.Default.GetRequiredService<IPublicAPI>().LogException(ClassName, "Http request failed", e);
}
finally
{
manifestUpdateLock.Release();
}
2025-02-10 04:01:21 +00:00
return false;
}
}
}