1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-25 23:09:22 +00:00

Get user custom emojies

This commit is contained in:
Pierre HUBERT 2020-05-26 19:45:38 +02:00
parent 2b1161c3f0
commit c4f8e5fa97
9 changed files with 106 additions and 4 deletions

View File

@ -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),
}
}
}

View File

@ -10,4 +10,5 @@
pub mod http_error; pub mod http_error;
pub mod login_success; pub mod login_success;
pub mod current_user_id; pub mod current_user_id;
pub mod user_info; pub mod user_info;
pub mod custom_emoji;

View File

@ -4,10 +4,11 @@
use serde::Serialize; use serde::Serialize;
use crate::data::user::{User, UserPageStatus, UserID}; 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::data::error::ResultBoxError;
use crate::utils::user_data_utils::user_data_url; use crate::utils::user_data_utils::user_data_url;
use crate::data::user::AccountImageVisibility::{EVERYONE, COMUNIC_USERS}; use crate::data::user::AccountImageVisibility::{EVERYONE, COMUNIC_USERS};
use crate::api_data::custom_emoji::CustomEmojiAPI;
#[derive(Serialize)] #[derive(Serialize)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
@ -19,6 +20,7 @@ pub struct APIUserInfo {
openPage: bool, openPage: bool,
virtualDirectory: String, virtualDirectory: String,
accountImage: String, accountImage: String,
customEmojis: Vec<CustomEmojiAPI>,
} }
impl APIUserInfo { impl APIUserInfo {
@ -33,6 +35,10 @@ impl APIUserInfo {
openPage: info.status == UserPageStatus::OPEN, openPage: info.status == UserPageStatus::OPEN,
virtualDirectory: info.virtual_directory.clone().unwrap_or(String::new()), virtualDirectory: info.virtual_directory.clone().unwrap_or(String::new()),
accountImage: APIUserInfo::get_account_image_url(user_id, info)?, 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(),
}) })
} }

View File

@ -17,6 +17,9 @@ pub mod database_tables_names {
/// Friends table /// Friends table
pub const FRIENDS_TABLE: &str = "amis"; 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 /// The account image to show for user who do not have any

13
src/data/custom_emoji.rs Normal file
View File

@ -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,
}

View File

@ -5,4 +5,5 @@ pub mod http_request_handler;
pub mod api_client; pub mod api_client;
pub mod user; pub mod user;
pub mod user_token; pub mod user_token;
pub mod custom_emoji;

View File

@ -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<Vec<CustomEmoji>> {
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<CustomEmoji> {
Ok(CustomEmoji {
id: row.get_u64("id")?,
user_id: row.get_int64("user_id")?,
shortcut: row.get_str("shortcut")?,
path: row.get_str("path")?
})
}

View File

@ -86,6 +86,11 @@ impl QueryInfo {
self 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 { pub fn cond_i64(mut self, key: &str, val: i64) -> QueryInfo {
self.conditions.insert(key.to_string(), val.to_string()); self.conditions.insert(key.to_string(), val.to_string());
self self
@ -96,6 +101,12 @@ impl QueryInfo {
self.fields.push(key.to_string()); self.fields.push(key.to_string());
self self
} }
/// Execute query
pub fn exec<E, F: Fn(&RowResult) -> ProcessRowResult<E>>(self, process_function: F)
-> Result<Vec<E>, Box<dyn Error>> {
query(self, process_function)
}
} }
/// Struct used to read the result of a request /// 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<u64, Box<dyn Error>> {
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 /// Find an integer included in the request
pub fn get_usize(&self, name: &str) -> Result<usize, Box<dyn Error>> { pub fn get_usize(&self, name: &str) -> Result<usize, Box<dyn Error>> {
let value = self.row.get_opt(self.find_col(name)?); let value = self.row.get_opt(self.find_col(name)?);

View File

@ -3,4 +3,5 @@ pub mod database;
pub mod api_helper; pub mod api_helper;
pub mod account_helper; pub mod account_helper;
pub mod user_helper; pub mod user_helper;
pub mod friends_helper; pub mod friends_helper;
pub mod custom_emojies_helper;