1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 07:19:22 +00:00

Add post with images support

This commit is contained in:
Pierre HUBERT 2020-07-03 10:06:24 +02:00
parent 2dde756d5d
commit a359b6656f
3 changed files with 94 additions and 6 deletions

View File

@ -3,8 +3,9 @@
//! @author Pierre Hubert //! @author Pierre Hubert
use serde::Serialize; use serde::Serialize;
use crate::data::post::Post; use crate::data::post::{Post, PostKind};
use crate::data::user::UserID; use crate::data::user::UserID;
use crate::utils::user_data_utils::user_data_url;
#[derive(Serialize)] #[derive(Serialize)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
@ -16,12 +17,19 @@ pub struct PostAPI {
post_time: u64, post_time: u64,
content: Option<String>, content: Option<String>,
visibility_level: String, visibility_level: String,
kind: String,
// File specific
file_size: Option<usize>,
file_type: Option<String>,
file_path: Option<String>,
file_path_url: Option<String>,
} }
impl PostAPI { impl PostAPI {
/// Turn a `Post` entry into an API entry /// Turn a `Post` entry into an API entry
pub fn new(p: &Post) -> PostAPI { pub fn new(p: &Post) -> PostAPI {
PostAPI { let mut post = PostAPI {
ID: p.id, ID: p.id,
userID: p.user_id.id(), userID: p.user_id.id(),
user_page_id: p.user_page_id().unwrap_or(&UserID::invalid()).id(), user_page_id: p.user_page_id().unwrap_or(&UserID::invalid()).id(),
@ -29,7 +37,32 @@ impl PostAPI {
post_time: p.time_create, post_time: p.time_create,
content: p.content.clone(), content: p.content.clone(),
visibility_level: p.visibility.to_api(), 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 /// Turn a list of posts into an API entry

View File

@ -37,6 +37,39 @@ pub enum PostPageKind {
PAGE_KIND_GROUP(GroupID), 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<String>,
}
pub struct Post { pub struct Post {
pub id: u64, pub id: u64,
pub user_id: UserID, pub user_id: UserID,
@ -44,6 +77,7 @@ pub struct Post {
pub target_page: PostPageKind, pub target_page: PostPageKind,
pub content: Option<String>, pub content: Option<String>,
pub visibility: PostVisibilityLevel, pub visibility: PostVisibilityLevel,
pub kind: PostKind,
} }
impl Post { impl Post {

View File

@ -3,8 +3,9 @@
//! @author Pierre Hubert //! @author Pierre Hubert
use crate::constants::database_tables_names::POSTS_TABLE; use crate::constants::database_tables_names::POSTS_TABLE;
use crate::data::error::ResultBoxError; use crate::data::error::{ExecError, ResultBoxError};
use crate::data::post::{Post, PostPageKind, PostVisibilityLevel}; use crate::data::post::{Post, PostFile, PostKind, PostPageKind, PostVisibilityLevel};
use crate::data::post::PostKind::POST_KIND_IMAGE;
use crate::data::user::UserID; use crate::data::user::UserID;
use crate::helpers::{database, friends_helper}; use crate::helpers::{database, friends_helper};
use crate::utils::date_utils::time; use crate::utils::date_utils::time;
@ -135,7 +136,16 @@ fn db_to_post(res: &database::RowResult) -> ResultBoxError<Post> {
PostPageKind::PAGE_KIND_GROUP(res.get_group_id("group_id")?) 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 // General information
id: res.get_u64("ID")?, id: res.get_u64("ID")?,
user_id: user_id.clone(), user_id: user_id.clone(),
@ -143,5 +153,16 @@ fn db_to_post(res: &database::RowResult) -> ResultBoxError<Post> {
target_page, target_page,
content: res.get_optional_str("texte")?, content: res.get_optional_str("texte")?,
visibility: PostVisibilityLevel::from_db(res.get_u32("niveau_visibilite")?), 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)
} }