Simplify lemmy_context() function (dont return errors)

This commit is contained in:
Felix Ableitner 2021-07-29 10:58:29 +02:00
parent ff265c7ebc
commit 57b6ecaf40
17 changed files with 145 additions and 145 deletions

View File

@ -86,7 +86,7 @@ impl AnnounceActivity {
cc: vec![community.followers_url()],
kind: AnnounceType::Announce,
common: ActivityCommonFields {
context: lemmy_context()?.into(),
context: lemmy_context(),
id: generate_activity_id(AnnounceType::Announce)?,
actor: community.actor_id(),
unparsed: Default::default(),

View File

@ -55,7 +55,7 @@ impl CreatePost {
cc: [community.actor_id()],
r#type: Default::default(),
common: ActivityCommonFields {
context: lemmy_context()?.into(),
context: lemmy_context(),
id: id.clone(),
actor: actor.actor_id(),
unparsed: Default::default(),

View File

@ -48,7 +48,7 @@ impl UpdatePost {
cc: [community.actor_id()],
r#type: Default::default(),
common: ActivityCommonFields {
context: lemmy_context()?.into(),
context: lemmy_context(),
id: id.clone(),
actor: actor.actor_id(),
unparsed: Default::default(),

View File

@ -64,7 +64,7 @@ impl ApubObjectType for Comment {
note.into_any_base()?,
);
create
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(CreateType::Create)?)
.set_to(public())
.set_many_ccs(maa.ccs.to_owned())
@ -97,7 +97,7 @@ impl ApubObjectType for Comment {
note.into_any_base()?,
);
update
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(UpdateType::Update)?)
.set_to(public())
.set_many_ccs(maa.ccs.to_owned())
@ -124,7 +124,7 @@ impl ApubObjectType for Comment {
self.ap_id.to_owned().into_inner(),
);
delete
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -153,7 +153,7 @@ impl ApubObjectType for Comment {
self.ap_id.to_owned().into_inner(),
);
delete
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -164,7 +164,7 @@ impl ApubObjectType for Comment {
delete.into_any_base()?,
);
undo
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -188,7 +188,7 @@ impl ApubObjectType for Comment {
self.ap_id.to_owned().into_inner(),
);
remove
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -217,7 +217,7 @@ impl ApubObjectType for Comment {
self.ap_id.to_owned().into_inner(),
);
remove
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -228,7 +228,7 @@ impl ApubObjectType for Comment {
remove.into_any_base()?,
);
undo
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -255,7 +255,7 @@ impl ApubLikeableType for Comment {
self.ap_id.to_owned().into_inner(),
);
like
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(LikeType::Like)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -279,7 +279,7 @@ impl ApubLikeableType for Comment {
self.ap_id.to_owned().into_inner(),
);
dislike
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(DislikeType::Dislike)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -307,7 +307,7 @@ impl ApubLikeableType for Comment {
self.ap_id.to_owned().into_inner(),
);
like
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(DislikeType::Dislike)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -318,7 +318,7 @@ impl ApubLikeableType for Comment {
like.into_any_base()?,
);
undo
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);

View File

@ -98,7 +98,7 @@ impl CommunityType for Community {
follow.into_any_base()?,
);
accept
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(AcceptType::Accept)?)
.set_to(person.actor_id());
@ -117,7 +117,7 @@ impl CommunityType for Community {
self.to_apub(context.pool()).await?.into_any_base()?,
);
update
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(UpdateType::Update)?)
.set_to(public())
.set_many_ccs(vec![self.actor_id()]);
@ -134,7 +134,7 @@ impl CommunityType for Community {
if self.local {
let mut delete = Delete::new(self.actor_id(), self.actor_id());
delete
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![self.followers_url()]);
@ -145,7 +145,7 @@ impl CommunityType for Community {
else {
let mut delete = Delete::new(mod_.actor_id(), self.actor_id());
delete
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![self.actor_id()]);
@ -163,14 +163,14 @@ impl CommunityType for Community {
if self.local {
let mut delete = Delete::new(self.actor_id(), self.actor_id());
delete
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![self.followers_url()]);
let mut undo = Undo::new(self.actor_id(), delete.into_any_base()?);
undo
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![self.followers_url()]);
@ -181,14 +181,14 @@ impl CommunityType for Community {
else {
let mut delete = Delete::new(mod_.actor_id(), self.actor_id());
delete
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![self.actor_id()]);
let mut undo = Undo::new(mod_.actor_id(), delete.into_any_base()?);
undo
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![self.actor_id()]);
@ -202,7 +202,7 @@ impl CommunityType for Community {
async fn send_remove(&self, context: &LemmyContext) -> Result<(), LemmyError> {
let mut remove = Remove::new(self.actor_id(), self.actor_id());
remove
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![self.followers_url()]);
@ -215,7 +215,7 @@ impl CommunityType for Community {
async fn send_undo_remove(&self, context: &LemmyContext) -> Result<(), LemmyError> {
let mut remove = Remove::new(self.actor_id(), self.actor_id());
remove
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![self.followers_url()]);
@ -223,7 +223,7 @@ impl CommunityType for Community {
// Undo that fake activity
let mut undo = Undo::new(self.actor_id(), remove.into_any_base()?);
undo
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(LikeType::Like)?)
.set_to(public())
.set_many_ccs(vec![self.followers_url()]);
@ -267,7 +267,7 @@ impl CommunityType for Community {
}
let mut announce = Announce::new(self.actor_id(), activity);
announce
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(AnnounceType::Announce)?)
.set_to(public())
.set_many_ccs(ccs);
@ -306,7 +306,7 @@ impl CommunityType for Community {
) -> Result<(), LemmyError> {
let mut add = Add::new(actor.actor_id(), added_mod.actor_id());
add
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(AddType::Add)?)
.set_to(public())
.set_many_ccs(vec![self.actor_id()])
@ -324,7 +324,7 @@ impl CommunityType for Community {
) -> Result<(), LemmyError> {
let mut remove = Remove::new(actor.actor_id(), removed_mod.actor_id());
remove
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![self.actor_id()])
@ -342,7 +342,7 @@ impl CommunityType for Community {
) -> Result<(), LemmyError> {
let mut block = Block::new(actor.actor_id(), blocked_user.actor_id());
block
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(BlockType::Block)?)
.set_to(public())
.set_many_ccs(vec![self.actor_id()]);
@ -359,7 +359,7 @@ impl CommunityType for Community {
) -> Result<(), LemmyError> {
let mut block = Block::new(actor.actor_id(), unblocked_user.actor_id());
block
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(BlockType::Block)?)
.set_to(public())
.set_many_ccs(vec![self.actor_id()]);
@ -367,7 +367,7 @@ impl CommunityType for Community {
// Undo that fake activity
let mut undo = Undo::new(actor.actor_id(), block.into_any_base()?);
undo
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![self.actor_id()]);

View File

@ -78,7 +78,7 @@ impl UserType for Person {
let mut follow = Follow::new(self.actor_id(), community.actor_id());
follow
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(FollowType::Follow)?)
.set_to(community.actor_id());
@ -99,7 +99,7 @@ impl UserType for Person {
let mut follow = Follow::new(self.actor_id(), community.actor_id());
follow
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(FollowType::Follow)?)
.set_to(community.actor_id());
@ -109,7 +109,7 @@ impl UserType for Person {
follow.into_any_base()?,
);
undo
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(community.actor_id());

View File

@ -54,7 +54,7 @@ impl ApubObjectType for Post {
self.ap_id.to_owned().into_inner(),
);
delete
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -79,7 +79,7 @@ impl ApubObjectType for Post {
self.ap_id.to_owned().into_inner(),
);
delete
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -90,7 +90,7 @@ impl ApubObjectType for Post {
delete.into_any_base()?,
);
undo
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -111,7 +111,7 @@ impl ApubObjectType for Post {
self.ap_id.to_owned().into_inner(),
);
remove
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -136,7 +136,7 @@ impl ApubObjectType for Post {
self.ap_id.to_owned().into_inner(),
);
remove
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(RemoveType::Remove)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -147,7 +147,7 @@ impl ApubObjectType for Post {
remove.into_any_base()?,
);
undo
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -171,7 +171,7 @@ impl ApubLikeableType for Post {
self.ap_id.to_owned().into_inner(),
);
like
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(LikeType::Like)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -192,7 +192,7 @@ impl ApubLikeableType for Post {
self.ap_id.to_owned().into_inner(),
);
dislike
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(DislikeType::Dislike)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -217,7 +217,7 @@ impl ApubLikeableType for Post {
self.ap_id.to_owned().into_inner(),
);
like
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(LikeType::Like)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);
@ -228,7 +228,7 @@ impl ApubLikeableType for Post {
like.into_any_base()?,
);
undo
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(public())
.set_many_ccs(vec![community.actor_id()]);

View File

@ -38,7 +38,7 @@ impl ApubObjectType for PrivateMessage {
);
create
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(CreateType::Create)?)
.set_to(recipient.actor_id());
@ -59,7 +59,7 @@ impl ApubObjectType for PrivateMessage {
note.into_any_base()?,
);
update
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(UpdateType::Update)?)
.set_to(recipient.actor_id());
@ -77,7 +77,7 @@ impl ApubObjectType for PrivateMessage {
self.ap_id.to_owned().into_inner(),
);
delete
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(recipient.actor_id());
@ -99,7 +99,7 @@ impl ApubObjectType for PrivateMessage {
self.ap_id.to_owned().into_inner(),
);
delete
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(DeleteType::Delete)?)
.set_to(recipient.actor_id());
@ -109,7 +109,7 @@ impl ApubObjectType for PrivateMessage {
delete.into_any_base()?,
);
undo
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(generate_activity_id(UndoType::Undo)?)
.set_to(recipient.actor_id());

View File

@ -1,9 +1,8 @@
use activitystreams::{base::AnyBase, context};
use lemmy_utils::LemmyError;
use activitystreams::{base::AnyBase, context, primitives::OneOrMany};
use serde_json::json;
use url::Url;
pub fn lemmy_context() -> Result<Vec<AnyBase>, LemmyError> {
pub fn lemmy_context() -> OneOrMany<AnyBase> {
let context_ext = AnyBase::from_arbitrary_json(json!(
{
"sc": "http://schema.org#",
@ -19,10 +18,11 @@ pub fn lemmy_context() -> Result<Vec<AnyBase>, LemmyError> {
"type": "sc:Text",
"id": "as:alsoKnownAs"
},
}))?;
Ok(vec![
}))
.expect("parse context");
OneOrMany::from(vec![
AnyBase::from(context()),
context_ext,
AnyBase::from(Url::parse("https://w3id.org/security/v1")?),
AnyBase::from(Url::parse("https://w3id.org/security/v1").expect("parse context")),
])
}

View File

@ -81,7 +81,7 @@ pub(crate) async fn get_apub_community_followers(
let mut collection = UnorderedCollection::new();
collection
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(community.followers_url.into())
.set_total_items(community_followers.len() as u64);
Ok(create_apub_response(&collection))
@ -112,7 +112,7 @@ pub(crate) async fn get_apub_community_outbox(
let mut collection = OrderedCollection::new();
collection
.set_many_items(activities)
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(community.get_outbox_url()?)
.set_total_items(len as u64);
Ok(create_apub_response(&collection))
@ -130,7 +130,7 @@ pub(crate) async fn get_apub_community_inbox(
let mut collection = OrderedCollection::new();
collection
.set_id(community.inbox_url.into())
.set_many_contexts(lemmy_context()?);
.set_many_contexts(lemmy_context());
Ok(create_apub_response(&collection))
}
@ -162,6 +162,6 @@ pub(crate) async fn get_apub_community_moderators(
.set_id(generate_moderators_url(&community.actor_id)?.into())
.set_total_items(moderators.len() as u64)
.set_many_items(moderators)
.set_many_contexts(lemmy_context()?);
.set_many_contexts(lemmy_context());
Ok(create_apub_response(&collection))
}

View File

@ -71,7 +71,7 @@ pub(crate) async fn get_apub_person_outbox(
let mut collection = OrderedCollection::new();
collection
.set_many_items(Vec::<Url>::new())
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(person.get_outbox_url()?)
.set_total_items(0_u64);
Ok(create_apub_response(&collection))
@ -89,6 +89,6 @@ pub(crate) async fn get_apub_person_inbox(
let mut collection = OrderedCollection::new();
collection
.set_id(person.inbox_url.into())
.set_many_contexts(lemmy_context()?);
.set_many_contexts(lemmy_context());
Ok(create_apub_response(&collection))
}

View File

@ -69,7 +69,7 @@ impl ToApub for Comment {
comment
// Not needed when the Post is embedded in a collection (like for community outbox)
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(self.ap_id.to_owned().into_inner())
.set_published(convert_datetime(self.published))
// NOTE: included community id for compatibility with lemmy v0.9.9

View File

@ -55,7 +55,7 @@ impl ToApub for Community {
let mut group = ApObject::new(Group::new());
group
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(self.actor_id.to_owned().into())
.set_name(self.title.to_owned())
.set_published(convert_datetime(self.published))

View File

@ -70,6 +70,25 @@ pub trait FromApubToForm<ApubType> {
Self: Sized;
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub enum MediaTypeMarkdown {
#[serde(rename = "text/markdown")]
Markdown,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub enum MediaTypeHtml {
#[serde(rename = "text/html")]
Markdown,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Source {
content: String,
media_type: MediaTypeMarkdown,
}
/// Updated is actually the deletion time
fn create_tombstone<T>(
deleted: bool,

View File

@ -50,7 +50,7 @@ impl ToApub for DbPerson {
let mut person = ApObject::new(actor);
person
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(self.actor_id.to_owned().into_inner())
.set_published(convert_datetime(self.published));

View File

@ -2,7 +2,7 @@ use crate::{
activities::extract_community,
extensions::context::lemmy_context,
fetcher::person::get_or_fetch_and_upsert_person,
objects::{create_tombstone, FromApub, ToApub},
objects::{create_tombstone, FromApub, MediaTypeHtml, MediaTypeMarkdown, Source, ToApub},
};
use activitystreams::{
base::AnyBase,
@ -34,84 +34,6 @@ use lemmy_utils::{
use lemmy_websocket::LemmyContext;
use url::Url;
#[async_trait::async_trait(?Send)]
impl ToApub for Post {
type ApubType = Page;
// Turn a Lemmy post into an ActivityPub page that can be sent out over the network.
async fn to_apub(&self, pool: &DbPool) -> Result<Page, LemmyError> {
let creator_id = self.creator_id;
let creator = blocking(pool, move |conn| Person::read(conn, creator_id)).await??;
let community_id = self.community_id;
let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
let source = self.body.clone().map(|body| Source {
content: body,
media_type: MediaTypeMarkdown::Markdown,
});
let image = self.thumbnail_url.clone().map(|thumb| ImageObject {
content: ImageType::Image,
url: thumb.into(),
});
let page = Page {
context: lemmy_context()?.into(),
r#type: PageType::Page,
id: self.ap_id.clone().into(),
attributed_to: creator.actor_id.into(),
to: [community.actor_id.into(), public()],
name: self.name.clone(),
content: self.body.as_ref().map(|b| markdown_to_html(b)),
media_type: MediaTypeHtml::Markdown,
source,
url: self.url.clone().map(|u| u.into()),
image,
comments_enabled: Some(!self.locked),
sensitive: Some(self.nsfw),
stickied: Some(self.stickied),
published: convert_datetime(self.published),
updated: self.updated.map(convert_datetime),
unparsed: Default::default(),
};
Ok(page)
}
fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
create_tombstone(
self.deleted,
self.ap_id.to_owned().into(),
self.updated,
PageType::Page,
)
}
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub enum MediaTypeMarkdown {
#[serde(rename = "text/markdown")]
Markdown,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub enum MediaTypeHtml {
#[serde(rename = "text/html")]
Markdown,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Source {
content: String,
media_type: MediaTypeMarkdown,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ImageObject {
content: ImageType,
url: Url,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Page {
@ -138,6 +60,13 @@ pub struct Page {
unparsed: Unparsed,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ImageObject {
content: ImageType,
url: Url,
}
impl Page {
/// Only mods can change the post's stickied/locked status. So if either of these is changed from
/// the current value, it is a mod action and needs to be verified as such.
@ -169,6 +98,58 @@ impl Page {
}
}
#[async_trait::async_trait(?Send)]
impl ToApub for Post {
type ApubType = Page;
// Turn a Lemmy post into an ActivityPub page that can be sent out over the network.
async fn to_apub(&self, pool: &DbPool) -> Result<Page, LemmyError> {
let creator_id = self.creator_id;
let creator = blocking(pool, move |conn| Person::read(conn, creator_id)).await??;
let community_id = self.community_id;
let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
let source = self.body.clone().map(|body| Source {
content: body,
media_type: MediaTypeMarkdown::Markdown,
});
let image = self.thumbnail_url.clone().map(|thumb| ImageObject {
content: ImageType::Image,
url: thumb.into(),
});
let page = Page {
context: lemmy_context(),
r#type: PageType::Page,
id: self.ap_id.clone().into(),
attributed_to: creator.actor_id.into(),
to: [community.actor_id.into(), public()],
name: self.name.clone(),
content: self.body.as_ref().map(|b| markdown_to_html(b)),
media_type: MediaTypeHtml::Markdown,
source,
url: self.url.clone().map(|u| u.into()),
image,
comments_enabled: Some(!self.locked),
sensitive: Some(self.nsfw),
stickied: Some(self.stickied),
published: convert_datetime(self.published),
updated: self.updated.map(convert_datetime),
unparsed: Default::default(),
};
Ok(page)
}
fn to_tombstone(&self) -> Result<Tombstone, LemmyError> {
create_tombstone(
self.deleted,
self.ap_id.to_owned().into(),
self.updated,
PageType::Page,
)
}
}
#[async_trait::async_trait(?Send)]
impl FromApub for Post {
type ApubType = Page;

View File

@ -42,7 +42,7 @@ impl ToApub for PrivateMessage {
let recipient = blocking(pool, move |conn| Person::read(conn, recipient_id)).await??;
private_message
.set_many_contexts(lemmy_context()?)
.set_many_contexts(lemmy_context())
.set_id(self.ap_id.to_owned().into_inner())
.set_published(convert_datetime(self.published))
.set_to(recipient.actor_id.into_inner())