1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-02-07 09:47:04 +00:00
comunicapiv3/src/helpers/custom_emojies_helper.rs

45 lines
1.5 KiB
Rust
Raw Normal View History

2020-05-26 19:45:38 +02:00
//! # Custom emojies helper
//!
//! @author Pierre Hubert
2021-01-20 18:31:01 +01:00
use crate::constants::database_tables_names::EMOJIS_TABLE;
2020-05-26 19:45:38 +02:00
use crate::data::custom_emoji::CustomEmoji;
2021-01-20 18:31:01 +01:00
use crate::data::error::ResultBoxError;
use crate::data::new_custom_emoji::NewCustomEmoji;
use crate::data::user::UserID;
2020-05-26 19:45:38 +02:00
use crate::helpers::database;
/// Get the list of emojies of a user
2020-06-25 10:08:34 +02:00
pub fn get_list_user(user_id: &UserID) -> ResultBoxError<Vec<CustomEmoji>> {
2020-05-26 19:45:38 +02:00
database::QueryInfo::new(EMOJIS_TABLE)
2020-06-25 10:08:34 +02:00
.cond_user_id("user_id", user_id)
2020-05-26 19:45:38 +02:00
.exec(db_to_custom_emoji)
}
2021-01-20 18:31:01 +01:00
/// Check if a given user already as an emoji shortcut or not
pub fn has_user_similar_shortcut(user_id: &UserID, shortcut: &str) -> ResultBoxError<bool> {
database::QueryInfo::new(EMOJIS_TABLE)
.cond_user_id("user_id", user_id)
.cond("shortcut", shortcut)
.exec_count()
.map(|r| r > 0)
}
/// Insert a new emoji in the database
pub fn insert(emoji: &NewCustomEmoji) -> ResultBoxError<u64> {
database::InsertQuery::new(EMOJIS_TABLE)
.add_user_id("user_id", &emoji.user_id)
.add_str("shortcut", &emoji.shortcut)
.add_str("path", &emoji.path)
.insert_expect_result()
}
2020-05-26 19:45:38 +02:00
/// Turn a database entry into a [CustomEmoji]
fn db_to_custom_emoji(row: &database::RowResult) -> ResultBoxError<CustomEmoji> {
Ok(CustomEmoji {
id: row.get_u64("id")?,
2020-06-25 10:08:34 +02:00
user_id: row.get_user_id("user_id")?,
2020-05-26 19:45:38 +02:00
shortcut: row.get_str("shortcut")?,
2021-01-20 18:31:01 +01:00
path: row.get_str("path")?,
2020-05-26 19:45:38 +02:00
})
}