2024-12-09 21:11:46 +01:00
|
|
|
// Copyright (C) 2023-present sam/u1f320 (vulpine.solutions)
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2024-05-28 17:09:50 +02:00
|
|
|
using Foxnouns.Backend.Database;
|
|
|
|
using Foxnouns.Backend.Database.Models;
|
2024-12-08 20:17:30 +01:00
|
|
|
using Foxnouns.Backend.Dto;
|
2024-07-13 19:38:40 +02:00
|
|
|
using Foxnouns.Backend.Utils;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
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-12-08 15:07:25 +01:00
|
|
|
bool canReadHiddenMembers =
|
2024-10-02 00:28:07 +02:00
|
|
|
token != null && token.UserId == user.Id && token.HasScope("member.read");
|
2024-12-08 15:07:25 +01:00
|
|
|
bool renderUnlisted =
|
2024-10-02 00:28:07 +02:00
|
|
|
token != null && token.UserId == user.Id && token.HasScope("user.read_hidden");
|
2024-12-08 15:07:25 +01:00
|
|
|
bool canReadMemberList = !user.ListHidden || canReadHiddenMembers;
|
2024-07-13 19:38:40 +02:00
|
|
|
|
|
|
|
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-11-25 17:35:24 +01:00
|
|
|
public MemberResponse RenderMember(
|
|
|
|
Member member,
|
|
|
|
Token? token = null,
|
|
|
|
string? overrideSid = null
|
|
|
|
)
|
2024-07-13 19:38:40 +02:00
|
|
|
{
|
2024-12-08 15:07:25 +01:00
|
|
|
bool renderUnlisted = token?.UserId == member.UserId && token.HasScope("user.read_hidden");
|
2024-07-13 19:38:40 +02:00
|
|
|
|
|
|
|
return new MemberResponse(
|
2024-10-02 00:28:07 +02:00
|
|
|
member.Id,
|
2024-11-25 17:35:24 +01:00
|
|
|
overrideSid ?? member.Sid,
|
2024-10-02 00:28:07 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-12-08 20:17:30 +01:00
|
|
|
private 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
|
|
|
|
2024-12-02 18:06:19 +01:00
|
|
|
public 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-12-09 14:52:31 +01:00
|
|
|
private string? ImageUrlFor(PrideFlag flag) =>
|
|
|
|
flag.Hash != null ? $"{config.MediaBaseUrl}/flags/{flag.Hash}.webp" : null;
|
2024-09-26 20:15:04 +02:00
|
|
|
|
2024-12-08 20:17:30 +01:00
|
|
|
private PrideFlagResponse RenderPrideFlag(PrideFlag flag) =>
|
2024-09-29 19:52:22 +02:00
|
|
|
new(flag.Id, ImageUrlFor(flag), flag.Name, flag.Description);
|
2024-10-02 00:28:07 +02:00
|
|
|
}
|