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-12 17:12:24 +02:00
|
|
|
using Foxnouns.Backend.Utils;
|
2024-05-28 17:09:50 +02:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
namespace Foxnouns.Backend.Services;
|
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
public class UserRendererService(
|
|
|
|
DatabaseContext db,
|
|
|
|
MemberRendererService memberRenderer,
|
|
|
|
Config config
|
|
|
|
)
|
2024-05-28 17:09:50 +02:00
|
|
|
{
|
2024-10-02 00:28:07 +02:00
|
|
|
public async Task<UserResponse> RenderUserAsync(
|
|
|
|
User user,
|
|
|
|
User? selfUser = null,
|
2024-07-12 17:12:24 +02:00
|
|
|
Token? token = null,
|
|
|
|
bool renderMembers = true,
|
2024-09-05 21:10:45 +02:00
|
|
|
bool renderAuthMethods = false,
|
2024-11-25 17:35:24 +01:00
|
|
|
string? overrideSid = null,
|
2024-10-02 00:28:07 +02:00
|
|
|
CancellationToken ct = default
|
2024-12-02 18:06:19 +01:00
|
|
|
) =>
|
|
|
|
await RenderUserInnerAsync(
|
|
|
|
user,
|
|
|
|
selfUser != null && selfUser.Id == user.Id,
|
|
|
|
token?.Scopes ?? [],
|
|
|
|
renderMembers,
|
|
|
|
renderAuthMethods,
|
|
|
|
overrideSid,
|
|
|
|
ct
|
|
|
|
);
|
|
|
|
|
|
|
|
public async Task<UserResponse> RenderUserInnerAsync(
|
|
|
|
User user,
|
|
|
|
bool isSelfUser,
|
|
|
|
string[] scopes,
|
|
|
|
bool renderMembers = true,
|
|
|
|
bool renderAuthMethods = false,
|
|
|
|
string? overrideSid = null,
|
|
|
|
CancellationToken ct = default
|
2024-10-02 00:28:07 +02:00
|
|
|
)
|
2024-05-28 17:09:50 +02:00
|
|
|
{
|
2024-12-02 18:06:19 +01:00
|
|
|
scopes = scopes.ExpandScopes();
|
2024-12-08 15:07:25 +01:00
|
|
|
bool tokenCanReadHiddenMembers = scopes.Contains("member.read") && isSelfUser;
|
|
|
|
bool tokenHidden = scopes.Contains("user.read_hidden") && isSelfUser;
|
|
|
|
bool tokenPrivileged = scopes.Contains("user.read_privileged") && isSelfUser;
|
2024-05-28 17:09:50 +02:00
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
renderMembers = renderMembers && (!user.ListHidden || tokenCanReadHiddenMembers);
|
2024-07-13 03:09:00 +02:00
|
|
|
renderAuthMethods = renderAuthMethods && tokenPrivileged;
|
2024-07-12 17:12:24 +02:00
|
|
|
|
2024-10-02 00:28:07 +02:00
|
|
|
IEnumerable<Member> members = renderMembers
|
|
|
|
? await db.Members.Where(m => m.UserId == user.Id).OrderBy(m => m.Name).ToListAsync(ct)
|
|
|
|
: [];
|
2024-07-12 17:12:24 +02:00
|
|
|
// Unless the user is requesting their own members AND the token can read hidden members, we filter out unlisted members.
|
2024-10-02 00:28:07 +02:00
|
|
|
if (!(isSelfUser && tokenCanReadHiddenMembers))
|
|
|
|
members = members.Where(m => !m.Unlisted);
|
2024-07-12 17:12:24 +02:00
|
|
|
|
2024-12-08 15:07:25 +01:00
|
|
|
List<UserFlag> flags = await db
|
2024-10-02 00:28:07 +02:00
|
|
|
.UserFlags.Where(f => f.UserId == user.Id)
|
|
|
|
.OrderBy(f => f.Id)
|
|
|
|
.ToListAsync(ct);
|
2024-09-27 14:48:09 +02:00
|
|
|
|
2024-12-08 15:07:25 +01:00
|
|
|
List<AuthMethod> authMethods = renderAuthMethods
|
2024-10-02 00:28:07 +02:00
|
|
|
? await db
|
|
|
|
.AuthMethods.Where(a => a.UserId == user.Id)
|
2024-07-12 17:12:24 +02:00
|
|
|
.Include(a => a.FediverseApplication)
|
2024-09-05 21:10:45 +02:00
|
|
|
.ToListAsync(ct)
|
2024-07-12 17:12:24 +02:00
|
|
|
: [];
|
2024-05-28 17:09:50 +02:00
|
|
|
|
2024-11-24 22:19:53 +01:00
|
|
|
int? utcOffset = null;
|
|
|
|
if (
|
|
|
|
user.Timezone != null
|
2024-12-08 15:07:25 +01:00
|
|
|
&& TimeZoneInfo.TryFindSystemTimeZoneById(user.Timezone, out TimeZoneInfo? tz)
|
2024-11-24 22:19:53 +01:00
|
|
|
)
|
2024-12-08 15:17:18 +01:00
|
|
|
{
|
2024-11-24 22:19:53 +01:00
|
|
|
utcOffset = (int)tz.GetUtcOffset(DateTimeOffset.UtcNow).TotalSeconds;
|
2024-12-08 15:17:18 +01:00
|
|
|
}
|
2024-11-24 22:19:53 +01:00
|
|
|
|
2024-05-28 17:09:50 +02:00
|
|
|
return new UserResponse(
|
2024-10-02 00:28:07 +02:00
|
|
|
user.Id,
|
2024-11-25 17:35:24 +01:00
|
|
|
overrideSid ?? user.Sid,
|
2024-10-02 00:28:07 +02:00
|
|
|
user.Username,
|
|
|
|
user.DisplayName,
|
|
|
|
user.Bio,
|
|
|
|
user.MemberTitle,
|
|
|
|
AvatarUrlFor(user),
|
2024-09-26 17:09:27 +02:00
|
|
|
user.Links,
|
2024-10-02 00:28:07 +02:00
|
|
|
user.Names,
|
|
|
|
user.Pronouns,
|
|
|
|
user.Fields,
|
2024-12-25 17:19:50 +01:00
|
|
|
user.CustomPreferences.Select(x => (x.Key, RenderCustomPreference(x.Value)))
|
|
|
|
.ToDictionary(),
|
2024-09-27 14:48:09 +02:00
|
|
|
flags.Select(f => RenderPrideFlag(f.PrideFlag)),
|
2024-11-24 22:19:53 +01:00
|
|
|
utcOffset,
|
2024-10-01 16:06:02 +02:00
|
|
|
user.Role,
|
2024-10-02 00:28:07 +02:00
|
|
|
renderMembers
|
|
|
|
? members.Select(m => memberRenderer.RenderPartialMember(m, tokenHidden))
|
|
|
|
: null,
|
2024-11-04 22:04:04 +01:00
|
|
|
renderAuthMethods ? authMethods.Select(RenderAuthMethod) : null,
|
2024-07-13 19:38:40 +02:00
|
|
|
tokenHidden ? user.ListHidden : null,
|
2024-09-26 17:09:27 +02:00
|
|
|
tokenHidden ? user.LastActive : null,
|
2024-11-24 22:19:53 +01:00
|
|
|
tokenHidden ? user.LastSidReroll : null,
|
2024-12-17 17:52:32 +01:00
|
|
|
tokenHidden ? user.Timezone ?? "<none>" : null,
|
2024-12-17 18:08:43 +01:00
|
|
|
tokenHidden ? user is { Deleted: true, DeletedBy: not null } : null,
|
2024-12-17 17:52:32 +01:00
|
|
|
tokenHidden ? user.Deleted : null
|
2024-07-12 17:12:24 +02:00
|
|
|
);
|
2024-05-28 17:09:50 +02:00
|
|
|
}
|
|
|
|
|
2024-11-04 22:04:04 +01:00
|
|
|
public static AuthMethodResponse RenderAuthMethod(AuthMethod a) =>
|
|
|
|
new(
|
|
|
|
a.Id,
|
|
|
|
a.AuthType,
|
|
|
|
a.RemoteId,
|
|
|
|
a.FediverseApplication != null
|
|
|
|
? $"@{a.RemoteUsername}@{a.FediverseApplication.Domain}"
|
|
|
|
: a.RemoteUsername
|
|
|
|
);
|
|
|
|
|
2024-12-25 17:19:50 +01:00
|
|
|
public static CustomPreferenceResponse RenderCustomPreference(User.CustomPreference pref) =>
|
|
|
|
new(pref.Icon, pref.Tooltip, pref.Muted, pref.Favourite, pref.Size);
|
|
|
|
|
|
|
|
public static Dictionary<Snowflake, CustomPreferenceResponse> RenderCustomPreferences(
|
|
|
|
User user
|
|
|
|
) =>
|
|
|
|
user.CustomPreferences.Select(x => (x.Key, RenderCustomPreference(x.Value))).ToDictionary();
|
|
|
|
|
2024-07-13 19:38:40 +02:00
|
|
|
public 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-12-02 18:06:19 +01:00
|
|
|
public 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-09-27 14:48:09 +02:00
|
|
|
|
2024-12-09 14:52:31 +01:00
|
|
|
public string? ImageUrlFor(PrideFlag flag) =>
|
|
|
|
flag.Hash != null ? $"{config.MediaBaseUrl}/flags/{flag.Hash}.webp" : null;
|
2024-07-12 17:12:24 +02:00
|
|
|
|
2024-09-27 14:48:09 +02:00
|
|
|
public PrideFlagResponse RenderPrideFlag(PrideFlag flag) =>
|
|
|
|
new(flag.Id, ImageUrlFor(flag), flag.Name, flag.Description);
|
2024-10-02 00:28:07 +02:00
|
|
|
}
|