mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-14 13:01:52 +00:00
Adding other community views.
This commit is contained in:
parent
caedb7fcc4
commit
5e510e7a67
4 changed files with 136 additions and 0 deletions
50
lemmy_db/src/views/community_follower_view.rs
Normal file
50
lemmy_db/src/views/community_follower_view.rs
Normal file
|
@ -0,0 +1,50 @@
|
|||
use crate::{
|
||||
community::{Community, CommunitySafe},
|
||||
schema::{community, community_follower, user_},
|
||||
user::{UserSafe, User_},
|
||||
ToSafe,
|
||||
};
|
||||
use diesel::{result::Error, *};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct CommunityFollowerView {
|
||||
pub community: CommunitySafe,
|
||||
pub follower: UserSafe,
|
||||
}
|
||||
|
||||
impl CommunityFollowerView {
|
||||
pub fn for_community(conn: &PgConnection, for_community_id: i32) -> Result<Vec<Self>, Error> {
|
||||
let res = community_follower::table
|
||||
.inner_join(community::table)
|
||||
.inner_join(user_::table)
|
||||
.select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
|
||||
.filter(community_follower::community_id.eq(for_community_id))
|
||||
.order_by(community_follower::published)
|
||||
.load::<(CommunitySafe, UserSafe)>(conn)?;
|
||||
|
||||
Ok(to_vec(res))
|
||||
}
|
||||
|
||||
pub fn for_user(conn: &PgConnection, for_user_id: i32) -> Result<Vec<Self>, Error> {
|
||||
let res = community_follower::table
|
||||
.inner_join(community::table)
|
||||
.inner_join(user_::table)
|
||||
.select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
|
||||
.filter(community_follower::user_id.eq(for_user_id))
|
||||
.order_by(community_follower::published)
|
||||
.load::<(CommunitySafe, UserSafe)>(conn)?;
|
||||
|
||||
Ok(to_vec(res))
|
||||
}
|
||||
}
|
||||
|
||||
fn to_vec(users: Vec<(CommunitySafe, UserSafe)>) -> Vec<CommunityFollowerView> {
|
||||
users
|
||||
.iter()
|
||||
.map(|a| CommunityFollowerView {
|
||||
community: a.0.to_owned(),
|
||||
follower: a.1.to_owned(),
|
||||
})
|
||||
.collect::<Vec<CommunityFollowerView>>()
|
||||
}
|
50
lemmy_db/src/views/community_moderator_view.rs
Normal file
50
lemmy_db/src/views/community_moderator_view.rs
Normal file
|
@ -0,0 +1,50 @@
|
|||
use crate::{
|
||||
community::{Community, CommunitySafe},
|
||||
schema::{community, community_moderator, user_},
|
||||
user::{UserSafe, User_},
|
||||
ToSafe,
|
||||
};
|
||||
use diesel::{result::Error, *};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct CommunityModeratorView {
|
||||
pub community: CommunitySafe,
|
||||
pub moderator: UserSafe,
|
||||
}
|
||||
|
||||
impl CommunityModeratorView {
|
||||
pub fn for_community(conn: &PgConnection, for_community_id: i32) -> Result<Vec<Self>, Error> {
|
||||
let res = community_moderator::table
|
||||
.inner_join(community::table)
|
||||
.inner_join(user_::table)
|
||||
.select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
|
||||
.filter(community_moderator::community_id.eq(for_community_id))
|
||||
.order_by(community_moderator::published)
|
||||
.load::<(CommunitySafe, UserSafe)>(conn)?;
|
||||
|
||||
Ok(to_vec(res))
|
||||
}
|
||||
|
||||
pub fn for_user(conn: &PgConnection, for_user_id: i32) -> Result<Vec<Self>, Error> {
|
||||
let res = community_moderator::table
|
||||
.inner_join(community::table)
|
||||
.inner_join(user_::table)
|
||||
.select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
|
||||
.filter(community_moderator::user_id.eq(for_user_id))
|
||||
.order_by(community_moderator::published)
|
||||
.load::<(CommunitySafe, UserSafe)>(conn)?;
|
||||
|
||||
Ok(to_vec(res))
|
||||
}
|
||||
}
|
||||
|
||||
fn to_vec(users: Vec<(CommunitySafe, UserSafe)>) -> Vec<CommunityModeratorView> {
|
||||
users
|
||||
.iter()
|
||||
.map(|a| CommunityModeratorView {
|
||||
community: a.0.to_owned(),
|
||||
moderator: a.1.to_owned(),
|
||||
})
|
||||
.collect::<Vec<CommunityModeratorView>>()
|
||||
}
|
33
lemmy_db/src/views/community_user_ban_view.rs
Normal file
33
lemmy_db/src/views/community_user_ban_view.rs
Normal file
|
@ -0,0 +1,33 @@
|
|||
use crate::{
|
||||
community::{Community, CommunitySafe},
|
||||
schema::{community, community_user_ban, user_},
|
||||
user::{UserSafe, User_},
|
||||
ToSafe,
|
||||
};
|
||||
use diesel::{result::Error, *};
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct CommunityUserBanView {
|
||||
pub community: CommunitySafe,
|
||||
pub user: UserSafe,
|
||||
}
|
||||
|
||||
impl CommunityUserBanView {
|
||||
pub fn get(
|
||||
conn: &PgConnection,
|
||||
from_user_id: i32,
|
||||
from_community_id: i32,
|
||||
) -> Result<Self, Error> {
|
||||
let (community, user) = community_user_ban::table
|
||||
.inner_join(community::table)
|
||||
.inner_join(user_::table)
|
||||
.select((Community::safe_columns_tuple(), User_::safe_columns_tuple()))
|
||||
.filter(community_user_ban::community_id.eq(from_community_id))
|
||||
.filter(community_user_ban::user_id.eq(from_user_id))
|
||||
.order_by(community_user_ban::published)
|
||||
.first::<(CommunitySafe, UserSafe)>(conn)?;
|
||||
|
||||
Ok(CommunityUserBanView { community, user })
|
||||
}
|
||||
}
|
|
@ -1,3 +1,6 @@
|
|||
pub mod community_follower_view;
|
||||
pub mod community_moderator_view;
|
||||
pub mod community_user_ban_view;
|
||||
pub mod community_view;
|
||||
pub mod site_view;
|
||||
pub mod user_view;
|
||||
|
|
Loading…
Reference in a new issue