Added test case for parsing community from apub json

- fixed a bug with objectid (de)serialization
- fixed a bug with outbox fetching (ref #1582)
This commit is contained in:
Felix Ableitner 2021-10-19 18:02:34 +02:00
parent 748aa342f1
commit 366532c61a
22 changed files with 143 additions and 52 deletions

View file

@ -0,0 +1,46 @@
{
"@context": [
"https://www.w3.org/ns/activitystreams",
{
"sensitive": "as:sensitive",
"sc": "http://schema.org#",
"matrixUserId": {
"type": "sc:Text",
"id": "as:alsoKnownAs"
},
"stickied": "as:stickied",
"pt": "https://join-lemmy.org#",
"comments_enabled": {
"type": "sc:Boolean",
"id": "pt:commentsEnabled"
},
"moderators": "as:moderators"
},
"https://w3id.org/security/v1"
],
"type": "Group",
"id": "https://lemmy.ml/c/meta",
"preferredUsername": "meta",
"name": "lemmy.ml meta",
"content": "<p>Anything about the lemmy.ml instance and its moderation.</p>\n<p>For discussion about the Lemmy software project, go to <a href=\"https://lemmy.ml/c/lemmy\">!lemmy@lemmy.ml</a>.</p>\n",
"mediaType": "text/html",
"source": {
"content": "Anything about the lemmy.ml instance and its moderation. \n\nFor discussion about the Lemmy software project, go to [!lemmy@lemmy.ml](https://lemmy.ml/c/lemmy).",
"mediaType": "text/markdown"
},
"sensitive": false,
"moderators": "https://lemmy.ml/c/meta/moderators",
"inbox": "https://lemmy.ml/c/meta/inbox",
"outbox": "https://lemmy.ml/c/meta/outbox",
"followers": "https://lemmy.ml/c/meta/followers",
"endpoints": {
"sharedInbox": "https://lemmy.ml/inbox"
},
"publicKey": {
"id": "https://lemmy.ml/c/meta#main-key",
"owner": "https://lemmy.ml/c/meta",
"publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApLImCO5LEXMWb6lPCfCI\necpxU17j8CFfrDJLUWcuCmKO9KxJFmsv9uY2sYr9YX9CD4o+jnJ4D/e+/D+UeJiD\naJ3aYLEjLm+u6KuAGVREjSLzp1Js/6F2k0MIIQueiO5BBiootEY6pv3CwqqMC9L4\npWvIjmd03dMBhU1DTAblrj61oGj3c5l0OPdjeW7XnTPSrCZvaQ/1PWwVyUj0y/SC\nHiUiUiqUD88aPB8c7s85Svzs5RFT8WHcelPXJ7ydhYlU8R2tE1Octjy9Gb1R38k4\ns62LiwlRUAIzvnpMWOBi60aRpL8+IuZXoB2cCEyX5SQ+ETVp5DobHDgI0zzBEjfD\nkQIDAQAB\n-----END PUBLIC KEY-----\n"
},
"published": "2021-06-14T22:50:50.985762+00:00",
"updated": "2021-06-14T23:29:55.801468+00:00"
}

View file

@ -2,18 +2,18 @@
"@context": [
"https://www.w3.org/ns/activitystreams",
{
"moderators": "as:moderators",
"sensitive": "as:sensitive",
"pt": "https://join-lemmy.org#",
"sc": "http://schema.org#",
"stickied": "as:stickied",
"matrixUserId": {
"type": "sc:Text",
"id": "as:alsoKnownAs"
},
"pt": "https://join-lemmy.org#",
"comments_enabled": {
"type": "sc:Boolean",
"id": "pt:commentsEnabled"
},
"stickied": "as:stickied",
"sensitive": "as:sensitive",
"moderators": "as:moderators",
"matrixUserId": {
"type": "sc:Text",
"id": "as:alsoKnownAs"
}
},
"https://w3id.org/security/v1"

View file

@ -11,6 +11,7 @@ use crate::{
},
context::lemmy_context,
fetcher::object_id::ObjectId,
migrations::PublicUrlMigration,
objects::{
comment::{ApubComment, Note},
community::ApubCommunity,
@ -22,7 +23,6 @@ use lemmy_api_common::{blocking, check_post_deleted_or_removed};
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler, ActorType, FromApub, ToApub},
values::PublicUrl,
verify::verify_domains_match,
};
use lemmy_db_schema::{
@ -38,7 +38,7 @@ use url::Url;
#[serde(rename_all = "camelCase")]
pub struct CreateOrUpdateComment {
actor: ObjectId<ApubPerson>,
to: [PublicUrl; 1],
to: PublicUrlMigration,
object: Note,
cc: Vec<Url>,
tag: Vec<Mention>,
@ -76,7 +76,7 @@ impl CreateOrUpdateComment {
let create_or_update = CreateOrUpdateComment {
actor: ObjectId::new(actor.actor_id()),
to: [PublicUrl::Public],
to: PublicUrlMigration::create(),
object: comment.to_apub(context.pool()).await?,
cc: maa.ccs,
tag: maa.tags,

View file

@ -10,6 +10,7 @@ use crate::{
context::lemmy_context,
fetcher::object_id::ObjectId,
generate_moderators_url,
migrations::PublicUrlMigration,
objects::{community::ApubCommunity, person::ApubPerson},
};
use activitystreams::{
@ -22,7 +23,6 @@ use lemmy_api_common::blocking;
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler, ActorType},
values::PublicUrl,
};
use lemmy_db_schema::{
source::community::{CommunityModerator, CommunityModeratorForm},
@ -37,7 +37,7 @@ use url::Url;
#[serde(rename_all = "camelCase")]
pub struct AddMod {
actor: ObjectId<ApubPerson>,
to: [PublicUrl; 1],
to: PublicUrlMigration,
object: ObjectId<ApubPerson>,
target: Url,
cc: [ObjectId<ApubCommunity>; 1],
@ -63,7 +63,7 @@ impl AddMod {
)?;
let add = AddMod {
actor: ObjectId::new(actor.actor_id()),
to: [PublicUrl::Public],
to: PublicUrlMigration::create(),
object: ObjectId::new(added_mod.actor_id()),
target: generate_moderators_url(&community.actor_id)?.into(),
cc: [ObjectId::new(community.actor_id())],

View file

@ -21,6 +21,7 @@ use crate::{
fetcher::object_id::ObjectId,
http::is_activity_already_known,
insert_activity,
migrations::PublicUrlMigration,
objects::community::ApubCommunity,
send_lemmy_activity,
CommunityType,
@ -34,7 +35,6 @@ use activitystreams::{
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler, ActorType},
values::PublicUrl,
};
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;
@ -63,7 +63,7 @@ pub enum AnnouncableActivities {
#[serde(rename_all = "camelCase")]
pub struct AnnounceActivity {
actor: ObjectId<ApubCommunity>,
to: [PublicUrl; 1],
to: PublicUrlMigration,
object: AnnouncableActivities,
cc: Vec<Url>,
#[serde(rename = "type")]
@ -84,7 +84,7 @@ impl AnnounceActivity {
) -> Result<(), LemmyError> {
let announce = AnnounceActivity {
actor: ObjectId::new(community.actor_id()),
to: [PublicUrl::Public],
to: PublicUrlMigration::create(),
object,
cc: vec![community.followers_url()],
kind: AnnounceType::Announce,

View file

@ -8,6 +8,7 @@ use crate::{
},
context::lemmy_context,
fetcher::object_id::ObjectId,
migrations::PublicUrlMigration,
objects::{community::ApubCommunity, person::ApubPerson},
};
use activitystreams::{
@ -20,7 +21,6 @@ use lemmy_api_common::blocking;
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler, ActorType},
values::PublicUrl,
};
use lemmy_db_schema::{
source::community::{
@ -40,7 +40,7 @@ use url::Url;
#[serde(rename_all = "camelCase")]
pub struct BlockUserFromCommunity {
actor: ObjectId<ApubPerson>,
to: [PublicUrl; 1],
to: PublicUrlMigration,
pub(in crate::activities::community) object: ObjectId<ApubPerson>,
cc: [ObjectId<ApubCommunity>; 1],
#[serde(rename = "type")]
@ -61,7 +61,7 @@ impl BlockUserFromCommunity {
) -> Result<BlockUserFromCommunity, LemmyError> {
Ok(BlockUserFromCommunity {
actor: ObjectId::new(actor.actor_id()),
to: [PublicUrl::Public],
to: PublicUrlMigration::create(),
object: ObjectId::new(target.actor_id()),
cc: [ObjectId::new(community.actor_id())],
kind: BlockType::Block,

View file

@ -11,6 +11,7 @@ use crate::{
context::lemmy_context,
fetcher::object_id::ObjectId,
generate_moderators_url,
migrations::PublicUrlMigration,
objects::{community::ApubCommunity, person::ApubPerson},
};
use activitystreams::{
@ -23,7 +24,6 @@ use lemmy_api_common::blocking;
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler, ActorType},
values::PublicUrl,
};
use lemmy_db_schema::{
source::community::{CommunityModerator, CommunityModeratorForm},
@ -38,7 +38,7 @@ use url::Url;
#[serde(rename_all = "camelCase")]
pub struct RemoveMod {
actor: ObjectId<ApubPerson>,
to: [PublicUrl; 1],
to: PublicUrlMigration,
pub(in crate::activities) object: ObjectId<ApubPerson>,
cc: [ObjectId<ApubCommunity>; 1],
#[serde(rename = "type")]
@ -65,7 +65,7 @@ impl RemoveMod {
)?;
let remove = RemoveMod {
actor: ObjectId::new(actor.actor_id()),
to: [PublicUrl::Public],
to: PublicUrlMigration::create(),
object: ObjectId::new(removed_mod.actor_id()),
target: Some(generate_moderators_url(&community.actor_id)?.into()),
id: id.clone(),

View file

@ -12,6 +12,7 @@ use crate::{
},
context::lemmy_context,
fetcher::object_id::ObjectId,
migrations::PublicUrlMigration,
objects::{community::ApubCommunity, person::ApubPerson},
};
use activitystreams::{
@ -24,7 +25,6 @@ use lemmy_api_common::blocking;
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler, ActorType},
values::PublicUrl,
};
use lemmy_db_schema::{
source::community::{CommunityPersonBan, CommunityPersonBanForm},
@ -39,7 +39,7 @@ use url::Url;
#[serde(rename_all = "camelCase")]
pub struct UndoBlockUserFromCommunity {
actor: ObjectId<ApubPerson>,
to: [PublicUrl; 1],
to: PublicUrlMigration,
object: BlockUserFromCommunity,
cc: [ObjectId<ApubCommunity>; 1],
#[serde(rename = "type")]
@ -66,7 +66,7 @@ impl UndoBlockUserFromCommunity {
)?;
let undo = UndoBlockUserFromCommunity {
actor: ObjectId::new(actor.actor_id()),
to: [PublicUrl::Public],
to: PublicUrlMigration::create(),
object: block,
cc: [ObjectId::new(community.actor_id())],
kind: UndoType::Undo,

View file

@ -8,6 +8,7 @@ use crate::{
},
context::lemmy_context,
fetcher::object_id::ObjectId,
migrations::PublicUrlMigration,
objects::{
community::{ApubCommunity, Group},
person::ApubPerson,
@ -23,7 +24,6 @@ use lemmy_api_common::blocking;
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler, ActorType, ToApub},
values::PublicUrl,
};
use lemmy_db_schema::{
source::community::{Community, CommunityForm},
@ -40,7 +40,7 @@ use url::Url;
#[serde(rename_all = "camelCase")]
pub struct UpdateCommunity {
actor: ObjectId<ApubPerson>,
to: [PublicUrl; 1],
to: PublicUrlMigration,
// TODO: would be nice to use a separate struct here, which only contains the fields updated here
object: Group,
cc: [ObjectId<ApubCommunity>; 1],
@ -65,7 +65,7 @@ impl UpdateCommunity {
)?;
let update = UpdateCommunity {
actor: ObjectId::new(actor.actor_id()),
to: [PublicUrl::Public],
to: PublicUrlMigration::create(),
object: community.to_apub(context.pool()).await?,
cc: [ObjectId::new(community.actor_id())],
kind: UpdateType::Update,

View file

@ -12,6 +12,7 @@ use crate::{
},
context::lemmy_context,
fetcher::object_id::ObjectId,
migrations::PublicUrlMigration,
objects::{community::ApubCommunity, person::ApubPerson},
};
use activitystreams::{
@ -25,7 +26,6 @@ use lemmy_api_common::blocking;
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler, ActorType},
values::PublicUrl,
};
use lemmy_db_schema::{
source::{
@ -66,7 +66,7 @@ use url::Url;
#[serde(rename_all = "camelCase")]
pub struct Delete {
actor: ObjectId<ApubPerson>,
to: [PublicUrl; 1],
to: PublicUrlMigration,
pub(in crate::activities::deletion) object: Url,
pub(in crate::activities::deletion) cc: [ObjectId<ApubCommunity>; 1],
#[serde(rename = "type")]
@ -144,7 +144,7 @@ impl Delete {
) -> Result<Delete, LemmyError> {
Ok(Delete {
actor: ObjectId::new(actor.actor_id()),
to: [PublicUrl::Public],
to: PublicUrlMigration::create(),
object: object_id,
cc: [ObjectId::new(community.actor_id())],
kind: DeleteType::Delete,

View file

@ -13,6 +13,7 @@ use crate::{
},
context::lemmy_context,
fetcher::object_id::ObjectId,
migrations::PublicUrlMigration,
objects::{community::ApubCommunity, person::ApubPerson},
};
use activitystreams::{
@ -26,7 +27,6 @@ use lemmy_api_common::blocking;
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler, ActorType},
values::PublicUrl,
};
use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post};
use lemmy_utils::LemmyError;
@ -42,7 +42,7 @@ use url::Url;
#[serde(rename_all = "camelCase")]
pub struct UndoDelete {
actor: ObjectId<ApubPerson>,
to: [PublicUrl; 1],
to: PublicUrlMigration,
object: Delete,
cc: [ObjectId<ApubCommunity>; 1],
#[serde(rename = "type")]
@ -117,7 +117,7 @@ impl UndoDelete {
)?;
let undo = UndoDelete {
actor: ObjectId::new(actor.actor_id()),
to: [PublicUrl::Public],
to: PublicUrlMigration::create(),
object,
cc: [ObjectId::new(community.actor_id())],
kind: UndoType::Undo,

View file

@ -10,6 +10,7 @@ use crate::{
},
context::lemmy_context,
fetcher::object_id::ObjectId,
migrations::PublicUrlMigration,
objects::{
community::ApubCommunity,
person::ApubPerson,
@ -22,7 +23,6 @@ use lemmy_api_common::blocking;
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler, ActorType, FromApub, ToApub},
values::PublicUrl,
verify::{verify_domains_match, verify_urls_match},
};
use lemmy_db_schema::{source::community::Community, traits::Crud};
@ -35,7 +35,7 @@ use url::Url;
#[serde(rename_all = "camelCase")]
pub struct CreateOrUpdatePost {
actor: ObjectId<ApubPerson>,
to: [PublicUrl; 1],
to: PublicUrlMigration,
object: Page,
cc: [ObjectId<ApubCommunity>; 1],
#[serde(rename = "type")]
@ -67,7 +67,7 @@ impl CreateOrUpdatePost {
)?;
let create_or_update = CreateOrUpdatePost {
actor: ObjectId::new(actor.actor_id()),
to: [PublicUrl::Public],
to: PublicUrlMigration::create(),
object: post.to_apub(context.pool()).await?,
cc: [ObjectId::new(community.actor_id())],
kind,

View file

@ -5,6 +5,7 @@ use crate::{
verify_activity,
},
fetcher::object_id::ObjectId,
migrations::PublicUrlMigration,
objects::{community::ApubCommunity, person::ApubPerson},
};
use activitystreams::{
@ -16,7 +17,6 @@ use activitystreams::{
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler},
values::PublicUrl,
};
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;
@ -27,7 +27,7 @@ use url::Url;
#[serde(rename_all = "camelCase")]
pub struct UndoRemovePostCommentOrCommunity {
actor: ObjectId<ApubPerson>,
to: [PublicUrl; 1],
to: PublicUrlMigration,
// Note, there is no such thing as Undo/Remove/Mod, so we ignore that
object: RemoveMod,
cc: [ObjectId<ApubCommunity>; 1],

View file

@ -12,6 +12,7 @@ use crate::{
},
context::lemmy_context,
fetcher::object_id::ObjectId,
migrations::PublicUrlMigration,
objects::{community::ApubCommunity, person::ApubPerson},
PostOrComment,
};
@ -25,7 +26,6 @@ use lemmy_api_common::blocking;
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler, ActorType},
values::PublicUrl,
verify::verify_urls_match,
};
use lemmy_db_schema::{newtypes::CommunityId, source::community::Community, traits::Crud};
@ -39,7 +39,7 @@ use url::Url;
#[serde(rename_all = "camelCase")]
pub struct UndoVote {
actor: ObjectId<ApubPerson>,
to: [PublicUrl; 1],
to: PublicUrlMigration,
object: Vote,
cc: [ObjectId<ApubCommunity>; 1],
#[serde(rename = "type")]
@ -72,7 +72,7 @@ impl UndoVote {
)?;
let undo_vote = UndoVote {
actor: ObjectId::new(actor.actor_id()),
to: [PublicUrl::Public],
to: PublicUrlMigration::create(),
object,
cc: [ObjectId::new(community.actor_id())],
kind: UndoType::Undo,

View file

@ -8,6 +8,7 @@ use crate::{
},
context::lemmy_context,
fetcher::object_id::ObjectId,
migrations::PublicUrlMigration,
objects::{community::ApubCommunity, person::ApubPerson},
PostOrComment,
};
@ -17,7 +18,6 @@ use lemmy_api_common::blocking;
use lemmy_apub_lib::{
data::Data,
traits::{ActivityFields, ActivityHandler, ActorType},
values::PublicUrl,
};
use lemmy_db_schema::{newtypes::CommunityId, source::community::Community, traits::Crud};
use lemmy_utils::LemmyError;
@ -58,7 +58,7 @@ impl From<&VoteType> for i16 {
#[serde(rename_all = "camelCase")]
pub struct Vote {
actor: ObjectId<ApubPerson>,
to: [PublicUrl; 1],
to: PublicUrlMigration,
pub(in crate::activities::voting) object: ObjectId<PostOrComment>,
cc: [ObjectId<ApubCommunity>; 1],
#[serde(rename = "type")]
@ -80,7 +80,7 @@ impl Vote {
) -> Result<Vote, LemmyError> {
Ok(Vote {
actor: ObjectId::new(actor.actor_id()),
to: [PublicUrl::Public],
to: PublicUrlMigration::create(),
object: ObjectId::new(object.ap_id()),
cc: [ObjectId::new(community.actor_id())],
kind: kind.clone(),

View file

@ -22,6 +22,7 @@ use url::Url;
static REQUEST_LIMIT: i32 = 25;
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug)]
#[serde(transparent)]
pub struct ObjectId<Kind>(Url, #[serde(skip)] PhantomData<Kind>)
where
Kind: FromApub<DataType = LemmyContext> + ApubObject<DataType = LemmyContext> + Send + 'static,

View file

@ -1,4 +1,5 @@
use crate::fetcher::{object_id::ObjectId, post_or_comment::PostOrComment};
use lemmy_apub_lib::values::PublicUrl;
use serde::{Deserialize, Serialize};
use url::Url;
@ -28,3 +29,21 @@ pub enum CommentInReplyToMigration {
//
// For v0.13, delete [`UndoRemovePostCommentOrCommunity`], and don't handle object deletion in
// [`RemoveMod`] handler.
/// Migrate value of field `to` from single value to vec.
///
/// v0.14: send as single value, accept both
/// v0.15: send as vec, accept both
/// v0.16: send and accept only vec
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum PublicUrlMigration {
Old(PublicUrl),
New([PublicUrl; 1]),
}
impl PublicUrlMigration {
pub(crate) fn create() -> PublicUrlMigration {
PublicUrlMigration::Old(PublicUrl::Public)
}
}

View file

@ -304,3 +304,26 @@ impl CommunityType for Community {
Ok(inboxes)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::objects::tests::{file_to_json_object, init_context};
use serial_test::serial;
#[actix_rt::test]
#[serial]
async fn test_fetch_lemmy_community() {
let json = file_to_json_object("assets/lemmy-community.json");
let url = Url::parse("https://lemmy.ml/c/meta").unwrap();
let community = ApubCommunity::from_apub(&json, &init_context(), &url, &mut 0)
.await
.unwrap();
assert_eq!(community.actor_id.clone().into_inner(), url);
assert_eq!(community.title, "lemmy.ml meta");
assert!(community.public_key.is_some());
assert!(!community.local);
assert_eq!(community.description.as_ref().unwrap().len(), 158);
}
}

View file

@ -112,7 +112,7 @@ mod tests {
}
let chat_server = ChatServer::startup(
pool.clone(),
rate_limiter.clone(),
rate_limiter,
|_, _, _, _| Box::pin(x()),
|_, _, _, _| Box::pin(x()),
client.clone(),

View file

@ -285,7 +285,7 @@ mod tests {
assert_eq!(person.actor_id.clone().into_inner(), url);
assert_eq!(person.name, "nutomic");
assert!(person.public_key.is_some());
assert_eq!(person.local, false);
assert!(!person.local);
assert!(person.bio.is_some());
}
@ -301,7 +301,7 @@ mod tests {
assert_eq!(person.actor_id.clone().into_inner(), url);
assert_eq!(person.name, "lanodan");
assert!(person.public_key.is_some());
assert_eq!(person.local, false);
assert!(!person.local);
// TODO: pleroma uses summary for user profile, while we use content
//assert!(person.bio.is_some());
}

View file

@ -54,7 +54,7 @@ pub struct Page {
to: [Url; 2],
name: String,
content: Option<String>,
media_type: MediaTypeHtml,
media_type: Option<MediaTypeHtml>,
source: Option<Source>,
url: Option<Url>,
image: Option<ImageObject>,
@ -189,7 +189,7 @@ impl ToApub for ApubPost {
to: [community.actor_id.into(), public()],
name: self.name.clone(),
content: self.body.as_ref().map(|b| markdown_to_html(b)),
media_type: MediaTypeHtml::Html,
media_type: Some(MediaTypeHtml::Html),
source,
url: self.url.clone().map(|u| u.into()),
image,

View file

@ -3,5 +3,7 @@ set -e
curl -H "Accept: application/activity+json" https://lemmy.ml/u/nutomic | jq \
> crates/apub/assets/lemmy-person.json
curl -H "Accept: application/activity+json" https://lemmy.ml/c/meta | jq \
> crates/apub/assets/lemmy-community.json
curl -H "Accept: application/activity+json" https://queer.hacktivis.me/users/lanodan | jq \
> crates/apub/assets/pleroma-person.json
> crates/apub/assets/pleroma-person.json