Skip to content

Commit

Permalink
chore: move consumed url logic to ForumURL util
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonVirgo committed Aug 3, 2024
1 parent 844688c commit e5d4e30
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
20 changes: 7 additions & 13 deletions src/routes/api/search_or_register_thread.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::models::thread::{create_thread, get_thread};
use crate::scraping::{
parser::{get_search_params, get_url_from_type, PageType, PostURL, ThreadURL, URLType},
scraper::get_page_details,
};
use crate::scraping::parser::get_search_params;
use crate::utils::app_state::AppState;
use crate::utils::url::ForumURL;
use actix_web::{
Expand All @@ -24,17 +21,14 @@ async fn search_or_register_thread(
) -> impl Responder {
let query_search_params = get_search_params(&form.url);

let invalid_url_response = html! { div."text-red-500" { "Invalid URL" } }.into_string();
let url = match (query_search_params.get("t"), query_search_params.get("p")) {
(Some(thread_id), _) => ForumURL::new(thread_id.to_string()),
(None, Some(post_id)) => ForumURL::new_from_post(post_id.to_string()),
_ => {
return HttpResponse::BadRequest().body(
html! {
div."text-red-500" { "Invalid URL" }
}
.into_string(),
)
}
(None, Some(post_id)) => match ForumURL::new_from_post(post_id.to_string()).await {
Some(url) => url,
None => return HttpResponse::BadRequest().body(invalid_url_response),
},
_ => return HttpResponse::BadRequest().body(invalid_url_response),
};

let page_data = match url.scrape().await {
Expand Down
18 changes: 15 additions & 3 deletions src/utils/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct ForumURL {

pub enum URLType {
Thread,
Post(String),
}

impl ForumURL {
Expand All @@ -19,16 +20,27 @@ impl ForumURL {
}
}

pub fn new_from_post(post_id: String) -> ForumURL {
ForumURL::new("thread_url".to_string())
pub async fn new_from_post(post_id: String) -> Option<ForumURL> {
match scraper::get_page_details(format!(
"https://forum.mafiascum.net/viewtopic.php?&p={}&ppp=1",
post_id
))
.await
{
Some(page) => Some(ForumURL::new(page.thread_id)),
None => return None,
}
}

pub fn url(&self, url_type: URLType) -> String {
match url_type {
URLType::Thread => format!(
"https://forum.mafiascum.com/t/{}/?ppp={}&start={}",
"https://forum.mafiascum.net/viewtopic.php?t={}&ppp={}&start={}",
self.thread_id, self.ppp, self.start
),
URLType::Post(post_id) => {
format!("https://forum.mafiascum.net/viewtopic.php?&p={}", post_id)
}
}
}

Expand Down

0 comments on commit e5d4e30

Please sign in to comment.