This commit is contained in:
parent
cd4096b2ff
commit
a0afb6dfd3
24 changed files with 536 additions and 274 deletions
31
src/main.rs
31
src/main.rs
|
|
@ -17,19 +17,19 @@ use tower_sessions_sqlx_store::SqliteStore;
|
|||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
mod models;
|
||||
use models::contact::ContactTrie;
|
||||
use models::contact::MentionTrie;
|
||||
use models::user::{Backend, User};
|
||||
|
||||
mod db;
|
||||
use db::{Database, DbId};
|
||||
|
||||
mod web;
|
||||
use web::{auth, contact, home, ics, journal, settings};
|
||||
use web::{auth, contact, group, home, ics, journal, settings};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppStateEntry {
|
||||
database: Arc<Database>,
|
||||
contact_search: Arc<RwLock<ContactTrie>>,
|
||||
contact_search: Arc<RwLock<MentionTrie>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
|
@ -50,7 +50,7 @@ impl AppState {
|
|||
pub async fn init(&mut self, user: &User) -> Result<Option<AppStateEntry>, AppError> {
|
||||
let database = Database::for_user(&user).await?;
|
||||
let mut trie = radix_trie::Trie::new();
|
||||
let rows = sqlx::query_as!(
|
||||
let mentionable_names = sqlx::query_as!(
|
||||
NameReference,
|
||||
"select name, contact_id from (
|
||||
select contact_id, name, count(name) as ct from names group by name
|
||||
|
|
@ -59,8 +59,21 @@ impl AppState {
|
|||
.fetch_all(&database.pool)
|
||||
.await?;
|
||||
|
||||
for row in rows {
|
||||
trie.insert(row.name, DbId::try_from(row.contact_id)?);
|
||||
for row in mentionable_names {
|
||||
trie.insert(
|
||||
row.name,
|
||||
format!("/contact/{}", DbId::try_from(row.contact_id)?),
|
||||
);
|
||||
}
|
||||
|
||||
let groups: Vec<(String, String)> =
|
||||
sqlx::query_as("select distinct name, slug from groups")
|
||||
.fetch_all(&database.pool)
|
||||
.await?;
|
||||
|
||||
for (group, slug) in groups {
|
||||
// TODO urlencode
|
||||
trie.insert(group, format!("/group/{}", slug));
|
||||
}
|
||||
|
||||
let mut map = self.map.write().expect("rwlock poisoned");
|
||||
|
|
@ -78,10 +91,9 @@ impl AppState {
|
|||
}
|
||||
pub fn db(&self, user: &impl AuthUser<Id = DbId>) -> Arc<Database> {
|
||||
let map = self.map.read().expect("rwlock poisoned");
|
||||
|
||||
map.get(&user.id()).unwrap().database.clone()
|
||||
}
|
||||
pub fn contact_search(&self, user: &impl AuthUser<Id = DbId>) -> Arc<RwLock<ContactTrie>> {
|
||||
pub fn contact_search(&self, user: &impl AuthUser<Id = DbId>) -> Arc<RwLock<MentionTrie>> {
|
||||
let map = self.map.read().expect("rwlock poisoned");
|
||||
map.get(&user.id()).unwrap().contact_search.clone()
|
||||
}
|
||||
|
|
@ -165,7 +177,7 @@ async fn serve(port: &u32) -> Result<(), anyhow::Error> {
|
|||
.with(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
|
||||
format!(
|
||||
"{}=debug,tower_http=debug,axum=trace",
|
||||
"{}=debug,tower_http=debug,axum=trace,sqlx=debug",
|
||||
env!("CARGO_CRATE_NAME")
|
||||
)
|
||||
.into()
|
||||
|
|
@ -177,6 +189,7 @@ async fn serve(port: &u32) -> Result<(), anyhow::Error> {
|
|||
let app = Router::new()
|
||||
.route("/", get(home::get::home))
|
||||
.merge(contact::router())
|
||||
.merge(group::router())
|
||||
.merge(journal::router())
|
||||
.merge(settings::router())
|
||||
.route_layer(login_required!(Backend, login_url = "/login"))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue