hyaenidae/profiles/src/apub/results/submission.rs
asonix ab8aa2cbe1 Profiles: expose needed information for non-apub profile creation
- Add image purge to profile update
- Add outbound processing in apub ingest
- Add follower + follow request cleanup in profile delete
- Add inboxes + id to Outbound trait
2021-01-06 02:21:17 -06:00

177 lines
5.1 KiB
Rust

use super::{SubmissionCreated, SubmissionDeleted, SubmissionUpdated};
use crate::{
store::{FileSource, Submission, Visibility},
Context, Error, Outbound, Required,
};
use activitystreams::{
activity::{Create, Delete, Update},
base::AnyBase,
context,
object::Note,
prelude::*,
public, security,
};
use std::collections::HashSet;
use url::Url;
use uuid::Uuid;
fn remote_inboxes(submissioner_id: Uuid, ctx: &Context) -> Result<Vec<Url>, Error> {
let follower_inboxes = ctx
.store
.view
.follows
.forward_iter(submissioner_id)
.filter_map(|follow_id| ctx.store.view.follows.right(follow_id).ok())
.filter_map(|opt| opt)
.filter_map(|follower_id| {
Some(
ctx.apub
.endpoints_for_profile(follower_id)
.ok()??
.shared_inbox,
)
})
.collect::<HashSet<_>>();
Ok(follower_inboxes.into_iter().collect())
}
fn build_submission(
submission: Submission,
note_id: Url,
person_id: Url,
ctx: &Context,
) -> Result<AnyBase, Error> {
let published = submission.published().req()?;
let endpoints = ctx
.apub
.endpoints_for_profile(submission.profile_id())?
.req()?;
let mut note = Note::new();
note.set_id(note_id)
.set_summary(submission.title())
.set_published(published.into())
.set_attributed_to(person_id);
if let Some(description) = submission.description() {
note.set_content(description);
}
match submission.visibility() {
Visibility::Public => {
note.add_to(public()).add_cc(endpoints.followers);
}
Visibility::Unlisted => {
note.add_to(endpoints.followers).add_cc(public());
}
Visibility::Followers => {
note.add_to(endpoints.followers);
}
}
for file in submission.files() {
let file = ctx.store.files.by_id(*file)?.req()?;
let FileSource::PictRs(pictrs_file) = file.source();
note.add_attachment(ctx.pictrs.image_url(pictrs_file.key()));
}
let any_base = note.into_any_base()?;
ctx.apub.store_object(&any_base)?;
Ok(any_base)
}
impl Outbound for SubmissionCreated {
fn id(&self) -> Option<Uuid> {
Some(self.submission_id)
}
fn inboxes(&self, ctx: &Context) -> Result<Vec<Url>, Error> {
let profile_id = ctx
.store
.submissions
.by_id(self.submission_id)?
.req()?
.profile_id();
remote_inboxes(profile_id, ctx)
}
fn to_apub(&self, ctx: &Context) -> Result<AnyBase, Error> {
let submission = ctx.store.submissions.by_id(self.submission_id)?.req()?;
let person_id = ctx.apub.apub_for_profile(submission.profile_id())?.req()?;
let note_id = ctx.apub.info.gen_id().req()?;
let note = build_submission(submission, note_id, person_id.clone(), ctx)?;
let mut create = Create::new(person_id, note);
create
.set_id(ctx.apub.info.gen_id().req()?)
.add_context(context())
.add_context(security());
let any_base = create.into_any_base()?;
ctx.apub.store_object(&any_base)?;
Ok(any_base)
}
}
impl Outbound for SubmissionUpdated {
fn id(&self) -> Option<Uuid> {
Some(self.submission_id)
}
fn inboxes(&self, ctx: &Context) -> Result<Vec<Url>, Error> {
let profile_id = ctx
.store
.submissions
.by_id(self.submission_id)?
.req()?
.profile_id();
remote_inboxes(profile_id, ctx)
}
fn to_apub(&self, ctx: &Context) -> Result<AnyBase, Error> {
let submission = ctx.store.submissions.by_id(self.submission_id)?.req()?;
let person_id = ctx.apub.apub_for_profile(submission.profile_id())?.req()?;
let note_id = ctx.apub.apub_for_submission(submission.id())?.req()?;
let note = build_submission(submission, person_id.clone(), note_id, ctx)?;
let mut update = Update::new(person_id, note);
update
.set_id(ctx.apub.info.gen_id().req()?)
.add_context(context())
.add_context(security());
let any_base = update.into_any_base()?;
ctx.apub.store_object(&any_base)?;
Ok(any_base)
}
}
impl Outbound for SubmissionDeleted {
fn id(&self) -> Option<Uuid> {
None
}
fn inboxes(&self, ctx: &Context) -> Result<Vec<Url>, Error> {
remote_inboxes(self.profile_id, ctx)
}
fn to_apub(&self, ctx: &Context) -> Result<AnyBase, Error> {
let person_id = ctx.apub.apub_for_profile(self.profile_id)?.req()?;
let note_id = ctx.apub.apub_for_submission(self.submission_id)?.req()?;
let note = ctx.apub.object(&note_id)?.req()?;
let mut delete = Delete::new(person_id, note);
delete
.set_id(ctx.apub.info.gen_id().req()?)
.add_context(context())
.add_context(security());
let any_base = delete.into_any_base()?;
ctx.apub.store_object(&any_base)?;
Ok(any_base)
}
}