Merge branch 'main' into feature/blog-admin

This commit is contained in:
Oliver Booth 2024-02-24 03:42:08 +00:00
commit bd55ac28e3
Signed by: oliverbooth
GPG Key ID: E60B570D1B7557B5
16 changed files with 235 additions and 42 deletions

View File

@ -1,5 +1,4 @@
using System.Text; using System.Text;
using System.Text.Json;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using OliverBooth.Data.Web; using OliverBooth.Data.Web;
using OliverBooth.Services; using OliverBooth.Services;

View File

@ -0,0 +1,10 @@
namespace OliverBooth.Data.Mastodon;
public enum AttachmentType
{
Unknown,
Image,
GifV,
Video,
Audio
}

View File

@ -0,0 +1,34 @@
using System.Text.Json.Serialization;
namespace OliverBooth.Data.Mastodon;
public sealed class MastodonStatus
{
/// <summary>
/// Gets the content of the status.
/// </summary>
/// <value>The content.</value>
[JsonPropertyName("content")]
public string Content { get; set; } = string.Empty;
/// <summary>
/// Gets the date and time at which this status was posted.
/// </summary>
/// <value>The post timestamp.</value>
[JsonPropertyName("created_at")]
public DateTimeOffset CreatedAt { get; set; }
/// <summary>
/// Gets the media attachments for this status.
/// </summary>
/// <value>The media attachments.</value>
[JsonPropertyName("media_attachments")]
public IReadOnlyList<MediaAttachment> MediaAttachments { get; set; } = ArraySegment<MediaAttachment>.Empty;
/// <summary>
/// Gets the original URI of the status.
/// </summary>
/// <value>The original URI.</value>
[JsonPropertyName("url")]
public Uri OriginalUri { get; set; } = null!;
}

View File

@ -0,0 +1,22 @@
namespace OliverBooth.Data.Mastodon;
public sealed class MediaAttachment
{
/// <summary>
/// Gets the preview URL of the attachment.
/// </summary>
/// <value>The preview URL.</value>
public Uri PreviewUrl { get; set; } = null!;
/// <summary>
/// Gets the type of this attachment.
/// </summary>
/// <value>The attachment type.</value>
public AttachmentType Type { get; set; } = AttachmentType.Unknown;
/// <summary>
/// Gets the URL of the attachment.
/// </summary>
/// <value>The URL.</value>
public Uri Url { get; set; } = null!;
}

View File

@ -2,7 +2,6 @@ using NetBarcode;
using SixLabors.ImageSharp; using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing;
using Type = System.Type;
namespace OliverBooth.Data.Web; namespace OliverBooth.Data.Web;

View File

@ -1,5 +1,3 @@
using SixLabors.ImageSharp;
namespace OliverBooth.Data.Web; namespace OliverBooth.Data.Web;
/// <summary> /// <summary>

View File

@ -11,6 +11,7 @@
<PackageReference Include="Alexinea.Extensions.Configuration.Toml" Version="7.0.0"/> <PackageReference Include="Alexinea.Extensions.Configuration.Toml" Version="7.0.0"/>
<PackageReference Include="AspNetCore.ReCaptcha" Version="1.7.0"/> <PackageReference Include="AspNetCore.ReCaptcha" Version="1.7.0"/>
<PackageReference Include="BCrypt.Net-Core" Version="1.6.0"/> <PackageReference Include="BCrypt.Net-Core" Version="1.6.0"/>
<PackageReference Include="HtmlAgilityPack" Version="1.11.59"/>
<PackageReference Include="Humanizer.Core" Version="2.14.1"/> <PackageReference Include="Humanizer.Core" Version="2.14.1"/>
<PackageReference Include="MailKit" Version="4.3.0"/> <PackageReference Include="MailKit" Version="4.3.0"/>
<PackageReference Include="MailKitSimplified.Sender" Version="2.8.0"/> <PackageReference Include="MailKitSimplified.Sender" Version="2.8.0"/>

View File

@ -1,10 +1,44 @@
@page @page
@using Humanizer
@using OliverBooth.Data.Mastodon
@using OliverBooth.Services
@model Index @model Index
@inject IMastodonService MastodonService
@{ @{
ViewData["Title"] = "Blog"; ViewData["Title"] = "Blog";
MastodonStatus latestStatus = MastodonService.GetLatestStatus();
} }
<div class="card text-center mastodon-update-card">
<div class="card-body">
@Html.Raw(latestStatus.Content)
@foreach (MediaAttachment attachment in latestStatus.MediaAttachments)
{
switch (attachment.Type)
{
case AttachmentType.Audio:
<p><audio controls="controls" src="@attachment.Url"></audio></p>
break;
case AttachmentType.Video:
<p><video controls="controls" class="figure-img img-fluid" src="@attachment.Url"></video></p>
break;
case AttachmentType.Image:
case AttachmentType.GifV:
<p><img class="figure-img img-fluid" src="@attachment.Url"></p>
break;
}
}
</div>
<div class="card-footer text-muted">
<abbr title="@latestStatus.CreatedAt.ToString("F")">@latestStatus.CreatedAt.Humanize()</abbr>
&bull;
<a href="@latestStatus.OriginalUri" target="_blank">View on Mastodon</a>
</div>
</div>
<div id="all-blog-posts"> <div id="all-blog-posts">
@await Html.PartialAsync("_LoadingSpinner") @await Html.PartialAsync("_LoadingSpinner")
</div> </div>

View File

@ -94,9 +94,17 @@
<div style="margin:50px 0;"></div> <div style="margin:50px 0;"></div>
@if (DateTimeOffset.UtcNow < new DateTime(2024, 03, 08))
{
<div id="usa-countdown" class="container"> <div id="usa-countdown" class="container">
<p>00 : 00 : 00 : 00</p> <div class="row">
<div class="col-3 usa-countdown-element" id="usa-countdown-days">00</div>
<div class="col-3 usa-countdown-element" id="usa-countdown-hours">00</div>
<div class="col-3 usa-countdown-element" id="usa-countdown-minutes">00</div>
<div class="col-3 usa-countdown-element" id="usa-countdown-seconds">00</div>
</div> </div>
</div>
}
<div style="margin:50px 0;"></div> <div style="margin:50px 0;"></div>

View File

@ -32,12 +32,14 @@ builder.Services.AddSingleton(provider => new MarkdownPipelineBuilder()
builder.Services.AddDbContextFactory<BlogContext>(); builder.Services.AddDbContextFactory<BlogContext>();
builder.Services.AddDbContextFactory<WebContext>(); builder.Services.AddDbContextFactory<WebContext>();
builder.Services.AddHttpClient();
builder.Services.AddSingleton<IContactService, ContactService>(); builder.Services.AddSingleton<IContactService, ContactService>();
builder.Services.AddSingleton<ITemplateService, TemplateService>(); builder.Services.AddSingleton<ITemplateService, TemplateService>();
builder.Services.AddSingleton<IBlogPostService, BlogPostService>(); builder.Services.AddSingleton<IBlogPostService, BlogPostService>();
builder.Services.AddSingleton<IBlogUserService, BlogUserService>(); builder.Services.AddSingleton<IBlogUserService, BlogUserService>();
builder.Services.AddSingleton<ISessionService, SessionService>(); builder.Services.AddSingleton<ISessionService, SessionService>();
builder.Services.AddSingleton<IProjectService, ProjectService>(); builder.Services.AddSingleton<IProjectService, ProjectService>();
builder.Services.AddSingleton<IMastodonService, MastodonService>();
builder.Services.AddSingleton<IReadingListService, ReadingListService>(); builder.Services.AddSingleton<IReadingListService, ReadingListService>();
builder.Services.AddRazorPages().AddRazorRuntimeCompilation(); builder.Services.AddRazorPages().AddRazorRuntimeCompilation();
builder.Services.AddControllersWithViews(); builder.Services.AddControllersWithViews();

View File

@ -0,0 +1,12 @@
using OliverBooth.Data.Mastodon;
namespace OliverBooth.Services;
public interface IMastodonService
{
/// <summary>
/// Gets the latest status posted to Mastodon.
/// </summary>
/// <returns>The latest status.</returns>
MastodonStatus GetLatestStatus();
}

View File

@ -0,0 +1,46 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using HtmlAgilityPack;
using OliverBooth.Data.Mastodon;
namespace OliverBooth.Services;
internal sealed class MastodonService : IMastodonService
{
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
{
Converters = { new JsonStringEnumConverter() },
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
};
private readonly HttpClient _httpClient;
public MastodonService(HttpClient httpClient)
{
_httpClient = httpClient;
}
/// <inheritdoc />
public MastodonStatus GetLatestStatus()
{
string token = Environment.GetEnvironmentVariable("MASTODON_TOKEN") ?? string.Empty;
string account = Environment.GetEnvironmentVariable("MASTODON_ACCOUNT") ?? string.Empty;
using var request = new HttpRequestMessage();
request.Headers.Add("Authorization", $"Bearer {token}");
request.RequestUri = new Uri($"https://mastodon.olivr.me/api/v1/accounts/{account}/statuses");
using HttpResponseMessage response = _httpClient.Send(request);
using var stream = response.Content.ReadAsStream();
var statuses = JsonSerializer.Deserialize<MastodonStatus[]>(stream, JsonSerializerOptions);
MastodonStatus status = statuses?[0]!;
var document = new HtmlDocument();
document.LoadHtml(status.Content);
HtmlNodeCollection links = document.DocumentNode.SelectNodes("//a");
foreach (HtmlNode link in links) link.InnerHtml = link.InnerText;
status.Content = document.DocumentNode.OuterHtml;
return status;
}
}

View File

@ -19,3 +19,5 @@ services:
environment: environment:
- SSL_CERT_PATH=${SSL_CERT_PATH} - SSL_CERT_PATH=${SSL_CERT_PATH}
- SSL_KEY_PATH=${SSL_KEY_PATH} - SSL_KEY_PATH=${SSL_KEY_PATH}
- MASTODON_TOKEN=${MASTODON_TOKEN}
- MASTODON_ACCOUNT=${MASTODON_ACCOUNT}

View File

@ -365,25 +365,27 @@ td.trim-p p:last-child {
background-position: center; background-position: center;
background-repeat: no-repeat; background-repeat: no-repeat;
background-size: cover; background-size: cover;
border-radius: 10px;
cursor: pointer;
* {
cursor: pointer;
}
p { .usa-countdown-element {
margin: 10px 0;
padding: 5px;
font-family: "Gabarito", sans-serif; font-family: "Gabarito", sans-serif;
font-weight: 500; font-weight: 500;
text-align: center; text-align: center;
font-size: 3em; font-size: 3em;
margin: 0; border-right: 2px solid #fff;
padding: 0; border-left: 2px solid #fff;
a { &:first-child {
transition: color 250ms; border-left: none;
} }
&:last-child {
a:link, a:visited, a:active { border-right: none;
color: #fff;
}
a:hover {
color: #03A9F4;
} }
} }
} }
@ -401,3 +403,16 @@ td.trim-p p:last-child {
background-color: #1E1E1E !important; background-color: #1E1E1E !important;
} }
} }
.mastodon-update-card.card {
background-color: desaturate(darken(#6364FF, 50%), 50%);
margin-bottom: 50px;
p:last-child {
margin-bottom: 0;
}
button.btn.btn-mastodon {
background-color: #6364FF;
}
}

View File

@ -82,6 +82,33 @@ class UI {
UI.updateProjectCards(element); UI.updateProjectCards(element);
} }
public static updateUsaCountdown(element?: Element){
element = element || document.getElementById("usa-countdown");
const daysElement = element.querySelector("#usa-countdown-days");
const hoursElement = element.querySelector("#usa-countdown-hours");
const minutesElement = element.querySelector("#usa-countdown-minutes");
const secondsElement = element.querySelector("#usa-countdown-seconds");
const start = new Date().getTime();
const end = Date.UTC(2024, 2, 7, 13, 20);
const diff = end - start;
let days = Math.floor(diff / (1000 * 60 * 60 * 24));
let hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
let minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor((diff % (1000 * 60)) / 1000);
if (days < 0) days = 0
if (hours < 0) hours = 0;
if (minutes < 0) minutes = 0;
if (seconds < 0) seconds = 0;
daysElement.innerHTML = days.toString().padStart(2, '0');
hoursElement.innerHTML = hours.toString().padStart(2, '0');
minutesElement.innerHTML = minutes.toString().padStart(2, '0');
secondsElement.innerHTML = seconds.toString().padStart(2, '0');
}
/** /**
* Adds Bootstrap tooltips to all elements with a title attribute. * Adds Bootstrap tooltips to all elements with a title attribute.
* @param element The element to search for elements with a title attribute in. * @param element The element to search for elements with a title attribute in.

View File

@ -98,26 +98,10 @@ declare const Prism: any;
UI.updateUI(); UI.updateUI();
setInterval(() => { const usaCountdown = document.getElementById("usa-countdown");
const countdown = document.querySelector("#usa-countdown p"); if (usaCountdown) {
const start = new Date().getTime(); usaCountdown.addEventListener("click", () => window.location.href = "/blog/2024/02/19/the-american");
const end = Date.UTC(2024, 2, 7, 13, 20); UI.updateUsaCountdown(usaCountdown);
const diff = end - start; setInterval(() => UI.updateUsaCountdown(usaCountdown), 1000);
let days = Math.floor(diff / (1000 * 60 * 60 * 24)); }
let hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
let minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor((diff % (1000 * 60)) / 1000);
if (days < 0) days = 0
if (hours < 0) hours = 0;
if (minutes < 0) minutes = 0;
if (seconds < 0) seconds = 0;
const blogUrl = '/blog/2024/02/19/the-american';
const dayStr = days.toString().padStart(2, '0');
const hourStr = hours.toString().padStart(2, '0');
const minuteStr = minutes.toString().padStart(2, '0');
const secondStr = seconds.toString().padStart(2, '0');
countdown.innerHTML = `<a href="${blogUrl}">${dayStr} : ${hourStr} : ${minuteStr} : ${secondStr}</a>`;
}, 1000);
})(); })();