init
This commit is contained in:
commit
f6629fbb33
32 changed files with 1608 additions and 0 deletions
39
Foxchat.Identity/Services/ChatInstanceResolverService.cs
Normal file
39
Foxchat.Identity/Services/ChatInstanceResolverService.cs
Normal file
|
@ -0,0 +1,39 @@
|
|||
using Foxchat.Core.Federation;
|
||||
using Foxchat.Core.Models;
|
||||
using Foxchat.Identity.Database;
|
||||
using Foxchat.Identity.Database.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Foxchat.Identity.Services;
|
||||
|
||||
public class ChatInstanceResolverService(ILogger logger, RequestSigningService requestSigningService, IdentityContext context, InstanceConfig config)
|
||||
{
|
||||
private readonly ILogger _logger = logger.ForContext<ChatInstanceResolverService>();
|
||||
|
||||
public async Task<ChatInstance> ResolveChatInstanceAsync(string domain)
|
||||
{
|
||||
var instance = await context.ChatInstances.Where(c => c.Domain == domain).FirstOrDefaultAsync();
|
||||
if (instance != null) return instance;
|
||||
|
||||
_logger.Information("Unknown chat instance {Domain}, fetching its data", domain);
|
||||
|
||||
var resp = await requestSigningService.RequestAsync<HelloResponse>(
|
||||
HttpMethod.Post,
|
||||
domain, "/_fox/chat/hello",
|
||||
userId: null,
|
||||
body: new HelloRequest(config.Domain)
|
||||
);
|
||||
|
||||
instance = new ChatInstance
|
||||
{
|
||||
Domain = domain,
|
||||
BaseUrl = $"https://{domain}",
|
||||
PublicKey = resp.PublicKey,
|
||||
Status = ChatInstance.InstanceStatus.Active,
|
||||
};
|
||||
await context.AddAsync(instance);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
return instance;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue