Upsert Post in from_activity()

This commit is contained in:
Kitaiti Makoto 2021-02-24 02:36:39 +09:00
parent 589c159eb9
commit 33221d386e

View File

@ -641,27 +641,72 @@ impl FromId<DbConn> for Post {
.and_then(|img| Media::from_activity(conn, &img).ok().map(|m| m.id)); .and_then(|img| Media::from_activity(conn, &img).ok().map(|m| m.id));
let title = article.object_props.name_string()?; let title = article.object_props.name_string()?;
// TODO: upsert let ap_url = article
let post = Post::insert( .object_props
conn, .url_string()
NewPost { .or_else(|_| article.object_props.id_string())?;
blog_id: blog?.id, let post = Post::from_db(conn, &ap_url)
slug: title.to_kebab_case(), .and_then(|mut post| {
title, let mut updated = false;
content: SafeString::new(&article.object_props.content_string()?),
published: true, let slug = title.to_kebab_case();
license, let content = SafeString::new(&article.object_props.content_string()?);
// FIXME: This is wrong: with this logic, we may use the display URL as the AP ID. We need two different fields let subtitle = article.object_props.summary_string()?;
ap_url: article let source = article.ap_object_props.source_object::<Source>()?.content;
.object_props if post.slug != slug {
.url_string() post.slug = slug;
.or_else(|_| article.object_props.id_string())?, updated = true;
creation_date: Some(article.object_props.published_utctime()?.naive_utc()), }
subtitle: article.object_props.summary_string()?, if post.title != title {
source: article.ap_object_props.source_object::<Source>()?.content, post.title = title.clone();
cover_id: cover, updated = true;
}, }
)?; if post.content != content {
post.content = content;
updated = true;
}
if post.license != license {
post.license = license.clone();
updated = true;
}
if post.subtitle != subtitle {
post.subtitle = subtitle;
updated = true;
}
if post.source != source {
post.source = source;
updated = true;
}
if post.cover_id != cover {
post.cover_id = cover;
updated = true;
}
if updated {
post.update(conn)?;
}
Ok(post)
})
.or_else(|_| {
Post::insert(
conn,
NewPost {
blog_id: blog?.id,
slug: title.to_kebab_case(),
title,
content: SafeString::new(&article.object_props.content_string()?),
published: true,
license,
// FIXME: This is wrong: with this logic, we may use the display URL as the AP ID. We need two different fields
ap_url,
creation_date: Some(article.object_props.published_utctime()?.naive_utc()),
subtitle: article.object_props.summary_string()?,
source: article.ap_object_props.source_object::<Source>()?.content,
cover_id: cover,
},
)
})?;
for author in authors { for author in authors {
PostAuthor::insert( PostAuthor::insert(