relay/src/apub.rs

153 lines
3.6 KiB
Rust
Raw Normal View History

2020-03-15 02:05:40 +00:00
use activitystreams::{
object::{Object, ObjectBox},
primitives::XsdAnyUri,
PropRefs,
};
2020-03-15 22:37:53 +00:00
use std::collections::HashMap;
2020-03-15 02:05:40 +00:00
2020-03-16 03:36:46 +00:00
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicKey {
pub id: XsdAnyUri,
pub owner: XsdAnyUri,
pub public_key_pem: String,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicKeyExtension<T> {
public_key: PublicKey,
#[serde(flatten)]
extending: T,
}
2020-03-15 02:05:40 +00:00
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, PropRefs)]
#[serde(rename_all = "camelCase")]
#[prop_refs(Object)]
pub struct AnyExistingObject {
pub id: XsdAnyUri,
#[serde(rename = "type")]
pub kind: String,
2020-03-15 22:37:53 +00:00
#[serde(flatten)]
ext: HashMap<String, serde_json::Value>,
2020-03-15 02:05:40 +00:00
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "PascalCase")]
pub enum ValidTypes {
Announce,
Create,
Delete,
Follow,
Undo,
2020-03-15 22:37:53 +00:00
Update,
2020-03-15 02:05:40 +00:00
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(untagged)]
#[serde(rename_all = "camelCase")]
pub enum ValidObjects {
Id(XsdAnyUri),
Object(AnyExistingObject),
}
2020-03-15 17:49:27 +00:00
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
2020-03-15 02:05:40 +00:00
#[serde(rename_all = "camelCase")]
pub struct AcceptedObjects {
pub id: XsdAnyUri,
#[serde(rename = "type")]
pub kind: ValidTypes,
pub actor: XsdAnyUri,
pub object: ValidObjects,
2020-03-15 22:37:53 +00:00
#[serde(flatten)]
ext: HashMap<String, serde_json::Value>,
2020-03-15 02:05:40 +00:00
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AcceptedActors {
pub id: XsdAnyUri,
#[serde(rename = "type")]
pub kind: String,
pub inbox: XsdAnyUri,
pub endpoints: Endpoints,
2020-03-16 04:15:50 +00:00
pub public_key: PublicKey,
2020-03-15 02:05:40 +00:00
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Endpoints {
shared_inbox: Option<XsdAnyUri>,
}
2020-03-16 03:36:46 +00:00
impl PublicKey {
pub fn extend<T>(self, extending: T) -> PublicKeyExtension<T> {
PublicKeyExtension {
public_key: self,
extending,
}
}
}
2020-03-15 02:05:40 +00:00
impl ValidObjects {
pub fn id(&self) -> &XsdAnyUri {
match self {
ValidObjects::Id(ref id) => id,
ValidObjects::Object(ref obj) => &obj.id,
}
}
2020-03-15 22:37:53 +00:00
2020-03-18 04:35:20 +00:00
pub fn kind(&self) -> Option<&str> {
match self {
ValidObjects::Id(_) => None,
ValidObjects::Object(AnyExistingObject { kind, .. }) => Some(kind),
}
}
2020-03-15 22:37:53 +00:00
pub fn is_kind(&self, query_kind: &str) -> bool {
match self {
ValidObjects::Id(_) => false,
ValidObjects::Object(AnyExistingObject { kind, .. }) => kind == query_kind,
}
}
pub fn is(&self, uri: &XsdAnyUri) -> bool {
match self {
ValidObjects::Id(id) => id == uri,
ValidObjects::Object(AnyExistingObject { id, .. }) => id == uri,
}
}
pub fn child_object_is(&self, uri: &XsdAnyUri) -> bool {
2020-03-15 22:37:53 +00:00
match self {
ValidObjects::Id(_) => false,
ValidObjects::Object(AnyExistingObject { ext, .. }) => {
if let Some(o) = ext.get("object") {
if let Ok(child_uri) = serde_json::from_value::<XsdAnyUri>(o.clone()) {
return child_uri == *uri;
2020-03-15 22:37:53 +00:00
}
}
false
}
}
}
2020-03-15 02:05:40 +00:00
}
2020-03-15 17:49:27 +00:00
impl AcceptedActors {
pub fn inbox(&self) -> &XsdAnyUri {
self.endpoints.shared_inbox.as_ref().unwrap_or(&self.inbox)
}
}