1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-07-15 11:48:05 +00:00

Add post with images support

This commit is contained in:
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
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<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 {
/// 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