Skip to content

Commit

Permalink
Reorder apub protocol struct members to clarify mandatory/optional fi…
Browse files Browse the repository at this point in the history
…elds
  • Loading branch information
Nutomic committed Feb 14, 2022
1 parent c77e1d6 commit a351f62
Show file tree
Hide file tree
Showing 32 changed files with 64 additions and 70 deletions.
19 changes: 0 additions & 19 deletions crates/apub/assets/lemmy/objects/instance.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
{
"@context": [
"https://www.w3.org/ns/activitystreams",
{
"stickied": "as:stickied",
"pt": "https://join-lemmy.org#",
"sc": "http://schema.org#",
"matrixUserId": {
"type": "sc:Text",
"id": "as:alsoKnownAs"
},
"sensitive": "as:sensitive",
"comments_enabled": {
"type": "sc:Boolean",
"id": "pt:commentsEnabled"
},
"moderators": "as:moderators"
},
"https://w3id.org/security/v1"
],
"type": "Service",
"id": "https://enterprise.lemmy.ml/",
"name": "Enterprise",
Expand Down
2 changes: 1 addition & 1 deletion crates/apub/assets/lemmy/objects/person.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"type": "Image",
"url": "https://enterprise.lemmy.ml/pictrs/image/XenaYI5hTn.png"
},
"matrix_user_id": "@picard:matrix.org",
"matrixUserId": "@picard:matrix.org",
"inbox": "https://enterprise.lemmy.ml/u/picard/inbox",
"outbox": "https://enterprise.lemmy.ml/u/picard/outbox",
"endpoints": {
Expand Down
1 change: 0 additions & 1 deletion crates/apub/src/objects/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ impl ApubObject for ApubComment {
published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime),
tag: maa.tags,
unparsed: Default::default(),
};

Ok(note)
Expand Down
1 change: 0 additions & 1 deletion crates/apub/src/objects/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ impl ApubObject for ApubCommunity {
public_key: self.get_public_key()?,
published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime),
unparsed: Default::default(),
};
Ok(group)
}
Expand Down
3 changes: 1 addition & 2 deletions crates/apub/src/objects/instance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
check_is_apub_id_valid,
objects::get_summary_from_string_or_source,
protocol::{objects::instance::Instance, ImageObject, Source, Unparsed},
protocol::{objects::instance::Instance, ImageObject, Source},
};
use activitystreams_kinds::actor::ServiceType;
use chrono::NaiveDateTime;
Expand Down Expand Up @@ -86,7 +86,6 @@ impl ApubObject for ApubSite {
public_key: self.get_public_key()?,
published: convert_datetime(self.published),
updated: self.updated.map(convert_datetime),
unparsed: Unparsed::default(),
};
Ok(instance)
}
Expand Down
1 change: 0 additions & 1 deletion crates/apub/src/objects/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ impl ApubObject for ApubPerson {
}),
public_key: self.get_public_key()?,
updated: self.updated.map(convert_datetime),
unparsed: Default::default(),
inbox: self.inbox_url.clone().into(),
};
Ok(person)
Expand Down
1 change: 0 additions & 1 deletion crates/apub/src/objects/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ impl ApubObject for ApubPost {
stickied: Some(self.stickied),
published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime),
unparsed: Default::default(),
};
Ok(page)
}
Expand Down
1 change: 0 additions & 1 deletion crates/apub/src/objects/private_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ impl ApubObject for ApubPrivateMessage {
source: Some(Source::new(self.content.clone())),
published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime),
unparsed: Default::default(),
};
Ok(note)
}
Expand Down
5 changes: 3 additions & 2 deletions crates/apub/src/protocol/activities/block/block_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ pub struct BlockUser {
pub(crate) target: ObjectId<SiteOrCommunity>,
#[serde(rename = "type")]
pub(crate) kind: BlockType,
pub(crate) id: Url,

/// Quick and dirty solution.
/// TODO: send a separate Delete activity instead
pub(crate) remove_data: Option<bool>,
/// block reason, written to mod log
pub(crate) summary: Option<String>,
pub(crate) id: Url,
pub(crate) expires: Option<DateTime<FixedOffset>>,
#[serde(flatten)]
pub(crate) unparsed: Unparsed,
pub(crate) expires: Option<DateTime<FixedOffset>>,
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct UndoBlockUser {
#[serde(rename = "type")]
pub(crate) kind: UndoType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
1 change: 1 addition & 0 deletions crates/apub/src/protocol/activities/community/add_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct AddMod {
#[serde(rename = "type")]
pub(crate) kind: AddType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
1 change: 1 addition & 0 deletions crates/apub/src/protocol/activities/community/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct AnnounceActivity {
#[serde(rename = "type")]
pub(crate) kind: AnnounceType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct RemoveMod {
pub(crate) kind: RemoveType,
pub(crate) target: Url,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
1 change: 1 addition & 0 deletions crates/apub/src/protocol/activities/community/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct Report {
#[serde(rename = "type")]
pub(crate) kind: FlagType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
1 change: 1 addition & 0 deletions crates/apub/src/protocol/activities/community/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct UpdateCommunity {
#[serde(rename = "type")]
pub(crate) kind: UpdateType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct CreateOrUpdateComment {
#[serde(rename = "type")]
pub(crate) kind: CreateOrUpdateType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct CreateOrUpdatePost {
#[serde(rename = "type")]
pub(crate) kind: CreateOrUpdateType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct CreateOrUpdatePrivateMessage {
pub(crate) object: ChatMessage,
#[serde(rename = "type")]
pub(crate) kind: CreateOrUpdateType,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
7 changes: 4 additions & 3 deletions crates/apub/src/protocol/activities/deletion/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ pub struct Delete {
#[serde(deserialize_with = "crate::deserialize_one_or_many")]
pub(crate) to: Vec<Url>,
pub(crate) object: IdOrNestedObject,
#[serde(rename = "type")]
pub(crate) kind: DeleteType,
pub(crate) id: Url,

#[serde(deserialize_with = "crate::deserialize_one_or_many")]
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub(crate) cc: Vec<Url>,
#[serde(rename = "type")]
pub(crate) kind: DeleteType,
/// If summary is present, this is a mod action (Remove in Lemmy terms). Otherwise, its a user
/// deleting their own content.
pub(crate) summary: Option<String>,
pub(crate) id: Url,
#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
Expand Down
7 changes: 4 additions & 3 deletions crates/apub/src/protocol/activities/deletion/undo_delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ pub struct UndoDelete {
#[serde(deserialize_with = "crate::deserialize_one_or_many")]
pub(crate) to: Vec<Url>,
pub(crate) object: Delete,
#[serde(rename = "type")]
pub(crate) kind: UndoType,
pub(crate) id: Url,

#[serde(deserialize_with = "crate::deserialize_one_or_many")]
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub(crate) cc: Vec<Url>,
#[serde(rename = "type")]
pub(crate) kind: UndoType,
pub(crate) id: Url,
#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
1 change: 1 addition & 0 deletions crates/apub/src/protocol/activities/following/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct AcceptFollowCommunity {
#[serde(rename = "type")]
pub(crate) kind: AcceptType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
1 change: 1 addition & 0 deletions crates/apub/src/protocol/activities/following/follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct FollowCommunity {
#[serde(rename = "type")]
pub(crate) kind: FollowType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct UndoFollowCommunity {
#[serde(rename = "type")]
pub(crate) kind: UndoType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
1 change: 1 addition & 0 deletions crates/apub/src/protocol/activities/voting/undo_vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct UndoVote {
#[serde(rename = "type")]
pub(crate) kind: UndoType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
1 change: 1 addition & 0 deletions crates/apub/src/protocol/activities/voting/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct Vote {
#[serde(rename = "type")]
pub(crate) kind: VoteType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
Expand Down
5 changes: 2 additions & 3 deletions crates/apub/src/protocol/objects/chat_message.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
objects::{person::ApubPerson, private_message::ApubPrivateMessage},
protocol::{Source, Unparsed},
protocol::Source,
};
use chrono::{DateTime, FixedOffset};
use lemmy_apub_lib::{object_id::ObjectId, values::MediaTypeHtml};
Expand All @@ -17,12 +17,11 @@ pub struct ChatMessage {
#[serde(deserialize_with = "crate::deserialize_one")]
pub(crate) to: [ObjectId<ApubPerson>; 1],
pub(crate) content: String,

pub(crate) media_type: Option<MediaTypeHtml>,
pub(crate) source: Option<Source>,
pub(crate) published: Option<DateTime<FixedOffset>>,
pub(crate) updated: Option<DateTime<FixedOffset>>,
#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}

/// https://docs.pleroma.social/backend/development/ap_extensions/#chatmessages
Expand Down
15 changes: 7 additions & 8 deletions crates/apub/src/protocol/objects/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
community_outbox::ApubCommunityOutbox,
},
objects::{community::ApubCommunity, get_summary_from_string_or_source},
protocol::{objects::Endpoints, ImageObject, Source, Unparsed},
protocol::{objects::Endpoints, ImageObject, Source},
};
use activitystreams_kinds::actor::GroupType;
use chrono::{DateTime, FixedOffset};
Expand All @@ -27,10 +27,14 @@ pub struct Group {
#[serde(rename = "type")]
pub(crate) kind: GroupType,
pub(crate) id: ObjectId<ApubCommunity>,
/// username, set at account creation and can never be changed
/// username, set at account creation and usually fixed after that
pub(crate) preferred_username: String,
/// title (can be changed at any time)
/// displayname
pub(crate) name: String,
pub(crate) inbox: Url,
pub(crate) followers: Url,
pub(crate) public_key: PublicKey,

pub(crate) summary: Option<String>,
pub(crate) source: Option<Source>,
pub(crate) icon: Option<ImageObject>,
Expand All @@ -40,15 +44,10 @@ pub struct Group {
pub(crate) sensitive: Option<bool>,
// lemmy extension
pub(crate) moderators: Option<ObjectId<ApubCommunityModerators>>,
pub(crate) inbox: Url,
pub(crate) outbox: ObjectId<ApubCommunityOutbox>,
pub(crate) followers: Url,
pub(crate) endpoints: Option<Endpoints>,
pub(crate) public_key: PublicKey,
pub(crate) published: Option<DateTime<FixedOffset>>,
pub(crate) updated: Option<DateTime<FixedOffset>>,
#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}

impl Group {
Expand Down
13 changes: 6 additions & 7 deletions crates/apub/src/protocol/objects/instance.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
objects::instance::ApubSite,
protocol::{ImageObject, Source, Unparsed},
protocol::{ImageObject, Source},
};
use activitystreams_kinds::actor::ServiceType;
use chrono::{DateTime, FixedOffset};
Expand All @@ -18,6 +18,11 @@ pub struct Instance {
pub(crate) id: ObjectId<ApubSite>,
// site name
pub(crate) name: String,
pub(crate) inbox: Url,
/// mandatory field in activitypub, lemmy currently serves an empty outbox
pub(crate) outbox: Url,
pub(crate) public_key: PublicKey,

// sidebar
pub(crate) content: Option<String>,
pub(crate) source: Option<Source>,
Expand All @@ -28,12 +33,6 @@ pub struct Instance {
pub(crate) icon: Option<ImageObject>,
/// instance banner
pub(crate) image: Option<ImageObject>,
pub(crate) inbox: Url,
/// mandatory field in activitypub, currently empty in lemmy
pub(crate) outbox: Url,
pub(crate) public_key: PublicKey,
pub(crate) published: DateTime<FixedOffset>,
pub(crate) updated: Option<DateTime<FixedOffset>>,
#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
8 changes: 8 additions & 0 deletions crates/apub/src/protocol/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@ mod tests {

#[test]
fn test_parse_objects_lemmy() {
dbg!("1");
test_parse_lemmy_item::<Instance>("assets/lemmy/objects/instance.json").unwrap();
dbg!("2");
test_parse_lemmy_item::<Group>("assets/lemmy/objects/group.json").unwrap();
dbg!("3");
test_parse_lemmy_item::<Person>("assets/lemmy/objects/person.json").unwrap();
dbg!("4");
test_parse_lemmy_item::<Page>("assets/lemmy/objects/page.json").unwrap();
dbg!("5");
test_parse_lemmy_item::<Note>("assets/lemmy/objects/note.json").unwrap();
dbg!("6");
test_parse_lemmy_item::<ChatMessage>("assets/lemmy/objects/chat_message.json").unwrap();
dbg!("7");
test_parse_lemmy_item::<Tombstone>("assets/lemmy/objects/tombstone.json").unwrap();
dbg!("8");
}

#[test]
Expand Down
Loading

0 comments on commit a351f62

Please sign in to comment.