feat: mentions in lives_with and text_body fields
Some checks failed
/ integration-test--firefox (push) Failing after 3m7s
Some checks failed
/ integration-test--firefox (push) Failing after 3m7s
This commit is contained in:
parent
fd5f1899c1
commit
d42adbe274
10 changed files with 369 additions and 200 deletions
45
src/main.rs
45
src/main.rs
|
|
@ -17,7 +17,6 @@ use tower_sessions_sqlx_store::SqliteStore;
|
|||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
mod models;
|
||||
use models::contact::MentionTrie;
|
||||
use models::user::{Backend, User};
|
||||
|
||||
mod db;
|
||||
|
|
@ -26,10 +25,13 @@ use db::{Database, DbId};
|
|||
mod web;
|
||||
use web::{auth, contact, group, home, ics, journal, settings};
|
||||
|
||||
mod switchboard;
|
||||
use switchboard::Switchboard;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppStateEntry {
|
||||
database: Arc<Database>,
|
||||
contact_search: Arc<RwLock<MentionTrie>>,
|
||||
switchboard: Arc<RwLock<Switchboard>>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
|
@ -37,10 +39,6 @@ struct AppState {
|
|||
map: Arc<RwLock<HashMap<DbId, AppStateEntry>>>,
|
||||
}
|
||||
|
||||
struct NameReference {
|
||||
name: String,
|
||||
contact_id: DbId,
|
||||
}
|
||||
impl AppState {
|
||||
pub fn new() -> Self {
|
||||
AppState {
|
||||
|
|
@ -49,39 +47,14 @@ 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 mentionable_names = sqlx::query_as!(
|
||||
NameReference,
|
||||
"select name, contact_id from (
|
||||
select contact_id, name, count(name) as ct from names group by name
|
||||
) where ct = 1;",
|
||||
)
|
||||
.fetch_all(&database.pool)
|
||||
.await?;
|
||||
|
||||
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 switchboard = Switchboard::new(&database.pool).await?;
|
||||
|
||||
let mut map = self.map.write().expect("rwlock poisoned");
|
||||
Ok(map.insert(
|
||||
user.id(),
|
||||
crate::AppStateEntry {
|
||||
database: Arc::new(database),
|
||||
contact_search: Arc::new(RwLock::new(trie)),
|
||||
switchboard: Arc::new(RwLock::new(switchboard)),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
|
@ -93,9 +66,9 @@ impl AppState {
|
|||
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<MentionTrie>> {
|
||||
pub fn switchboard(&self, user: &impl AuthUser<Id = DbId>) -> Arc<RwLock<Switchboard>> {
|
||||
let map = self.map.read().expect("rwlock poisoned");
|
||||
map.get(&user.id()).unwrap().contact_search.clone()
|
||||
map.get(&user.id()).unwrap().switchboard.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -177,7 +150,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,sqlx=debug",
|
||||
"{}=debug,tower_http=debug,axum=trace",
|
||||
env!("CARGO_CRATE_NAME")
|
||||
)
|
||||
.into()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue