From 689cf07ee44faebeaea97fc72d1c0a0ce58062db Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 3 Jul 2020 10:18:26 +0200 Subject: [PATCH] Add post with weblink support --- src/api_data/post_api.rs | 22 +++++++++++++++++++++- src/data/post.rs | 23 +++++++++++++++-------- src/helpers/posts_helper.rs | 11 +++++++++-- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/api_data/post_api.rs b/src/api_data/post_api.rs index 8db3f9f..e490d70 100644 --- a/src/api_data/post_api.rs +++ b/src/api_data/post_api.rs @@ -24,6 +24,12 @@ pub struct PostAPI { file_type: Option, file_path: Option, file_path_url: Option, + + // Weblink specific + link_url: Option, + link_title: Option, + link_description: Option, + link_image: Option, } impl PostAPI { @@ -44,17 +50,31 @@ impl PostAPI { file_type: None, file_path: None, file_path_url: None, + + // Weblink specific + link_url: None, + link_title: None, + link_description: None, + link_image: 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_WEBLINK(link) => { + post.link_url = Some(link.url.clone()); + post.link_description = link.description.clone(); + post.link_title = link.title.clone(); + post.link_image = link.image.clone(); + } + PostKind::POST_KIND_PDF => {} PostKind::POST_KIND_MOVIE => {} PostKind::POST_KIND_COUNTDOWN => {} diff --git a/src/data/post.rs b/src/data/post.rs index a34256b..b3b9f81 100644 --- a/src/data/post.rs +++ b/src/data/post.rs @@ -37,11 +37,24 @@ pub enum PostPageKind { PAGE_KIND_GROUP(GroupID), } +pub struct PostFile { + pub path: String, + pub size: usize, + pub file_type: Option, +} + +pub struct PostWebLink { + pub url: String, + pub title: Option, + pub description: Option, + pub image: Option, +} + #[allow(non_camel_case_types)] pub enum PostKind { POST_KIND_TEXT, POST_KIND_IMAGE(PostFile), - POST_KIND_WEBLINK, + POST_KIND_WEBLINK(PostWebLink), POST_KIND_PDF, POST_KIND_MOVIE, POST_KIND_COUNTDOWN, @@ -54,7 +67,7 @@ impl PostKind { match self { PostKind::POST_KIND_TEXT => "text", PostKind::POST_KIND_IMAGE(_) => "image", - PostKind::POST_KIND_WEBLINK => "weblink", + PostKind::POST_KIND_WEBLINK(_) => "weblink", PostKind::POST_KIND_PDF => "pdf", PostKind::POST_KIND_MOVIE => "movie", PostKind::POST_KIND_COUNTDOWN => "countdown", @@ -64,12 +77,6 @@ impl PostKind { } } -pub struct PostFile { - pub path: String, - pub size: usize, - pub file_type: Option, -} - pub struct Post { pub id: u64, pub user_id: UserID, diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index a3430ec..faf32cf 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -4,8 +4,8 @@ use crate::constants::database_tables_names::POSTS_TABLE; 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::post::{Post, PostFile, PostKind, PostPageKind, PostVisibilityLevel, PostWebLink}; +use crate::data::post::PostKind::{POST_KIND_IMAGE, POST_KIND_WEBLINK}; use crate::data::user::UserID; use crate::helpers::{database, friends_helper}; use crate::utils::date_utils::time; @@ -161,6 +161,13 @@ fn db_to_post(res: &database::RowResult) -> ResultBoxError { match res.get_str("type")?.as_str() { "image" => post.kind = POST_KIND_IMAGE(file?), + "webpage_link" => post.kind = POST_KIND_WEBLINK(PostWebLink { + url: res.get_str("url_page")?, + title: res.get_optional_str("titre_page")?, + description: res.get_optional_str("description_page")?, + image: res.get_optional_str("image_page")?, + }), + _ => {} }