2020-05-16 14:04:08 +00:00
|
|
|
use crate::{
|
2020-08-04 14:39:55 +00:00
|
|
|
api::{
|
|
|
|
check_community_ban,
|
|
|
|
check_slurs,
|
|
|
|
check_slurs_opt,
|
|
|
|
get_user_from_jwt,
|
|
|
|
get_user_from_jwt_opt,
|
|
|
|
is_mod_or_admin,
|
|
|
|
APIError,
|
|
|
|
Oper,
|
|
|
|
Perform,
|
|
|
|
},
|
2020-05-16 14:04:08 +00:00
|
|
|
apub::{ApubLikeableType, ApubObjectType},
|
2020-07-01 12:54:29 +00:00
|
|
|
blocking,
|
2020-06-24 01:11:38 +00:00
|
|
|
fetch_iframely_and_pictrs_data,
|
2020-05-16 14:04:08 +00:00
|
|
|
websocket::{
|
|
|
|
server::{JoinCommunityRoom, JoinPostRoom, SendPost},
|
|
|
|
UserOperation,
|
|
|
|
WebsocketInfo,
|
|
|
|
},
|
2020-07-01 12:54:29 +00:00
|
|
|
DbPool,
|
|
|
|
LemmyError,
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
2020-07-10 18:15:41 +00:00
|
|
|
use lemmy_db::{
|
|
|
|
comment_view::*,
|
|
|
|
community_view::*,
|
|
|
|
moderator::*,
|
|
|
|
naive_now,
|
|
|
|
post::*,
|
|
|
|
post_view::*,
|
|
|
|
site_view::*,
|
|
|
|
Crud,
|
|
|
|
Likeable,
|
|
|
|
ListingType,
|
|
|
|
Saveable,
|
|
|
|
SortType,
|
|
|
|
};
|
2020-08-04 14:39:55 +00:00
|
|
|
use lemmy_utils::{is_valid_post_title, make_apub_endpoint, EndpointType};
|
2020-05-16 14:04:08 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::str::FromStr;
|
2020-07-17 22:46:59 +00:00
|
|
|
use url::Url;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-04-13 13:06:41 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
2019-05-05 05:20:38 +00:00
|
|
|
pub struct CreatePost {
|
|
|
|
name: String,
|
|
|
|
url: Option<String>,
|
|
|
|
body: Option<String>,
|
2019-08-14 02:52:43 +00:00
|
|
|
nsfw: bool,
|
2020-02-01 01:02:20 +00:00
|
|
|
pub community_id: i32,
|
2019-09-07 15:35:05 +00:00
|
|
|
auth: String,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
|
|
|
pub struct PostResponse {
|
2019-09-07 15:35:05 +00:00
|
|
|
pub post: PostView,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetPost {
|
|
|
|
pub id: i32,
|
2019-09-07 15:35:05 +00:00
|
|
|
auth: Option<String>,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetPostResponse {
|
|
|
|
post: PostView,
|
|
|
|
comments: Vec<CommentView>,
|
|
|
|
community: CommunityView,
|
|
|
|
moderators: Vec<CommunityModeratorView>,
|
2020-02-01 01:02:20 +00:00
|
|
|
pub online: usize,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 00:05:42 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
2019-05-05 05:20:38 +00:00
|
|
|
pub struct GetPosts {
|
|
|
|
type_: String,
|
|
|
|
sort: String,
|
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
2020-02-01 01:02:20 +00:00
|
|
|
pub community_id: Option<i32>,
|
2020-07-13 13:50:13 +00:00
|
|
|
pub community_name: Option<String>,
|
2019-09-07 15:35:05 +00:00
|
|
|
auth: Option<String>,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 21:03:05 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
2019-05-05 05:20:38 +00:00
|
|
|
pub struct GetPostsResponse {
|
2020-03-14 21:03:05 +00:00
|
|
|
pub posts: Vec<PostView>,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct CreatePostLike {
|
|
|
|
post_id: i32,
|
|
|
|
score: i16,
|
2019-09-07 15:35:05 +00:00
|
|
|
auth: String,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct EditPost {
|
|
|
|
pub edit_id: i32,
|
|
|
|
name: String,
|
|
|
|
url: Option<String>,
|
|
|
|
body: Option<String>,
|
2019-08-14 02:52:43 +00:00
|
|
|
nsfw: bool,
|
2020-07-21 03:46:36 +00:00
|
|
|
auth: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct DeletePost {
|
|
|
|
pub edit_id: i32,
|
|
|
|
deleted: bool,
|
|
|
|
auth: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct RemovePost {
|
|
|
|
pub edit_id: i32,
|
|
|
|
removed: bool,
|
2019-05-05 05:20:38 +00:00
|
|
|
reason: Option<String>,
|
2019-09-07 15:35:05 +00:00
|
|
|
auth: String,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 03:46:36 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct LockPost {
|
|
|
|
pub edit_id: i32,
|
|
|
|
locked: bool,
|
|
|
|
auth: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct StickyPost {
|
|
|
|
pub edit_id: i32,
|
|
|
|
stickied: bool,
|
|
|
|
auth: String,
|
|
|
|
}
|
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct SavePost {
|
|
|
|
post_id: i32,
|
|
|
|
save: bool,
|
2019-09-07 15:35:05 +00:00
|
|
|
auth: String,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-04-20 03:59:07 +00:00
|
|
|
impl Perform for Oper<CreatePost> {
|
|
|
|
type Response = PostResponse;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-07-01 12:54:29 +00:00
|
|
|
pool: &DbPool,
|
2020-04-19 22:08:25 +00:00
|
|
|
websocket_info: Option<WebsocketInfo>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<PostResponse, LemmyError> {
|
2019-05-05 16:20:30 +00:00
|
|
|
let data: &CreatePost = &self.data;
|
2020-08-04 14:39:55 +00:00
|
|
|
let user = get_user_from_jwt(&data.auth, 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) {
|
|
|
|
return Err(APIError::err("invalid_post_title").into());
|
|
|
|
}
|
|
|
|
|
2020-08-04 14:39:55 +00:00
|
|
|
check_community_ban(user.id, data.community_id, pool).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-17 22:46:59 +00:00
|
|
|
if let Some(url) = data.url.as_ref() {
|
|
|
|
match Url::parse(url) {
|
|
|
|
Ok(_t) => (),
|
|
|
|
Err(_e) => return Err(APIError::err("invalid_url").into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 02:47:06 +00:00
|
|
|
// Fetch Iframely and pictrs cached image
|
|
|
|
let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
|
2020-07-01 12:54:29 +00:00
|
|
|
fetch_iframely_and_pictrs_data(&self.client, data.url.to_owned()).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(),
|
2019-05-05 05:20:38 +00:00
|
|
|
url: data.url.to_owned(),
|
|
|
|
body: data.body.to_owned(),
|
|
|
|
community_id: data.community_id,
|
2020-08-04 14:39:55 +00:00
|
|
|
creator_id: user.id,
|
2019-05-05 05:20:38 +00:00
|
|
|
removed: None,
|
|
|
|
deleted: None,
|
2019-08-14 02:52:43 +00:00
|
|
|
nsfw: data.nsfw,
|
2019-05-05 05:20:38 +00:00
|
|
|
locked: None,
|
2019-09-09 06:14:13 +00:00
|
|
|
stickied: None,
|
2019-09-07 15:35:05 +00:00
|
|
|
updated: None,
|
2020-03-07 23:31:13 +00:00
|
|
|
embed_title: iframely_title,
|
|
|
|
embed_description: iframely_description,
|
|
|
|
embed_html: iframely_html,
|
2020-06-11 02:47:06 +00:00
|
|
|
thumbnail_url: pictrs_thumbnail,
|
2020-06-27 01:12:41 +00:00
|
|
|
ap_id: "http://fake.com".into(),
|
2020-04-04 00:04:57 +00:00
|
|
|
local: true,
|
2020-04-07 16:47:19 +00:00
|
|
|
published: None,
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let inserted_post = match blocking(pool, move |conn| Post::create(conn, &post_form)).await? {
|
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_create_post"
|
|
|
|
};
|
|
|
|
|
|
|
|
return Err(APIError::err(err_type).into());
|
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let inserted_post_id = inserted_post.id;
|
2020-07-10 18:15:41 +00:00
|
|
|
let updated_post = match blocking(pool, move |conn| {
|
|
|
|
let apub_id =
|
|
|
|
make_apub_endpoint(EndpointType::Post, &inserted_post_id.to_string()).to_string();
|
|
|
|
Post::update_ap_id(conn, inserted_post_id, apub_id)
|
|
|
|
})
|
|
|
|
.await?
|
|
|
|
{
|
|
|
|
Ok(post) => post,
|
|
|
|
Err(_e) => return Err(APIError::err("couldnt_create_post").into()),
|
|
|
|
};
|
2020-04-04 00:04:57 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
updated_post.send_create(&user, &self.client, pool).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,
|
2020-08-04 14:39:55 +00:00
|
|
|
user_id: user.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);
|
|
|
|
if blocking(pool, like).await?.is_err() {
|
|
|
|
return Err(APIError::err("couldnt_like_post").into());
|
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
updated_post.send_like(&user, &self.client, pool).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;
|
|
|
|
let post_view = match blocking(pool, move |conn| {
|
2020-08-04 14:39:55 +00:00
|
|
|
PostView::read(conn, inserted_post_id, Some(user.id))
|
2020-07-01 12:54:29 +00:00
|
|
|
})
|
|
|
|
.await?
|
|
|
|
{
|
2019-05-05 05:20:38 +00:00
|
|
|
Ok(post) => post,
|
2020-01-16 14:39:08 +00:00
|
|
|
Err(_e) => return Err(APIError::err("couldnt_find_post").into()),
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
let res = PostResponse { post: post_view };
|
|
|
|
|
|
|
|
if let Some(ws) = websocket_info {
|
|
|
|
ws.chatserver.do_send(SendPost {
|
|
|
|
op: UserOperation::CreatePost,
|
|
|
|
post: res.clone(),
|
|
|
|
my_id: ws.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-04-20 03:59:07 +00:00
|
|
|
impl Perform for Oper<GetPost> {
|
|
|
|
type Response = GetPostResponse;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-07-01 12:54:29 +00:00
|
|
|
pool: &DbPool,
|
2020-04-19 22:08:25 +00:00
|
|
|
websocket_info: Option<WebsocketInfo>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<GetPostResponse, LemmyError> {
|
2019-05-05 16:20:30 +00:00
|
|
|
let data: &GetPost = &self.data;
|
2020-08-04 14:39:55 +00:00
|
|
|
let user = get_user_from_jwt_opt(&data.auth, pool).await?;
|
|
|
|
let user_id = user.map(|u| u.id);
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let id = data.id;
|
|
|
|
let post_view = match blocking(pool, move |conn| PostView::read(conn, id, user_id)).await? {
|
2019-05-05 05:20:38 +00:00
|
|
|
Ok(post) => post,
|
2020-01-16 14:39:08 +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;
|
|
|
|
let comments = blocking(pool, move |conn| {
|
|
|
|
CommentQueryBuilder::create(conn)
|
|
|
|
.for_post_id(id)
|
|
|
|
.my_user_id(user_id)
|
|
|
|
.limit(9999)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let community_id = post_view.community_id;
|
|
|
|
let community = blocking(pool, move |conn| {
|
|
|
|
CommunityView::read(conn, community_id, user_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let community_id = post_view.community_id;
|
|
|
|
let moderators = blocking(pool, move |conn| {
|
|
|
|
CommunityModeratorView::for_community(conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
let online = if let Some(ws) = websocket_info {
|
|
|
|
if let Some(id) = ws.id {
|
|
|
|
ws.chatserver.do_send(JoinPostRoom {
|
|
|
|
post_id: data.id,
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
1
|
|
|
|
// let fut = async {
|
|
|
|
// ws.chatserver.send(GetPostUsersOnline {post_id: data.id}).await.unwrap()
|
|
|
|
// };
|
|
|
|
// Runtime::new().unwrap().block_on(fut)
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
// Return the jwt
|
2019-09-07 15:35:05 +00:00
|
|
|
Ok(GetPostResponse {
|
|
|
|
post: post_view,
|
2019-12-09 19:08:19 +00:00
|
|
|
comments,
|
|
|
|
community,
|
|
|
|
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-04-20 03:59:07 +00:00
|
|
|
impl Perform for Oper<GetPosts> {
|
|
|
|
type Response = GetPostsResponse;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-07-01 12:54:29 +00:00
|
|
|
pool: &DbPool,
|
2020-04-19 22:08:25 +00:00
|
|
|
websocket_info: Option<WebsocketInfo>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<GetPostsResponse, LemmyError> {
|
2019-05-05 16:20:30 +00:00
|
|
|
let data: &GetPosts = &self.data;
|
2020-08-04 14:39:55 +00:00
|
|
|
let user = get_user_from_jwt_opt(&data.auth, pool).await?;
|
2019-09-07 15:35:05 +00:00
|
|
|
|
2020-07-27 13:23:08 +00:00
|
|
|
let user_id = match &user {
|
|
|
|
Some(user) => Some(user.id),
|
2019-09-07 15:35:05 +00:00
|
|
|
None => None,
|
2019-08-14 02:52:43 +00:00
|
|
|
};
|
|
|
|
|
2020-07-27 13:23:08 +00:00
|
|
|
let show_nsfw = match &user {
|
|
|
|
Some(user) => 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-07-01 12:54:29 +00:00
|
|
|
let posts = match blocking(pool, move |conn| {
|
|
|
|
PostQueryBuilder::create(conn)
|
|
|
|
.listing_type(type_)
|
|
|
|
.sort(&sort)
|
|
|
|
.show_nsfw(show_nsfw)
|
|
|
|
.for_community_id(community_id)
|
2020-07-13 13:50:13 +00:00
|
|
|
.for_community_name(community_name)
|
2020-07-01 12:54:29 +00:00
|
|
|
.my_user_id(user_id)
|
|
|
|
.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,
|
2020-01-16 14:39:08 +00:00
|
|
|
Err(_e) => return Err(APIError::err("couldnt_get_posts").into()),
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
if let Some(ws) = websocket_info {
|
|
|
|
// You don't need to join the specific community room, bc this is already handled by
|
|
|
|
// GetCommunity
|
|
|
|
if data.community_id.is_none() {
|
|
|
|
if let Some(id) = ws.id {
|
|
|
|
// 0 is the "all" community
|
|
|
|
ws.chatserver.do_send(JoinCommunityRoom {
|
|
|
|
community_id: 0,
|
|
|
|
id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-20 03:59:07 +00:00
|
|
|
impl Perform for Oper<CreatePostLike> {
|
|
|
|
type Response = PostResponse;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-07-01 12:54:29 +00:00
|
|
|
pool: &DbPool,
|
2020-04-19 22:08:25 +00:00
|
|
|
websocket_info: Option<WebsocketInfo>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<PostResponse, LemmyError> {
|
2019-05-05 16:20:30 +00:00
|
|
|
let data: &CreatePostLike = &self.data;
|
2020-08-04 14:39:55 +00:00
|
|
|
let user = get_user_from_jwt(&data.auth, 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
|
|
|
|
if data.score == -1 {
|
2020-07-01 12:54:29 +00:00
|
|
|
let site = blocking(pool, move |conn| SiteView::read(conn)).await??;
|
2020-01-02 11:30:00 +00:00
|
|
|
if !site.enable_downvotes {
|
2020-01-16 14:39:08 +00:00
|
|
|
return Err(APIError::err("downvotes_disabled").into());
|
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;
|
|
|
|
let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
|
|
|
|
|
2020-08-04 14:39:55 +00:00
|
|
|
check_community_ban(user.id, post.community_id, pool).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
let like_form = PostLikeForm {
|
|
|
|
post_id: data.post_id,
|
2020-08-04 14:39:55 +00:00
|
|
|
user_id: user.id,
|
2019-09-07 15:35:05 +00:00
|
|
|
score: data.score,
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Remove any likes first
|
2020-07-01 12:54:29 +00:00
|
|
|
let like_form2 = like_form.clone();
|
|
|
|
blocking(pool, move |conn| PostLike::remove(conn, &like_form2)).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);
|
|
|
|
if blocking(pool, like).await?.is_err() {
|
|
|
|
return Err(APIError::err("couldnt_like_post").into());
|
|
|
|
}
|
2020-04-28 02:46:09 +00:00
|
|
|
|
|
|
|
if like_form.score == 1 {
|
2020-07-01 12:54:29 +00:00
|
|
|
post.send_like(&user, &self.client, pool).await?;
|
2020-04-28 02:46:09 +00:00
|
|
|
} else if like_form.score == -1 {
|
2020-07-01 12:54:29 +00:00
|
|
|
post.send_dislike(&user, &self.client, pool).await?;
|
2020-04-28 02:46:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-01 12:54:29 +00:00
|
|
|
post.send_undo_like(&user, &self.client, pool).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let post_id = data.post_id;
|
2020-08-04 14:39:55 +00:00
|
|
|
let user_id = user.id;
|
2020-07-01 12:54:29 +00:00
|
|
|
let post_view = match blocking(pool, move |conn| {
|
|
|
|
PostView::read(conn, post_id, Some(user_id))
|
|
|
|
})
|
|
|
|
.await?
|
|
|
|
{
|
2019-05-05 05:20:38 +00:00
|
|
|
Ok(post) => post,
|
2020-01-16 14:39:08 +00:00
|
|
|
Err(_e) => return Err(APIError::err("couldnt_find_post").into()),
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
let res = PostResponse { post: post_view };
|
|
|
|
|
|
|
|
if let Some(ws) = websocket_info {
|
|
|
|
ws.chatserver.do_send(SendPost {
|
|
|
|
op: UserOperation::CreatePostLike,
|
|
|
|
post: res.clone(),
|
|
|
|
my_id: ws.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-04-20 03:59:07 +00:00
|
|
|
impl Perform for Oper<EditPost> {
|
|
|
|
type Response = PostResponse;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-07-01 12:54:29 +00:00
|
|
|
pool: &DbPool,
|
2020-04-19 22:08:25 +00:00
|
|
|
websocket_info: Option<WebsocketInfo>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<PostResponse, LemmyError> {
|
2019-05-05 16:20:30 +00:00
|
|
|
let data: &EditPost = &self.data;
|
2020-08-04 14:39:55 +00:00
|
|
|
let user = get_user_from_jwt(&data.auth, 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) {
|
|
|
|
return Err(APIError::err("invalid_post_title").into());
|
|
|
|
}
|
|
|
|
|
2020-07-14 13:17:25 +00:00
|
|
|
let edit_id = data.edit_id;
|
2020-07-21 03:46:36 +00:00
|
|
|
let orig_post = blocking(pool, move |conn| Post::read(conn, edit_id)).await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-08-04 14:39:55 +00:00
|
|
|
check_community_ban(user.id, orig_post.community_id, 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
|
2020-08-04 14:39:55 +00:00
|
|
|
if !Post::is_post_creator(user.id, orig_post.creator_id) {
|
2020-07-21 03:46:36 +00:00
|
|
|
return Err(APIError::err("no_post_edit_allowed").into());
|
|
|
|
}
|
|
|
|
|
2020-06-11 02:47:06 +00:00
|
|
|
// Fetch Iframely and Pictrs cached image
|
|
|
|
let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
|
2020-07-01 12:54:29 +00:00
|
|
|
fetch_iframely_and_pictrs_data(&self.client, data.url.to_owned()).await;
|
2020-03-07 23:31:13 +00:00
|
|
|
|
2020-07-21 03:46:36 +00:00
|
|
|
let post_form = PostForm {
|
|
|
|
name: data.name.trim().to_owned(),
|
|
|
|
url: data.url.to_owned(),
|
|
|
|
body: data.body.to_owned(),
|
|
|
|
nsfw: data.nsfw,
|
|
|
|
creator_id: orig_post.creator_id.to_owned(),
|
|
|
|
community_id: orig_post.community_id,
|
|
|
|
removed: Some(orig_post.removed),
|
|
|
|
deleted: Some(orig_post.deleted),
|
|
|
|
locked: Some(orig_post.locked),
|
|
|
|
stickied: Some(orig_post.stickied),
|
|
|
|
updated: Some(naive_now()),
|
|
|
|
embed_title: iframely_title,
|
|
|
|
embed_description: iframely_description,
|
|
|
|
embed_html: iframely_html,
|
|
|
|
thumbnail_url: pictrs_thumbnail,
|
|
|
|
ap_id: orig_post.ap_id,
|
|
|
|
local: orig_post.local,
|
|
|
|
published: None,
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let edit_id = data.edit_id;
|
|
|
|
let res = blocking(pool, move |conn| Post::update(conn, edit_id, &post_form)).await?;
|
|
|
|
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"
|
|
|
|
};
|
|
|
|
|
|
|
|
return Err(APIError::err(err_type).into());
|
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-07-21 03:46:36 +00:00
|
|
|
// Send apub update
|
|
|
|
updated_post.send_update(&user, &self.client, pool).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-21 03:46:36 +00:00
|
|
|
let edit_id = data.edit_id;
|
|
|
|
let post_view = blocking(pool, move |conn| {
|
2020-08-04 14:39:55 +00:00
|
|
|
PostView::read(conn, edit_id, Some(user.id))
|
2020-07-21 03:46:36 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-21 03:46:36 +00:00
|
|
|
let res = PostResponse { post: post_view };
|
|
|
|
|
|
|
|
if let Some(ws) = websocket_info {
|
|
|
|
ws.chatserver.do_send(SendPost {
|
|
|
|
op: UserOperation::EditPost,
|
|
|
|
post: res.clone(),
|
|
|
|
my_id: ws.id,
|
|
|
|
});
|
2019-09-09 06:14:13 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 03:46:36 +00:00
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for Oper<DeletePost> {
|
|
|
|
type Response = PostResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
pool: &DbPool,
|
|
|
|
websocket_info: Option<WebsocketInfo>,
|
|
|
|
) -> Result<PostResponse, LemmyError> {
|
|
|
|
let data: &DeletePost = &self.data;
|
2020-08-04 14:39:55 +00:00
|
|
|
let user = get_user_from_jwt(&data.auth, pool).await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
let edit_id = data.edit_id;
|
|
|
|
let orig_post = blocking(pool, move |conn| Post::read(conn, edit_id)).await??;
|
|
|
|
|
2020-08-04 14:39:55 +00:00
|
|
|
check_community_ban(user.id, orig_post.community_id, pool).await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Verify that only the creator can delete
|
2020-08-04 14:39:55 +00:00
|
|
|
if !Post::is_post_creator(user.id, orig_post.creator_id) {
|
2020-07-21 03:46:36 +00:00
|
|
|
return Err(APIError::err("no_post_edit_allowed").into());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the post
|
|
|
|
let edit_id = data.edit_id;
|
|
|
|
let deleted = data.deleted;
|
|
|
|
let updated_post = blocking(pool, move |conn| {
|
|
|
|
Post::update_deleted(conn, edit_id, deleted)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// apub updates
|
|
|
|
if deleted {
|
|
|
|
updated_post.send_delete(&user, &self.client, pool).await?;
|
2020-05-01 14:07:38 +00:00
|
|
|
} else {
|
2020-07-21 03:46:36 +00:00
|
|
|
updated_post
|
|
|
|
.send_undo_delete(&user, &self.client, pool)
|
|
|
|
.await?;
|
2020-04-29 14:51:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 03:46:36 +00:00
|
|
|
// Refetch the post
|
2020-07-01 12:54:29 +00:00
|
|
|
let edit_id = data.edit_id;
|
|
|
|
let post_view = blocking(pool, move |conn| {
|
2020-08-04 14:39:55 +00:00
|
|
|
PostView::read(conn, edit_id, Some(user.id))
|
2020-07-01 12:54:29 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
let res = PostResponse { post: post_view };
|
|
|
|
|
|
|
|
if let Some(ws) = websocket_info {
|
|
|
|
ws.chatserver.do_send(SendPost {
|
2020-07-21 03:46:36 +00:00
|
|
|
op: UserOperation::DeletePost,
|
|
|
|
post: res.clone(),
|
|
|
|
my_id: ws.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for Oper<RemovePost> {
|
|
|
|
type Response = PostResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
pool: &DbPool,
|
|
|
|
websocket_info: Option<WebsocketInfo>,
|
|
|
|
) -> Result<PostResponse, LemmyError> {
|
|
|
|
let data: &RemovePost = &self.data;
|
2020-08-04 14:39:55 +00:00
|
|
|
let user = get_user_from_jwt(&data.auth, pool).await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
let edit_id = data.edit_id;
|
|
|
|
let orig_post = blocking(pool, move |conn| Post::read(conn, edit_id)).await??;
|
|
|
|
|
2020-08-04 14:39:55 +00:00
|
|
|
check_community_ban(user.id, orig_post.community_id, pool).await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Verify that only the mods can remove
|
2020-08-04 14:39:55 +00:00
|
|
|
is_mod_or_admin(pool, user.id, orig_post.community_id).await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Update the post
|
|
|
|
let edit_id = data.edit_id;
|
|
|
|
let removed = data.removed;
|
|
|
|
let updated_post = blocking(pool, move |conn| {
|
|
|
|
Post::update_removed(conn, edit_id, removed)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let form = ModRemovePostForm {
|
2020-08-04 14:39:55 +00:00
|
|
|
mod_user_id: user.id,
|
2020-07-21 03:46:36 +00:00
|
|
|
post_id: data.edit_id,
|
|
|
|
removed: Some(removed),
|
|
|
|
reason: data.reason.to_owned(),
|
|
|
|
};
|
|
|
|
blocking(pool, move |conn| ModRemovePost::create(conn, &form)).await??;
|
|
|
|
|
|
|
|
// apub updates
|
|
|
|
if removed {
|
|
|
|
updated_post.send_remove(&user, &self.client, pool).await?;
|
|
|
|
} else {
|
|
|
|
updated_post
|
|
|
|
.send_undo_remove(&user, &self.client, pool)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Refetch the post
|
|
|
|
let edit_id = data.edit_id;
|
2020-08-04 14:39:55 +00:00
|
|
|
let user_id = user.id;
|
2020-07-21 03:46:36 +00:00
|
|
|
let post_view = blocking(pool, move |conn| {
|
|
|
|
PostView::read(conn, edit_id, Some(user_id))
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let res = PostResponse { post: post_view };
|
|
|
|
|
|
|
|
if let Some(ws) = websocket_info {
|
|
|
|
ws.chatserver.do_send(SendPost {
|
|
|
|
op: UserOperation::RemovePost,
|
|
|
|
post: res.clone(),
|
|
|
|
my_id: ws.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for Oper<LockPost> {
|
|
|
|
type Response = PostResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
pool: &DbPool,
|
|
|
|
websocket_info: Option<WebsocketInfo>,
|
|
|
|
) -> Result<PostResponse, LemmyError> {
|
|
|
|
let data: &LockPost = &self.data;
|
2020-08-04 14:39:55 +00:00
|
|
|
let user = get_user_from_jwt(&data.auth, pool).await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
let edit_id = data.edit_id;
|
|
|
|
let orig_post = blocking(pool, move |conn| Post::read(conn, edit_id)).await??;
|
|
|
|
|
2020-08-04 14:39:55 +00:00
|
|
|
check_community_ban(user.id, orig_post.community_id, pool).await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Verify that only the mods can lock
|
2020-08-04 14:39:55 +00:00
|
|
|
is_mod_or_admin(pool, user.id, orig_post.community_id).await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Update the post
|
|
|
|
let edit_id = data.edit_id;
|
|
|
|
let locked = data.locked;
|
|
|
|
let updated_post =
|
|
|
|
blocking(pool, move |conn| Post::update_locked(conn, edit_id, locked)).await??;
|
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let form = ModLockPostForm {
|
2020-08-04 14:39:55 +00:00
|
|
|
mod_user_id: user.id,
|
2020-07-21 03:46:36 +00:00
|
|
|
post_id: data.edit_id,
|
|
|
|
locked: Some(locked),
|
|
|
|
};
|
|
|
|
blocking(pool, move |conn| ModLockPost::create(conn, &form)).await??;
|
|
|
|
|
|
|
|
// apub updates
|
|
|
|
updated_post.send_update(&user, &self.client, pool).await?;
|
|
|
|
|
|
|
|
// Refetch the post
|
|
|
|
let edit_id = data.edit_id;
|
|
|
|
let post_view = blocking(pool, move |conn| {
|
2020-08-04 14:39:55 +00:00
|
|
|
PostView::read(conn, edit_id, Some(user.id))
|
2020-07-21 03:46:36 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let res = PostResponse { post: post_view };
|
|
|
|
|
|
|
|
if let Some(ws) = websocket_info {
|
|
|
|
ws.chatserver.do_send(SendPost {
|
|
|
|
op: UserOperation::LockPost,
|
|
|
|
post: res.clone(),
|
|
|
|
my_id: ws.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for Oper<StickyPost> {
|
|
|
|
type Response = PostResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
pool: &DbPool,
|
|
|
|
websocket_info: Option<WebsocketInfo>,
|
|
|
|
) -> Result<PostResponse, LemmyError> {
|
|
|
|
let data: &StickyPost = &self.data;
|
2020-08-04 14:39:55 +00:00
|
|
|
let user = get_user_from_jwt(&data.auth, pool).await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
let edit_id = data.edit_id;
|
|
|
|
let orig_post = blocking(pool, move |conn| Post::read(conn, edit_id)).await??;
|
|
|
|
|
2020-08-04 14:39:55 +00:00
|
|
|
check_community_ban(user.id, orig_post.community_id, pool).await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Verify that only the mods can sticky
|
2020-08-04 14:39:55 +00:00
|
|
|
is_mod_or_admin(pool, user.id, orig_post.community_id).await?;
|
2020-07-21 03:46:36 +00:00
|
|
|
|
|
|
|
// Update the post
|
|
|
|
let edit_id = data.edit_id;
|
|
|
|
let stickied = data.stickied;
|
|
|
|
let updated_post = blocking(pool, move |conn| {
|
|
|
|
Post::update_stickied(conn, edit_id, stickied)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let form = ModStickyPostForm {
|
2020-08-04 14:39:55 +00:00
|
|
|
mod_user_id: user.id,
|
2020-07-21 03:46:36 +00:00
|
|
|
post_id: data.edit_id,
|
|
|
|
stickied: Some(stickied),
|
|
|
|
};
|
|
|
|
blocking(pool, move |conn| ModStickyPost::create(conn, &form)).await??;
|
|
|
|
|
|
|
|
// Apub updates
|
|
|
|
// TODO stickied should pry work like locked for ease of use
|
|
|
|
updated_post.send_update(&user, &self.client, pool).await?;
|
|
|
|
|
|
|
|
// Refetch the post
|
|
|
|
let edit_id = data.edit_id;
|
|
|
|
let post_view = blocking(pool, move |conn| {
|
2020-08-04 14:39:55 +00:00
|
|
|
PostView::read(conn, edit_id, Some(user.id))
|
2020-07-21 03:46:36 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let res = PostResponse { post: post_view };
|
|
|
|
|
|
|
|
if let Some(ws) = websocket_info {
|
|
|
|
ws.chatserver.do_send(SendPost {
|
|
|
|
op: UserOperation::StickyPost,
|
2020-04-19 22:08:25 +00:00
|
|
|
post: res.clone(),
|
|
|
|
my_id: ws.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-04-20 03:59:07 +00:00
|
|
|
impl Perform for Oper<SavePost> {
|
|
|
|
type Response = PostResponse;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-07-01 12:54:29 +00:00
|
|
|
pool: &DbPool,
|
2020-04-19 22:08:25 +00:00
|
|
|
_websocket_info: Option<WebsocketInfo>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<PostResponse, LemmyError> {
|
2019-05-05 16:20:30 +00:00
|
|
|
let data: &SavePost = &self.data;
|
2020-08-04 14:39:55 +00:00
|
|
|
let user = get_user_from_jwt(&data.auth, pool).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
let post_saved_form = PostSavedForm {
|
|
|
|
post_id: data.post_id,
|
2020-08-04 14:39:55 +00:00
|
|
|
user_id: user.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);
|
|
|
|
if blocking(pool, save).await?.is_err() {
|
|
|
|
return Err(APIError::err("couldnt_save_post").into());
|
|
|
|
}
|
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);
|
|
|
|
if blocking(pool, unsave).await?.is_err() {
|
|
|
|
return Err(APIError::err("couldnt_save_post").into());
|
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let post_id = data.post_id;
|
2020-08-04 14:39:55 +00:00
|
|
|
let user_id = user.id;
|
2020-07-01 12:54:29 +00:00
|
|
|
let post_view = blocking(pool, move |conn| {
|
|
|
|
PostView::read(conn, post_id, Some(user_id))
|
|
|
|
})
|
|
|
|
.await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-01-16 14:39:08 +00:00
|
|
|
Ok(PostResponse { post: post_view })
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|