2020-07-25 18:09:02 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2020-08-05 20:53:19 +00:00
|
|
|
|
using System.Threading;
|
2020-07-25 18:09:02 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Wabbajack.BuildServer;
|
|
|
|
|
using Wabbajack.Common;
|
|
|
|
|
using Wabbajack.Lib.Downloaders;
|
|
|
|
|
using Wabbajack.Lib.NexusApi;
|
|
|
|
|
using Wabbajack.Server.DataLayer;
|
|
|
|
|
using Wabbajack.Server.DTOs;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Server.Services
|
|
|
|
|
{
|
2020-08-17 04:15:19 +00:00
|
|
|
|
public class NexusPermissionsUpdater : AbstractService<NexusPermissionsUpdater, int>
|
2020-07-25 18:09:02 +00:00
|
|
|
|
{
|
|
|
|
|
private DiscordWebHook _discord;
|
|
|
|
|
private SqlService _sql;
|
2020-08-05 20:53:19 +00:00
|
|
|
|
|
2020-08-17 04:15:19 +00:00
|
|
|
|
public NexusPermissionsUpdater(ILogger<NexusPermissionsUpdater> logger, AppSettings settings, QuickSync quickSync, DiscordWebHook discord, SqlService sql) : base(logger, settings, quickSync, TimeSpan.FromMinutes(5))
|
2020-07-25 18:09:02 +00:00
|
|
|
|
{
|
|
|
|
|
_discord = discord;
|
|
|
|
|
_sql = sql;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task<int> Execute()
|
|
|
|
|
{
|
2020-08-05 00:34:09 +00:00
|
|
|
|
await _sql.UpdateGameMetadata();
|
|
|
|
|
|
2020-08-05 20:53:19 +00:00
|
|
|
|
|
2020-07-25 18:09:02 +00:00
|
|
|
|
var data = await _sql.ModListArchives();
|
|
|
|
|
var nexusArchives = data.Select(a => a.State).OfType<NexusDownloader.State>().Select(d => (d.Game, d.ModID))
|
2020-08-12 04:25:12 +00:00
|
|
|
|
.Where(g => g.Game.MetaData().NexusGameId != 0)
|
2020-07-25 18:09:02 +00:00
|
|
|
|
.Distinct()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation($"Starting nexus permissions updates for {nexusArchives.Count} mods");
|
|
|
|
|
|
2020-08-12 04:25:12 +00:00
|
|
|
|
using var queue = new WorkQueue();
|
2020-07-25 18:09:02 +00:00
|
|
|
|
|
2020-08-12 04:25:12 +00:00
|
|
|
|
var prev = await _sql.GetHiddenNexusMods();
|
|
|
|
|
_logger.LogInformation($"Found {prev.Count} hidden nexus mods to check");
|
2020-08-05 20:53:19 +00:00
|
|
|
|
|
2020-08-12 04:25:12 +00:00
|
|
|
|
await prev.PMap(queue, async archive =>
|
2020-07-25 18:09:02 +00:00
|
|
|
|
{
|
2020-08-12 04:25:12 +00:00
|
|
|
|
var (game, modID) = archive.Key;
|
|
|
|
|
_logger.LogInformation($"Checking permissions for {game} {modID}");
|
|
|
|
|
var result = await HTMLInterface.GetUploadPermissions(game, modID);
|
|
|
|
|
await _sql.SetNexusPermission(game, modID, result);
|
2020-08-05 00:34:09 +00:00
|
|
|
|
|
2020-08-12 04:25:12 +00:00
|
|
|
|
if (archive.Value != result)
|
2020-07-25 18:09:02 +00:00
|
|
|
|
{
|
2020-08-12 04:25:12 +00:00
|
|
|
|
await _discord.Send(Channel.Ham,
|
|
|
|
|
new DiscordMessage {
|
|
|
|
|
Content = $"Permissions status of {game} {modID} was {archive.Value} is now {result}"
|
|
|
|
|
});
|
|
|
|
|
await _sql.PurgeNexusCache(modID);
|
|
|
|
|
await _quickSync.Notify<ListValidator>();
|
2020-07-25 18:09:02 +00:00
|
|
|
|
}
|
2020-08-05 00:34:09 +00:00
|
|
|
|
});
|
2020-07-25 18:09:02 +00:00
|
|
|
|
|
2020-08-05 00:34:09 +00:00
|
|
|
|
return 1;
|
2020-07-25 18:09:02 +00:00
|
|
|
|
}
|
2020-08-05 00:34:09 +00:00
|
|
|
|
|
2020-07-25 18:09:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|