chore (backend-rs): cleanup

This commit is contained in:
naskya 2024-05-20 19:14:02 +09:00
parent 847aa848f3
commit 84c374da20
No known key found for this signature in database
GPG Key ID: 712D413B3A9FED5C
15 changed files with 31 additions and 26 deletions

View File

@ -18,7 +18,7 @@ async fn init_database() -> Result<&'static DbConn, DbErr> {
.sqlx_logging_level(LevelFilter::Trace)
.to_owned();
tracing::info!("Initializing PostgreSQL connection");
tracing::info!("initializing connection");
let conn = Database::connect(option).await?;
Ok(DB_CONN.get_or_init(move || conn))

View File

@ -67,10 +67,10 @@ async fn init_conn_pool() -> Result<(), RedisError> {
params.concat()
};
tracing::info!("Initializing connection manager");
tracing::info!("initializing connection manager");
let manager = RedisConnectionManager::new(redis_url)?;
tracing::info!("Creating connection pool");
tracing::info!("creating connection pool");
let pool = Pool::builder().build(manager).await?;
CONN_POOL.get_or_init(|| async { pool }).await;

View File

@ -16,5 +16,5 @@ pub fn greet() {
println!("{}", GREETING_MESSAGE);
tracing::info!("Welcome to Firefish!");
tracing::info!("Firefish {VERSION}");
tracing::info!("Firefish v{VERSION}");
}

View File

@ -3,9 +3,9 @@ use crate::config::CONFIG;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Idna error: {0}")]
IdnaError(#[from] idna::Errors),
IdnaErr(#[from] idna::Errors),
#[error("Url parse error: {0}")]
UrlParseError(#[from] url::ParseError),
UrlParseErr(#[from] url::ParseError),
#[error("Hostname is missing")]
NoHostname,
}

View File

@ -1,4 +1,4 @@
#[inline]
#[crate::ts_only_warn("Use `emojis::get(str).is_some()` instead.")]
#[crate::export]
pub fn is_unicode_emoji(s: &str) -> bool {
emojis::get(s).is_some()
@ -6,9 +6,11 @@ pub fn is_unicode_emoji(s: &str) -> bool {
#[cfg(test)]
mod unit_test {
#[allow(deprecated)]
use super::is_unicode_emoji;
#[test]
#[allow(deprecated)]
fn test_unicode_emoji_check() {
assert!(is_unicode_emoji(""));
assert!(is_unicode_emoji("👍"));

View File

@ -69,7 +69,7 @@ pub async fn get_image_size_from_url(url: &str) -> Result<ImageSize, Error> {
return Err(Error::TooManyAttempts(url.to_string()));
}
tracing::info!("retrieving image size from {}", url);
tracing::info!("retrieving image from {}", url);
let mut response = http_client::client()?.get(url)?;

View File

@ -52,7 +52,7 @@ pub async fn all_texts(note: NoteLike) -> Result<Vec<String>, DbErr> {
texts.push(c);
}
} else {
tracing::warn!("nonexistent renote id: {:#?}", renote_id);
tracing::warn!("nonexistent renote id: {}", renote_id);
}
}
@ -71,7 +71,7 @@ pub async fn all_texts(note: NoteLike) -> Result<Vec<String>, DbErr> {
texts.push(c);
}
} else {
tracing::warn!("nonexistent reply id: {:#?}", reply_id);
tracing::warn!("nonexistent reply id: {}", reply_id);
}
}

View File

@ -1,5 +1,6 @@
use crate::model::entity::note;
// for napi export
// https://github.com/napi-rs/napi-rs/issues/2060
type Note = note::Model;

View File

@ -15,11 +15,11 @@ pub fn hash_password(password: &str) -> Result<String, password_hash::errors::Er
#[derive(thiserror::Error, Debug)]
pub enum VerifyError {
#[error("An error occured while bcrypt verification: {0}")]
BcryptError(#[from] bcrypt::BcryptError),
BcryptErr(#[from] bcrypt::BcryptError),
#[error("Invalid argon2 password hash: {0}")]
InvalidArgon2Hash(#[from] password_hash::Error),
#[error("An error occured while argon2 verification: {0}")]
Argon2Error(#[from] argon2::Error),
Argon2Err(#[from] argon2::Error),
}
#[crate::export]

View File

@ -1,5 +1,5 @@
use crate::database::db_conn;
use crate::misc::{convert_host::to_puny, emoji::is_unicode_emoji, meta::fetch_meta};
use crate::misc::{convert_host::to_puny, meta::fetch_meta};
use crate::model::entity::emoji;
use once_cell::sync::Lazy;
use regex::Regex;
@ -58,9 +58,9 @@ pub fn count_reactions(reactions: &HashMap<String, u32>) -> HashMap<String, u32>
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Idna error: {0}")]
IdnaError(#[from] idna::Errors),
IdnaErr(#[from] idna::Errors),
#[error("Database error: {0}")]
DbError(#[from] DbErr),
DbErr(#[from] DbErr),
}
#[crate::export]
@ -72,7 +72,8 @@ pub async fn to_db_reaction(reaction: Option<&str>, host: Option<&str>) -> Resul
return Ok("❤️".to_owned());
}
if is_unicode_emoji(reaction) {
// check if the reaction is an Unicode emoji
if emojis::get(reaction).is_some() {
return Ok(reaction.to_owned());
}

View File

@ -2,18 +2,18 @@
use crate::database::db_conn;
use crate::model::entity::attestation_challenge;
use chrono::{Duration, Local};
use chrono::{Duration, Utc};
use sea_orm::{ColumnTrait, DbErr, EntityTrait, QueryFilter};
/// Delete all entries in the "attestation_challenge" table created at more than 5 minutes ago
#[crate::export]
pub async fn remove_old_attestation_challenges() -> Result<(), DbErr> {
let res = attestation_challenge::Entity::delete_many()
.filter(attestation_challenge::Column::CreatedAt.lt(Local::now() - Duration::minutes(5)))
.filter(attestation_challenge::Column::CreatedAt.lt(Utc::now() - Duration::minutes(5)))
.exec(db_conn().await?)
.await?;
tracing::info!("{} attestation challenges are removed", res.rows_affected);
tracing::info!("removed {} rows", res.rows_affected);
Ok(())
}

View File

@ -26,6 +26,7 @@ pub enum Error {
AntennaCheckErr(#[from] AntennaCheckError),
}
// for napi export
// https://github.com/napi-rs/napi-rs/issues/2060
type Antenna = antenna::Model;
type Note = note::Model;

View File

@ -5,9 +5,9 @@ use serde::{Deserialize, Serialize};
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Http client aquisition error: {0}")]
#[error("HTTP client aquisition error: {0}")]
HttpClientErr(#[from] http_client::Error),
#[error("Http error: {0}")]
#[error("HTTP error: {0}")]
HttpErr(#[from] isahc::Error),
#[error("Bad status: {0}")]
BadStatus(String),

View File

@ -174,7 +174,7 @@ pub async fn send_push_notification(
serde_json::to_string(&compact_content(&kind, content.clone())?)?
)
};
tracing::trace!("payload: {:#?}", payload);
tracing::trace!("payload: {}", payload);
let encoding = if kind == PushNotificationKind::Mastodon {
ContentEncoding::AesGcm

View File

@ -48,13 +48,13 @@ pub enum Stream {
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Redis error: {0}")]
RedisError(#[from] RedisError),
RedisErr(#[from] RedisError),
#[error("Redis connection error: {0}")]
RedisConnErr(#[from] RedisConnError),
#[error("Json (de)serialization error: {0}")]
JsonError(#[from] serde_json::Error),
JsonErr(#[from] serde_json::Error),
#[error("Value error: {0}")]
ValueError(String),
ValueErr(String),
}
pub async fn publish_to_stream(
@ -69,7 +69,7 @@ pub async fn publish_to_stream(
value.unwrap_or("null".to_string()),
)
} else {
value.ok_or(Error::ValueError("Invalid streaming message".to_string()))?
value.ok_or(Error::ValueErr("Invalid streaming message".to_string()))?
};
redis_conn()