Кэш для GIF, подсветка цитат и ответов
This commit is contained in:
@@ -24,8 +24,6 @@ public class ConfigController : ControllerBase
|
||||
{
|
||||
conf.EnableCalls,
|
||||
conf.EnableKlipy,
|
||||
KlipyApiKey = conf.EnableKlipy ? conf.KlipyApiKey : null,
|
||||
KlipyCustomerId = conf.EnableKlipy ? conf.KlipyCustomerId : null,
|
||||
conf.MaxFileSizeMb,
|
||||
conf.MaxGroupMembers,
|
||||
conf.EnableConfederation
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
using Knot.Shared.Kernel.Configuration;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Host.Controllers;
|
||||
|
||||
[Authorize]
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class KlipyController : ControllerBase
|
||||
{
|
||||
private readonly ISettingsService _settings;
|
||||
private readonly IMemoryCache _cache;
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
|
||||
public KlipyController(ISettingsService settings, IMemoryCache cache, IHttpClientFactory httpClientFactory)
|
||||
{
|
||||
_settings = settings;
|
||||
_cache = cache;
|
||||
_httpClientFactory = httpClientFactory;
|
||||
}
|
||||
|
||||
[HttpGet("trending")]
|
||||
public async Task<IActionResult> GetTrending()
|
||||
{
|
||||
var conf = _settings.Current;
|
||||
if (!conf.EnableKlipy || string.IsNullOrEmpty(conf.KlipyApiKey))
|
||||
return BadRequest(new { error = "Klipy is disabled or not configured." });
|
||||
|
||||
if (_cache.TryGetValue("klipy_trending", out JsonElement cachedResult))
|
||||
{
|
||||
return Ok(cachedResult);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient();
|
||||
var url = $"https://api.klipy.com/api/v1/{conf.KlipyApiKey}/gifs/trending?page=1&per_page=30&customer_id={conf.KlipyCustomerId ?? "anonymous"}";
|
||||
var result = await client.GetFromJsonAsync<JsonElement>(url);
|
||||
|
||||
_cache.Set("klipy_trending", result, TimeSpan.FromMinutes(60)); // Cache trending for 60 minutes
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, new { error = "Failed to fetch from Klipy", details = ex.Message });
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("search")]
|
||||
public async Task<IActionResult> Search([FromQuery] string q)
|
||||
{
|
||||
var conf = _settings.Current;
|
||||
if (!conf.EnableKlipy || string.IsNullOrEmpty(conf.KlipyApiKey))
|
||||
return BadRequest(new { error = "Klipy is disabled or not configured." });
|
||||
|
||||
if (string.IsNullOrWhiteSpace(q))
|
||||
return BadRequest(new { error = "Query is empty." });
|
||||
|
||||
var cacheKey = $"klipy_search_{q.ToLowerInvariant()}";
|
||||
if (_cache.TryGetValue(cacheKey, out JsonElement cachedResult))
|
||||
{
|
||||
return Ok(cachedResult);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var client = _httpClientFactory.CreateClient();
|
||||
var url = $"https://api.klipy.com/api/v1/{conf.KlipyApiKey}/gifs/search?page=1&per_page=30&q={Uri.EscapeDataString(q)}&customer_id={conf.KlipyCustomerId ?? "anonymous"}";
|
||||
var result = await client.GetFromJsonAsync<JsonElement>(url);
|
||||
|
||||
_cache.Set(cacheKey, result, TimeSpan.FromMinutes(15)); // Cache searches for 15 minutes
|
||||
return Ok(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return StatusCode(500, new { error = "Failed to fetch from Klipy", details = ex.Message });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,6 +79,8 @@ builder.Services.AddRouting(options =>
|
||||
options.LowercaseQueryStrings = true;
|
||||
});
|
||||
|
||||
builder.Services.AddMemoryCache();
|
||||
builder.Services.AddHttpClient();
|
||||
|
||||
builder.Services.AddControllers()
|
||||
.AddJsonOptions(options =>
|
||||
|
||||
Reference in New Issue
Block a user