Foxchat.NET/Foxchat.Chat/Services/UserResolverService.cs

35 lines
1.1 KiB
C#
Raw Normal View History

2024-05-21 20:14:52 +02:00
using Foxchat.Chat.Database;
using Foxchat.Chat.Database.Models;
using Foxchat.Core.Federation;
using Foxchat.Core.Models;
using Microsoft.EntityFrameworkCore;
namespace Foxchat.Chat.Services;
public class UserResolverService(ILogger logger, ChatContext db, RequestSigningService requestSigningService)
{
public async Task<User> ResolveUserAsync(IdentityInstance instance, string userId)
{
var user = await db.Users.FirstOrDefaultAsync(u => u.InstanceId == instance.Id && u.RemoteUserId == userId);
if (user != null)
{
// TODO: update user if it's been long enough
return user;
}
var userResponse = await requestSigningService.RequestAsync<Users.User>(HttpMethod.Get, instance.Domain,
$"/_fox/ident/users/{userId}");
user = new User
{
Instance = instance,
Username = userResponse.Username,
RemoteUserId = userResponse.Id,
Avatar = userResponse.AvatarUrl
};
db.Add(user);
await db.SaveChangesAsync();
return user;
}
}