1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-27 22:18:51 +00:00

Can create YouTube posts

This commit is contained in:
Pierre HUBERT 2020-07-08 13:05:06 +02:00
parent 8753f77227
commit 11051b28a0
3 changed files with 41 additions and 3 deletions

View File

@ -12,7 +12,7 @@ use crate::data::http_request_handler::HttpRequestHandler;
use crate::data::post::{Post, PostAccessLevel, PostFile, PostKind, PostPageKind, PostVisibilityLevel};
use crate::helpers::{groups_helper, posts_helper, user_helper};
use crate::utils::date_utils::time;
use crate::utils::string_utils::check_string_before_insert;
use crate::utils::string_utils::{check_string_before_insert, check_youtube_id};
use crate::utils::user_data_utils::user_data_path;
impl PostFile {
@ -141,6 +141,18 @@ pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult {
PostKind::POST_KIND_IMAGE(PostFile::new_from_created_file(&path)?)
}
"youtube" => {
let youtube = r.post_string("youtube_id")?;
if !check_youtube_id(&youtube) {
r.bad_request("Invalid YouTube ID!".to_string())?;
}
PostKind::POST_KIND_YOUTUBE(youtube)
}
// TODO : add support for next types
_ => {
r.internal_error(ExecError::boxed_new("Unsupported kind of post!"))?;
unreachable!();

View File

@ -75,7 +75,7 @@ pub fn create(p: &Post) -> ResultBoxError<u64> {
.add_opt_str("texte", p.content.as_ref());
match &p.kind {
PostKind::POST_KIND_TEXT => {/* nothing to do */},
PostKind::POST_KIND_TEXT => { /* nothing to do */ }
// Posts with associated file
POST_KIND_IMAGE(file) | POST_KIND_PDF(file) => {
@ -84,6 +84,11 @@ pub fn create(p: &Post) -> ResultBoxError<u64> {
.add_opt_str("file_type", file.file_type.as_ref());
}
// YouTube posts
POST_KIND_YOUTUBE(id) => {
insert_query = insert_query.add_str("path", id)
.add_str("type", "youtube");
}
_ => unimplemented!()
/*
@ -91,7 +96,7 @@ pub fn create(p: &Post) -> ResultBoxError<u64> {
POST_KIND_MOVIE(_) => {},
POST_KIND_COUNTDOWN(_) => {},
POST_KIND_SURVEY => {},
POST_KIND_YOUTUBE(_) => {},*/
*/
}
// Execute insertion

View File

@ -50,4 +50,25 @@ pub fn check_url(url: &str) -> bool {
/// ```
pub fn check_string_before_insert(s: &str) -> bool {
s.trim().len() > 3
}
/// Check the validity of a YouTube ID
///
/// ```
/// use comunic_server::utils::string_utils::check_youtube_id;
///
/// assert_eq!(check_youtube_id("/ab/"), false);
/// assert_eq!(check_youtube_id("abxZ96C"), true);
/// assert_eq!(check_youtube_id("a6C"), false);
/// ```
pub fn check_youtube_id(id: &str) -> bool {
id.len() >= 5
&& !id.contains("/")
&& !id.contains("\\")
&& !id.contains("@")
&& !id.contains("&")
&& !id.contains("?")
&& !id.contains(".")
&& !id.contains("'")
&& !id.contains("\"")
}