asklyphe/authservice/src/db/deranklist.rs
husky 08903aa0a1
All checks were successful
/ build-all-services (push) Successful in 9m57s
initial nats routes for deranking + authservice
currently only routes for fetching deranks, they'll have to be manually
inserted into the database at the moment until we add further routes
2025-03-18 16:55:58 -07:00

39 lines
No EOL
1.7 KiB
Rust

use log::error;
use sea_orm::ColumnTrait;
use sea_orm::{DatabaseConnection, EntityTrait, QueryFilter};
use asklyphe_common::nats::authservice::deranklist::DerankEntry;
use entity::{derank_list_entry, derank_list_subscription};
/// given a user id, finds and returns all active derank list entries, regardless of list
/// will just return an empty list if no derank entries exist for that user, should only
/// return an error if the database returns an error
pub async fn user_all_active_derank_entries(db: &DatabaseConnection, user_id: String) -> Result<Vec<DerankEntry>, ()> {
let user_subscribed_derank_lists = derank_list_subscription::Entity::find()
.filter(derank_list_subscription::Column::UserId.eq(user_id))
.filter(derank_list_subscription::Column::Active.eq(true))
.all(db).await
.map_err(|e| {
error!("DATABASE ERROR WHILE USERALLDERANKENTRIES (get user): {e}");
})?;
let mut derank_entries: Vec<DerankEntry> = vec![];
for list in user_subscribed_derank_lists {
if list.active {
// should always be true but just being safe
derank_entries.extend(derank_list_entry::Entity::find()
.filter(derank_list_entry::Column::DerankListId.eq(&list.id))
.all(db).await.map_err(|e| {
error!("DATABASE ERROR WHILE USERALLDERANKENTRIES (get entries): {e}");
})?.into_iter().map(|v| {
DerankEntry {
urlmatch: v.url_match,
and: v.and,
unless: v.unless,
multiplier: v.multiplier,
comment: v.comment,
}
}));
}
}
Ok(derank_entries)
}