hyaenidae/profiles/src/apub/results/follow_request.rs

137 lines
4.4 KiB
Rust

use super::{
FollowRequestAccepted, FollowRequestDeleted, FollowRequestRejected, FollowRequested,
UndoneFollowRequestAccepted, Unfollowed,
};
use crate::{Completed, Context, Error, Required};
use activitystreams::{
activity::{Accept, Follow, Reject, Undo},
base::AnyBase,
context,
prelude::*,
security,
};
impl Completed for FollowRequested {
fn to_apub(&self, ctx: &Context) -> Result<AnyBase, Error> {
let follow = ctx
.store
.view
.follow_requests
.by_id(self.follow_request_id)?
.req()?;
let actor_id = ctx.apub.apub_for_profile(follow.right)?.req()?;
let object_id = ctx.apub.apub_for_profile(follow.left)?.req()?;
let published = follow.published;
let mut follow = Follow::new(actor_id, object_id);
follow
.set_id(ctx.apub.info.gen_id())
.set_published(published.into())
.add_context(context())
.add_context(security());
let follow = follow.into_any_base()?;
ctx.apub.store_object(&follow)?;
Ok(follow)
}
}
impl Completed for FollowRequestDeleted {
fn to_apub(&self, ctx: &Context) -> Result<AnyBase, Error> {
let person_id = ctx.apub.apub_for_profile(self.profile_id)?.req()?;
let follow_request_id = ctx
.apub
.apub_for_follow_request(self.follow_request_id)?
.req()?;
let follow_request = ctx.apub.object(&follow_request_id)?.req()?;
let mut undo = Undo::new(person_id, follow_request);
undo.set_id(ctx.apub.info.gen_id())
.add_context(context())
.add_context(security());
let undo = undo.into_any_base()?;
ctx.apub.store_object(&undo)?;
Ok(undo)
}
}
impl Completed for FollowRequestRejected {
fn to_apub(&self, ctx: &Context) -> Result<AnyBase, Error> {
let person_id = ctx.apub.apub_for_profile(self.profile_id)?.req()?;
let follow_request_id = ctx
.apub
.apub_for_follow_request(self.follow_request_id)?
.req()?;
let follow_request = ctx.apub.object(&follow_request_id)?.req()?;
let mut reject = Reject::new(person_id, follow_request);
reject
.set_id(ctx.apub.info.gen_id())
.add_context(context())
.add_context(security());
let reject = reject.into_any_base()?;
ctx.apub.store_object(&reject)?;
Ok(reject)
}
}
impl Completed for FollowRequestAccepted {
fn to_apub(&self, ctx: &Context) -> Result<AnyBase, Error> {
let person_id = ctx.apub.apub_for_profile(self.profile_id)?.req()?;
let follow_request_id = ctx
.apub
.apub_for_follow_request(self.follow_request_id)?
.req()?;
let follow_request = ctx.apub.object(&follow_request_id)?.req()?;
let accept_id = ctx.apub.info.gen_id();
ctx.apub.follow(&accept_id, self.follow_id)?;
let mut accept = Accept::new(person_id, follow_request);
accept
.set_id(accept_id)
.add_context(context())
.add_context(security());
let accept = accept.into_any_base()?;
ctx.apub.store_object(&accept)?;
Ok(accept)
}
}
impl Completed for UndoneFollowRequestAccepted {
fn to_apub(&self, ctx: &Context) -> Result<AnyBase, Error> {
let person_id = ctx.apub.apub_for_profile(self.profile_id)?.req()?;
let follow_id = ctx.apub.apub_for_follow(self.follow_id)?.req()?;
let follow = ctx.apub.object(&follow_id)?.req()?;
let mut undo = Undo::new(person_id, follow);
undo.set_id(ctx.apub.info.gen_id())
.add_context(context())
.add_context(security());
let undo = undo.into_any_base()?;
ctx.apub.store_object(&undo)?;
Ok(undo)
}
}
impl Completed for Unfollowed {
fn to_apub(&self, ctx: &Context) -> Result<AnyBase, Error> {
let person_id = ctx.apub.apub_for_profile(self.profile_id)?.req()?;
let follow_id = ctx.apub.apub_for_follow(self.follow_id)?.req()?;
let follow = ctx.apub.object(&follow_id)?.req()?;
let mut undo = Undo::new(person_id, follow);
undo.set_id(ctx.apub.info.gen_id())
.add_context(context())
.add_context(security());
let undo = undo.into_any_base()?;
ctx.apub.store_object(&undo)?;
Ok(undo)
}
}