feat(api): add news to /api/meta response
This commit is contained in:
parent
31b6ac2cac
commit
5c57b75335
6 changed files with 147 additions and 5 deletions
|
|
@ -14,11 +14,43 @@
|
|||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
using Catalogger.Backend.Database.Redis;
|
||||
using Remora.Discord.API;
|
||||
using Remora.Discord.API.Abstractions.Objects;
|
||||
using Remora.Discord.API.Abstractions.Rest;
|
||||
|
||||
namespace Catalogger.Backend.Api;
|
||||
|
||||
public class ApiCache(RedisService redisService)
|
||||
public class ApiCache(RedisService redisService, IDiscordRestChannelAPI channelApi, Config config)
|
||||
{
|
||||
private List<IMessage>? _news;
|
||||
private readonly SemaphoreSlim _newsSemaphore = new(1);
|
||||
|
||||
public async Task<List<IMessage>> GetNewsAsync()
|
||||
{
|
||||
await _newsSemaphore.WaitAsync();
|
||||
try
|
||||
{
|
||||
if (_news != null)
|
||||
return _news;
|
||||
|
||||
if (config.Web.NewsChannel == null)
|
||||
return [];
|
||||
|
||||
var res = await channelApi.GetChannelMessagesAsync(
|
||||
DiscordSnowflake.New(config.Web.NewsChannel.Value),
|
||||
limit: 5
|
||||
);
|
||||
if (res.IsSuccess)
|
||||
return _news = res.Entity.ToList();
|
||||
|
||||
return [];
|
||||
}
|
||||
finally
|
||||
{
|
||||
_newsSemaphore.Release();
|
||||
}
|
||||
}
|
||||
|
||||
private static string UserKey(string id) => $"api-user:{id}";
|
||||
|
||||
private static string GuildsKey(string userId) => $"api-user-guilds:{userId}";
|
||||
|
|
|
|||
|
|
@ -31,7 +31,6 @@ namespace Catalogger.Backend.Api;
|
|||
|
||||
[Route("/api/guilds/{id}")]
|
||||
public partial class GuildsController(
|
||||
Config config,
|
||||
ILogger logger,
|
||||
DatabaseContext db,
|
||||
ChannelCache channelCache,
|
||||
|
|
|
|||
|
|
@ -15,7 +15,10 @@
|
|||
|
||||
using Catalogger.Backend.Api.Middleware;
|
||||
using Catalogger.Backend.Cache.InMemoryCache;
|
||||
using Catalogger.Backend.Extensions;
|
||||
using Catalogger.Backend.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Remora.Discord.API.Abstractions.Objects;
|
||||
|
||||
namespace Catalogger.Backend.Api;
|
||||
|
||||
|
|
@ -23,20 +26,24 @@ namespace Catalogger.Backend.Api;
|
|||
public class MetaController(
|
||||
Config config,
|
||||
GuildCache guildCache,
|
||||
NewsService newsService,
|
||||
DiscordRequestService discordRequestService
|
||||
) : ApiControllerBase
|
||||
{
|
||||
[HttpGet("meta")]
|
||||
public IActionResult GetMeta()
|
||||
public async Task<IActionResult> GetMetaAsync()
|
||||
{
|
||||
var inviteUrl =
|
||||
$"https://discord.com/oauth2/authorize?client_id={config.Discord.ApplicationId}"
|
||||
+ "&permissions=537250993&scope=bot+applications.commands";
|
||||
|
||||
var news = await newsService.GetNewsAsync();
|
||||
|
||||
return Ok(
|
||||
new MetaResponse(
|
||||
Guilds: (int)CataloggerMetrics.GuildsCached.Value,
|
||||
InviteUrl: inviteUrl
|
||||
InviteUrl: inviteUrl,
|
||||
News: news
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
@ -60,7 +67,11 @@ public class MetaController(
|
|||
);
|
||||
}
|
||||
|
||||
private record MetaResponse(int Guilds, string InviteUrl);
|
||||
private record MetaResponse(
|
||||
int Guilds,
|
||||
string InviteUrl,
|
||||
IEnumerable<NewsService.NewsMessage> News
|
||||
);
|
||||
|
||||
private record CurrentUserResponse(ApiUser User, IEnumerable<ApiGuild> Guilds);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue