Foxchat.NET/Foxchat.Identity/Services/ChatInstanceResolverService.cs

40 lines
1.3 KiB
C#
Raw Normal View History

2024-05-11 15:26:47 +02:00
using Foxchat.Core.Federation;
using Foxchat.Core.Models.Http;
2024-05-11 15:26:47 +02:00
using Foxchat.Identity.Database;
using Foxchat.Identity.Database.Models;
using Microsoft.EntityFrameworkCore;
namespace Foxchat.Identity.Services;
public class ChatInstanceResolverService(ILogger logger, RequestSigningService requestSigningService, IdentityContext db, InstanceConfig config)
2024-05-11 15:26:47 +02:00
{
private readonly ILogger _logger = logger.ForContext<ChatInstanceResolverService>();
public async Task<ChatInstance> ResolveChatInstanceAsync(string domain)
{
var instance = await db.ChatInstances.Where(c => c.Domain == domain).FirstOrDefaultAsync();
2024-05-11 15:26:47 +02:00
if (instance != null) return instance;
_logger.Information("Unknown chat instance {Domain}, fetching its data", domain);
var resp = await requestSigningService.RequestAsync<Hello.HelloResponse>(
2024-05-11 15:26:47 +02:00
HttpMethod.Post,
domain, "/_fox/chat/hello",
userId: null,
body: new Hello.HelloRequest(config.Domain)
2024-05-11 15:26:47 +02:00
);
instance = new ChatInstance
{
Domain = domain,
BaseUrl = $"https://{domain}",
PublicKey = resp.PublicKey,
Status = ChatInstance.InstanceStatus.Active,
};
2024-05-21 17:45:35 +02:00
db.Add(instance);
await db.SaveChangesAsync();
2024-05-11 15:26:47 +02:00
return instance;
}
}