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