Remove the bot views from the visitors counter

master
kirbylife 2024-06-14 18:28:36 -06:00
parent bbba1ead64
commit ad519d80f7
3 changed files with 37 additions and 7 deletions

View File

@ -14,3 +14,5 @@ chrono = { version = "0.4.9", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
comrak = "0.24.1"
tera = "1.19.1"
isbot = "0.1.3"
lazy_static = "1.4.0"

View File

@ -13,13 +13,39 @@ extern crate diesel;
use comrak::{markdown_to_html, Options};
use controllers::posts;
use dotenv::dotenv;
use isbot::Bots;
use lazy_static::lazy_static;
use rocket::fs::{FileServer, NamedFile};
use rocket::http::Status;
use rocket::request::{FromRequest, Outcome, Request};
use rocket::response::content::RawXml;
use rocket::Request;
use rocket_dyn_templates::Template;
use std::path::Path;
use std::vec::Vec;
lazy_static! {
static ref ISBOT: Bots = Bots::default();
}
struct Headers {
user_agent: String,
}
#[rocket::async_trait]
impl<'r> FromRequest<'r> for Headers {
type Error = ();
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
let token = request.headers().get_one("User-Agent");
match token {
Some(token) => Outcome::Success(Headers {
user_agent: token.to_string(),
}),
None => Outcome::Error((Status::Unauthorized, ())),
}
}
}
#[get("/?<page>")]
fn index(page: Option<u64>) -> Template {
let page: u64 = page.unwrap_or(1);
@ -72,13 +98,15 @@ fn atom_feed() -> RawXml<Template> {
}
#[get("/post/<title>")]
fn show_post(title: String) -> Template {
fn show_post(title: String, headers: Headers) -> Template {
let title_splited: Vec<&str> = title.split('-').collect();
let id = title_splited.last().unwrap().parse().unwrap_or(-1);
match posts::get_post(id) {
Ok(mut post) => {
if !ISBOT.is_bot(&headers.user_agent) {
posts::add_visit(id);
}
let mut comrak_options = Options::default();
comrak_options.extension.table = true;