From 438414a64b29bf271fccdb757ab9bc19acac4bc1 Mon Sep 17 00:00:00 2001
From: eiknat <eiknat@dev.chapo.chat>
Date: Mon, 2 Nov 2020 22:06:20 -0500
Subject: [PATCH] add more details to the report views

---
 lemmy_api/src/comment.rs                      |  2 +-
 lemmy_api/src/community.rs                    |  1 -
 lemmy_api/src/post.rs                         |  2 +-
 lemmy_db/src/comment_report.rs                | 33 ++++++++++++++--
 lemmy_db/src/lib.rs                           |  4 +-
 lemmy_db/src/post_report.rs                   | 33 ++++++++++++++--
 .../up.sql                                    | 39 ++++++++++++++++++-
 7 files changed, 101 insertions(+), 13 deletions(-)

diff --git a/lemmy_api/src/comment.rs b/lemmy_api/src/comment.rs
index 2b0ee054..083cb841 100644
--- a/lemmy_api/src/comment.rs
+++ b/lemmy_api/src/comment.rs
@@ -775,7 +775,7 @@ impl Perform for ResolveCommentReport {
       if resolved {
         CommentReport::resolve(conn, report_id.clone(), user_id)
       } else {
-        CommentReport::unresolve(conn, report_id.clone())
+        CommentReport::unresolve(conn, report_id.clone(), user_id)
       }
     };
 
diff --git a/lemmy_api/src/community.rs b/lemmy_api/src/community.rs
index ae19c5f8..35ed4aa9 100644
--- a/lemmy_api/src/community.rs
+++ b/lemmy_api/src/community.rs
@@ -885,7 +885,6 @@ impl Perform for CommunityJoin {
   }
 }
 
-// is this the right place for this?
 #[async_trait::async_trait(?Send)]
 impl Perform for ModJoin {
   type Response = ModJoinResponse;
diff --git a/lemmy_api/src/post.rs b/lemmy_api/src/post.rs
index 0abb879c..b703c2ef 100644
--- a/lemmy_api/src/post.rs
+++ b/lemmy_api/src/post.rs
@@ -841,7 +841,7 @@ impl Perform for ResolvePostReport {
       if resolved {
         PostReport::resolve(conn, report_id.clone(), user_id)
       } else {
-        PostReport::unresolve(conn, report_id.clone())
+        PostReport::unresolve(conn, report_id.clone(), user_id)
       }
     };
 
diff --git a/lemmy_db/src/comment_report.rs b/lemmy_db/src/comment_report.rs
index a5ac701c..83f43240 100644
--- a/lemmy_db/src/comment_report.rs
+++ b/lemmy_db/src/comment_report.rs
@@ -17,9 +17,22 @@ table! {
       post_id -> Int4,
       current_comment_text -> Text,
       community_id -> Int4,
+      creator_actor_id -> Text,
       creator_name -> Varchar,
+      creator_preferred_username -> Nullable<Varchar>,
+      creator_avatar -> Nullable<Text>,
+      creator_local -> Bool,
       comment_creator_id -> Int4,
+      comment_creator_actor_id -> Text,
       comment_creator_name -> Varchar,
+      comment_creator_preferred_username -> Nullable<Varchar>,
+      comment_creator_avatar -> Nullable<Text>,
+      comment_creator_local -> Bool,
+      resolver_actor_id -> Nullable<Text>,
+      resolver_name -> Nullable<Varchar>,
+      resolver_preferred_username -> Nullable<Varchar>,
+      resolver_avatar -> Nullable<Text>,
+      resolver_local -> Nullable<Bool>,
     }
 }
 
@@ -55,22 +68,23 @@ impl Reportable<CommentReportForm> for CommentReport {
       .get_result::<Self>(conn)
   }
 
-  fn resolve(conn: &PgConnection, report_id: i32, by_user_id: i32) -> Result<usize, Error> {
+  fn resolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result<usize, Error> {
     use crate::schema::comment_report::dsl::*;
     update(comment_report.find(report_id))
       .set((
         resolved.eq(true),
-        resolver_id.eq(by_user_id),
+        resolver_id.eq(by_resolver_id),
         updated.eq(naive_now()),
       ))
       .execute(conn)
   }
 
-  fn unresolve(conn: &PgConnection, report_id: i32) -> Result<usize, Error> {
+  fn unresolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result<usize, Error> {
     use crate::schema::comment_report::dsl::*;
     update(comment_report.find(report_id))
       .set((
         resolved.eq(false),
+        resolver_id.eq(by_resolver_id),
         updated.eq(naive_now()),
       ))
       .execute(conn)
@@ -94,9 +108,22 @@ pub struct CommentReportView {
   pub post_id: i32,
   pub current_comment_text: String,
   pub community_id: i32,
+  pub creator_actor_id: String,
   pub creator_name: String,
+  pub creator_preferred_username: Option<String>,
+  pub creator_avatar: Option<String>,
+  pub creator_local: bool,
   pub comment_creator_id: i32,
+  pub comment_creator_actor_id: String,
   pub comment_creator_name: String,
+  pub comment_creator_preferred_username: Option<String>,
+  pub comment_creator_avatar: Option<String>,
+  pub comment_creator_local: bool,
+  pub resolver_actor_id: Option<String>,
+  pub resolver_name: Option<String>,
+  pub resolver_preferred_username: Option<String>,
+  pub resolver_avatar: Option<String>,
+  pub resolver_local: Option<bool>,
 }
 
 pub struct CommentReportQueryBuilder<'a> {
diff --git a/lemmy_db/src/lib.rs b/lemmy_db/src/lib.rs
index cf578c4c..d7f59d03 100644
--- a/lemmy_db/src/lib.rs
+++ b/lemmy_db/src/lib.rs
@@ -115,10 +115,10 @@ pub trait Reportable<T> {
   fn report(conn: &PgConnection, form: &T) -> Result<Self, Error>
     where
         Self: Sized;
-  fn resolve(conn: &PgConnection, report_id: i32, user_id: i32) -> Result<usize, Error>
+  fn resolve(conn: &PgConnection, report_id: i32, resolver_id: i32) -> Result<usize, Error>
     where
         Self: Sized;
-  fn unresolve(conn: &PgConnection, report_id: i32) -> Result<usize, Error>
+  fn unresolve(conn: &PgConnection, report_id: i32, resolver_id: i32) -> Result<usize, Error>
     where
         Self: Sized;
 }
diff --git a/lemmy_db/src/post_report.rs b/lemmy_db/src/post_report.rs
index 234b0e4d..61c32c0e 100644
--- a/lemmy_db/src/post_report.rs
+++ b/lemmy_db/src/post_report.rs
@@ -20,9 +20,22 @@ table! {
         current_post_url -> Nullable<Text>,
         current_post_body -> Nullable<Text>,
         community_id -> Int4,
+        creator_actor_id -> Text,
         creator_name -> Varchar,
+        creator_preferred_username -> Nullable<Varchar>,
+        creator_avatar -> Nullable<Text>,
+        creator_local -> Bool,
         post_creator_id -> Int4,
+        post_creator_actor_id -> Text,
         post_creator_name -> Varchar,
+        post_creator_preferred_username -> Nullable<Varchar>,
+        post_creator_avatar -> Nullable<Text>,
+        post_creator_local -> Bool,
+        resolver_actor_id -> Nullable<Text>,
+        resolver_name -> Nullable<Varchar>,
+        resolver_preferred_username -> Nullable<Varchar>,
+        resolver_avatar -> Nullable<Text>,
+        resolver_local -> Nullable<Bool>,
     }
 }
 
@@ -62,22 +75,23 @@ impl Reportable<PostReportForm> for PostReport {
       .get_result::<Self>(conn)
   }
 
-  fn resolve(conn: &PgConnection, report_id: i32, by_user_id: i32) -> Result<usize, Error> {
+  fn resolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result<usize, Error> {
     use crate::schema::post_report::dsl::*;
     update(post_report.find(report_id))
       .set((
         resolved.eq(true),
-        resolver_id.eq(by_user_id),
+        resolver_id.eq(by_resolver_id),
         updated.eq(naive_now()),
       ))
       .execute(conn)
   }
 
-  fn unresolve(conn: &PgConnection, report_id: i32) -> Result<usize, Error> {
+  fn unresolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result<usize, Error> {
     use crate::schema::post_report::dsl::*;
     update(post_report.find(report_id))
       .set((
         resolved.eq(false),
+        resolver_id.eq(by_resolver_id),
         updated.eq(naive_now()),
       ))
       .execute(conn)
@@ -104,9 +118,22 @@ pub struct PostReportView {
   pub current_post_url: Option<String>,
   pub current_post_body: Option<String>,
   pub community_id: i32,
+  pub creator_actor_id: String,
   pub creator_name: String,
+  pub creator_preferred_username: Option<String>,
+  pub creator_avatar: Option<String>,
+  pub creator_local: bool,
   pub post_creator_id: i32,
+  pub post_creator_actor_id: String,
   pub post_creator_name: String,
+  pub post_creator_preferred_username: Option<String>,
+  pub post_creator_avatar: Option<String>,
+  pub post_creator_local: bool,
+  pub resolver_actor_id: Option<String>,
+  pub resolver_name: Option<String>,
+  pub resolver_preferred_username: Option<String>,
+  pub resolver_avatar: Option<String>,
+  pub resolver_local: Option<bool>,
 }
 
 impl PostReportView {
diff --git a/migrations/2020-10-13-212240_create_report_tables/up.sql b/migrations/2020-10-13-212240_create_report_tables/up.sql
index 840d3d99..c0547d08 100644
--- a/migrations/2020-10-13-212240_create_report_tables/up.sql
+++ b/migrations/2020-10-13-212240_create_report_tables/up.sql
@@ -31,14 +31,31 @@ select cr.*,
 c.post_id,
 c.content as current_comment_text,
 p.community_id,
+-- report creator details
+f.actor_id as creator_actor_id,
 f.name as creator_name,
+f.preferred_username as creator_preferred_username,
+f.avatar as creator_avatar,
+f.local as creator_local,
+-- comment creator details
 u.id as comment_creator_id,
-u.name as comment_creator_name
+u.actor_id as comment_creator_actor_id,
+u.name as comment_creator_name,
+u.preferred_username as comment_creator_preferred_username,
+u.avatar as comment_creator_avatar,
+u.local as comment_creator_local,
+-- resolver details
+r.actor_id as resolver_actor_id,
+r.name as resolver_name,
+r.preferred_username as resolver_preferred_username,
+r.avatar as resolver_avatar,
+r.local as resolver_local
 from comment_report cr
 left join comment c on c.id = cr.comment_id
 left join post p on p.id = c.post_id
 left join user_ u on u.id = c.creator_id
 left join user_ f on f.id = cr.creator_id;
+left join user_ r on r.id = cr.resolver_id;
 
 create or replace view post_report_view as
 select pr.*,
@@ -47,9 +64,27 @@ p.url as current_post_url,
 p.body as current_post_body,
 p.community_id,
 f.name as creator_name,
+-- report creator details
+f.actor_id as creator_actor_id,
+f.name as creator_name,
+f.preferred_username as creator_preferred_username,
+f.avatar as creator_avatar,
+f.local as creator_local,
+-- post creator details
 u.id as post_creator_id,
-u.name as post_creator_name
+u.actor_id as post_creator_actor_id,
+u.name as post_creator_name,
+u.preferred_username as post_creator_preferred_username,
+u.avatar as post_creator_avatar,
+u.local as post_creator_local,
+-- resolver details
+r.actor_id as resolver_actor_id,
+r.name as resolver_name,
+r.preferred_username as resolver_preferred_username,
+r.avatar as resolver_avatar,
+r.local as resolver_local
 from post_report pr
 left join post p on p.id = pr.post_id
 left join user_ u on u.id = p.creator_id
 left join user_ f on f.id = pr.creator_id;
+left join user_ r on r.id = pr.resolver_id;
-- 
2.44.1