From a359b6656fb8871eb3af9417c9474a0760d260e6 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 3 Jul 2020 10:06:24 +0200 Subject: [PATCH] Add post with images support --- src/api_data/post_api.rs | 37 +++++++++++++++++++++++++++++++++++-- src/data/post.rs | 34 ++++++++++++++++++++++++++++++++++ src/helpers/posts_helper.rs | 29 +++++++++++++++++++++++++---- 3 files changed, 94 insertions(+), 6 deletions(-) diff --git a/src/api_data/post_api.rs b/src/api_data/post_api.rs index 11b164e..8db3f9f 100644 --- a/src/api_data/post_api.rs +++ b/src/api_data/post_api.rs @@ -3,8 +3,9 @@ //! @author Pierre Hubert use serde::Serialize; -use crate::data::post::Post; +use crate::data::post::{Post, PostKind}; use crate::data::user::UserID; +use crate::utils::user_data_utils::user_data_url; #[derive(Serialize)] #[allow(non_snake_case)] @@ -16,12 +17,19 @@ pub struct PostAPI { post_time: u64, content: Option, visibility_level: String, + kind: String, + + // File specific + file_size: Option, + file_type: Option, + file_path: Option, + file_path_url: Option, } impl PostAPI { /// Turn a `Post` entry into an API entry pub fn new(p: &Post) -> PostAPI { - PostAPI { + let mut post = PostAPI { ID: p.id, userID: p.user_id.id(), user_page_id: p.user_page_id().unwrap_or(&UserID::invalid()).id(), @@ -29,7 +37,32 @@ impl PostAPI { post_time: p.time_create, content: p.content.clone(), visibility_level: p.visibility.to_api(), + kind: p.kind.to_api(), + + // File specific + file_size: None, + file_type: None, + file_path: None, + file_path_url: None, + }; + + match &p.kind { + PostKind::POST_KIND_TEXT => { /* do nothing */ } + PostKind::POST_KIND_IMAGE(file) => { + post.file_size = Option::from(file.size); + post.file_type = file.file_type.clone(); + post.file_path = Some(file.path.clone()); + post.file_path_url = Some(user_data_url(file.path.as_ref())) + } + PostKind::POST_KIND_WEBLINK => {} + PostKind::POST_KIND_PDF => {} + PostKind::POST_KIND_MOVIE => {} + PostKind::POST_KIND_COUNTDOWN => {} + PostKind::POST_KIND_SURVEY => {} + PostKind::POST_KIND_YOUTUBE => {} } + + post } /// Turn a list of posts into an API entry diff --git a/src/data/post.rs b/src/data/post.rs index 5de1e8e..a34256b 100644 --- a/src/data/post.rs +++ b/src/data/post.rs @@ -37,6 +37,39 @@ pub enum PostPageKind { PAGE_KIND_GROUP(GroupID), } +#[allow(non_camel_case_types)] +pub enum PostKind { + POST_KIND_TEXT, + POST_KIND_IMAGE(PostFile), + POST_KIND_WEBLINK, + POST_KIND_PDF, + POST_KIND_MOVIE, + POST_KIND_COUNTDOWN, + POST_KIND_SURVEY, + POST_KIND_YOUTUBE, +} + +impl PostKind { + pub fn to_api(&self) -> String { + match self { + PostKind::POST_KIND_TEXT => "text", + PostKind::POST_KIND_IMAGE(_) => "image", + PostKind::POST_KIND_WEBLINK => "weblink", + PostKind::POST_KIND_PDF => "pdf", + PostKind::POST_KIND_MOVIE => "movie", + PostKind::POST_KIND_COUNTDOWN => "countdown", + PostKind::POST_KIND_SURVEY => "survey", + PostKind::POST_KIND_YOUTUBE => "youtube", + }.to_string() + } +} + +pub struct PostFile { + pub path: String, + pub size: usize, + pub file_type: Option, +} + pub struct Post { pub id: u64, pub user_id: UserID, @@ -44,6 +77,7 @@ pub struct Post { pub target_page: PostPageKind, pub content: Option, pub visibility: PostVisibilityLevel, + pub kind: PostKind, } impl Post { diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index 7258771..a3430ec 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -3,8 +3,9 @@ //! @author Pierre Hubert use crate::constants::database_tables_names::POSTS_TABLE; -use crate::data::error::ResultBoxError; -use crate::data::post::{Post, PostPageKind, PostVisibilityLevel}; +use crate::data::error::{ExecError, ResultBoxError}; +use crate::data::post::{Post, PostFile, PostKind, PostPageKind, PostVisibilityLevel}; +use crate::data::post::PostKind::POST_KIND_IMAGE; use crate::data::user::UserID; use crate::helpers::{database, friends_helper}; use crate::utils::date_utils::time; @@ -135,7 +136,16 @@ fn db_to_post(res: &database::RowResult) -> ResultBoxError { PostPageKind::PAGE_KIND_GROUP(res.get_group_id("group_id")?) }; - Ok(Post { + let file = match res.get_optional_str("path")? { + None => None, + Some(path) => Some(PostFile { + path, + size: res.get_usize("size").unwrap_or(0), + file_type: res.get_optional_str("file_type")?, + }), + }; + + let mut post = Post { // General information id: res.get_u64("ID")?, user_id: user_id.clone(), @@ -143,5 +153,16 @@ fn db_to_post(res: &database::RowResult) -> ResultBoxError { target_page, content: res.get_optional_str("texte")?, visibility: PostVisibilityLevel::from_db(res.get_u32("niveau_visibilite")?), - }) + kind: PostKind::POST_KIND_TEXT, + }; + + let file = file.ok_or(ExecError::new("A file is required with this post type!")); + + match res.get_str("type")?.as_str() { + "image" => post.kind = POST_KIND_IMAGE(file?), + + _ => {} + } + + Ok(post) } \ No newline at end of file