2021-10-28 21:17:59 +00:00
|
|
|
use crate::{
|
2022-12-01 20:52:49 +00:00
|
|
|
activities::verify_community_matches,
|
2022-05-06 23:53:33 +00:00
|
|
|
fetcher::user_or_community::{PersonOrGroupType, UserOrCommunity},
|
2021-10-28 21:17:59 +00:00
|
|
|
objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
|
2022-12-01 20:52:49 +00:00
|
|
|
protocol::{objects::LanguageTag, ImageObject, InCommunity, Source},
|
2021-10-28 21:17:59 +00:00
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use activitypub_federation::{
|
2023-03-21 15:03:05 +00:00
|
|
|
config::Data,
|
|
|
|
fetch::object_id::ObjectId,
|
|
|
|
kinds::{
|
|
|
|
link::LinkType,
|
|
|
|
object::{DocumentType, ImageType},
|
|
|
|
},
|
|
|
|
protocol::{
|
2022-06-02 14:33:41 +00:00
|
|
|
helpers::{deserialize_one_or_many, deserialize_skip_error},
|
|
|
|
values::MediaTypeMarkdownOrHtml,
|
|
|
|
},
|
2023-03-21 15:03:05 +00:00
|
|
|
traits::{ActivityHandler, Object},
|
2022-11-12 13:51:08 +00:00
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use chrono::{DateTime, FixedOffset};
|
|
|
|
use itertools::Itertools;
|
2022-11-28 14:29:33 +00:00
|
|
|
use lemmy_api_common::context::LemmyContext;
|
2022-08-22 20:55:10 +00:00
|
|
|
use lemmy_db_schema::newtypes::DbUrl;
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorType};
|
2023-01-20 17:43:23 +00:00
|
|
|
use serde::{de::Error, Deserialize, Deserializer, Serialize};
|
2021-10-28 21:17:59 +00:00
|
|
|
use serde_with::skip_serializing_none;
|
|
|
|
use url::Url;
|
|
|
|
|
2022-09-26 14:09:32 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
|
2022-01-17 14:40:47 +00:00
|
|
|
pub enum PageType {
|
|
|
|
Page,
|
2022-03-29 15:48:29 +00:00
|
|
|
Article,
|
2022-01-17 14:40:47 +00:00
|
|
|
Note,
|
2022-05-06 23:53:33 +00:00
|
|
|
Video,
|
2022-11-12 13:51:08 +00:00
|
|
|
Event,
|
2022-01-17 14:40:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 21:17:59 +00:00
|
|
|
#[skip_serializing_none]
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Page {
|
2022-05-06 23:53:33 +00:00
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub(crate) kind: PageType,
|
2021-11-03 16:26:09 +00:00
|
|
|
pub(crate) id: ObjectId<ApubPost>,
|
2022-05-06 23:53:33 +00:00
|
|
|
pub(crate) attributed_to: AttributedTo,
|
2022-06-02 14:33:41 +00:00
|
|
|
#[serde(deserialize_with = "deserialize_one_or_many")]
|
2021-10-28 21:17:59 +00:00
|
|
|
pub(crate) to: Vec<Url>,
|
2023-01-20 17:43:23 +00:00
|
|
|
// If there is inReplyTo field this is actually a comment and must not be parsed
|
|
|
|
#[serde(deserialize_with = "deserialize_not_present", default)]
|
|
|
|
pub(crate) in_reply_to: Option<String>,
|
2022-02-17 22:04:01 +00:00
|
|
|
|
2023-01-20 17:43:23 +00:00
|
|
|
pub(crate) name: Option<String>,
|
2022-06-02 14:33:41 +00:00
|
|
|
#[serde(deserialize_with = "deserialize_one_or_many", default)]
|
2021-11-06 17:44:34 +00:00
|
|
|
pub(crate) cc: Vec<Url>,
|
2021-10-28 21:17:59 +00:00
|
|
|
pub(crate) content: Option<String>,
|
2022-05-06 23:53:33 +00:00
|
|
|
pub(crate) media_type: Option<MediaTypeMarkdownOrHtml>,
|
2022-06-02 14:33:41 +00:00
|
|
|
#[serde(deserialize_with = "deserialize_skip_error", default)]
|
2022-04-01 18:25:19 +00:00
|
|
|
pub(crate) source: Option<Source>,
|
|
|
|
/// most software uses array type for attachment field, so we do the same. nevertheless, we only
|
|
|
|
/// use the first item
|
|
|
|
#[serde(default)]
|
|
|
|
pub(crate) attachment: Vec<Attachment>,
|
2021-10-28 21:17:59 +00:00
|
|
|
pub(crate) image: Option<ImageObject>,
|
|
|
|
pub(crate) comments_enabled: Option<bool>,
|
|
|
|
pub(crate) sensitive: Option<bool>,
|
|
|
|
pub(crate) published: Option<DateTime<FixedOffset>>,
|
|
|
|
pub(crate) updated: Option<DateTime<FixedOffset>>,
|
2022-08-18 19:11:19 +00:00
|
|
|
pub(crate) language: Option<LanguageTag>,
|
2022-12-01 20:52:49 +00:00
|
|
|
pub(crate) audience: Option<ObjectId<ApubCommunity>>,
|
2021-10-28 21:17:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 18:25:19 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2022-08-16 10:12:43 +00:00
|
|
|
pub(crate) struct Link {
|
2022-04-01 18:25:19 +00:00
|
|
|
pub(crate) href: Url,
|
|
|
|
pub(crate) r#type: LinkType,
|
|
|
|
}
|
|
|
|
|
2022-08-16 10:12:43 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub(crate) struct Image {
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub(crate) kind: ImageType,
|
|
|
|
pub(crate) url: Url,
|
|
|
|
}
|
|
|
|
|
2022-11-12 13:51:08 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub(crate) struct Document {
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub(crate) kind: DocumentType,
|
|
|
|
pub(crate) url: Url,
|
|
|
|
}
|
|
|
|
|
2022-08-16 10:12:43 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
pub(crate) enum Attachment {
|
|
|
|
Link(Link),
|
|
|
|
Image(Image),
|
2022-11-12 13:51:08 +00:00
|
|
|
Document(Document),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Attachment {
|
|
|
|
pub(crate) fn url(self) -> Url {
|
|
|
|
match self {
|
|
|
|
// url as sent by Lemmy (new)
|
|
|
|
Attachment::Link(l) => l.href,
|
|
|
|
// image sent by lotide
|
|
|
|
Attachment::Image(i) => i.url,
|
|
|
|
// sent by mobilizon
|
|
|
|
Attachment::Document(d) => d.url,
|
|
|
|
}
|
|
|
|
}
|
2022-08-16 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 23:53:33 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
pub(crate) enum AttributedTo {
|
|
|
|
Lemmy(ObjectId<ApubPerson>),
|
|
|
|
Peertube([AttributedToPeertube; 2]),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub(crate) struct AttributedToPeertube {
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub kind: PersonOrGroupType,
|
|
|
|
pub id: ObjectId<UserOrCommunity>,
|
|
|
|
}
|
|
|
|
|
2021-10-28 21:17:59 +00:00
|
|
|
impl Page {
|
2023-06-07 19:18:17 +00:00
|
|
|
/// Only mods can change the post's locked status. So if it is changed from the default value,
|
|
|
|
/// it is a mod action and needs to be verified as such.
|
2021-10-28 21:17:59 +00:00
|
|
|
///
|
2023-06-07 19:18:17 +00:00
|
|
|
/// Locked needs to be false on a newly created post (verified in [[CreatePost]].
|
2023-03-21 15:03:05 +00:00
|
|
|
pub(crate) async fn is_mod_action(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
) -> Result<bool, LemmyError> {
|
|
|
|
let old_post = self.id.clone().dereference_local(context).await;
|
2023-06-07 19:18:17 +00:00
|
|
|
Ok(Page::is_locked_changed(&old_post, &self.comments_enabled))
|
2022-04-11 23:03:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn is_locked_changed<E>(
|
|
|
|
old_post: &Result<ApubPost, E>,
|
|
|
|
new_comments_enabled: &Option<bool>,
|
|
|
|
) -> bool {
|
|
|
|
if let Some(new_comments_enabled) = new_comments_enabled {
|
|
|
|
if let Ok(old_post) = old_post {
|
|
|
|
return new_comments_enabled != &!old_post.locked;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2022-05-06 23:53:33 +00:00
|
|
|
pub(crate) fn creator(&self) -> Result<ObjectId<ApubPerson>, LemmyError> {
|
|
|
|
match &self.attributed_to {
|
|
|
|
AttributedTo::Lemmy(l) => Ok(l.clone()),
|
|
|
|
AttributedTo::Peertube(p) => p
|
|
|
|
.iter()
|
|
|
|
.find(|a| a.kind == PersonOrGroupType::Person)
|
2023-03-21 15:03:05 +00:00
|
|
|
.map(|a| ObjectId::<ApubPerson>::from(a.id.clone().into_inner()))
|
2023-07-10 14:50:07 +00:00
|
|
|
.ok_or_else(|| LemmyErrorType::PageDoesNotSpecifyCreator.into()),
|
2022-05-06 23:53:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-28 21:17:59 +00:00
|
|
|
}
|
2021-11-11 19:49:15 +00:00
|
|
|
|
2022-04-01 18:25:19 +00:00
|
|
|
impl Attachment {
|
|
|
|
pub(crate) fn new(url: DbUrl) -> Attachment {
|
2022-08-16 10:12:43 +00:00
|
|
|
Attachment::Link(Link {
|
2022-04-01 18:25:19 +00:00
|
|
|
href: url.into(),
|
|
|
|
r#type: Default::default(),
|
2022-08-16 10:12:43 +00:00
|
|
|
})
|
2022-04-01 18:25:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 17:04:28 +00:00
|
|
|
// Used for community outbox, so that it can be compatible with Pleroma/Mastodon.
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2021-11-11 19:49:15 +00:00
|
|
|
impl ActivityHandler for Page {
|
|
|
|
type DataType = LemmyContext;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
|
|
|
fn id(&self) -> &Url {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
fn actor(&self) -> &Url {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn verify(&self, data: &Data<Self::DataType>) -> Result<(), LemmyError> {
|
|
|
|
ApubPost::verify(self, self.id.inner(), data).await
|
2021-11-11 19:49:15 +00:00
|
|
|
}
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), LemmyError> {
|
|
|
|
ApubPost::from_json(self, data).await?;
|
2021-11-18 17:04:28 +00:00
|
|
|
Ok(())
|
2021-11-11 19:49:15 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-01 20:52:49 +00:00
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2022-12-01 20:52:49 +00:00
|
|
|
impl InCommunity for Page {
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn community(&self, context: &Data<LemmyContext>) -> Result<ApubCommunity, LemmyError> {
|
2022-12-01 20:52:49 +00:00
|
|
|
let community = match &self.attributed_to {
|
|
|
|
AttributedTo::Lemmy(_) => {
|
|
|
|
let mut iter = self.to.iter().merge(self.cc.iter());
|
|
|
|
loop {
|
|
|
|
if let Some(cid) = iter.next() {
|
2023-03-21 15:03:05 +00:00
|
|
|
let cid = ObjectId::from(cid.clone());
|
|
|
|
if let Ok(c) = cid.dereference(context).await {
|
2022-12-01 20:52:49 +00:00
|
|
|
break c;
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-10 14:50:07 +00:00
|
|
|
return Err(LemmyErrorType::NoCommunityFoundInCc)?;
|
2022-12-01 20:52:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AttributedTo::Peertube(p) => {
|
|
|
|
p.iter()
|
|
|
|
.find(|a| a.kind == PersonOrGroupType::Group)
|
2023-03-21 15:03:05 +00:00
|
|
|
.map(|a| ObjectId::<ApubCommunity>::from(a.id.clone().into_inner()))
|
2023-07-10 14:50:07 +00:00
|
|
|
.ok_or(LemmyErrorType::PageDoesNotSpecifyGroup)?
|
2023-03-21 15:03:05 +00:00
|
|
|
.dereference(context)
|
2022-12-01 20:52:49 +00:00
|
|
|
.await?
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if let Some(audience) = &self.audience {
|
2023-02-18 14:50:28 +00:00
|
|
|
verify_community_matches(audience, community.actor_id.clone())?;
|
2022-12-01 20:52:49 +00:00
|
|
|
}
|
2023-02-18 14:50:28 +00:00
|
|
|
Ok(community)
|
2022-12-01 20:52:49 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-20 17:43:23 +00:00
|
|
|
|
|
|
|
/// Only allows deserialization if the field is missing or null. If it is present, throws an error.
|
|
|
|
pub fn deserialize_not_present<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let result: Option<String> = Deserialize::deserialize(deserializer)?;
|
|
|
|
match result {
|
|
|
|
None => Ok(None),
|
|
|
|
Some(_) => Err(D::Error::custom("Post must not have inReplyTo property")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2023-07-17 15:04:14 +00:00
|
|
|
#![allow(clippy::unwrap_used)]
|
|
|
|
#![allow(clippy::indexing_slicing)]
|
|
|
|
|
2023-01-20 17:43:23 +00:00
|
|
|
use crate::protocol::{objects::page::Page, tests::test_parse_lemmy_item};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_not_parsing_note_as_page() {
|
|
|
|
assert!(test_parse_lemmy_item::<Page>("assets/lemmy/objects/note.json").is_err());
|
|
|
|
}
|
|
|
|
}
|