From 38dd4d10797b3e6e397fc219e818f379bdff19f2 Mon Sep 17 00:00:00 2001 From: Zomatree Date: Fri, 19 Sep 2025 01:48:23 +0100 Subject: [PATCH] fix: swap to using reqwest for query building --- Cargo.lock | 1 - crates/services/gifbox/Cargo.toml | 1 - crates/services/gifbox/Dockerfile | 2 - crates/services/gifbox/src/tenor/mod.rs | 85 ++++++++++++------------- 4 files changed, 42 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3270fbf3..01223df4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6525,7 +6525,6 @@ dependencies = [ "serde_json", "tokio 1.47.1", "tracing", - "urlencoding", "utoipa", "utoipa-scalar", ] diff --git a/crates/services/gifbox/Cargo.toml b/crates/services/gifbox/Cargo.toml index b640766e..3e2b4173 100644 --- a/crates/services/gifbox/Cargo.toml +++ b/crates/services/gifbox/Cargo.toml @@ -45,4 +45,3 @@ tracing = "0.1" # Utils lru_time_cache = "0.11.11" -urlencoding = "2.1.3" diff --git a/crates/services/gifbox/Dockerfile b/crates/services/gifbox/Dockerfile index 5668448f..196409b7 100644 --- a/crates/services/gifbox/Dockerfile +++ b/crates/services/gifbox/Dockerfile @@ -4,8 +4,6 @@ FROM ghcr.io/revoltchat/base:latest AS builder # Bundle Stage FROM gcr.io/distroless/cc-debian12:nonroot COPY --from=builder /home/rust/src/target/release/revolt-gifbox ./ -COPY --from=mwader/static-ffmpeg:7.0.2 /ffmpeg /usr/local/bin/ -COPY --from=mwader/static-ffmpeg:7.0.2 /ffprobe /usr/local/bin/ EXPOSE 14706 USER nonroot diff --git a/crates/services/gifbox/src/tenor/mod.rs b/crates/services/gifbox/src/tenor/mod.rs index c90b74ce..a9971b17 100644 --- a/crates/services/gifbox/src/tenor/mod.rs +++ b/crates/services/gifbox/src/tenor/mod.rs @@ -1,4 +1,4 @@ -//! Interal Tenor API wrapper +//! Internal Tenor API wrapper use std::{sync::Arc, time::Duration}; @@ -7,7 +7,6 @@ use reqwest::Client; use revolt_coalesced::{CoalescionService, CoalescionServiceConfig}; use serde::de::DeserializeOwned; use tokio::sync::RwLock; -use urlencoding::encode as url_encode; pub mod types; @@ -60,10 +59,11 @@ impl Tenor { } } - pub async fn request(&self, path: String) -> Result, TenorError> { + pub async fn request(&self, path: &str, query: &[Option<(&str, &str)>]) -> Result, TenorError> { let response = self .client .get(format!("{TENOR_API_BASE_URL}{path}")) + .query(query) .send() .await .inspect_err(|e| { @@ -72,7 +72,7 @@ impl Tenor { .map_err(|_| TenorError::HttpError)?; let text = response.text().await.map_err(|e| { - println!("{e:?}"); + revolt_config::capture_error(&e); TenorError::HttpError })?; @@ -95,24 +95,21 @@ impl Tenor { } } - 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, - url_encode(query), - url_encode(locale), - ); - - if !position.is_empty() { - path.push_str("&pos="); - path.push_str(position); - }; - - if is_category { - path.push_str("&component=categories"); - } - - self.request::(path) + let res = self.coalescion.execute(unique_key.clone(), || async move { + self.request::( + "/search", + &[ + Some(("key", &self.key)), + Some(("q", query)), + Some(("client_key", "Gifbox")), + Some(("media_filter", "webm,tinywebm")), + Some(("locale", locale)), + Some(("contentfilter", "high")), + Some(("limit", &limit.to_string())), + position.is_empty().then_some(("pos", position)), + is_category.then_some(("component", "categories")) + ] + ).await }) .await .unwrap(); @@ -138,14 +135,16 @@ impl Tenor { let res = self .coalescion - .execute(unique_key.clone(), || { - let path = format!( - "/categories?key={}&client_key=Gifbox&locale={}&contentfilter=high", - &self.key, - url_encode(locale), - ); - - self.request::(path) + .execute(unique_key.clone(), || async move { + self.request::( + "/categories", + &[ + Some(("key", &self.key)), + Some(("client_key", "Gifbox")), + Some(("locale", locale)), + Some(("contentfilter", "high")), + ] + ).await }) .await .unwrap(); @@ -174,19 +173,19 @@ impl Tenor { } } - 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, - url_encode(locale), - ); - - if !position.is_empty() { - path.push_str("&pos="); - path.push_str(position); - }; - - self.request::(path) + let res = self.coalescion.execute(unique_key.clone(), || async move { + self.request::( + "/featured", + &[ + Some(("key", &self.key)), + Some(("client_key", "Gifbox")), + Some(("media_filter", "webm,tinywebm")), + Some(("locale", locale)), + Some(("contentfilter", "high")), + Some(("limit", &limit.to_string())), + position.is_empty().then_some(("pos", position)), + ] + ).await }) .await .unwrap();