2024-04-11 00:29:32 +02:00
|
|
|
use apb::{target::Addressed, Activity, Base, Object};
|
2024-04-14 16:47:36 +02:00
|
|
|
use reqwest::StatusCode;
|
2024-05-02 15:29:42 +02:00
|
|
|
use sea_orm::{sea_query::Expr, ActiveModelTrait, ColumnTrait, Condition, EntityTrait, IntoActiveModel, QueryFilter, QuerySelect, SelectColumns, Set};
|
2024-04-09 01:14:48 +02:00
|
|
|
|
2024-04-20 04:34:47 +02:00
|
|
|
use crate::{errors::{LoggableError, UpubError}, model::{self, FieldError}};
|
2024-04-09 01:14:48 +02:00
|
|
|
|
2024-04-18 05:25:56 +02:00
|
|
|
use super::{fetcher::Fetcher, Context};
|
2024-04-09 01:14:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
#[axum::async_trait]
|
2024-04-11 00:29:32 +02:00
|
|
|
impl apb::server::Inbox for Context {
|
|
|
|
type Error = UpubError;
|
|
|
|
type Activity = serde_json::Value;
|
|
|
|
|
2024-04-22 22:52:05 +02:00
|
|
|
async fn create(&self, server: String, activity: serde_json::Value) -> crate::Result<()> {
|
2024-04-09 01:14:48 +02:00
|
|
|
let activity_model = model::activity::Model::new(&activity)?;
|
2024-04-22 22:52:05 +02:00
|
|
|
let aid = activity_model.id.clone();
|
2024-04-09 01:14:48 +02:00
|
|
|
let Some(object_node) = activity.object().extract() else {
|
|
|
|
// TODO we could process non-embedded activities or arrays but im lazy rn
|
|
|
|
tracing::error!("refusing to process activity without embedded object: {}", serde_json::to_string_pretty(&activity).unwrap());
|
|
|
|
return Err(UpubError::unprocessable());
|
|
|
|
};
|
2024-04-22 22:52:05 +02:00
|
|
|
let mut object_model = model::object::Model::new(&object_node)?;
|
|
|
|
let oid = object_model.id.clone();
|
2024-04-30 14:22:44 +02:00
|
|
|
let uid = object_model.attributed_to.clone();
|
2024-04-22 22:52:05 +02:00
|
|
|
// make sure we're allowed to edit this object
|
|
|
|
if let Some(object_author) = &object_model.attributed_to {
|
|
|
|
if server != Context::server(object_author) {
|
|
|
|
return Err(UpubError::forbidden());
|
|
|
|
}
|
|
|
|
} else if server != Context::server(&object_model.id) {
|
|
|
|
return Err(UpubError::forbidden());
|
|
|
|
};
|
|
|
|
// fix context also for remote posts
|
|
|
|
// TODO this is not really appropriate because we're mirroring incorrectly remote objects, but
|
|
|
|
// it makes it SOO MUCH EASIER for us to fetch threads and stuff, so we're filling it for them
|
|
|
|
match (&object_model.in_reply_to, &object_model.context) {
|
|
|
|
(Some(reply_id), None) => // get context from replied object
|
|
|
|
object_model.context = self.fetch_object(reply_id).await?.context,
|
|
|
|
(None, None) => // generate a new context
|
|
|
|
object_model.context = Some(crate::url!(self, "/context/{}", uuid::Uuid::new_v4().to_string())),
|
|
|
|
(_, Some(_)) => {}, // leave it as set by user
|
|
|
|
}
|
|
|
|
// update replies counter
|
2024-04-19 04:03:57 +02:00
|
|
|
if let Some(ref in_reply_to) = object_model.in_reply_to {
|
|
|
|
if self.fetch_object(in_reply_to).await.is_ok() {
|
|
|
|
model::object::Entity::update_many()
|
|
|
|
.filter(model::object::Column::Id.eq(in_reply_to))
|
|
|
|
.col_expr(model::object::Column::Comments, Expr::col(model::object::Column::Comments).add(1))
|
|
|
|
.exec(self.db())
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
2024-04-09 01:14:48 +02:00
|
|
|
model::object::Entity::insert(object_model.into_active_model()).exec(self.db()).await?;
|
|
|
|
model::activity::Entity::insert(activity_model.into_active_model()).exec(self.db()).await?;
|
2024-04-21 22:58:33 +02:00
|
|
|
for attachment in object_node.attachment() {
|
2024-05-12 01:21:12 +02:00
|
|
|
let attachment_model = model::attachment::ActiveModel::new(&attachment, oid.clone(), None)?;
|
2024-04-21 22:58:33 +02:00
|
|
|
model::attachment::Entity::insert(attachment_model)
|
|
|
|
.exec(self.db())
|
|
|
|
.await?;
|
|
|
|
}
|
2024-05-12 00:54:48 +02:00
|
|
|
// lemmy sends us an image field in posts, treat it like an attachment i'd say
|
2024-05-12 00:56:22 +02:00
|
|
|
if let Some(img) = object_node.image().get() {
|
2024-05-12 01:21:12 +02:00
|
|
|
// TODO lemmy doesnt tell us the media type but we use it to display the thing...
|
|
|
|
let img_url = img.url().id().unwrap_or_default();
|
|
|
|
let media_type = if img_url.ends_with("png") {
|
|
|
|
Some("image/png".to_string())
|
|
|
|
} else if img_url.ends_with("webp") {
|
|
|
|
Some("image/webp".to_string())
|
|
|
|
} else if img_url.ends_with("jpeg") || img_url.ends_with("jpg") {
|
|
|
|
Some("image/jpeg".to_string())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let attachment_model = model::attachment::ActiveModel::new(img, oid.clone(), media_type)?;
|
2024-05-12 00:54:48 +02:00
|
|
|
model::attachment::Entity::insert(attachment_model)
|
2024-05-12 00:56:22 +02:00
|
|
|
.exec(self.db())
|
2024-05-12 00:54:48 +02:00
|
|
|
.await?;
|
|
|
|
}
|
2024-04-30 14:22:44 +02:00
|
|
|
// TODO can we even receive anonymous objects?
|
|
|
|
if let Some(object_author) = uid {
|
|
|
|
model::user::Entity::update_many()
|
|
|
|
.col_expr(model::user::Column::StatusesCount, Expr::col(model::user::Column::StatusesCount).add(1))
|
|
|
|
.filter(model::user::Column::Id.eq(&object_author))
|
|
|
|
.exec(self.db())
|
|
|
|
.await?;
|
|
|
|
}
|
2024-04-12 18:40:24 +02:00
|
|
|
let expanded_addressing = self.expand_addressing(activity.addressed()).await?;
|
2024-04-21 15:41:29 +02:00
|
|
|
self.address_to(Some(&aid), Some(&oid), &expanded_addressing).await?;
|
2024-04-09 01:14:48 +02:00
|
|
|
tracing::info!("{} posted {}", aid, oid);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-04-22 22:52:05 +02:00
|
|
|
async fn like(&self, _: String, activity: serde_json::Value) -> crate::Result<()> {
|
2024-04-14 05:53:36 +02:00
|
|
|
let aid = activity.id().ok_or(UpubError::bad_request())?;
|
|
|
|
let uid = activity.actor().id().ok_or(UpubError::bad_request())?;
|
2024-04-09 01:14:48 +02:00
|
|
|
let oid = activity.object().id().ok_or(UpubError::bad_request())?;
|
2024-04-18 05:25:56 +02:00
|
|
|
if let Err(e) = self.fetch_object(&oid).await {
|
2024-04-18 04:38:21 +02:00
|
|
|
tracing::warn!("failed fetching liked object: {e}");
|
|
|
|
}
|
2024-04-09 01:14:48 +02:00
|
|
|
let like = model::like::ActiveModel {
|
|
|
|
id: sea_orm::ActiveValue::NotSet,
|
2024-04-14 05:53:36 +02:00
|
|
|
actor: sea_orm::Set(uid.clone()),
|
2024-04-09 01:14:48 +02:00
|
|
|
likes: sea_orm::Set(oid.clone()),
|
2024-04-20 04:33:23 +02:00
|
|
|
date: sea_orm::Set(activity.published().unwrap_or(chrono::Utc::now())),
|
2024-04-09 01:14:48 +02:00
|
|
|
};
|
|
|
|
match model::like::Entity::insert(like).exec(self.db()).await {
|
|
|
|
Err(sea_orm::DbErr::RecordNotInserted) => Err(UpubError::not_modified()),
|
|
|
|
Err(sea_orm::DbErr::Exec(_)) => Err(UpubError::not_modified()), // bad fix for sqlite
|
|
|
|
Err(e) => {
|
2024-04-14 05:53:36 +02:00
|
|
|
tracing::error!("unexpected error procesing like from {uid} to {oid}: {e}");
|
2024-04-09 01:14:48 +02:00
|
|
|
Err(UpubError::internal_server_error())
|
|
|
|
}
|
|
|
|
Ok(_) => {
|
2024-04-12 18:40:24 +02:00
|
|
|
let activity_model = model::activity::Model::new(&activity)?.into_active_model();
|
|
|
|
model::activity::Entity::insert(activity_model)
|
|
|
|
.exec(self.db())
|
|
|
|
.await?;
|
2024-05-02 15:29:42 +02:00
|
|
|
let mut expanded_addressing = self.expand_addressing(activity.addressed()).await?;
|
|
|
|
if expanded_addressing.is_empty() { // WHY MASTODON!!!!!!!
|
|
|
|
expanded_addressing.push(
|
|
|
|
model::object::Entity::find_by_id(&oid)
|
|
|
|
.select_only()
|
|
|
|
.select_column(model::object::Column::AttributedTo)
|
|
|
|
.into_tuple::<String>()
|
|
|
|
.one(self.db())
|
|
|
|
.await?
|
|
|
|
.ok_or_else(UpubError::not_found)?
|
|
|
|
);
|
|
|
|
}
|
2024-04-21 15:41:29 +02:00
|
|
|
self.address_to(Some(aid), None, &expanded_addressing).await?;
|
2024-04-09 01:14:48 +02:00
|
|
|
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.clone()))
|
|
|
|
.exec(self.db())
|
|
|
|
.await?;
|
2024-04-14 05:53:36 +02:00
|
|
|
tracing::info!("{} liked {}", uid, oid);
|
2024-04-09 01:14:48 +02:00
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-22 22:52:05 +02:00
|
|
|
async fn follow(&self, _: String, activity: serde_json::Value) -> crate::Result<()> {
|
2024-04-09 01:14:48 +02:00
|
|
|
let activity_model = model::activity::Model::new(&activity)?;
|
|
|
|
let aid = activity_model.id.clone();
|
2024-05-02 14:36:56 +02:00
|
|
|
let target_user_id = activity_model.object
|
|
|
|
.as_deref()
|
|
|
|
.ok_or_else(UpubError::bad_request)?
|
|
|
|
.to_string();
|
|
|
|
tracing::info!("{} wants to follow {}", activity_model.actor, target_user_id);
|
2024-04-09 01:14:48 +02:00
|
|
|
model::activity::Entity::insert(activity_model.into_active_model())
|
|
|
|
.exec(self.db()).await?;
|
2024-05-02 14:36:56 +02:00
|
|
|
let mut expanded_addressing = self.expand_addressing(activity.addressed()).await?;
|
|
|
|
if !expanded_addressing.contains(&target_user_id) {
|
|
|
|
expanded_addressing.push(target_user_id);
|
|
|
|
}
|
2024-04-21 15:41:29 +02:00
|
|
|
self.address_to(Some(&aid), None, &expanded_addressing).await?;
|
2024-04-09 01:14:48 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-04-22 22:52:05 +02:00
|
|
|
async fn accept(&self, _: String, activity: serde_json::Value) -> crate::Result<()> {
|
2024-04-09 01:14:48 +02:00
|
|
|
// TODO what about TentativeAccept
|
|
|
|
let activity_model = model::activity::Model::new(&activity)?;
|
2024-04-29 21:58:08 +02:00
|
|
|
|
|
|
|
if let Some(mut r) = model::relay::Entity::find_by_id(&activity_model.actor)
|
|
|
|
.one(self.db())
|
|
|
|
.await?
|
|
|
|
{
|
|
|
|
r.accepted = true;
|
|
|
|
model::relay::Entity::update(r.into_active_model()).exec(self.db()).await?;
|
|
|
|
model::activity::Entity::insert(activity_model.clone().into_active_model())
|
|
|
|
.exec(self.db())
|
|
|
|
.await?;
|
|
|
|
tracing::info!("relay {} is now broadcasting to us", activity_model.actor);
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2024-04-12 18:40:24 +02:00
|
|
|
let Some(follow_request_id) = &activity_model.object else {
|
2024-04-09 01:14:48 +02:00
|
|
|
return Err(UpubError::bad_request());
|
|
|
|
};
|
|
|
|
let Some(follow_activity) = model::activity::Entity::find_by_id(follow_request_id)
|
|
|
|
.one(self.db()).await?
|
|
|
|
else {
|
|
|
|
return Err(UpubError::not_found());
|
|
|
|
};
|
|
|
|
if follow_activity.object.unwrap_or("".into()) != activity_model.actor {
|
|
|
|
return Err(UpubError::forbidden());
|
|
|
|
}
|
|
|
|
|
|
|
|
tracing::info!("{} accepted follow request by {}", activity_model.actor, follow_activity.actor);
|
|
|
|
|
2024-04-12 18:40:24 +02:00
|
|
|
model::activity::Entity::insert(activity_model.clone().into_active_model())
|
|
|
|
.exec(self.db())
|
|
|
|
.await?;
|
2024-04-18 03:41:27 +02:00
|
|
|
model::user::Entity::update_many()
|
|
|
|
.col_expr(
|
|
|
|
model::user::Column::FollowingCount,
|
|
|
|
Expr::col(model::user::Column::FollowingCount).add(1)
|
|
|
|
)
|
|
|
|
.filter(model::user::Column::Id.eq(&follow_activity.actor))
|
|
|
|
.exec(self.db())
|
|
|
|
.await?;
|
2024-04-09 01:14:48 +02:00
|
|
|
model::relation::Entity::insert(
|
|
|
|
model::relation::ActiveModel {
|
2024-05-02 14:36:56 +02:00
|
|
|
follower: Set(follow_activity.actor.clone()),
|
2024-04-09 01:14:48 +02:00
|
|
|
following: Set(activity_model.actor),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
).exec(self.db()).await?;
|
|
|
|
|
2024-05-02 14:36:56 +02:00
|
|
|
let mut expanded_addressing = self.expand_addressing(activity.addressed()).await?;
|
|
|
|
if !expanded_addressing.contains(&follow_activity.actor) {
|
|
|
|
expanded_addressing.push(follow_activity.actor);
|
|
|
|
}
|
2024-04-21 15:41:29 +02:00
|
|
|
self.address_to(Some(&activity_model.id), None, &expanded_addressing).await?;
|
2024-04-09 01:14:48 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-04-22 22:52:05 +02:00
|
|
|
async fn reject(&self, _: String, activity: serde_json::Value) -> crate::Result<()> {
|
2024-04-09 01:14:48 +02:00
|
|
|
// TODO what about TentativeReject?
|
|
|
|
let activity_model = model::activity::Model::new(&activity)?;
|
2024-04-12 18:40:24 +02:00
|
|
|
let Some(follow_request_id) = &activity_model.object else {
|
2024-04-09 01:14:48 +02:00
|
|
|
return Err(UpubError::bad_request());
|
|
|
|
};
|
|
|
|
let Some(follow_activity) = model::activity::Entity::find_by_id(follow_request_id)
|
|
|
|
.one(self.db()).await?
|
|
|
|
else {
|
|
|
|
return Err(UpubError::not_found());
|
|
|
|
};
|
|
|
|
if follow_activity.object.unwrap_or("".into()) != activity_model.actor {
|
|
|
|
return Err(UpubError::forbidden());
|
|
|
|
}
|
2024-04-12 18:40:24 +02:00
|
|
|
|
2024-04-09 01:14:48 +02:00
|
|
|
tracing::info!("{} rejected follow request by {}", activity_model.actor, follow_activity.actor);
|
2024-04-12 18:40:24 +02:00
|
|
|
|
|
|
|
model::activity::Entity::insert(activity_model.clone().into_active_model())
|
|
|
|
.exec(self.db())
|
|
|
|
.await?;
|
|
|
|
|
2024-05-02 14:36:56 +02:00
|
|
|
let mut expanded_addressing = self.expand_addressing(activity.addressed()).await?;
|
|
|
|
if !expanded_addressing.contains(&follow_activity.actor) {
|
|
|
|
expanded_addressing.push(follow_activity.actor);
|
|
|
|
}
|
2024-04-21 15:41:29 +02:00
|
|
|
self.address_to(Some(&activity_model.id), None, &expanded_addressing).await?;
|
2024-04-09 01:14:48 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-04-22 22:52:05 +02:00
|
|
|
async fn delete(&self, _: String, activity: serde_json::Value) -> crate::Result<()> {
|
2024-04-09 01:14:48 +02:00
|
|
|
// TODO verify the signature before just deleting lmao
|
|
|
|
let oid = activity.object().id().ok_or(UpubError::bad_request())?;
|
2024-04-19 16:15:05 +02:00
|
|
|
tracing::debug!("deleting '{oid}'"); // this is so spammy wtf!
|
2024-04-09 01:14:48 +02:00
|
|
|
// TODO maybe we should keep the tombstone?
|
|
|
|
model::user::Entity::delete_by_id(&oid).exec(self.db()).await.info_failed("failed deleting from users");
|
|
|
|
model::activity::Entity::delete_by_id(&oid).exec(self.db()).await.info_failed("failed deleting from activities");
|
|
|
|
model::object::Entity::delete_by_id(&oid).exec(self.db()).await.info_failed("failed deleting from objects");
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-04-22 22:52:05 +02:00
|
|
|
async fn update(&self, server: String, activity: serde_json::Value) -> crate::Result<()> {
|
2024-04-09 01:14:48 +02:00
|
|
|
let activity_model = model::activity::Model::new(&activity)?;
|
2024-04-22 22:52:05 +02:00
|
|
|
let aid = activity_model.id.clone();
|
2024-04-09 01:14:48 +02:00
|
|
|
let Some(object_node) = activity.object().extract() else {
|
|
|
|
// TODO we could process non-embedded activities or arrays but im lazy rn
|
|
|
|
tracing::error!("refusing to process activity without embedded object: {}", serde_json::to_string_pretty(&activity).unwrap());
|
|
|
|
return Err(UpubError::unprocessable());
|
|
|
|
};
|
|
|
|
let Some(oid) = object_node.id().map(|x| x.to_string()) else {
|
|
|
|
return Err(UpubError::bad_request());
|
|
|
|
};
|
2024-04-22 22:52:05 +02:00
|
|
|
// make sure we're allowed to edit this object
|
|
|
|
if let Some(object_author) = object_node.attributed_to().id() {
|
|
|
|
if server != Context::server(&object_author) {
|
|
|
|
return Err(UpubError::forbidden());
|
|
|
|
}
|
|
|
|
} else if server != Context::server(&oid) {
|
|
|
|
return Err(UpubError::forbidden());
|
|
|
|
};
|
2024-04-09 01:14:48 +02:00
|
|
|
match object_node.object_type() {
|
|
|
|
Some(apb::ObjectType::Actor(_)) => {
|
|
|
|
// TODO oof here is an example of the weakness of this model, we have to go all the way
|
|
|
|
// back up to serde_json::Value because impl Object != impl Actor
|
|
|
|
let actor_model = model::user::Model::new(&object_node)?;
|
2024-05-02 13:31:09 +02:00
|
|
|
let mut update_model = actor_model.into_active_model();
|
2024-05-02 15:15:16 +02:00
|
|
|
update_model.updated = sea_orm::Set(chrono::Utc::now());
|
2024-05-02 13:16:23 +02:00
|
|
|
update_model.reset(model::user::Column::Name);
|
|
|
|
update_model.reset(model::user::Column::Summary);
|
|
|
|
update_model.reset(model::user::Column::Image);
|
|
|
|
update_model.reset(model::user::Column::Icon);
|
|
|
|
model::user::Entity::update(update_model)
|
2024-04-09 01:14:48 +02:00
|
|
|
.exec(self.db()).await?;
|
|
|
|
},
|
|
|
|
Some(apb::ObjectType::Note) => {
|
|
|
|
let object_model = model::object::Model::new(&object_node)?;
|
2024-05-02 13:31:09 +02:00
|
|
|
let mut update_model = object_model.into_active_model();
|
2024-05-02 15:15:16 +02:00
|
|
|
update_model.updated = sea_orm::Set(Some(chrono::Utc::now()));
|
2024-05-02 13:16:23 +02:00
|
|
|
update_model.reset(model::object::Column::Name);
|
|
|
|
update_model.reset(model::object::Column::Summary);
|
|
|
|
update_model.reset(model::object::Column::Content);
|
|
|
|
update_model.reset(model::object::Column::Sensitive);
|
|
|
|
model::object::Entity::update(update_model)
|
2024-04-09 01:14:48 +02:00
|
|
|
.exec(self.db()).await?;
|
|
|
|
},
|
|
|
|
Some(t) => tracing::warn!("no side effects implemented for update type {t:?}"),
|
|
|
|
None => tracing::warn!("empty type on embedded updated object"),
|
|
|
|
}
|
2024-04-12 18:40:24 +02:00
|
|
|
|
2024-04-09 01:14:48 +02:00
|
|
|
tracing::info!("{} updated {}", aid, oid);
|
2024-04-12 18:40:24 +02:00
|
|
|
model::activity::Entity::insert(activity_model.into_active_model())
|
|
|
|
.exec(self.db())
|
|
|
|
.await?;
|
|
|
|
let expanded_addressing = self.expand_addressing(activity.addressed()).await?;
|
2024-04-21 15:41:29 +02:00
|
|
|
self.address_to(Some(&aid), Some(&oid), &expanded_addressing).await?;
|
2024-04-09 01:14:48 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-04-22 22:52:05 +02:00
|
|
|
async fn undo(&self, server: String, activity: serde_json::Value) -> crate::Result<()> {
|
2024-04-14 16:47:36 +02:00
|
|
|
let uid = activity.actor().id().ok_or_else(UpubError::bad_request)?;
|
|
|
|
// TODO in theory we could work with just object_id but right now only accept embedded
|
|
|
|
let undone_activity = activity.object().extract().ok_or_else(UpubError::bad_request)?;
|
|
|
|
let undone_aid = undone_activity.id().ok_or_else(UpubError::bad_request)?;
|
|
|
|
let undone_object_id = undone_activity.object().id().ok_or_else(UpubError::bad_request)?;
|
|
|
|
let activity_type = undone_activity.activity_type().ok_or_else(UpubError::bad_request)?;
|
2024-04-22 22:52:05 +02:00
|
|
|
let undone_activity_author = undone_activity.actor().id().ok_or_else(UpubError::bad_request)?;
|
|
|
|
|
|
|
|
// can't undo activities from remote actors!
|
|
|
|
if server != Context::server(&undone_activity_author) {
|
|
|
|
return Err(UpubError::forbidden());
|
|
|
|
};
|
2024-04-14 16:47:36 +02:00
|
|
|
|
|
|
|
match activity_type {
|
|
|
|
apb::ActivityType::Like => {
|
|
|
|
model::like::Entity::delete_many()
|
|
|
|
.filter(
|
|
|
|
Condition::all()
|
|
|
|
.add(model::like::Column::Actor.eq(&uid))
|
|
|
|
.add(model::like::Column::Likes.eq(&undone_object_id))
|
|
|
|
)
|
|
|
|
.exec(self.db())
|
|
|
|
.await?;
|
|
|
|
model::object::Entity::update_many()
|
|
|
|
.filter(model::object::Column::Id.eq(&undone_object_id))
|
|
|
|
.col_expr(model::object::Column::Likes, Expr::col(model::object::Column::Likes).sub(1))
|
|
|
|
.exec(self.db())
|
|
|
|
.await?;
|
|
|
|
},
|
|
|
|
apb::ActivityType::Follow => {
|
|
|
|
model::relation::Entity::delete_many()
|
|
|
|
.filter(
|
|
|
|
Condition::all()
|
|
|
|
.add(model::relation::Column::Follower.eq(&uid))
|
|
|
|
.add(model::relation::Column::Following.eq(&undone_object_id))
|
|
|
|
)
|
|
|
|
.exec(self.db())
|
|
|
|
.await?;
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
tracing::error!("received 'Undo' for unimplemented activity: {}", serde_json::to_string_pretty(&activity).unwrap());
|
|
|
|
return Err(StatusCode::NOT_IMPLEMENTED.into());
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
model::activity::Entity::delete_by_id(undone_aid).exec(self.db()).await?;
|
|
|
|
|
|
|
|
Ok(())
|
2024-04-20 04:33:23 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-04-22 22:52:05 +02:00
|
|
|
async fn announce(&self, _: String, activity: serde_json::Value) -> crate::Result<()> {
|
2024-04-20 04:33:23 +02:00
|
|
|
let activity_model = model::activity::Model::new(&activity)?;
|
|
|
|
let Some(oid) = &activity_model.object else {
|
|
|
|
return Err(FieldError("object").into());
|
|
|
|
};
|
|
|
|
self.fetch_object(oid).await?;
|
2024-04-29 21:58:08 +02:00
|
|
|
|
|
|
|
// relays send us activities as Announce, but we don't really want to count those towards the
|
|
|
|
// total shares count of an object, so just fetch the object and be done with it
|
2024-04-30 15:33:41 +02:00
|
|
|
if self.is_relay(&activity_model.actor) {
|
2024-04-29 21:58:08 +02:00
|
|
|
tracing::info!("relay {} broadcasted {}", activity_model.actor, oid);
|
|
|
|
return Ok(())
|
|
|
|
}
|
|
|
|
|
2024-04-20 04:33:23 +02:00
|
|
|
let share = model::share::ActiveModel {
|
|
|
|
id: sea_orm::ActiveValue::NotSet,
|
|
|
|
actor: sea_orm::Set(activity_model.actor.clone()),
|
|
|
|
shares: sea_orm::Set(oid.clone()),
|
|
|
|
date: sea_orm::Set(activity.published().unwrap_or(chrono::Utc::now())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let expanded_addressing = self.expand_addressing(activity.addressed()).await?;
|
2024-04-21 15:41:29 +02:00
|
|
|
self.address_to(Some(&activity_model.id), None, &expanded_addressing).await?;
|
2024-04-20 04:33:23 +02:00
|
|
|
model::share::Entity::insert(share)
|
|
|
|
.exec(self.db()).await?;
|
|
|
|
model::activity::Entity::insert(activity_model.clone().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.clone()))
|
|
|
|
.exec(self.db())
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
tracing::info!("{} shared {}", activity_model.actor, oid);
|
|
|
|
Ok(())
|
2024-04-09 01:14:48 +02:00
|
|
|
}
|
|
|
|
}
|