use diesel::{dsl::*, pg::Pg, result::Error, *}; use serde::{Deserialize, Serialize}; use crate::{ limit_and_offset, naive_now, post::Post, schema::post_report, MaybeOptional, Reportable, }; table! { post_report_view (id) { id -> Int4, creator_id -> Int4, post_id -> Int4, original_post_name -> Varchar, original_post_url -> Nullable, original_post_body -> Nullable, reason -> Text, resolved -> Bool, resolver_id -> Nullable, published -> Timestamp, updated -> Nullable, current_post_name -> Varchar, current_post_url -> Nullable, current_post_body -> Nullable, community_id -> Int4, creator_actor_id -> Text, creator_name -> Varchar, creator_preferred_username -> Nullable, creator_avatar -> Nullable, creator_local -> Bool, post_creator_id -> Int4, post_creator_actor_id -> Text, post_creator_name -> Varchar, post_creator_preferred_username -> Nullable, post_creator_avatar -> Nullable, post_creator_local -> Bool, resolver_actor_id -> Nullable, resolver_name -> Nullable, resolver_preferred_username -> Nullable, resolver_avatar -> Nullable, resolver_local -> Nullable, } } #[derive(Identifiable, Queryable, Associations, PartialEq, Serialize, Deserialize, Debug)] #[belongs_to(Post)] #[table_name = "post_report"] pub struct PostReport { pub id: i32, pub creator_id: i32, pub post_id: i32, pub original_post_name: String, pub original_post_url: Option, pub original_post_body: Option, pub reason: String, pub resolved: bool, pub resolver_id: Option, pub published: chrono::NaiveDateTime, pub updated: Option, } #[derive(Insertable, AsChangeset, Clone)] #[table_name = "post_report"] pub struct PostReportForm { pub creator_id: i32, pub post_id: i32, pub original_post_name: String, pub original_post_url: Option, pub original_post_body: Option, pub reason: String, } impl Reportable for PostReport { /// creates a post report and returns it /// /// * `conn` - the postgres connection /// * `post_report_form` - the filled CommentReportForm to insert fn report(conn: &PgConnection, post_report_form: &PostReportForm) -> Result { use crate::schema::post_report::dsl::*; insert_into(post_report) .values(post_report_form) .get_result::(conn) } /// resolve a post report /// /// * `conn` - the postgres connection /// * `report_id` - the id of the report to resolve /// * `by_resolver_id` - the id of the user resolving the report fn resolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result { use crate::schema::post_report::dsl::*; update(post_report.find(report_id)) .set(( resolved.eq(true), resolver_id.eq(by_resolver_id), updated.eq(naive_now()), )) .execute(conn) } /// resolve a post report /// /// * `conn` - the postgres connection /// * `report_id` - the id of the report to unresolve /// * `by_resolver_id` - the id of the user unresolving the report fn unresolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result { 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) } } #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize, Clone)] #[table_name = "post_report_view"] pub struct PostReportView { pub id: i32, pub creator_id: i32, pub post_id: i32, pub original_post_name: String, pub original_post_url: Option, pub original_post_body: Option, pub reason: String, pub resolved: bool, pub resolver_id: Option, pub published: chrono::NaiveDateTime, pub updated: Option, pub current_post_name: String, pub current_post_url: Option, pub current_post_body: Option, pub community_id: i32, pub creator_actor_id: String, pub creator_name: String, pub creator_preferred_username: Option, pub creator_avatar: Option, 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, pub post_creator_avatar: Option, pub post_creator_local: bool, pub resolver_actor_id: Option, pub resolver_name: Option, pub resolver_preferred_username: Option, pub resolver_avatar: Option, pub resolver_local: Option, } impl PostReportView { /// returns the PostReportView for the provided report_id /// /// * `report_id` - the report id to obtain pub fn read(conn: &PgConnection, report_id: i32) -> Result { use super::post_report::post_report_view::dsl::*; post_report_view.find(report_id).first::(conn) } /// returns the current unresolved post report count for the supplied community ids /// /// * `community_ids` - a Vec of community_ids to get a count for pub fn get_report_count(conn: &PgConnection, community_ids: &Vec) -> Result { use super::post_report::post_report_view::dsl::*; post_report_view .filter(resolved.eq(false).and(community_id.eq_any(community_ids))) .select(count(id)) .first::(conn) } } pub struct PostReportQueryBuilder<'a> { conn: &'a PgConnection, query: post_report_view::BoxedQuery<'a, Pg>, for_community_ids: Option>, page: Option, limit: Option, resolved: Option, } impl<'a> PostReportQueryBuilder<'a> { pub fn create(conn: &'a PgConnection) -> Self { use super::post_report::post_report_view::dsl::*; let query = post_report_view.into_boxed(); PostReportQueryBuilder { conn, query, for_community_ids: None, page: None, limit: None, resolved: Some(false), } } pub fn community_ids>>(mut self, community_ids: T) -> Self { self.for_community_ids = community_ids.get_optional(); self } pub fn page>(mut self, page: T) -> Self { self.page = page.get_optional(); self } pub fn limit>(mut self, limit: T) -> Self { self.limit = limit.get_optional(); self } pub fn resolved>(mut self, resolved: T) -> Self { self.resolved = resolved.get_optional(); self } pub fn list(self) -> Result, Error> { use super::post_report::post_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)); } 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 .order_by(published.asc()) .limit(limit) .offset(offset) .load::(self.conn) } }