✨ Configurable post page
This commit is contained in:
@@ -59,7 +59,11 @@ public enum PublicationPageType
|
|||||||
/// The redirect mode allows user to create a shortcut for their own link.
|
/// The redirect mode allows user to create a shortcut for their own link.
|
||||||
/// such as example.solian.page/rickroll -- DyZ 301 -> youtube.com/...
|
/// such as example.solian.page/rickroll -- DyZ 301 -> youtube.com/...
|
||||||
/// </summary>
|
/// </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
|
public class SnPublicationPage : ModelBase
|
||||||
@@ -72,4 +76,4 @@ public class SnPublicationPage : ModelBase
|
|||||||
|
|
||||||
public Guid SiteId { get; set; }
|
public Guid SiteId { get; set; }
|
||||||
[JsonIgnore] public SnPublicationSite Site { get; set; } = null!;
|
[JsonIgnore] public SnPublicationSite Site { get; set; } = null!;
|
||||||
}
|
}
|
||||||
|
|||||||
18
DysonNetwork.Zone/Customization/PostPageConfig.cs
Normal file
18
DysonNetwork.Zone/Customization/PostPageConfig.cs
Normal 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;
|
||||||
|
}
|
||||||
@@ -2,10 +2,9 @@
|
|||||||
@model DysonNetwork.Zone.Pages.PostsModel
|
@model DysonNetwork.Zone.Pages.PostsModel
|
||||||
@{
|
@{
|
||||||
Layout = "_LayoutContained";
|
Layout = "_LayoutContained";
|
||||||
const string defaultAvatar = "https://www.gravatar.com/avatar/00000000000000000000000000000000?d=mp";
|
|
||||||
|
|
||||||
var pageTitle = "Posts";
|
var pageTitle = Model.LayoutConfig?.Title ?? "Posts";
|
||||||
var pageDescription = "A collection of posts.";
|
var pageDescription = Model.LayoutConfig?.Description ?? "A collection of posts.";
|
||||||
string? ogImageUrl = null;
|
string? ogImageUrl = null;
|
||||||
var canonicalUrl = $"{Request.Scheme}://{Request.Host}{Request.Path}{Request.QueryString}";
|
var canonicalUrl = $"{Request.Scheme}://{Request.Host}{Request.Path}{Request.QueryString}";
|
||||||
var siteName = Model.Site?.Name ?? "Solar Network";
|
var siteName = Model.Site?.Name ?? "Solar Network";
|
||||||
@@ -48,7 +47,7 @@
|
|||||||
|
|
||||||
<div class="container mx-auto px-8 py-8">
|
<div class="container mx-auto px-8 py-8">
|
||||||
<h1 class="text-3xl font-bold mb-8 px-5">
|
<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>
|
</h1>
|
||||||
|
|
||||||
<div class="w-full grid grid-cols-3 gap-4">
|
<div class="w-full grid grid-cols-3 gap-4">
|
||||||
@@ -75,8 +74,8 @@
|
|||||||
<div class="join">
|
<div class="join">
|
||||||
@{
|
@{
|
||||||
const int maxPagesToShow = 5; // e.g., 2 before, current, 2 after
|
const int maxPagesToShow = 5; // e.g., 2 before, current, 2 after
|
||||||
var startPage = Math.Max(1, Model.CurrentPage - (maxPagesToShow / 2));
|
var startPage = Math.Max(1, Model.Index - (maxPagesToShow / 2));
|
||||||
var endPage = Math.Min(Model.TotalPages, Model.CurrentPage + (maxPagesToShow / 2));
|
var endPage = Math.Min(Model.TotalPages, Model.Index + (maxPagesToShow / 2));
|
||||||
|
|
||||||
// Adjust startPage and endPage to ensure exactly maxPagesToShow are shown if possible
|
// Adjust startPage and endPage to ensure exactly maxPagesToShow are shown if possible
|
||||||
if (endPage - startPage + 1 < maxPagesToShow)
|
if (endPage - startPage + 1 < maxPagesToShow)
|
||||||
@@ -92,15 +91,14 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
<a asp-page="/Posts"
|
<a href="/posts?index=@(Model.Index > 1 ? Model.Index - 1 : 1)"
|
||||||
asp-route-currentPage="@(Model.CurrentPage > 1 ? Model.CurrentPage - 1 : 1)"
|
class="join-item btn @(Model.Index == 1 ? "btn-disabled" : "")">«</a>
|
||||||
class="join-item btn @(Model.CurrentPage == 1 ? "btn-disabled" : "")">«</a>
|
|
||||||
|
|
||||||
@if (startPage > 1)
|
@if (startPage > 1)
|
||||||
{
|
{
|
||||||
<a asp-page="/Posts"
|
<a asp-page="/Posts"
|
||||||
asp-route-currentPage="1"
|
asp-route-currentPage="1"
|
||||||
class="join-item btn @(1 == Model.CurrentPage ? "btn-active" : "")">
|
class="join-item btn @(1 == Model.Index ? "btn-active" : "")">
|
||||||
1
|
1
|
||||||
</a>
|
</a>
|
||||||
@if (startPage > 2)
|
@if (startPage > 2)
|
||||||
@@ -111,11 +109,8 @@
|
|||||||
|
|
||||||
@for (var idx = startPage; idx <= endPage; idx++)
|
@for (var idx = startPage; idx <= endPage; idx++)
|
||||||
{
|
{
|
||||||
var pageIdx = idx;
|
<a href="/posts?index=@(idx)" class="join-item btn @(idx == Model.Index ? "btn-active" : "")">
|
||||||
<a asp-page="/Posts"
|
@idx
|
||||||
asp-route-currentPage="@pageIdx"
|
|
||||||
class="join-item btn @(pageIdx == Model.CurrentPage ? "btn-active" : "")">
|
|
||||||
@pageIdx
|
|
||||||
</a>
|
</a>
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,16 +120,14 @@
|
|||||||
{
|
{
|
||||||
<span class="join-item btn btn-disabled">...</span>
|
<span class="join-item btn btn-disabled">...</span>
|
||||||
}
|
}
|
||||||
<a asp-page="/Posts"
|
<a href="/posts?index=@(Model.TotalPages)"
|
||||||
asp-route-currentPage="@Model.TotalPages"
|
class="join-item btn @(Model.TotalPages == Model.Index ? "btn-active" : "")">
|
||||||
class="join-item btn @(Model.TotalPages == Model.CurrentPage ? "btn-active" : "")">
|
|
||||||
@Model.TotalPages
|
@Model.TotalPages
|
||||||
</a>
|
</a>
|
||||||
}
|
}
|
||||||
|
|
||||||
<a asp-page="/Posts"
|
<a href="/posts?index=@(Model.Index < Model.TotalPages ? Model.Index + 1 : Model.TotalPages)"
|
||||||
asp-route-currentPage="@(Model.CurrentPage < Model.TotalPages ? Model.CurrentPage + 1 : Model.TotalPages)"
|
class="join-item btn @(Model.Index == Model.TotalPages ? "btn-disabled" : "")">»</a>
|
||||||
class="join-item btn @(Model.CurrentPage == Model.TotalPages ? "btn-disabled" : "")">»</a>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using DysonNetwork.Shared.Models;
|
using DysonNetwork.Shared.Models;
|
||||||
using DysonNetwork.Shared.Proto;
|
using DysonNetwork.Shared.Proto;
|
||||||
using DysonNetwork.Shared.Registry;
|
using DysonNetwork.Shared.Registry;
|
||||||
|
using DysonNetwork.Zone.Customization;
|
||||||
using DysonNetwork.Zone.Publication;
|
using DysonNetwork.Zone.Publication;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
// Add this using statement
|
// Add this using statement
|
||||||
@@ -15,34 +16,54 @@ public class PostsModel(
|
|||||||
MarkdownConverter markdownConverter
|
MarkdownConverter markdownConverter
|
||||||
) : PageModel
|
) : PageModel
|
||||||
{
|
{
|
||||||
[FromQuery] public bool ShowAll { get; set; } = false;
|
|
||||||
|
|
||||||
public SnPublicationSite? Site { get; set; }
|
public SnPublicationSite? Site { get; set; }
|
||||||
public SnPublisher? Publisher { get; set; }
|
public SnPublisher? Publisher { get; set; }
|
||||||
public List<SnPost> Posts { get; set; } = [];
|
public List<SnPost> Posts { get; set; } = [];
|
||||||
public int TotalCount { 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 PageSize { get; set; } = 10;
|
||||||
public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);
|
public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);
|
||||||
|
|
||||||
public async Task OnGetAsync(int currentPage = 1)
|
public PostPageFilterConfig? FilterConfig { get; set; }
|
||||||
{
|
public PostPageLayoutConfig? LayoutConfig { get; set; }
|
||||||
Site = HttpContext.Items[PublicationSiteMiddleware.SiteContextKey] as SnPublicationSite;
|
|
||||||
CurrentPage = currentPage;
|
|
||||||
|
|
||||||
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
|
var request = new ListPostsRequest
|
||||||
{
|
{
|
||||||
OrderBy = "date",
|
OrderBy = FilterConfig?.OrderBy,
|
||||||
OrderDesc = true,
|
OrderDesc = FilterConfig?.OrderDesc ?? true,
|
||||||
PageSize = PageSize,
|
PageSize = PageSize,
|
||||||
PageToken = ((CurrentPage - 1) * PageSize).ToString(),
|
PageToken = ((Index - 1) * PageSize).ToString(),
|
||||||
PublisherId = Site!.PublisherId.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);
|
var response = await postClient.ListPostsAsync(request);
|
||||||
|
|
||||||
|
|||||||
@@ -80,8 +80,7 @@
|
|||||||
<div class="text-sm text-base-content/60">
|
<div class="text-sm text-base-content/60">
|
||||||
Posted on @Model.CreatedAt.ToDateTimeOffset().ToString("yyyy-MM-dd")
|
Posted on @Model.CreatedAt.ToDateTimeOffset().ToString("yyyy-MM-dd")
|
||||||
</div>
|
</div>
|
||||||
<a asp-page="/Posts/Details" asp-route-slug="@(Model.Slug ?? Model.Id.ToString())"
|
<a href="/p/@(Model.Slug ?? Model.Id.ToString())"class="btn btn-sm btn-ghost btn-circle">
|
||||||
class="btn btn-sm btn-ghost btn-circle">
|
|
||||||
<span class="mdi mdi-arrow-right text-lg"></span>
|
<span class="mdi mdi-arrow-right text-lg"></span>
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using DysonNetwork.Shared.Models;
|
using DysonNetwork.Shared.Models;
|
||||||
|
using DysonNetwork.Shared.Proto;
|
||||||
|
using DysonNetwork.Zone.Customization;
|
||||||
using DysonNetwork.Zone.Pages.Dynamic;
|
using DysonNetwork.Zone.Pages.Dynamic;
|
||||||
using Microsoft.AspNetCore.Http.Features;
|
using Microsoft.AspNetCore.Http.Features;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
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:
|
when page.Config.TryGetValue("target", out var tgt) && tgt is JsonElement redirectUrl:
|
||||||
context.Response.Redirect(redirectUrl.ToString());
|
context.Response.Redirect(redirectUrl.ToString());
|
||||||
return;
|
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:
|
default:
|
||||||
throw new ArgumentOutOfRangeException();
|
throw new ArgumentOutOfRangeException();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user