diff --git a/upub/cli/src/fetch.rs b/upub/cli/src/fetch.rs index 7679a3d5..e512c664 100644 --- a/upub/cli/src/fetch.rs +++ b/upub/cli/src/fetch.rs @@ -17,7 +17,7 @@ pub async fn fetch(ctx: upub::Context, uri: String, save: bool) -> Result<(), Pu match obj.base_type() { Ok(apb::BaseType::Object(apb::ObjectType::Actor(_))) => { upub::model::actor::Entity::insert( - upub::AP::actor_q(&obj).unwrap() + upub::AP::actor_q(&obj, None).unwrap() ).exec(&tx).await.unwrap(); }, Ok(apb::BaseType::Object(apb::ObjectType::Activity(_))) => { diff --git a/upub/cli/src/update.rs b/upub/cli/src/update.rs index a682af68..4b48a6f5 100644 --- a/upub/cli/src/update.rs +++ b/upub/cli/src/update.rs @@ -1,5 +1,5 @@ use futures::TryStreamExt; -use sea_orm::{ActiveModelTrait, ActiveValue::{Set, Unchanged}, ColumnTrait, EntityTrait, QueryFilter}; +use sea_orm::{ActiveModelTrait, ActiveValue::Set, ColumnTrait, EntityTrait, QueryFilter}; use upub::traits::Fetcher; pub async fn update_users(ctx: upub::Context, days: i64) -> Result<(), sea_orm::DbErr> { @@ -18,9 +18,8 @@ pub async fn update_users(ctx: upub::Context, days: i64) -> Result<(), sea_orm:: match ctx.pull(&user.id).await.map(|x| x.actor()) { Err(e) => tracing::warn!("could not update user {}: {e}", user.id), Ok(Err(e)) => tracing::warn!("could not update user {}: {e}", user.id), - Ok(Ok(doc)) => match upub::AP::actor_q(&doc) { + Ok(Ok(doc)) => match upub::AP::actor_q(&doc, Some(user.internal)) { Ok(mut u) => { - u.internal = Unchanged(user.internal); u.updated = Set(chrono::Utc::now()); insertions.push((user.id, u)); count += 1; diff --git a/upub/core/src/traits/fetch.rs b/upub/core/src/traits/fetch.rs index edeeefe4..728b513d 100644 --- a/upub/core/src/traits/fetch.rs +++ b/upub/core/src/traits/fetch.rs @@ -305,7 +305,7 @@ impl Fetcher for crate::Context { } } - let user_model = AP::actor_q(&document)?; + let user_model = AP::actor_q(&document, None)?; // TODO this may fail: while fetching, remote server may fetch our service actor. // if it does so with http signature, we will fetch that actor in background diff --git a/upub/core/src/traits/normalize.rs b/upub/core/src/traits/normalize.rs index 35248630..dd0c8c19 100644 --- a/upub/core/src/traits/normalize.rs +++ b/upub/core/src/traits/normalize.rs @@ -1,5 +1,5 @@ use apb::{field::OptionalString, Collection, Document, Endpoints, Node, Object, PublicKey}; -use sea_orm::{sea_query::Expr, ActiveValue::{NotSet, Set}, ColumnTrait, ConnectionTrait, DbErr, EntityTrait, IntoActiveModel, QueryFilter}; +use sea_orm::{sea_query::Expr, ActiveModelTrait, ActiveValue::{Unchanged, NotSet, Set}, ColumnTrait, ConnectionTrait, DbErr, EntityTrait, IntoActiveModel, QueryFilter}; use super::Addresser; @@ -89,7 +89,7 @@ impl Normalizer for crate::Context { published: Set(chrono::Utc::now()), }, Node::Object(o) => - AP::attachment_q(o.as_document()?, object_model.internal)?, + AP::attachment_q(o.as_document()?, object_model.internal, None)?, }; crate::model::attachment::Entity::insert(attachment_model) .exec(tx) @@ -109,7 +109,7 @@ impl Normalizer for crate::Context { None }; - let mut attachment_model = AP::attachment_q(img, object_model.internal)?; + let mut attachment_model = AP::attachment_q(img, object_model.internal, None)?; // ugly fix for lemmy if let Some(m) = media_type { @@ -169,9 +169,13 @@ impl AP { }) } - pub fn activity_q(activity: &impl apb::Activity) -> Result { + pub fn activity_q(activity: &impl apb::Activity, internal: Option) -> Result { let mut m = AP::activity(activity)?.into_active_model(); - m.internal = NotSet; + m = m.reset_all(); + match internal { + Some(x) => m.internal = Unchanged(x), + None => m.internal = NotSet, + } Ok(m) } @@ -194,9 +198,13 @@ impl AP { }) } - pub fn attachment_q(document: &impl apb::Document, parent: i64) -> Result { + pub fn attachment_q(document: &impl apb::Document, parent: i64, internal: Option) -> Result { let mut m = AP::attachment(document, parent)?.into_active_model(); - m.internal = NotSet; + m = m.reset_all(); + match internal { + Some(x) => m.internal = Unchanged(x), + None => m.internal = NotSet, + } Ok(m) } @@ -246,9 +254,13 @@ impl AP { }) } - pub fn object_q(object: &impl apb::Object) -> Result { + pub fn object_q(object: &impl apb::Object, internal: Option) -> Result { let mut m = AP::object(object)?.into_active_model(); - m.internal = NotSet; + m = m.reset_all(); + match internal { + Some(x) => m.internal = Unchanged(x), + None => m.internal = NotSet, + } Ok(m) } @@ -294,9 +306,13 @@ impl AP { }) } - pub fn actor_q(actor: &impl apb::Actor) -> Result { + pub fn actor_q(actor: &impl apb::Actor, internal: Option) -> Result { let mut m = AP::actor(actor)?.into_active_model(); - m.internal = NotSet; + m = m.reset_all(); + match internal { + Some(x) => m.internal = Unchanged(x), + None => m.internal = NotSet, + } Ok(m) } } diff --git a/upub/core/src/traits/process.rs b/upub/core/src/traits/process.rs index 3de14a51..17bcadb3 100644 --- a/upub/core/src/traits/process.rs +++ b/upub/core/src/traits/process.rs @@ -1,5 +1,5 @@ use apb::{target::Addressed, Activity, Base, Object}; -use sea_orm::{sea_query::Expr, ActiveModelTrait, ActiveValue::{NotSet, Set, Unchanged}, ColumnTrait, Condition, DatabaseTransaction, EntityTrait, QueryFilter, QuerySelect, SelectColumns}; +use sea_orm::{sea_query::Expr, ActiveModelTrait, ActiveValue::{NotSet, Set}, ColumnTrait, Condition, DatabaseTransaction, EntityTrait, QueryFilter, QuerySelect, SelectColumns}; use crate::{ext::{AnyQuery, LoggableError}, model, traits::{fetch::Pull, Fetcher, Normalizer}}; #[derive(Debug, thiserror::Error)] @@ -265,8 +265,7 @@ pub async fn update(ctx: &crate::Context, activity: impl apb::Activity, tx: &Dat let internal_uid = crate::model::actor::Entity::ap_to_internal(&oid, tx) .await? .ok_or(ProcessorError::Incomplete)?; - let mut actor_model = crate::AP::actor_q(object_node.as_actor()?)?; - actor_model.internal = Unchanged(internal_uid); + let mut actor_model = crate::AP::actor_q(object_node.as_actor()?, Some(internal_uid))?; actor_model.updated = Set(chrono::Utc::now()); actor_model.update(tx).await?; }, @@ -274,8 +273,7 @@ pub async fn update(ctx: &crate::Context, activity: impl apb::Activity, tx: &Dat let internal_oid = crate::model::object::Entity::ap_to_internal(&oid, tx) .await? .ok_or(ProcessorError::Incomplete)?; - let mut object_model = crate::AP::object_q(&object_node)?; - object_model.internal = Unchanged(internal_oid); + let mut object_model = crate::AP::object_q(&object_node, Some(internal_oid))?; object_model.updated = Set(chrono::Utc::now()); object_model.update(tx).await?; },