Skip to content

Commit

Permalink
refactor assyst out of some structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacherr committed Aug 1, 2024
1 parent 4ab307c commit 99adec4
Show file tree
Hide file tree
Showing 24 changed files with 117 additions and 102 deletions.
7 changes: 1 addition & 6 deletions assyst-core/src/assyst.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use assyst_common::config::{CONFIG, PATREON_REFRESH_LOCATION};
use assyst_common::config::CONFIG;
use assyst_common::err;
use assyst_common::metrics_handler::MetricsHandler;
use assyst_common::pipe::CACHE_PIPE_PATH;
Expand Down Expand Up @@ -57,8 +57,6 @@ pub struct Assyst {
/// All command ratelimits, in the format <(guild/user id, command name) => time command was
/// ran>
pub command_ratelimits: CommandRatelimits,
/// patreon refresh token
pub patreon_refresh: tokio::sync::Mutex<String>,
}
impl Assyst {
pub async fn new() -> anyhow::Result<Assyst> {
Expand All @@ -68,8 +66,6 @@ impl Assyst {
Arc::new(DatabaseHandler::new(CONFIG.database.to_url(), CONFIG.database.to_url_safe()).await?);
let premium_users = Arc::new(Mutex::new(vec![]));
let current_application = http_client.current_user_application().await?.model().await?;
let patreon_refresh = std::fs::read_to_string(PATREON_REFRESH_LOCATION).unwrap_or_default();
let patreon_refresh_t = patreon_refresh.trim();

Ok(Assyst {
bad_translator: BadTranslator::new(),
Expand All @@ -86,7 +82,6 @@ impl Assyst {
flux_handler: FluxHandler::new(database_handler.clone(), Arc::new(Mutex::new(HashMap::new()))),
rest_cache_handler: RestCacheHandler::new(http_client.clone()),
command_ratelimits: CommandRatelimits::new(),
patreon_refresh: tokio::sync::Mutex::new(patreon_refresh_t.to_owned()),
})
}

Expand Down
18 changes: 14 additions & 4 deletions assyst-core/src/command/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,16 +809,26 @@ impl ParseArgument for Image {
async fn parse_raw_message(ctxt: &mut RawMessageParseCtxt<'_>, label: Label) -> Result<Self, TagParseError> {
let ImageUrl(url) = ImageUrl::parse_raw_message(ctxt, label).await?;

let data =
downloader::download_content(ctxt.cx.assyst(), &url, ABSOLUTE_INPUT_FILE_SIZE_LIMIT_BYTES, true).await?;
let data = downloader::download_content(
&ctxt.cx.assyst().reqwest_client,
&url,
ABSOLUTE_INPUT_FILE_SIZE_LIMIT_BYTES,
true,
)
.await?;
Ok(Image(data))
}

async fn parse_command_option(ctxt: &mut InteractionCommandParseCtxt<'_>) -> Result<Self, TagParseError> {
let ImageUrl(url) = ImageUrl::parse_command_option(ctxt).await?;

let data =
downloader::download_content(ctxt.cx.assyst(), &url, ABSOLUTE_INPUT_FILE_SIZE_LIMIT_BYTES, true).await?;
let data = downloader::download_content(
&ctxt.cx.assyst().reqwest_client,
&url,
ABSOLUTE_INPUT_FILE_SIZE_LIMIT_BYTES,
true,
)
.await?;
Ok(Image(data))
}

Expand Down
2 changes: 1 addition & 1 deletion assyst-core/src/command/fun/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub mod translation;
send_processing = true
)]
pub async fn findsong(ctxt: CommandCtxt<'_>, input: ImageUrl) -> anyhow::Result<()> {
let result = identify_song_notsoidentify(ctxt.assyst().clone(), input.0)
let result = identify_song_notsoidentify(&ctxt.assyst().reqwest_client, input.0)
.await
.context("Failed to identify song")?;

Expand Down
14 changes: 10 additions & 4 deletions assyst-core/src/command/misc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,15 @@ pub async fn exec(ctxt: CommandCtxt<'_>, script: RestNoFlags) -> anyhow::Result<
examples = ["1"]
)]
pub async fn eval(ctxt: CommandCtxt<'_>, script: Codeblock) -> anyhow::Result<()> {
let result = fake_eval(ctxt.assyst(), script.0, true, ctxt.data.message, Vec::new())
.await
.context("Evaluation failed")?;
let result = fake_eval(
&ctxt.assyst().reqwest_client,
script.0,
true,
ctxt.data.message,
Vec::new(),
)
.await
.context("Evaluation failed")?;

match result {
FakeEvalImageResponse::Image(im, _) => {
Expand Down Expand Up @@ -377,7 +383,7 @@ pub async fn chars(ctxt: CommandCtxt<'_>, chars: RestNoFlags) -> anyhow::Result<
let mut output = String::new();

for ch in chars {
let (html, url) = get_char_info(ctxt.assyst().clone(), ch).await?;
let (html, url) = get_char_info(&ctxt.assyst().reqwest_client, ch).await?;

let title = extract_page_title(&html).unwrap_or_else(|| "<unknown>".to_owned());

Expand Down
2 changes: 1 addition & 1 deletion assyst-core/src/command/misc/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ pub async fn rustc(ctxt: CommandCtxt<'_>, script: Codeblock) -> anyhow::Result<(

// download toolchain so we use correct compiler for clippy_utils and other internal crates
let raw = download_content(
ctxt.assyst(),
&ctxt.assyst().reqwest_client,
"https://raw.githubusercontent.com/rust-lang/rust-clippy/master/rust-toolchain",
usize::MAX,
false,
Expand Down
2 changes: 1 addition & 1 deletion assyst-core/src/command/misc/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub async fn stats(ctxt: CommandCtxt<'_>, option: Option<Word>) -> anyhow::Resul
async fn get_storage_stats(ctxt: &CommandCtxt<'_>) -> anyhow::Result<String> {
let database_size = ctxt.assyst().database_handler.database_size().await?.size;
let cache_size = human_bytes(ctxt.assyst().database_handler.cache.size_of() as f64);
let filer_stats = filer_stats(ctxt.assyst().clone()).await.unwrap_or(FilerStats {
let filer_stats = filer_stats(&ctxt.assyst().reqwest_client).await.unwrap_or(FilerStats {
count: 0,
size_bytes: 0,
});
Expand Down
11 changes: 8 additions & 3 deletions assyst-core/src/command/misc/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,13 @@ impl assyst_tag::Context for TagContext {
code: &str,
args: Vec<String>,
) -> anyhow::Result<assyst_common::eval::FakeEvalImageResponse> {
self.tokio
.block_on(fake_eval(&self.assyst, code.into(), true, self.message.as_ref(), args))
self.tokio.block_on(fake_eval(
&self.assyst.reqwest_client,
code.into(),
true,
self.message.as_ref(),
args,
))
}

fn get_last_attachment(&self) -> anyhow::Result<String> {
Expand All @@ -509,7 +514,7 @@ impl assyst_tag::Context for TagContext {
fn download(&self, url: &str) -> anyhow::Result<String> {
self.tokio
.block_on(download_content(
&self.assyst,
&self.assyst.reqwest_client,
url,
ABSOLUTE_INPUT_FILE_SIZE_LIMIT_BYTES,
true,
Expand Down
11 changes: 8 additions & 3 deletions assyst-core/src/command/services/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ flag_parse_argument! { DownloadFlags }
]
)]
pub async fn download(ctxt: CommandCtxt<'_>, url: Word, options: DownloadFlags) -> anyhow::Result<()> {
let mut opts = WebDownloadOpts::from_download_flags(options);
let mut opts =
WebDownloadOpts::from_download_flags(options, ctxt.assyst().rest_cache_handler.get_web_download_urls_copied());

if url.0.to_ascii_lowercase().contains("youtube.com/playlist") {
let videos = get_youtube_playlist_entries(&url.0).await?;
Expand Down Expand Up @@ -127,7 +128,11 @@ pub async fn download(ctxt: CommandCtxt<'_>, url: Word, options: DownloadFlags)
}
};

let media = timeout(Duration::from_secs(120), download_web_media(a, &url, opts)).await;
let media = timeout(
Duration::from_secs(120),
download_web_media(&a.reqwest_client, &url, opts),
)
.await;
match media {
Ok(Ok(m)) => {
let mut z_lock = z.lock().await;
Expand Down Expand Up @@ -222,7 +227,7 @@ pub async fn download(ctxt: CommandCtxt<'_>, url: Word, options: DownloadFlags)
))
.await?;
} else {
let result = download_web_media(ctxt.assyst().clone(), &url.0, opts).await?;
let result = download_web_media(&ctxt.assyst().reqwest_client, &url.0, opts).await?;

ctxt.reply((
result,
Expand Down
2 changes: 1 addition & 1 deletion assyst-core/src/command/services/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub async fn burntext(ctxt: CommandCtxt<'_>, text: Rest) -> anyhow::Result<()> {
age_restricted = true
)]
pub async fn r34(ctxt: CommandCtxt<'_>, tags: Option<Rest>) -> anyhow::Result<()> {
let result = get_random_r34(ctxt.assyst().clone(), &tags.unwrap_or(Rest(String::new())).0).await?;
let result = get_random_r34(&ctxt.assyst().reqwest_client, &tags.unwrap_or(Rest(String::new())).0).await?;
let reply = format!("{} (Score: **{}**)", result.file_url, result.score);

ctxt.reply(reply).await?;
Expand Down
5 changes: 1 addition & 4 deletions assyst-core/src/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use futures_util::{Stream, StreamExt};
use human_bytes::human_bytes;
use reqwest::{Client, StatusCode, Url};

use crate::assyst::Assyst;

pub const ABSOLUTE_INPUT_FILE_SIZE_LIMIT_BYTES: usize = 250_000_000;
static PROXY_NUM: AtomicUsize = AtomicUsize::new(0);

Expand Down Expand Up @@ -97,7 +95,7 @@ where

/// Attempts to download a resource from a URL.
pub async fn download_content(
assyst: &Assyst,
client: &Client,
url: &str,
limit: usize,
untrusted: bool,
Expand All @@ -118,7 +116,6 @@ pub async fn download_content(
];

let config = &CONFIG;
let client = &assyst.reqwest_client;

let url_p = Url::parse(url).map_err(DownloadError::Url)?;
let host = url_p.host_str().ok_or(DownloadError::NoHost)?;
Expand Down
2 changes: 1 addition & 1 deletion assyst-core/src/gateway_handler/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async fn get_filer_url(

if data.len() > guild_upload_limit as usize {
filer_url = upload_to_filer(
ctxt.assyst().clone(),
&ctxt.assyst().reqwest_client,
data.clone(),
get_sig(&data).unwrap_or(Type::PNG).as_mime(),
)
Expand Down
7 changes: 5 additions & 2 deletions assyst-core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ use std::sync::Arc;
use std::time::Duration;

use assyst_common::config::config::LoggingWebhook;
use assyst_common::config::CONFIG;
use assyst_common::config::{CONFIG, PATREON_REFRESH_LOCATION};
use assyst_common::pipe::{Pipe, GATEWAY_PIPE_PATH};
use assyst_common::util::tracing_init;
use assyst_common::{err, ok_or_break};
use assyst_flux_iface::FluxHandler;
use command::registry::register_interaction_commands;
use gateway_handler::handle_raw_event;
use gateway_handler::incoming_event::IncomingEvent;
use rest::patreon::init_patreon_refresh;
use rest::web_media_download::get_web_download_api_urls;
use task::tasks::refresh_web_download_urls::refresh_web_download_urls;
use task::tasks::reminders::handle_reminders;
Expand Down Expand Up @@ -95,6 +96,8 @@ async fn main() {
info!(
"Patreon synchronisation disabled in config.dev.disable_patreson_synchronisation, will only load admins as patrons"
);

init_patreon_refresh(std::fs::read_to_string(PATREON_REFRESH_LOCATION).unwrap_or_default()).await;
}

assyst.register_task(Task::new(
Expand Down Expand Up @@ -216,7 +219,7 @@ async fn main() {
});

info!("Caching web download API URLs");
let web_download_urls = get_web_download_api_urls(a.clone()).await.unwrap();
let web_download_urls = get_web_download_api_urls(&a.reqwest_client).await.unwrap();
info!("Got {} URLs to cache", web_download_urls.len());
debug!(?web_download_urls);
a.rest_cache_handler.set_web_download_urls(web_download_urls);
Expand Down
9 changes: 2 additions & 7 deletions assyst-core/src/rest/audio_identification.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use assyst_common::config::CONFIG;
use reqwest::multipart::{Form, Part};

use crate::assyst::ThreadSafeAssyst;
use reqwest::Client;

const NOT_SO_IDENTIFY_URL: &str = "https://notsobot.com/api/media/av/tools/identify";

Expand Down Expand Up @@ -31,11 +30,7 @@ pub mod notsoidentify {
}
}

pub async fn identify_song_notsoidentify(
assyst: ThreadSafeAssyst,
search: String,
) -> anyhow::Result<Vec<notsoidentify::Song>> {
let client = &assyst.reqwest_client;
pub async fn identify_song_notsoidentify(client: &Client, search: String) -> anyhow::Result<Vec<notsoidentify::Song>> {
let formdata = Form::new();
let formdata = formdata.part("url", Part::text(search));
Ok(client
Expand Down
6 changes: 3 additions & 3 deletions assyst-core/src/rest/charinfo.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::assyst::ThreadSafeAssyst;
use reqwest::Client;

static CHARINFO_URL: &str = "https://www.fileformat.info/info/unicode/char/";

pub async fn get_char_info(assyst: ThreadSafeAssyst, ch: char) -> anyhow::Result<(String, String)> {
pub async fn get_char_info(client: &Client, ch: char) -> anyhow::Result<(String, String)> {
let url = format!("{}{:x}", CHARINFO_URL, ch as u32);

Ok((assyst.reqwest_client.get(&url).send().await?.text().await?, url))
Ok((client.get(&url).send().await?.text().await?, url))
}

/// Attempts to extract the page title for charingo
Expand Down
8 changes: 3 additions & 5 deletions assyst-core/src/rest/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ use anyhow::Context;
use assyst_common::config::CONFIG;
use assyst_common::eval::{FakeEvalBody, FakeEvalImageResponse, FakeEvalMessageData};
use assyst_common::util::filetype::get_sig;
use reqwest::Client;
use twilight_model::channel::Message;

use crate::assyst::Assyst;

pub async fn fake_eval(
assyst: &Assyst,
client: &Client,
code: String,
accept_image: bool,
message: Option<&Message>,
args: Vec<String>,
) -> anyhow::Result<FakeEvalImageResponse> {
let response = assyst
.reqwest_client
let response = client
.post(format!("{}/eval", CONFIG.urls.eval))
.query(&[("returnBuffer", accept_image)])
.json(&FakeEvalBody {
Expand Down
13 changes: 5 additions & 8 deletions assyst-core/src/rest/filer.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
use anyhow::Context;
use assyst_common::config::CONFIG;
use reqwest::Client;
use serde::Deserialize;

use crate::assyst::ThreadSafeAssyst;

#[derive(Deserialize)]
pub struct FilerStats {
pub count: u64,
pub size_bytes: u64,
}

pub async fn get_filer_stats(assyst: ThreadSafeAssyst) -> anyhow::Result<FilerStats> {
Ok(assyst
.reqwest_client
pub async fn get_filer_stats(client: &Client) -> anyhow::Result<FilerStats> {
Ok(client
.get(&format!("{}/stats", CONFIG.urls.filer))
.send()
.await?
.json::<FilerStats>()
.await?)
}

pub async fn upload_to_filer(assyst: ThreadSafeAssyst, data: Vec<u8>, content_type: &str) -> anyhow::Result<String> {
Ok(assyst
.reqwest_client
pub async fn upload_to_filer(client: &Client, data: Vec<u8>, content_type: &str) -> anyhow::Result<String> {
Ok(client
.post(&CONFIG.urls.filer)
.header(reqwest::header::CONTENT_TYPE, content_type)
.header(reqwest::header::AUTHORIZATION, &CONFIG.authentication.filer_key)
Expand Down
Loading

0 comments on commit 99adec4

Please sign in to comment.