2022-02-07 19:23:12 +00:00
|
|
|
use crate::{
|
2023-08-02 16:52:41 +00:00
|
|
|
objects::{community::ApubCommunity, instance::ApubSite},
|
2022-11-28 14:29:33 +00:00
|
|
|
protocol::{
|
|
|
|
activities::block::{block_user::BlockUser, undo_block_user::UndoBlockUser},
|
|
|
|
objects::{group::Group, instance::Instance},
|
|
|
|
},
|
2022-02-07 19:23:12 +00:00
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::{
|
|
|
|
config::Data,
|
|
|
|
fetch::object_id::ObjectId,
|
|
|
|
traits::{Actor, Object},
|
|
|
|
};
|
2023-08-24 15:27:00 +00:00
|
|
|
use chrono::{DateTime, Utc};
|
2023-10-17 17:25:35 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
community::BanFromCommunity,
|
|
|
|
context::LemmyContext,
|
|
|
|
utils::check_expire_time,
|
|
|
|
};
|
2022-11-28 14:29:33 +00:00
|
|
|
use lemmy_db_schema::{
|
2023-08-02 16:52:41 +00:00
|
|
|
newtypes::CommunityId,
|
2022-11-28 14:29:33 +00:00
|
|
|
source::{community::Community, person::Person, site::Site},
|
|
|
|
traits::Crud,
|
|
|
|
utils::DbPool,
|
|
|
|
};
|
|
|
|
use lemmy_db_views::structs::SiteView;
|
2023-10-17 17:25:35 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyResult};
|
2022-02-07 19:23:12 +00:00
|
|
|
use serde::Deserialize;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
pub mod block_user;
|
|
|
|
pub mod undo_block_user;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum SiteOrCommunity {
|
|
|
|
Site(ApubSite),
|
|
|
|
Community(ApubCommunity),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
pub enum InstanceOrGroup {
|
|
|
|
Instance(Instance),
|
|
|
|
Group(Group),
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl Object for SiteOrCommunity {
|
2022-02-07 19:23:12 +00:00
|
|
|
type DataType = LemmyContext;
|
2023-03-21 15:03:05 +00:00
|
|
|
type Kind = InstanceOrGroup;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
2022-02-07 19:23:12 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2023-08-24 15:27:00 +00:00
|
|
|
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {
|
2022-02-07 19:23:12 +00:00
|
|
|
Some(match self {
|
|
|
|
SiteOrCommunity::Site(i) => i.last_refreshed_at,
|
|
|
|
SiteOrCommunity::Community(c) => c.last_refreshed_at,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2024-04-10 14:14:11 +00:00
|
|
|
async fn read_from_id(object_id: Url, data: &Data<Self::DataType>) -> LemmyResult<Option<Self>>
|
2022-02-07 19:23:12 +00:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2023-03-21 15:03:05 +00:00
|
|
|
let site = ApubSite::read_from_id(object_id.clone(), data).await?;
|
2022-02-07 19:23:12 +00:00
|
|
|
Ok(match site {
|
|
|
|
Some(o) => Some(SiteOrCommunity::Site(o)),
|
2023-03-21 15:03:05 +00:00
|
|
|
None => ApubCommunity::read_from_id(object_id, data)
|
2022-02-07 19:23:12 +00:00
|
|
|
.await?
|
|
|
|
.map(SiteOrCommunity::Community),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-10 14:14:11 +00:00
|
|
|
async fn delete(self, _data: &Data<Self::DataType>) -> LemmyResult<()> {
|
2022-02-07 19:23:12 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
2024-04-10 14:14:11 +00:00
|
|
|
async fn into_json(self, _data: &Data<Self::DataType>) -> LemmyResult<Self::Kind> {
|
2022-02-07 19:23:12 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
|
|
|
async fn verify(
|
2023-03-21 15:03:05 +00:00
|
|
|
apub: &Self::Kind,
|
2022-02-07 19:23:12 +00:00
|
|
|
expected_domain: &Url,
|
2023-03-21 15:03:05 +00:00
|
|
|
data: &Data<Self::DataType>,
|
2024-04-10 14:14:11 +00:00
|
|
|
) -> LemmyResult<()> {
|
2022-02-07 19:23:12 +00:00
|
|
|
match apub {
|
2023-03-21 15:03:05 +00:00
|
|
|
InstanceOrGroup::Instance(i) => ApubSite::verify(i, expected_domain, data).await,
|
|
|
|
InstanceOrGroup::Group(g) => ApubCommunity::verify(g, expected_domain, data).await,
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2024-04-10 14:14:11 +00:00
|
|
|
async fn from_json(apub: Self::Kind, data: &Data<Self::DataType>) -> LemmyResult<Self>
|
2022-02-07 19:23:12 +00:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
Ok(match apub {
|
2023-03-21 15:03:05 +00:00
|
|
|
InstanceOrGroup::Instance(p) => SiteOrCommunity::Site(ApubSite::from_json(p, data).await?),
|
2022-02-07 19:23:12 +00:00
|
|
|
InstanceOrGroup::Group(n) => {
|
2023-03-21 15:03:05 +00:00
|
|
|
SiteOrCommunity::Community(ApubCommunity::from_json(n, data).await?)
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SiteOrCommunity {
|
|
|
|
fn id(&self) -> ObjectId<SiteOrCommunity> {
|
|
|
|
match self {
|
2023-03-21 15:03:05 +00:00
|
|
|
SiteOrCommunity::Site(s) => ObjectId::from(s.actor_id.clone()),
|
|
|
|
SiteOrCommunity::Community(c) => ObjectId::from(c.actor_id.clone()),
|
2022-02-07 19:23:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-10 14:14:11 +00:00
|
|
|
async fn generate_cc(target: &SiteOrCommunity, pool: &mut DbPool<'_>) -> LemmyResult<Vec<Url>> {
|
2022-02-07 19:23:12 +00:00
|
|
|
Ok(match target {
|
2022-11-09 10:05:00 +00:00
|
|
|
SiteOrCommunity::Site(_) => Site::read_remote_sites(pool)
|
|
|
|
.await?
|
2022-02-07 19:23:12 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|s| s.actor_id.into())
|
|
|
|
.collect(),
|
2023-03-21 15:03:05 +00:00
|
|
|
SiteOrCommunity::Community(c) => vec![c.id()],
|
2022-02-07 19:23:12 +00:00
|
|
|
})
|
|
|
|
}
|
2022-11-28 14:29:33 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
pub(crate) async fn send_ban_from_site(
|
2024-01-25 14:24:09 +00:00
|
|
|
moderator: Person,
|
2023-08-02 16:52:41 +00:00
|
|
|
banned_user: Person,
|
2024-01-25 14:24:09 +00:00
|
|
|
reason: Option<String>,
|
|
|
|
remove_data: Option<bool>,
|
|
|
|
ban: bool,
|
|
|
|
expires: Option<i64>,
|
2023-08-02 16:52:41 +00:00
|
|
|
context: Data<LemmyContext>,
|
2024-04-10 14:14:11 +00:00
|
|
|
) -> LemmyResult<()> {
|
2023-08-02 16:52:41 +00:00
|
|
|
let site = SiteOrCommunity::Site(SiteView::read_local(&mut context.pool()).await?.site.into());
|
2024-01-25 14:24:09 +00:00
|
|
|
let expires = check_expire_time(expires)?;
|
2023-08-02 16:52:41 +00:00
|
|
|
|
|
|
|
// if the action affects a local user, federate to other instances
|
|
|
|
if banned_user.local {
|
2024-01-25 14:24:09 +00:00
|
|
|
if ban {
|
2022-11-28 14:29:33 +00:00
|
|
|
BlockUser::send(
|
2023-08-02 16:52:41 +00:00
|
|
|
&site,
|
|
|
|
&banned_user.into(),
|
2024-01-25 14:24:09 +00:00
|
|
|
&moderator.into(),
|
|
|
|
remove_data.unwrap_or(false),
|
|
|
|
reason.clone(),
|
2022-11-28 14:29:33 +00:00
|
|
|
expires,
|
2023-08-02 16:52:41 +00:00
|
|
|
&context,
|
2022-11-28 14:29:33 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
} else {
|
|
|
|
UndoBlockUser::send(
|
2023-08-02 16:52:41 +00:00
|
|
|
&site,
|
|
|
|
&banned_user.into(),
|
2024-01-25 14:24:09 +00:00
|
|
|
&moderator.into(),
|
|
|
|
reason.clone(),
|
2023-08-02 16:52:41 +00:00
|
|
|
&context,
|
2022-11-28 14:29:33 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
2023-08-02 16:52:41 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn send_ban_from_community(
|
|
|
|
mod_: Person,
|
|
|
|
community_id: CommunityId,
|
|
|
|
banned_person: Person,
|
|
|
|
data: BanFromCommunity,
|
|
|
|
context: Data<LemmyContext>,
|
|
|
|
) -> LemmyResult<()> {
|
|
|
|
let community: ApubCommunity = Community::read(&mut context.pool(), community_id)
|
|
|
|
.await?
|
|
|
|
.into();
|
2023-10-17 17:25:35 +00:00
|
|
|
let expires = check_expire_time(data.expires)?;
|
2023-08-02 16:52:41 +00:00
|
|
|
|
|
|
|
if data.ban {
|
|
|
|
BlockUser::send(
|
|
|
|
&SiteOrCommunity::Community(community),
|
|
|
|
&banned_person.into(),
|
|
|
|
&mod_.into(),
|
|
|
|
data.remove_data.unwrap_or(false),
|
|
|
|
data.reason.clone(),
|
|
|
|
expires,
|
|
|
|
&context,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
} else {
|
|
|
|
UndoBlockUser::send(
|
|
|
|
&SiteOrCommunity::Community(community),
|
|
|
|
&banned_person.into(),
|
|
|
|
&mod_.into(),
|
|
|
|
data.reason.clone(),
|
|
|
|
&context,
|
|
|
|
)
|
|
|
|
.await
|
2022-11-28 14:29:33 +00:00
|
|
|
}
|
|
|
|
}
|