35 lines
1.3 KiB
C#
35 lines
1.3 KiB
C#
using System.Net;
|
|||
|
|
|
||
|
|
using collAnon.Client.Services;
|
||
|
|
|
||
|
|
using decePubClient.Extensions;
|
||
|
|
using decePubClient.Models;
|
||
|
|
|
||
|
|
using PrivaPub.ClientModels.Group;
|
||
|
|
|
||
|
|
namespace decePubClient.Services
|
||
|
|
{
|
||
|
|
/// <summary>The communities and circles a persona belongs to (/clientapi/group/list).</summary>
|
||
|
|
public class GroupService(IHttpService httpService, ILoggingService logger, CoalescingStringLocalizer localizer)
|
||
|
|
{
|
||
|
|
/// <summary>The persona's groups, communities first; empty on failure.</summary>
|
||
|
|
public async Task<List<ViewGroup>> List(string personaId, CancellationToken cancellationToken = default)
|
||
|
|
{
|
||
|
|
if (personaId is null)
|
||
|
|
return [];
|
||
|
|
|
||
|
|
using var response = await httpService.Get(APIs.ClientApi.GET_Groups, [$"avatarId={Uri.EscapeDataString(personaId)}"],
|
||
|
|
cancellationToken: cancellationToken);
|
||
|
|
if (!response.IsSuccessStatusCode)
|
||
|
|
{
|
||
|
|
var result = await response.Content.ReadWebResult(response.StatusCode, localizer, cancellationToken);
|
||
|
|
if (result.StatusCode is not (int)HttpStatusCode.Gone)
|
||
|
|
await logger.ProcessWarning(result.ToString(localizer), $"{nameof(GroupService)}.{nameof(List)}", cancellationToken);
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
|
||
|
|
var groups = await response.DefaultReadFromJsonAsync<List<ViewGroup>>(cancellationToken) ?? [];
|
||
|
|
return [.. groups.OrderByDescending(g => g.IsCommunity).ThenBy(g => g.Name ?? g.UserName)];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|