]> Untitled Git - lemmy.git/commitdiff
Private message query debugging.
authorDessalines <tyhou13@gmx.com>
Wed, 6 Jan 2021 21:02:08 +0000 (16:02 -0500)
committerDessalines <tyhou13@gmx.com>
Wed, 6 Jan 2021 21:02:08 +0000 (16:02 -0500)
lemmy_api/src/comment.rs
lemmy_db_views/src/private_message_view.rs

index 840600cbc9700c88cc3c6ae75809b6ff4fdcb33c..fca5eb5dea1dad7d2a06eb4f0b1aa63b1574e577 100644 (file)
@@ -53,6 +53,17 @@ impl Perform for CreateComment {
 
     let content_slurs_removed = remove_slurs(&data.content.to_owned());
 
+    // Check for a community ban
+    let post_id = data.post_id;
+    let post = get_post(post_id, context.pool()).await?;
+
+    check_community_ban(user.id, post.community_id, context.pool()).await?;
+
+    // Check if post is locked, no new comments
+    if post.locked {
+      return Err(APIError::err("locked").into());
+    }
+
     let comment_form = CommentForm {
       content: content_slurs_removed,
       parent_id: data.parent_id.to_owned(),
@@ -67,17 +78,6 @@ impl Perform for CreateComment {
       local: true,
     };
 
-    // Check for a community ban
-    let post_id = data.post_id;
-    let post = get_post(post_id, context.pool()).await?;
-
-    check_community_ban(user.id, post.community_id, context.pool()).await?;
-
-    // Check if post is locked, no new comments
-    if post.locked {
-      return Err(APIError::err("locked").into());
-    }
-
     // Create the comment
     let comment_form2 = comment_form.clone();
     let inserted_comment = match blocking(context.pool(), move |conn| {
@@ -133,11 +133,25 @@ impl Perform for CreateComment {
     updated_comment.send_like(&user, context).await?;
 
     let user_id = user.id;
-    let comment_view = blocking(context.pool(), move |conn| {
+    let mut comment_view = blocking(context.pool(), move |conn| {
       CommentView::read(&conn, inserted_comment.id, Some(user_id))
     })
     .await??;
 
+    // If its a comment to yourself, mark it as read
+    let comment_id = comment_view.comment.id;
+    if user.id == comment_view.get_recipient_id() {
+      match blocking(context.pool(), move |conn| {
+        Comment::update_read(conn, comment_id, true)
+      })
+      .await?
+      {
+        Ok(comment) => comment,
+        Err(_e) => return Err(APIError::err("couldnt_update_comment").into()),
+      };
+      comment_view.comment.read = true;
+    }
+
     let mut res = CommentResponse {
       comment_view,
       recipient_ids,
index 709b2551fbae2a4601ebf70d0af5aa968cdafd7a..578af80e9946707e9d2ff39ff39cf2b1ab6c08aa 100644 (file)
@@ -1,4 +1,4 @@
-use diesel::{result::Error, *};
+use diesel::{pg::Pg, result::Error, *};
 use lemmy_db_queries::{limit_and_offset, MaybeOptional, ToSafe, ViewToVec};
 use lemmy_db_schema::{
   schema::{private_message, user_, user_alias_1},
@@ -7,6 +7,7 @@ use lemmy_db_schema::{
     user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
   },
 };
+use log::debug;
 use serde::Serialize;
 
 #[derive(Debug, PartialEq, Serialize, Clone)]
@@ -102,12 +103,18 @@ impl<'a> PrivateMessageQueryBuilder<'a> {
 
     let (limit, offset) = limit_and_offset(self.page, self.limit);
 
-    let res = query
+    query = query
       .filter(private_message::deleted.eq(false))
       .limit(limit)
       .offset(offset)
-      .order_by(private_message::published.desc())
-      .load::<PrivateMessageViewTuple>(self.conn)?;
+      .order_by(private_message::published.desc());
+
+    debug!(
+      "Private Message View Query: {:?}",
+      debug_query::<Pg, _>(&query)
+    );
+
+    let res = query.load::<PrivateMessageViewTuple>(self.conn)?;
 
     Ok(PrivateMessageView::from_tuple_to_vec(res))
   }