lemmy/lemmy_db/src/comment_report.rs

213 lines
6.1 KiB
Rust
Raw Normal View History

use diesel::{dsl::*, pg::Pg, result::Error, *};
2020-10-13 19:32:35 -04:00
use serde::{Deserialize, Serialize};
use crate::{limit_and_offset, MaybeOptional, schema::comment_report, comment::Comment, Reportable, naive_now};
2020-10-13 19:32:35 -04:00
table! {
comment_report_view (id) {
id -> Int4,
creator_id -> Int4,
2020-10-13 19:32:35 -04:00
comment_id -> Int4,
comment_text -> Text,
reason -> Text,
resolved -> Bool,
resolver_id -> Nullable<Int4>,
published -> Timestamp,
updated -> Nullable<Timestamp>,
2020-10-13 19:32:35 -04:00
post_id -> Int4,
2020-10-24 23:24:50 -04:00
current_comment_text -> Text,
2020-10-13 19:32:35 -04:00
community_id -> Int4,
2020-11-02 22:06:20 -05:00
creator_actor_id -> Text,
2020-10-13 19:32:35 -04:00
creator_name -> Varchar,
2020-11-02 22:06:20 -05:00
creator_preferred_username -> Nullable<Varchar>,
creator_avatar -> Nullable<Text>,
creator_local -> Bool,
comment_creator_id -> Int4,
2020-11-02 22:06:20 -05:00
comment_creator_actor_id -> Text,
comment_creator_name -> Varchar,
2020-11-02 22:06:20 -05:00
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>,
2020-10-13 19:32:35 -04:00
}
}
2020-10-26 20:25:18 -04:00
#[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Serialize)]
2020-10-13 19:32:35 -04:00
#[belongs_to(Comment)]
#[table_name = "comment_report"]
pub struct CommentReport {
pub id: i32,
pub creator_id: i32,
2020-10-13 19:32:35 -04:00
pub comment_id: i32,
pub comment_text: String,
pub reason: String,
pub resolved: bool,
pub resolver_id: Option<i32>,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
2020-10-13 19:32:35 -04:00
}
#[derive(Insertable, AsChangeset, Clone)]
#[table_name = "comment_report"]
pub struct CommentReportForm {
pub creator_id: i32,
2020-10-13 19:32:35 -04:00
pub comment_id: i32,
pub comment_text: String,
pub reason: String,
2020-10-13 19:32:35 -04:00
}
impl Reportable<CommentReportForm> for CommentReport {
fn report(conn: &PgConnection, comment_report_form: &CommentReportForm) -> Result<Self, Error> {
use crate::schema::comment_report::dsl::*;
insert_into(comment_report)
.values(comment_report_form)
.get_result::<Self>(conn)
2020-10-13 19:32:35 -04:00
}
2020-11-02 22:06:20 -05:00
fn resolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result<usize, Error> {
2020-10-13 19:32:35 -04:00
use crate::schema::comment_report::dsl::*;
update(comment_report.find(report_id))
.set((
resolved.eq(true),
2020-11-02 22:06:20 -05:00
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
))
.execute(conn)
}
2020-11-02 22:06:20 -05:00
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),
2020-11-02 22:06:20 -05:00
resolver_id.eq(by_resolver_id),
updated.eq(naive_now()),
))
.execute(conn)
2020-10-13 19:32:35 -04:00
}
}
#[derive(
Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, Clone,
2020-10-13 19:32:35 -04:00
)]
#[table_name = "comment_report_view"]
pub struct CommentReportView {
pub id: i32,
pub creator_id: i32,
2020-10-13 19:32:35 -04:00
pub comment_id: i32,
pub comment_text: String,
pub reason: String,
pub resolved: bool,
pub resolver_id: Option<i32>,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
2020-10-13 19:32:35 -04:00
pub post_id: i32,
2020-10-24 23:24:50 -04:00
pub current_comment_text: String,
2020-10-13 19:32:35 -04:00
pub community_id: i32,
2020-11-02 22:06:20 -05:00
pub creator_actor_id: String,
2020-10-13 19:32:35 -04:00
pub creator_name: String,
2020-11-02 22:06:20 -05:00
pub creator_preferred_username: Option<String>,
pub creator_avatar: Option<String>,
pub creator_local: bool,
pub comment_creator_id: i32,
2020-11-02 22:06:20 -05:00
pub comment_creator_actor_id: String,
pub comment_creator_name: String,
2020-11-02 22:06:20 -05:00
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>,
2020-10-13 19:32:35 -04:00
}
pub struct CommentReportQueryBuilder<'a> {
conn: &'a PgConnection,
query: comment_report_view::BoxedQuery<'a, Pg>,
for_community_ids: Option<Vec<i32>>,
2020-10-13 19:32:35 -04:00
page: Option<i64>,
limit: Option<i64>,
resolved: Option<bool>,
}
impl CommentReportView {
pub fn read(conn: &PgConnection, report_id: i32) -> Result<Self, Error> {
2020-10-13 19:32:35 -04:00
use super::comment_report::comment_report_view::dsl::*;
comment_report_view
2020-10-20 21:37:37 -04:00
.find(report_id)
2020-10-13 19:32:35 -04:00
.first::<Self>(conn)
}
pub fn get_report_count(conn: &PgConnection, community_ids: &Vec<i32>) -> Result<i32, Error> {
use super::comment_report::comment_report_view::dsl::*;
comment_report_view
.filter(resolved.eq(false).and(community_id.eq_any(community_ids)))
.select(sql::<sql_types::Integer>("COUNT(*)"))
.first::<i32>(conn)
}
2020-10-13 19:32:35 -04:00
}
impl<'a> CommentReportQueryBuilder<'a> {
pub fn create(conn: &'a PgConnection) -> Self {
use super::comment_report::comment_report_view::dsl::*;
let query = comment_report_view.into_boxed();
CommentReportQueryBuilder {
conn,
query,
for_community_ids: None,
2020-10-13 19:32:35 -04:00
page: None,
limit: None,
resolved: Some(false),
}
}
pub fn community_ids<T: MaybeOptional<Vec<i32>>>(mut self, community_ids: T) -> Self {
self.for_community_ids = community_ids.get_optional();
2020-10-13 19:32:35 -04:00
self
}
pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
self.page = page.get_optional();
self
}
pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
self.limit = limit.get_optional();
self
}
pub fn resolved<T: MaybeOptional<bool>>(mut self, resolved: T) -> Self {
self.resolved = resolved.get_optional();
self
}
pub fn list(self) -> Result<Vec<CommentReportView>, Error> {
use super::comment_report::comment_report_view::dsl::*;
let mut query = self.query;
if let Some(comm_ids) = self.for_community_ids {
query = query.filter(community_id.eq_any(comm_ids));
2020-10-13 19:32:35 -04:00
}
if let Some(resolved_flag) = self.resolved {
query = query.filter(resolved.eq(resolved_flag));
}
let (limit, offset) = limit_and_offset(self.page, self.limit);
query
2020-10-20 21:37:37 -04:00
.order_by(published.asc())
2020-10-13 19:32:35 -04:00
.limit(limit)
.offset(offset)
.load::<CommentReportView>(self.conn)
}
}