Skip to content

Commit

Permalink
Fix double slashes in Atom feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
hongquan committed Sep 9, 2023
1 parent 961b866 commit 4f2d482
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/front/views/feeds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::consts::DEFAULT_PAGE_SIZE;
use crate::errors::PageError;
use crate::models::feeds::{JsonFeed, JsonItem, EntryExt, DEFAULT_SITE_URL};
use crate::stores;
use crate::types::Paginator;
use crate::types::{Paginator, ext::UriExt};

// Generate from Python: uuid.uuid5(uuid.NAMESPACE_DNS, 'quan.hoabinh.vn'
const SITE_UUID: &str = "4543aea6-ab17-5c18-9279-19e73529594d";
Expand All @@ -39,21 +39,21 @@ pub async fn gen_atom_feeds(
current_page,
total_pages,
};
let self_url = format!("{base_url}{current_url}");
let self_url = base_url.join(&current_url.to_string()).to_string();
let first_page_url = paginator.first_url(&current_url);
let last_page_url = paginator.last_url(&current_url);
let next_page_url = paginator.next_url(&current_url);
let prev_page_url = paginator.previous_url(&current_url);
let mut links = vec![
LinkBuilder::default().rel("self".to_string()).href(self_url).build(),
LinkBuilder::default().rel("first".to_string()).href(format!("{base_url}{first_page_url}")).build(),
LinkBuilder::default().rel("last".to_string()).href(format!("{base_url}{last_page_url}")).build(),
LinkBuilder::default().rel("first".to_string()).href(base_url.join(&first_page_url).to_string()).build(),
LinkBuilder::default().rel("last".to_string()).href(base_url.join(&last_page_url).to_string()).build(),
];
if let Some(url) = next_page_url {
links.push(LinkBuilder::default().rel("next".to_string()).href(format!("{base_url}{url}")).build())
links.push(LinkBuilder::default().rel("next".to_string()).href(base_url.join(&url).to_string()).build())
}
if let Some(url) = prev_page_url {
links.push(LinkBuilder::default().rel("previous".to_string()).href(format!("{base_url}{url}")).build())
links.push(LinkBuilder::default().rel("previous".to_string()).href(base_url.join(&url).to_string()).build())
}
let mut entries: Vec<Entry> = posts.into_iter().map(Entry::from).collect();
entries.iter_mut().for_each(|e| e.prepend_url(&base_url));
Expand Down
3 changes: 2 additions & 1 deletion src/models/feeds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use http::Uri;
use serde::Serialize;
use atom_syndication::Entry;

use crate::types::ext::UriExt;
pub const DEFAULT_SITE_URL: &str = "https://quan.hoabinh.vn";

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -68,7 +69,7 @@ impl EntryExt for Entry {
self.links.iter_mut().for_each(|u| {
let old_url = u.href();
if old_url.starts_with("/") {
u.set_href(format!("{base_url}{old_url}"));
u.set_href(base_url.join(old_url).to_string());
}
});
}
Expand Down
27 changes: 27 additions & 0 deletions src/types/ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Traits to extend the types from the external crates

use std::str::FromStr;

use http::uri::{Uri, PathAndQuery, Builder};

pub trait UriExt {
fn join(&self, path: &str) -> Uri;
}

impl UriExt for Uri {
fn join(&self, to_join: &str) -> Uri {
let path_alone = self.path();
let new_path = format!("{}{}", path_alone, to_join).replace("//", "/");
let mut builder = Builder::new();
if let Some(scheme) = self.scheme() {
builder = builder.scheme(scheme.as_str());
}
if let Some(authority) = self.authority() {
builder = builder.authority(authority.as_str());
}
if let Ok(p_and_q) = PathAndQuery::from_str(new_path.as_str()) {
builder = builder.path_and_query(p_and_q);
}
builder.build().unwrap_or(self.clone())
}
}
1 change: 1 addition & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod conversions;
pub mod ext;
#[cfg(test)]
pub mod tests;

Expand Down

0 comments on commit 4f2d482

Please sign in to comment.