]> Untitled Git - lemmy.git/commitdiff
Adding a user agent. Fixes #1769 (#1800)
authorDessalines <dessalines@users.noreply.github.com>
Wed, 29 Sep 2021 20:05:38 +0000 (16:05 -0400)
committerGitHub <noreply@github.com>
Wed, 29 Sep 2021 20:05:38 +0000 (16:05 -0400)
* Adding a user agent. Fixes #1769

* Fix client duplicate creation.

crates/apub/src/activity_queue.rs
crates/utils/src/request.rs
src/main.rs

index f05c4fce434c0078e16f124c6dea162ae55ba780..3359df5d2b773a9b0ff3872cbf0130a02e64c006 100644 (file)
@@ -89,7 +89,7 @@ where
       private_key: actor.private_key().context(location_info!())?,
     };
     if env::var("LEMMY_TEST_SEND_SYNC").is_ok() {
-      do_send(message, &Client::default()).await?;
+      do_send(message, context.client()).await?;
     } else {
       context.activity_queue.queue::<SendActivityTask>(message)?;
     }
index f143b1781e9510021478f05e3f085be101068a15..d8bd11a3adfdd138cdb9ac7428958eea8cf994ef 100644 (file)
@@ -1,4 +1,4 @@
-use crate::{settings::structs::Settings, LemmyError};
+use crate::{settings::structs::Settings, version::VERSION, LemmyError};
 use anyhow::anyhow;
 use log::error;
 use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
@@ -216,17 +216,30 @@ async fn is_image_content_type(client: &Client, test: &Url) -> Result<(), LemmyE
   }
 }
 
+pub fn build_user_agent(settings: &Settings) -> String {
+  format!(
+    "Lemmy/{}; +{}",
+    VERSION,
+    settings.get_protocol_and_hostname()
+  )
+}
+
 #[cfg(test)]
 mod tests {
-  use crate::request::fetch_site_metadata;
+  use crate::request::{build_user_agent, fetch_site_metadata};
   use url::Url;
 
   use super::SiteMetadata;
+  use crate::settings::structs::Settings;
 
   // These helped with testing
   #[actix_rt::test]
   async fn test_site_metadata() {
-    let client = reqwest::Client::default();
+    let settings = Settings::init().unwrap();
+    let client = reqwest::Client::builder()
+      .user_agent(build_user_agent(&settings))
+      .build()
+      .unwrap();
     let sample_url = Url::parse("https://www.redspark.nu/en/peoples-war/district-leader-of-chand-led-cpn-arrested-in-bhojpur/").unwrap();
     let sample_res = fetch_site_metadata(&client, &sample_url).await.unwrap();
     assert_eq!(
index 4e827d72a0842065b1b2be27aab54b1ee07ec2d8..4f436d2cb9b24b2cc3568771da15a2fe51bb712f 100644 (file)
@@ -17,6 +17,7 @@ use lemmy_routes::{feeds, images, nodeinfo, webfinger};
 use lemmy_server::{api_routes, code_migrations::run_advanced_migrations, scheduled_tasks};
 use lemmy_utils::{
   rate_limit::{rate_limiter::RateLimiter, RateLimit},
+  request::build_user_agent,
   settings::structs::Settings,
   LemmyError,
 };
@@ -72,6 +73,10 @@ async fn main() -> Result<(), LemmyError> {
     settings.bind, settings.port
   );
 
+  let client = Client::builder()
+    .user_agent(build_user_agent(&settings))
+    .build()?;
+
   let activity_queue = create_activity_queue();
 
   let chat_server = ChatServer::startup(
@@ -79,7 +84,7 @@ async fn main() -> Result<(), LemmyError> {
     rate_limiter.clone(),
     |c, i, o, d| Box::pin(match_websocket_operation(c, i, o, d)),
     |c, i, o, d| Box::pin(match_websocket_operation_crud(c, i, o, d)),
-    Client::default(),
+    client.clone(),
     activity_queue.clone(),
     settings.clone(),
     secret.clone(),
@@ -92,7 +97,7 @@ async fn main() -> Result<(), LemmyError> {
     let context = LemmyContext::create(
       pool.clone(),
       chat_server.to_owned(),
-      Client::default(),
+      client.clone(),
       activity_queue.to_owned(),
       settings.to_owned(),
       secret.to_owned(),