hyaenidae/profiles/src/apub/actions/react.rs

90 lines
2.5 KiB
Rust

use crate::{
apub::actions::{CreateReact, DeleteReact},
Action, Context, Error, Outbound, Required,
};
impl Action for CreateReact {
fn perform(&self, context: &Context) -> Result<Option<Box<dyn Outbound + Send>>, Error> {
let submissioner_id = context
.store
.submissions
.by_id(self.submission_id)?
.req()?
.profile_id();
context.check_block(submissioner_id, self.profile_id)?;
let commenter_id = if let Some(comment_id) = self.comment_id {
let commenter_id = context
.store
.comments
.by_id(comment_id)?
.req()?
.profile_id();
context.check_block(commenter_id, self.profile_id)?;
Some(commenter_id)
} else {
None
};
let react = context.store.reacts.create(
self.submission_id,
self.profile_id,
self.comment_id,
&self.react,
self.published,
)?;
if let Some(apub_id) = &self.like_apub_id {
context.apub.react(&apub_id, react.id())?;
}
let notifier_id = commenter_id.unwrap_or(submissioner_id);
if let Ok(Some(true)) = context.store.profiles.is_local(notifier_id) {
context
.store
.view
.reacts
.new(notifier_id, react.id(), self.published);
}
if context.is_local(react.profile_id())? {
return Ok(Some(Box::new(crate::apub::results::React {
react_id: react.id(),
})));
}
Ok(None)
}
}
impl Action for DeleteReact {
fn perform(&self, context: &Context) -> Result<Option<Box<dyn Outbound + Send>>, Error> {
let react_id = self.react_id;
let opt = context.store.reacts.delete(react_id)?;
if let Some(undo_react) = opt {
context.store.view.reacts.remove(react_id);
let like_apub_id = context.apub.apub_for_react(react_id)?.req()?;
context.apub.delete_object(&like_apub_id)?;
let profile_id = undo_react.0.profile_id();
if context.is_local(profile_id)? {
return Ok(Some(Box::new(crate::apub::results::UndoReact {
like_apub_id,
profile_id,
submission_id: undo_react.0.submission_id(),
comment_id: undo_react.0.comment_id(),
})));
}
}
Ok(None)
}
}