2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 check_community_deleted_or_removed,
7 check_downvotes_enabled,
8 get_local_user_view_from_jwt,
15 fetcher::post_or_comment::PostOrComment,
16 objects::post::ApubPost,
17 protocol::activities::{
18 create_or_update::post::CreateOrUpdatePost,
21 vote::{Vote, VoteType},
26 use lemmy_db_schema::{
27 source::{moderator::*, post::*},
28 traits::{Crud, Likeable, Saveable},
30 use lemmy_db_views::post_view::PostView;
31 use lemmy_utils::{request::fetch_site_metadata, ConnectionId, LemmyError};
32 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperation};
33 use std::convert::TryInto;
35 #[async_trait::async_trait(?Send)]
36 impl Perform for CreatePostLike {
37 type Response = PostResponse;
39 #[tracing::instrument(skip(context, websocket_id))]
42 context: &Data<LemmyContext>,
43 websocket_id: Option<ConnectionId>,
44 ) -> Result<PostResponse, LemmyError> {
45 let data: &CreatePostLike = self;
47 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
49 // Don't do a downvote if site has downvotes disabled
50 check_downvotes_enabled(data.score, context.pool()).await?;
52 // Check for a community ban
53 let post_id = data.post_id;
54 let post: ApubPost = blocking(context.pool(), move |conn| Post::read(conn, post_id))
58 check_community_ban(local_user_view.person.id, post.community_id, context.pool()).await?;
59 check_community_deleted_or_removed(post.community_id, context.pool()).await?;
61 let like_form = PostLikeForm {
62 post_id: data.post_id,
63 person_id: local_user_view.person.id,
67 // Remove any likes first
68 let person_id = local_user_view.person.id;
69 blocking(context.pool(), move |conn| {
70 PostLike::remove(conn, person_id, post_id)
74 let community_id = post.community_id;
75 let object = PostOrComment::Post(Box::new(post));
77 // Only add the like if the score isnt 0
78 let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
80 let like_form2 = like_form.clone();
81 let like = move |conn: &'_ _| PostLike::like(conn, &like_form2);
82 blocking(context.pool(), like)
84 .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_post"))?;
88 &local_user_view.person.clone().into(),
90 like_form.score.try_into()?,
95 // API doesn't distinguish between Undo/Like and Undo/Dislike
98 &local_user_view.person.clone().into(),
106 // Mark the post as read
107 mark_post_as_read(person_id, post_id, context.pool()).await?;
109 send_post_ws_message(
111 UserOperation::CreatePostLike,
113 Some(local_user_view.person.id),
120 #[async_trait::async_trait(?Send)]
121 impl Perform for MarkPostAsRead {
122 type Response = PostResponse;
124 #[tracing::instrument(skip(context, _websocket_id))]
127 context: &Data<LemmyContext>,
128 _websocket_id: Option<ConnectionId>,
129 ) -> Result<Self::Response, LemmyError> {
131 let local_user_view =
132 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
134 let post_id = data.post_id;
135 let person_id = local_user_view.person.id;
137 // Mark the post as read / unread
139 mark_post_as_read(person_id, post_id, context.pool()).await?;
141 mark_post_as_unread(person_id, post_id, context.pool()).await?;
145 let post_view = blocking(context.pool(), move |conn| {
146 PostView::read(conn, post_id, Some(person_id))
150 let res = Self::Response { post_view };
156 #[async_trait::async_trait(?Send)]
157 impl Perform for LockPost {
158 type Response = PostResponse;
160 #[tracing::instrument(skip(context, websocket_id))]
163 context: &Data<LemmyContext>,
164 websocket_id: Option<ConnectionId>,
165 ) -> Result<PostResponse, LemmyError> {
166 let data: &LockPost = self;
167 let local_user_view =
168 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
170 let post_id = data.post_id;
171 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
174 local_user_view.person.id,
175 orig_post.community_id,
179 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
181 // Verify that only the mods can lock
184 local_user_view.person.id,
185 orig_post.community_id,
190 let post_id = data.post_id;
191 let locked = data.locked;
192 let updated_post: ApubPost = blocking(context.pool(), move |conn| {
193 Post::update_locked(conn, post_id, locked)
199 let form = ModLockPostForm {
200 mod_person_id: local_user_view.person.id,
201 post_id: data.post_id,
202 locked: Some(locked),
204 blocking(context.pool(), move |conn| ModLockPost::create(conn, &form)).await??;
207 CreateOrUpdatePost::send(
209 &local_user_view.person.clone().into(),
210 CreateOrUpdateType::Update,
215 send_post_ws_message(
217 UserOperation::LockPost,
219 Some(local_user_view.person.id),
226 #[async_trait::async_trait(?Send)]
227 impl Perform for StickyPost {
228 type Response = PostResponse;
230 #[tracing::instrument(skip(context, websocket_id))]
233 context: &Data<LemmyContext>,
234 websocket_id: Option<ConnectionId>,
235 ) -> Result<PostResponse, LemmyError> {
236 let data: &StickyPost = self;
237 let local_user_view =
238 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
240 let post_id = data.post_id;
241 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
244 local_user_view.person.id,
245 orig_post.community_id,
249 check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;
251 // Verify that only the mods can sticky
254 local_user_view.person.id,
255 orig_post.community_id,
260 let post_id = data.post_id;
261 let stickied = data.stickied;
262 let updated_post: ApubPost = blocking(context.pool(), move |conn| {
263 Post::update_stickied(conn, post_id, stickied)
269 let form = ModStickyPostForm {
270 mod_person_id: local_user_view.person.id,
271 post_id: data.post_id,
272 stickied: Some(stickied),
274 blocking(context.pool(), move |conn| {
275 ModStickyPost::create(conn, &form)
280 // TODO stickied should pry work like locked for ease of use
281 CreateOrUpdatePost::send(
283 &local_user_view.person.clone().into(),
284 CreateOrUpdateType::Update,
289 send_post_ws_message(
291 UserOperation::StickyPost,
293 Some(local_user_view.person.id),
300 #[async_trait::async_trait(?Send)]
301 impl Perform for SavePost {
302 type Response = PostResponse;
304 #[tracing::instrument(skip(context, _websocket_id))]
307 context: &Data<LemmyContext>,
308 _websocket_id: Option<ConnectionId>,
309 ) -> Result<PostResponse, LemmyError> {
310 let data: &SavePost = self;
311 let local_user_view =
312 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
314 let post_saved_form = PostSavedForm {
315 post_id: data.post_id,
316 person_id: local_user_view.person.id,
320 let save = move |conn: &'_ _| PostSaved::save(conn, &post_saved_form);
321 blocking(context.pool(), save)
323 .map_err(|e| LemmyError::from_error_message(e, "couldnt_save_post"))?;
325 let unsave = move |conn: &'_ _| PostSaved::unsave(conn, &post_saved_form);
326 blocking(context.pool(), unsave)
328 .map_err(|e| LemmyError::from_error_message(e, "couldnt_save_post"))?;
331 let post_id = data.post_id;
332 let person_id = local_user_view.person.id;
333 let post_view = blocking(context.pool(), move |conn| {
334 PostView::read(conn, post_id, Some(person_id))
338 // Mark the post as read
339 mark_post_as_read(person_id, post_id, context.pool()).await?;
341 Ok(PostResponse { post_view })
345 #[async_trait::async_trait(?Send)]
346 impl Perform for GetSiteMetadata {
347 type Response = GetSiteMetadataResponse;
349 #[tracing::instrument(skip(context, _websocket_id))]
352 context: &Data<LemmyContext>,
353 _websocket_id: Option<ConnectionId>,
354 ) -> Result<GetSiteMetadataResponse, LemmyError> {
355 let data: &Self = self;
357 let metadata = fetch_site_metadata(context.client(), &data.url).await?;
359 Ok(GetSiteMetadataResponse { metadata })