✨ Activity-based browsing
This commit is contained in:
21
DysonNetwork.Sphere/Activity/Activity.cs
Normal file
21
DysonNetwork.Sphere/Activity/Activity.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DysonNetwork.Sphere.Activity;
|
||||
|
||||
public enum ActivityVisibility
|
||||
{
|
||||
Public,
|
||||
Friends,
|
||||
}
|
||||
|
||||
public class Activity : ModelBase
|
||||
{
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
[MaxLength(1024)] public string Type { get; set; } = null!;
|
||||
[MaxLength(4096)] public string ResourceIdentifier { get; set; } = null!;
|
||||
public ActivityVisibility Visibility { get; set; } = ActivityVisibility.Public;
|
||||
public Dictionary<string, object> Meta = new();
|
||||
|
||||
public long AccountId { get; set; }
|
||||
public Account.Account Account { get; set; } = null!;
|
||||
}
|
33
DysonNetwork.Sphere/Activity/ActivityController.cs
Normal file
33
DysonNetwork.Sphere/Activity/ActivityController.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using DysonNetwork.Sphere.Account;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DysonNetwork.Sphere.Activity;
|
||||
|
||||
[ApiController]
|
||||
[Route("/activities")]
|
||||
public class ActivityController(AppDatabase db, ActivityService act, RelationshipService rels) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<List<Activity>>> ListActivities([FromQuery] int offset, [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.Activities
|
||||
.FilterWithVisibility(currentUser, userFriends)
|
||||
.CountAsync();
|
||||
var posts = await db.Activities
|
||||
.Include(e => e.Account)
|
||||
.FilterWithVisibility(currentUser, userFriends)
|
||||
.OrderByDescending(e => e.CreatedAt)
|
||||
.Skip(offset)
|
||||
.Take(take)
|
||||
.ToListAsync();
|
||||
|
||||
Response.Headers["X-Total"] = totalCount.ToString();
|
||||
|
||||
return Ok(posts);
|
||||
}
|
||||
}
|
54
DysonNetwork.Sphere/Activity/ActivityService.cs
Normal file
54
DysonNetwork.Sphere/Activity/ActivityService.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using DysonNetwork.Sphere.Post;
|
||||
using NodaTime;
|
||||
|
||||
namespace DysonNetwork.Sphere.Activity;
|
||||
|
||||
public class ActivityService(AppDatabase db)
|
||||
{
|
||||
public async Task<Activity> CreateActivity(
|
||||
Account.Account user,
|
||||
string type,
|
||||
string identifier,
|
||||
ActivityVisibility visibility = ActivityVisibility.Public
|
||||
)
|
||||
{
|
||||
var activity = new Activity
|
||||
{
|
||||
Type = type,
|
||||
ResourceIdentifier = identifier,
|
||||
Visibility = visibility,
|
||||
AccountId = user.Id,
|
||||
};
|
||||
|
||||
db.Activities.Add(activity);
|
||||
await db.SaveChangesAsync();
|
||||
|
||||
return activity;
|
||||
}
|
||||
|
||||
public async Task CreateNewPostActivity(Account.Account user, Post.Post post)
|
||||
{
|
||||
if (post.Visibility is PostVisibility.Unlisted or PostVisibility.Private) return;
|
||||
|
||||
var identifier = $"posts/{post.Id}";
|
||||
await CreateActivity(user, "posts.new", identifier,
|
||||
post.Visibility == PostVisibility.Friends ? ActivityVisibility.Friends : ActivityVisibility.Public);
|
||||
}
|
||||
}
|
||||
|
||||
public static class ActivityQueryExtensions
|
||||
{
|
||||
public static IQueryable<Activity> FilterWithVisibility(this IQueryable<Activity> source,
|
||||
Account.Account? currentUser, List<long> userFriends)
|
||||
{
|
||||
var now = Instant.FromDateTimeUtc(DateTime.UtcNow);
|
||||
|
||||
if (currentUser is null)
|
||||
return source.Where(e => e.Visibility == ActivityVisibility.Public);
|
||||
|
||||
return source
|
||||
.Where(e => e.Visibility != ActivityVisibility.Friends ||
|
||||
userFriends.Contains(e.AccountId) ||
|
||||
e.AccountId == currentUser.Id);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user