2022-12-19 20:20:13 +00:00
|
|
|
use std::sync::Arc;
|
2023-03-03 01:19:48 +00:00
|
|
|
use deunicode::deunicode;
|
2022-12-19 20:20:13 +00:00
|
|
|
use sigh::{PublicKey, Key};
|
|
|
|
|
|
|
|
use crate::activitypub;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub enum ActorKind {
|
|
|
|
TagRelay(String),
|
|
|
|
InstanceRelay(String),
|
|
|
|
}
|
|
|
|
|
2023-03-03 01:19:48 +00:00
|
|
|
impl ActorKind {
|
|
|
|
pub fn from_tag(tag: &str) -> Self {
|
|
|
|
let tag = deunicode(tag)
|
|
|
|
.to_lowercase()
|
|
|
|
.replace(char::is_whitespace, "");
|
|
|
|
ActorKind::TagRelay(tag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-19 20:20:13 +00:00
|
|
|
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub struct Actor {
|
|
|
|
pub host: Arc<String>,
|
|
|
|
pub kind: ActorKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Actor {
|
2023-10-31 03:02:31 +00:00
|
|
|
pub fn from_uri(mut uri: &str) -> Option<Self> {
|
|
|
|
if ! uri.starts_with("https://") {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
uri = &uri[8..];
|
|
|
|
|
|
|
|
let parts = uri.split("/").collect::<Vec<_>>();
|
|
|
|
if parts.len() != 3 {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let Ok(topic) = urlencoding::decode(parts[2]) else { return None; };
|
|
|
|
let kind = match parts[1] {
|
|
|
|
"tag" =>
|
|
|
|
ActorKind::TagRelay(topic.to_string()),
|
|
|
|
"instance" =>
|
|
|
|
ActorKind::InstanceRelay(topic.to_string()),
|
|
|
|
_ =>
|
|
|
|
return None,
|
|
|
|
};
|
|
|
|
let host = Arc::new(parts[0].to_string());
|
|
|
|
Some(Actor { host, kind })
|
|
|
|
}
|
|
|
|
|
2022-12-19 20:20:13 +00:00
|
|
|
pub fn uri(&self) -> String {
|
|
|
|
match &self.kind {
|
|
|
|
ActorKind::TagRelay(tag) =>
|
|
|
|
format!("https://{}/tag/{}", self.host, tag),
|
|
|
|
ActorKind::InstanceRelay(instance) =>
|
|
|
|
format!("https://{}/instance/{}", self.host, instance),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn key_id(&self) -> String {
|
|
|
|
format!("{}#key", self.uri())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn as_activitypub(&self, pub_key: &PublicKey) -> activitypub::Actor {
|
|
|
|
activitypub::Actor {
|
|
|
|
jsonld_context: serde_json::Value::String("https://www.w3.org/ns/activitystreams".to_string()),
|
|
|
|
actor_type: "Service".to_string(),
|
|
|
|
id: self.uri(),
|
2022-12-23 17:56:45 +00:00
|
|
|
name: Some(match &self.kind {
|
|
|
|
ActorKind::TagRelay(tag) =>
|
|
|
|
format!("#{}", tag),
|
|
|
|
ActorKind::InstanceRelay(instance) =>
|
|
|
|
instance.to_string(),
|
|
|
|
}),
|
|
|
|
icon: Some(activitypub::Media {
|
2023-10-29 23:29:11 +00:00
|
|
|
media_type: Some("Image".to_string()),
|
|
|
|
content_type: Some("image/jpeg".to_string()),
|
2022-12-23 17:56:45 +00:00
|
|
|
url: "https://fedi.buzz/assets/favicon48.png".to_string(),
|
|
|
|
}),
|
2022-12-19 20:20:13 +00:00
|
|
|
inbox: self.uri(),
|
2023-10-30 00:21:30 +00:00
|
|
|
endpoints: Some(activitypub::ActorEndpoints {
|
|
|
|
shared_inbox: format!("https://{}/instance/{}", self.host, self.host),
|
|
|
|
}),
|
2023-10-29 23:29:11 +00:00
|
|
|
outbox: Some(format!("{}/outbox", self.uri())),
|
2022-12-19 20:20:13 +00:00
|
|
|
public_key: activitypub::ActorPublicKey {
|
|
|
|
id: self.key_id(),
|
|
|
|
owner: Some(self.uri()),
|
|
|
|
pem: pub_key.to_pem().unwrap(),
|
|
|
|
},
|
|
|
|
preferred_username: Some(match &self.kind {
|
|
|
|
ActorKind::TagRelay(tag) =>
|
|
|
|
format!("tag-{}", tag),
|
|
|
|
ActorKind::InstanceRelay(instance) =>
|
|
|
|
format!("instance-{}", instance),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|