✨ Activity-based browsing
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Casbin;
|
||||
using DysonNetwork.Sphere.Account;
|
||||
using DysonNetwork.Sphere.Permission;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@ -9,16 +10,17 @@ namespace DysonNetwork.Sphere.Post;
|
||||
|
||||
[ApiController]
|
||||
[Route("/posts")]
|
||||
public class PostController(AppDatabase db, PostService ps) : ControllerBase
|
||||
public class PostController(AppDatabase db, PostService ps, RelationshipService rels) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<List<Post>>> ListPosts([FromQuery] int offset = 0, [FromQuery] int take = 20)
|
||||
{
|
||||
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
|
||||
var currentUser = currentUserValue as Account.Account;
|
||||
var userFriends = await rels.ListAccountFriends(currentUser!);
|
||||
|
||||
var totalCount = await db.Posts
|
||||
.FilterWithVisibility(currentUser, isListing: true)
|
||||
.FilterWithVisibility(currentUser, userFriends, isListing: true)
|
||||
.CountAsync();
|
||||
var posts = await db.Posts
|
||||
.Include(e => e.Publisher)
|
||||
@ -30,7 +32,7 @@ public class PostController(AppDatabase db, PostService ps) : ControllerBase
|
||||
.Include(e => e.Categories)
|
||||
.Include(e => e.Tags)
|
||||
.Where(e => e.RepliedPostId == null)
|
||||
.FilterWithVisibility(currentUser, isListing: true)
|
||||
.FilterWithVisibility(currentUser, userFriends, isListing: true)
|
||||
.OrderByDescending(e => e.PublishedAt ?? e.CreatedAt)
|
||||
.Skip(offset)
|
||||
.Take(take)
|
||||
@ -46,6 +48,7 @@ public class PostController(AppDatabase db, PostService ps) : ControllerBase
|
||||
{
|
||||
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
|
||||
var currentUser = currentUserValue as Account.Account;
|
||||
var userFriends = await rels.ListAccountFriends(currentUser!);
|
||||
|
||||
var post = await db.Posts
|
||||
.Where(e => e.Id == id)
|
||||
@ -58,7 +61,7 @@ public class PostController(AppDatabase db, PostService ps) : ControllerBase
|
||||
.Include(e => e.Tags)
|
||||
.Include(e => e.Categories)
|
||||
.Include(e => e.Attachments)
|
||||
.FilterWithVisibility(currentUser)
|
||||
.FilterWithVisibility(currentUser, userFriends)
|
||||
.FirstOrDefaultAsync();
|
||||
if (post is null) return NotFound();
|
||||
|
||||
@ -71,6 +74,7 @@ public class PostController(AppDatabase db, PostService ps) : ControllerBase
|
||||
{
|
||||
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
|
||||
var currentUser = currentUserValue as Account.Account;
|
||||
var userFriends = await rels.ListAccountFriends(currentUser!);
|
||||
|
||||
var post = await db.Posts
|
||||
.Where(e => e.Id == id)
|
||||
@ -79,7 +83,7 @@ public class PostController(AppDatabase db, PostService ps) : ControllerBase
|
||||
|
||||
var totalCount = await db.Posts
|
||||
.Where(e => e.RepliedPostId == post.Id)
|
||||
.FilterWithVisibility(currentUser, isListing: true)
|
||||
.FilterWithVisibility(currentUser, userFriends, isListing: true)
|
||||
.CountAsync();
|
||||
var posts = await db.Posts
|
||||
.Where(e => e.RepliedPostId == id)
|
||||
@ -91,7 +95,7 @@ public class PostController(AppDatabase db, PostService ps) : ControllerBase
|
||||
.Include(e => e.Attachments)
|
||||
.Include(e => e.Categories)
|
||||
.Include(e => e.Tags)
|
||||
.FilterWithVisibility(currentUser, isListing: true)
|
||||
.FilterWithVisibility(currentUser, userFriends, isListing: true)
|
||||
.OrderByDescending(e => e.PublishedAt ?? e.CreatedAt)
|
||||
.Skip(offset)
|
||||
.Take(take)
|
||||
@ -179,6 +183,7 @@ public class PostController(AppDatabase db, PostService ps) : ControllerBase
|
||||
try
|
||||
{
|
||||
post = await ps.PostAsync(
|
||||
currentUser,
|
||||
post,
|
||||
attachments: request.Attachments,
|
||||
tags: request.Tags,
|
||||
|
@ -1,12 +1,14 @@
|
||||
using DysonNetwork.Sphere.Activity;
|
||||
using DysonNetwork.Sphere.Storage;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NodaTime;
|
||||
|
||||
namespace DysonNetwork.Sphere.Post;
|
||||
|
||||
public class PostService(AppDatabase db, FileService fs)
|
||||
public class PostService(AppDatabase db, FileService fs, ActivityService act)
|
||||
{
|
||||
public async Task<Post> PostAsync(
|
||||
Account.Account user,
|
||||
Post post,
|
||||
List<string>? attachments = null,
|
||||
List<string>? tags = null,
|
||||
@ -66,6 +68,8 @@ public class PostService(AppDatabase db, FileService fs)
|
||||
await db.SaveChangesAsync();
|
||||
await fs.MarkUsageRangeAsync(post.Attachments, 1);
|
||||
|
||||
await act.CreateNewPostActivity(user, post);
|
||||
|
||||
return post;
|
||||
}
|
||||
|
||||
@ -153,7 +157,7 @@ public class PostService(AppDatabase db, FileService fs)
|
||||
public static class PostQueryExtensions
|
||||
{
|
||||
public static IQueryable<Post> FilterWithVisibility(this IQueryable<Post> source, Account.Account? currentUser,
|
||||
bool isListing = false)
|
||||
List<long> userFriends, bool isListing = false)
|
||||
{
|
||||
var now = Instant.FromDateTimeUtc(DateTime.UtcNow);
|
||||
|
||||
@ -172,6 +176,9 @@ public static class PostQueryExtensions
|
||||
|
||||
return source
|
||||
.Where(e => e.PublishedAt != null && now >= e.PublishedAt && e.Publisher.AccountId == currentUser.Id)
|
||||
.Where(e => e.Visibility != PostVisibility.Private || e.Publisher.AccountId == currentUser.Id);
|
||||
.Where(e => e.Visibility != PostVisibility.Private || e.Publisher.AccountId == currentUser.Id)
|
||||
.Where(e => e.Visibility != PostVisibility.Friends ||
|
||||
(e.Publisher.AccountId != null && userFriends.Contains(e.Publisher.AccountId.Value)) ||
|
||||
e.Publisher.AccountId == currentUser.Id);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user