from_opt_str_to_opt_enum,
source::{
comment::Comment_,
+ community::Community_,
local_user::LocalUser_,
password_reset_request::PasswordResetRequest_,
person::Person_,
naive_now,
source::{
comment::Comment,
+ community::Community,
local_user::{LocalUser, LocalUserForm},
moderator::*,
password_reset_request::*,
};
use lemmy_db_views_actor::{
community_follower_view::CommunityFollowerView,
+ community_moderator_view::CommunityModeratorView,
person_mention_view::{PersonMentionQueryBuilder, PersonMentionView},
person_view::PersonViewSafe,
};
// Communities
// Remove all communities where they're the top mod
- // TODO couldn't get group by's working in diesel,
// for now, remove the communities manually
+ let first_mod_communities = blocking(context.pool(), move |conn: &'_ _| {
+ CommunityModeratorView::get_community_first_mods(conn)
+ })
+ .await??;
+
+ // Filter to only this banned users top communities
+ let banned_user_first_communities: Vec<CommunityModeratorView> = first_mod_communities
+ .into_iter()
+ .filter(|fmc| fmc.moderator.id == banned_person_id)
+ .collect();
+
+ for first_mod_community in banned_user_first_communities {
+ blocking(context.pool(), move |conn: &'_ _| {
+ Community::update_removed(conn, first_mod_community.community.id, true)
+ })
+ .await??;
+ }
// Comments
blocking(context.pool(), move |conn: &'_ _| {
Ok(Self::from_tuple_to_vec(res))
}
+
+ /// Finds all communities first mods / creators
+ /// Ideally this should be a group by, but diesel doesn't support it yet
+ pub fn get_community_first_mods(conn: &PgConnection) -> Result<Vec<Self>, Error> {
+ let res = community_moderator::table
+ .inner_join(community::table)
+ .inner_join(person::table)
+ .select((
+ Community::safe_columns_tuple(),
+ Person::safe_columns_tuple(),
+ ))
+ // A hacky workaround instead of group_bys
+ // https://stackoverflow.com/questions/24042359/how-to-join-only-one-row-in-joined-table-with-postgres
+ .distinct_on(community_moderator::community_id)
+ .order_by((
+ community_moderator::community_id,
+ community_moderator::person_id,
+ ))
+ .load::<CommunityModeratorViewTuple>(conn)?;
+
+ Ok(Self::from_tuple_to_vec(res))
+ }
}
impl ViewToVec for CommunityModeratorView {