2021-09-30 02:30:20 +00:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
using PluralKit.Core;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.API;
|
|
|
|
|
|
|
|
public class PKControllerBase: ControllerBase
|
2021-09-30 02:30:20 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly Guid _requestId = Guid.NewGuid();
|
|
|
|
private readonly Regex _shortIdRegex = new("^[a-z]{5}$");
|
|
|
|
private readonly Regex _snowflakeRegex = new("^[0-9]{17,19}$");
|
2021-09-30 02:30:20 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
protected readonly ApiConfig _config;
|
|
|
|
protected readonly IDatabase _db;
|
|
|
|
protected readonly ModelRepository _repo;
|
|
|
|
protected readonly DispatchService _dispatch;
|
2021-09-30 02:30:20 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public PKControllerBase(IServiceProvider svc)
|
|
|
|
{
|
|
|
|
_config = svc.GetRequiredService<ApiConfig>();
|
|
|
|
_db = svc.GetRequiredService<IDatabase>();
|
|
|
|
_repo = svc.GetRequiredService<ModelRepository>();
|
|
|
|
_dispatch = svc.GetRequiredService<DispatchService>();
|
|
|
|
}
|
2021-10-02 01:50:01 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
protected Task<PKSystem?> ResolveSystem(string systemRef)
|
|
|
|
{
|
|
|
|
if (systemRef == "@me")
|
2021-10-02 01:50:01 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
HttpContext.Items.TryGetValue("SystemId", out var systemId);
|
|
|
|
if (systemId == null)
|
|
|
|
throw Errors.GenericAuthError;
|
|
|
|
return _repo.GetSystem((SystemId)systemId);
|
|
|
|
}
|
2021-10-02 01:50:01 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (Guid.TryParse(systemRef, out var guid))
|
|
|
|
return _repo.GetSystemByGuid(guid);
|
2021-10-02 01:50:01 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (_snowflakeRegex.IsMatch(systemRef))
|
|
|
|
return _repo.GetSystemByAccount(ulong.Parse(systemRef));
|
2021-10-02 01:50:01 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (_shortIdRegex.IsMatch(systemRef))
|
|
|
|
return _repo.GetSystemByHid(systemRef);
|
2021-10-02 01:50:01 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return Task.FromResult<PKSystem?>(null);
|
|
|
|
}
|
2021-10-02 01:50:01 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
protected Task<PKMember?> ResolveMember(string memberRef)
|
|
|
|
{
|
|
|
|
if (Guid.TryParse(memberRef, out var guid))
|
|
|
|
return _repo.GetMemberByGuid(guid);
|
2021-10-12 09:17:54 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (_shortIdRegex.IsMatch(memberRef))
|
|
|
|
return _repo.GetMemberByHid(memberRef);
|
2021-10-12 09:17:54 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return Task.FromResult<PKMember?>(null);
|
|
|
|
}
|
2021-10-12 09:17:54 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
protected Task<PKGroup?> ResolveGroup(string groupRef)
|
|
|
|
{
|
|
|
|
if (Guid.TryParse(groupRef, out var guid))
|
|
|
|
return _repo.GetGroupByGuid(guid);
|
2021-10-12 09:17:54 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
if (_shortIdRegex.IsMatch(groupRef))
|
|
|
|
return _repo.GetGroupByHid(groupRef);
|
2021-10-12 09:17:54 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
return Task.FromResult<PKGroup?>(null);
|
|
|
|
}
|
2021-10-12 09:17:54 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
protected LookupContext ContextFor(PKSystem system)
|
|
|
|
{
|
|
|
|
HttpContext.Items.TryGetValue("SystemId", out var systemId);
|
|
|
|
if (systemId == null) return LookupContext.ByNonOwner;
|
|
|
|
return (SystemId)systemId == system.Id ? LookupContext.ByOwner : LookupContext.ByNonOwner;
|
|
|
|
}
|
2021-10-12 09:17:54 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
protected LookupContext ContextFor(PKMember member)
|
|
|
|
{
|
|
|
|
HttpContext.Items.TryGetValue("SystemId", out var systemId);
|
|
|
|
if (systemId == null) return LookupContext.ByNonOwner;
|
|
|
|
return (SystemId)systemId == member.System ? LookupContext.ByOwner : LookupContext.ByNonOwner;
|
|
|
|
}
|
2021-10-12 09:17:54 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
protected LookupContext ContextFor(PKGroup group)
|
|
|
|
{
|
|
|
|
HttpContext.Items.TryGetValue("SystemId", out var systemId);
|
|
|
|
if (systemId == null) return LookupContext.ByNonOwner;
|
|
|
|
return (SystemId)systemId == group.System ? LookupContext.ByOwner : LookupContext.ByNonOwner;
|
2021-09-30 02:30:20 +00:00
|
|
|
}
|
|
|
|
}
|