2020-05-16 14:04:08 +00:00
|
|
|
use crate::{
|
2020-09-24 13:53:21 +00:00
|
|
|
check_community_ban,
|
2020-12-20 01:10:47 +00:00
|
|
|
check_downvotes_enabled,
|
2020-11-04 02:15:11 +00:00
|
|
|
collect_moderated_communities,
|
2021-03-10 22:33:55 +00:00
|
|
|
get_local_user_view_from_jwt,
|
|
|
|
get_local_user_view_from_jwt_opt,
|
2020-09-24 13:53:21 +00:00
|
|
|
is_mod_or_admin,
|
|
|
|
Perform,
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
2020-08-18 13:43:50 +00:00
|
|
|
use actix_web::web::Data;
|
2021-03-01 13:08:41 +00:00
|
|
|
use lemmy_api_structs::{blocking, post::*};
|
2021-02-04 16:34:58 +00:00
|
|
|
use lemmy_apub::{generate_apub_endpoint, ApubLikeableType, ApubObjectType, EndpointType};
|
2020-12-21 23:27:42 +00:00
|
|
|
use lemmy_db_queries::{
|
2020-12-21 13:38:34 +00:00
|
|
|
source::post::Post_,
|
2020-07-10 18:15:41 +00:00
|
|
|
Crud,
|
|
|
|
Likeable,
|
|
|
|
ListingType,
|
2020-11-04 02:15:11 +00:00
|
|
|
Reportable,
|
2020-07-10 18:15:41 +00:00
|
|
|
Saveable,
|
|
|
|
SortType,
|
|
|
|
};
|
2020-12-18 17:27:25 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
naive_now,
|
2020-12-21 13:38:34 +00:00
|
|
|
source::{
|
|
|
|
moderator::*,
|
|
|
|
post::*,
|
|
|
|
post_report::{PostReport, PostReportForm},
|
|
|
|
},
|
2020-12-18 17:27:25 +00:00
|
|
|
};
|
2020-12-21 16:30:34 +00:00
|
|
|
use lemmy_db_views::{
|
|
|
|
comment_view::CommentQueryBuilder,
|
|
|
|
post_report_view::{PostReportQueryBuilder, PostReportView},
|
|
|
|
post_view::{PostQueryBuilder, PostView},
|
|
|
|
};
|
2020-12-24 00:09:41 +00:00
|
|
|
use lemmy_db_views_actor::{
|
|
|
|
community_moderator_view::CommunityModeratorView,
|
|
|
|
community_view::CommunityView,
|
|
|
|
};
|
2020-09-01 14:25:34 +00:00
|
|
|
use lemmy_utils::{
|
2020-09-24 17:43:42 +00:00
|
|
|
request::fetch_iframely_and_pictrs_data,
|
2020-09-14 15:29:50 +00:00
|
|
|
utils::{check_slurs, check_slurs_opt, is_valid_post_title},
|
2021-02-22 18:04:32 +00:00
|
|
|
ApiError,
|
2020-09-01 14:25:34 +00:00
|
|
|
ConnectionId,
|
|
|
|
LemmyError,
|
|
|
|
};
|
2020-09-24 13:53:21 +00:00
|
|
|
use lemmy_websocket::{
|
2021-01-18 21:57:31 +00:00
|
|
|
messages::{GetPostUsersOnline, SendModRoomMessage, SendPost, SendUserRoomMessage},
|
2020-09-24 13:53:21 +00:00
|
|
|
LemmyContext,
|
|
|
|
UserOperation,
|
|
|
|
};
|
2020-05-16 14:04:08 +00:00
|
|
|
use std::str::FromStr;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for CreatePost {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = PostResponse;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<PostResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &CreatePost = &self;
|
2021-03-10 22:33:55 +00:00
|
|
|
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-08-04 14:39:55 +00:00
|
|
|
check_slurs(&data.name)?;
|
|
|
|
check_slurs_opt(&data.body)?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-11 01:15:53 +00:00
|
|
|
if !is_valid_post_title(&data.name) {
|
2021-02-22 18:04:32 +00:00
|
|
|
return Err(ApiError::err("invalid_post_title").into());
|
2020-07-11 01:15:53 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?;
|
2020-07-17 22:46:59 +00:00
|
|
|
|
2020-06-11 02:47:06 +00:00
|
|
|
// Fetch Iframely and pictrs cached image
|
2021-03-02 12:41:48 +00:00
|
|
|
let data_url = data.url.as_ref();
|
2020-06-11 02:47:06 +00:00
|
|
|
let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
|
2021-03-02 12:41:48 +00:00
|
|
|
fetch_iframely_and_pictrs_data(context.client(), data_url).await;
|
2020-03-07 23:31:13 +00:00
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
let post_form = PostForm {
|
2020-07-11 01:15:53 +00:00
|
|
|
name: data.name.trim().to_owned(),
|
2021-03-02 12:41:48 +00:00
|
|
|
url: data_url.map(|u| u.to_owned().into()),
|
2019-05-05 05:20:38 +00:00
|
|
|
body: data.body.to_owned(),
|
|
|
|
community_id: data.community_id,
|
2021-03-10 22:33:55 +00:00
|
|
|
creator_id: local_user_view.person.id,
|
2019-08-14 02:52:43 +00:00
|
|
|
nsfw: data.nsfw,
|
2020-03-07 23:31:13 +00:00
|
|
|
embed_title: iframely_title,
|
|
|
|
embed_description: iframely_description,
|
|
|
|
embed_html: iframely_html,
|
2021-03-02 12:41:48 +00:00
|
|
|
thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
|
2021-03-20 20:59:07 +00:00
|
|
|
..PostForm::default()
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let inserted_post =
|
|
|
|
match blocking(context.pool(), move |conn| Post::create(conn, &post_form)).await? {
|
|
|
|
Ok(post) => post,
|
|
|
|
Err(e) => {
|
|
|
|
let err_type = if e.to_string() == "value too long for type character varying(200)" {
|
|
|
|
"post_title_too_long"
|
|
|
|
} else {
|
|
|
|
"couldnt_create_post"
|
|
|
|
};
|
|
|
|
|
2021-02-22 18:04:32 +00:00
|
|
|
return Err(ApiError::err(err_type).into());
|
2020-08-18 13:43:50 +00:00
|
|
|
}
|
|
|
|
};
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let inserted_post_id = inserted_post.id;
|
2021-02-04 16:34:58 +00:00
|
|
|
let updated_post = match blocking(context.pool(), move |conn| -> Result<Post, LemmyError> {
|
|
|
|
let apub_id = generate_apub_endpoint(EndpointType::Post, &inserted_post_id.to_string())?;
|
|
|
|
Ok(Post::update_ap_id(conn, inserted_post_id, apub_id)?)
|
2020-07-10 18:15:41 +00:00
|
|
|
})
|
|
|
|
.await?
|
|
|
|
{
|
|
|
|
Ok(post) => post,
|
2021-02-22 18:04:32 +00:00
|
|
|
Err(_e) => return Err(ApiError::err("couldnt_create_post").into()),
|
2020-07-10 18:15:41 +00:00
|
|
|
};
|
2020-04-04 00:04:57 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
updated_post
|
|
|
|
.send_create(&local_user_view.person, context)
|
|
|
|
.await?;
|
2020-04-09 19:04:31 +00:00
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
// They like their own post by default
|
|
|
|
let like_form = PostLikeForm {
|
|
|
|
post_id: inserted_post.id,
|
2021-03-10 22:33:55 +00:00
|
|
|
person_id: local_user_view.person.id,
|
2019-09-07 15:35:05 +00:00
|
|
|
score: 1,
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let like = move |conn: &'_ _| PostLike::like(conn, &like_form);
|
2020-08-18 13:43:50 +00:00
|
|
|
if blocking(context.pool(), like).await?.is_err() {
|
2021-02-22 18:04:32 +00:00
|
|
|
return Err(ApiError::err("couldnt_like_post").into());
|
2020-07-01 12:54:29 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
updated_post
|
|
|
|
.send_like(&local_user_view.person, context)
|
|
|
|
.await?;
|
2020-04-28 02:46:09 +00:00
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
// Refetch the view
|
2020-07-01 12:54:29 +00:00
|
|
|
let inserted_post_id = inserted_post.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_view = match blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
PostView::read(conn, inserted_post_id, Some(local_user_view.person.id))
|
2020-07-01 12:54:29 +00:00
|
|
|
})
|
|
|
|
.await?
|
|
|
|
{
|
2019-05-05 05:20:38 +00:00
|
|
|
Ok(post) => post,
|
2021-02-22 18:04:32 +00:00
|
|
|
Err(_e) => return Err(ApiError::err("couldnt_find_post").into()),
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-12-11 15:27:33 +00:00
|
|
|
let res = PostResponse { post_view };
|
2020-04-19 22:08:25 +00:00
|
|
|
|
2020-08-24 11:58:24 +00:00
|
|
|
context.chat_server().do_send(SendPost {
|
|
|
|
op: UserOperation::CreatePost,
|
|
|
|
post: res.clone(),
|
|
|
|
websocket_id,
|
|
|
|
});
|
2020-04-19 22:08:25 +00:00
|
|
|
|
|
|
|
Ok(res)
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for GetPost {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = GetPostResponse;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-09-15 19:26:47 +00:00
|
|
|
_websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<GetPostResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &GetPost = &self;
|
2021-03-10 22:33:55 +00:00
|
|
|
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
|
|
|
|
let person_id = local_user_view.map(|u| u.person.id);
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let id = data.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_view = match blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
PostView::read(conn, id, person_id)
|
2020-08-18 13:43:50 +00:00
|
|
|
})
|
|
|
|
.await?
|
|
|
|
{
|
2019-05-05 05:20:38 +00:00
|
|
|
Ok(post) => post,
|
2021-02-22 18:04:32 +00:00
|
|
|
Err(_e) => return Err(ApiError::err("couldnt_find_post").into()),
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let id = data.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let comments = blocking(context.pool(), move |conn| {
|
2020-12-16 18:59:43 +00:00
|
|
|
CommentQueryBuilder::create(conn)
|
2021-03-10 22:33:55 +00:00
|
|
|
.my_person_id(person_id)
|
2020-12-16 18:59:43 +00:00
|
|
|
.post_id(id)
|
2020-07-01 12:54:29 +00:00
|
|
|
.limit(9999)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-12-11 15:27:33 +00:00
|
|
|
let community_id = post_view.community.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let moderators = blocking(context.pool(), move |conn| {
|
2020-07-01 12:54:29 +00:00
|
|
|
CommunityModeratorView::for_community(conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-12-23 21:56:20 +00:00
|
|
|
// Necessary for the sidebar
|
|
|
|
let community_view = match blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
CommunityView::read(conn, community_id, person_id)
|
2020-12-23 21:56:20 +00:00
|
|
|
})
|
|
|
|
.await?
|
|
|
|
{
|
|
|
|
Ok(community) => community,
|
2021-02-22 18:04:32 +00:00
|
|
|
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
|
2020-12-23 21:56:20 +00:00
|
|
|
};
|
|
|
|
|
2020-08-24 11:58:24 +00:00
|
|
|
let online = context
|
|
|
|
.chat_server()
|
|
|
|
.send(GetPostUsersOnline { post_id: data.id })
|
|
|
|
.await
|
|
|
|
.unwrap_or(1);
|
2020-04-19 22:08:25 +00:00
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
// Return the jwt
|
2019-09-07 15:35:05 +00:00
|
|
|
Ok(GetPostResponse {
|
2020-12-11 15:27:33 +00:00
|
|
|
post_view,
|
2020-12-23 21:56:20 +00:00
|
|
|
community_view,
|
2019-12-09 19:08:19 +00:00
|
|
|
comments,
|
|
|
|
moderators,
|
2020-04-19 22:08:25 +00:00
|
|
|
online,
|
2019-09-07 15:35:05 +00:00
|
|
|
})
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for GetPosts {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = GetPostsResponse;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-09-15 19:26:47 +00:00
|
|
|
_websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<GetPostsResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &GetPosts = &self;
|
2021-03-10 22:33:55 +00:00
|
|
|
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
|
2019-09-07 15:35:05 +00:00
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
let person_id = match &local_user_view {
|
|
|
|
Some(uv) => Some(uv.person.id),
|
2019-09-07 15:35:05 +00:00
|
|
|
None => None,
|
2019-08-14 02:52:43 +00:00
|
|
|
};
|
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
let show_nsfw = match &local_user_view {
|
|
|
|
Some(uv) => uv.local_user.show_nsfw,
|
2019-09-07 15:35:05 +00:00
|
|
|
None => false,
|
2019-08-14 02:52:43 +00:00
|
|
|
};
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2019-10-21 04:21:54 +00:00
|
|
|
let type_ = ListingType::from_str(&data.type_)?;
|
2019-05-05 05:20:38 +00:00
|
|
|
let sort = SortType::from_str(&data.sort)?;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let page = data.page;
|
|
|
|
let limit = data.limit;
|
|
|
|
let community_id = data.community_id;
|
2020-07-13 13:50:13 +00:00
|
|
|
let community_name = data.community_name.to_owned();
|
2020-08-18 13:43:50 +00:00
|
|
|
let posts = match blocking(context.pool(), move |conn| {
|
2020-12-16 18:59:43 +00:00
|
|
|
PostQueryBuilder::create(conn)
|
2020-11-26 17:26:31 +00:00
|
|
|
.listing_type(&type_)
|
2020-07-01 12:54:29 +00:00
|
|
|
.sort(&sort)
|
|
|
|
.show_nsfw(show_nsfw)
|
2020-12-16 18:59:43 +00:00
|
|
|
.community_id(community_id)
|
|
|
|
.community_name(community_name)
|
2021-03-10 22:33:55 +00:00
|
|
|
.my_person_id(person_id)
|
2020-07-01 12:54:29 +00:00
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await?
|
2019-12-07 12:03:03 +00:00
|
|
|
{
|
2019-05-05 05:20:38 +00:00
|
|
|
Ok(posts) => posts,
|
2021-02-22 18:04:32 +00:00
|
|
|
Err(_e) => return Err(ApiError::err("couldnt_get_posts").into()),
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-01-16 14:39:08 +00:00
|
|
|
Ok(GetPostsResponse { posts })
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for CreatePostLike {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = PostResponse;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<PostResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &CreatePostLike = &self;
|
2021-03-10 22:33:55 +00:00
|
|
|
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2019-12-11 20:21:47 +00:00
|
|
|
// Don't do a downvote if site has downvotes disabled
|
2020-12-20 01:10:47 +00:00
|
|
|
check_downvotes_enabled(data.score, context.pool()).await?;
|
2019-12-11 20:21:47 +00:00
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
// Check for a community ban
|
2020-07-01 12:54:29 +00:00
|
|
|
let post_id = data.post_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
check_community_ban(local_user_view.person.id, post.community_id, context.pool()).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
let like_form = PostLikeForm {
|
|
|
|
post_id: data.post_id,
|
2021-03-10 22:33:55 +00:00
|
|
|
person_id: local_user_view.person.id,
|
2019-09-07 15:35:05 +00:00
|
|
|
score: data.score,
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Remove any likes first
|
2021-03-10 22:33:55 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
PostLike::remove(conn, person_id, post_id)
|
2020-08-18 13:43:50 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
// Only add the like if the score isnt 0
|
2020-01-02 11:30:00 +00:00
|
|
|
let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
|
2019-05-15 16:46:39 +00:00
|
|
|
if do_add {
|
2020-07-01 12:54:29 +00:00
|
|
|
let like_form2 = like_form.clone();
|
|
|
|
let like = move |conn: &'_ _| PostLike::like(conn, &like_form2);
|
2020-08-18 13:43:50 +00:00
|
|
|
if blocking(context.pool(), like).await?.is_err() {
|
2021-02-22 18:04:32 +00:00
|
|
|
return Err(ApiError::err("couldnt_like_post").into());
|
2020-07-01 12:54:29 +00:00
|
|
|
}
|
2020-04-28 02:46:09 +00:00
|
|
|
|
|
|
|
if like_form.score == 1 {
|
2021-03-10 22:33:55 +00:00
|
|
|
post.send_like(&local_user_view.person, context).await?;
|
2020-04-28 02:46:09 +00:00
|
|
|
} else if like_form.score == -1 {
|
2021-03-10 22:33:55 +00:00
|
|
|
post.send_dislike(&local_user_view.person, context).await?;
|
2020-04-28 02:46:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-03-11 04:43:11 +00:00
|
|
|
post
|
|
|
|
.send_undo_like(&local_user_view.person, context)
|
|
|
|
.await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let post_id = data.post_id;
|
2021-03-10 22:33:55 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_view = match blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
PostView::read(conn, post_id, Some(person_id))
|
2020-07-01 12:54:29 +00:00
|
|
|
})
|
|
|
|
.await?
|
|
|
|
{
|
2019-05-05 05:20:38 +00:00
|
|
|
Ok(post) => post,
|
2021-02-22 18:04:32 +00:00
|
|
|
Err(_e) => return Err(ApiError::err("couldnt_find_post").into()),
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-12-11 15:27:33 +00:00
|
|
|
let res = PostResponse { post_view };
|
2020-04-19 22:08:25 +00:00
|
|
|
|
2020-08-24 11:58:24 +00:00
|
|
|
context.chat_server().do_send(SendPost {
|
|
|
|
op: UserOperation::CreatePostLike,
|
|
|
|
post: res.clone(),
|
|
|
|
websocket_id,
|
|
|
|
});
|
2020-04-19 22:08:25 +00:00
|
|
|
|
|
|
|
Ok(res)
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for EditPost {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = PostResponse;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<PostResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &EditPost = &self;
|
2021-03-10 22:33:55 +00:00
|
|
|
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
|
2020-02-03 03:51:54 +00:00
|
|
|
|
2020-08-04 14:39:55 +00:00
|
|
|
check_slurs(&data.name)?;
|
|
|
|
check_slurs_opt(&data.body)?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-11 01:15:53 +00:00
|
|
|
if !is_valid_post_title(&data.name) {
|
2021-02-22 18:04:32 +00:00
|
|
|
return Err(ApiError::err("invalid_post_title").into());
|
2020-07-11 01:15:53 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 21:57:31 +00:00
|
|
|
let post_id = data.post_id;
|
|
|
|
let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
check_community_ban(
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_post.community_id,
|
|
|
|
context.pool(),
|
|
|
|
)
|
|
|
|
.await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-21 03:46:36 +00:00
|
|
|
// Verify that only the creator can edit
|
2021-03-10 22:33:55 +00:00
|
|
|
if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
|
2021-02-22 18:04:32 +00:00
|
|
|
return Err(ApiError::err("no_post_edit_allowed").into());
|
2020-07-21 03:46:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 02:47:06 +00:00
|
|
|
// Fetch Iframely and Pictrs cached image
|
2021-03-02 12:41:48 +00:00
|
|
|
let data_url = data.url.as_ref();
|
2020-06-11 02:47:06 +00:00
|
|
|
let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
|
2021-03-02 12:41:48 +00:00
|
|
|
fetch_iframely_and_pictrs_data(context.client(), data_url).await;
|
2020-03-07 23:31:13 +00:00
|
|
|
|
2020-07-21 03:46:36 +00:00
|
|
|
let post_form = PostForm {
|
2021-03-20 20:59:07 +00:00
|
|
|
creator_id: orig_post.creator_id.to_owned(),
|
|
|
|
community_id: orig_post.community_id,
|
2020-07-21 03:46:36 +00:00
|
|
|
name: data.name.trim().to_owned(),
|
2021-03-02 12:41:48 +00:00
|
|
|
url: data_url.map(|u| u.to_owned().into()),
|
2020-07-21 03:46:36 +00:00
|
|
|
body: data.body.to_owned(),
|
|
|
|
nsfw: data.nsfw,
|
|
|
|
updated: Some(naive_now()),
|
|
|
|
embed_title: iframely_title,
|
|
|
|
embed_description: iframely_description,
|
|
|
|
embed_html: iframely_html,
|
2021-03-02 12:41:48 +00:00
|
|
|
thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
|
2021-03-20 20:59:07 +00:00
|
|
|
..PostForm::default()
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2021-01-18 21:57:31 +00:00
|
|
|
let post_id = data.post_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let res = blocking(context.pool(), move |conn| {
|
2021-01-18 21:57:31 +00:00
|
|
|
Post::update(conn, post_id, &post_form)
|
2020-08-18 13:43:50 +00:00
|
|
|
})
|
|
|
|
.await?;
|
2020-07-01 12:54:29 +00:00
|
|
|
let updated_post: Post = match res {
|
2019-05-05 05:20:38 +00:00
|
|
|
Ok(post) => post,
|
2020-02-06 18:10:43 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_type = if e.to_string() == "value too long for type character varying(200)" {
|
|
|
|
"post_title_too_long"
|
|
|
|
} else {
|
|
|
|
"couldnt_update_post"
|
|
|
|
};
|
|
|
|
|
2021-02-22 18:04:32 +00:00
|
|
|
return Err(ApiError::err(err_type).into());
|
2020-02-06 18:10:43 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-07-21 03:46:36 +00:00
|
|
|
// Send apub update
|
2021-03-11 04:43:11 +00:00
|
|
|
updated_post
|
|
|
|
.send_update(&local_user_view.person, context)
|
|
|
|
.await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2021-01-18 21:57:31 +00:00
|
|
|
let post_id = data.post_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
PostView::read(conn, post_id, Some(local_user_view.person.id))
|
2020-07-21 03:46:36 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-12-11 15:27:33 +00:00
|
|
|
let res = PostResponse { post_view };
|
2020-07-21 03:46:36 +00:00
|
|
|
|
2020-08-24 11:58:24 +00:00
|
|
|
context.chat_server().do_send(SendPost {
|
|
|
|
op: UserOperation::EditPost,
|
|
|
|
post: res.clone(),
|
|
|
|
websocket_id,
|
|
|
|
});
|
2019-09-09 06:14:13 +00:00
|
|
|
|
2020-07-21 03:46:36 +00:00
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for DeletePost {
|
2020-07-21 03:46:36 +00:00
|
|
|
type Response = PostResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2020-07-21 03:46:36 +00:00
|
|
|
) -> Result<PostResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &DeletePost = &self;
|
2021-03-10 22:33:55 +00:00
|
|
|
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
2021-01-18 21:57:31 +00:00
|
|
|
let post_id = data.post_id;
|
|
|
|
let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
check_community_ban(
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_post.community_id,
|
|
|
|
context.pool(),
|
|
|
|
)
|
|
|
|
.await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Verify that only the creator can delete
|
2021-03-10 22:33:55 +00:00
|
|
|
if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
|
2021-02-22 18:04:32 +00:00
|
|
|
return Err(ApiError::err("no_post_edit_allowed").into());
|
2020-07-21 03:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the post
|
2021-01-18 21:57:31 +00:00
|
|
|
let post_id = data.post_id;
|
2020-07-21 03:46:36 +00:00
|
|
|
let deleted = data.deleted;
|
2020-08-18 13:43:50 +00:00
|
|
|
let updated_post = blocking(context.pool(), move |conn| {
|
2021-01-18 21:57:31 +00:00
|
|
|
Post::update_deleted(conn, post_id, deleted)
|
2020-07-21 03:46:36 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// apub updates
|
|
|
|
if deleted {
|
2021-03-11 04:43:11 +00:00
|
|
|
updated_post
|
|
|
|
.send_delete(&local_user_view.person, context)
|
|
|
|
.await?;
|
2020-05-01 14:07:38 +00:00
|
|
|
} else {
|
2021-03-11 04:43:11 +00:00
|
|
|
updated_post
|
|
|
|
.send_undo_delete(&local_user_view.person, context)
|
|
|
|
.await?;
|
2020-04-29 14:51:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 03:46:36 +00:00
|
|
|
// Refetch the post
|
2021-01-18 21:57:31 +00:00
|
|
|
let post_id = data.post_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
PostView::read(conn, post_id, Some(local_user_view.person.id))
|
2020-07-01 12:54:29 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-12-11 15:27:33 +00:00
|
|
|
let res = PostResponse { post_view };
|
2020-04-19 22:08:25 +00:00
|
|
|
|
2020-08-24 11:58:24 +00:00
|
|
|
context.chat_server().do_send(SendPost {
|
|
|
|
op: UserOperation::DeletePost,
|
|
|
|
post: res.clone(),
|
|
|
|
websocket_id,
|
|
|
|
});
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for RemovePost {
|
2020-07-21 03:46:36 +00:00
|
|
|
type Response = PostResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2020-07-21 03:46:36 +00:00
|
|
|
) -> Result<PostResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &RemovePost = &self;
|
2021-03-10 22:33:55 +00:00
|
|
|
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
2021-01-18 21:57:31 +00:00
|
|
|
let post_id = data.post_id;
|
|
|
|
let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
check_community_ban(
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_post.community_id,
|
|
|
|
context.pool(),
|
|
|
|
)
|
|
|
|
.await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Verify that only the mods can remove
|
2021-03-11 04:43:11 +00:00
|
|
|
is_mod_or_admin(
|
|
|
|
context.pool(),
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_post.community_id,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Update the post
|
2021-01-18 21:57:31 +00:00
|
|
|
let post_id = data.post_id;
|
2020-07-21 03:46:36 +00:00
|
|
|
let removed = data.removed;
|
2020-08-18 13:43:50 +00:00
|
|
|
let updated_post = blocking(context.pool(), move |conn| {
|
2021-01-18 21:57:31 +00:00
|
|
|
Post::update_removed(conn, post_id, removed)
|
2020-07-21 03:46:36 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let form = ModRemovePostForm {
|
2021-03-10 22:33:55 +00:00
|
|
|
mod_person_id: local_user_view.person.id,
|
2021-01-18 21:57:31 +00:00
|
|
|
post_id: data.post_id,
|
2020-07-21 03:46:36 +00:00
|
|
|
removed: Some(removed),
|
|
|
|
reason: data.reason.to_owned(),
|
|
|
|
};
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
ModRemovePost::create(conn, &form)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// apub updates
|
|
|
|
if removed {
|
2021-03-11 04:43:11 +00:00
|
|
|
updated_post
|
|
|
|
.send_remove(&local_user_view.person, context)
|
|
|
|
.await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
} else {
|
2021-03-11 04:43:11 +00:00
|
|
|
updated_post
|
|
|
|
.send_undo_remove(&local_user_view.person, context)
|
|
|
|
.await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Refetch the post
|
2021-01-18 21:57:31 +00:00
|
|
|
let post_id = data.post_id;
|
2021-03-10 22:33:55 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
PostView::read(conn, post_id, Some(person_id))
|
2020-07-21 03:46:36 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-12-11 15:27:33 +00:00
|
|
|
let res = PostResponse { post_view };
|
2020-07-21 03:46:36 +00:00
|
|
|
|
2020-08-24 11:58:24 +00:00
|
|
|
context.chat_server().do_send(SendPost {
|
|
|
|
op: UserOperation::RemovePost,
|
|
|
|
post: res.clone(),
|
|
|
|
websocket_id,
|
|
|
|
});
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for LockPost {
|
2020-07-21 03:46:36 +00:00
|
|
|
type Response = PostResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2020-07-21 03:46:36 +00:00
|
|
|
) -> Result<PostResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &LockPost = &self;
|
2021-03-10 22:33:55 +00:00
|
|
|
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
2021-01-18 21:57:31 +00:00
|
|
|
let post_id = data.post_id;
|
|
|
|
let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
check_community_ban(
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_post.community_id,
|
|
|
|
context.pool(),
|
|
|
|
)
|
|
|
|
.await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Verify that only the mods can lock
|
2021-03-11 04:43:11 +00:00
|
|
|
is_mod_or_admin(
|
|
|
|
context.pool(),
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_post.community_id,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Update the post
|
2021-01-18 21:57:31 +00:00
|
|
|
let post_id = data.post_id;
|
2020-07-21 03:46:36 +00:00
|
|
|
let locked = data.locked;
|
2020-08-18 13:43:50 +00:00
|
|
|
let updated_post = blocking(context.pool(), move |conn| {
|
2021-01-18 21:57:31 +00:00
|
|
|
Post::update_locked(conn, post_id, locked)
|
2020-08-18 13:43:50 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let form = ModLockPostForm {
|
2021-03-10 22:33:55 +00:00
|
|
|
mod_person_id: local_user_view.person.id,
|
2021-01-18 21:57:31 +00:00
|
|
|
post_id: data.post_id,
|
2020-07-21 03:46:36 +00:00
|
|
|
locked: Some(locked),
|
|
|
|
};
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| ModLockPost::create(conn, &form)).await??;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// apub updates
|
2021-03-11 04:43:11 +00:00
|
|
|
updated_post
|
|
|
|
.send_update(&local_user_view.person, context)
|
|
|
|
.await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Refetch the post
|
2021-01-18 21:57:31 +00:00
|
|
|
let post_id = data.post_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
PostView::read(conn, post_id, Some(local_user_view.person.id))
|
2020-07-21 03:46:36 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-12-11 15:27:33 +00:00
|
|
|
let res = PostResponse { post_view };
|
2020-07-21 03:46:36 +00:00
|
|
|
|
2020-08-24 11:58:24 +00:00
|
|
|
context.chat_server().do_send(SendPost {
|
|
|
|
op: UserOperation::LockPost,
|
|
|
|
post: res.clone(),
|
|
|
|
websocket_id,
|
|
|
|
});
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for StickyPost {
|
2020-07-21 03:46:36 +00:00
|
|
|
type Response = PostResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2020-07-21 03:46:36 +00:00
|
|
|
) -> Result<PostResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &StickyPost = &self;
|
2021-03-10 22:33:55 +00:00
|
|
|
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
2021-01-18 21:57:31 +00:00
|
|
|
let post_id = data.post_id;
|
|
|
|
let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
check_community_ban(
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_post.community_id,
|
|
|
|
context.pool(),
|
|
|
|
)
|
|
|
|
.await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Verify that only the mods can sticky
|
2021-03-11 04:43:11 +00:00
|
|
|
is_mod_or_admin(
|
|
|
|
context.pool(),
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_post.community_id,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Update the post
|
2021-01-18 21:57:31 +00:00
|
|
|
let post_id = data.post_id;
|
2020-07-21 03:46:36 +00:00
|
|
|
let stickied = data.stickied;
|
2020-08-18 13:43:50 +00:00
|
|
|
let updated_post = blocking(context.pool(), move |conn| {
|
2021-01-18 21:57:31 +00:00
|
|
|
Post::update_stickied(conn, post_id, stickied)
|
2020-07-21 03:46:36 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let form = ModStickyPostForm {
|
2021-03-10 22:33:55 +00:00
|
|
|
mod_person_id: local_user_view.person.id,
|
2021-01-18 21:57:31 +00:00
|
|
|
post_id: data.post_id,
|
2020-07-21 03:46:36 +00:00
|
|
|
stickied: Some(stickied),
|
|
|
|
};
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
ModStickyPost::create(conn, &form)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Apub updates
|
|
|
|
// TODO stickied should pry work like locked for ease of use
|
2021-03-11 04:43:11 +00:00
|
|
|
updated_post
|
|
|
|
.send_update(&local_user_view.person, context)
|
|
|
|
.await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Refetch the post
|
2021-01-18 21:57:31 +00:00
|
|
|
let post_id = data.post_id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
PostView::read(conn, post_id, Some(local_user_view.person.id))
|
2020-07-21 03:46:36 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-12-11 15:27:33 +00:00
|
|
|
let res = PostResponse { post_view };
|
2020-07-21 03:46:36 +00:00
|
|
|
|
2020-08-24 11:58:24 +00:00
|
|
|
context.chat_server().do_send(SendPost {
|
|
|
|
op: UserOperation::StickyPost,
|
|
|
|
post: res.clone(),
|
|
|
|
websocket_id,
|
|
|
|
});
|
2020-04-19 22:08:25 +00:00
|
|
|
|
|
|
|
Ok(res)
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for SavePost {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = PostResponse;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
_websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<PostResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &SavePost = &self;
|
2021-03-10 22:33:55 +00:00
|
|
|
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
let post_saved_form = PostSavedForm {
|
|
|
|
post_id: data.post_id,
|
2021-03-10 22:33:55 +00:00
|
|
|
person_id: local_user_view.person.id,
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if data.save {
|
2020-07-01 12:54:29 +00:00
|
|
|
let save = move |conn: &'_ _| PostSaved::save(conn, &post_saved_form);
|
2020-08-18 13:43:50 +00:00
|
|
|
if blocking(context.pool(), save).await?.is_err() {
|
2021-02-22 18:04:32 +00:00
|
|
|
return Err(ApiError::err("couldnt_save_post").into());
|
2020-07-01 12:54:29 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
} else {
|
2020-07-01 12:54:29 +00:00
|
|
|
let unsave = move |conn: &'_ _| PostSaved::unsave(conn, &post_saved_form);
|
2020-08-18 13:43:50 +00:00
|
|
|
if blocking(context.pool(), unsave).await?.is_err() {
|
2021-02-22 18:04:32 +00:00
|
|
|
return Err(ApiError::err("couldnt_save_post").into());
|
2020-07-01 12:54:29 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let post_id = data.post_id;
|
2021-03-10 22:33:55 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
PostView::read(conn, post_id, Some(person_id))
|
2020-07-01 12:54:29 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-12-11 15:27:33 +00:00
|
|
|
Ok(PostResponse { post_view })
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 19:26:47 +00:00
|
|
|
|
2020-11-04 02:15:11 +00:00
|
|
|
/// Creates a post report and notifies the moderators of the community
|
2020-10-25 02:59:13 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for CreatePostReport {
|
|
|
|
type Response = CreatePostReportResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
2020-10-27 00:25:18 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2020-10-25 02:59:13 +00:00
|
|
|
) -> Result<CreatePostReportResponse, LemmyError> {
|
|
|
|
let data: &CreatePostReport = &self;
|
2021-03-10 22:33:55 +00:00
|
|
|
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
|
2020-10-25 02:59:13 +00:00
|
|
|
|
|
|
|
// check size of report and check for whitespace
|
|
|
|
let reason = data.reason.trim();
|
|
|
|
if reason.is_empty() {
|
2021-02-22 18:04:32 +00:00
|
|
|
return Err(ApiError::err("report_reason_required").into());
|
2020-10-25 02:59:13 +00:00
|
|
|
}
|
2021-02-10 15:36:22 +00:00
|
|
|
if reason.chars().count() > 1000 {
|
2021-02-22 18:04:32 +00:00
|
|
|
return Err(ApiError::err("report_too_long").into());
|
2020-10-25 02:59:13 +00:00
|
|
|
}
|
2020-11-04 02:15:11 +00:00
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
2020-10-25 02:59:13 +00:00
|
|
|
let post_id = data.post_id;
|
2020-12-11 15:27:33 +00:00
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
2020-10-25 02:59:13 +00:00
|
|
|
PostView::read(&conn, post_id, None)
|
2020-11-04 02:15:11 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2020-10-25 02:59:13 +00:00
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
check_community_ban(person_id, post_view.community.id, context.pool()).await?;
|
2020-10-25 02:59:13 +00:00
|
|
|
|
|
|
|
let report_form = PostReportForm {
|
2021-03-10 22:33:55 +00:00
|
|
|
creator_id: person_id,
|
2020-10-25 02:59:13 +00:00
|
|
|
post_id,
|
2020-12-11 15:27:33 +00:00
|
|
|
original_post_name: post_view.post.name,
|
|
|
|
original_post_url: post_view.post.url,
|
|
|
|
original_post_body: post_view.post.body,
|
2020-10-25 02:59:13 +00:00
|
|
|
reason: data.reason.to_owned(),
|
|
|
|
};
|
|
|
|
|
2020-10-27 00:25:18 +00:00
|
|
|
let report = match blocking(context.pool(), move |conn| {
|
2020-10-25 02:59:13 +00:00
|
|
|
PostReport::report(conn, &report_form)
|
2020-11-04 02:15:11 +00:00
|
|
|
})
|
|
|
|
.await?
|
|
|
|
{
|
2020-10-25 02:59:13 +00:00
|
|
|
Ok(report) => report,
|
2021-02-22 18:04:32 +00:00
|
|
|
Err(_e) => return Err(ApiError::err("couldnt_create_report").into()),
|
2020-10-25 02:59:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let res = CreatePostReportResponse { success: true };
|
|
|
|
|
2020-10-27 00:25:18 +00:00
|
|
|
context.chat_server().do_send(SendUserRoomMessage {
|
|
|
|
op: UserOperation::CreatePostReport,
|
|
|
|
response: res.clone(),
|
2021-03-12 20:18:03 +00:00
|
|
|
local_recipient_id: local_user_view.local_user.id,
|
2020-10-27 00:25:18 +00:00
|
|
|
websocket_id,
|
|
|
|
});
|
|
|
|
|
|
|
|
context.chat_server().do_send(SendModRoomMessage {
|
|
|
|
op: UserOperation::CreatePostReport,
|
|
|
|
response: report,
|
2020-12-11 15:27:33 +00:00
|
|
|
community_id: post_view.community.id,
|
2020-10-27 00:25:18 +00:00
|
|
|
websocket_id,
|
|
|
|
});
|
2020-10-25 02:59:13 +00:00
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 02:15:11 +00:00
|
|
|
/// Resolves or unresolves a post report and notifies the moderators of the community
|
2020-10-25 02:59:13 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for ResolvePostReport {
|
|
|
|
type Response = ResolvePostReportResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
2020-10-27 00:25:18 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2020-10-25 02:59:13 +00:00
|
|
|
) -> Result<ResolvePostReportResponse, LemmyError> {
|
|
|
|
let data: &ResolvePostReport = &self;
|
2021-03-10 22:33:55 +00:00
|
|
|
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
|
2020-10-25 02:59:13 +00:00
|
|
|
|
|
|
|
let report_id = data.report_id;
|
|
|
|
let report = blocking(context.pool(), move |conn| {
|
|
|
|
PostReportView::read(&conn, report_id)
|
2020-11-04 02:15:11 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2020-10-25 02:59:13 +00:00
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
|
|
|
is_mod_or_admin(context.pool(), person_id, report.community.id).await?;
|
2020-10-25 02:59:13 +00:00
|
|
|
|
|
|
|
let resolved = data.resolved;
|
|
|
|
let resolve_fun = move |conn: &'_ _| {
|
|
|
|
if resolved {
|
2021-03-10 22:33:55 +00:00
|
|
|
PostReport::resolve(conn, report_id, person_id)
|
2020-10-25 02:59:13 +00:00
|
|
|
} else {
|
2021-03-10 22:33:55 +00:00
|
|
|
PostReport::unresolve(conn, report_id, person_id)
|
2020-10-25 02:59:13 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = ResolvePostReportResponse {
|
|
|
|
report_id,
|
|
|
|
resolved: true,
|
|
|
|
};
|
|
|
|
|
2020-11-04 02:15:11 +00:00
|
|
|
if blocking(context.pool(), resolve_fun).await?.is_err() {
|
2021-02-22 18:04:32 +00:00
|
|
|
return Err(ApiError::err("couldnt_resolve_report").into());
|
2020-10-25 02:59:13 +00:00
|
|
|
};
|
|
|
|
|
2020-10-27 00:25:18 +00:00
|
|
|
context.chat_server().do_send(SendModRoomMessage {
|
|
|
|
op: UserOperation::ResolvePostReport,
|
|
|
|
response: res.clone(),
|
2020-12-17 03:03:03 +00:00
|
|
|
community_id: report.community.id,
|
2020-10-27 00:25:18 +00:00
|
|
|
websocket_id,
|
|
|
|
});
|
2020-10-25 02:59:13 +00:00
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 02:15:11 +00:00
|
|
|
/// Lists post reports for a community if an id is supplied
|
|
|
|
/// or returns all post reports for communities a user moderates
|
2020-10-25 02:59:13 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for ListPostReports {
|
|
|
|
type Response = ListPostReportsResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<ListPostReportsResponse, LemmyError> {
|
|
|
|
let data: &ListPostReports = &self;
|
2021-03-10 22:33:55 +00:00
|
|
|
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
|
2020-10-25 02:59:13 +00:00
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
2020-10-25 02:59:13 +00:00
|
|
|
let community_id = data.community;
|
2020-11-04 02:15:11 +00:00
|
|
|
let community_ids =
|
2021-03-10 22:33:55 +00:00
|
|
|
collect_moderated_communities(person_id, community_id, context.pool()).await?;
|
2020-10-25 02:59:13 +00:00
|
|
|
|
|
|
|
let page = data.page;
|
|
|
|
let limit = data.limit;
|
|
|
|
let posts = blocking(context.pool(), move |conn| {
|
|
|
|
PostReportQueryBuilder::create(conn)
|
2020-11-04 02:15:11 +00:00
|
|
|
.community_ids(community_ids)
|
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
2020-10-25 02:59:13 +00:00
|
|
|
})
|
2020-11-04 02:15:11 +00:00
|
|
|
.await??;
|
2020-10-25 02:59:13 +00:00
|
|
|
|
|
|
|
let res = ListPostReportsResponse { posts };
|
|
|
|
|
|
|
|
context.chat_server().do_send(SendUserRoomMessage {
|
|
|
|
op: UserOperation::ListPostReports,
|
|
|
|
response: res.clone(),
|
2021-03-12 20:18:03 +00:00
|
|
|
local_recipient_id: local_user_view.local_user.id,
|
2020-10-25 02:59:13 +00:00
|
|
|
websocket_id,
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|