fix: updates should now work!

This commit is contained in:
əlemi 2024-06-14 02:10:00 +02:00
parent e1f1548e7e
commit 6fed451513
Signed by: alemi
GPG key ID: A4895B84D311642C
5 changed files with 34 additions and 21 deletions

View file

@ -17,7 +17,7 @@ pub async fn fetch(ctx: upub::Context, uri: String, save: bool) -> Result<(), Pu
match obj.base_type() { match obj.base_type() {
Ok(apb::BaseType::Object(apb::ObjectType::Actor(_))) => { Ok(apb::BaseType::Object(apb::ObjectType::Actor(_))) => {
upub::model::actor::Entity::insert( upub::model::actor::Entity::insert(
upub::AP::actor_q(&obj).unwrap() upub::AP::actor_q(&obj, None).unwrap()
).exec(&tx).await.unwrap(); ).exec(&tx).await.unwrap();
}, },
Ok(apb::BaseType::Object(apb::ObjectType::Activity(_))) => { Ok(apb::BaseType::Object(apb::ObjectType::Activity(_))) => {

View file

@ -1,5 +1,5 @@
use futures::TryStreamExt; 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; use upub::traits::Fetcher;
pub async fn update_users(ctx: upub::Context, days: i64) -> Result<(), sea_orm::DbErr> { 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()) { match ctx.pull(&user.id).await.map(|x| x.actor()) {
Err(e) => tracing::warn!("could not update user {}: {e}", user.id), Err(e) => tracing::warn!("could not update user {}: {e}", user.id),
Ok(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) => { Ok(mut u) => {
u.internal = Unchanged(user.internal);
u.updated = Set(chrono::Utc::now()); u.updated = Set(chrono::Utc::now());
insertions.push((user.id, u)); insertions.push((user.id, u));
count += 1; count += 1;

View file

@ -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. // 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 // if it does so with http signature, we will fetch that actor in background

View file

@ -1,5 +1,5 @@
use apb::{field::OptionalString, Collection, Document, Endpoints, Node, Object, PublicKey}; 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; use super::Addresser;
@ -89,7 +89,7 @@ impl Normalizer for crate::Context {
published: Set(chrono::Utc::now()), published: Set(chrono::Utc::now()),
}, },
Node::Object(o) => 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) crate::model::attachment::Entity::insert(attachment_model)
.exec(tx) .exec(tx)
@ -109,7 +109,7 @@ impl Normalizer for crate::Context {
None 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 // ugly fix for lemmy
if let Some(m) = media_type { if let Some(m) = media_type {
@ -169,9 +169,13 @@ impl AP {
}) })
} }
pub fn activity_q(activity: &impl apb::Activity) -> Result<crate::model::activity::ActiveModel, NormalizerError> { pub fn activity_q(activity: &impl apb::Activity, internal: Option<i64>) -> Result<crate::model::activity::ActiveModel, NormalizerError> {
let mut m = AP::activity(activity)?.into_active_model(); 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) Ok(m)
} }
@ -194,9 +198,13 @@ impl AP {
}) })
} }
pub fn attachment_q(document: &impl apb::Document, parent: i64) -> Result<crate::model::attachment::ActiveModel, NormalizerError> { pub fn attachment_q(document: &impl apb::Document, parent: i64, internal: Option<i64>) -> Result<crate::model::attachment::ActiveModel, NormalizerError> {
let mut m = AP::attachment(document, parent)?.into_active_model(); 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) Ok(m)
} }
@ -246,9 +254,13 @@ impl AP {
}) })
} }
pub fn object_q(object: &impl apb::Object) -> Result<crate::model::object::ActiveModel, NormalizerError> { pub fn object_q(object: &impl apb::Object, internal: Option<i64>) -> Result<crate::model::object::ActiveModel, NormalizerError> {
let mut m = AP::object(object)?.into_active_model(); 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) Ok(m)
} }
@ -294,9 +306,13 @@ impl AP {
}) })
} }
pub fn actor_q(actor: &impl apb::Actor) -> Result<crate::model::actor::ActiveModel, NormalizerError> { pub fn actor_q(actor: &impl apb::Actor, internal: Option<i64>) -> Result<crate::model::actor::ActiveModel, NormalizerError> {
let mut m = AP::actor(actor)?.into_active_model(); 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) Ok(m)
} }
} }

View file

@ -1,5 +1,5 @@
use apb::{target::Addressed, Activity, Base, Object}; 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}}; use crate::{ext::{AnyQuery, LoggableError}, model, traits::{fetch::Pull, Fetcher, Normalizer}};
#[derive(Debug, thiserror::Error)] #[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) let internal_uid = crate::model::actor::Entity::ap_to_internal(&oid, tx)
.await? .await?
.ok_or(ProcessorError::Incomplete)?; .ok_or(ProcessorError::Incomplete)?;
let mut actor_model = crate::AP::actor_q(object_node.as_actor()?)?; let mut actor_model = crate::AP::actor_q(object_node.as_actor()?, Some(internal_uid))?;
actor_model.internal = Unchanged(internal_uid);
actor_model.updated = Set(chrono::Utc::now()); actor_model.updated = Set(chrono::Utc::now());
actor_model.update(tx).await?; 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) let internal_oid = crate::model::object::Entity::ap_to_internal(&oid, tx)
.await? .await?
.ok_or(ProcessorError::Incomplete)?; .ok_or(ProcessorError::Incomplete)?;
let mut object_model = crate::AP::object_q(&object_node)?; let mut object_model = crate::AP::object_q(&object_node, Some(internal_oid))?;
object_model.internal = Unchanged(internal_oid);
object_model.updated = Set(chrono::Utc::now()); object_model.updated = Set(chrono::Utc::now());
object_model.update(tx).await?; object_model.update(tx).await?;
}, },