2021-10-28 21:17:59 +00:00
|
|
|
use crate::{
|
2022-06-02 14:33:41 +00:00
|
|
|
check_apub_id_valid_with_strictness,
|
2021-10-28 21:17:59 +00:00
|
|
|
collections::{
|
2023-02-18 14:50:28 +00:00
|
|
|
community_featured::ApubCommunityFeatured,
|
2021-10-28 21:17:59 +00:00
|
|
|
community_moderators::ApubCommunityModerators,
|
|
|
|
community_outbox::ApubCommunityOutbox,
|
|
|
|
},
|
2023-07-05 15:08:02 +00:00
|
|
|
local_site_data_cached,
|
2022-05-10 17:08:13 +00:00
|
|
|
objects::{community::ApubCommunity, read_from_string_or_source_opt},
|
2022-10-06 18:27:58 +00:00
|
|
|
protocol::{
|
|
|
|
objects::{Endpoints, LanguageTag},
|
|
|
|
ImageObject,
|
|
|
|
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
|
|
|
fetch::{collection_id::CollectionId, object_id::ObjectId},
|
|
|
|
kinds::actor::GroupType,
|
|
|
|
protocol::{
|
|
|
|
helpers::deserialize_skip_error,
|
|
|
|
public_key::PublicKey,
|
|
|
|
verification::verify_domains_match,
|
|
|
|
},
|
2022-06-02 14:33:41 +00:00
|
|
|
};
|
2023-08-24 15:27:00 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
2023-07-26 18:01:15 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
context::LemmyContext,
|
|
|
|
utils::{local_site_opt_to_slur_regex, sanitize_html, sanitize_html_opt},
|
|
|
|
};
|
2022-10-27 09:24:07 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
newtypes::InstanceId,
|
|
|
|
source::community::{CommunityInsertForm, CommunityUpdateForm},
|
|
|
|
utils::naive_now,
|
|
|
|
};
|
2021-10-28 21:17:59 +00:00
|
|
|
use lemmy_utils::{
|
2022-06-02 14:33:41 +00:00
|
|
|
error::LemmyError,
|
2023-02-16 04:05:14 +00:00
|
|
|
utils::slurs::{check_slurs, check_slurs_opt},
|
2021-10-28 21:17:59 +00:00
|
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_with::skip_serializing_none;
|
2023-02-05 05:38:08 +00:00
|
|
|
use std::fmt::Debug;
|
2021-10-28 21:17:59 +00:00
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
#[skip_serializing_none]
|
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Group {
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub(crate) kind: GroupType,
|
2021-11-03 16:26:09 +00:00
|
|
|
pub(crate) id: ObjectId<ApubCommunity>,
|
2022-02-17 22:04:01 +00:00
|
|
|
/// username, set at account creation and usually fixed after that
|
2021-10-28 21:17:59 +00:00
|
|
|
pub(crate) preferred_username: String,
|
2022-02-17 22:04:01 +00:00
|
|
|
pub(crate) inbox: Url,
|
|
|
|
pub(crate) followers: Url,
|
|
|
|
pub(crate) public_key: PublicKey,
|
|
|
|
|
2022-03-24 16:33:42 +00:00
|
|
|
/// title
|
|
|
|
pub(crate) name: Option<String>,
|
2021-10-28 21:17:59 +00:00
|
|
|
pub(crate) summary: Option<String>,
|
2022-06-02 14:33:41 +00:00
|
|
|
#[serde(deserialize_with = "deserialize_skip_error", default)]
|
2022-04-01 18:25:19 +00:00
|
|
|
pub(crate) source: Option<Source>,
|
2021-10-28 21:17:59 +00:00
|
|
|
pub(crate) icon: Option<ImageObject>,
|
|
|
|
/// banner
|
|
|
|
pub(crate) image: Option<ImageObject>,
|
|
|
|
// lemmy extension
|
|
|
|
pub(crate) sensitive: Option<bool>,
|
2022-11-25 14:01:58 +00:00
|
|
|
#[serde(deserialize_with = "deserialize_skip_error", default)]
|
2023-03-21 15:03:05 +00:00
|
|
|
pub(crate) attributed_to: Option<CollectionId<ApubCommunityModerators>>,
|
2022-04-28 20:32:32 +00:00
|
|
|
// lemmy extension
|
|
|
|
pub(crate) posting_restricted_to_mods: Option<bool>,
|
2023-03-21 15:03:05 +00:00
|
|
|
pub(crate) outbox: CollectionId<ApubCommunityOutbox>,
|
2022-01-17 14:40:47 +00:00
|
|
|
pub(crate) endpoints: Option<Endpoints>,
|
2023-03-21 15:03:05 +00:00
|
|
|
pub(crate) featured: Option<CollectionId<ApubCommunityFeatured>>,
|
2022-10-06 18:27:58 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub(crate) language: Vec<LanguageTag>,
|
2023-08-24 15:27:00 +00:00
|
|
|
pub(crate) published: Option<DateTime<Utc>>,
|
|
|
|
pub(crate) updated: Option<DateTime<Utc>>,
|
2021-10-28 21:17:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Group {
|
2021-11-06 17:35:14 +00:00
|
|
|
pub(crate) async fn verify(
|
|
|
|
&self,
|
2021-10-28 21:17:59 +00:00
|
|
|
expected_domain: &Url,
|
2021-11-06 17:35:14 +00:00
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError> {
|
2023-07-05 15:08:02 +00:00
|
|
|
check_apub_id_valid_with_strictness(self.id.inner(), true, context).await?;
|
2021-11-06 12:37:55 +00:00
|
|
|
verify_domains_match(expected_domain, self.id.inner())?;
|
2021-10-28 21:17:59 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
let local_site_data = local_site_data_cached(&mut context.pool()).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
let slur_regex = &local_site_opt_to_slur_regex(&local_site_data.local_site);
|
|
|
|
|
2021-11-06 17:35:14 +00:00
|
|
|
check_slurs(&self.preferred_username, slur_regex)?;
|
2022-03-24 16:33:42 +00:00
|
|
|
check_slurs_opt(&self.name, slur_regex)?;
|
2022-05-06 23:53:33 +00:00
|
|
|
let description = read_from_string_or_source_opt(&self.summary, &None, &self.source);
|
2021-10-28 21:17:59 +00:00
|
|
|
check_slurs_opt(&description, slur_regex)?;
|
2021-11-06 17:35:14 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-10-28 21:17:59 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
pub(crate) fn into_insert_form(self, instance_id: InstanceId) -> CommunityInsertForm {
|
2023-07-26 18:01:15 +00:00
|
|
|
let name = sanitize_html(&self.preferred_username);
|
|
|
|
let title = sanitize_html(&self.name.unwrap_or(self.preferred_username));
|
|
|
|
let description = read_from_string_or_source_opt(&self.summary, &None, &self.source);
|
|
|
|
let description = sanitize_html_opt(&description);
|
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
CommunityInsertForm {
|
2023-07-26 18:01:15 +00:00
|
|
|
name,
|
|
|
|
title,
|
|
|
|
description,
|
2022-10-27 09:24:07 +00:00
|
|
|
removed: None,
|
2023-08-24 15:27:00 +00:00
|
|
|
published: self.published,
|
|
|
|
updated: self.updated,
|
2022-11-03 13:39:30 +00:00
|
|
|
deleted: Some(false),
|
2022-10-27 09:24:07 +00:00
|
|
|
nsfw: Some(self.sensitive.unwrap_or(false)),
|
|
|
|
actor_id: Some(self.id.into()),
|
|
|
|
local: Some(false),
|
|
|
|
private_key: None,
|
2023-06-15 11:39:58 +00:00
|
|
|
hidden: None,
|
2022-10-27 09:24:07 +00:00
|
|
|
public_key: self.public_key.public_key_pem,
|
|
|
|
last_refreshed_at: Some(naive_now()),
|
|
|
|
icon: self.icon.map(|i| i.url.into()),
|
|
|
|
banner: self.image.map(|i| i.url.into()),
|
|
|
|
followers_url: Some(self.followers.into()),
|
|
|
|
inbox_url: Some(self.inbox.into()),
|
|
|
|
shared_inbox_url: self.endpoints.map(|e| e.shared_inbox.into()),
|
2023-06-07 19:18:17 +00:00
|
|
|
moderators_url: self.attributed_to.map(Into::into),
|
2022-10-27 09:24:07 +00:00
|
|
|
posting_restricted_to_mods: self.posting_restricted_to_mods,
|
|
|
|
instance_id,
|
2023-02-18 14:50:28 +00:00
|
|
|
featured_url: self.featured.map(Into::into),
|
2022-10-27 09:24:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn into_update_form(self) -> CommunityUpdateForm {
|
|
|
|
CommunityUpdateForm {
|
|
|
|
title: Some(self.name.unwrap_or(self.preferred_username)),
|
2022-07-29 13:04:21 +00:00
|
|
|
description: Some(read_from_string_or_source_opt(
|
|
|
|
&self.summary,
|
|
|
|
&None,
|
|
|
|
&self.source,
|
|
|
|
)),
|
2021-10-28 21:17:59 +00:00
|
|
|
removed: None,
|
2023-08-24 15:27:00 +00:00
|
|
|
published: self.published.map(Into::into),
|
|
|
|
updated: Some(self.updated.map(Into::into)),
|
2021-10-28 21:17:59 +00:00
|
|
|
deleted: None,
|
2021-11-06 12:37:55 +00:00
|
|
|
nsfw: Some(self.sensitive.unwrap_or(false)),
|
|
|
|
actor_id: Some(self.id.into()),
|
2023-06-14 12:34:45 +00:00
|
|
|
local: None,
|
2021-10-28 21:17:59 +00:00
|
|
|
private_key: None,
|
2023-06-15 11:39:58 +00:00
|
|
|
hidden: None,
|
2022-07-11 18:25:33 +00:00
|
|
|
public_key: Some(self.public_key.public_key_pem),
|
2021-10-28 21:17:59 +00:00
|
|
|
last_refreshed_at: Some(naive_now()),
|
2021-11-06 12:37:55 +00:00
|
|
|
icon: Some(self.icon.map(|i| i.url.into())),
|
|
|
|
banner: Some(self.image.map(|i| i.url.into())),
|
|
|
|
followers_url: Some(self.followers.into()),
|
|
|
|
inbox_url: Some(self.inbox.into()),
|
2022-01-17 14:40:47 +00:00
|
|
|
shared_inbox_url: Some(self.endpoints.map(|e| e.shared_inbox.into())),
|
2023-06-07 19:18:17 +00:00
|
|
|
moderators_url: self.attributed_to.map(Into::into),
|
2022-04-28 20:32:32 +00:00
|
|
|
posting_restricted_to_mods: self.posting_restricted_to_mods,
|
2023-02-18 14:50:28 +00:00
|
|
|
featured_url: self.featured.map(Into::into),
|
2021-11-08 12:11:24 +00:00
|
|
|
}
|
2021-10-28 21:17:59 +00:00
|
|
|
}
|
|
|
|
}
|