💄 Updated the hosted site post page

This commit is contained in:
2025-11-22 13:09:57 +08:00
parent 92a4899e7c
commit 0b0598712e
6 changed files with 291 additions and 61 deletions

View File

@@ -211,7 +211,11 @@ message PostAward {
// ====================================
message GetPostRequest {
string id = 1;
oneof identifier {
string id = 1;
string slug = 2;
}
google.protobuf.StringValue publisher_id = 3;
}
message GetPostBatchRequest {

View File

@@ -10,10 +10,26 @@ public class PostServiceGrpc(AppDatabase db, PostService ps) : Shared.Proto.Post
{
public override async Task<Shared.Proto.Post> GetPost(GetPostRequest request, ServerCallContext context)
{
if (!Guid.TryParse(request.Id, out var id))
throw new RpcException(new Status(StatusCode.InvalidArgument, "invalid post id"));
var postQuery = db.Posts.AsQueryable();
var post = await db.Posts
switch (request.IdentifierCase)
{
case GetPostRequest.IdentifierOneofCase.Id:
if (!Guid.TryParse(request.Id, out var id))
throw new RpcException(new Status(StatusCode.InvalidArgument, "invalid post id"));
postQuery = postQuery.Where(p => p.Id == id);
break;
case GetPostRequest.IdentifierOneofCase.Slug:
postQuery = postQuery.Where(p => p.Slug == request.Slug);
break;
default:
throw new RpcException(new Status(StatusCode.InvalidArgument, "invalid identifier case"));
}
if (!string.IsNullOrWhiteSpace(request.PublisherId) && Guid.TryParse(request.PublisherId, out var pid))
postQuery = postQuery.Where(p => p.PublisherId == pid);
var post = await postQuery
.Include(p => p.Publisher)
.Include(p => p.Tags)
.Include(p => p.Categories)
@@ -21,7 +37,7 @@ public class PostServiceGrpc(AppDatabase db, PostService ps) : Shared.Proto.Post
.Include(p => p.ForwardedPost)
.Include(p => p.FeaturedRecords)
.FilterWithVisibility(null, [], [])
.FirstOrDefaultAsync(p => p.Id == id);
.FirstOrDefaultAsync();
if (post == null) throw new RpcException(new Status(StatusCode.NotFound, "post not found"));

View File

@@ -1,29 +1,105 @@
@page
@model DysonNetwork.Zone.Pages.PostsModel
@using DysonNetwork.Shared.Models
@{
Layout = "_LayoutContained";
const string defaultAvatar = "https://www.gravatar.com/avatar/00000000000000000000000000000000?d=mp";
}
<div class="container mx-auto px-8 pb-8">
<h1 class="text-3xl font-bold mb-8">Posts</h1>
<div class="container mx-auto px-8 py-8">
<h1 class="text-3xl font-bold mb-8">
<span class="mdi mdi-note-text-outline"></span> Posts
</h1>
<div class="flex flex-col md:flex-row gap-8">
<div class="w-full md:w-2/3">
<div class="w-full grid grid-cols-3 gap-4">
<div class="col-span-3 md:col-span-2">
@if (Model.Posts.Any())
{
<div class="space-y-8">
@foreach (var post in Model.Posts)
{
<div class="card bg-base-100 border">
<div class="card-body">
<h2 class="card-title">@post.Title</h2>
<p>@post.Content</p>
<div class="card-actions justify-end">
<div class="card bg-base-200 shadow-md">
<div class="card-body flex flex-col gap-5">
<div>
@if (!string.IsNullOrWhiteSpace(post.Title) || !string.IsNullOrWhiteSpace(post.Description))
{
<section class="mb-1">
@if (!string.IsNullOrWhiteSpace(post.Title))
{
<h2 class="text-lg font-bold">
<a asp-page="/Posts/Details"
asp-route-slug="@(post.Slug ?? post.Id.ToString())">@post.Title</a>
</h2>
}
@if (!string.IsNullOrWhiteSpace(post.Description))
{
<p>@post.Description</p>
}
</section>
}
<p>@post.Content</p>
</div>
@if (post.Attachments.Any())
{
<div class="flex flex-col gap-4">
@foreach (var attachment in post.Attachments)
{
<div>
@if (attachment.MimeType!.StartsWith("image"))
{
<figure>
<img src="/drive/files/@attachment.Id" alt="@attachment.Name"
class="max-w-full h-auto rounded-lg"/>
</figure>
}
else if (attachment.MimeType!.StartsWith("video"))
{
<video controls src="/drive/files/@attachment.Id"
class="max-w-full h-auto rounded-lg"></video>
}
else if (attachment.MimeType!.StartsWith("audio"))
{
<audio controls src="/drive/files/@attachment.Id"
class="max-w-full h-auto rounded-lg"></audio>
}
else
{
<a href="/drive/files/@attachment.Id" target="_blank"
class="text-blue-500 hover:underline">@attachment.Name</a>
}
</div>
}
</div>
}
@if (post.Categories.Any() || post.Tags.Any())
{
<div>
@foreach (var category in post.Categories)
{
<span class="badge badge-primary">
<span class="mdi mdi-shape"></span>
@(!string.IsNullOrEmpty(category.Name) ? category.Name : category.Slug)
</span>
}
@foreach (var tag in post.Tags)
{
<span class="badge badge-info">
<span class="mdi mdi-tag"></span>
@(!string.IsNullOrEmpty(tag.Name) ? tag.Name : tag.Slug)
</span>
}
</div>
}
<div class="card-actions justify-end items-center">
<div class="text-sm text-base-content/60">
Posted on @post.CreatedAt.ToDateTimeOffset().ToString("yyyy-MM-dd")
</div>
<a asp-page="/Posts/Details" asp-route-slug="@(post.Slug ?? post.Id.ToString())"
class="btn btn-sm btn-ghost btn-circle">
<span class="mdi mdi-arrow-right text-lg"></span>
</a>
</div>
</div>
</div>
@@ -40,23 +116,54 @@
@if (Model.TotalPages > 1)
{
<div class="flex justify-center mt-8">
<div class="btn-group">
@for (var i = 1; i <= Model.TotalPages; i++)
<div class="join">
@for (var idx = 1; idx <= Model.TotalPages; idx++)
{
<a href="/posts?currentPage=@i" class="btn @(i == Model.CurrentPage ? "btn-active" : "")">@i</a>
var pageIdx = idx;
<a asp-page="/Posts"
asp-route-currentPage="@pageIdx"
class="join-item btn @(pageIdx == Model.CurrentPage ? "btn-active" : "")">
@pageIdx
</a>
}
</div>
</div>
}
</div>
<div class="w-full md:w-1/3">
<div class="card bg-base-100 border">
<div class="card-body">
<h3 class="card-title">Publisher Info</h3>
<p>This is where publisher information will be displayed.</p>
@if (Model.Publisher != null)
{
<div class="col-span-3 md:col-span-1">
<div class="card bg-base-200 shadow-md">
@if (Model.Publisher!.Background != null)
{
<figure class="relative">
<img
src="/drive/files/@Model.Publisher!.Background.Id"
alt="Background"/>
</figure>
}
<div class="card-body">
<div class="flex gap-4 card-title">
<div class="avatar">
<div class="w-16 rounded-full">
<img
src="@(Model.Publisher.Picture is null ? defaultAvatar : "/drive/files/" + Model.Publisher.Picture!.Id)"
alt="Avatar"/>
</div>
</div>
<h2 class="flex-1 flex flex-col">
@Model.Publisher.Nick
<span class="text-xs">@@@Model.Publisher.Name</span>
</h2>
</div>
@if (!string.IsNullOrWhiteSpace(Model.Publisher.Bio))
{
<p>@Model.Publisher.Bio</p>
}
</div>
</div>
</div>
</div>
}
</div>
</div>
</div>

View File

@@ -1,48 +1,44 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using Microsoft.AspNetCore.Mvc;
using DysonNetwork.Shared.Registry;
using DysonNetwork.Zone.Publication;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace DysonNetwork.Zone.Pages
namespace DysonNetwork.Zone.Pages;
public class PostsModel(PostService.PostServiceClient postClient, RemotePublisherService rps) : PageModel
{
public class PostsModel : PageModel
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 PageSize { get; set; } = 10;
public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);
public async Task OnGetAsync(int currentPage = 1)
{
private readonly PostService.PostServiceClient _postClient;
Site = HttpContext.Items[PublicationSiteMiddleware.SiteContextKey] as SnPublicationSite;
CurrentPage = currentPage;
Publisher = await rps.GetPublisher(id: Site!.PublisherId.ToString());
public PostsModel(PostService.PostServiceClient postClient)
var request = new ListPostsRequest
{
_postClient = postClient;
}
OrderBy = "date",
OrderDesc = true,
PageSize = PageSize,
PageToken = ((CurrentPage - 1) * PageSize).ToString(),
PublisherId = Site!.PublisherId.ToString()
};
public List<SnPost> Posts { get; set; } = new();
public int TotalCount { get; set; }
public int CurrentPage { get; set; }
public int PageSize { get; set; } = 10;
public int TotalPages => (int)System.Math.Ceiling(TotalCount / (double)PageSize);
var response = await postClient.ListPostsAsync(request);
public async Task OnGetAsync(int currentPage = 1)
if (response?.Posts != null)
{
CurrentPage = currentPage;
var request = new ListPostsRequest
{
OrderBy = "date",
OrderDesc = true,
PageSize = PageSize,
PageToken = ((CurrentPage - 1) * PageSize).ToString()
};
var response = await _postClient.ListPostsAsync(request);
if (response?.Posts != null)
{
Posts = response.Posts.Select(SnPost.FromProtoValue).ToList();
TotalCount = response.TotalSize;
}
Posts = response.Posts.Select(SnPost.FromProtoValue).ToList();
TotalCount = response.TotalSize;
}
}
}
}

View File

@@ -0,0 +1,70 @@
@page "/p/{slug}"
@model DysonNetwork.Zone.Pages.Posts.DetailsModel
@{
Layout = "_LayoutContained";
var defaultAvatar = "https://www.gravatar.com/avatar/00000000000000000000000000000000?d=mp";
}
<div class="container mx-auto px-8 pb-8">
@if (Model.Post != null)
{
<div class="card bg-base-100 border">
<div class="card-body">
<div class="flex items-center gap-4 mb-4">
<div class="avatar">
<div class="w-12 h-12 rounded-full">
<img src="@(Model.Post.Publisher?.Picture != null ? $"/files/{Model.Post.Publisher.Picture.Id}" : defaultAvatar)" />
</div>
</div>
<div>
<div class="font-bold">@Model.Post.Publisher?.Name</div>
<div class="text-sm text-base-content/60">@Model.Post.Publisher?.Nick</div>
</div>
</div>
<h1 class="text-3xl font-bold mb-4">@Model.Post.Title</h1>
@if (Model.Post.Attachments.Any())
{
<div class="mt-4">
@foreach (var attachment in Model.Post.Attachments)
{
if (attachment.MimeType != null && attachment.MimeType.StartsWith("image"))
{
<img src="/files/@attachment.Id" alt="@attachment.Name" class="max-w-full h-auto rounded-lg mb-4"/>
}
else
{
<a href="/files/@attachment.Id" target="_blank" class="text-blue-500 hover:underline">@attachment.Name</a>
}
}
</div>
}
<div class="prose max-w-none">
@Model.Post.Content
</div>
@if (Model.Post.Categories.Any())
{
<div class="mt-4">
@foreach (var category in Model.Post.Categories)
{
<span class="badge badge-outline">@(!string.IsNullOrEmpty(category.Name) ? category.Name : category.Slug)</span>
}
</div>
}
<div class="text-sm text-base-content/60 mt-8">
Posted on @Model.Post.CreatedAt.ToDateTimeOffset().ToString("yyyy-MM-dd")
</div>
</div>
</div>
}
else
{
<div class="text-center py-16">
<p class="text-lg">Post not found.</p>
</div>
}
</div>

View File

@@ -0,0 +1,37 @@
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Zone.Publication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace DysonNetwork.Zone.Pages.Posts;
public class DetailsModel(PostService.PostServiceClient postClient) : PageModel
{
[FromRoute] public string Slug { get; set; } = null!;
public SnPublicationSite? Site { get; set; }
public SnPost? Post { get; set; }
public async Task<IActionResult> OnGetAsync()
{
Site = HttpContext.Items[PublicationSiteMiddleware.SiteContextKey] as SnPublicationSite;
if (string.IsNullOrEmpty(Slug))
return NotFound();
var request = new GetPostRequest { PublisherId = Site!.PublisherId.ToString() };
if (Guid.TryParse(Slug, out var guid)) request.Id = guid.ToString();
else request.Slug = Slug;
var response = await postClient.GetPostAsync(request);
if (response == null)
{
return NotFound();
}
Post = SnPost.FromProtoValue(response);
return Page();
}
}