1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-11-03 09:04:03 +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 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<Post> {
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<Post> {
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)
}