Configurable post page

This commit is contained in:
2025-12-12 00:10:57 +08:00
parent 8181938aaf
commit 8642737a07
6 changed files with 87 additions and 38 deletions

View File

@@ -59,7 +59,11 @@ public enum PublicationPageType
/// The redirect mode allows user to create a shortcut for their own link.
/// such as example.solian.page/rickroll -- DyZ 301 -> youtube.com/...
/// </summary>
Redirect
Redirect,
/// <summary>
/// The Post Page type allows user render a list of posts based on the preconfigured filter.
/// </summary>
PostPage
}
public class SnPublicationPage : ModelBase
@@ -72,4 +76,4 @@ public class SnPublicationPage : ModelBase
public Guid SiteId { get; set; }
[JsonIgnore] public SnPublicationSite Site { get; set; } = null!;
}
}

View File

@@ -0,0 +1,18 @@
namespace DysonNetwork.Zone.Customization;
// PostPage.Config -> filter
public class PostPageFilterConfig
{
public List<int> Types { get; set; }
public string? PubName { get; set; }
public string? OrderBy { get; set; }
public bool OrderDesc { get; set; } = true;
}
// PostPage.Config -> layout
public class PostPageLayoutConfig
{
public string? Title { get; set; }
public string? Description { get; set; }
public bool ShowPub { get; set; } = true;
}

View File

@@ -2,10 +2,9 @@
@model DysonNetwork.Zone.Pages.PostsModel
@{
Layout = "_LayoutContained";
const string defaultAvatar = "https://www.gravatar.com/avatar/00000000000000000000000000000000?d=mp";
var pageTitle = "Posts";
var pageDescription = "A collection of posts.";
var pageTitle = Model.LayoutConfig?.Title ?? "Posts";
var pageDescription = Model.LayoutConfig?.Description ?? "A collection of posts.";
string? ogImageUrl = null;
var canonicalUrl = $"{Request.Scheme}://{Request.Host}{Request.Path}{Request.QueryString}";
var siteName = Model.Site?.Name ?? "Solar Network";
@@ -48,7 +47,7 @@
<div class="container mx-auto px-8 py-8">
<h1 class="text-3xl font-bold mb-8 px-5">
<span class="mdi mdi-note-text-outline"></span> Posts
<span class="mdi mdi-note-text-outline"></span> @pageTitle
</h1>
<div class="w-full grid grid-cols-3 gap-4">
@@ -75,8 +74,8 @@
<div class="join">
@{
const int maxPagesToShow = 5; // e.g., 2 before, current, 2 after
var startPage = Math.Max(1, Model.CurrentPage - (maxPagesToShow / 2));
var endPage = Math.Min(Model.TotalPages, Model.CurrentPage + (maxPagesToShow / 2));
var startPage = Math.Max(1, Model.Index - (maxPagesToShow / 2));
var endPage = Math.Min(Model.TotalPages, Model.Index + (maxPagesToShow / 2));
// Adjust startPage and endPage to ensure exactly maxPagesToShow are shown if possible
if (endPage - startPage + 1 < maxPagesToShow)
@@ -92,15 +91,14 @@
}
}
<a asp-page="/Posts"
asp-route-currentPage="@(Model.CurrentPage > 1 ? Model.CurrentPage - 1 : 1)"
class="join-item btn @(Model.CurrentPage == 1 ? "btn-disabled" : "")">«</a>
<a href="/posts?index=@(Model.Index > 1 ? Model.Index - 1 : 1)"
class="join-item btn @(Model.Index == 1 ? "btn-disabled" : "")">«</a>
@if (startPage > 1)
{
<a asp-page="/Posts"
asp-route-currentPage="1"
class="join-item btn @(1 == Model.CurrentPage ? "btn-active" : "")">
class="join-item btn @(1 == Model.Index ? "btn-active" : "")">
1
</a>
@if (startPage > 2)
@@ -111,11 +109,8 @@
@for (var idx = startPage; idx <= endPage; idx++)
{
var pageIdx = idx;
<a asp-page="/Posts"
asp-route-currentPage="@pageIdx"
class="join-item btn @(pageIdx == Model.CurrentPage ? "btn-active" : "")">
@pageIdx
<a href="/posts?index=@(idx)" class="join-item btn @(idx == Model.Index ? "btn-active" : "")">
@idx
</a>
}
@@ -125,16 +120,14 @@
{
<span class="join-item btn btn-disabled">...</span>
}
<a asp-page="/Posts"
asp-route-currentPage="@Model.TotalPages"
class="join-item btn @(Model.TotalPages == Model.CurrentPage ? "btn-active" : "")">
<a href="/posts?index=@(Model.TotalPages)"
class="join-item btn @(Model.TotalPages == Model.Index ? "btn-active" : "")">
@Model.TotalPages
</a>
}
<a asp-page="/Posts"
asp-route-currentPage="@(Model.CurrentPage < Model.TotalPages ? Model.CurrentPage + 1 : Model.TotalPages)"
class="join-item btn @(Model.CurrentPage == Model.TotalPages ? "btn-disabled" : "")">»</a>
<a href="/posts?index=@(Model.Index < Model.TotalPages ? Model.Index + 1 : Model.TotalPages)"
class="join-item btn @(Model.Index == Model.TotalPages ? "btn-disabled" : "")">»</a>
</div>
</div>
}

View File

@@ -1,6 +1,7 @@
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Shared.Registry;
using DysonNetwork.Zone.Customization;
using DysonNetwork.Zone.Publication;
using Microsoft.AspNetCore.Mvc;
// Add this using statement
@@ -15,34 +16,54 @@ public class PostsModel(
MarkdownConverter markdownConverter
) : PageModel
{
[FromQuery] public bool ShowAll { get; set; } = false;
public SnPublicationSite? Site { get; set; }
public SnPublisher? Publisher { get; set; }
public List<SnPost> Posts { get; set; } = [];
public int TotalCount { get; set; }
public int CurrentPage { get; set; }
public int Index { get; set; }
public int PageSize { get; set; } = 10;
public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);
public async Task OnGetAsync(int currentPage = 1)
{
Site = HttpContext.Items[PublicationSiteMiddleware.SiteContextKey] as SnPublicationSite;
CurrentPage = currentPage;
public PostPageFilterConfig? FilterConfig { get; set; }
public PostPageLayoutConfig? LayoutConfig { get; set; }
Publisher = await rps.GetPublisher(id: Site!.PublisherId.ToString());
public async Task OnGetAsync(int index = 1)
{
FilterConfig = HttpContext.Items["PostPage_FilterConfig"] as PostPageFilterConfig;
LayoutConfig = HttpContext.Items["PostPage_LayoutConfig"] as PostPageLayoutConfig;
Site = HttpContext.Items[PublicationSiteMiddleware.SiteContextKey] as SnPublicationSite;
Index = index;
Publisher = FilterConfig?.PubName is not null
? await rps.GetPublisher(FilterConfig.PubName)
: await rps.GetPublisher(id: Site!.PublisherId.ToString());
var request = new ListPostsRequest
{
OrderBy = "date",
OrderDesc = true,
OrderBy = FilterConfig?.OrderBy,
OrderDesc = FilterConfig?.OrderDesc ?? true,
PageSize = PageSize,
PageToken = ((CurrentPage - 1) * PageSize).ToString(),
PublisherId = Site!.PublisherId.ToString()
PageToken = ((Index - 1) * PageSize).ToString(),
PublisherId = Publisher!.Id.ToString()
};
if (!ShowAll) request.Types_.Add(DysonNetwork.Shared.Proto.PostType.Article);
if (FilterConfig?.Types is not null)
{
foreach (var type in FilterConfig.Types)
{
request.Types_.Add(type switch
{
0 => DysonNetwork.Shared.Proto.PostType.Moment,
1 => DysonNetwork.Shared.Proto.PostType.Article,
_ => DysonNetwork.Shared.Proto.PostType.Unspecified,
});
}
}
else
{
request.Types_.Add(DysonNetwork.Shared.Proto.PostType.Article);
}
var response = await postClient.ListPostsAsync(request);

View File

@@ -80,8 +80,7 @@
<div class="text-sm text-base-content/60">
Posted on @Model.CreatedAt.ToDateTimeOffset().ToString("yyyy-MM-dd")
</div>
<a asp-page="/Posts/Details" asp-route-slug="@(Model.Slug ?? Model.Id.ToString())"
class="btn btn-sm btn-ghost btn-circle">
<a href="/p/@(Model.Slug ?? Model.Id.ToString())"class="btn btn-sm btn-ghost btn-circle">
<span class="mdi mdi-arrow-right text-lg"></span>
</a>
</div>

View File

@@ -1,5 +1,7 @@
using System.Text.Json;
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Zone.Customization;
using DysonNetwork.Zone.Pages.Dynamic;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
@@ -76,6 +78,18 @@ public class PublicationSiteMiddleware(RequestDelegate next)
when page.Config.TryGetValue("target", out var tgt) && tgt is JsonElement redirectUrl:
context.Response.Redirect(redirectUrl.ToString());
return;
case PublicationPageType.PostPage:
PostPageFilterConfig? filterConfig = null;
if (page.Config.TryGetValue("filter", out var filter) && filter is JsonElement filterJson)
filterConfig = filterJson.Deserialize<PostPageFilterConfig>(GrpcTypeHelper.SerializerOptions);
PostPageLayoutConfig? layoutConfig = null;
if (page.Config.TryGetValue("layout", out var layout) && layout is JsonElement layoutJson)
layoutConfig = layoutJson.Deserialize<PostPageLayoutConfig>(GrpcTypeHelper.SerializerOptions);
context.Items["PostPage_LayoutConfig"] = layoutConfig;
context.Items["PostPage_FilterConfig"] = filterConfig;
context.Request.Path = "/Posts";
await next(context);
return;
default:
throw new ArgumentOutOfRangeException();
}