Upsert Media in from_activity()

This commit is contained in:
Kitaiti Makoto 2021-02-24 01:05:19 +09:00
parent 462c5a1d42
commit ea1f4d48d5

View File

@ -19,7 +19,7 @@ use url::Url;
const REMOTE_MEDIA_DIRECTORY: &str = "remote"; const REMOTE_MEDIA_DIRECTORY: &str = "remote";
#[derive(Clone, Identifiable, Queryable)] #[derive(Clone, Identifiable, Queryable, AsChangeset)]
pub struct Media { pub struct Media {
pub id: i32, pub id: i32,
pub file_path: String, pub file_path: String,
@ -225,32 +225,65 @@ impl Media {
.copy_to(&mut dest) .copy_to(&mut dest)
.ok()?; .ok()?;
// TODO: upsert Media::find_by_file_path(conn, &path.to_str()?)
Media::insert( .and_then(|mut media| {
conn, let mut updated = false;
NewMedia {
file_path: path.to_str()?.to_string(), let alt_text = image.object_props.content_string().ok()?;
alt_text: image.object_props.content_string().ok()?, let sensitive = image.object_props.summary_string().is_ok();
is_remote: false, let content_warning = image.object_props.summary_string().ok();
remote_url: None, if media.alt_text != alt_text {
sensitive: image.object_props.summary_string().is_ok(), media.alt_text = alt_text;
content_warning: image.object_props.summary_string().ok(), updated = true;
owner_id: User::from_id( }
if media.is_remote {
media.is_remote = false;
updated = true;
}
if media.remote_url.is_some() {
media.remote_url = None;
updated = true;
}
if media.sensitive != sensitive {
media.sensitive = sensitive;
updated = true;
}
if media.content_warning != content_warning {
media.content_warning = content_warning;
updated = true;
}
if updated {
diesel::update(&media).set(&media).execute(&**conn)?;
}
Ok(media)
})
.or_else(|_| {
Media::insert(
conn, conn,
image NewMedia {
.object_props file_path: path.to_str()?.to_string(),
.attributed_to_link_vec::<Id>() alt_text: image.object_props.content_string().ok()?,
.ok()? is_remote: false,
.into_iter() remote_url: None,
.next()? sensitive: image.object_props.summary_string().is_ok(),
.as_ref(), content_warning: image.object_props.summary_string().ok(),
None, owner_id: User::from_id(
CONFIG.proxy(), conn,
image
.object_props
.attributed_to_link_vec::<Id>()
.ok()?
.into_iter()
.next()?
.as_ref(),
None,
CONFIG.proxy(),
)
.map_err(|(_, e)| e)?
.id,
},
) )
.map_err(|(_, e)| e)? })
.id,
},
)
} }
pub fn get_media_processor<'a>(conn: &'a Connection, user: Vec<&User>) -> MediaProcessor<'a> { pub fn get_media_processor<'a>(conn: &'a Connection, user: Vec<&User>) -> MediaProcessor<'a> {