Skip to content

Commit

Permalink
Migrate towards using page.attachment field for url (ref #2144)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic authored and dessalines committed Apr 1, 2022
1 parent 56b275a commit 784f079
Show file tree
Hide file tree
Showing 23 changed files with 116 additions and 55 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/apub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ lemmy_db_views_actor = { version = "=0.16.2", path = "../db_views_actor" }
lemmy_api_common = { version = "=0.16.2", path = "../api_common" }
lemmy_websocket = { version = "=0.16.2", path = "../websocket" }
diesel = "1.4.8"
activitystreams-kinds = "0.1.2"
activitystreams-kinds = "0.2.1"
bcrypt = "0.10.1"
chrono = { version = "0.4.19", features = ["serde"] }
serde_json = { version = "1.0.72", features = ["preserve_order"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
"mediaType": "text/markdown"
},
"url": "https://lemmy.ml/pictrs/image/xl8W7FZfk9.jpg",
"attachment": [
{
"type": "Link",
"href": "https://lemmy.ml/pictrs/image/xl8W7FZfk9.jpg"
}
],
"commentsEnabled": true,
"sensitive": false,
"stickied": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
"mediaType": "text/markdown"
},
"url": "https://lemmy.ml/pictrs/image/xl8W7FZfk9.jpg",
"attachment": [
{
"type": "Link",
"href": "https://lemmy.ml/pictrs/image/xl8W7FZfk9.jpg"
}
],
"commentsEnabled": true,
"sensitive": false,
"stickied": false,
Expand Down
6 changes: 6 additions & 0 deletions crates/apub/assets/lemmy/objects/page.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
"mediaType": "text/markdown"
},
"url": "https://enterprise.lemmy.ml/pictrs/image/eOtYb9iEiB.png",
"attachment": [
{
"type": "Link",
"href": "https://enterprise.lemmy.ml/pictrs/image/eOtYb9iEiB.png"
}
],
"image": {
"type": "Image",
"url": "https://enterprise.lemmy.ml/pictrs/image/eOtYb9iEiB.png"
Expand Down
2 changes: 1 addition & 1 deletion crates/apub/src/activities/create_or_update/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl CreateOrUpdatePost {

let create_or_update = CreateOrUpdatePost::new(post, actor, &community, kind, context).await?;
let id = create_or_update.id.clone();
let activity = AnnouncableActivities::CreateOrUpdatePost(create_or_update);
let activity = AnnouncableActivities::CreateOrUpdatePost(Box::new(create_or_update));
send_activity_in_community(activity, &id, actor, &community, vec![], context).await
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/apub/src/activity_lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub enum PersonInboxActivities {
#[activity_handler(LemmyContext)]
pub enum AnnouncableActivities {
CreateOrUpdateComment(CreateOrUpdateComment),
CreateOrUpdatePost(CreateOrUpdatePost),
CreateOrUpdatePost(Box<CreateOrUpdatePost>),
Vote(Vote),
UndoVote(UndoVote),
Delete(Delete),
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/fetcher/post_or_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum PostOrComment {
#[derive(Deserialize)]
#[serde(untagged)]
pub enum PageOrNote {
Page(Page),
Page(Box<Page>),
Note(Note),
}

Expand Down Expand Up @@ -85,7 +85,7 @@ impl ApubObject for PostOrComment {
) -> Result<Self, LemmyError> {
Ok(match apub {
PageOrNote::Page(p) => PostOrComment::Post(Box::new(
ApubPost::from_apub(p, context, request_counter).await?,
ApubPost::from_apub(*p, context, request_counter).await?,
)),
PageOrNote::Note(n) => PostOrComment::Comment(Box::new(
ApubComment::from_apub(n, context, request_counter).await?,
Expand Down
12 changes: 12 additions & 0 deletions crates/apub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ where
})
}

pub(crate) fn deserialize_skip_error<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: Deserialize<'de> + Default,
D: Deserializer<'de>,
{
let result = Deserialize::deserialize(deserializer);
Ok(match result {
Ok(o) => o,
Err(_) => Default::default(),
})
}

pub enum EndpointType {
Community,
Person,
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/objects/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
objects::read_from_string_or_source,
protocol::{
objects::{note::Note, tombstone::Tombstone},
SourceCompat,
Source,
},
PostOrComment,
};
Expand Down Expand Up @@ -118,7 +118,7 @@ impl ApubObject for ApubComment {
cc: maa.ccs,
content: markdown_to_html(&self.content),
media_type: Some(MediaTypeHtml::Html),
source: Some(SourceCompat::new(self.content.clone())),
source: Some(Source::new(self.content.clone())),
in_reply_to,
published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime),
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/objects/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
protocol::{
objects::{group::Group, tombstone::Tombstone, Endpoints},
ImageObject,
SourceCompat,
Source,
},
};
use activitystreams_kinds::actor::GroupType;
Expand Down Expand Up @@ -87,7 +87,7 @@ impl ApubObject for ApubCommunity {
preferred_username: self.name.clone(),
name: Some(self.title.clone()),
summary: self.description.as_ref().map(|b| markdown_to_html(b)),
source: self.description.clone().map(SourceCompat::new),
source: self.description.clone().map(Source::new),
icon: self.icon.clone().map(ImageObject::new),
image: self.banner.clone().map(ImageObject::new),
sensitive: Some(self.nsfw),
Expand Down
4 changes: 2 additions & 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::{read_from_string_or_source_opt, verify_image_domain_matches},
protocol::{objects::instance::Instance, ImageObject, SourceCompat},
protocol::{objects::instance::Instance, ImageObject, Source},
};
use activitystreams_kinds::actor::ServiceType;
use chrono::NaiveDateTime;
Expand Down Expand Up @@ -77,7 +77,7 @@ impl ApubObject for ApubSite {
id: ObjectId::new(self.actor_id()),
name: self.name.clone(),
content: self.sidebar.as_ref().map(|d| markdown_to_html(d)),
source: self.sidebar.clone().map(SourceCompat::new),
source: self.sidebar.clone().map(Source::new),
summary: self.description.clone(),
media_type: self.sidebar.as_ref().map(|_| MediaTypeHtml::Html),
icon: self.icon.clone().map(ImageObject::new),
Expand Down
10 changes: 5 additions & 5 deletions crates/apub/src/objects/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::protocol::{ImageObject, SourceCompat};
use crate::protocol::{ImageObject, Source};
use html2md::parse_html;
use lemmy_apub_lib::verify::verify_domains_match;
use lemmy_utils::LemmyError;
Expand All @@ -11,8 +11,8 @@ pub mod person;
pub mod post;
pub mod private_message;

pub(crate) fn read_from_string_or_source(raw: &str, source: &Option<SourceCompat>) -> String {
if let Some(SourceCompat::Lemmy(s)) = source {
pub(crate) fn read_from_string_or_source(raw: &str, source: &Option<Source>) -> String {
if let Some(s) = source {
s.content.clone()
} else {
parse_html(raw)
Expand All @@ -21,9 +21,9 @@ pub(crate) fn read_from_string_or_source(raw: &str, source: &Option<SourceCompat

pub(crate) fn read_from_string_or_source_opt(
raw: &Option<String>,
source: &Option<SourceCompat>,
source: &Option<Source>,
) -> Option<String> {
if let Some(SourceCompat::Lemmy(s2)) = source {
if let Some(s2) = source {
Some(s2.content.clone())
} else {
raw.as_ref().map(|s| parse_html(s))
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/objects/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
Endpoints,
},
ImageObject,
SourceCompat,
Source,
},
};
use chrono::NaiveDateTime;
Expand Down Expand Up @@ -99,7 +99,7 @@ impl ApubObject for ApubPerson {
preferred_username: self.name.clone(),
name: self.display_name.clone(),
summary: self.bio.as_ref().map(|b| markdown_to_html(b)),
source: self.bio.clone().map(SourceCompat::new),
source: self.bio.clone().map(Source::new),
icon: self.avatar.clone().map(ImageObject::new),
image: self.banner.clone().map(ImageObject::new),
matrix_user_id: self.matrix_user_id.clone(),
Expand Down
18 changes: 12 additions & 6 deletions crates/apub/src/objects/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use crate::{
objects::read_from_string_or_source_opt,
protocol::{
objects::{
page::{Page, PageType},
page::{Attachment, Page, PageType},
tombstone::Tombstone,
},
ImageObject,
SourceCompat,
Source,
},
};
use activitystreams_kinds::public;
Expand Down Expand Up @@ -110,8 +110,9 @@ impl ApubObject for ApubPost {
name: self.name.clone(),
content: self.body.as_ref().map(|b| markdown_to_html(b)),
media_type: Some(MediaTypeHtml::Html),
source: self.body.clone().map(SourceCompat::new),
source: self.body.clone().map(Source::new),
url: self.url.clone().map(|u| u.into()),
attachment: self.url.clone().map(Attachment::new).into_iter().collect(),
image: self.thumbnail_url.clone().map(ImageObject::new),
comments_enabled: Some(!self.locked),
sensitive: Some(self.nsfw),
Expand Down Expand Up @@ -160,8 +161,13 @@ impl ApubObject for ApubPost {
.await?;
let community = page.extract_community(context, request_counter).await?;

let url = if let Some(attachment) = page.attachment.first() {
Some(attachment.href.clone())
} else {
page.url
};
let thumbnail_url: Option<Url> = page.image.map(|i| i.url);
let (metadata_res, pictrs_thumbnail) = if let Some(url) = &page.url {
let (metadata_res, pictrs_thumbnail) = if let Some(url) = &url {
fetch_site_data(context.client(), &context.settings(), Some(url)).await
} else {
(None, thumbnail_url)
Expand All @@ -173,8 +179,8 @@ impl ApubObject for ApubPost {
let body_slurs_removed = read_from_string_or_source_opt(&page.content, &page.source)
.map(|s| remove_slurs(&s, &context.settings().slur_regex()));
let form = PostForm {
name: page.name,
url: page.url.map(|u| u.into()),
name: page.name.clone(),
url: url.map(Into::into),
body: body_slurs_removed,
creator_id: creator.id,
community_id: community.id,
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/objects/private_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
objects::read_from_string_or_source,
protocol::{
objects::chat_message::{ChatMessage, ChatMessageType},
SourceCompat,
Source,
},
};
use chrono::NaiveDateTime;
Expand Down Expand Up @@ -90,7 +90,7 @@ impl ApubObject for ApubPrivateMessage {
to: [ObjectId::new(recipient.actor_id)],
content: markdown_to_html(&self.content),
media_type: Some(MediaTypeHtml::Html),
source: Some(SourceCompat::new(self.content.clone())),
source: Some(Source::new(self.content.clone())),
published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime),
};
Expand Down
20 changes: 5 additions & 15 deletions crates/apub/src/protocol/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use activitystreams_kinds::object::ImageType;
use serde::{Deserialize, Serialize};
use url::Url;

use lemmy_apub_lib::values::MediaTypeMarkdown;
use lemmy_db_schema::newtypes::DbUrl;
use serde_json::Value;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use url::Url;

pub mod activities;
pub(crate) mod collections;
Expand All @@ -18,20 +16,12 @@ pub struct Source {
pub(crate) media_type: MediaTypeMarkdown,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
pub(crate) enum SourceCompat {
Lemmy(Source),
Other(Value),
}

impl SourceCompat {
impl Source {
pub(crate) fn new(content: String) -> Self {
SourceCompat::Lemmy(Source {
Source {
content,
media_type: MediaTypeMarkdown::Markdown,
})
}
}
}

Expand Down
6 changes: 4 additions & 2 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::SourceCompat,
protocol::Source,
};
use chrono::{DateTime, FixedOffset};
use lemmy_apub_lib::{object_id::ObjectId, values::MediaTypeHtml};
Expand All @@ -19,7 +19,9 @@ pub struct ChatMessage {
pub(crate) content: String,

pub(crate) media_type: Option<MediaTypeHtml>,
pub(crate) source: Option<SourceCompat>,
#[serde(default)]
#[serde(deserialize_with = "crate::deserialize_skip_error")]
pub(crate) source: Option<Source>,
pub(crate) published: Option<DateTime<FixedOffset>>,
pub(crate) updated: Option<DateTime<FixedOffset>>,
}
Expand Down
6 changes: 4 additions & 2 deletions crates/apub/src/protocol/objects/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
read_from_string_or_source_opt,
verify_image_domain_matches,
},
protocol::{objects::Endpoints, ImageObject, SourceCompat},
protocol::{objects::Endpoints, ImageObject, Source},
};
use activitystreams_kinds::actor::GroupType;
use chrono::{DateTime, FixedOffset};
Expand Down Expand Up @@ -40,7 +40,9 @@ pub struct Group {
/// title
pub(crate) name: Option<String>,
pub(crate) summary: Option<String>,
pub(crate) source: Option<SourceCompat>,
#[serde(default)]
#[serde(deserialize_with = "crate::deserialize_skip_error")]
pub(crate) source: Option<Source>,
pub(crate) icon: Option<ImageObject>,
/// banner
pub(crate) image: Option<ImageObject>,
Expand Down
6 changes: 4 additions & 2 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, SourceCompat},
protocol::{ImageObject, Source},
};
use activitystreams_kinds::actor::ServiceType;
use chrono::{DateTime, FixedOffset};
Expand All @@ -25,7 +25,9 @@ pub struct Instance {

// sidebar
pub(crate) content: Option<String>,
pub(crate) source: Option<SourceCompat>,
#[serde(default)]
#[serde(deserialize_with = "crate::deserialize_skip_error")]
pub(crate) source: Option<Source>,
// short instance description
pub(crate) summary: Option<String>,
pub(crate) media_type: Option<MediaTypeHtml>,
Expand Down
Loading

0 comments on commit 784f079

Please sign in to comment.