relay/src/apub.rs

188 lines
4.6 KiB
Rust
Raw Normal View History

2020-03-15 02:05:40 +00:00
use activitystreams::{
2020-03-18 22:49:56 +00:00
actor::Actor,
ext::Extension,
2020-03-15 02:05:40 +00:00
object::{Object, ObjectBox},
primitives::XsdAnyUri,
2020-03-18 22:49:56 +00:00
Base, BaseBox, PropRefs,
2020-03-15 02:05:40 +00:00
};
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,
}
2020-03-19 01:57:39 +00:00
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicKeyExtension {
pub public_key: PublicKey,
}
2020-03-16 03:36:46 +00:00
2020-03-18 22:49:56 +00:00
#[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize, PropRefs)]
2020-03-15 02:05:40 +00:00
#[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
}
2020-03-18 04:58:13 +00:00
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize)]
2020-03-15 02:05:40 +00:00
#[serde(rename_all = "PascalCase")]
pub enum ValidTypes {
Accept,
2020-03-15 02:05:40 +00:00
Announce,
Create,
Delete,
Follow,
Reject,
2020-03-15 02:05:40 +00:00
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,
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-19 01:57:39 +00:00
impl PublicKey {
pub fn to_ext(self) -> PublicKeyExtension {
self.into()
}
}
impl From<PublicKey> for PublicKeyExtension {
fn from(public_key: PublicKey) -> Self {
PublicKeyExtension { public_key }
}
}
impl<T> Extension<T> for PublicKeyExtension where T: Actor {}
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_id(&self) -> Option<XsdAnyUri> {
2020-03-15 22:37:53 +00:00
match self {
ValidObjects::Id(_) => None,
2020-03-15 22:37:53 +00:00
ValidObjects::Object(AnyExistingObject { ext, .. }) => {
if let Some(o) = ext.get("object") {
if let Ok(child_uri) = serde_json::from_value::<XsdAnyUri>(o.clone()) {
return Some(child_uri);
2020-03-15 22:37:53 +00:00
}
}
None
2020-03-15 22:37:53 +00:00
}
}
}
pub fn child_object_is(&self, uri: &XsdAnyUri) -> bool {
if let Some(child_object_id) = self.child_object_id() {
return *uri == child_object_id;
}
false
}
pub fn child_actor_id(&self) -> Option<XsdAnyUri> {
match self {
ValidObjects::Id(_) => None,
ValidObjects::Object(AnyExistingObject { ext, .. }) => {
if let Some(o) = ext.get("actor") {
if let Ok(child_uri) = serde_json::from_value::<XsdAnyUri>(o.clone()) {
return Some(child_uri);
}
}
None
}
}
}
pub fn child_actor_is(&self, uri: &XsdAnyUri) -> bool {
if let Some(child_actor_id) = self.child_actor_id() {
return *uri == child_actor_id;
}
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)
2020-03-15 17:49:27 +00:00
}
}