2021-03-25 19:19:40 +00:00
|
|
|
use crate::Perform;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
|
|
|
blocking,
|
2020-09-24 13:53:21 +00:00
|
|
|
check_community_ban,
|
2020-12-20 01:10:47 +00:00
|
|
|
check_downvotes_enabled,
|
2021-08-19 20:54:15 +00:00
|
|
|
check_person_block,
|
2021-03-10 22:33:55 +00:00
|
|
|
get_local_user_view_from_jwt,
|
2020-09-24 13:53:21 +00:00
|
|
|
is_mod_or_admin,
|
2021-04-24 22:26:50 +00:00
|
|
|
mark_post_as_read,
|
2021-03-25 19:19:40 +00:00
|
|
|
post::*,
|
2020-12-24 00:09:41 +00:00
|
|
|
};
|
2021-07-31 17:26:17 +00:00
|
|
|
use lemmy_apub::{
|
2021-08-02 20:33:40 +00:00
|
|
|
activities::{
|
|
|
|
post::create_or_update::CreateOrUpdatePost,
|
|
|
|
voting::{
|
|
|
|
undo_vote::UndoVote,
|
|
|
|
vote::{Vote, VoteType},
|
|
|
|
},
|
|
|
|
CreateOrUpdateType,
|
|
|
|
},
|
2021-09-25 15:44:52 +00:00
|
|
|
fetcher::post_or_comment::PostOrComment,
|
2021-07-31 17:26:17 +00:00
|
|
|
};
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_db_queries::{source::post::Post_, Crud, Likeable, Saveable};
|
|
|
|
use lemmy_db_schema::source::{moderator::*, post::*};
|
|
|
|
use lemmy_db_views::post_view::PostView;
|
2021-08-19 14:12:49 +00:00
|
|
|
use lemmy_utils::{request::fetch_site_metadata, ApiError, ConnectionId, LemmyError};
|
2021-08-17 18:04:58 +00:00
|
|
|
use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperation};
|
2021-08-02 20:33:40 +00:00
|
|
|
use std::convert::TryInto;
|
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> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &CreatePostLike = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).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
|
|
|
|
2021-08-19 20:54:15 +00:00
|
|
|
check_person_block(local_user_view.person.id, post.creator_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
|
|
|
|
2021-08-02 20:33:40 +00:00
|
|
|
let community_id = post.community_id;
|
|
|
|
let object = PostOrComment::Post(Box::new(post));
|
|
|
|
|
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
|
|
|
|
2021-08-02 20:33:40 +00:00
|
|
|
Vote::send(
|
|
|
|
&object,
|
|
|
|
&local_user_view.person,
|
|
|
|
community_id,
|
|
|
|
like_form.score.try_into()?,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-04-28 02:46:09 +00:00
|
|
|
} else {
|
2021-08-02 20:33:40 +00:00
|
|
|
// API doesn't distinguish between Undo/Like and Undo/Dislike
|
|
|
|
UndoVote::send(
|
|
|
|
&object,
|
|
|
|
&local_user_view.person,
|
|
|
|
community_id,
|
|
|
|
VoteType::Like,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-24 22:26:50 +00:00
|
|
|
// Mark the post as read
|
|
|
|
mark_post_as_read(person_id, post_id, context.pool()).await?;
|
|
|
|
|
2021-08-17 18:04:58 +00:00
|
|
|
send_post_ws_message(
|
|
|
|
data.post_id,
|
|
|
|
UserOperation::CreatePostLike,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id,
|
2021-08-17 18:04:58 +00:00
|
|
|
Some(local_user_view.person.id),
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 03:46:36 +00:00
|
|
|
#[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> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &LockPost = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).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-07-31 17:26:17 +00:00
|
|
|
CreateOrUpdatePost::send(
|
|
|
|
&updated_post,
|
|
|
|
&local_user_view.person,
|
|
|
|
CreateOrUpdateType::Update,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
2021-08-17 18:04:58 +00:00
|
|
|
send_post_ws_message(
|
|
|
|
data.post_id,
|
|
|
|
UserOperation::LockPost,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id,
|
2021-08-17 18:04:58 +00:00
|
|
|
Some(local_user_view.person.id),
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await
|
2020-07-21 03:46:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &StickyPost = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).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-07-31 17:26:17 +00:00
|
|
|
CreateOrUpdatePost::send(
|
|
|
|
&updated_post,
|
|
|
|
&local_user_view.person,
|
|
|
|
CreateOrUpdateType::Update,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
2021-08-17 18:04:58 +00:00
|
|
|
send_post_ws_message(
|
|
|
|
data.post_id,
|
|
|
|
UserOperation::StickyPost,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id,
|
2021-08-17 18:04:58 +00:00
|
|
|
Some(local_user_view.person.id),
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await
|
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> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &SavePost = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).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
|
|
|
|
2021-04-24 22:26:50 +00:00
|
|
|
// Mark the post as read
|
|
|
|
mark_post_as_read(person_id, post_id, context.pool()).await?;
|
|
|
|
|
2020-12-11 15:27:33 +00:00
|
|
|
Ok(PostResponse { post_view })
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-08-19 14:12:49 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for GetSiteMetadata {
|
|
|
|
type Response = GetSiteMetadataResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<GetSiteMetadataResponse, LemmyError> {
|
|
|
|
let data: &Self = self;
|
|
|
|
|
|
|
|
let metadata = fetch_site_metadata(context.client(), &data.url).await?;
|
|
|
|
|
|
|
|
Ok(GetSiteMetadataResponse { metadata })
|
|
|
|
}
|
|
|
|
}
|