feat: add context to outgoing posts

This commit is contained in:
əlemi 2024-04-19 04:25:17 +02:00
parent 973a3060ac
commit 67c4051226
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -3,7 +3,7 @@ use sea_orm::{sea_query::Expr, ColumnTrait, EntityTrait, IntoActiveModel, QueryF
use crate::{errors::UpubError, model}; use crate::{errors::UpubError, model};
use super::Context; use super::{fetcher::Fetcher, Context};
#[axum::async_trait] #[axum::async_trait]
@ -16,12 +16,20 @@ impl apb::server::Outbox for Context {
let oid = self.oid(uuid::Uuid::new_v4().to_string()); let oid = self.oid(uuid::Uuid::new_v4().to_string());
let aid = self.aid(uuid::Uuid::new_v4().to_string()); let aid = self.aid(uuid::Uuid::new_v4().to_string());
let activity_targets = object.addressed(); let activity_targets = object.addressed();
let object_model = model::object::Model::new( let mut object_model = model::object::Model::new(
&object &object
.set_id(Some(&oid)) .set_id(Some(&oid))
.set_attributed_to(Node::link(uid.clone())) .set_attributed_to(Node::link(uid.clone()))
.set_published(Some(chrono::Utc::now())) .set_published(Some(chrono::Utc::now()))
)?; )?;
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
}
let activity_model = model::activity::Model { let activity_model = model::activity::Model {
id: aid.clone(), id: aid.clone(),
activity_type: apb::ActivityType::Create, activity_type: apb::ActivityType::Create,
@ -65,11 +73,18 @@ impl apb::server::Outbox for Context {
.set_actor(Node::link(uid.clone())) .set_actor(Node::link(uid.clone()))
.set_published(Some(chrono::Utc::now())) .set_published(Some(chrono::Utc::now()))
)?; )?;
activity_model.object = Some(oid.clone());
object_model.to = activity_model.to.clone(); object_model.to = activity_model.to.clone();
object_model.bto = activity_model.bto.clone(); object_model.bto = activity_model.bto.clone();
object_model.cc = activity_model.cc.clone(); object_model.cc = activity_model.cc.clone();
object_model.bcc = activity_model.bcc.clone(); object_model.bcc = activity_model.bcc.clone();
activity_model.object = Some(oid.clone()); 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
}
model::object::Entity::insert(object_model.into_active_model()) model::object::Entity::insert(object_model.into_active_model())
.exec(self.db()).await?; .exec(self.db()).await?;