This repository has been archived on 2025-01-27. You can view files and clone it, but cannot push or open issues or pull requests.
puyoskey-firefish/packages/backend-rs/src/misc/should_nyaify.rs
naskya d096da02e6 🎉 First Commit
release: v20240729

Co-authored-by: Laura Hausmann <laura@hausmann.dev>
Co-authored-by: GitLab CI <project_7_bot_1bfaee5701aed20091a86249a967a6c1@noreply.firefish.dev>
Co-authored-by: Hosted Weblate <hosted@weblate.org>
Co-authored-by: Saamkhaih Kyakya <70475761+hiohlan@users.noreply.github.com>

See merge request firefish/firefish!11214
2024-08-01 00:03:39 +09:00

45 lines
1.1 KiB
Rust

//! Determine whether to enable the cat language conversion
use crate::{
database::{cache, db_conn},
model::entity::user,
};
use sea_orm::{DbErr, EntityTrait, QuerySelect, SelectColumns};
#[macros::errors]
pub enum Error {
#[doc = "database error"]
#[error(transparent)]
Db(#[from] DbErr),
#[doc = "cache error"]
#[error(transparent)]
Cache(#[from] cache::Error),
#[error("user {0} not found")]
NotFound(String),
}
#[macros::export]
pub async fn should_nyaify(reader_user_id: &str) -> Result<bool, Error> {
let cached_value = cache::get_one::<bool>(cache::Category::CatLang, reader_user_id).await?;
if let Some(value) = cached_value {
return Ok(value);
}
let fetched_value = user::Entity::find_by_id(reader_user_id)
.select_only()
.select_column(user::Column::ReadCatLanguage)
.into_tuple::<bool>()
.one(db_conn().await?)
.await?
.ok_or_else(|| Error::NotFound(reader_user_id.to_owned()))?;
cache::set_one(
cache::Category::CatLang,
reader_user_id,
&fetched_value,
10 * 60,
)
.await?;
Ok(fetched_value)
}