From ceae7eb47aad4a6bf477ecd56165151b55c4defc Mon Sep 17 00:00:00 2001
From: Dessalines <tyhou13@gmx.com>
Date: Wed, 6 Jan 2021 16:02:08 -0500
Subject: [PATCH] Private message query debugging.

---
 lemmy_api/src/comment.rs                   | 38 +++++++++++++++-------
 lemmy_db_views/src/private_message_view.rs | 15 ++++++---
 2 files changed, 37 insertions(+), 16 deletions(-)

diff --git a/lemmy_api/src/comment.rs b/lemmy_api/src/comment.rs
index 840600cb..fca5eb5d 100644
--- a/lemmy_api/src/comment.rs
+++ b/lemmy_api/src/comment.rs
@@ -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,
diff --git a/lemmy_db_views/src/private_message_view.rs b/lemmy_db_views/src/private_message_view.rs
index 709b2551..578af80e 100644
--- a/lemmy_db_views/src/private_message_view.rs
+++ b/lemmy_db_views/src/private_message_view.rs
@@ -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))
   }
-- 
2.44.1