2021-10-28 21:17:59 +00:00
|
|
|
use crate::{
|
2023-09-09 16:25:03 +00:00
|
|
|
activities::GetActorType,
|
2023-07-05 15:08:02 +00:00
|
|
|
check_apub_id_valid,
|
|
|
|
local_site_data_cached,
|
2024-01-25 14:22:11 +00:00
|
|
|
objects::{instance::fetch_instance_actor_for_object, read_from_string_or_source_opt},
|
2021-10-28 21:17:59 +00:00
|
|
|
protocol::{
|
2022-10-06 18:27:58 +00:00
|
|
|
objects::{group::Group, Endpoints, LanguageTag},
|
2021-10-28 21:17:59 +00:00
|
|
|
ImageObject,
|
2022-04-01 18:25:19 +00:00
|
|
|
Source,
|
2021-10-28 21:17:59 +00:00
|
|
|
},
|
2022-06-02 14:33:41 +00:00
|
|
|
};
|
|
|
|
use activitypub_federation::{
|
2023-03-21 15:03:05 +00:00
|
|
|
config::Data,
|
|
|
|
kinds::actor::GroupType,
|
|
|
|
traits::{Actor, Object},
|
2021-10-28 21:17:59 +00:00
|
|
|
};
|
2023-08-24 15:27:00 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
2022-11-28 14:29:33 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
context::LemmyContext,
|
2024-01-25 14:22:11 +00:00
|
|
|
utils::{
|
|
|
|
generate_featured_url,
|
|
|
|
generate_moderators_url,
|
|
|
|
generate_outbox_url,
|
2024-03-15 11:03:29 +00:00
|
|
|
get_url_blocklist,
|
2024-01-25 14:22:11 +00:00
|
|
|
local_site_opt_to_slur_regex,
|
|
|
|
process_markdown_opt,
|
|
|
|
proxy_image_link_opt_apub,
|
|
|
|
},
|
2022-11-28 14:29:33 +00:00
|
|
|
};
|
2022-10-06 18:27:58 +00:00
|
|
|
use lemmy_db_schema::{
|
2022-10-27 09:24:07 +00:00
|
|
|
source::{
|
2023-09-09 16:25:03 +00:00
|
|
|
activity::ActorType,
|
2022-10-27 09:24:07 +00:00
|
|
|
actor_language::CommunityLanguage,
|
2024-01-25 14:22:11 +00:00
|
|
|
community::{Community, CommunityInsertForm, CommunityUpdateForm},
|
|
|
|
local_site::LocalSite,
|
2022-10-27 09:24:07 +00:00
|
|
|
},
|
|
|
|
traits::{ApubActor, Crud},
|
2024-01-25 14:22:11 +00:00
|
|
|
utils::naive_now,
|
2022-10-06 18:27:58 +00:00
|
|
|
};
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_db_views_actor::structs::CommunityFollowerView;
|
2024-01-04 16:42:18 +00:00
|
|
|
use lemmy_utils::{error::LemmyError, spawn_try_task, utils::markdown::markdown_to_html};
|
2021-11-05 00:24:10 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
use url::Url;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2021-10-18 21:36:44 +00:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct ApubCommunity(Community);
|
|
|
|
|
|
|
|
impl Deref for ApubCommunity {
|
|
|
|
type Target = Community;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Community> for ApubCommunity {
|
|
|
|
fn from(c: Community) -> Self {
|
2022-03-30 14:58:03 +00:00
|
|
|
ApubCommunity(c)
|
2021-10-18 21:36:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl Object for ApubCommunity {
|
2021-10-18 21:36:44 +00:00
|
|
|
type DataType = LemmyContext;
|
2023-03-21 15:03:05 +00:00
|
|
|
type Kind = Group;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
2021-10-18 21:36:44 +00:00
|
|
|
|
2023-08-24 15:27:00 +00:00
|
|
|
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
|
2021-10-18 21:36:44 +00:00
|
|
|
Some(self.last_refreshed_at)
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn read_from_id(
|
2021-10-18 21:36:44 +00:00
|
|
|
object_id: Url,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<Self::DataType>,
|
2021-10-18 21:36:44 +00:00
|
|
|
) -> Result<Option<Self>, LemmyError> {
|
|
|
|
Ok(
|
2023-07-11 13:09:59 +00:00
|
|
|
Community::read_from_apub_id(&mut context.pool(), &object_id.into())
|
2022-11-09 10:05:00 +00:00
|
|
|
.await?
|
|
|
|
.map(Into::into),
|
2021-10-18 21:36:44 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn delete(self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
|
2023-08-08 09:41:41 +00:00
|
|
|
let form = CommunityUpdateForm {
|
|
|
|
deleted: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2023-07-11 13:09:59 +00:00
|
|
|
Community::update(&mut context.pool(), self.id, &form).await?;
|
2021-10-18 21:36:44 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn into_json(self, data: &Data<Self::DataType>) -> Result<Group, LemmyError> {
|
2022-10-06 18:27:58 +00:00
|
|
|
let community_id = self.id;
|
2023-07-11 13:09:59 +00:00
|
|
|
let langs = CommunityLanguage::read(&mut data.pool(), community_id).await?;
|
|
|
|
let language = LanguageTag::new_multiple(langs, &mut data.pool()).await?;
|
2022-10-06 18:27:58 +00:00
|
|
|
|
2021-08-12 12:48:09 +00:00
|
|
|
let group = Group {
|
|
|
|
kind: GroupType::Group,
|
2023-03-21 15:03:05 +00:00
|
|
|
id: self.id().into(),
|
2021-08-12 12:48:09 +00:00
|
|
|
preferred_username: self.name.clone(),
|
2022-03-24 16:33:42 +00:00
|
|
|
name: Some(self.title.clone()),
|
2021-10-22 16:21:26 +00:00
|
|
|
summary: self.description.as_ref().map(|b| markdown_to_html(b)),
|
2022-04-01 18:25:19 +00:00
|
|
|
source: self.description.clone().map(Source::new),
|
2022-02-07 19:23:12 +00:00
|
|
|
icon: self.icon.clone().map(ImageObject::new),
|
|
|
|
image: self.banner.clone().map(ImageObject::new),
|
2021-08-12 12:48:09 +00:00
|
|
|
sensitive: Some(self.nsfw),
|
2023-02-18 14:50:28 +00:00
|
|
|
featured: Some(generate_featured_url(&self.actor_id)?.into()),
|
2021-08-12 12:48:09 +00:00
|
|
|
inbox: self.inbox_url.clone().into(),
|
2023-03-21 15:03:05 +00:00
|
|
|
outbox: generate_outbox_url(&self.actor_id)?.into(),
|
2021-08-12 12:48:09 +00:00
|
|
|
followers: self.followers_url.clone().into(),
|
2022-01-17 14:40:47 +00:00
|
|
|
endpoints: self.shared_inbox_url.clone().map(|s| Endpoints {
|
|
|
|
shared_inbox: s.into(),
|
|
|
|
}),
|
2023-03-21 15:03:05 +00:00
|
|
|
public_key: self.public_key(),
|
2022-10-06 18:27:58 +00:00
|
|
|
language,
|
2023-10-17 17:25:35 +00:00
|
|
|
published: Some(self.published),
|
|
|
|
updated: self.updated,
|
2022-04-28 20:32:32 +00:00
|
|
|
posting_restricted_to_mods: Some(self.posting_restricted_to_mods),
|
2023-02-18 14:50:28 +00:00
|
|
|
attributed_to: Some(generate_moderators_url(&self.actor_id)?.into()),
|
2021-08-12 12:48:09 +00:00
|
|
|
};
|
|
|
|
Ok(group)
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-11-06 17:35:14 +00:00
|
|
|
async fn verify(
|
|
|
|
group: &Group,
|
|
|
|
expected_domain: &Url,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<Self::DataType>,
|
2021-11-06 17:35:14 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
group.verify(expected_domain, context).await
|
|
|
|
}
|
|
|
|
|
2021-03-05 13:45:30 +00:00
|
|
|
/// Converts a `Group` to `Community`, inserts it into the database and updates moderators.
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn from_json(
|
2021-11-06 12:37:55 +00:00
|
|
|
group: Group,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<Self::DataType>,
|
2021-10-18 21:36:44 +00:00
|
|
|
) -> Result<ApubCommunity, LemmyError> {
|
2023-03-21 15:03:05 +00:00
|
|
|
let instance_id = fetch_instance_actor_for_object(&group.id, context).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2024-01-25 14:22:11 +00:00
|
|
|
let local_site = LocalSite::read(&mut context.pool()).await.ok();
|
|
|
|
let slur_regex = &local_site_opt_to_slur_regex(&local_site);
|
2024-03-15 11:03:29 +00:00
|
|
|
let url_blocklist = get_url_blocklist(context).await?;
|
2024-01-25 14:22:11 +00:00
|
|
|
let description = read_from_string_or_source_opt(&group.summary, &None, &group.source);
|
2024-03-15 11:03:29 +00:00
|
|
|
let description =
|
|
|
|
process_markdown_opt(&description, slur_regex, &url_blocklist, context).await?;
|
2024-01-25 14:22:11 +00:00
|
|
|
let icon = proxy_image_link_opt_apub(group.icon.map(|i| i.url), context).await?;
|
|
|
|
let banner = proxy_image_link_opt_apub(group.image.map(|i| i.url), context).await?;
|
|
|
|
|
|
|
|
let form = CommunityInsertForm {
|
|
|
|
name: group.preferred_username.clone(),
|
|
|
|
title: group.name.unwrap_or(group.preferred_username.clone()),
|
|
|
|
description,
|
|
|
|
published: group.published,
|
|
|
|
updated: group.updated,
|
|
|
|
deleted: Some(false),
|
|
|
|
nsfw: Some(group.sensitive.unwrap_or(false)),
|
|
|
|
actor_id: Some(group.id.into()),
|
|
|
|
local: Some(false),
|
|
|
|
public_key: group.public_key.public_key_pem,
|
|
|
|
last_refreshed_at: Some(naive_now()),
|
|
|
|
icon,
|
|
|
|
banner,
|
|
|
|
followers_url: Some(group.followers.clone().into()),
|
|
|
|
inbox_url: Some(group.inbox.into()),
|
|
|
|
shared_inbox_url: group.endpoints.map(|e| e.shared_inbox.into()),
|
|
|
|
moderators_url: group.attributed_to.clone().map(Into::into),
|
|
|
|
posting_restricted_to_mods: group.posting_restricted_to_mods,
|
|
|
|
instance_id,
|
2024-02-26 14:45:23 +00:00
|
|
|
featured_url: group.featured.clone().map(Into::into),
|
2024-01-25 16:04:25 +00:00
|
|
|
..Default::default()
|
2024-01-25 14:22:11 +00:00
|
|
|
};
|
2023-07-11 13:09:59 +00:00
|
|
|
let languages =
|
|
|
|
LanguageTag::to_language_id_multiple(group.language, &mut context.pool()).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
let community = Community::create(&mut context.pool(), &form).await?;
|
|
|
|
CommunityLanguage::update(&mut context.pool(), languages, community.id).await?;
|
2022-11-09 10:05:00 +00:00
|
|
|
|
|
|
|
let community: ApubCommunity = community.into();
|
2021-10-27 16:03:07 +00:00
|
|
|
|
2024-03-14 20:57:56 +00:00
|
|
|
// These collections are not necessary for Lemmy to work, so ignore errors.
|
2024-01-04 16:42:18 +00:00
|
|
|
let community_ = community.clone();
|
|
|
|
let context_ = context.reset_request_count();
|
|
|
|
spawn_try_task(async move {
|
2024-03-14 20:57:56 +00:00
|
|
|
group.outbox.dereference(&community_, &context_).await.ok();
|
|
|
|
group
|
|
|
|
.followers
|
|
|
|
.dereference(&community_, &context_)
|
|
|
|
.await
|
|
|
|
.ok();
|
2024-02-26 14:45:23 +00:00
|
|
|
if let Some(featured) = group.featured {
|
2024-03-14 20:57:56 +00:00
|
|
|
featured.dereference(&community_, &context_).await.ok();
|
2024-02-26 14:45:23 +00:00
|
|
|
}
|
2024-01-04 16:42:18 +00:00
|
|
|
if let Some(moderators) = group.attributed_to {
|
2024-03-14 20:57:56 +00:00
|
|
|
moderators.dereference(&community_, &context_).await.ok();
|
2024-01-04 16:42:18 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
});
|
2021-10-27 16:03:07 +00:00
|
|
|
|
|
|
|
Ok(community)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 15:45:39 +00:00
|
|
|
impl Actor for ApubCommunity {
|
2023-03-21 15:03:05 +00:00
|
|
|
fn id(&self) -> Url {
|
|
|
|
self.actor_id.inner().clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn public_key_pem(&self) -> &str {
|
2022-06-08 15:45:39 +00:00
|
|
|
&self.public_key
|
2021-10-27 16:03:07 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
fn private_key_pem(&self) -> Option<String> {
|
|
|
|
self.private_key.clone()
|
|
|
|
}
|
|
|
|
|
2022-06-08 15:45:39 +00:00
|
|
|
fn inbox(&self) -> Url {
|
2021-10-27 16:03:07 +00:00
|
|
|
self.inbox_url.clone().into()
|
|
|
|
}
|
2021-09-25 15:44:52 +00:00
|
|
|
|
2022-06-08 15:45:39 +00:00
|
|
|
fn shared_inbox(&self) -> Option<Url> {
|
2022-11-19 04:33:54 +00:00
|
|
|
self.shared_inbox_url.clone().map(Into::into)
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-06 20:20:05 +00:00
|
|
|
|
2023-09-09 16:25:03 +00:00
|
|
|
impl GetActorType for ApubCommunity {
|
|
|
|
fn actor_type(&self) -> ActorType {
|
|
|
|
ActorType::Community
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 11:46:48 +00:00
|
|
|
impl ApubCommunity {
|
2021-10-06 20:20:05 +00:00
|
|
|
/// For a given community, returns the inboxes of all followers.
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-10-28 11:46:48 +00:00
|
|
|
pub(crate) async fn get_follower_inboxes(
|
2021-10-06 20:20:05 +00:00
|
|
|
&self,
|
2021-11-02 13:02:39 +00:00
|
|
|
context: &LemmyContext,
|
2021-10-06 20:20:05 +00:00
|
|
|
) -> Result<Vec<Url>, LemmyError> {
|
|
|
|
let id = self.id;
|
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
let local_site_data = local_site_data_cached(&mut context.pool()).await?;
|
|
|
|
let follows =
|
|
|
|
CommunityFollowerView::get_community_follower_inboxes(&mut context.pool(), id).await?;
|
2021-11-15 21:37:19 +00:00
|
|
|
let inboxes: Vec<Url> = follows
|
2021-10-06 20:20:05 +00:00
|
|
|
.into_iter()
|
2023-07-05 15:50:26 +00:00
|
|
|
.map(Into::into)
|
2021-11-15 21:37:19 +00:00
|
|
|
.filter(|inbox: &Url| inbox.host_str() != Some(&context.settings().hostname))
|
2021-10-06 20:20:05 +00:00
|
|
|
// Don't send to blocked instances
|
2023-07-05 15:08:02 +00:00
|
|
|
.filter(|inbox| check_apub_id_valid(inbox, &local_site_data).is_ok())
|
2021-10-06 20:20:05 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
Ok(inboxes)
|
|
|
|
}
|
|
|
|
}
|
2021-10-21 17:25:35 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2021-11-01 13:05:20 +00:00
|
|
|
pub(crate) mod tests {
|
2021-10-29 14:54:19 +00:00
|
|
|
use super::*;
|
2022-02-17 22:04:01 +00:00
|
|
|
use crate::{
|
2024-01-25 14:22:11 +00:00
|
|
|
objects::instance::tests::parse_lemmy_instance,
|
2022-02-17 22:04:01 +00:00
|
|
|
protocol::tests::file_to_json_object,
|
2022-02-07 19:23:12 +00:00
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::fetch::collection_id::CollectionId;
|
2024-02-26 14:47:10 +00:00
|
|
|
use lemmy_db_schema::source::site::Site;
|
2023-11-17 03:51:33 +00:00
|
|
|
use lemmy_utils::error::LemmyResult;
|
2024-01-04 09:47:18 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2021-10-29 14:54:19 +00:00
|
|
|
use serial_test::serial;
|
2021-10-28 21:17:59 +00:00
|
|
|
|
2023-11-17 03:51:33 +00:00
|
|
|
pub(crate) async fn parse_lemmy_community(
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
) -> LemmyResult<ApubCommunity> {
|
2023-03-21 15:03:05 +00:00
|
|
|
// use separate counter so this doesnt affect tests
|
|
|
|
let context2 = context.reset_request_count();
|
2023-11-17 03:51:33 +00:00
|
|
|
let mut json: Group = file_to_json_object("assets/lemmy/objects/group.json")?;
|
2021-10-21 17:25:35 +00:00
|
|
|
// change these links so they dont fetch over the network
|
2022-11-25 14:01:58 +00:00
|
|
|
json.attributed_to = None;
|
2023-11-17 03:51:33 +00:00
|
|
|
json.outbox = CollectionId::parse("https://enterprise.lemmy.ml/c/tenforward/not_outbox")?;
|
|
|
|
json.followers = CollectionId::parse("https://enterprise.lemmy.ml/c/tenforward/not_followers")?;
|
|
|
|
|
|
|
|
let url = Url::parse("https://enterprise.lemmy.ml/c/tenforward")?;
|
|
|
|
ApubCommunity::verify(&json, &url, &context2).await?;
|
|
|
|
let community = ApubCommunity::from_json(json, &context2).await?;
|
|
|
|
Ok(community)
|
2021-11-01 13:05:20 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 08:24:11 +00:00
|
|
|
#[tokio::test]
|
2021-11-01 13:05:20 +00:00
|
|
|
#[serial]
|
2023-11-17 03:51:33 +00:00
|
|
|
async fn test_parse_lemmy_community() -> LemmyResult<()> {
|
2024-01-25 14:22:11 +00:00
|
|
|
let context = LemmyContext::init_test_context().await;
|
2023-11-17 03:51:33 +00:00
|
|
|
let site = parse_lemmy_instance(&context).await?;
|
|
|
|
let community = parse_lemmy_community(&context).await?;
|
2021-10-21 17:25:35 +00:00
|
|
|
|
2021-10-22 16:21:26 +00:00
|
|
|
assert_eq!(community.title, "Ten Forward");
|
2021-10-21 17:25:35 +00:00
|
|
|
assert!(!community.local);
|
2023-11-17 03:51:33 +00:00
|
|
|
assert_eq!(
|
|
|
|
community.description.as_ref().map(std::string::String::len),
|
|
|
|
Some(132)
|
|
|
|
);
|
2021-10-21 17:25:35 +00:00
|
|
|
|
2023-11-17 03:51:33 +00:00
|
|
|
Community::delete(&mut context.pool(), community.id).await?;
|
|
|
|
Site::delete(&mut context.pool(), site.id).await?;
|
|
|
|
Ok(())
|
2021-10-21 17:25:35 +00:00
|
|
|
}
|
|
|
|
}
|