| 1 |
use serde::Deserialize; |
| 2 |
|
| 3 |
use crate::prelude::*; |
| 4 |
|
| 5 |
pub mod about; |
| 6 |
pub mod git; |
| 7 |
pub mod repository; |
| 8 |
pub mod r#static; |
| 9 |
|
| 10 |
const PAGE_SIZE: usize = 50; |
| 11 |
|
| 12 |
#[derive(Clone, Deserialize)] |
| 13 |
pub struct PageQuery { |
| 14 |
#[serde(rename = "h")] |
| 15 |
pub head: Option<String>, |
| 16 |
|
| 17 |
#[serde(rename = "qt")] |
| 18 |
pub query_type: Option<String>, |
| 19 |
|
| 20 |
#[serde(rename = "v")] |
| 21 |
pub value: Option<String>, |
| 22 |
|
| 23 |
pub id: Option<String>, |
| 24 |
|
| 25 |
#[serde(rename = "ofs")] |
| 26 |
pub offset: Option<usize>, |
| 27 |
} |
| 28 |
|
| 29 |
impl PageQuery { |
| 30 |
pub fn id_param(&self) -> String { |
| 31 |
match &self.id { |
| 32 |
Some(id) => format!("?id={id}"), |
| 33 |
None => String::new(), |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
pub fn head_param(&self) -> String { |
| 38 |
match (&self.id, &self.head) { |
| 39 |
(None, Some(head)) => format!("?h={head}"), |
| 40 |
_ => String::new(), |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
pub fn id_or_head_param(&self) -> String { |
| 45 |
if let Some(id) = &self.id { |
| 46 |
format!("?id={id}") |
| 47 |
} else if let Some(head) = &self.head { |
| 48 |
format!("?h={head}") |
| 49 |
} else { |
| 50 |
String::new() |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
struct RepoEntry { |
| 56 |
repo: Repository, |
| 57 |
last_time: Option<Time>, |
| 58 |
} |
| 59 |
|
| 60 |
#[derive(Template)] |
| 61 |
#[template(path = "repositorys.html")] |
| 62 |
struct ReposPage { |
| 63 |
repos: Vec<RepoEntry>, |
| 64 |
} |
| 65 |
|
| 66 |
#[get("/")] |
| 67 |
pub async fn page(query: web::Query<PageQuery>, data: web::Data<App>) -> ActixResult<HttpResponse> { |
| 68 |
let rendered = { |
| 69 |
let raw_repos: Vec<Repository> = { |
| 70 |
let guard = match data.repos.read() { |
| 71 |
Ok(g) => g, |
| 72 |
Err(e) => return Ok(HttpResponse::InternalServerError().body(e.to_string())), |
| 73 |
}; |
| 74 |
|
| 75 |
match (query.query_type.as_deref(), query.value.as_deref()) { |
| 76 |
(Some(qt), Some(value)) => { |
| 77 |
let value_lower = value.to_lowercase(); |
| 78 |
guard |
| 79 |
.iter() |
| 80 |
.filter(|r| { |
| 81 |
field_for(r, Some(qt)) |
| 82 |
.map(|f| f.to_lowercase().contains(&value_lower)) |
| 83 |
.unwrap_or(false) |
| 84 |
}) |
| 85 |
.cloned() |
| 86 |
.collect() |
| 87 |
} |
| 88 |
_ => guard.clone(), |
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
let mut repos: Vec<RepoEntry> = raw_repos |
| 94 |
.into_iter() |
| 95 |
.map(|repo| { |
| 96 |
let last_time = repo.last_commit_time(); |
| 97 |
RepoEntry { repo, last_time } |
| 98 |
}) |
| 99 |
.collect(); |
| 100 |
|
| 101 |
if let (qt, Some(value)) = (query.query_type.as_deref(), query.value.as_deref()) { |
| 102 |
let value_lower = value.to_lowercase(); |
| 103 |
repos.sort_by_key(|a| { |
| 104 |
( |
| 105 |
relevance(&a.repo, qt, &value_lower), |
| 106 |
std::cmp::Reverse( |
| 107 |
a.last_time |
| 108 |
.as_ref() |
| 109 |
.map(|t| t.seconds()) |
| 110 |
.unwrap_or(i64::MIN), |
| 111 |
), |
| 112 |
) |
| 113 |
}); |
| 114 |
} else { |
| 115 |
|
| 116 |
repos.sort_by_key(|a| { |
| 117 |
std::cmp::Reverse( |
| 118 |
a.last_time |
| 119 |
.as_ref() |
| 120 |
.map(|t| t.seconds()) |
| 121 |
.unwrap_or(i64::MIN), |
| 122 |
) |
| 123 |
}); |
| 124 |
} |
| 125 |
ReposPage { repos }.render() |
| 126 |
}; |
| 127 |
match rendered { |
| 128 |
Ok(html) => Ok(HttpResponse::Ok().content_type("text/html").body(html)), |
| 129 |
Err(e) => Ok(HttpResponse::InternalServerError().body(e.to_string())), |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
fn field_for<'a>(repo: &'a Repository, qt: Option<&str>) -> Option<&'a str> { |
| 134 |
match qt { |
| 135 |
Some("description") => Some(&repo.description), |
| 136 |
_ => Some(&repo.name), |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
fn relevance(repo: &Repository, qt: Option<&str>, value_lower: &str) -> u8 { |
| 142 |
match field_for(repo, qt) { |
| 143 |
Some(field) => { |
| 144 |
let field_lower = field.to_lowercase(); |
| 145 |
if field_lower == value_lower { |
| 146 |
0 |
| 147 |
} else if field_lower.starts_with(value_lower) { |
| 148 |
1 |
| 149 |
} else { |
| 150 |
2 |
| 151 |
} |
| 152 |
} |
| 153 |
None => 3, |
| 154 |
} |
| 155 |
} |