mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 09:34:04 +00:00 
			
		
		
		
	Can create YouTube posts
This commit is contained in:
		@@ -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!();
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -51,3 +51,24 @@ 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("\"")
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user