wabbajack/Wabbajack.BuildServer/Controllers/ListValidation.cs

128 lines
4.0 KiB
C#
Raw Normal View History

2020-01-13 04:31:43 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
2020-01-08 04:41:50 +00:00
using System.Threading.Tasks;
2020-01-13 04:31:43 +00:00
using Microsoft.AspNetCore.Http.Extensions;
2020-01-08 04:41:50 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
2020-01-13 04:31:43 +00:00
using Nettle;
using Wabbajack.BuildServer.Model.Models;
2020-01-08 04:41:50 +00:00
using Wabbajack.BuildServer.Models;
using Wabbajack.Common;
2020-01-08 04:41:50 +00:00
using Wabbajack.Lib.ModListRegistry;
namespace Wabbajack.BuildServer.Controllers
{
[ApiController]
[Route("/lists")]
public class ListValidation : AControllerBase<ListValidation>
{
public ListValidation(ILogger<ListValidation> logger, DBContext db, SqlService sql) : base(logger, db, sql)
2020-01-08 04:41:50 +00:00
{
}
2020-01-13 04:31:43 +00:00
2020-01-08 04:41:50 +00:00
[HttpGet]
2020-01-09 13:36:27 +00:00
[Route("status.json")]
2020-04-03 03:57:59 +00:00
public async Task<IEnumerable<ModlistSummary>> HandleGetLists()
2020-01-08 04:41:50 +00:00
{
2020-04-03 03:57:59 +00:00
return await SQL.GetModListSummaries();
2020-01-08 04:41:50 +00:00
}
2020-01-13 04:31:43 +00:00
private static readonly Func<object, string> HandleGetRssFeedTemplate = NettleEngine.GetCompiler().Compile(@"
<?xml version=""1.0""?>
<rss version=""2.0"">
<channel>
<title>{{lst.Name}} - Broken Mods</title>
<link>http://build.wabbajack.org/status/{{lst.Name}}.html</link>
<description>These are mods that are broken and need updating</description>
{{ each $.failed }}
<item>
<title>{{$.Archive.Name}} {{$.Archive.Hash}} {{$.Archive.State.PrimaryKeyString}}</title>
2020-01-13 04:31:43 +00:00
<link>{{$.Archive.Name}}</link>
</item>
{{/each}}
</channel>
</rss>
");
[HttpGet]
[Route("status/{Name}/broken.rss")]
public async Task<ContentResult> HandleGetRSSFeed(string Name)
{
2020-04-03 03:57:59 +00:00
var lst = await SQL.GetDetailedModlistStatus(Name);
2020-01-13 04:31:43 +00:00
var response = HandleGetRssFeedTemplate(new
{
lst,
failed = lst.Archives.Where(a => a.IsFailing).ToList(),
passed = lst.Archives.Where(a => !a.IsFailing).ToList()
});
return new ContentResult
{
ContentType = "application/rss+xml",
StatusCode = (int) HttpStatusCode.OK,
Content = response
};
}
private static readonly Func<object, string> HandleGetListTemplate = NettleEngine.GetCompiler().Compile(@"
<html><body>
<h2>{{lst.Name}} - {{lst.Checked}} - {{ago}}min ago</h2>
<h3>Failed ({{failed.Count}}):</h3>
<ul>
{{each $.failed }}
<li>{{$.Archive.Name}}</li>
{{/each}}
</ul>
<h3>Passed ({{passed.Count}}):</h3>
<ul>
{{each $.passed }}
<li>{{$.Archive.Name}}</li>
{{/each}}
</ul>
</body></html>
");
[HttpGet]
[Route("status/{Name}.html")]
public async Task<ContentResult> HandleGetListHtml(string Name)
{
2020-04-03 03:57:59 +00:00
var lst = await SQL.GetDetailedModlistStatus(Name);
2020-01-13 04:31:43 +00:00
var response = HandleGetListTemplate(new
{
lst,
ago = (DateTime.UtcNow - lst.Checked).TotalMinutes,
2020-01-13 04:31:43 +00:00
failed = lst.Archives.Where(a => a.IsFailing).ToList(),
passed = lst.Archives.Where(a => !a.IsFailing).ToList()
});
return new ContentResult
{
ContentType = "text/html",
StatusCode = (int) HttpStatusCode.OK,
Content = response
};
}
[HttpGet]
[Route("status/{Name}.json")]
public async Task<ContentResult> HandleGetListJson(string Name)
{
2020-04-03 03:57:59 +00:00
var lst = await SQL.GetDetailedModlistStatus(Name);
lst.Archives.Do(a => a.Archive.Meta = null);
return new ContentResult
{
ContentType = "application/json",
StatusCode = (int) HttpStatusCode.OK,
Content = lst.ToJSON()
};
}
2020-01-13 04:31:43 +00:00
2020-01-08 04:41:50 +00:00
}
}