fix: better entity resolving in group member endpoints

This commit is contained in:
spiral 2022-04-07 03:48:32 -04:00
parent 36873e08ee
commit 70cf472fb5
No known key found for this signature in database
GPG Key ID: 244A11E4B0BCF40E
2 changed files with 45 additions and 14 deletions

View File

@ -12,6 +12,9 @@ public class PKControllerBase: ControllerBase
private readonly Regex _shortIdRegex = new("^[a-z]{5}$");
private readonly Regex _snowflakeRegex = new("^[0-9]{17,19}$");
private List<PKMember>? _memberLookupCache { get; set; }
private List<PKGroup>? _groupLookupCache { get; set; }
protected readonly ApiConfig _config;
protected readonly IDatabase _db;
protected readonly ModelRepository _repo;
@ -47,26 +50,54 @@ public class PKControllerBase: ControllerBase
return Task.FromResult<PKSystem?>(null);
}
protected Task<PKMember?> ResolveMember(string memberRef)
protected async Task<PKMember?> ResolveMember(string memberRef, bool cache = false)
{
if (cache)
{
if (_memberLookupCache == null)
{
HttpContext.Items.TryGetValue("SystemId", out var systemId);
if (systemId == null)
throw new Exception("Authenticated user must not be null to use lookup cache!");
_memberLookupCache = await _repo.GetSystemMembers((SystemId)systemId).ToListAsync();
}
return _memberLookupCache.FirstOrDefault(x => x.Hid == memberRef || x.Uuid.ToString() == memberRef);
}
if (Guid.TryParse(memberRef, out var guid))
return _repo.GetMemberByGuid(guid);
return await _repo.GetMemberByGuid(guid);
if (_shortIdRegex.IsMatch(memberRef))
return _repo.GetMemberByHid(memberRef);
return await _repo.GetMemberByHid(memberRef);
return Task.FromResult<PKMember?>(null);
return null;
}
protected Task<PKGroup?> ResolveGroup(string groupRef)
protected async Task<PKGroup?> ResolveGroup(string groupRef, bool cache = true)
{
if (cache)
{
if (_groupLookupCache == null)
{
HttpContext.Items.TryGetValue("SystemId", out var systemId);
if (systemId == null)
throw new Exception("Authenticated user must not be null to use lookup cache!");
_groupLookupCache = await _repo.GetSystemGroups((SystemId)systemId).ToListAsync();
}
return _groupLookupCache.FirstOrDefault(x => x.Hid == groupRef || x.Uuid.ToString() == groupRef);
}
if (Guid.TryParse(groupRef, out var guid))
return _repo.GetGroupByGuid(guid);
return await _repo.GetGroupByGuid(guid);
if (_shortIdRegex.IsMatch(groupRef))
return _repo.GetGroupByHid(groupRef);
return await _repo.GetGroupByHid(groupRef);
return Task.FromResult<PKGroup?>(null);
return null;
}
protected LookupContext ContextFor(PKSystem system)

View File

@ -53,7 +53,7 @@ public class GroupMemberControllerV2: PKControllerBase
foreach (var JmemberRef in memberRefs)
{
var memberRef = JmemberRef.Value<string>();
var member = await ResolveMember(memberRef);
var member = await ResolveMember(memberRef, cache: true);
// todo: have a list of these errors instead of immediately throwing
@ -93,7 +93,7 @@ public class GroupMemberControllerV2: PKControllerBase
foreach (var JmemberRef in memberRefs)
{
var memberRef = JmemberRef.Value<string>();
var member = await ResolveMember(memberRef);
var member = await ResolveMember(memberRef, cache: true);
if (member == null)
throw Errors.MemberNotFoundWithRef(memberRef);
@ -124,7 +124,7 @@ public class GroupMemberControllerV2: PKControllerBase
foreach (var JmemberRef in memberRefs)
{
var memberRef = JmemberRef.Value<string>();
var member = await ResolveMember(memberRef);
var member = await ResolveMember(memberRef, cache: true);
if (member == null)
throw Errors.MemberNotFoundWithRef(memberRef);
@ -182,7 +182,7 @@ public class GroupMemberControllerV2: PKControllerBase
foreach (var JgroupRef in groupRefs)
{
var groupRef = JgroupRef.Value<string>();
var group = await ResolveGroup(groupRef);
var group = await ResolveGroup(groupRef, cache: true);
if (group == null)
throw Errors.GroupNotFound;
@ -220,7 +220,7 @@ public class GroupMemberControllerV2: PKControllerBase
foreach (var JgroupRef in groupRefs)
{
var groupRef = JgroupRef.Value<string>();
var group = await ResolveGroup(groupRef);
var group = await ResolveGroup(groupRef, cache: true);
if (group == null)
throw Errors.GroupNotFoundWithRef(groupRef);
@ -251,7 +251,7 @@ public class GroupMemberControllerV2: PKControllerBase
foreach (var JgroupRef in groupRefs)
{
var groupRef = JgroupRef.Value<string>();
var group = await ResolveGroup(groupRef);
var group = await ResolveGroup(groupRef, cache: true);
if (group == null)
throw Errors.GroupNotFoundWithRef(groupRef);