1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +00:00

Ready to implement account image visibility

This commit is contained in:
Pierre HUBERT 2020-05-26 13:53:24 +02:00
parent d355f33cb0
commit cca7f02f8e
6 changed files with 47 additions and 7 deletions

View File

@ -0,0 +1,7 @@
<component name="ProjectDictionaryState">
<dictionary name="pierre">
<words>
<w>comunic</w>
</words>
</dictionary>
</component>

View File

@ -3,7 +3,7 @@
//! @author Pierre Hubert
use serde::Serialize;
use crate::data::user::{User, UserPageStatus};
use crate::data::user::{User, UserPageStatus, UserID};
#[derive(Serialize)]
#[allow(non_snake_case)]
@ -14,17 +14,26 @@ pub struct APIUserInfo {
publicPage: bool,
openPage: bool,
virtualDirectory: String,
accountImage: String,
}
impl APIUserInfo {
pub fn new(info: User) -> APIUserInfo {
/// Construct a new API user instance. Note: `user_id` is the ID of the user who makes the
/// requests, not the user whose we return information about!
pub fn new(user_id: Option<UserID>, info: &User) -> APIUserInfo {
APIUserInfo {
userID: info.id,
firstName: info.first_name,
lastName: info.last_name,
firstName: info.first_name.to_string(),
lastName: info.last_name.to_string(),
publicPage: info.status != UserPageStatus::PRIVATE,
openPage: info.status == UserPageStatus::OPEN,
virtualDirectory: info.virtual_directory.unwrap_or("".to_string())
virtualDirectory: info.virtual_directory.clone().unwrap_or(String::new()),
accountImage: APIUserInfo::get_account_image_url(user_id, info),
}
}
/// Get the URL of a specific user account image
pub fn get_account_image_url(user_id: Option<UserID>, user: &User) -> String {
"do it".to_string()
}
}

View File

@ -21,5 +21,5 @@ pub fn get_single(request: &mut HttpRequestHandler) -> RequestResult {
}
};
request.set_response(APIUserInfo::new(user))
request.set_response(APIUserInfo::new(request.user_id_opt(), &user))
}

View File

@ -256,6 +256,11 @@ impl HttpRequestHandler {
}
}
/// Get a user ID, if available
pub fn user_id_opt(&self) -> Option<UserID> {
self.curr_user_id
}
/// Get an email included in the request
pub fn post_email(&mut self, name: &str) -> ResultBoxError<String> {
let mail = self.post_string(name)?;

View File

@ -11,6 +11,14 @@ pub enum UserPageStatus {
PRIVATE
}
#[derive(Debug, PartialEq)]
#[allow(non_camel_case_types)]
pub enum AccountImageVisibility {
FRIENDS,
COMUNIC_USERS,
EVERYONE
}
#[derive(Debug)]
pub struct User {
pub id: UserID,
@ -20,4 +28,6 @@ pub struct User {
pub last_name: String,
pub status: UserPageStatus,
pub virtual_directory: Option<String>,
pub account_image_path: Option<String>,
pub account_image_visibility: AccountImageVisibility,
}

View File

@ -1,5 +1,5 @@
use crate::data::error::ResultBoxError;
use crate::data::user::{User, UserID, UserPageStatus};
use crate::data::user::{User, UserID, UserPageStatus, AccountImageVisibility};
use crate::helpers::database;
use crate::database_structure::USERS_TABLE;
@ -32,6 +32,13 @@ fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError<User> {
UserPageStatus::PRIVATE
};
// Account image visibility
let account_image_visibility = match res.get_str("account_image_visibility")?.as_ref() {
"friends" => AccountImageVisibility::FRIENDS,
"comunic_users" => AccountImageVisibility::COMUNIC_USERS,
"everyone" => AccountImageVisibility::EVERYONE,
_ => unreachable!()
};
Ok(User {
id: res.get_int64("ID")?,
@ -41,6 +48,8 @@ fn exec_get_user_query(query: database::QueryInfo) -> ResultBoxError<User> {
last_name: res.get_str("nom")?,
status: page_status,
virtual_directory: res.get_optional_str("sous_repertoire")?,
account_image_path: res.get_optional_str("account_image_path")?,
account_image_visibility,
})
})
}