1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +00:00

Add support for movie posts

This commit is contained in:
Pierre HUBERT 2020-07-03 16:41:14 +02:00
parent b858eac3c4
commit d632d0b888
11 changed files with 109 additions and 11 deletions

View File

@ -31,3 +31,4 @@ pub mod group_member_api;
pub mod friend_api;
pub mod friendship_status_api;
pub mod post_api;
pub mod movie_api;

34
src/api_data/movie_api.rs Normal file
View File

@ -0,0 +1,34 @@
//! # Movie API information
//!
//! @author Pierre Hubert
use serde::Serialize;
use crate::data::movie::Movie;
use crate::utils::user_data_utils::user_data_url;
#[derive(Serialize)]
#[allow(non_snake_case)]
pub struct MovieAPI {
id: u64,
uri: String,
url: String,
userID: u64,
name: String,
file_type: String,
size: usize,
}
impl MovieAPI {
/// Construct a new instance of movie
pub fn new(m: &Movie) -> MovieAPI {
MovieAPI {
id: m.id,
uri: m.uri.clone(),
url: user_data_url(m.uri.as_str()),
userID: m.user_id.id(),
name: m.name.clone(),
file_type: m.file_type.clone(),
size: m.size,
}
}
}

View File

@ -3,8 +3,11 @@
//! @author Pierre Hubert
use serde::Serialize;
use crate::api_data::movie_api::MovieAPI;
use crate::data::error::ResultBoxError;
use crate::data::post::{Post, PostKind};
use crate::data::user::UserID;
use crate::helpers::movies_helper;
use crate::utils::user_data_utils::user_data_url;
#[derive(Serialize)]
@ -30,11 +33,15 @@ pub struct PostAPI {
link_title: Option<String>,
link_description: Option<String>,
link_image: Option<String>,
// Movie specific
video_id: Option<u64>,
video_info: Option<MovieAPI>,
}
impl PostAPI {
/// Turn a `Post` entry into an API entry
pub fn new(p: &Post) -> PostAPI {
pub fn new(p: &Post) -> ResultBoxError<PostAPI> {
let mut post = PostAPI {
ID: p.id,
userID: p.user_id.id(),
@ -56,6 +63,10 @@ impl PostAPI {
link_title: None,
link_description: None,
link_image: None,
// Movie specific
video_id: None,
video_info: None,
};
match &p.kind {
@ -75,17 +86,21 @@ impl PostAPI {
post.link_image = link.image.clone();
}
PostKind::POST_KIND_MOVIE => {}
PostKind::POST_KIND_MOVIE(movie_id) => {
post.video_id = Some(*movie_id);
post.video_info = Some(MovieAPI::new(&movies_helper::get_info(*movie_id)?))
}
PostKind::POST_KIND_COUNTDOWN => {}
PostKind::POST_KIND_SURVEY => {}
PostKind::POST_KIND_YOUTUBE => {}
}
post
Ok(post)
}
/// Turn a list of posts into an API entry
pub fn for_list(l: &Vec<Post>) -> Vec<PostAPI> {
pub fn for_list(l: &Vec<Post>) -> ResultBoxError<Vec<PostAPI>> {
l.iter().map(Self::new).collect()
}
}

View File

@ -37,6 +37,9 @@ pub mod database_tables_names {
/// Posts table
pub const POSTS_TABLE: &str = "texte";
/// Movies table
pub const MOVIES_TABLE: &str = "galerie_video";
}
/// The account image to show for user who do not have any

View File

@ -20,5 +20,5 @@ pub fn get_list_user(r: &mut HttpRequestHandler) -> RequestResult {
.set_start_from(start_from)
.get_user(&user_id)?;
r.set_response(PostAPI::for_list(&posts))
r.set_response(PostAPI::for_list(&posts)?)
}

View File

@ -20,3 +20,4 @@ pub mod global_search_result;
pub mod friend;
pub mod friendship_status;
pub mod post;
pub mod movie;

14
src/data/movie.rs Normal file
View File

@ -0,0 +1,14 @@
//! # Movie information
//!
//! @author Pierre Hubert
use crate::data::user::UserID;
pub struct Movie {
pub id: u64,
pub user_id: UserID,
pub name: String,
pub uri: String,
pub file_type: String,
pub size: usize,
}

View File

@ -56,7 +56,7 @@ pub enum PostKind {
POST_KIND_IMAGE(PostFile),
POST_KIND_WEBLINK(PostWebLink),
POST_KIND_PDF(PostFile),
POST_KIND_MOVIE,
POST_KIND_MOVIE(u64), // The ID of the movie
POST_KIND_COUNTDOWN,
POST_KIND_SURVEY,
POST_KIND_YOUTUBE,
@ -69,7 +69,7 @@ impl PostKind {
PostKind::POST_KIND_IMAGE(_) => "image",
PostKind::POST_KIND_WEBLINK(_) => "weblink",
PostKind::POST_KIND_PDF(_) => "pdf",
PostKind::POST_KIND_MOVIE => "movie",
PostKind::POST_KIND_MOVIE(_) => "movie",
PostKind::POST_KIND_COUNTDOWN => "countdown",
PostKind::POST_KIND_SURVEY => "survey",
PostKind::POST_KIND_YOUTUBE => "youtube",

View File

@ -11,3 +11,4 @@ pub mod groups_helper;
pub mod posts_helper;
pub mod conversations_helper;
pub mod virtual_directory_helper;
pub mod movies_helper;

View File

@ -0,0 +1,27 @@
//! # Movies helper
//!
//! @author Pierre Hubert
use crate::constants::database_tables_names::MOVIES_TABLE;
use crate::data::error::ResultBoxError;
use crate::data::movie::Movie;
use crate::helpers::database;
/// Get information about a single movie
pub fn get_info(movie_id: u64) -> ResultBoxError<Movie> {
database::QueryInfo::new(MOVIES_TABLE)
.cond_u64("ID", movie_id)
.query_row(db_to_movie)
}
/// Turn a database entry into a movie object
fn db_to_movie(row: &database::RowResult) -> ResultBoxError<Movie> {
Ok(Movie {
id: row.get_u64("ID")?,
user_id: row.get_user_id("ID_user")?,
name: row.get_str("nom_video")?,
uri: row.get_str("URL")?,
file_type: row.get_str("file_type")?,
size: row.get_usize("size")?,
})
}

View File

@ -5,7 +5,7 @@
use crate::constants::database_tables_names::POSTS_TABLE;
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::post::{Post, PostFile, PostKind, PostPageKind, PostVisibilityLevel, PostWebLink};
use crate::data::post::PostKind::{POST_KIND_IMAGE, POST_KIND_PDF, POST_KIND_WEBLINK};
use crate::data::post::PostKind::{POST_KIND_IMAGE, POST_KIND_MOVIE, POST_KIND_PDF, POST_KIND_WEBLINK};
use crate::data::user::UserID;
use crate::helpers::{database, friends_helper};
use crate::utils::date_utils::time;
@ -170,6 +170,8 @@ fn db_to_post(res: &database::RowResult) -> ResultBoxError<Post> {
"pdf" => post.kind = POST_KIND_PDF(file?),
"video" => post.kind = POST_KIND_MOVIE(res.get_u64("idvideo")?),
_ => {}
}