-use activitystreams::{
- collection::{CollectionExt, OrderedCollection},
- unparsed::UnparsedMutExt,
-};
+use activitystreams::unparsed::UnparsedMutExt;
use activitystreams_ext::UnparsedExtension;
use lemmy_utils::LemmyError;
use serde::{Deserialize, Serialize};
#[serde(rename_all = "camelCase")]
pub struct GroupExtension {
pub sensitive: Option<bool>,
- pub moderators: Option<OrderedCollection>,
+ pub moderators: Option<Url>,
}
impl GroupExtension {
- pub fn new(sensitive: bool, moderators: Vec<Url>) -> Result<GroupExtension, LemmyError> {
- let mut mods = OrderedCollection::new();
- mods.set_total_items(moderators.len() as u64);
- mods.set_many_items(moderators);
+ pub fn new(sensitive: bool, moderators_url: Url) -> Result<GroupExtension, LemmyError> {
Ok(GroupExtension {
sensitive: Some(sensitive),
- moderators: Some(mods),
+ moderators: Some(moderators_url),
})
}
}
Ok(())
}
+
+pub(crate) async fn fetch_community_mods(
+ context: &LemmyContext,
+ group: &GroupExt,
+ recursion_counter: &mut i32,
+) -> Result<Vec<Url>, LemmyError> {
+ if let Some(mods_url) = &group.ext_one.moderators {
+ let mods =
+ fetch_remote_object::<OrderedCollection>(context.client(), mods_url, recursion_counter)
+ .await?;
+ let mods = mods
+ .items()
+ .map(|i| i.as_many())
+ .flatten()
+ .context(location_info!())?
+ .iter()
+ .filter_map(|i| i.as_xsd_any_uri())
+ .map(|u| u.to_owned())
+ .collect();
+ Ok(mods)
+ } else {
+ Ok(vec![])
+ }
+}
use serde::Deserialize;
#[derive(Deserialize)]
-pub struct CommentQuery {
+pub(crate) struct CommentQuery {
comment_id: String,
}
/// Return the ActivityPub json representation of a local comment over HTTP.
-pub async fn get_apub_comment(
+pub(crate) async fn get_apub_comment(
info: Path<CommentQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
use crate::{
extensions::context::lemmy_context,
+ generate_moderators_url,
http::{create_apub_response, create_apub_tombstone_response},
objects::ToApub,
ActorType,
use activitystreams::{
base::{AnyBase, BaseExt},
collection::{CollectionExt, OrderedCollection, UnorderedCollection},
+ url::Url,
};
use actix_web::{body::Body, web, HttpResponse};
use lemmy_api_structs::blocking;
use lemmy_db_queries::source::{activity::Activity_, community::Community_};
use lemmy_db_schema::source::{activity::Activity, community::Community};
-use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
+use lemmy_db_views_actor::{
+ community_follower_view::CommunityFollowerView,
+ community_moderator_view::CommunityModeratorView,
+};
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::Deserialize;
#[derive(Deserialize)]
-pub struct CommunityQuery {
+pub(crate) struct CommunityQuery {
community_name: String,
}
/// Return the ActivityPub json representation of a local community over HTTP.
-pub async fn get_apub_community_http(
+pub(crate) async fn get_apub_community_http(
info: web::Path<CommunityQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
}
/// Returns an empty followers collection, only populating the size (for privacy).
-pub async fn get_apub_community_followers(
+pub(crate) async fn get_apub_community_followers(
info: web::Path<CommunityQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
/// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
/// activites like votes or comments).
-pub async fn get_apub_community_outbox(
+pub(crate) async fn get_apub_community_outbox(
info: web::Path<CommunityQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
Ok(create_apub_response(&collection))
}
-pub async fn get_apub_community_inbox(
+pub(crate) async fn get_apub_community_inbox(
+ info: web::Path<CommunityQuery>,
+ context: web::Data<LemmyContext>,
+) -> Result<HttpResponse<Body>, LemmyError> {
+ let community = blocking(context.pool(), move |conn| {
+ Community::read_from_name(&conn, &info.community_name)
+ })
+ .await??;
+
+ let mut collection = OrderedCollection::new();
+ collection
+ .set_id(community.inbox_url.into())
+ .set_many_contexts(lemmy_context()?);
+ Ok(create_apub_response(&collection))
+}
+
+pub(crate) async fn get_apub_community_moderators(
info: web::Path<CommunityQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
})
.await??;
+ // The attributed to, is an ordered vector with the creator actor_ids first,
+ // then the rest of the moderators
+ // TODO Technically the instance admins can mod the community, but lets
+ // ignore that for now
+ let cid = community.id;
+ let moderators = blocking(context.pool(), move |conn| {
+ CommunityModeratorView::for_community(&conn, cid)
+ })
+ .await??;
+
+ let moderators: Vec<Url> = moderators
+ .into_iter()
+ .map(|m| m.moderator.actor_id.into_inner())
+ .collect();
let mut collection = OrderedCollection::new();
collection
- .set_id(format!("{}/inbox", community.actor_id).parse()?)
+ .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()?);
Ok(create_apub_response(&collection))
}
}
/// Return the ActivityPub json representation of a local community over HTTP.
-pub async fn get_activity(
+pub(crate) async fn get_activity(
info: web::Path<CommunityQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
use serde::Deserialize;
#[derive(Deserialize)]
-pub struct PostQuery {
+pub(crate) struct PostQuery {
post_id: String,
}
/// Return the ActivityPub json representation of a local post over HTTP.
-pub async fn get_apub_post(
+pub(crate) async fn get_apub_post(
info: web::Path<PostQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
use url::Url;
#[derive(Deserialize)]
-pub struct UserQuery {
+pub(crate) struct UserQuery {
user_name: String,
}
/// Return the ActivityPub json representation of a local user over HTTP.
-pub async fn get_apub_user_http(
+pub(crate) async fn get_apub_user_http(
info: web::Path<UserQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
}
}
-pub async fn get_apub_user_outbox(
+pub(crate) async fn get_apub_user_outbox(
info: web::Path<UserQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
Ok(create_apub_response(&collection))
}
-pub async fn get_apub_user_inbox(
+pub(crate) async fn get_apub_user_inbox(
info: web::Path<UserQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
let mut collection = OrderedCollection::new();
collection
- .set_id(format!("{}/inbox", user.actor_id.into_inner()).parse()?)
+ .set_id(user.inbox_url.into())
.set_many_contexts(lemmy_context()?);
Ok(create_apub_response(&collection))
}
Ok(Url::parse(&url)?.into())
}
+pub(crate) fn generate_moderators_url(community_id: &DbUrl) -> Result<DbUrl, LemmyError> {
+ Ok(Url::parse(&format!("{}/moderators", community_id))?.into())
+}
+
/// Store a sent or received activity in the database, for logging purposes. These records are not
/// persistent.
pub(crate) async fn insert_activity<T>(
use crate::{
extensions::{context::lemmy_context, group_extensions::GroupExtension},
- fetcher::user::get_or_fetch_and_upsert_user,
+ fetcher::{community::fetch_community_mods, user::get_or_fetch_and_upsert_user},
+ generate_moderators_url,
objects::{
check_object_domain,
create_tombstone,
impl ToApub for Community {
type ApubType = GroupExt;
- async fn to_apub(&self, pool: &DbPool) -> Result<GroupExt, LemmyError> {
- // The attributed to, is an ordered vector with the creator actor_ids first,
- // then the rest of the moderators
- // TODO Technically the instance admins can mod the community, but lets
- // ignore that for now
- let id = self.id;
- let moderators = blocking(pool, move |conn| {
- CommunityModeratorView::for_community(&conn, id)
- })
- .await??;
-
+ async fn to_apub(&self, _pool: &DbPool) -> Result<GroupExt, LemmyError> {
let mut group = ApObject::new(Group::new());
group
.set_many_contexts(lemmy_context()?)
..Default::default()
});
- let moderators: Vec<Url> = moderators
- .into_iter()
- .map(|m| m.moderator.actor_id.into_inner())
- .collect();
-
Ok(Ext2::new(
ap_actor,
- GroupExtension::new(self.nsfw, moderators)?,
+ GroupExtension::new(self.nsfw, generate_moderators_url(&self.actor_id)?.into())?,
self.get_public_key_ext()?,
))
}
let community: Community =
get_object_from_apub(group, context, expected_domain, request_counter).await?;
- let new_moderators = get_community_moderators(group)?;
+ let new_moderators = fetch_community_mods(context, group, request_counter).await?;
let community_id = community.id;
let current_moderators = blocking(context.pool(), move |conn| {
CommunityModeratorView::for_community(&conn, community_id)
expected_domain: Url,
request_counter: &mut i32,
) -> Result<Self, LemmyError> {
- let moderator_uris = get_community_moderators(group)?;
+ let moderator_uris = fetch_community_mods(context, group, request_counter).await?;
let creator_uri = moderator_uris.first().context(location_info!())?;
let creator = get_or_fetch_and_upsert_user(creator_uri, context, request_counter).await?;
})
}
}
-
-fn get_community_moderators(group: &GroupExt) -> Result<Vec<&Url>, LemmyError> {
- if let Some(moderators) = &group.ext_one.moderators {
- Ok(
- moderators
- .items()
- .map(|i| i.as_many())
- .flatten()
- .context(location_info!())?
- .iter()
- .filter_map(|i| i.as_xsd_any_uri())
- .collect(),
- )
- } else {
- Ok(vec![])
- }
-}
get_apub_community_followers,
get_apub_community_http,
get_apub_community_inbox,
+ get_apub_community_moderators,
get_apub_community_outbox,
},
get_activity,
"/c/{community_name}/inbox",
web::get().to(get_apub_community_inbox),
)
+ .route(
+ "/c/{community_name}/moderators",
+ web::get().to(get_apub_community_moderators),
+ )
.route("/u/{user_name}", web::get().to(get_apub_user_http))
.route("/u/{user_name}/outbox", web::get().to(get_apub_user_outbox))
.route("/u/{user_name}/inbox", web::get().to(get_apub_user_inbox))