48 lines
2.6 KiB
C#
48 lines
2.6 KiB
C#
using System.Net;
|
|||
|
|
|
||
|
|
using decePubClient.Extensions;
|
||
|
|
using decePubClient.Models;
|
||
|
|
using decePubClient.Models.Mastodon;
|
||
|
|
|
||
|
|
using PrivaPub.ClientModels;
|
||
|
|
|
||
|
|
namespace decePubClient.Services
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// The administrators' announcements as the current persona reads them (/api/v1/announcements): the ones shown now,
|
||
|
|
/// dismissed one by one, and reacted to with a unicode emoji (eight different ones at most on each). Dismissals and
|
||
|
|
/// reactions are the persona's own and never leave PrivaPub; the stream tells of new, changed and removed ones
|
||
|
|
/// (StreamingService.Announced). The administrators write them through InstanceAdminService.
|
||
|
|
/// </summary>
|
||
|
|
public class AnnouncementService(IHttpService httpService, ILoggingService logger, CoalescingStringLocalizer localizer)
|
||
|
|
{
|
||
|
|
/// <summary>The announcements shown now, with whether the persona dismissed each.</summary>
|
||
|
|
public async Task<List<Announcement>> List(CancellationToken cancellationToken = default)
|
||
|
|
{
|
||
|
|
using var response = await httpService.Get(APIs.Mastodon.GET_Announcements, cancellationToken: cancellationToken);
|
||
|
|
if (response.IsSuccessStatusCode)
|
||
|
|
return await response.MastodonReadFromJsonAsync<List<Announcement>>(cancellationToken) ?? [];
|
||
|
|
|
||
|
|
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(AnnouncementService)}.{nameof(List)}", cancellationToken);
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<WebResult> Dismiss(string announcementId, CancellationToken cancellationToken = default)
|
||
|
|
{
|
||
|
|
using var response = await httpService.Post(APIs.Mastodon.POST_M_DismissAnnouncement(announcementId), cancellationToken: cancellationToken);
|
||
|
|
return response.IsSuccessStatusCode ? new WebResult() : await response.Content.ReadWebResult(response.StatusCode, localizer, cancellationToken);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Adds the persona's reaction, or takes it back; PrivaPub refuses anything but one unicode emoji (422).</summary>
|
||
|
|
public async Task<WebResult> React(string announcementId, string emoji, bool add, CancellationToken cancellationToken = default)
|
||
|
|
{
|
||
|
|
var route = APIs.Mastodon.M_AnnouncementReaction(announcementId, emoji);
|
||
|
|
using var response = add
|
||
|
|
? await httpService.Put(route, cancellationToken: cancellationToken)
|
||
|
|
: await httpService.Delete(route, cancellationToken: cancellationToken);
|
||
|
|
return response.IsSuccessStatusCode ? new WebResult() : await response.Content.ReadWebResult(response.StatusCode, localizer, cancellationToken);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|