2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 check_downvotes_enabled,
7 get_local_user_view_from_jwt,
13 activities::{post::create_or_update::CreateOrUpdatePost, CreateOrUpdateType},
16 use lemmy_db_queries::{source::post::Post_, Crud, Likeable, Saveable};
17 use lemmy_db_schema::source::{moderator::*, post::*};
18 use lemmy_db_views::post_view::PostView;
19 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
20 use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation};
22 #[async_trait::async_trait(?Send)]
23 impl Perform for CreatePostLike {
24 type Response = PostResponse;
28 context: &Data<LemmyContext>,
29 websocket_id: Option<ConnectionId>,
30 ) -> Result<PostResponse, LemmyError> {
31 let data: &CreatePostLike = self;
32 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
34 // Don't do a downvote if site has downvotes disabled
35 check_downvotes_enabled(data.score, context.pool()).await?;
37 // Check for a community ban
38 let post_id = data.post_id;
39 let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
41 check_community_ban(local_user_view.person.id, post.community_id, context.pool()).await?;
43 let like_form = PostLikeForm {
44 post_id: data.post_id,
45 person_id: local_user_view.person.id,
49 // Remove any likes first
50 let person_id = local_user_view.person.id;
51 blocking(context.pool(), move |conn| {
52 PostLike::remove(conn, person_id, post_id)
56 // Only add the like if the score isnt 0
57 let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
59 let like_form2 = like_form.clone();
60 let like = move |conn: &'_ _| PostLike::like(conn, &like_form2);
61 if blocking(context.pool(), like).await?.is_err() {
62 return Err(ApiError::err("couldnt_like_post").into());
65 if like_form.score == 1 {
66 post.send_like(&local_user_view.person, context).await?;
67 } else if like_form.score == -1 {
68 post.send_dislike(&local_user_view.person, context).await?;
72 .send_undo_like(&local_user_view.person, context)
76 // Mark the post as read
77 mark_post_as_read(person_id, post_id, context.pool()).await?;
79 let post_id = data.post_id;
80 let person_id = local_user_view.person.id;
81 let post_view = blocking(context.pool(), move |conn| {
82 PostView::read(conn, post_id, Some(person_id))
85 .map_err(|_| ApiError::err("couldnt_find_post"))?;
87 let res = PostResponse { post_view };
89 context.chat_server().do_send(SendPost {
90 op: UserOperation::CreatePostLike,
99 #[async_trait::async_trait(?Send)]
100 impl Perform for LockPost {
101 type Response = PostResponse;
105 context: &Data<LemmyContext>,
106 websocket_id: Option<ConnectionId>,
107 ) -> Result<PostResponse, LemmyError> {
108 let data: &LockPost = self;
109 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
111 let post_id = data.post_id;
112 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
115 local_user_view.person.id,
116 orig_post.community_id,
121 // Verify that only the mods can lock
124 local_user_view.person.id,
125 orig_post.community_id,
130 let post_id = data.post_id;
131 let locked = data.locked;
132 let updated_post = blocking(context.pool(), move |conn| {
133 Post::update_locked(conn, post_id, locked)
138 let form = ModLockPostForm {
139 mod_person_id: local_user_view.person.id,
140 post_id: data.post_id,
141 locked: Some(locked),
143 blocking(context.pool(), move |conn| ModLockPost::create(conn, &form)).await??;
146 CreateOrUpdatePost::send(
148 &local_user_view.person,
149 CreateOrUpdateType::Update,
155 let post_id = data.post_id;
156 let post_view = blocking(context.pool(), move |conn| {
157 PostView::read(conn, post_id, Some(local_user_view.person.id))
161 let res = PostResponse { post_view };
163 context.chat_server().do_send(SendPost {
164 op: UserOperation::LockPost,
173 #[async_trait::async_trait(?Send)]
174 impl Perform for StickyPost {
175 type Response = PostResponse;
179 context: &Data<LemmyContext>,
180 websocket_id: Option<ConnectionId>,
181 ) -> Result<PostResponse, LemmyError> {
182 let data: &StickyPost = self;
183 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
185 let post_id = data.post_id;
186 let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
189 local_user_view.person.id,
190 orig_post.community_id,
195 // Verify that only the mods can sticky
198 local_user_view.person.id,
199 orig_post.community_id,
204 let post_id = data.post_id;
205 let stickied = data.stickied;
206 let updated_post = blocking(context.pool(), move |conn| {
207 Post::update_stickied(conn, post_id, stickied)
212 let form = ModStickyPostForm {
213 mod_person_id: local_user_view.person.id,
214 post_id: data.post_id,
215 stickied: Some(stickied),
217 blocking(context.pool(), move |conn| {
218 ModStickyPost::create(conn, &form)
223 // TODO stickied should pry work like locked for ease of use
224 CreateOrUpdatePost::send(
226 &local_user_view.person,
227 CreateOrUpdateType::Update,
233 let post_id = data.post_id;
234 let post_view = blocking(context.pool(), move |conn| {
235 PostView::read(conn, post_id, Some(local_user_view.person.id))
239 let res = PostResponse { post_view };
241 context.chat_server().do_send(SendPost {
242 op: UserOperation::StickyPost,
251 #[async_trait::async_trait(?Send)]
252 impl Perform for SavePost {
253 type Response = PostResponse;
257 context: &Data<LemmyContext>,
258 _websocket_id: Option<ConnectionId>,
259 ) -> Result<PostResponse, LemmyError> {
260 let data: &SavePost = self;
261 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
263 let post_saved_form = PostSavedForm {
264 post_id: data.post_id,
265 person_id: local_user_view.person.id,
269 let save = move |conn: &'_ _| PostSaved::save(conn, &post_saved_form);
270 if blocking(context.pool(), save).await?.is_err() {
271 return Err(ApiError::err("couldnt_save_post").into());
274 let unsave = move |conn: &'_ _| PostSaved::unsave(conn, &post_saved_form);
275 if blocking(context.pool(), unsave).await?.is_err() {
276 return Err(ApiError::err("couldnt_save_post").into());
280 let post_id = data.post_id;
281 let person_id = local_user_view.person.id;
282 let post_view = blocking(context.pool(), move |conn| {
283 PostView::read(conn, post_id, Some(person_id))
287 // Mark the post as read
288 mark_post_as_read(person_id, post_id, context.pool()).await?;
290 Ok(PostResponse { post_view })