From 313f0467c89f2e01a420ff4de2f958bcf631015a Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 16 Dec 2020 16:28:18 -0500 Subject: [PATCH] Adding moderator views. --- lemmy_api/src/community.rs | 8 +- lemmy_api/src/lib.rs | 2 +- lemmy_api/src/post.rs | 3 +- lemmy_api/src/site.rs | 14 +- lemmy_api/src/user.rs | 6 +- .../src/activities/receive/community.rs | 6 +- lemmy_apub/src/activities/send/community.rs | 2 +- lemmy_apub/src/fetcher.rs | 2 +- lemmy_apub/src/http/community.rs | 2 +- lemmy_apub/src/inbox/community_inbox.rs | 2 +- lemmy_apub/src/objects/community.rs | 2 +- lemmy_db/src/lib.rs | 1 - lemmy_db/src/moderator_views.rs | 513 ------------------ lemmy_db/src/source/community.rs | 2 +- lemmy_db/src/source/moderator.rs | 19 +- .../community_follower_view.rs | 0 .../community_moderator_view.rs | 0 .../community_user_ban_view.rs | 0 .../views/{ => community}/community_view.rs | 0 lemmy_db/src/views/community/mod.rs | 4 + lemmy_db/src/views/mod.rs | 6 +- lemmy_db/src/views/moderator/mod.rs | 9 + .../views/moderator/mod_add_community_view.rs | 78 +++ lemmy_db/src/views/moderator/mod_add_view.rs | 68 +++ .../moderator/mod_ban_from_community_view.rs | 78 +++ lemmy_db/src/views/moderator/mod_ban_view.rs | 68 +++ .../src/views/moderator/mod_lock_post_view.rs | 79 +++ .../moderator/mod_remove_comment_view.rs | 95 ++++ .../moderator/mod_remove_community_view.rs | 69 +++ .../views/moderator/mod_remove_post_view.rs | 79 +++ .../views/moderator/mod_sticky_post_view.rs | 79 +++ lemmy_structs/src/community.rs | 8 +- lemmy_structs/src/post.rs | 3 +- lemmy_structs/src/site.rs | 14 +- lemmy_structs/src/user.rs | 6 +- 35 files changed, 774 insertions(+), 553 deletions(-) delete mode 100644 lemmy_db/src/moderator_views.rs rename lemmy_db/src/views/{ => community}/community_follower_view.rs (100%) rename lemmy_db/src/views/{ => community}/community_moderator_view.rs (100%) rename lemmy_db/src/views/{ => community}/community_user_ban_view.rs (100%) rename lemmy_db/src/views/{ => community}/community_view.rs (100%) create mode 100644 lemmy_db/src/views/community/mod.rs create mode 100644 lemmy_db/src/views/moderator/mod.rs create mode 100644 lemmy_db/src/views/moderator/mod_add_community_view.rs create mode 100644 lemmy_db/src/views/moderator/mod_add_view.rs create mode 100644 lemmy_db/src/views/moderator/mod_ban_from_community_view.rs create mode 100644 lemmy_db/src/views/moderator/mod_ban_view.rs create mode 100644 lemmy_db/src/views/moderator/mod_lock_post_view.rs create mode 100644 lemmy_db/src/views/moderator/mod_remove_comment_view.rs create mode 100644 lemmy_db/src/views/moderator/mod_remove_community_view.rs create mode 100644 lemmy_db/src/views/moderator/mod_remove_post_view.rs create mode 100644 lemmy_db/src/views/moderator/mod_sticky_post_view.rs diff --git a/lemmy_api/src/community.rs b/lemmy_api/src/community.rs index 04059a7c5..6e20a30ba 100644 --- a/lemmy_api/src/community.rs +++ b/lemmy_api/src/community.rs @@ -15,9 +15,11 @@ use lemmy_db::{ source::{comment::Comment, community::*, moderator::*, post::Post, site::*}, views::{ comment_view::CommentQueryBuilder, - community_follower_view::CommunityFollowerView, - community_moderator_view::CommunityModeratorView, - community_view::{CommunityQueryBuilder, CommunityView}, + community::{ + community_follower_view::CommunityFollowerView, + community_moderator_view::CommunityModeratorView, + community_view::{CommunityQueryBuilder, CommunityView}, + }, user_view::UserViewSafe, }, ApubObject, diff --git a/lemmy_api/src/lib.rs b/lemmy_api/src/lib.rs index 92287f8d3..ad7355e1c 100644 --- a/lemmy_api/src/lib.rs +++ b/lemmy_api/src/lib.rs @@ -6,7 +6,7 @@ use lemmy_db::{ post::Post, user::User_, }, - views::community_user_ban_view::CommunityUserBanView, + views::community::community_user_ban_view::CommunityUserBanView, Crud, DbPool, }; diff --git a/lemmy_api/src/post.rs b/lemmy_api/src/post.rs index d998e7d97..02da229fd 100644 --- a/lemmy_api/src/post.rs +++ b/lemmy_api/src/post.rs @@ -15,8 +15,7 @@ use lemmy_db::{ source::{moderator::*, post::*}, views::{ comment_view::CommentQueryBuilder, - community_moderator_view::CommunityModeratorView, - community_view::CommunityView, + community::{community_moderator_view::CommunityModeratorView, community_view::CommunityView}, post_view::{PostQueryBuilder, PostView}, site_view::SiteView, }, diff --git a/lemmy_api/src/site.rs b/lemmy_api/src/site.rs index e8dfaca32..138cc8751 100644 --- a/lemmy_api/src/site.rs +++ b/lemmy_api/src/site.rs @@ -12,12 +12,22 @@ use lemmy_apub::fetcher::search_by_apub_id; use lemmy_db::{ aggregates::site_aggregates::SiteAggregates, diesel_option_overwrite, - moderator_views::*, naive_now, source::{category::*, moderator::*, site::*}, views::{ comment_view::CommentQueryBuilder, - community_view::CommunityQueryBuilder, + community::community_view::CommunityQueryBuilder, + moderator::{ + mod_add_community_view::ModAddCommunityView, + mod_add_view::ModAddView, + mod_ban_from_community_view::ModBanFromCommunityView, + mod_ban_view::ModBanView, + mod_lock_post_view::ModLockPostView, + mod_remove_comment_view::ModRemoveCommentView, + mod_remove_community_view::ModRemoveCommunityView, + mod_remove_post_view::ModRemovePostView, + mod_sticky_post_view::ModStickyPostView, + }, post_view::PostQueryBuilder, site_view::SiteView, user_view::{UserQueryBuilder, UserViewSafe}, diff --git a/lemmy_api/src/user.rs b/lemmy_api/src/user.rs index 17e3fac65..c47a5e1ce 100644 --- a/lemmy_api/src/user.rs +++ b/lemmy_api/src/user.rs @@ -33,8 +33,10 @@ use lemmy_db::{ }, views::{ comment_view::CommentQueryBuilder, - community_follower_view::CommunityFollowerView, - community_moderator_view::CommunityModeratorView, + community::{ + community_follower_view::CommunityFollowerView, + community_moderator_view::CommunityModeratorView, + }, post_view::PostQueryBuilder, site_view::SiteView, user_mention_view::{UserMentionQueryBuilder, UserMentionView}, diff --git a/lemmy_apub/src/activities/receive/community.rs b/lemmy_apub/src/activities/receive/community.rs index cacb54eef..534da5cb7 100644 --- a/lemmy_apub/src/activities/receive/community.rs +++ b/lemmy_apub/src/activities/receive/community.rs @@ -4,7 +4,11 @@ use activitystreams::{ base::{AnyBase, ExtendsExt}, }; use anyhow::Context; -use lemmy_db::{source::community::Community, views::community_view::CommunityView, ApubObject}; +use lemmy_db::{ + source::community::Community, + views::community::community_view::CommunityView, + ApubObject, +}; use lemmy_structs::{blocking, community::CommunityResponse}; use lemmy_utils::{location_info, LemmyError}; use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation}; diff --git a/lemmy_apub/src/activities/send/community.rs b/lemmy_apub/src/activities/send/community.rs index 8596fc4e3..96152fa0d 100644 --- a/lemmy_apub/src/activities/send/community.rs +++ b/lemmy_apub/src/activities/send/community.rs @@ -25,7 +25,7 @@ use anyhow::Context; use itertools::Itertools; use lemmy_db::{ source::community::Community, - views::community_follower_view::CommunityFollowerView, + views::community::community_follower_view::CommunityFollowerView, DbPool, }; use lemmy_structs::blocking; diff --git a/lemmy_apub/src/fetcher.rs b/lemmy_apub/src/fetcher.rs index 08735b4c2..61cdbd47a 100644 --- a/lemmy_apub/src/fetcher.rs +++ b/lemmy_apub/src/fetcher.rs @@ -22,7 +22,7 @@ use lemmy_db::{ }, views::{ comment_view::CommentView, - community_view::CommunityView, + community::community_view::CommunityView, post_view::PostView, user_view::UserViewSafe, }, diff --git a/lemmy_apub/src/http/community.rs b/lemmy_apub/src/http/community.rs index 3caaf6613..a17e2abf9 100644 --- a/lemmy_apub/src/http/community.rs +++ b/lemmy_apub/src/http/community.rs @@ -11,7 +11,7 @@ use activitystreams::{ use actix_web::{body::Body, web, HttpResponse}; use lemmy_db::{ source::{community::Community, post::Post}, - views::community_follower_view::CommunityFollowerView, + views::community::community_follower_view::CommunityFollowerView, }; use lemmy_structs::blocking; use lemmy_utils::LemmyError; diff --git a/lemmy_apub/src/inbox/community_inbox.rs b/lemmy_apub/src/inbox/community_inbox.rs index 82df26bea..a2bed621c 100644 --- a/lemmy_apub/src/inbox/community_inbox.rs +++ b/lemmy_apub/src/inbox/community_inbox.rs @@ -31,7 +31,7 @@ use lemmy_db::{ community::{Community, CommunityFollower, CommunityFollowerForm}, user::User_, }, - views::community_user_ban_view::CommunityUserBanView, + views::community::community_user_ban_view::CommunityUserBanView, ApubObject, DbPool, Followable, diff --git a/lemmy_apub/src/objects/community.rs b/lemmy_apub/src/objects/community.rs index 3bd47560e..9d8210a6a 100644 --- a/lemmy_apub/src/objects/community.rs +++ b/lemmy_apub/src/objects/community.rs @@ -25,7 +25,7 @@ use anyhow::Context; use lemmy_db::{ naive_now, source::community::{Community, CommunityForm}, - views::community_moderator_view::CommunityModeratorView, + views::community::community_moderator_view::CommunityModeratorView, DbPool, }; use lemmy_structs::blocking; diff --git a/lemmy_db/src/lib.rs b/lemmy_db/src/lib.rs index ba5dccdf6..8906f32c4 100644 --- a/lemmy_db/src/lib.rs +++ b/lemmy_db/src/lib.rs @@ -12,7 +12,6 @@ use serde::{Deserialize, Serialize}; use std::{env, env::VarError}; pub mod comment_report; -pub mod moderator_views; pub mod post_report; pub mod private_message_view; diff --git a/lemmy_db/src/moderator_views.rs b/lemmy_db/src/moderator_views.rs deleted file mode 100644 index efa949a4a..000000000 --- a/lemmy_db/src/moderator_views.rs +++ /dev/null @@ -1,513 +0,0 @@ -use crate::limit_and_offset; -use diesel::{result::Error, *}; -use serde::Serialize; - -table! { - mod_remove_post_view (id) { - id -> Int4, - mod_user_id -> Int4, - post_id -> Int4, - reason -> Nullable, - removed -> Nullable, - when_ -> Timestamp, - mod_user_name -> Varchar, - post_name -> Varchar, - community_id -> Int4, - community_name -> Varchar, - } -} - -#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] -#[table_name = "mod_remove_post_view"] -pub struct ModRemovePostView { - pub id: i32, - pub mod_user_id: i32, - pub post_id: i32, - pub reason: Option, - pub removed: Option, - pub when_: chrono::NaiveDateTime, - pub mod_user_name: String, - pub post_name: String, - pub community_id: i32, - pub community_name: String, -} - -impl ModRemovePostView { - pub fn list( - conn: &PgConnection, - from_community_id: Option, - from_mod_user_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { - use super::moderator_views::mod_remove_post_view::dsl::*; - let mut query = mod_remove_post_view.into_boxed(); - - let (limit, offset) = limit_and_offset(page, limit); - - if let Some(from_community_id) = from_community_id { - query = query.filter(community_id.eq(from_community_id)); - }; - - if let Some(from_mod_user_id) = from_mod_user_id { - query = query.filter(mod_user_id.eq(from_mod_user_id)); - }; - - query - .limit(limit) - .offset(offset) - .order_by(when_.desc()) - .load::(conn) - } -} - -table! { - mod_lock_post_view (id) { - id -> Int4, - mod_user_id -> Int4, - post_id -> Int4, - locked -> Nullable, - when_ -> Timestamp, - mod_user_name -> Varchar, - post_name -> Varchar, - community_id -> Int4, - community_name -> Varchar, - } -} - -#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] -#[table_name = "mod_lock_post_view"] -pub struct ModLockPostView { - pub id: i32, - pub mod_user_id: i32, - pub post_id: i32, - pub locked: Option, - pub when_: chrono::NaiveDateTime, - pub mod_user_name: String, - pub post_name: String, - pub community_id: i32, - pub community_name: String, -} - -impl ModLockPostView { - pub fn list( - conn: &PgConnection, - from_community_id: Option, - from_mod_user_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { - use super::moderator_views::mod_lock_post_view::dsl::*; - let mut query = mod_lock_post_view.into_boxed(); - - let (limit, offset) = limit_and_offset(page, limit); - - if let Some(from_community_id) = from_community_id { - query = query.filter(community_id.eq(from_community_id)); - }; - - if let Some(from_mod_user_id) = from_mod_user_id { - query = query.filter(mod_user_id.eq(from_mod_user_id)); - }; - - query - .limit(limit) - .offset(offset) - .order_by(when_.desc()) - .load::(conn) - } -} - -table! { - mod_sticky_post_view (id) { - id -> Int4, - mod_user_id -> Int4, - post_id -> Int4, - stickied -> Nullable, - when_ -> Timestamp, - mod_user_name -> Varchar, - post_name -> Varchar, - community_id -> Int4, - community_name -> Varchar, - } -} - -#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] -#[table_name = "mod_sticky_post_view"] -pub struct ModStickyPostView { - pub id: i32, - pub mod_user_id: i32, - pub post_id: i32, - pub stickied: Option, - pub when_: chrono::NaiveDateTime, - pub mod_user_name: String, - pub post_name: String, - pub community_id: i32, - pub community_name: String, -} - -impl ModStickyPostView { - pub fn list( - conn: &PgConnection, - from_community_id: Option, - from_mod_user_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { - use super::moderator_views::mod_sticky_post_view::dsl::*; - let mut query = mod_sticky_post_view.into_boxed(); - - let (limit, offset) = limit_and_offset(page, limit); - - if let Some(from_community_id) = from_community_id { - query = query.filter(community_id.eq(from_community_id)); - }; - - if let Some(from_mod_user_id) = from_mod_user_id { - query = query.filter(mod_user_id.eq(from_mod_user_id)); - }; - - query - .limit(limit) - .offset(offset) - .order_by(when_.desc()) - .load::(conn) - } -} - -table! { - mod_remove_comment_view (id) { - id -> Int4, - mod_user_id -> Int4, - comment_id -> Int4, - reason -> Nullable, - removed -> Nullable, - when_ -> Timestamp, - mod_user_name -> Varchar, - comment_user_id -> Int4, - comment_user_name -> Varchar, - comment_content -> Text, - post_id -> Int4, - post_name -> Varchar, - community_id -> Int4, - community_name -> Varchar, - } -} - -#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] -#[table_name = "mod_remove_comment_view"] -pub struct ModRemoveCommentView { - pub id: i32, - pub mod_user_id: i32, - pub comment_id: i32, - pub reason: Option, - pub removed: Option, - pub when_: chrono::NaiveDateTime, - pub mod_user_name: String, - pub comment_user_id: i32, - pub comment_user_name: String, - pub comment_content: String, - pub post_id: i32, - pub post_name: String, - pub community_id: i32, - pub community_name: String, -} - -impl ModRemoveCommentView { - pub fn list( - conn: &PgConnection, - from_community_id: Option, - from_mod_user_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { - use super::moderator_views::mod_remove_comment_view::dsl::*; - let mut query = mod_remove_comment_view.into_boxed(); - - let (limit, offset) = limit_and_offset(page, limit); - - if let Some(from_community_id) = from_community_id { - query = query.filter(community_id.eq(from_community_id)); - }; - - if let Some(from_mod_user_id) = from_mod_user_id { - query = query.filter(mod_user_id.eq(from_mod_user_id)); - }; - - query - .limit(limit) - .offset(offset) - .order_by(when_.desc()) - .load::(conn) - } -} - -table! { - mod_remove_community_view (id) { - id -> Int4, - mod_user_id -> Int4, - community_id -> Int4, - reason -> Nullable, - removed -> Nullable, - expires -> Nullable, - when_ -> Timestamp, - mod_user_name -> Varchar, - community_name -> Varchar, - } -} - -#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] -#[table_name = "mod_remove_community_view"] -pub struct ModRemoveCommunityView { - pub id: i32, - pub mod_user_id: i32, - pub community_id: i32, - pub reason: Option, - pub removed: Option, - pub expires: Option, - pub when_: chrono::NaiveDateTime, - pub mod_user_name: String, - pub community_name: String, -} - -impl ModRemoveCommunityView { - pub fn list( - conn: &PgConnection, - from_mod_user_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { - use super::moderator_views::mod_remove_community_view::dsl::*; - let mut query = mod_remove_community_view.into_boxed(); - - let (limit, offset) = limit_and_offset(page, limit); - - if let Some(from_mod_user_id) = from_mod_user_id { - query = query.filter(mod_user_id.eq(from_mod_user_id)); - }; - - query - .limit(limit) - .offset(offset) - .order_by(when_.desc()) - .load::(conn) - } -} - -table! { - mod_ban_from_community_view (id) { - id -> Int4, - mod_user_id -> Int4, - other_user_id -> Int4, - community_id -> Int4, - reason -> Nullable, - banned -> Nullable, - expires -> Nullable, - when_ -> Timestamp, - mod_user_name -> Varchar, - other_user_name -> Varchar, - community_name -> Varchar, - } -} - -#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] -#[table_name = "mod_ban_from_community_view"] -pub struct ModBanFromCommunityView { - pub id: i32, - pub mod_user_id: i32, - pub other_user_id: i32, - pub community_id: i32, - pub reason: Option, - pub banned: Option, - pub expires: Option, - pub when_: chrono::NaiveDateTime, - pub mod_user_name: String, - pub other_user_name: String, - pub community_name: String, -} - -impl ModBanFromCommunityView { - pub fn list( - conn: &PgConnection, - from_community_id: Option, - from_mod_user_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { - use super::moderator_views::mod_ban_from_community_view::dsl::*; - let mut query = mod_ban_from_community_view.into_boxed(); - - let (limit, offset) = limit_and_offset(page, limit); - - if let Some(from_community_id) = from_community_id { - query = query.filter(community_id.eq(from_community_id)); - }; - - if let Some(from_mod_user_id) = from_mod_user_id { - query = query.filter(mod_user_id.eq(from_mod_user_id)); - }; - - query - .limit(limit) - .offset(offset) - .order_by(when_.desc()) - .load::(conn) - } -} - -table! { - mod_ban_view (id) { - id -> Int4, - mod_user_id -> Int4, - other_user_id -> Int4, - reason -> Nullable, - banned -> Nullable, - expires -> Nullable, - when_ -> Timestamp, - mod_user_name -> Varchar, - other_user_name -> Varchar, - } -} - -#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] -#[table_name = "mod_ban_view"] -pub struct ModBanView { - pub id: i32, - pub mod_user_id: i32, - pub other_user_id: i32, - pub reason: Option, - pub banned: Option, - pub expires: Option, - pub when_: chrono::NaiveDateTime, - pub mod_user_name: String, - pub other_user_name: String, -} - -impl ModBanView { - pub fn list( - conn: &PgConnection, - from_mod_user_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { - use super::moderator_views::mod_ban_view::dsl::*; - let mut query = mod_ban_view.into_boxed(); - - let (limit, offset) = limit_and_offset(page, limit); - - if let Some(from_mod_user_id) = from_mod_user_id { - query = query.filter(mod_user_id.eq(from_mod_user_id)); - }; - - query - .limit(limit) - .offset(offset) - .order_by(when_.desc()) - .load::(conn) - } -} - -table! { - mod_add_community_view (id) { - id -> Int4, - mod_user_id -> Int4, - other_user_id -> Int4, - community_id -> Int4, - removed -> Nullable, - when_ -> Timestamp, - mod_user_name -> Varchar, - other_user_name -> Varchar, - community_name -> Varchar, - } -} - -#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] -#[table_name = "mod_add_community_view"] -pub struct ModAddCommunityView { - pub id: i32, - pub mod_user_id: i32, - pub other_user_id: i32, - pub community_id: i32, - pub removed: Option, - pub when_: chrono::NaiveDateTime, - pub mod_user_name: String, - pub other_user_name: String, - pub community_name: String, -} - -impl ModAddCommunityView { - pub fn list( - conn: &PgConnection, - from_community_id: Option, - from_mod_user_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { - use super::moderator_views::mod_add_community_view::dsl::*; - let mut query = mod_add_community_view.into_boxed(); - - let (limit, offset) = limit_and_offset(page, limit); - - if let Some(from_community_id) = from_community_id { - query = query.filter(community_id.eq(from_community_id)); - }; - - if let Some(from_mod_user_id) = from_mod_user_id { - query = query.filter(mod_user_id.eq(from_mod_user_id)); - }; - - query - .limit(limit) - .offset(offset) - .order_by(when_.desc()) - .load::(conn) - } -} - -table! { - mod_add_view (id) { - id -> Int4, - mod_user_id -> Int4, - other_user_id -> Int4, - removed -> Nullable, - when_ -> Timestamp, - mod_user_name -> Varchar, - other_user_name -> Varchar, - } -} - -#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, QueryableByName, Clone)] -#[table_name = "mod_add_view"] -pub struct ModAddView { - pub id: i32, - pub mod_user_id: i32, - pub other_user_id: i32, - pub removed: Option, - pub when_: chrono::NaiveDateTime, - pub mod_user_name: String, - pub other_user_name: String, -} - -impl ModAddView { - pub fn list( - conn: &PgConnection, - from_mod_user_id: Option, - page: Option, - limit: Option, - ) -> Result, Error> { - use super::moderator_views::mod_add_view::dsl::*; - let mut query = mod_add_view.into_boxed(); - - let (limit, offset) = limit_and_offset(page, limit); - - if let Some(from_mod_user_id) = from_mod_user_id { - query = query.filter(mod_user_id.eq(from_mod_user_id)); - }; - - query - .limit(limit) - .offset(offset) - .order_by(when_.desc()) - .load::(conn) - } -} diff --git a/lemmy_db/src/source/community.rs b/lemmy_db/src/source/community.rs index 84db0c7c4..0ad90da28 100644 --- a/lemmy_db/src/source/community.rs +++ b/lemmy_db/src/source/community.rs @@ -1,6 +1,7 @@ use crate::{ naive_now, schema::{community, community_follower, community_moderator, community_user_ban}, + views::{community::community_moderator_view::CommunityModeratorView, user_view::UserViewSafe}, ApubObject, Bannable, Crud, @@ -223,7 +224,6 @@ impl Community { } fn community_mods_and_admins(conn: &PgConnection, community_id: i32) -> Result, Error> { - use crate::views::{community_moderator_view::CommunityModeratorView, user_view::UserViewSafe}; let mut mods_and_admins: Vec = Vec::new(); mods_and_admins.append( &mut CommunityModeratorView::for_community(conn, community_id) diff --git a/lemmy_db/src/source/moderator.rs b/lemmy_db/src/source/moderator.rs index 1be3e31b8..766c17fc5 100644 --- a/lemmy_db/src/source/moderator.rs +++ b/lemmy_db/src/source/moderator.rs @@ -13,8 +13,9 @@ use crate::{ Crud, }; use diesel::{dsl::*, result::Error, *}; +use serde::Serialize; -#[derive(Queryable, Identifiable, PartialEq, Debug)] +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "mod_remove_post"] pub struct ModRemovePost { pub id: i32, @@ -55,7 +56,7 @@ impl Crud for ModRemovePost { } } -#[derive(Queryable, Identifiable, PartialEq, Debug)] +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "mod_lock_post"] pub struct ModLockPost { pub id: i32, @@ -94,7 +95,7 @@ impl Crud for ModLockPost { } } -#[derive(Queryable, Identifiable, PartialEq, Debug)] +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "mod_sticky_post"] pub struct ModStickyPost { pub id: i32, @@ -133,7 +134,7 @@ impl Crud for ModStickyPost { } } -#[derive(Queryable, Identifiable, PartialEq, Debug)] +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "mod_remove_comment"] pub struct ModRemoveComment { pub id: i32, @@ -174,7 +175,7 @@ impl Crud for ModRemoveComment { } } -#[derive(Queryable, Identifiable, PartialEq, Debug)] +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "mod_remove_community"] pub struct ModRemoveCommunity { pub id: i32, @@ -221,7 +222,7 @@ impl Crud for ModRemoveCommunity { } } -#[derive(Queryable, Identifiable, PartialEq, Debug)] +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "mod_ban_from_community"] pub struct ModBanFromCommunity { pub id: i32, @@ -270,7 +271,7 @@ impl Crud for ModBanFromCommunity { } } -#[derive(Queryable, Identifiable, PartialEq, Debug)] +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "mod_ban"] pub struct ModBan { pub id: i32, @@ -311,7 +312,7 @@ impl Crud for ModBan { } } -#[derive(Queryable, Identifiable, PartialEq, Debug)] +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "mod_add_community"] pub struct ModAddCommunity { pub id: i32, @@ -352,7 +353,7 @@ impl Crud for ModAddCommunity { } } -#[derive(Queryable, Identifiable, PartialEq, Debug)] +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "mod_add"] pub struct ModAdd { pub id: i32, diff --git a/lemmy_db/src/views/community_follower_view.rs b/lemmy_db/src/views/community/community_follower_view.rs similarity index 100% rename from lemmy_db/src/views/community_follower_view.rs rename to lemmy_db/src/views/community/community_follower_view.rs diff --git a/lemmy_db/src/views/community_moderator_view.rs b/lemmy_db/src/views/community/community_moderator_view.rs similarity index 100% rename from lemmy_db/src/views/community_moderator_view.rs rename to lemmy_db/src/views/community/community_moderator_view.rs diff --git a/lemmy_db/src/views/community_user_ban_view.rs b/lemmy_db/src/views/community/community_user_ban_view.rs similarity index 100% rename from lemmy_db/src/views/community_user_ban_view.rs rename to lemmy_db/src/views/community/community_user_ban_view.rs diff --git a/lemmy_db/src/views/community_view.rs b/lemmy_db/src/views/community/community_view.rs similarity index 100% rename from lemmy_db/src/views/community_view.rs rename to lemmy_db/src/views/community/community_view.rs diff --git a/lemmy_db/src/views/community/mod.rs b/lemmy_db/src/views/community/mod.rs new file mode 100644 index 000000000..491dde7f5 --- /dev/null +++ b/lemmy_db/src/views/community/mod.rs @@ -0,0 +1,4 @@ +pub mod community_follower_view; +pub mod community_moderator_view; +pub mod community_user_ban_view; +pub mod community_view; diff --git a/lemmy_db/src/views/mod.rs b/lemmy_db/src/views/mod.rs index 2516caeb0..28323fca2 100644 --- a/lemmy_db/src/views/mod.rs +++ b/lemmy_db/src/views/mod.rs @@ -1,8 +1,6 @@ pub mod comment_view; -pub mod community_follower_view; -pub mod community_moderator_view; -pub mod community_user_ban_view; -pub mod community_view; +pub mod community; +pub mod moderator; pub mod post_view; pub mod site_view; pub mod user_mention_view; diff --git a/lemmy_db/src/views/moderator/mod.rs b/lemmy_db/src/views/moderator/mod.rs new file mode 100644 index 000000000..827dd1447 --- /dev/null +++ b/lemmy_db/src/views/moderator/mod.rs @@ -0,0 +1,9 @@ +pub mod mod_add_community_view; +pub mod mod_add_view; +pub mod mod_ban_from_community_view; +pub mod mod_ban_view; +pub mod mod_lock_post_view; +pub mod mod_remove_comment_view; +pub mod mod_remove_community_view; +pub mod mod_remove_post_view; +pub mod mod_sticky_post_view; diff --git a/lemmy_db/src/views/moderator/mod_add_community_view.rs b/lemmy_db/src/views/moderator/mod_add_community_view.rs new file mode 100644 index 000000000..402c5fe1b --- /dev/null +++ b/lemmy_db/src/views/moderator/mod_add_community_view.rs @@ -0,0 +1,78 @@ +use crate::{ + limit_and_offset, + schema::{community, mod_add_community, user_, user_alias_1}, + source::{ + community::{Community, CommunitySafe}, + moderator::ModAddCommunity, + user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, + }, + views::ViewToVec, + ToSafe, +}; +use diesel::{result::Error, *}; +use serde::Serialize; + +#[derive(Debug, Serialize, Clone)] +pub struct ModAddCommunityView { + pub mod_add_community: ModAddCommunity, + pub moderator: UserSafe, + pub community: CommunitySafe, + pub modded_user: UserSafeAlias1, +} + +type ModAddCommunityViewTuple = (ModAddCommunity, UserSafe, CommunitySafe, UserSafeAlias1); + +impl ModAddCommunityView { + pub fn list( + conn: &PgConnection, + community_id: Option, + mod_user_id: Option, + page: Option, + limit: Option, + ) -> Result, Error> { + let mut query = mod_add_community::table + .inner_join(user_::table.on(mod_add_community::mod_user_id.eq(user_::id))) + .inner_join(community::table) + .inner_join(user_alias_1::table.on(mod_add_community::other_user_id.eq(user_::id))) + .select(( + mod_add_community::all_columns, + User_::safe_columns_tuple(), + Community::safe_columns_tuple(), + UserAlias1::safe_columns_tuple(), + )) + .into_boxed(); + + if let Some(mod_user_id) = mod_user_id { + query = query.filter(mod_add_community::mod_user_id.eq(mod_user_id)); + }; + + if let Some(community_id) = community_id { + query = query.filter(mod_add_community::community_id.eq(community_id)); + }; + + let (limit, offset) = limit_and_offset(page, limit); + + let res = query + .limit(limit) + .offset(offset) + .order_by(mod_add_community::when_.desc()) + .load::(conn)?; + + Ok(Self::to_vec(res)) + } +} + +impl ViewToVec for ModAddCommunityView { + type DbTuple = ModAddCommunityViewTuple; + fn to_vec(mrp: Vec) -> Vec { + mrp + .iter() + .map(|a| Self { + mod_add_community: a.0.to_owned(), + moderator: a.1.to_owned(), + community: a.2.to_owned(), + modded_user: a.3.to_owned(), + }) + .collect::>() + } +} diff --git a/lemmy_db/src/views/moderator/mod_add_view.rs b/lemmy_db/src/views/moderator/mod_add_view.rs new file mode 100644 index 000000000..fc1993d45 --- /dev/null +++ b/lemmy_db/src/views/moderator/mod_add_view.rs @@ -0,0 +1,68 @@ +use crate::{ + limit_and_offset, + schema::{mod_add, user_, user_alias_1}, + source::{ + moderator::ModAdd, + user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, + }, + views::ViewToVec, + ToSafe, +}; +use diesel::{result::Error, *}; +use serde::Serialize; + +#[derive(Debug, Serialize, Clone)] +pub struct ModAddView { + pub mod_add: ModAdd, + pub moderator: UserSafe, + pub modded_user: UserSafeAlias1, +} + +type ModAddViewTuple = (ModAdd, UserSafe, UserSafeAlias1); + +impl ModAddView { + pub fn list( + conn: &PgConnection, + mod_user_id: Option, + page: Option, + limit: Option, + ) -> Result, Error> { + let mut query = mod_add::table + .inner_join(user_::table.on(mod_add::mod_user_id.eq(user_::id))) + .inner_join(user_alias_1::table.on(mod_add::other_user_id.eq(user_::id))) + .select(( + mod_add::all_columns, + User_::safe_columns_tuple(), + UserAlias1::safe_columns_tuple(), + )) + .into_boxed(); + + if let Some(mod_user_id) = mod_user_id { + query = query.filter(mod_add::mod_user_id.eq(mod_user_id)); + }; + + let (limit, offset) = limit_and_offset(page, limit); + + let res = query + .limit(limit) + .offset(offset) + .order_by(mod_add::when_.desc()) + .load::(conn)?; + + Ok(Self::to_vec(res)) + } +} + +impl ViewToVec for ModAddView { + type DbTuple = ModAddViewTuple; + fn to_vec(mrp: Vec) -> Vec { + mrp + .iter() + .map(|a| Self { + mod_add: a.0.to_owned(), + moderator: a.1.to_owned(), + modded_user: a.2.to_owned(), + }) + .collect::>() + } +} diff --git a/lemmy_db/src/views/moderator/mod_ban_from_community_view.rs b/lemmy_db/src/views/moderator/mod_ban_from_community_view.rs new file mode 100644 index 000000000..6ad232e87 --- /dev/null +++ b/lemmy_db/src/views/moderator/mod_ban_from_community_view.rs @@ -0,0 +1,78 @@ +use crate::{ + limit_and_offset, + schema::{community, mod_ban_from_community, user_, user_alias_1}, + source::{ + community::{Community, CommunitySafe}, + moderator::ModBanFromCommunity, + user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, + }, + views::ViewToVec, + ToSafe, +}; +use diesel::{result::Error, *}; +use serde::Serialize; + +#[derive(Debug, Serialize, Clone)] +pub struct ModBanFromCommunityView { + pub mod_ban_from_community: ModBanFromCommunity, + pub moderator: UserSafe, + pub community: CommunitySafe, + pub banned_user: UserSafeAlias1, +} + +type ModBanFromCommunityViewTuple = (ModBanFromCommunity, UserSafe, CommunitySafe, UserSafeAlias1); + +impl ModBanFromCommunityView { + pub fn list( + conn: &PgConnection, + community_id: Option, + mod_user_id: Option, + page: Option, + limit: Option, + ) -> Result, Error> { + let mut query = mod_ban_from_community::table + .inner_join(user_::table.on(mod_ban_from_community::mod_user_id.eq(user_::id))) + .inner_join(community::table) + .inner_join(user_alias_1::table.on(mod_ban_from_community::other_user_id.eq(user_::id))) + .select(( + mod_ban_from_community::all_columns, + User_::safe_columns_tuple(), + Community::safe_columns_tuple(), + UserAlias1::safe_columns_tuple(), + )) + .into_boxed(); + + if let Some(mod_user_id) = mod_user_id { + query = query.filter(mod_ban_from_community::mod_user_id.eq(mod_user_id)); + }; + + if let Some(community_id) = community_id { + query = query.filter(mod_ban_from_community::community_id.eq(community_id)); + }; + + let (limit, offset) = limit_and_offset(page, limit); + + let res = query + .limit(limit) + .offset(offset) + .order_by(mod_ban_from_community::when_.desc()) + .load::(conn)?; + + Ok(Self::to_vec(res)) + } +} + +impl ViewToVec for ModBanFromCommunityView { + type DbTuple = ModBanFromCommunityViewTuple; + fn to_vec(mrp: Vec) -> Vec { + mrp + .iter() + .map(|a| Self { + mod_ban_from_community: a.0.to_owned(), + moderator: a.1.to_owned(), + community: a.2.to_owned(), + banned_user: a.3.to_owned(), + }) + .collect::>() + } +} diff --git a/lemmy_db/src/views/moderator/mod_ban_view.rs b/lemmy_db/src/views/moderator/mod_ban_view.rs new file mode 100644 index 000000000..28214d2da --- /dev/null +++ b/lemmy_db/src/views/moderator/mod_ban_view.rs @@ -0,0 +1,68 @@ +use crate::{ + limit_and_offset, + schema::{mod_ban, user_, user_alias_1}, + source::{ + moderator::ModBan, + user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, + }, + views::ViewToVec, + ToSafe, +}; +use diesel::{result::Error, *}; +use serde::Serialize; + +#[derive(Debug, Serialize, Clone)] +pub struct ModBanView { + pub mod_ban: ModBan, + pub moderator: UserSafe, + pub banned_user: UserSafeAlias1, +} + +type ModBanViewTuple = (ModBan, UserSafe, UserSafeAlias1); + +impl ModBanView { + pub fn list( + conn: &PgConnection, + mod_user_id: Option, + page: Option, + limit: Option, + ) -> Result, Error> { + let mut query = mod_ban::table + .inner_join(user_::table.on(mod_ban::mod_user_id.eq(user_::id))) + .inner_join(user_alias_1::table.on(mod_ban::other_user_id.eq(user_::id))) + .select(( + mod_ban::all_columns, + User_::safe_columns_tuple(), + UserAlias1::safe_columns_tuple(), + )) + .into_boxed(); + + if let Some(mod_user_id) = mod_user_id { + query = query.filter(mod_ban::mod_user_id.eq(mod_user_id)); + }; + + let (limit, offset) = limit_and_offset(page, limit); + + let res = query + .limit(limit) + .offset(offset) + .order_by(mod_ban::when_.desc()) + .load::(conn)?; + + Ok(Self::to_vec(res)) + } +} + +impl ViewToVec for ModBanView { + type DbTuple = ModBanViewTuple; + fn to_vec(mrp: Vec) -> Vec { + mrp + .iter() + .map(|a| Self { + mod_ban: a.0.to_owned(), + moderator: a.1.to_owned(), + banned_user: a.2.to_owned(), + }) + .collect::>() + } +} diff --git a/lemmy_db/src/views/moderator/mod_lock_post_view.rs b/lemmy_db/src/views/moderator/mod_lock_post_view.rs new file mode 100644 index 000000000..8182b54f7 --- /dev/null +++ b/lemmy_db/src/views/moderator/mod_lock_post_view.rs @@ -0,0 +1,79 @@ +use crate::{ + limit_and_offset, + schema::{community, mod_lock_post, post, user_}, + source::{ + community::{Community, CommunitySafe}, + moderator::ModLockPost, + post::Post, + user::{UserSafe, User_}, + }, + views::ViewToVec, + ToSafe, +}; +use diesel::{result::Error, *}; +use serde::Serialize; + +#[derive(Debug, Serialize, Clone)] +pub struct ModLockPostView { + pub mod_lock_post: ModLockPost, + pub moderator: UserSafe, + pub post: Post, + pub community: CommunitySafe, +} + +type ModLockPostViewTuple = (ModLockPost, UserSafe, Post, CommunitySafe); + +impl ModLockPostView { + pub fn list( + conn: &PgConnection, + community_id: Option, + mod_user_id: Option, + page: Option, + limit: Option, + ) -> Result, Error> { + let mut query = mod_lock_post::table + .inner_join(user_::table) + .inner_join(post::table) + .inner_join(community::table.on(post::community_id.eq(community::id))) + .select(( + mod_lock_post::all_columns, + User_::safe_columns_tuple(), + post::all_columns, + Community::safe_columns_tuple(), + )) + .into_boxed(); + + if let Some(community_id) = community_id { + query = query.filter(post::community_id.eq(community_id)); + }; + + if let Some(mod_user_id) = mod_user_id { + query = query.filter(mod_lock_post::mod_user_id.eq(mod_user_id)); + }; + + let (limit, offset) = limit_and_offset(page, limit); + + let res = query + .limit(limit) + .offset(offset) + .order_by(mod_lock_post::when_.desc()) + .load::(conn)?; + + Ok(Self::to_vec(res)) + } +} + +impl ViewToVec for ModLockPostView { + type DbTuple = ModLockPostViewTuple; + fn to_vec(mrp: Vec) -> Vec { + mrp + .iter() + .map(|a| Self { + mod_lock_post: a.0.to_owned(), + moderator: a.1.to_owned(), + post: a.2.to_owned(), + community: a.3.to_owned(), + }) + .collect::>() + } +} diff --git a/lemmy_db/src/views/moderator/mod_remove_comment_view.rs b/lemmy_db/src/views/moderator/mod_remove_comment_view.rs new file mode 100644 index 000000000..fb4b77724 --- /dev/null +++ b/lemmy_db/src/views/moderator/mod_remove_comment_view.rs @@ -0,0 +1,95 @@ +use crate::{ + limit_and_offset, + schema::{comment, community, mod_remove_comment, post, user_, user_alias_1}, + source::{ + comment::Comment, + community::{Community, CommunitySafe}, + moderator::ModRemoveComment, + post::Post, + user::{UserAlias1, UserSafe, UserSafeAlias1, User_}, + }, + views::ViewToVec, + ToSafe, +}; +use diesel::{result::Error, *}; +use serde::Serialize; + +#[derive(Debug, Serialize, Clone)] +pub struct ModRemoveCommentView { + pub mod_remove_comment: ModRemoveComment, + pub moderator: UserSafe, + pub comment: Comment, + pub commenter: UserSafeAlias1, + pub post: Post, + pub community: CommunitySafe, +} + +type ModRemoveCommentViewTuple = ( + ModRemoveComment, + UserSafe, + Comment, + UserSafeAlias1, + Post, + CommunitySafe, +); + +impl ModRemoveCommentView { + pub fn list( + conn: &PgConnection, + community_id: Option, + mod_user_id: Option, + page: Option, + limit: Option, + ) -> Result, Error> { + let mut query = mod_remove_comment::table + .inner_join(user_::table) + .inner_join(comment::table) + .inner_join(user_alias_1::table.on(comment::creator_id.eq(user_alias_1::id))) + .inner_join(post::table.on(comment::post_id.eq(post::id))) + .inner_join(community::table.on(post::community_id.eq(community::id))) + .select(( + mod_remove_comment::all_columns, + User_::safe_columns_tuple(), + comment::all_columns, + UserAlias1::safe_columns_tuple(), + post::all_columns, + Community::safe_columns_tuple(), + )) + .into_boxed(); + + if let Some(community_id) = community_id { + query = query.filter(post::community_id.eq(community_id)); + }; + + if let Some(mod_user_id) = mod_user_id { + query = query.filter(mod_remove_comment::mod_user_id.eq(mod_user_id)); + }; + + let (limit, offset) = limit_and_offset(page, limit); + + let res = query + .limit(limit) + .offset(offset) + .order_by(mod_remove_comment::when_.desc()) + .load::(conn)?; + + Ok(Self::to_vec(res)) + } +} + +impl ViewToVec for ModRemoveCommentView { + type DbTuple = ModRemoveCommentViewTuple; + fn to_vec(mrp: Vec) -> Vec { + mrp + .iter() + .map(|a| Self { + mod_remove_comment: a.0.to_owned(), + moderator: a.1.to_owned(), + comment: a.2.to_owned(), + commenter: a.3.to_owned(), + post: a.4.to_owned(), + community: a.5.to_owned(), + }) + .collect::>() + } +} diff --git a/lemmy_db/src/views/moderator/mod_remove_community_view.rs b/lemmy_db/src/views/moderator/mod_remove_community_view.rs new file mode 100644 index 000000000..daaf6d78f --- /dev/null +++ b/lemmy_db/src/views/moderator/mod_remove_community_view.rs @@ -0,0 +1,69 @@ +use crate::{ + limit_and_offset, + schema::{community, mod_remove_community, user_}, + source::{ + community::{Community, CommunitySafe}, + moderator::ModRemoveCommunity, + user::{UserSafe, User_}, + }, + views::ViewToVec, + ToSafe, +}; +use diesel::{result::Error, *}; +use serde::Serialize; + +#[derive(Debug, Serialize, Clone)] +pub struct ModRemoveCommunityView { + pub mod_remove_community: ModRemoveCommunity, + pub moderator: UserSafe, + pub community: CommunitySafe, +} + +type ModRemoveCommunityTuple = (ModRemoveCommunity, UserSafe, CommunitySafe); + +impl ModRemoveCommunityView { + pub fn list( + conn: &PgConnection, + mod_user_id: Option, + page: Option, + limit: Option, + ) -> Result, Error> { + let mut query = mod_remove_community::table + .inner_join(user_::table) + .inner_join(community::table) + .select(( + mod_remove_community::all_columns, + User_::safe_columns_tuple(), + Community::safe_columns_tuple(), + )) + .into_boxed(); + + if let Some(mod_user_id) = mod_user_id { + query = query.filter(mod_remove_community::mod_user_id.eq(mod_user_id)); + }; + + let (limit, offset) = limit_and_offset(page, limit); + + let res = query + .limit(limit) + .offset(offset) + .order_by(mod_remove_community::when_.desc()) + .load::(conn)?; + + Ok(Self::to_vec(res)) + } +} + +impl ViewToVec for ModRemoveCommunityView { + type DbTuple = ModRemoveCommunityTuple; + fn to_vec(mrp: Vec) -> Vec { + mrp + .iter() + .map(|a| Self { + mod_remove_community: a.0.to_owned(), + moderator: a.1.to_owned(), + community: a.2.to_owned(), + }) + .collect::>() + } +} diff --git a/lemmy_db/src/views/moderator/mod_remove_post_view.rs b/lemmy_db/src/views/moderator/mod_remove_post_view.rs new file mode 100644 index 000000000..613a8a541 --- /dev/null +++ b/lemmy_db/src/views/moderator/mod_remove_post_view.rs @@ -0,0 +1,79 @@ +use crate::{ + limit_and_offset, + schema::{community, mod_remove_post, post, user_}, + source::{ + community::{Community, CommunitySafe}, + moderator::ModRemovePost, + post::Post, + user::{UserSafe, User_}, + }, + views::ViewToVec, + ToSafe, +}; +use diesel::{result::Error, *}; +use serde::Serialize; + +#[derive(Debug, Serialize, Clone)] +pub struct ModRemovePostView { + pub mod_remove_post: ModRemovePost, + pub moderator: UserSafe, + pub post: Post, + pub community: CommunitySafe, +} + +type ModRemovePostViewTuple = (ModRemovePost, UserSafe, Post, CommunitySafe); + +impl ModRemovePostView { + pub fn list( + conn: &PgConnection, + community_id: Option, + mod_user_id: Option, + page: Option, + limit: Option, + ) -> Result, Error> { + let mut query = mod_remove_post::table + .inner_join(user_::table) + .inner_join(post::table) + .inner_join(community::table.on(post::community_id.eq(community::id))) + .select(( + mod_remove_post::all_columns, + User_::safe_columns_tuple(), + post::all_columns, + Community::safe_columns_tuple(), + )) + .into_boxed(); + + if let Some(community_id) = community_id { + query = query.filter(post::community_id.eq(community_id)); + }; + + if let Some(mod_user_id) = mod_user_id { + query = query.filter(mod_remove_post::mod_user_id.eq(mod_user_id)); + }; + + let (limit, offset) = limit_and_offset(page, limit); + + let res = query + .limit(limit) + .offset(offset) + .order_by(mod_remove_post::when_.desc()) + .load::(conn)?; + + Ok(Self::to_vec(res)) + } +} + +impl ViewToVec for ModRemovePostView { + type DbTuple = ModRemovePostViewTuple; + fn to_vec(mrp: Vec) -> Vec { + mrp + .iter() + .map(|a| Self { + mod_remove_post: a.0.to_owned(), + moderator: a.1.to_owned(), + post: a.2.to_owned(), + community: a.3.to_owned(), + }) + .collect::>() + } +} diff --git a/lemmy_db/src/views/moderator/mod_sticky_post_view.rs b/lemmy_db/src/views/moderator/mod_sticky_post_view.rs new file mode 100644 index 000000000..9a3d118b9 --- /dev/null +++ b/lemmy_db/src/views/moderator/mod_sticky_post_view.rs @@ -0,0 +1,79 @@ +use crate::{ + limit_and_offset, + schema::{community, mod_sticky_post, post, user_}, + source::{ + community::{Community, CommunitySafe}, + moderator::ModStickyPost, + post::Post, + user::{UserSafe, User_}, + }, + views::ViewToVec, + ToSafe, +}; +use diesel::{result::Error, *}; +use serde::Serialize; + +#[derive(Debug, Serialize, Clone)] +pub struct ModStickyPostView { + pub mod_sticky_post: ModStickyPost, + pub moderator: UserSafe, + pub post: Post, + pub community: CommunitySafe, +} + +type ModStickyPostViewTuple = (ModStickyPost, UserSafe, Post, CommunitySafe); + +impl ModStickyPostView { + pub fn list( + conn: &PgConnection, + community_id: Option, + mod_user_id: Option, + page: Option, + limit: Option, + ) -> Result, Error> { + let mut query = mod_sticky_post::table + .inner_join(user_::table) + .inner_join(post::table) + .inner_join(community::table.on(post::community_id.eq(community::id))) + .select(( + mod_sticky_post::all_columns, + User_::safe_columns_tuple(), + post::all_columns, + Community::safe_columns_tuple(), + )) + .into_boxed(); + + if let Some(community_id) = community_id { + query = query.filter(post::community_id.eq(community_id)); + }; + + if let Some(mod_user_id) = mod_user_id { + query = query.filter(mod_sticky_post::mod_user_id.eq(mod_user_id)); + }; + + let (limit, offset) = limit_and_offset(page, limit); + + let res = query + .limit(limit) + .offset(offset) + .order_by(mod_sticky_post::when_.desc()) + .load::(conn)?; + + Ok(Self::to_vec(res)) + } +} + +impl ViewToVec for ModStickyPostView { + type DbTuple = ModStickyPostViewTuple; + fn to_vec(mrp: Vec) -> Vec { + mrp + .iter() + .map(|a| Self { + mod_sticky_post: a.0.to_owned(), + moderator: a.1.to_owned(), + post: a.2.to_owned(), + community: a.3.to_owned(), + }) + .collect::>() + } +} diff --git a/lemmy_structs/src/community.rs b/lemmy_structs/src/community.rs index c107084bb..ac7837c52 100644 --- a/lemmy_structs/src/community.rs +++ b/lemmy_structs/src/community.rs @@ -1,7 +1,9 @@ use lemmy_db::views::{ - community_follower_view::CommunityFollowerView, - community_moderator_view::CommunityModeratorView, - community_view::CommunityView, + community::{ + community_follower_view::CommunityFollowerView, + community_moderator_view::CommunityModeratorView, + community_view::CommunityView, + }, user_view::UserViewSafe, }; use serde::{Deserialize, Serialize}; diff --git a/lemmy_structs/src/post.rs b/lemmy_structs/src/post.rs index eea107a7e..bf0af2f8a 100644 --- a/lemmy_structs/src/post.rs +++ b/lemmy_structs/src/post.rs @@ -2,8 +2,7 @@ use lemmy_db::{ post_report::PostReportView, views::{ comment_view::CommentView, - community_moderator_view::CommunityModeratorView, - community_view::CommunityView, + community::{community_moderator_view::CommunityModeratorView, community_view::CommunityView}, post_view::PostView, }, }; diff --git a/lemmy_structs/src/site.rs b/lemmy_structs/src/site.rs index f32b84134..9209a5420 100644 --- a/lemmy_structs/src/site.rs +++ b/lemmy_structs/src/site.rs @@ -1,10 +1,20 @@ use lemmy_db::{ aggregates::site_aggregates::SiteAggregates, - moderator_views::*, source::{category::*, user::*}, views::{ comment_view::CommentView, - community_view::CommunityView, + community::community_view::CommunityView, + moderator::{ + mod_add_community_view::ModAddCommunityView, + mod_add_view::ModAddView, + mod_ban_from_community_view::ModBanFromCommunityView, + mod_ban_view::ModBanView, + mod_lock_post_view::ModLockPostView, + mod_remove_comment_view::ModRemoveCommentView, + mod_remove_community_view::ModRemoveCommunityView, + mod_remove_post_view::ModRemovePostView, + mod_sticky_post_view::ModStickyPostView, + }, post_view::PostView, site_view::SiteView, user_view::UserViewSafe, diff --git a/lemmy_structs/src/user.rs b/lemmy_structs/src/user.rs index 9ebb14f4c..1bd320cd0 100644 --- a/lemmy_structs/src/user.rs +++ b/lemmy_structs/src/user.rs @@ -2,8 +2,10 @@ use lemmy_db::{ private_message_view::PrivateMessageView, views::{ comment_view::CommentView, - community_follower_view::CommunityFollowerView, - community_moderator_view::CommunityModeratorView, + community::{ + community_follower_view::CommunityFollowerView, + community_moderator_view::CommunityModeratorView, + }, post_view::PostView, user_mention_view::UserMentionView, user_view::{UserViewDangerous, UserViewSafe},