-
-
Notifications
You must be signed in to change notification settings - Fork 887
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add community reports (only the database part) (#4996)
* database stuff, not including tests * change migration date * fix community_report_view * update stuff related to report_combined * add db_schema/src/impls/community_report.rs * add report counts to community_aggregates * fix community_report columns and update report_combined_view::tests::test_combined * add column for original sidebar; use None instead of clone; add report_combined_view::tests::test_community_reports * use ts(optional) in CommunityReportView * remove CommunityReportView::read
- Loading branch information
1 parent
11e0513
commit 4d17eef
Showing
12 changed files
with
443 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use crate::{ | ||
newtypes::{CommunityId, CommunityReportId, PersonId}, | ||
schema::community_report::{ | ||
community_id, | ||
dsl::{community_report, resolved, resolver_id, updated}, | ||
}, | ||
source::community_report::{CommunityReport, CommunityReportForm}, | ||
traits::Reportable, | ||
utils::{get_conn, DbPool}, | ||
}; | ||
use chrono::Utc; | ||
use diesel::{ | ||
dsl::{insert_into, update}, | ||
result::Error, | ||
ExpressionMethods, | ||
QueryDsl, | ||
}; | ||
use diesel_async::RunQueryDsl; | ||
|
||
#[async_trait] | ||
impl Reportable for CommunityReport { | ||
type Form = CommunityReportForm; | ||
type IdType = CommunityReportId; | ||
type ObjectIdType = CommunityId; | ||
/// creates a community report and returns it | ||
/// | ||
/// * `conn` - the postgres connection | ||
/// * `community_report_form` - the filled CommunityReportForm to insert | ||
async fn report( | ||
pool: &mut DbPool<'_>, | ||
community_report_form: &CommunityReportForm, | ||
) -> Result<Self, Error> { | ||
let conn = &mut get_conn(pool).await?; | ||
insert_into(community_report) | ||
.values(community_report_form) | ||
.get_result::<Self>(conn) | ||
.await | ||
} | ||
|
||
/// resolve a community 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 | ||
async fn resolve( | ||
pool: &mut DbPool<'_>, | ||
report_id_: Self::IdType, | ||
by_resolver_id: PersonId, | ||
) -> Result<usize, Error> { | ||
let conn = &mut get_conn(pool).await?; | ||
update(community_report.find(report_id_)) | ||
.set(( | ||
resolved.eq(true), | ||
resolver_id.eq(by_resolver_id), | ||
updated.eq(Utc::now()), | ||
)) | ||
.execute(conn) | ||
.await | ||
} | ||
|
||
async fn resolve_all_for_object( | ||
pool: &mut DbPool<'_>, | ||
community_id_: CommunityId, | ||
by_resolver_id: PersonId, | ||
) -> Result<usize, Error> { | ||
let conn = &mut get_conn(pool).await?; | ||
update(community_report.filter(community_id.eq(community_id_))) | ||
.set(( | ||
resolved.eq(true), | ||
resolver_id.eq(by_resolver_id), | ||
updated.eq(Utc::now()), | ||
)) | ||
.execute(conn) | ||
.await | ||
} | ||
|
||
/// unresolve a community 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 | ||
async fn unresolve( | ||
pool: &mut DbPool<'_>, | ||
report_id_: Self::IdType, | ||
by_resolver_id: PersonId, | ||
) -> Result<usize, Error> { | ||
let conn = &mut get_conn(pool).await?; | ||
update(community_report.find(report_id_)) | ||
.set(( | ||
resolved.eq(false), | ||
resolver_id.eq(by_resolver_id), | ||
updated.eq(Utc::now()), | ||
)) | ||
.execute(conn) | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::newtypes::{CommunityId, CommunityReportId, DbUrl, PersonId}; | ||
#[cfg(feature = "full")] | ||
use crate::schema::community_report; | ||
use chrono::{DateTime, Utc}; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_with::skip_serializing_none; | ||
#[cfg(feature = "full")] | ||
use ts_rs::TS; | ||
|
||
#[skip_serializing_none] | ||
#[derive(PartialEq, Eq, Serialize, Deserialize, Debug, Clone)] | ||
#[cfg_attr( | ||
feature = "full", | ||
derive(Queryable, Selectable, Associations, Identifiable, TS) | ||
)] | ||
#[cfg_attr( | ||
feature = "full", | ||
diesel(belongs_to(crate::source::community::Community)) | ||
)] | ||
#[cfg_attr(feature = "full", diesel(table_name = community_report))] | ||
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] | ||
#[cfg_attr(feature = "full", ts(export))] | ||
/// A comment report. | ||
pub struct CommunityReport { | ||
pub id: CommunityReportId, | ||
pub creator_id: PersonId, | ||
pub community_id: CommunityId, | ||
pub original_community_name: String, | ||
pub original_community_title: String, | ||
#[cfg_attr(feature = "full", ts(optional))] | ||
pub original_community_description: Option<String>, | ||
#[cfg_attr(feature = "full", ts(optional))] | ||
pub original_community_sidebar: Option<String>, | ||
#[cfg_attr(feature = "full", ts(optional))] | ||
pub original_community_icon: Option<String>, | ||
#[cfg_attr(feature = "full", ts(optional))] | ||
pub original_community_banner: Option<String>, | ||
pub reason: String, | ||
pub resolved: bool, | ||
#[cfg_attr(feature = "full", ts(optional))] | ||
pub resolver_id: Option<PersonId>, | ||
pub published: DateTime<Utc>, | ||
#[cfg_attr(feature = "full", ts(optional))] | ||
pub updated: Option<DateTime<Utc>>, | ||
} | ||
|
||
#[derive(Clone)] | ||
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] | ||
#[cfg_attr(feature = "full", diesel(table_name = community_report))] | ||
pub struct CommunityReportForm { | ||
pub creator_id: PersonId, | ||
pub community_id: CommunityId, | ||
pub original_community_name: String, | ||
pub original_community_title: String, | ||
pub original_community_description: Option<String>, | ||
pub original_community_sidebar: Option<String>, | ||
pub original_community_icon: Option<DbUrl>, | ||
pub original_community_banner: Option<DbUrl>, | ||
pub reason: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.