Plume/src/activity_pub/mod.rs

86 lines
2.6 KiB
Rust
Raw Normal View History

2018-04-23 13:57:14 +02:00
use models::instance::Instance;
use diesel::PgConnection;
2018-04-23 17:09:05 +02:00
use serde_json::Value;
2018-04-23 13:57:14 +02:00
2018-04-23 18:26:01 +02:00
pub enum ActorType {
Person,
Blog
}
impl ToString for ActorType {
fn to_string(&self) -> String {
String::from(match self {
ActorType::Person => "Person",
ActorType::Blog => "Blog"
})
}
}
pub trait Actor {
fn get_box_prefix() -> &'static str;
2018-04-23 13:57:14 +02:00
fn get_actor_id(&self) -> String;
fn get_instance(&self, conn: &PgConnection) -> Instance;
2018-04-23 18:26:01 +02:00
fn get_actor_type() -> ActorType;
fn as_activity_pub (&self, conn: &PgConnection) -> Value {
json!({
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
"sensitive": "as:sensitive",
"movedTo": "as:movedTo",
"Hashtag": "as:Hashtag",
"ostatus":"http://ostatus.org#",
"atomUri":"ostatus:atomUri",
"inReplyToAtomUri":"ostatus:inReplyToAtomUri",
"conversation":"ostatus:conversation",
"toot":"http://joinmastodon.org/ns#",
"Emoji":"toot:Emoji",
"focalPoint": {
"@container":"@list",
"@id":"toot:focalPoint"
},
"featured":"toot:featured"
}
],
"id": self.compute_id(conn),
"type": Self::get_actor_type().to_string(),
"inbox": self.compute_inbox(conn),
"outbox": self.compute_outbox(conn),
"preferredUsername": self.get_actor_id(),
"name": "",
"summary": "",
"url": self.compute_id(conn),
"endpoints": {
"sharedInbox": "https://plu.me/inbox"
}
})
2018-04-23 17:09:05 +02:00
}
2018-04-23 13:57:14 +02:00
fn compute_outbox(&self, conn: &PgConnection) -> String {
self.compute_box(conn, "outbox")
}
fn compute_inbox(&self, conn: &PgConnection) -> String {
self.compute_box(conn, "inbox")
}
fn compute_box(&self, conn: &PgConnection, box_name: &str) -> String {
2018-04-23 18:26:01 +02:00
format!("{id}/{name}", id = self.compute_id(conn), name = box_name)
}
fn compute_id(&self, conn: &PgConnection) -> String {
2018-04-23 13:57:14 +02:00
format!(
2018-04-23 18:26:01 +02:00
"https://{instance}/{prefix}/{user}",
2018-04-23 13:57:14 +02:00
instance = self.get_instance(conn).public_domain,
prefix = Self::get_box_prefix(),
2018-04-23 18:26:01 +02:00
user = self.get_actor_id()
2018-04-23 13:57:14 +02:00
)
}
}