2020-10-28 16:14:18 +00:00
|
|
|
use crate::{activities::receive::verify_activity_domains_valid, inbox::is_addressed_to_public};
|
|
|
|
use activitystreams::{
|
|
|
|
activity::{ActorAndObjectRefExt, Delete, Remove, Undo},
|
|
|
|
base::{AnyBase, ExtendsExt},
|
|
|
|
};
|
|
|
|
use anyhow::Context;
|
2020-12-13 17:04:42 +00:00
|
|
|
use lemmy_db::{source::community::Community, views::community_view::CommunityView, ApubObject};
|
2020-10-21 17:37:50 +00:00
|
|
|
use lemmy_structs::{blocking, community::CommunityResponse};
|
2020-10-28 16:14:18 +00:00
|
|
|
use lemmy_utils::{location_info, LemmyError};
|
2020-10-21 17:37:50 +00:00
|
|
|
use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
|
2020-10-28 16:14:18 +00:00
|
|
|
use url::Url;
|
2020-10-21 17:37:50 +00:00
|
|
|
|
|
|
|
pub(crate) async fn receive_delete_community(
|
|
|
|
context: &LemmyContext,
|
|
|
|
community: Community,
|
2020-10-28 16:14:18 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2020-10-21 17:37:50 +00:00
|
|
|
let deleted_community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::update_deleted(conn, community.id, true)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let community_id = deleted_community.id;
|
|
|
|
let res = CommunityResponse {
|
2020-12-06 14:12:51 +00:00
|
|
|
community_view: blocking(context.pool(), move |conn| {
|
2020-10-21 17:37:50 +00:00
|
|
|
CommunityView::read(conn, community_id, None)
|
|
|
|
})
|
|
|
|
.await??,
|
|
|
|
};
|
|
|
|
|
2020-12-06 14:12:51 +00:00
|
|
|
let community_id = res.community_view.community.id;
|
2020-10-21 17:37:50 +00:00
|
|
|
context.chat_server().do_send(SendCommunityRoomMessage {
|
|
|
|
op: UserOperation::EditCommunity,
|
|
|
|
response: res,
|
|
|
|
community_id,
|
|
|
|
websocket_id: None,
|
|
|
|
});
|
|
|
|
|
2020-10-28 16:14:18 +00:00
|
|
|
Ok(())
|
2020-10-21 17:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn receive_remove_community(
|
|
|
|
context: &LemmyContext,
|
2020-10-28 16:14:18 +00:00
|
|
|
activity: AnyBase,
|
|
|
|
expected_domain: &Url,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let remove = Remove::from_any_base(activity)?.context(location_info!())?;
|
|
|
|
verify_activity_domains_valid(&remove, expected_domain, true)?;
|
|
|
|
is_addressed_to_public(&remove)?;
|
|
|
|
|
|
|
|
let community_uri = remove
|
|
|
|
.object()
|
|
|
|
.to_owned()
|
|
|
|
.single_xsd_any_uri()
|
|
|
|
.context(location_info!())?;
|
|
|
|
let community = blocking(context.pool(), move |conn| {
|
2020-12-08 17:38:48 +00:00
|
|
|
Community::read_from_apub_id(conn, community_uri.as_str())
|
2020-10-28 16:14:18 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-10-21 17:37:50 +00:00
|
|
|
let removed_community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::update_removed(conn, community.id, true)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let community_id = removed_community.id;
|
|
|
|
let res = CommunityResponse {
|
2020-12-06 14:12:51 +00:00
|
|
|
community_view: blocking(context.pool(), move |conn| {
|
2020-10-21 17:37:50 +00:00
|
|
|
CommunityView::read(conn, community_id, None)
|
|
|
|
})
|
|
|
|
.await??,
|
|
|
|
};
|
|
|
|
|
2020-12-06 14:12:51 +00:00
|
|
|
let community_id = res.community_view.community.id;
|
2020-10-21 17:37:50 +00:00
|
|
|
context.chat_server().do_send(SendCommunityRoomMessage {
|
|
|
|
op: UserOperation::EditCommunity,
|
|
|
|
response: res,
|
|
|
|
community_id,
|
|
|
|
websocket_id: None,
|
|
|
|
});
|
|
|
|
|
2020-10-28 16:14:18 +00:00
|
|
|
Ok(())
|
2020-10-21 17:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn receive_undo_delete_community(
|
|
|
|
context: &LemmyContext,
|
|
|
|
undo: Undo,
|
|
|
|
community: Community,
|
2020-10-28 16:14:18 +00:00
|
|
|
expected_domain: &Url,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
is_addressed_to_public(&undo)?;
|
|
|
|
let inner = undo.object().to_owned().one().context(location_info!())?;
|
|
|
|
let delete = Delete::from_any_base(inner)?.context(location_info!())?;
|
|
|
|
verify_activity_domains_valid(&delete, expected_domain, true)?;
|
|
|
|
is_addressed_to_public(&delete)?;
|
|
|
|
|
2020-10-21 17:37:50 +00:00
|
|
|
let deleted_community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::update_deleted(conn, community.id, false)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let community_id = deleted_community.id;
|
|
|
|
let res = CommunityResponse {
|
2020-12-06 14:12:51 +00:00
|
|
|
community_view: blocking(context.pool(), move |conn| {
|
2020-10-21 17:37:50 +00:00
|
|
|
CommunityView::read(conn, community_id, None)
|
|
|
|
})
|
|
|
|
.await??,
|
|
|
|
};
|
|
|
|
|
2020-12-06 14:12:51 +00:00
|
|
|
let community_id = res.community_view.community.id;
|
2020-10-21 17:37:50 +00:00
|
|
|
context.chat_server().do_send(SendCommunityRoomMessage {
|
|
|
|
op: UserOperation::EditCommunity,
|
|
|
|
response: res,
|
|
|
|
community_id,
|
|
|
|
websocket_id: None,
|
|
|
|
});
|
|
|
|
|
2020-10-28 16:14:18 +00:00
|
|
|
Ok(())
|
2020-10-21 17:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn receive_undo_remove_community(
|
|
|
|
context: &LemmyContext,
|
|
|
|
undo: Undo,
|
2020-10-28 16:14:18 +00:00
|
|
|
expected_domain: &Url,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
is_addressed_to_public(&undo)?;
|
|
|
|
|
|
|
|
let inner = undo.object().to_owned().one().context(location_info!())?;
|
|
|
|
let remove = Remove::from_any_base(inner)?.context(location_info!())?;
|
|
|
|
verify_activity_domains_valid(&remove, &expected_domain, true)?;
|
|
|
|
is_addressed_to_public(&remove)?;
|
|
|
|
|
|
|
|
let community_uri = remove
|
|
|
|
.object()
|
|
|
|
.to_owned()
|
|
|
|
.single_xsd_any_uri()
|
|
|
|
.context(location_info!())?;
|
|
|
|
let community = blocking(context.pool(), move |conn| {
|
2020-12-08 17:38:48 +00:00
|
|
|
Community::read_from_apub_id(conn, community_uri.as_str())
|
2020-10-28 16:14:18 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-10-21 17:37:50 +00:00
|
|
|
let removed_community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::update_removed(conn, community.id, false)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let community_id = removed_community.id;
|
|
|
|
let res = CommunityResponse {
|
2020-12-06 14:12:51 +00:00
|
|
|
community_view: blocking(context.pool(), move |conn| {
|
2020-10-21 17:37:50 +00:00
|
|
|
CommunityView::read(conn, community_id, None)
|
|
|
|
})
|
|
|
|
.await??,
|
|
|
|
};
|
|
|
|
|
2020-12-06 14:12:51 +00:00
|
|
|
let community_id = res.community_view.community.id;
|
2020-10-21 17:37:50 +00:00
|
|
|
|
|
|
|
context.chat_server().do_send(SendCommunityRoomMessage {
|
|
|
|
op: UserOperation::EditCommunity,
|
|
|
|
response: res,
|
|
|
|
community_id,
|
|
|
|
websocket_id: None,
|
|
|
|
});
|
|
|
|
|
2020-10-28 16:14:18 +00:00
|
|
|
Ok(())
|
2020-10-21 17:37:50 +00:00
|
|
|
}
|