dialtone_sqlx 0.1.0

Dialtone SQLx Back-End
Documentation
use crate::logic::ap_object::html::process_html;
use crate::logic::ap_object::local_id::make_local_id;
use anyhow::{anyhow, Context, Result};
use dialtone_common::ap::ap_object::{ApObject, ApObjectMediaType};
use dialtone_common::ap::id::create_ap_object_id;
use dialtone_common::rest::ap_objects::ap_object_model::CreateOwnedApObject;

pub fn new_html_ap_object(
    host_name: &str,
    pun: &str,
    actor_id: &str,
    new_ap_object: &CreateOwnedApObject,
) -> Result<ApObject> {
    let content = &new_ap_object
        .content
        .as_ref()
        .with_context(|| "ap_objects content required".to_string())?;
    let (html, title) = process_html(content.as_bytes()).map_err(|err| anyhow!(err.to_string()))?;
    let local_id = make_local_id(Some(pun), title.as_deref());
    let ap_object_id = create_ap_object_id(host_name, &new_ap_object.ap_type, &local_id);
    let html = String::from_utf8(html).map_err(|err| anyhow!(err.to_string()))?;
    // names and summary are cloned here because they must be cloned for creating the ApObject
    let name = new_ap_object.name.clone().map_or(title.clone(), Some);
    let summary = new_ap_object.summary.clone().map_or(title, Some);
    Ok(ApObject {
        name,
        id: Some(ap_object_id),
        url: None,
        media_type: Some(ApObjectMediaType::TextHtml),
        ap_type: Some(new_ap_object.ap_type.clone()),
        content: Some(html),
        summary,
        actor: Some(actor_id.to_string()),
        attributed_to: Some(actor_id.to_string()),
        to: new_ap_object.to.clone(),
        cc: new_ap_object.cc.clone(),
        bto: new_ap_object.bto.clone(),
        bcc: new_ap_object.bcc.clone(),
    })
}

pub fn new_media_ap_object(
    host_name: &str,
    actor_id: &str,
    url: &str,
    file_name: &str,
    pun: &str,
    new_ap_object: &CreateOwnedApObject,
) -> ApObject {
    let local_id = make_local_id(Some(pun), Some(file_name));
    let ap_object_id = create_ap_object_id(host_name, &new_ap_object.ap_type, &local_id);
    ApObject {
        name: None,
        id: Some(ap_object_id),
        url: Some(url.to_string()),
        media_type: new_ap_object.media_type.clone(),
        ap_type: Some(new_ap_object.ap_type.clone()),
        content: None,
        summary: None,
        actor: Some(actor_id.to_string()),
        attributed_to: Some(actor_id.to_string()),
        to: new_ap_object.to.clone(),
        cc: new_ap_object.cc.clone(),
        bto: new_ap_object.bto.clone(),
        bcc: new_ap_object.bcc.clone(),
    }
}

#[cfg(test)]
mod new_ap_object_tests {
    use super::*;
    use dialtone_common::ap::ap_object::ApObjectType;
    use dialtone_common::rest::ap_objects::ap_object_model::CreateOwnedApObject;

    #[test]
    fn new_html_ap_object_test() {
        let create = CreateOwnedApObject {
            name: None,
            media_type: None,
            ap_type: ApObjectType::Article,
            content: Some("<p>this is a post</p>".to_string()),
            summary: None,
            owner_data: None,
            to: None,
            cc: None,
            bto: None,
            bcc: None,
        };
        let ap_object =
            new_html_ap_object("example.com", "foo", "http://example.com/p/foo", &create).unwrap();
        assert_eq!(ap_object.name, Some("this is a post".to_string()));
        assert_eq!(ap_object.summary, Some("this is a post".to_string()));
        assert_eq!(ap_object.content, Some("<p>this is a post</p>".to_string()));
        let id = ap_object.id.unwrap();
        assert!(id.starts_with("https://example.com/pub/article/foo,this-is-a-post"));
        assert_eq!(
            ap_object.actor,
            Some("http://example.com/p/foo".to_string())
        );
        assert_eq!(
            ap_object.attributed_to,
            Some("http://example.com/p/foo".to_string())
        );
    }
}