1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-27 22:18:51 +00:00

Start account export

This commit is contained in:
Pierre HUBERT 2020-07-13 19:12:39 +02:00
parent f21636aa4e
commit 67dd29fe2d
8 changed files with 84 additions and 4 deletions

View File

@ -0,0 +1,33 @@
//! # Export account API entry
//!
//! @author Pierre Hubert
use serde::Serialize;
use crate::api_data::comment_api::CommentAPI;
use crate::api_data::post_api::PostAPI;
use crate::api_data::user_info::APIUserInfo;
use crate::data::account_export::AccountExport;
use crate::data::error::ResultBoxError;
#[derive(Serialize)]
#[allow(non_snake_case)]
pub struct AccountExportAPI {
userID: u64,
advanced_info: APIUserInfo,
posts: Vec<PostAPI>,
comments: Vec<CommentAPI>,
}
impl AccountExportAPI {
pub fn new(export: &AccountExport) -> ResultBoxError<AccountExportAPI> {
let curr_user_id = &export.user.id;
let export = AccountExportAPI {
userID: curr_user_id.id(),
advanced_info: APIUserInfo::new_advanced_info(&curr_user_id.as_option(), &export.user)?,
posts: PostAPI::for_list(&export.posts, curr_user_id.as_option())?,
comments: CommentAPI::for_list(&export.comments, &curr_user_id.as_option())?,
};
Ok(export)
}
}

View File

@ -46,4 +46,5 @@ mod type_container_api;
pub mod res_check_email_exists;
pub mod res_check_security_questions_exists;
pub mod res_get_security_questions;
pub mod res_check_security_answers;
pub mod res_check_security_answers;
pub mod account_export_api;

View File

@ -1,5 +1,6 @@
use percent_encoding::percent_decode_str;
use crate::api_data::account_export_api::AccountExportAPI;
use crate::api_data::current_user_id::CurrentUserID;
use crate::api_data::login_success::LoginSuccess;
use crate::api_data::res_check_email_exists::ResCheckEmailExists;
@ -184,5 +185,7 @@ pub fn reset_user_password(r: &mut HttpRequestHandler) -> RequestResult {
pub fn export_data(r: &mut HttpRequestHandler) -> RequestResult {
r.need_user_password("password")?;
r.success("Go on")
let data = account_helper::export(r.user_id_ref()?)?;
r.set_response(AccountExportAPI::new(&data)?)
}

View File

@ -0,0 +1,13 @@
//! # Export of all account's data
//!
//! @author Pierre Hubert
use crate::data::comment::Comment;
use crate::data::post::Post;
use crate::data::user::User;
pub struct AccountExport {
pub user: User,
pub posts: Vec<Post>,
pub comments: Vec<Comment>,
}

View File

@ -26,4 +26,5 @@ pub mod comment;
pub mod new_survey;
pub mod notification;
pub mod user_membership;
pub mod new_account;
pub mod new_account;
pub mod account_export;

View File

@ -5,10 +5,11 @@ use crate::data::error::{ExecError, ResultBoxError};
use crate::data::new_account::NewAccount;
use crate::data::user::UserID;
use crate::data::user_token::UserAccessToken;
use crate::helpers::{database, user_helper};
use crate::helpers::{database, user_helper, posts_helper, comments_helper};
use crate::helpers::database::{DeleteQuery, InsertQuery, QueryInfo};
use crate::utils::crypt_utils::{crypt_pass, rand_str};
use crate::utils::date_utils::{mysql_date, time};
use crate::data::account_export::AccountExport;
/// Account helper
///
@ -188,4 +189,15 @@ pub fn update_last_activity(user_id: &UserID) -> ResultBoxError {
.cond_user_id("ID", user_id)
.set_u64("last_activity", time())
.exec()
}
/// Export an account's data
pub fn export(user_id: &UserID) -> ResultBoxError<AccountExport> {
let data = AccountExport {
user: user_helper::find_user_by_id(user_id)?,
posts: posts_helper::export_all_posts_user(user_id)?,
comments: comments_helper::export_all_user(user_id)?
};
Ok(data)
}

View File

@ -5,6 +5,7 @@
use crate::constants::database_tables_names::COMMENTS_TABLE;
use crate::data::comment::Comment;
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::user::UserID;
use crate::helpers::{database, likes_helper};
use crate::helpers::likes_helper::LikeType;
use crate::utils::date_utils::mysql_date;
@ -40,6 +41,13 @@ pub fn get_single(comment_id: u64) -> ResultBoxError<Comment> {
.query_row(db_to_comment)
}
/// Export all the comments of a given user
pub fn export_all_user(user_id: &UserID) -> ResultBoxError<Vec<Comment>> {
database::QueryInfo::new(COMMENTS_TABLE)
.cond_user_id("ID_personne", user_id)
.exec(db_to_comment)
}
/// Turn a database entry into a comment object
fn db_to_comment(row: &database::RowResult) -> ResultBoxError<Comment> {

View File

@ -325,6 +325,15 @@ pub fn get_single(post_id: u64) -> ResultBoxError<Post> {
.query_row(db_to_post)
}
/// Get the entire list of posts of a given user + the posts created on user's page
pub fn export_all_posts_user(user_id: &UserID) -> ResultBoxError<Vec<Post>> {
database::QueryInfo::new(POSTS_TABLE)
.set_custom_where("ID_personne = ? OR ID_amis = ?")
.add_custom_where_argument_user_id(user_id)
.add_custom_where_argument_user_id(user_id)
.exec(db_to_post)
}
/// Get the access level of a user over a post
pub fn get_access_level(p: &Post, user_id: &Option<UserID>) -> ResultBoxError<PostAccessLevel> {
if user_id == &p.user_id.as_option() {