Merge pull request 'Explicitly mark posts and comments as public (ref #1220)' (#167) from comments-posts-public into main

Reviewed-on: https://yerbamate.ml/LemmyNet/lemmy/pulls/167
pull/1426/head
dessalines 2021-02-10 19:45:36 +00:00
commit f899831ed3
3 changed files with 35 additions and 19 deletions

View File

@ -18,6 +18,7 @@ use crate::{
use activitystreams::{ use activitystreams::{
object::{kind::NoteType, ApObject, Note, Tombstone}, object::{kind::NoteType, ApObject, Note, Tombstone},
prelude::*, prelude::*,
public,
}; };
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use lemmy_db_queries::{Crud, DbPool}; use lemmy_db_queries::{Crud, DbPool};
@ -67,7 +68,7 @@ impl ToApub for Comment {
.set_many_contexts(lemmy_context()?) .set_many_contexts(lemmy_context()?)
.set_id(self.ap_id.to_owned().into_inner()) .set_id(self.ap_id.to_owned().into_inner())
.set_published(convert_datetime(self.published)) .set_published(convert_datetime(self.published))
.set_to(community.actor_id.into_inner()) .set_many_tos(vec![community.actor_id.into_inner(), public()])
.set_many_in_reply_tos(in_reply_to_vec) .set_many_in_reply_tos(in_reply_to_vec)
.set_attributed_to(creator.actor_id.into_inner()); .set_attributed_to(creator.actor_id.into_inner());

View File

@ -11,7 +11,9 @@ use activitystreams::{
}; };
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use diesel::result::Error::NotFound;
use lemmy_db_queries::{ApubObject, Crud, DbPool}; use lemmy_db_queries::{ApubObject, Crud, DbPool};
use lemmy_db_schema::source::community::Community;
use lemmy_structs::blocking; use lemmy_structs::blocking;
use lemmy_utils::{location_info, settings::Settings, utils::convert_datetime, LemmyError}; use lemmy_utils::{location_info, settings::Settings, utils::convert_datetime, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
@ -217,11 +219,31 @@ where
.as_single_xsd_any_uri() .as_single_xsd_any_uri()
.context(location_info!())?; .context(location_info!())?;
let user = get_or_fetch_and_upsert_user(user_id, context, request_counter).await?; let user = get_or_fetch_and_upsert_user(user_id, context, request_counter).await?;
let community_id = object let community = get_to_community(object, context, request_counter).await?;
.to()
.context(location_info!())?
.as_single_xsd_any_uri()
.context(location_info!())?;
let community = get_or_fetch_and_upsert_community(community_id, context, request_counter).await?;
check_community_or_site_ban(&user, &community, context.pool()).await check_community_or_site_ban(&user, &community, context.pool()).await
} }
pub(in crate::objects) async fn get_to_community<T, Kind>(
object: &T,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<Community, LemmyError>
where
T: ObjectExt<Kind>,
{
let community_ids = object
.to()
.context(location_info!())?
.as_many()
.context(location_info!())?
.iter()
.map(|a| a.as_xsd_any_uri().context(location_info!()))
.collect::<Result<Vec<&Url>, anyhow::Error>>()?;
for cid in community_ids {
let community = get_or_fetch_and_upsert_community(&cid, context, request_counter).await;
if community.is_ok() {
return community;
}
}
Err(NotFound.into())
}

View File

@ -1,12 +1,13 @@
use crate::{ use crate::{
extensions::{context::lemmy_context, page_extension::PageExtension}, extensions::{context::lemmy_context, page_extension::PageExtension},
fetcher::{community::get_or_fetch_and_upsert_community, user::get_or_fetch_and_upsert_user}, fetcher::user::get_or_fetch_and_upsert_user,
objects::{ objects::{
check_object_domain, check_object_domain,
check_object_for_community_or_site_ban, check_object_for_community_or_site_ban,
create_tombstone, create_tombstone,
get_object_from_apub, get_object_from_apub,
get_source_markdown_value, get_source_markdown_value,
get_to_community,
set_content_and_source, set_content_and_source,
FromApub, FromApub,
FromApubToForm, FromApubToForm,
@ -17,6 +18,7 @@ use crate::{
use activitystreams::{ use activitystreams::{
object::{kind::PageType, ApObject, Image, Page, Tombstone}, object::{kind::PageType, ApObject, Image, Page, Tombstone},
prelude::*, prelude::*,
public,
}; };
use activitystreams_ext::Ext1; use activitystreams_ext::Ext1;
use anyhow::Context; use anyhow::Context;
@ -60,7 +62,7 @@ impl ToApub for Post {
// https://mastodon.xyz/@Louisa/103987265222901387.json // https://mastodon.xyz/@Louisa/103987265222901387.json
.set_summary(self.name.to_owned()) .set_summary(self.name.to_owned())
.set_published(convert_datetime(self.published)) .set_published(convert_datetime(self.published))
.set_to(community.actor_id.into_inner()) .set_many_tos(vec![community.actor_id.into_inner(), public()])
.set_attributed_to(creator.actor_id.into_inner()); .set_attributed_to(creator.actor_id.into_inner());
if let Some(body) = &self.body { if let Some(body) = &self.body {
@ -139,16 +141,7 @@ impl FromApubToForm<PageExt> for PostForm {
let creator = get_or_fetch_and_upsert_user(creator_actor_id, context, request_counter).await?; let creator = get_or_fetch_and_upsert_user(creator_actor_id, context, request_counter).await?;
let community_actor_id = page let community = get_to_community(page, context, request_counter).await?;
.inner
.to()
.as_ref()
.context(location_info!())?
.as_single_xsd_any_uri()
.context(location_info!())?;
let community =
get_or_fetch_and_upsert_community(community_actor_id, context, request_counter).await?;
let thumbnail_url = match &page.inner.image() { let thumbnail_url = match &page.inner.image() {
Some(any_image) => Image::from_any_base( Some(any_image) => Image::from_any_base(