2024-05-28 17:09:50 +02:00
|
|
|
using Foxnouns.Backend.Database;
|
|
|
|
using Foxnouns.Backend.Database.Models;
|
2024-07-13 19:38:40 +02:00
|
|
|
using Foxnouns.Backend.Utils;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Newtonsoft.Json;
|
2024-05-28 17:09:50 +02:00
|
|
|
|
|
|
|
namespace Foxnouns.Backend.Services;
|
|
|
|
|
2024-07-12 17:12:24 +02:00
|
|
|
public class MemberRendererService(DatabaseContext db, Config config)
|
2024-05-28 17:09:50 +02:00
|
|
|
{
|
2024-07-13 19:38:40 +02:00
|
|
|
public async Task<IEnumerable<PartialMember>> RenderUserMembersAsync(User user, Token? token)
|
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
var canReadHiddenMembers =
|
|
|
|
token != null && token.UserId == user.Id && token.HasScope("member.read");
|
|
|
|
var renderUnlisted =
|
|
|
|
token != null && token.UserId == user.Id && token.HasScope("user.read_hidden");
|
2024-07-13 19:38:40 +02:00
|
|
|
var canReadMemberList = !user.ListHidden || canReadHiddenMembers;
|
|
|
|
|
|
|
|
IEnumerable<Member> members = canReadMemberList
|
2024-10-02 00:28:07 +02:00
|
|
|
? await db.Members.Where(m => m.UserId == user.Id).OrderBy(m => m.Name).ToListAsync()
|
2024-07-13 19:38:40 +02:00
|
|
|
: [];
|
2024-10-02 00:28:07 +02:00
|
|
|
if (!canReadHiddenMembers)
|
|
|
|
members = members.Where(m => !m.Unlisted);
|
2024-09-25 19:48:05 +02:00
|
|
|
return members.Select(m => RenderPartialMember(m, renderUnlisted));
|
2024-07-13 19:38:40 +02:00
|
|
|
}
|
|
|
|
|
2024-09-29 19:52:22 +02:00
|
|
|
public MemberResponse RenderMember(Member member, Token? token = null)
|
2024-07-13 19:38:40 +02:00
|
|
|
{
|
|
|
|
var renderUnlisted = token?.UserId == member.UserId && token.HasScope("user.read_hidden");
|
|
|
|
|
|
|
|
return new MemberResponse(
|
2024-10-02 00:28:07 +02:00
|
|
|
member.Id,
|
|
|
|
member.Sid,
|
|
|
|
member.Name,
|
2024-11-23 20:41:11 +01:00
|
|
|
member.DisplayName ?? member.Name,
|
2024-10-02 00:28:07 +02:00
|
|
|
member.Bio,
|
|
|
|
AvatarUrlFor(member),
|
|
|
|
member.Links,
|
|
|
|
member.Names,
|
|
|
|
member.Pronouns,
|
|
|
|
member.Fields,
|
2024-09-29 19:52:22 +02:00
|
|
|
member.ProfileFlags.Select(f => RenderPrideFlag(f.PrideFlag)),
|
2024-10-02 00:28:07 +02:00
|
|
|
RenderPartialUser(member.User),
|
|
|
|
renderUnlisted ? member.Unlisted : null
|
|
|
|
);
|
2024-07-13 19:38:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private UserRendererService.PartialUser RenderPartialUser(User user) =>
|
2024-10-02 00:28:07 +02:00
|
|
|
new(
|
|
|
|
user.Id,
|
|
|
|
user.Sid,
|
|
|
|
user.Username,
|
|
|
|
user.DisplayName,
|
|
|
|
AvatarUrlFor(user),
|
|
|
|
user.CustomPreferences
|
|
|
|
);
|
2024-07-13 19:38:40 +02:00
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public PartialMember RenderPartialMember(Member member, bool renderUnlisted = false) =>
|
|
|
|
new(
|
|
|
|
member.Id,
|
|
|
|
member.Sid,
|
|
|
|
member.Name,
|
2024-11-23 20:41:11 +01:00
|
|
|
member.DisplayName ?? member.Name,
|
2024-10-02 00:28:07 +02:00
|
|
|
member.Bio,
|
|
|
|
AvatarUrlFor(member),
|
|
|
|
member.Names,
|
|
|
|
member.Pronouns,
|
|
|
|
renderUnlisted ? member.Unlisted : null
|
|
|
|
);
|
2024-07-12 17:12:24 +02:00
|
|
|
|
|
|
|
private string? AvatarUrlFor(Member member) =>
|
2024-10-02 00:28:07 +02:00
|
|
|
member.Avatar != null
|
|
|
|
? $"{config.MediaBaseUrl}/members/{member.Id}/avatars/{member.Avatar}.webp"
|
|
|
|
: null;
|
2024-05-28 17:09:50 +02:00
|
|
|
|
2024-07-13 19:38:40 +02:00
|
|
|
private string? AvatarUrlFor(User user) =>
|
2024-10-02 00:28:07 +02:00
|
|
|
user.Avatar != null
|
|
|
|
? $"{config.MediaBaseUrl}/users/{user.Id}/avatars/{user.Avatar}.webp"
|
|
|
|
: null;
|
2024-07-13 19:38:40 +02:00
|
|
|
|
2024-09-26 20:15:04 +02:00
|
|
|
private string ImageUrlFor(PrideFlag flag) => $"{config.MediaBaseUrl}/flags/{flag.Hash}.webp";
|
|
|
|
|
2024-09-29 19:52:22 +02:00
|
|
|
private UserRendererService.PrideFlagResponse RenderPrideFlag(PrideFlag flag) =>
|
|
|
|
new(flag.Id, ImageUrlFor(flag), flag.Name, flag.Description);
|
|
|
|
|
2024-05-28 17:09:50 +02:00
|
|
|
public record PartialMember(
|
|
|
|
Snowflake Id,
|
2024-09-26 15:26:52 +02:00
|
|
|
string Sid,
|
2024-05-28 17:09:50 +02:00
|
|
|
string Name,
|
2024-11-23 20:41:11 +01:00
|
|
|
string DisplayName,
|
2024-05-28 17:09:50 +02:00
|
|
|
string? Bio,
|
2024-07-12 17:12:24 +02:00
|
|
|
string? AvatarUrl,
|
2024-05-28 17:09:50 +02:00
|
|
|
IEnumerable<FieldEntry> Names,
|
2024-09-25 19:48:05 +02:00
|
|
|
IEnumerable<Pronoun> Pronouns,
|
2024-10-02 00:28:07 +02:00
|
|
|
[property: JsonProperty(NullValueHandling = NullValueHandling.Ignore)] bool? Unlisted
|
|
|
|
);
|
2024-07-13 19:38:40 +02:00
|
|
|
|
|
|
|
public record MemberResponse(
|
|
|
|
Snowflake Id,
|
2024-09-26 15:26:52 +02:00
|
|
|
string Sid,
|
2024-07-13 19:38:40 +02:00
|
|
|
string Name,
|
2024-11-23 20:41:11 +01:00
|
|
|
string DisplayName,
|
2024-07-13 19:38:40 +02:00
|
|
|
string? Bio,
|
|
|
|
string? AvatarUrl,
|
|
|
|
string[] Links,
|
|
|
|
IEnumerable<FieldEntry> Names,
|
|
|
|
IEnumerable<Pronoun> Pronouns,
|
|
|
|
IEnumerable<Field> Fields,
|
2024-09-29 19:52:22 +02:00
|
|
|
IEnumerable<UserRendererService.PrideFlagResponse> Flags,
|
2024-07-13 19:38:40 +02:00
|
|
|
UserRendererService.PartialUser User,
|
2024-10-02 00:28:07 +02:00
|
|
|
[property: JsonProperty(NullValueHandling = NullValueHandling.Ignore)] bool? Unlisted
|
|
|
|
);
|
|
|
|
}
|