mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-02 10:08:39 +00:00
Optimize GameServer /announce and add website announcements (#810)
* Improve game server announce by using StringBuilder * Implement web announcements (condensed commit) * Implement discord webhook support * Display a separate message if there are no announcements * Fix announcement string unit tests * Fix header admin button unit test * Clarify announcement id variable name * Increase webhook truncation limit to 250 chars * Convert announce text to string when returning 200 * Fix announcement unit tests ... again * Make announcement text input a textarea rather than a simple input * Fix styling discrepancy * Clarify submission button * Improve announcement webhook & set default textarea row amount
This commit is contained in:
parent
0fd8759f3f
commit
689ebd3791
12 changed files with 256 additions and 20 deletions
|
@ -57,4 +57,7 @@
|
||||||
<data name="email" xml:space="preserve">
|
<data name="email" xml:space="preserve">
|
||||||
<value>Email</value>
|
<value>Email</value>
|
||||||
</data>
|
</data>
|
||||||
|
<data name="announcements" xml:space="preserve">
|
||||||
|
<value>Announcements</value>
|
||||||
|
</data>
|
||||||
</root>
|
</root>
|
|
@ -15,6 +15,7 @@ public static class GeneralStrings
|
||||||
public static readonly TranslatableString RecentPhotos = create("recent_photos");
|
public static readonly TranslatableString RecentPhotos = create("recent_photos");
|
||||||
public static readonly TranslatableString RecentActivity = create("recent_activity");
|
public static readonly TranslatableString RecentActivity = create("recent_activity");
|
||||||
public static readonly TranslatableString Soon = create("soon");
|
public static readonly TranslatableString Soon = create("soon");
|
||||||
|
public static readonly TranslatableString Announcements = create("announcements");
|
||||||
|
|
||||||
private static TranslatableString create(string key) => new(TranslationAreas.General, key);
|
private static TranslatableString create(string key) => new(TranslationAreas.General, key);
|
||||||
}
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
#nullable enable
|
#nullable enable
|
||||||
|
using System.Text;
|
||||||
using LBPUnion.ProjectLighthouse.Configuration;
|
using LBPUnion.ProjectLighthouse.Configuration;
|
||||||
using LBPUnion.ProjectLighthouse.Database;
|
using LBPUnion.ProjectLighthouse.Database;
|
||||||
using LBPUnion.ProjectLighthouse.Extensions;
|
using LBPUnion.ProjectLighthouse.Extensions;
|
||||||
|
@ -51,25 +52,22 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.";
|
||||||
|
|
||||||
string username = await this.database.UsernameFromGameToken(token);
|
string username = await this.database.UsernameFromGameToken(token);
|
||||||
|
|
||||||
string announceText = ServerConfiguration.Instance.AnnounceText;
|
StringBuilder announceText = new(ServerConfiguration.Instance.AnnounceText);
|
||||||
|
|
||||||
announceText = announceText.Replace("%user", username);
|
announceText.Replace("%user", username);
|
||||||
announceText = announceText.Replace("%id", token.UserId.ToString());
|
announceText.Replace("%id", token.UserId.ToString());
|
||||||
|
|
||||||
return this.Ok
|
#if DEBUG
|
||||||
(
|
announceText.Append("\n\n---DEBUG INFO---\n" +
|
||||||
announceText +
|
$"user.UserId: {token.UserId}\n" +
|
||||||
#if DEBUG
|
$"token.UserLocation: {token.UserLocation}\n" +
|
||||||
"\n\n---DEBUG INFO---\n" +
|
$"token.GameVersion: {token.GameVersion}\n" +
|
||||||
$"user.UserId: {token.UserId}\n" +
|
$"token.TicketHash: {token.TicketHash}\n" +
|
||||||
$"token.UserLocation: {token.UserLocation}\n" +
|
$"token.ExpiresAt: {token.ExpiresAt.ToString()}\n" +
|
||||||
$"token.GameVersion: {token.GameVersion}\n" +
|
"---DEBUG INFO---");
|
||||||
$"token.TicketHash: {token.TicketHash}\n" +
|
#endif
|
||||||
$"token.ExpiresAt: {token.ExpiresAt.ToString()}\n" +
|
|
||||||
"---DEBUG INFO---" +
|
return this.Ok(announceText.ToString());
|
||||||
#endif
|
|
||||||
(string.IsNullOrWhiteSpace(announceText) ? "" : "\n")
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("notification")]
|
[HttpGet("notification")]
|
||||||
|
|
67
ProjectLighthouse.Servers.Website/Pages/AnnouncePage.cshtml
Normal file
67
ProjectLighthouse.Servers.Website/Pages/AnnouncePage.cshtml
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
@page "/announce"
|
||||||
|
@using LBPUnion.ProjectLighthouse.Localization.StringLists
|
||||||
|
@using LBPUnion.ProjectLighthouse.Types.Entities.Website
|
||||||
|
@model LBPUnion.ProjectLighthouse.Servers.Website.Pages.AnnouncePage
|
||||||
|
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
|
||||||
|
|
||||||
|
@{
|
||||||
|
Layout = "Layouts/BaseLayout";
|
||||||
|
Model.Title = Model.Translate(GeneralStrings.Announcements);
|
||||||
|
}
|
||||||
|
|
||||||
|
@if (Model.User != null && Model.User.IsAdmin)
|
||||||
|
{
|
||||||
|
<div class="ui red segment">
|
||||||
|
<h3>Post New Announcement</h3>
|
||||||
|
@if (!string.IsNullOrWhiteSpace(Model.Error))
|
||||||
|
{
|
||||||
|
@await Html.PartialAsync("Partials/ErrorModalPartial", (Model.Translate(GeneralStrings.Error), Model.Error), ViewData)
|
||||||
|
}
|
||||||
|
<form id="form" method="POST" class="ui form center aligned" action="/announce">
|
||||||
|
@Html.AntiForgeryToken()
|
||||||
|
<div class="field">
|
||||||
|
<label style="text-align: left" for="title">Announcement Title</label>
|
||||||
|
<input type="text" name="title" id="title">
|
||||||
|
</div>
|
||||||
|
<div class="field">
|
||||||
|
<label style="text-align: left" for="content">Announcement Content</label>
|
||||||
|
<textarea name="content" id="content" spellcheck="false" rows="3"></textarea>
|
||||||
|
</div>
|
||||||
|
<button class="ui button green" type="submit" tabindex="0">Post Announcement</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
|
||||||
|
@if (Model.Announcements.Any())
|
||||||
|
{
|
||||||
|
@foreach (WebsiteAnnouncementEntity announcement in Model.Announcements)
|
||||||
|
{
|
||||||
|
<div class="ui blue segment" style="position: relative;">
|
||||||
|
<h3>@announcement.Title</h3>
|
||||||
|
<p style="white-space: initial;">
|
||||||
|
@announcement.Content
|
||||||
|
</p>
|
||||||
|
|
||||||
|
@if (Model.User != null && Model.User.IsAdmin)
|
||||||
|
{
|
||||||
|
<form method="post">
|
||||||
|
@Html.AntiForgeryToken()
|
||||||
|
<button
|
||||||
|
asp-page-handler="delete"
|
||||||
|
asp-route-id="@announcement.AnnouncementId"
|
||||||
|
onclick="return confirm('Are you sure you want to delete this announcement?')"
|
||||||
|
class="ui red icon button"
|
||||||
|
style="position: absolute; right: 0.5em; top: 0.5em">
|
||||||
|
<i class="trash icon"></i>
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<div class="ui blue segment" style="position: relative;">
|
||||||
|
<p>There are no announcements to display.</p>
|
||||||
|
</div>
|
||||||
|
}
|
|
@ -0,0 +1,82 @@
|
||||||
|
#nullable enable
|
||||||
|
|
||||||
|
using LBPUnion.ProjectLighthouse.Configuration;
|
||||||
|
using LBPUnion.ProjectLighthouse.Database;
|
||||||
|
using LBPUnion.ProjectLighthouse.Helpers;
|
||||||
|
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
|
||||||
|
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
||||||
|
using LBPUnion.ProjectLighthouse.Types.Entities.Website;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace LBPUnion.ProjectLighthouse.Servers.Website.Pages;
|
||||||
|
|
||||||
|
public class AnnouncePage : BaseLayout
|
||||||
|
{
|
||||||
|
public AnnouncePage(DatabaseContext database) : base(database)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
public List<WebsiteAnnouncementEntity> Announcements { get; set; } = new();
|
||||||
|
public string Error { get; set; } = "";
|
||||||
|
|
||||||
|
public async Task<IActionResult> OnGet()
|
||||||
|
{
|
||||||
|
this.Announcements = await this.Database.WebsiteAnnouncements
|
||||||
|
.OrderByDescending(a => a.AnnouncementId)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
return this.Page();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> OnPost([FromForm] string title, [FromForm] string content)
|
||||||
|
{
|
||||||
|
UserEntity? user = this.Database.UserFromWebRequest(this.Request);
|
||||||
|
if (user == null) return this.BadRequest();
|
||||||
|
if (!user.IsAdmin) return this.BadRequest();
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(title) || string.IsNullOrWhiteSpace(content))
|
||||||
|
{
|
||||||
|
this.Error = "Invalid form data, please ensure all fields are filled out.";
|
||||||
|
return this.Page();
|
||||||
|
}
|
||||||
|
|
||||||
|
WebsiteAnnouncementEntity announcement = new()
|
||||||
|
{
|
||||||
|
Title = title,
|
||||||
|
Content = content,
|
||||||
|
};
|
||||||
|
|
||||||
|
await this.Database.WebsiteAnnouncements.AddAsync(announcement);
|
||||||
|
await this.Database.SaveChangesAsync();
|
||||||
|
|
||||||
|
if (DiscordConfiguration.Instance.DiscordIntegrationEnabled)
|
||||||
|
{
|
||||||
|
string truncatedAnnouncement = content.Length > 250
|
||||||
|
? content[..250] + $"... [read more]({ServerConfiguration.Instance.ExternalUrl}/announce)"
|
||||||
|
: content;
|
||||||
|
|
||||||
|
await WebhookHelper.SendWebhook($":mega: {title}",
|
||||||
|
truncatedAnnouncement);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.RedirectToPage();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> OnPostDelete(int id)
|
||||||
|
{
|
||||||
|
UserEntity? user = this.Database.UserFromWebRequest(this.Request);
|
||||||
|
if (user == null) return this.BadRequest();
|
||||||
|
if (!user.IsAdmin) return this.BadRequest();
|
||||||
|
|
||||||
|
WebsiteAnnouncementEntity? announcement = await this.Database.WebsiteAnnouncements
|
||||||
|
.Where(a => a.AnnouncementId == id)
|
||||||
|
.FirstOrDefaultAsync();
|
||||||
|
|
||||||
|
if (announcement == null) return this.BadRequest();
|
||||||
|
|
||||||
|
this.Database.WebsiteAnnouncements.Remove(announcement);
|
||||||
|
await this.Database.SaveChangesAsync();
|
||||||
|
|
||||||
|
return this.RedirectToPage();
|
||||||
|
}
|
||||||
|
}
|
|
@ -33,6 +33,8 @@ public class BaseLayout : PageModel
|
||||||
this.NavigationItems.Add(new PageNavigationItem(BaseLayoutStrings.HeaderUsers, "/users/0", "user friends"));
|
this.NavigationItems.Add(new PageNavigationItem(BaseLayoutStrings.HeaderUsers, "/users/0", "user friends"));
|
||||||
this.NavigationItems.Add(new PageNavigationItem(BaseLayoutStrings.HeaderPhotos, "/photos/0", "camera"));
|
this.NavigationItems.Add(new PageNavigationItem(BaseLayoutStrings.HeaderPhotos, "/photos/0", "camera"));
|
||||||
this.NavigationItems.Add(new PageNavigationItem(BaseLayoutStrings.HeaderSlots, "/slots/0", "globe americas"));
|
this.NavigationItems.Add(new PageNavigationItem(BaseLayoutStrings.HeaderSlots, "/slots/0", "globe americas"));
|
||||||
|
|
||||||
|
this.NavigationItemsRight.Add(new PageNavigationItem(GeneralStrings.Announcements, "/announce", "bullhorn"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public new UserEntity? User {
|
public new UserEntity? User {
|
||||||
|
|
|
@ -82,7 +82,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>." + "\nuni
|
||||||
|
|
||||||
ServerConfiguration.Instance.AnnounceText = "you are now logged in as %user (id: %id)";
|
ServerConfiguration.Instance.AnnounceText = "you are now logged in as %user (id: %id)";
|
||||||
|
|
||||||
const string expected = "you are now logged in as unittest (id: 1)\n";
|
const string expected = "you are now logged in as unittest (id: 1)";
|
||||||
|
|
||||||
IActionResult result = await messageController.Announce();
|
IActionResult result = await messageController.Announce();
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ namespace ProjectLighthouse.Tests.WebsiteTests.Integration;
|
||||||
[Trait("Category", "Integration")]
|
[Trait("Category", "Integration")]
|
||||||
public class AdminTests : LighthouseWebTest
|
public class AdminTests : LighthouseWebTest
|
||||||
{
|
{
|
||||||
private const string adminPanelButtonXPath = "/html/body/div/header/div/div/div/a[1]";
|
private const string adminPanelButtonXPath = "/html/body/div/header/div/div/div/a[2]";
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task ShouldShowAdminPanelButtonWhenAdmin()
|
public async Task ShouldShowAdminPanelButtonWhenAdmin()
|
||||||
|
|
|
@ -5,6 +5,7 @@ using LBPUnion.ProjectLighthouse.Types.Entities.Maintenance;
|
||||||
using LBPUnion.ProjectLighthouse.Types.Entities.Moderation;
|
using LBPUnion.ProjectLighthouse.Types.Entities.Moderation;
|
||||||
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
|
||||||
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
|
using LBPUnion.ProjectLighthouse.Types.Entities.Token;
|
||||||
|
using LBPUnion.ProjectLighthouse.Types.Entities.Website;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace LBPUnion.ProjectLighthouse.Database;
|
namespace LBPUnion.ProjectLighthouse.Database;
|
||||||
|
@ -61,6 +62,10 @@ public partial class DatabaseContext : DbContext
|
||||||
#region Misc
|
#region Misc
|
||||||
public DbSet<CompletedMigrationEntity> CompletedMigrations { get; set; }
|
public DbSet<CompletedMigrationEntity> CompletedMigrations { get; set; }
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Website
|
||||||
|
public DbSet<WebsiteAnnouncementEntity> WebsiteAnnouncements { get; set; }
|
||||||
|
#endregion
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
using LBPUnion.ProjectLighthouse.Database;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Metadata;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace ProjectLighthouse.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(DatabaseContext))]
|
||||||
|
[Migration("20230620211613_AddWebAnnouncementsToDb")]
|
||||||
|
public partial class AddWebAnnouncementsToDb : Migration
|
||||||
|
{
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "WebsiteAnnouncements",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
AnnouncementId = table.Column<int>(
|
||||||
|
type: "int",
|
||||||
|
nullable: false)
|
||||||
|
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
|
||||||
|
Title = table.Column<string>(
|
||||||
|
type: "longtext",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: "")
|
||||||
|
.Annotation("MySql:CharSet", "utf8mb4"),
|
||||||
|
Content = table.Column<string>(
|
||||||
|
type: "longtext",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: "")
|
||||||
|
.Annotation("MySql:CharSet", "utf8mb4")
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_WebsiteAnnouncements", x => x.AnnouncementId);
|
||||||
|
})
|
||||||
|
.Annotation("MySql:CharSet", "utf8mb4");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "WebsiteAnnouncements");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,7 +16,7 @@ namespace ProjectLighthouse.Migrations
|
||||||
{
|
{
|
||||||
#pragma warning disable 612, 618
|
#pragma warning disable 612, 618
|
||||||
modelBuilder
|
modelBuilder
|
||||||
.HasAnnotation("ProductVersion", "7.0.4")
|
.HasAnnotation("ProductVersion", "7.0.7")
|
||||||
.HasAnnotation("Relational:MaxIdentifierLength", 64);
|
.HasAnnotation("Relational:MaxIdentifierLength", 64);
|
||||||
|
|
||||||
modelBuilder.Entity("LBPUnion.ProjectLighthouse.Types.Entities.Interaction.HeartedLevelEntity", b =>
|
modelBuilder.Entity("LBPUnion.ProjectLighthouse.Types.Entities.Interaction.HeartedLevelEntity", b =>
|
||||||
|
@ -1024,6 +1024,23 @@ namespace ProjectLighthouse.Migrations
|
||||||
b.ToTable("WebTokens");
|
b.ToTable("WebTokens");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("LBPUnion.ProjectLighthouse.Types.Entities.Website.WebsiteAnnouncementEntity", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("AnnouncementId")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Content")
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<string>("Title")
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.HasKey("Identifier");
|
||||||
|
|
||||||
|
b.ToTable("WebsiteAnnouncements");
|
||||||
|
});
|
||||||
|
|
||||||
modelBuilder.Entity("LBPUnion.ProjectLighthouse.Types.Entities.Interaction.HeartedLevelEntity", b =>
|
modelBuilder.Entity("LBPUnion.ProjectLighthouse.Types.Entities.Interaction.HeartedLevelEntity", b =>
|
||||||
{
|
{
|
||||||
b.HasOne("LBPUnion.ProjectLighthouse.Types.Entities.Level.SlotEntity", "Slot")
|
b.HasOne("LBPUnion.ProjectLighthouse.Types.Entities.Level.SlotEntity", "Slot")
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace LBPUnion.ProjectLighthouse.Types.Entities.Website;
|
||||||
|
|
||||||
|
public class WebsiteAnnouncementEntity
|
||||||
|
{
|
||||||
|
[Key]
|
||||||
|
public int AnnouncementId { get; set; }
|
||||||
|
|
||||||
|
public string Title { get; set; }
|
||||||
|
|
||||||
|
public string Content { get; set; }
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue