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

186 lines
6.4 KiB
Rust

use super::{ProfileCreated, ProfileDeleted, ProfileUpdated};
use crate::{
apub::{ExtendedPerson, PublicKey, PublicKeyInner},
store::FileSource,
Completed, Context, Error, Required,
};
use activitystreams::{
activity::{Create, Delete, Update},
actor::{ApActor, Endpoints, Person},
base::AnyBase,
context,
prelude::*,
public, security,
};
impl Completed for ProfileCreated {
fn to_apub(&self, ctx: &Context) -> Result<AnyBase, Error> {
let profile = ctx.store.profiles.by_id(self.profile_id)?.req()?;
let person_id = ctx.apub.info.gen_id();
let public_key_id = ctx.apub.info.public_key(&person_id).req()?;
let following = ctx.apub.info.following(&person_id).req()?;
let followers = ctx.apub.info.followers(&person_id).req()?;
let inbox = ctx.apub.info.inbox(&person_id).req()?;
let outbox = ctx.apub.info.outbox(&person_id).req()?;
let shared_inbox = ctx.apub.info.shared_inbox();
ctx.apub.profile(&person_id, profile.id())?;
ctx.apub.store_endpoints(
profile.id(),
&person_id,
crate::apub::Endpoints {
inbox: inbox.clone(),
outbox: outbox.clone(),
following: following.clone(),
followers: followers.clone(),
shared_inbox: shared_inbox.clone(),
public_key: public_key_id.clone(),
},
)?;
let public_key_pem = ctx.apub.gen_keys(profile.id(), &person_id)?;
let mut person = ExtendedPerson::new(
ApActor::new(inbox, Person::new()),
PublicKey {
public_key: PublicKeyInner {
id: public_key_id,
owner: person_id.clone(),
public_key_pem,
},
},
);
person
.set_id(person_id.clone())
.set_following(following)
.set_followers(followers)
.set_outbox(outbox)
.set_endpoints(Endpoints {
shared_inbox: Some(shared_inbox),
..Endpoints::default()
})
.set_preferred_username(profile.handle())
.set_published(profile.published().into());
if let Some(display_name) = profile.display_name() {
person.set_name(display_name);
}
if let Some(description) = profile.description() {
person.set_summary(description);
}
if let Some(icon) = profile.icon() {
let file = ctx.store.files.by_id(icon)?.req()?;
let FileSource::PictRs(pictrs_file) = file.source();
person.set_icon(ctx.pictrs.image_url(pictrs_file.key()));
}
if let Some(banner) = profile.banner() {
let file = ctx.store.files.by_id(banner)?.req()?;
let FileSource::PictRs(pictrs_file) = file.source();
person.set_image(ctx.pictrs.image_url(pictrs_file.key()));
}
if !profile.login_required() {
person.add_to(public());
}
let any_base = person.into_any_base()?;
ctx.apub.store_object(&any_base)?;
let mut create = Create::new(person_id, any_base);
create
.set_id(ctx.apub.info.gen_id())
.add_context(context())
.add_context(security());
let any_base = create.into_any_base()?;
ctx.apub.store_object(&any_base)?;
Ok(any_base)
}
}
impl Completed for ProfileUpdated {
fn to_apub(&self, ctx: &Context) -> Result<AnyBase, Error> {
let profile = ctx.store.profiles.by_id(self.profile_id)?.req()?;
let endpoints = ctx.apub.endpoints_for_profile(self.profile_id)?.req()?;
let public_key_id = ctx.apub.key_for_profile(profile.id())?.req()?;
let public_key_pem = ctx.apub.public_key_for_id(&public_key_id)?.req()?;
let person_id = ctx.apub.apub_for_profile(self.profile_id)?.req()?;
let mut person = ExtendedPerson::new(
ApActor::new(endpoints.inbox, Person::new()),
PublicKey {
public_key: PublicKeyInner {
id: public_key_id,
owner: person_id.clone(),
public_key_pem,
},
},
);
person
.set_id(person_id.clone())
.set_following(endpoints.following)
.set_followers(endpoints.followers)
.set_outbox(endpoints.outbox)
.set_endpoints(Endpoints {
shared_inbox: Some(endpoints.shared_inbox),
..Endpoints::default()
})
.set_preferred_username(profile.handle())
.set_published(profile.published().into());
if let Some(display_name) = profile.display_name() {
person.set_name(display_name);
}
if let Some(description) = profile.description() {
person.set_summary(description);
}
if let Some(icon) = profile.icon() {
let file = ctx.store.files.by_id(icon)?.req()?;
let FileSource::PictRs(pictrs_file) = file.source();
person.set_icon(ctx.pictrs.image_url(pictrs_file.key()));
}
if let Some(banner) = profile.banner() {
let file = ctx.store.files.by_id(banner)?.req()?;
let FileSource::PictRs(pictrs_file) = file.source();
person.set_image(ctx.pictrs.image_url(pictrs_file.key()));
}
if !profile.login_required() {
person.add_to(public());
}
let any_base = person.into_any_base()?;
ctx.apub.store_object(&any_base)?;
let mut update = Update::new(person_id, any_base);
update
.set_id(ctx.apub.info.gen_id())
.add_context(context())
.add_context(security());
let any_base = update.into_any_base()?;
ctx.apub.store_object(&any_base)?;
Ok(any_base)
}
}
impl Completed for ProfileDeleted {
fn to_apub(&self, ctx: &Context) -> Result<AnyBase, Error> {
let person_id = ctx.apub.apub_for_profile(self.profile_id)?.req()?;
let person = ctx.apub.object(&person_id)?.req()?;
let mut delete = Delete::new(person_id, person);
delete
.set_id(ctx.apub.info.gen_id())
.add_context(context())
.add_context(security());
let any_base = delete.into_any_base()?;
ctx.apub.store_object(&any_base)?;
Ok(any_base)
}
}