♻️ I have no idea what am I doing. Might be mixing stuff

This commit is contained in:
2025-07-14 19:55:28 +08:00
parent ef9175d27d
commit cbfdb4aa60
232 changed files with 990 additions and 115807 deletions

View File

@@ -1,8 +1,8 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
using DysonNetwork.Shared.Data;
using DysonNetwork.Sphere.Post;
using DysonNetwork.Sphere.Storage;
using Microsoft.EntityFrameworkCore;
using NodaTime;
@@ -30,7 +30,7 @@ public class Publisher : ModelBase, IIdentifiedResource
[Column(TypeName = "jsonb")] public CloudFileReferenceObject? Picture { get; set; }
[Column(TypeName = "jsonb")] public CloudFileReferenceObject? Background { get; set; }
[Column(TypeName = "jsonb")] public Account.VerificationMark? Verification { get; set; }
[Column(TypeName = "jsonb")] public VerificationMark? Verification { get; set; }
[JsonIgnore] public ICollection<Post.Post> Posts { get; set; } = new List<Post.Post>();
[JsonIgnore] public ICollection<PostCollection> Collections { get; set; } = new List<PostCollection>();
@@ -41,11 +41,10 @@ public class Publisher : ModelBase, IIdentifiedResource
public ICollection<PublisherSubscription> Subscriptions { get; set; } = new List<PublisherSubscription>();
public Guid? AccountId { get; set; }
public Account.Account? Account { get; set; }
public Guid? RealmId { get; set; }
[JsonIgnore] public Realm.Realm? Realm { get; set; }
public string ResourceIdentifier => $"publisher/{Id}";
public string ResourceIdentifier => $"publisher:{Id}";
}
public enum PublisherMemberRole
@@ -61,7 +60,6 @@ public class PublisherMember : ModelBase
public Guid PublisherId { get; set; }
[JsonIgnore] public Publisher Publisher { get; set; } = null!;
public Guid AccountId { get; set; }
public Account.Account Account { get; set; } = null!;
public PublisherMemberRole Role { get; set; } = PublisherMemberRole.Viewer;
public Instant? JoinedAt { get; set; }
@@ -81,7 +79,6 @@ public class PublisherSubscription : ModelBase
public Guid PublisherId { get; set; }
[JsonIgnore] public Publisher Publisher { get; set; } = null!;
public Guid AccountId { get; set; }
[JsonIgnore] public Account.Account Account { get; set; } = null!;
public PublisherSubscriptionStatus Status { get; set; } = PublisherSubscriptionStatus.Active;
public int Tier { get; set; } = 0;

View File

@@ -49,7 +49,7 @@ public class PublisherController(
[Authorize]
public async Task<ActionResult<List<Publisher>>> ListManagedPublishers()
{
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var userId = currentUser.Id;
var members = await db.PublisherMembers
@@ -65,7 +65,7 @@ public class PublisherController(
[Authorize]
public async Task<ActionResult<List<PublisherMember>>> ListInvites()
{
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var userId = currentUser.Id;
var members = await db.PublisherMembers
@@ -88,7 +88,7 @@ public class PublisherController(
public async Task<ActionResult<PublisherMember>> InviteMember(string name,
[FromBody] PublisherMemberRequest request)
{
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var userId = currentUser.Id;
var relatedUser = await db.Accounts.FindAsync(request.RelatedUserId);
@@ -128,7 +128,7 @@ public class PublisherController(
[Authorize]
public async Task<ActionResult<Publisher>> AcceptMemberInvite(string name)
{
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var userId = currentUser.Id;
var member = await db.PublisherMembers
@@ -154,7 +154,7 @@ public class PublisherController(
[Authorize]
public async Task<ActionResult> DeclineMemberInvite(string name)
{
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var userId = currentUser.Id;
var member = await db.PublisherMembers
@@ -179,7 +179,7 @@ public class PublisherController(
[Authorize]
public async Task<ActionResult> RemoveMember(string name, Guid memberId)
{
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var publisher = await db.Publishers
.Where(p => p.Name == name)
@@ -224,7 +224,7 @@ public class PublisherController(
[RequiredPermission("global", "publishers.create")]
public async Task<ActionResult<Publisher>> CreatePublisherIndividual([FromBody] PublisherRequest request)
{
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var takenName = request.Name ?? currentUser.Name;
var duplicateNameCount = await db.Publishers
@@ -274,7 +274,7 @@ public class PublisherController(
public async Task<ActionResult<Publisher>> CreatePublisherOrganization(string realmSlug,
[FromBody] PublisherRequest request)
{
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var realm = await db.Realms.FirstOrDefaultAsync(r => r.Slug == realmSlug);
if (realm == null) return NotFound("Realm not found");
@@ -328,7 +328,7 @@ public class PublisherController(
[Authorize]
public async Task<ActionResult<Publisher>> UpdatePublisher(string name, PublisherRequest request)
{
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var userId = currentUser.Id;
var publisher = await db.Publishers
@@ -405,7 +405,7 @@ public class PublisherController(
[Authorize]
public async Task<ActionResult<Publisher>> DeletePublisher(string name)
{
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var userId = currentUser.Id;
var publisher = await db.Publishers
@@ -473,7 +473,7 @@ public class PublisherController(
[Authorize]
public async Task<ActionResult<PublisherMember>> GetCurrentIdentity(string name)
{
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var userId = currentUser.Id;
var publisher = await db.Publishers

View File

@@ -1,12 +1,17 @@
using DysonNetwork.Shared.Cache;
using DysonNetwork.Shared.Data;
using DysonNetwork.Shared.Proto;
using DysonNetwork.Sphere.Post;
using DysonNetwork.Sphere.Storage;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using NodaTime;
namespace DysonNetwork.Sphere.Publisher;
public class PublisherService(AppDatabase db, FileReferenceService fileRefService, ICacheService cache)
public class PublisherService(
AppDatabase db,
FileReferenceService.FileReferenceServiceClient fileRefs,
ICacheService cache
)
{
public async Task<Publisher?> GetPublisherByName(string name)
{
@@ -20,12 +25,12 @@ public class PublisherService(AppDatabase db, FileReferenceService fileRefServic
public async Task<List<Publisher>> GetUserPublishers(Guid userId)
{
var cacheKey = string.Format(UserPublishersCacheKey, userId);
// Try to get publishers from the cache first
var publishers = await cache.GetAsync<List<Publisher>>(cacheKey);
if (publishers is not null)
return publishers;
// If not in cache, fetch from a database
var publishersId = await db.PublisherMembers
.Where(p => p.AccountId == userId)
@@ -34,10 +39,10 @@ public class PublisherService(AppDatabase db, FileReferenceService fileRefServic
publishers = await db.Publishers
.Where(p => publishersId.Contains(p.Id))
.ToListAsync();
// Store in a cache for 5 minutes
await cache.SetAsync(cacheKey, publishers, TimeSpan.FromMinutes(5));
return publishers;
}
@@ -92,11 +97,10 @@ public class PublisherService(AppDatabase db, FileReferenceService fileRefServic
return result;
}
public const string SubscribedPublishersCacheKey = "accounts:{0}:subscribed-publishers";
public async Task<List<Publisher>> GetSubscribedPublishers(Guid userId)
{
var cacheKey = string.Format(SubscribedPublishersCacheKey, userId);
@@ -127,32 +131,30 @@ public class PublisherService(AppDatabase db, FileReferenceService fileRefServic
public async Task<List<PublisherMember>> GetPublisherMembers(Guid publisherId)
{
var cacheKey = string.Format(PublisherMembersCacheKey, publisherId);
// Try to get members from the cache first
var members = await cache.GetAsync<List<PublisherMember>>(cacheKey);
if (members is not null)
return members;
// If not in cache, fetch from a database
members = await db.PublisherMembers
.Where(p => p.PublisherId == publisherId)
.Include(p => p.Account)
.ThenInclude(p => p.Profile)
.ToListAsync();
// Store in cache for 5 minutes (consistent with other cache durations in the class)
await cache.SetAsync(cacheKey, members, TimeSpan.FromMinutes(5));
return members;
}
public async Task<Publisher> CreateIndividualPublisher(
Account.Account account,
Account account,
string? name,
string? nick,
string? bio,
CloudFile? picture,
CloudFile? background
CloudFileReferenceObject? picture,
CloudFileReferenceObject? background
)
{
var publisher = new Publisher
@@ -161,14 +163,14 @@ public class PublisherService(AppDatabase db, FileReferenceService fileRefServic
Name = name ?? account.Name,
Nick = nick ?? account.Nick,
Bio = bio ?? account.Profile.Bio,
Picture = picture?.ToReferenceObject() ?? account.Profile.Picture,
Background = background?.ToReferenceObject() ?? account.Profile.Background,
AccountId = account.Id,
Picture = picture ?? CloudFileReferenceObject.FromProtoValue(account.Profile.Picture),
Background = background ?? CloudFileReferenceObject.FromProtoValue(account.Profile.Background),
AccountId = Guid.Parse(account.Id),
Members = new List<PublisherMember>
{
new()
{
AccountId = account.Id,
AccountId = Guid.Parse(account.Id),
Role = PublisherMemberRole.Owner,
JoinedAt = Instant.FromDateTimeUtc(DateTime.UtcNow)
}
@@ -178,21 +180,26 @@ public class PublisherService(AppDatabase db, FileReferenceService fileRefServic
db.Publishers.Add(publisher);
await db.SaveChangesAsync();
var publisherResourceId = $"publisher:{publisher.Id}";
if (publisher.Picture is not null) {
await fileRefService.CreateReferenceAsync(
publisher.Picture.Id,
"publisher.picture",
publisherResourceId
if (publisher.Picture is not null)
{
await fileRefs.CreateReferenceAsync(
new CreateReferenceRequest
{
FileId = publisher.Picture.Id,
Usage = "publisher.picture",
ResourceId = publisher.ResourceIdentifier,
}
);
}
if (publisher.Background is not null) {
await fileRefService.CreateReferenceAsync(
publisher.Background.Id,
"publisher.background",
publisherResourceId
if (publisher.Background is not null)
{
await fileRefs.CreateReferenceAsync(
new CreateReferenceRequest
{
FileId = publisher.Background.Id,
Usage = "publisher.background",
ResourceId = publisher.ResourceIdentifier,
}
);
}
@@ -201,12 +208,12 @@ public class PublisherService(AppDatabase db, FileReferenceService fileRefServic
public async Task<Publisher> CreateOrganizationPublisher(
Realm.Realm realm,
Account.Account account,
Account account,
string? name,
string? nick,
string? bio,
CloudFile? picture,
CloudFile? background
CloudFileReferenceObject? picture,
CloudFileReferenceObject? background
)
{
var publisher = new Publisher
@@ -215,14 +222,14 @@ public class PublisherService(AppDatabase db, FileReferenceService fileRefServic
Name = name ?? realm.Slug,
Nick = nick ?? realm.Name,
Bio = bio ?? realm.Description,
Picture = picture?.ToReferenceObject() ?? realm.Picture,
Background = background?.ToReferenceObject() ?? realm.Background,
Picture = picture ?? CloudFileReferenceObject.FromProtoValue(account.Profile.Picture),
Background = background ?? CloudFileReferenceObject.FromProtoValue(account.Profile.Background),
RealmId = realm.Id,
Members = new List<PublisherMember>
{
new()
{
AccountId = account.Id,
AccountId = Guid.Parse(account.Id),
Role = PublisherMemberRole.Owner,
JoinedAt = Instant.FromDateTimeUtc(DateTime.UtcNow)
}
@@ -232,21 +239,26 @@ public class PublisherService(AppDatabase db, FileReferenceService fileRefServic
db.Publishers.Add(publisher);
await db.SaveChangesAsync();
var publisherResourceId = $"publisher:{publisher.Id}";
if (publisher.Picture is not null) {
await fileRefService.CreateReferenceAsync(
publisher.Picture.Id,
"publisher.picture",
publisherResourceId
if (publisher.Picture is not null)
{
await fileRefs.CreateReferenceAsync(
new CreateReferenceRequest
{
FileId = publisher.Picture.Id,
Usage = "publisher.picture",
ResourceId = publisher.ResourceIdentifier,
}
);
}
if (publisher.Background is not null) {
await fileRefService.CreateReferenceAsync(
publisher.Background.Id,
"publisher.background",
publisherResourceId
if (publisher.Background is not null)
{
await fileRefs.CreateReferenceAsync(
new CreateReferenceRequest
{
FileId = publisher.Background.Id,
Usage = "publisher.background",
ResourceId = publisher.ResourceIdentifier,
}
);
}

View File

@@ -30,7 +30,7 @@ public class PublisherSubscriptionController(
[Authorize]
public async Task<ActionResult<SubscriptionStatusResponse>> CheckSubscriptionStatus(string name)
{
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
// Check if the publisher exists
var publisher = await db.Publishers.FirstOrDefaultAsync(p => p.Name == name);
@@ -53,7 +53,7 @@ public class PublisherSubscriptionController(
string name,
[FromBody] SubscribeRequest request)
{
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
// Check if the publisher exists
var publisher = await db.Publishers.FirstOrDefaultAsync(p => p.Name == name);
@@ -81,7 +81,7 @@ public class PublisherSubscriptionController(
[Authorize]
public async Task<ActionResult> Unsubscribe(string name)
{
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
// Check if the publisher exists
var publisher = await db.Publishers.FirstOrDefaultAsync(e => e.Name == name);
@@ -104,7 +104,7 @@ public class PublisherSubscriptionController(
[Authorize]
public async Task<ActionResult<List<PublisherSubscription>>> GetCurrentSubscriptions()
{
if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized();
if (HttpContext.Items["CurrentUser"] is not Account currentUser) return Unauthorized();
var subscriptions = await subs.GetAccountSubscriptionsAsync(currentUser.Id);
return subscriptions;