forked from jmug/stoatchat
docs: document revolt-coalesced
This commit is contained in:
committed by
Angelo Kontaxis
parent
5885e067a6
commit
db55998546
@@ -22,8 +22,13 @@ revolt-result = { version = "0.8.8", path = "../../core/result", features = [
|
||||
"utoipa",
|
||||
"axum",
|
||||
] }
|
||||
revolt-coalesced = { version = "0.8.8", path = "../../core/coalesced" }
|
||||
revolt-database = { version = "0.8.8", path = "../../core/database", features = ["axum-impl"] }
|
||||
revolt-coalesced = { version = "0.8.8", path = "../../core/coalesced", features = [
|
||||
"queue",
|
||||
] }
|
||||
revolt-database = { version = "0.8.8", path = "../../core/database", features = [
|
||||
"axum-impl",
|
||||
] }
|
||||
|
||||
# Axum / web server
|
||||
axum = { version = "0.7.5" }
|
||||
axum-extra = { version = "0.9", features = ["typed-header"] }
|
||||
@@ -37,4 +42,4 @@ tracing = "0.1"
|
||||
|
||||
# Utils
|
||||
lru_time_cache = "0.11.11"
|
||||
urlencoding = "2.1.3"
|
||||
urlencoding = "2.1.3"
|
||||
|
||||
@@ -11,6 +11,7 @@ use crate::{tenor, types};
|
||||
|
||||
#[derive(Deserialize, IntoParams)]
|
||||
pub struct CategoriesQueryParams {
|
||||
/// Users locale
|
||||
#[param(example = "en_US")]
|
||||
pub locale: String,
|
||||
}
|
||||
|
||||
@@ -19,4 +19,4 @@ pub async fn root() -> Json<types::RootResponse<'static>> {
|
||||
message: "Gifbox lives on!",
|
||||
version: CRATE_VERSION,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,12 +11,17 @@ use crate::{tenor, types};
|
||||
|
||||
#[derive(Deserialize, IntoParams)]
|
||||
pub struct SearchQueryParams {
|
||||
/// Search query
|
||||
#[param(example = "Wave")]
|
||||
pub query: String,
|
||||
/// Users locale
|
||||
#[param(example = "en_US")]
|
||||
pub locale: String,
|
||||
/// Amount of results to respond with
|
||||
pub limit: Option<u32>,
|
||||
/// Flag for if searching in a gif category
|
||||
pub is_category: Option<bool>,
|
||||
/// Value of `next` for getting the next page of results with the current search query
|
||||
pub position: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
@@ -4,20 +4,20 @@ use axum::{
|
||||
};
|
||||
use revolt_database::User;
|
||||
use revolt_result::{create_error, Result};
|
||||
use serde::{Deserialize};
|
||||
use utoipa::{IntoParams};
|
||||
use serde::Deserialize;
|
||||
use utoipa::IntoParams;
|
||||
|
||||
use crate::{
|
||||
tenor,
|
||||
types,
|
||||
};
|
||||
use crate::{tenor, types};
|
||||
|
||||
#[derive(Deserialize, IntoParams)]
|
||||
pub struct TrendingQueryParams {
|
||||
#[param(example = "en_US")]
|
||||
/// Users locale
|
||||
pub locale: String,
|
||||
/// Amount of results to respond with
|
||||
pub limit: Option<u32>,
|
||||
pub position: Option<String>
|
||||
/// Value of `next` for getting the next page of results of featured gifs
|
||||
pub position: Option<String>,
|
||||
}
|
||||
|
||||
/// Trending GIFs
|
||||
@@ -37,7 +37,11 @@ pub async fn trending(
|
||||
State(tenor): State<tenor::Tenor>,
|
||||
) -> Result<Json<types::PaginatedMediaResponse>> {
|
||||
tenor
|
||||
.featured(¶ms.locale, params.limit.unwrap_or(50), params.position.as_deref().unwrap_or_default())
|
||||
.featured(
|
||||
¶ms.locale,
|
||||
params.limit.unwrap_or(50),
|
||||
params.position.as_deref().unwrap_or_default(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| create_error!(InternalError))
|
||||
.map(|results| results.as_ref().clone().into())
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//! Interal Tenor API wrapper
|
||||
|
||||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
use lru_time_cache::LruCache;
|
||||
@@ -20,18 +22,11 @@ pub enum TenorError {
|
||||
pub struct Tenor {
|
||||
pub key: Arc<str>,
|
||||
pub client: Client,
|
||||
|
||||
pub coalescion: CoalescionService<String>,
|
||||
pub cache: Arc<RwLock<LruCache<String, Arc<types::PaginatedMediaResponse>>>>,
|
||||
pub cache_coalescion:
|
||||
CoalescionService<String, Result<Arc<types::PaginatedMediaResponse>, TenorError>>,
|
||||
|
||||
pub categories: Arc<RwLock<LruCache<String, Arc<types::CategoriesResponse>>>>,
|
||||
pub categories_coalescion:
|
||||
CoalescionService<String, Result<Arc<types::CategoriesResponse>, TenorError>>,
|
||||
|
||||
pub featured: Arc<RwLock<LruCache<String, Arc<types::PaginatedMediaResponse>>>>,
|
||||
pub featured_coalescion:
|
||||
CoalescionService<String, Result<Arc<types::PaginatedMediaResponse>, TenorError>>,
|
||||
}
|
||||
|
||||
impl Tenor {
|
||||
@@ -39,39 +34,29 @@ impl Tenor {
|
||||
Self {
|
||||
key: Arc::from(key),
|
||||
client: Client::new(),
|
||||
coalescion: CoalescionService::from_config(CoalescionServiceConfig {
|
||||
max_concurrent: Some(100),
|
||||
queue_requests: true,
|
||||
max_queue: None,
|
||||
}),
|
||||
|
||||
// 1 hour, 1k requests
|
||||
cache: Arc::new(RwLock::new(LruCache::with_expiry_duration_and_capacity(
|
||||
Duration::from_secs(60 * 60),
|
||||
1000,
|
||||
))),
|
||||
cache_coalescion: CoalescionService::from_config(CoalescionServiceConfig {
|
||||
max_concurrent: Some(100),
|
||||
queue_requests: true,
|
||||
max_queue: None,
|
||||
}),
|
||||
|
||||
// 1 day, 1k requests
|
||||
categories: Arc::new(RwLock::new(LruCache::with_expiry_duration_and_capacity(
|
||||
Duration::from_secs(60 * 60 * 24),
|
||||
1000,
|
||||
))),
|
||||
categories_coalescion: CoalescionService::from_config(CoalescionServiceConfig {
|
||||
max_concurrent: Some(100),
|
||||
queue_requests: true,
|
||||
max_queue: None,
|
||||
}),
|
||||
|
||||
// 1 day, 1k requests
|
||||
featured: Arc::new(RwLock::new(LruCache::with_expiry_duration_and_capacity(
|
||||
Duration::from_secs(60 * 60 * 24),
|
||||
1000,
|
||||
))),
|
||||
featured_coalescion: CoalescionService::from_config(CoalescionServiceConfig {
|
||||
max_concurrent: Some(100),
|
||||
queue_requests: true,
|
||||
max_queue: None,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,7 +95,7 @@ impl Tenor {
|
||||
}
|
||||
}
|
||||
|
||||
let res = self.cache_coalescion.execute(unique_key.clone(), || {
|
||||
let res = self.coalescion.execute(unique_key.clone(), || {
|
||||
let mut path = format!(
|
||||
"/search?key={}&q={}&client_key=Gifbox&media_filter=webm,tinywebm&locale={}&contentfilter=high&limit={limit}",
|
||||
&self.key,
|
||||
@@ -127,7 +112,7 @@ impl Tenor {
|
||||
path.push_str("&component=categories");
|
||||
}
|
||||
|
||||
self.request(path)
|
||||
self.request::<types::PaginatedMediaResponse>(path)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -152,7 +137,7 @@ impl Tenor {
|
||||
}
|
||||
|
||||
let res = self
|
||||
.categories_coalescion
|
||||
.coalescion
|
||||
.execute(unique_key.clone(), || {
|
||||
let path = format!(
|
||||
"/categories?key={}&client_key=Gifbox&locale={}&contentfilter=high",
|
||||
@@ -160,7 +145,7 @@ impl Tenor {
|
||||
url_encode(locale),
|
||||
);
|
||||
|
||||
self.request(path)
|
||||
self.request::<types::CategoriesResponse>(path)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
@@ -189,7 +174,7 @@ impl Tenor {
|
||||
}
|
||||
}
|
||||
|
||||
let res = self.featured_coalescion.execute(unique_key.clone(), || {
|
||||
let res = self.coalescion.execute(unique_key.clone(), || {
|
||||
let mut path = format!(
|
||||
"/featured?key={}&client_key=Gifbox&media_filter=webm,tinywebm&locale={}&contentfilter=high&limit={limit}",
|
||||
&self.key,
|
||||
@@ -201,16 +186,13 @@ impl Tenor {
|
||||
path.push_str(position);
|
||||
};
|
||||
|
||||
self.request(path)
|
||||
self.request::<types::PaginatedMediaResponse>(path)
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
if let Ok(resp) = &*res {
|
||||
self.featured
|
||||
.write()
|
||||
.await
|
||||
.insert(unique_key, resp.clone());
|
||||
self.featured.write().await.insert(unique_key, resp.clone());
|
||||
}
|
||||
|
||||
(*res).clone()
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//! Tenor API models
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
Reference in New Issue
Block a user