From c4f8e5fa97b9d3705ed2fb7c75786ee78c70aa3d Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 26 May 2020 19:45:38 +0200 Subject: [PATCH] Get user custom emojies --- src/api_data/custom_emoji.rs | 29 ++++++++++++++++++++++++++++ src/api_data/mod.rs | 3 ++- src/api_data/user_info.rs | 8 +++++++- src/constants.rs | 3 +++ src/data/custom_emoji.rs | 13 +++++++++++++ src/data/mod.rs | 3 ++- src/helpers/custom_emojies_helper.rs | 26 +++++++++++++++++++++++++ src/helpers/database.rs | 22 +++++++++++++++++++++ src/helpers/mod.rs | 3 ++- 9 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 src/api_data/custom_emoji.rs create mode 100644 src/data/custom_emoji.rs create mode 100644 src/helpers/custom_emojies_helper.rs diff --git a/src/api_data/custom_emoji.rs b/src/api_data/custom_emoji.rs new file mode 100644 index 0000000..0ad3702 --- /dev/null +++ b/src/api_data/custom_emoji.rs @@ -0,0 +1,29 @@ +//! # Custom emoji API object +//! +//! @author Pierre Hubert +use serde::Serialize; +use crate::data::custom_emoji::CustomEmoji; +use crate::utils::user_data_utils::user_data_url; + +#[derive(Serialize)] +#[allow(non_snake_case)] +pub struct CustomEmojiAPI { + id: u64, + userID: i64, + shortcut: String, + url: String +} + +impl CustomEmojiAPI { + + /// Create a new Custom Emoji API entry + pub fn new(custom_emoji: &CustomEmoji) -> CustomEmojiAPI { + CustomEmojiAPI { + id: custom_emoji.id, + userID: custom_emoji.user_id, + shortcut: custom_emoji.shortcut.to_string(), + url: user_data_url(&custom_emoji.path), + } + } + +} \ No newline at end of file diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 8d95291..143031b 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -10,4 +10,5 @@ pub mod http_error; pub mod login_success; pub mod current_user_id; -pub mod user_info; \ No newline at end of file +pub mod user_info; +pub mod custom_emoji; \ No newline at end of file diff --git a/src/api_data/user_info.rs b/src/api_data/user_info.rs index 068a41e..45a4584 100644 --- a/src/api_data/user_info.rs +++ b/src/api_data/user_info.rs @@ -4,10 +4,11 @@ use serde::Serialize; use crate::data::user::{User, UserPageStatus, UserID}; -use crate::helpers::friends_helper; +use crate::helpers::{friends_helper, custom_emojies_helper}; use crate::data::error::ResultBoxError; use crate::utils::user_data_utils::user_data_url; use crate::data::user::AccountImageVisibility::{EVERYONE, COMUNIC_USERS}; +use crate::api_data::custom_emoji::CustomEmojiAPI; #[derive(Serialize)] #[allow(non_snake_case)] @@ -19,6 +20,7 @@ pub struct APIUserInfo { openPage: bool, virtualDirectory: String, accountImage: String, + customEmojis: Vec, } impl APIUserInfo { @@ -33,6 +35,10 @@ impl APIUserInfo { openPage: info.status == UserPageStatus::OPEN, virtualDirectory: info.virtual_directory.clone().unwrap_or(String::new()), accountImage: APIUserInfo::get_account_image_url(user_id, info)?, + customEmojis: custom_emojies_helper::get_list_user(info.id)? + .iter() + .map(|f| CustomEmojiAPI::new(f)) + .collect(), }) } diff --git a/src/constants.rs b/src/constants.rs index 71e7f62..8fcaef0 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -17,6 +17,9 @@ pub mod database_tables_names { /// Friends table pub const FRIENDS_TABLE: &str = "amis"; + + /// Custom emojis table + pub const EMOJIS_TABLE: &str = "comunic_custom_emojis"; } /// The account image to show for user who do not have any diff --git a/src/data/custom_emoji.rs b/src/data/custom_emoji.rs new file mode 100644 index 0000000..58ac825 --- /dev/null +++ b/src/data/custom_emoji.rs @@ -0,0 +1,13 @@ +//! User custom emoji +//! +//! @author Pierre Hubert + +use crate::data::user::UserID; + +#[derive(Debug)] +pub struct CustomEmoji { + pub id: u64, + pub user_id: UserID, + pub shortcut: String, + pub path: String, +} \ No newline at end of file diff --git a/src/data/mod.rs b/src/data/mod.rs index 29dd6cf..ed56c43 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -5,4 +5,5 @@ pub mod http_request_handler; pub mod api_client; pub mod user; -pub mod user_token; \ No newline at end of file +pub mod user_token; +pub mod custom_emoji; \ No newline at end of file diff --git a/src/helpers/custom_emojies_helper.rs b/src/helpers/custom_emojies_helper.rs new file mode 100644 index 0000000..527f251 --- /dev/null +++ b/src/helpers/custom_emojies_helper.rs @@ -0,0 +1,26 @@ +//! # Custom emojies helper +//! +//! @author Pierre Hubert + +use crate::data::user::UserID; +use crate::data::error::ResultBoxError; +use crate::data::custom_emoji::CustomEmoji; +use crate::helpers::database; +use crate::constants::database_tables_names::EMOJIS_TABLE; + +/// Get the list of emojies of a user +pub fn get_list_user(user_id: UserID) -> ResultBoxError> { + database::QueryInfo::new(EMOJIS_TABLE) + .cond_i64("user_id", user_id) + .exec(db_to_custom_emoji) +} + +/// Turn a database entry into a [CustomEmoji] +fn db_to_custom_emoji(row: &database::RowResult) -> ResultBoxError { + Ok(CustomEmoji { + id: row.get_u64("id")?, + user_id: row.get_int64("user_id")?, + shortcut: row.get_str("shortcut")?, + path: row.get_str("path")? + }) +} \ No newline at end of file diff --git a/src/helpers/database.rs b/src/helpers/database.rs index 8e823be..1066b8a 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -86,6 +86,11 @@ impl QueryInfo { self } + pub fn cond_u64(mut self, key: &str, val: u64) -> QueryInfo { + self.conditions.insert(key.to_string(), val.to_string()); + self + } + pub fn cond_i64(mut self, key: &str, val: i64) -> QueryInfo { self.conditions.insert(key.to_string(), val.to_string()); self @@ -96,6 +101,12 @@ impl QueryInfo { self.fields.push(key.to_string()); self } + + /// Execute query + pub fn exec ProcessRowResult>(self, process_function: F) + -> Result, Box> { + query(self, process_function) + } } /// Struct used to read the result of a request @@ -136,6 +147,17 @@ impl<'a> RowResult<'a> { } } + /// Find an integer included in the request + pub fn get_u64(&self, name: &str) -> Result> { + let value = self.row.get_opt(self.find_col(name)?); + + match value { + None => Err(ExecError::boxed_string( + format!("Could not extract integer field {} !", name))), + Some(s) => Ok(s?) + } + } + /// Find an integer included in the request pub fn get_usize(&self, name: &str) -> Result> { let value = self.row.get_opt(self.find_col(name)?); diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index f65b104..55734c0 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -3,4 +3,5 @@ pub mod database; pub mod api_helper; pub mod account_helper; pub mod user_helper; -pub mod friends_helper; \ No newline at end of file +pub mod friends_helper; +pub mod custom_emojies_helper; \ No newline at end of file