Make Media follow activitystreams 0.7

This commit is contained in:
Kitaiti Makoto 2022-01-12 03:28:31 +09:00
parent fff1b8396b
commit f6e8b4de1b

View File

@ -2,11 +2,14 @@ use crate::{
ap_url, db_conn::DbConn, instance::Instance, safe_string::SafeString, schema::medias, ap_url, db_conn::DbConn, instance::Instance, safe_string::SafeString, schema::medias,
users::User, Connection, Error, Result, CONFIG, users::User, Connection, Error, Result, CONFIG,
}; };
use activitypub::object::Image; use activitystreams::{
base::BaseExt,
object::{Image, ObjectExt},
};
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl}; use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
use guid_create::GUID; use guid_create::GUID;
use plume_common::{ use plume_common::{
activity_pub::{inbox::FromId, request, Id}, activity_pub::{inbox::FromId, request},
utils::{escape, MediaProcessor}, utils::{escape, MediaProcessor},
}; };
use std::{ use std::{
@ -208,10 +211,11 @@ impl Media {
// TODO: merge with save_remote? // TODO: merge with save_remote?
pub fn from_activity(conn: &DbConn, image: &Image) -> Result<Media> { pub fn from_activity(conn: &DbConn, image: &Image) -> Result<Media> {
let remote_url = image let remote_url = image
.object_props .id(&Instance::get_local()?.public_domain)
.url_string() .expect("authorized domain")
.or(Err(Error::MissingApProperty))?; .expect("exists and only")
let path = determine_mirror_file_path(&remote_url); .as_str();
let path = determine_mirror_file_path(remote_url);
let parent = path.parent().ok_or(Error::InvalidValue)?; let parent = path.parent().ok_or(Error::InvalidValue)?;
if !parent.is_dir() { if !parent.is_dir() {
DirBuilder::new().recursive(true).create(parent)?; DirBuilder::new().recursive(true).create(parent)?;
@ -219,25 +223,27 @@ impl Media {
let mut dest = fs::File::create(path.clone())?; let mut dest = fs::File::create(path.clone())?;
// TODO: conditional GET // TODO: conditional GET
request::get( request::get(remote_url, User::get_sender(), CONFIG.proxy().cloned())?
remote_url.as_str(), .copy_to(&mut dest)?;
User::get_sender(),
CONFIG.proxy().cloned(),
)?
.copy_to(&mut dest)?;
Media::find_by_file_path(conn, path.to_str().ok_or(Error::InvalidValue)?) Media::find_by_file_path(conn, path.to_str().ok_or(Error::InvalidValue)?)
.and_then(|mut media| { .and_then(|mut media| {
let mut updated = false; let mut updated = false;
let alt_text = image let alt_text = image
.object_props .content()
.content_string() .ok_or(Error::NotFound)?
.or(Err(Error::NotFound))?; .as_single_xsd_string()
let sensitive = image.object_props.summary_string().is_ok(); .expect("only one");
let content_warning = image.object_props.summary_string().ok(); let sensitive = image.summary().is_some();
let content_warning = image.summary().map(|summary| {
summary
.as_single_xsd_string()
.expect("exist and only")
.to_string()
});
if media.alt_text != alt_text { if media.alt_text != alt_text {
media.alt_text = alt_text; media.alt_text = alt_text.to_string();
updated = true; updated = true;
} }
if media.is_remote { if media.is_remote {
@ -267,23 +273,28 @@ impl Media {
NewMedia { NewMedia {
file_path: path.to_str().ok_or(Error::InvalidValue)?.to_string(), file_path: path.to_str().ok_or(Error::InvalidValue)?.to_string(),
alt_text: image alt_text: image
.object_props .content()
.content_string() .ok_or(Error::NotFound)?
.or(Err(Error::NotFound))?, .as_single_xsd_string()
.expect("only one")
.to_string(),
is_remote: false, is_remote: false,
remote_url: None, remote_url: None,
sensitive: image.object_props.summary_string().is_ok(), sensitive: image.summary().is_some(),
content_warning: image.object_props.summary_string().ok(), content_warning: image.summary().map(|summary| {
summary
.as_single_xsd_string()
.expect("exist and only")
.to_string()
}),
owner_id: User::from_id( owner_id: User::from_id(
conn, conn,
image image
.object_props .attributed_to()
.attributed_to_link_vec::<Id>()
.or(Err(Error::NotFound))?
.into_iter()
.next()
.ok_or(Error::NotFound)? .ok_or(Error::NotFound)?
.as_ref(), .as_single_id()
.expect("exists and only")
.as_str(),
None, None,
CONFIG.proxy(), CONFIG.proxy(),
) )