2021-11-03 17:33:51 +00:00
|
|
|
use crate::{
|
2023-09-09 16:25:03 +00:00
|
|
|
activities::GetActorType,
|
2021-11-03 17:33:51 +00:00
|
|
|
objects::{community::ApubCommunity, person::ApubPerson},
|
|
|
|
protocol::objects::{group::Group, person::Person},
|
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::{
|
|
|
|
config::Data,
|
|
|
|
traits::{Actor, Object},
|
|
|
|
};
|
2023-08-24 15:27:00 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
2022-11-28 14:29:33 +00:00
|
|
|
use lemmy_api_common::context::LemmyContext;
|
2023-09-09 16:25:03 +00:00
|
|
|
use lemmy_db_schema::source::activity::ActorType;
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
2022-05-06 23:53:33 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-11-19 17:47:06 +00:00
|
|
|
use url::Url;
|
2021-11-03 17:33:51 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum UserOrCommunity {
|
|
|
|
User(ApubPerson),
|
|
|
|
Community(ApubCommunity),
|
|
|
|
}
|
|
|
|
|
2022-05-06 23:53:33 +00:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
2021-11-03 17:33:51 +00:00
|
|
|
#[serde(untagged)]
|
|
|
|
pub enum PersonOrGroup {
|
|
|
|
Person(Person),
|
|
|
|
Group(Group),
|
|
|
|
}
|
|
|
|
|
2022-09-26 14:09:32 +00:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
|
2022-05-06 23:53:33 +00:00
|
|
|
pub enum PersonOrGroupType {
|
|
|
|
Person,
|
|
|
|
Group,
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl Object for UserOrCommunity {
|
2021-11-03 17:33:51 +00:00
|
|
|
type DataType = LemmyContext;
|
2023-03-21 15:03:05 +00:00
|
|
|
type Kind = PersonOrGroup;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
2021-11-03 17:33:51 +00:00
|
|
|
|
2023-08-24 15:27:00 +00:00
|
|
|
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
|
2021-11-03 17:33:51 +00:00
|
|
|
Some(match self {
|
|
|
|
UserOrCommunity::User(p) => p.last_refreshed_at,
|
|
|
|
UserOrCommunity::Community(p) => p.last_refreshed_at,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn read_from_id(
|
2021-11-03 17:33:51 +00:00
|
|
|
object_id: Url,
|
2023-03-21 15:03:05 +00:00
|
|
|
data: &Data<Self::DataType>,
|
2021-11-03 17:33:51 +00:00
|
|
|
) -> Result<Option<Self>, LemmyError> {
|
2023-03-21 15:03:05 +00:00
|
|
|
let person = ApubPerson::read_from_id(object_id.clone(), data).await?;
|
2021-11-03 17:33:51 +00:00
|
|
|
Ok(match person {
|
|
|
|
Some(o) => Some(UserOrCommunity::User(o)),
|
2023-03-21 15:03:05 +00:00
|
|
|
None => ApubCommunity::read_from_id(object_id, data)
|
2021-11-03 17:33:51 +00:00
|
|
|
.await?
|
|
|
|
.map(UserOrCommunity::Community),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn delete(self, data: &Data<Self::DataType>) -> Result<(), LemmyError> {
|
2021-11-03 17:33:51 +00:00
|
|
|
match self {
|
|
|
|
UserOrCommunity::User(p) => p.delete(data).await,
|
|
|
|
UserOrCommunity::Community(p) => p.delete(data).await,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn into_json(self, _data: &Data<Self::DataType>) -> Result<Self::Kind, LemmyError> {
|
2021-11-03 17:33:51 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-11-06 17:35:14 +00:00
|
|
|
async fn verify(
|
2023-03-21 15:03:05 +00:00
|
|
|
apub: &Self::Kind,
|
2021-11-06 17:35:14 +00:00
|
|
|
expected_domain: &Url,
|
2023-03-21 15:03:05 +00:00
|
|
|
data: &Data<Self::DataType>,
|
2021-11-06 17:35:14 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
match apub {
|
2023-03-21 15:03:05 +00:00
|
|
|
PersonOrGroup::Person(a) => ApubPerson::verify(a, expected_domain, data).await,
|
|
|
|
PersonOrGroup::Group(a) => ApubCommunity::verify(a, expected_domain, data).await,
|
2021-11-06 17:35:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn from_json(apub: Self::Kind, data: &Data<Self::DataType>) -> Result<Self, LemmyError> {
|
2021-11-03 17:33:51 +00:00
|
|
|
Ok(match apub {
|
2023-03-21 15:03:05 +00:00
|
|
|
PersonOrGroup::Person(p) => UserOrCommunity::User(ApubPerson::from_json(p, data).await?),
|
2021-11-06 17:35:14 +00:00
|
|
|
PersonOrGroup::Group(p) => {
|
2023-03-21 15:03:05 +00:00
|
|
|
UserOrCommunity::Community(ApubCommunity::from_json(p, data).await?)
|
2021-11-06 17:35:14 +00:00
|
|
|
}
|
2021-11-03 17:33:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 15:45:39 +00:00
|
|
|
impl Actor for UserOrCommunity {
|
2023-03-21 15:03:05 +00:00
|
|
|
fn id(&self) -> Url {
|
2021-11-03 17:33:51 +00:00
|
|
|
match self {
|
2023-03-21 15:03:05 +00:00
|
|
|
UserOrCommunity::User(u) => u.id(),
|
|
|
|
UserOrCommunity::Community(c) => c.id(),
|
2021-11-03 17:33:51 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-08 15:45:39 +00:00
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
fn public_key_pem(&self) -> &str {
|
2022-11-23 23:40:47 +00:00
|
|
|
match self {
|
2023-03-21 15:03:05 +00:00
|
|
|
UserOrCommunity::User(p) => p.public_key_pem(),
|
|
|
|
UserOrCommunity::Community(p) => p.public_key_pem(),
|
2022-11-23 23:40:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
fn private_key_pem(&self) -> Option<String> {
|
2022-11-23 23:40:47 +00:00
|
|
|
match self {
|
2023-03-21 15:03:05 +00:00
|
|
|
UserOrCommunity::User(p) => p.private_key_pem(),
|
|
|
|
UserOrCommunity::Community(p) => p.private_key_pem(),
|
2022-11-23 23:40:47 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-21 15:03:05 +00:00
|
|
|
|
|
|
|
fn inbox(&self) -> Url {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2022-11-23 23:40:47 +00:00
|
|
|
}
|
2023-09-09 16:25:03 +00:00
|
|
|
|
|
|
|
impl GetActorType for UserOrCommunity {
|
|
|
|
fn actor_type(&self) -> ActorType {
|
|
|
|
match self {
|
|
|
|
UserOrCommunity::User(p) => p.actor_type(),
|
|
|
|
UserOrCommunity::Community(p) => p.actor_type(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|