fix: delete with filters instead of activemodel

This commit is contained in:
əlemi 2024-04-23 04:24:27 +02:00
parent 43d5571b27
commit 63d78e7ff5
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -202,34 +202,31 @@ impl apb::server::Outbox for Context {
async fn undo(&self, uid: String, activity: serde_json::Value) -> crate::Result<String> { async fn undo(&self, uid: String, activity: serde_json::Value) -> crate::Result<String> {
let aid = self.aid(uuid::Uuid::new_v4().to_string()); let aid = self.aid(uuid::Uuid::new_v4().to_string());
let activity_targets = activity.addressed(); let activity_targets = activity.addressed();
{ let old_aid = activity.object().id().ok_or_else(UpubError::bad_request)?;
let Some(old_aid) = activity.object().id() else { let old_activity = model::activity::Entity::find_by_id(old_aid)
return Err(UpubError::bad_request()); .one(self.db())
}; .await?
let Some(old_activity) = model::activity::Entity::find_by_id(old_aid) .ok_or_else(UpubError::not_found)?;
.one(self.db()).await?
else {
return Err(UpubError::not_found());
};
if old_activity.actor != uid { if old_activity.actor != uid {
return Err(UpubError::forbidden()); return Err(UpubError::forbidden());
} }
match old_activity.activity_type { match old_activity.activity_type {
apb::ActivityType::Like => { apb::ActivityType::Like => {
model::like::Entity::delete(model::like::ActiveModel { model::like::Entity::delete_many()
actor: Set(old_activity.actor), likes: Set(old_activity.object.unwrap_or("".into())), .filter(model::like::Column::Actor.eq(old_activity.actor))
..Default::default() .filter(model::like::Column::Likes.eq(old_activity.object.unwrap_or("".into())))
}).exec(self.db()).await?; .exec(self.db())
.await?;
}, },
apb::ActivityType::Follow => { apb::ActivityType::Follow => {
model::relation::Entity::delete(model::relation::ActiveModel { model::relation::Entity::delete_many()
follower: Set(old_activity.actor), following: Set(old_activity.object.unwrap_or("".into())), .filter(model::relation::Column::Follower.eq(old_activity.actor))
..Default::default() .filter(model::relation::Column::Following.eq(old_activity.object.unwrap_or("".into())))
}).exec(self.db()).await?; .exec(self.db())
.await?;
}, },
t => tracing::warn!("extra side effects for activity {t:?} not implemented"), t => tracing::warn!("extra side effects for activity {t:?} not implemented"),
} }
}
let activity_model = model::activity::Model::new( let activity_model = model::activity::Model::new(
&activity &activity
.set_id(Some(&aid)) .set_id(Some(&aid))