1
0
Fork 0
forked from alemi/upub

fix: increment share and like counters for outbox

This commit is contained in:
əlemi 2024-04-29 20:40:20 +02:00
parent 5ae9a140b4
commit 4fe53f7011
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -100,9 +100,8 @@ impl apb::server::Outbox for Context {
async fn like(&self, uid: String, activity: serde_json::Value) -> crate::Result<String> {
let aid = self.aid(uuid::Uuid::new_v4().to_string());
let activity_targets = activity.addressed();
let Some(oid) = activity.object().id() else {
return Err(UpubError::bad_request());
};
let oid = activity.object().id().ok_or_else(UpubError::bad_request)?;
self.fetch_object(&oid).await?;
let activity_model = model::activity::Model::new(
&activity
.set_id(Some(&aid))
@ -112,13 +111,18 @@ impl apb::server::Outbox for Context {
let like_model = model::like::ActiveModel {
actor: Set(uid.clone()),
likes: Set(oid),
likes: Set(oid.clone()),
date: Set(chrono::Utc::now()),
..Default::default()
};
model::like::Entity::insert(like_model).exec(self.db()).await?;
model::activity::Entity::insert(activity_model.into_active_model())
.exec(self.db()).await?;
model::object::Entity::update_many()
.col_expr(model::object::Column::Likes, Expr::col(model::object::Column::Likes).add(1))
.filter(model::object::Column::Id.eq(oid))
.exec(self.db())
.await?;
self.dispatch(&uid, activity_targets, &aid, None).await?;
@ -338,6 +342,7 @@ impl apb::server::Outbox for Context {
let aid = self.aid(uuid::Uuid::new_v4().to_string());
let activity_targets = activity.addressed();
let oid = activity.object().id().ok_or_else(UpubError::bad_request)?;
self.fetch_object(&oid).await?;
let activity_model = model::activity::Model::new(
&activity
.set_id(Some(&aid))
@ -347,13 +352,18 @@ impl apb::server::Outbox for Context {
let share_model = model::share::ActiveModel {
actor: Set(uid.clone()),
shares: Set(oid),
shares: Set(oid.clone()),
date: Set(chrono::Utc::now()),
..Default::default()
};
model::share::Entity::insert(share_model).exec(self.db()).await?;
model::activity::Entity::insert(activity_model.into_active_model())
.exec(self.db()).await?;
model::object::Entity::update_many()
.col_expr(model::object::Column::Shares, Expr::col(model::object::Column::Shares).add(1))
.filter(model::object::Column::Id.eq(oid))
.exec(self.db())
.await?;
self.dispatch(&uid, activity_targets, &aid, None).await?;