Adding community_view to PostResponse.

- Changing to_vec function name.
This commit is contained in:
Dessalines 2020-12-23 16:56:20 -05:00
parent e5a65d5807
commit 58281208b9
22 changed files with 77 additions and 64 deletions

View file

@ -14,7 +14,7 @@ use lemmy_db::{
source::post::Post_, source::post::Post_,
views::{ views::{
comment_view::CommentQueryBuilder, comment_view::CommentQueryBuilder,
community::community_moderator_view::CommunityModeratorView, community::{community_moderator_view::CommunityModeratorView, community_view::CommunityView},
post_report_view::{PostReportQueryBuilder, PostReportView}, post_report_view::{PostReportQueryBuilder, PostReportView},
post_view::{PostQueryBuilder, PostView}, post_view::{PostQueryBuilder, PostView},
}, },
@ -201,6 +201,16 @@ impl Perform for GetPost {
}) })
.await??; .await??;
// Necessary for the sidebar
let community_view = match blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, user_id)
})
.await?
{
Ok(community) => community,
Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
};
let online = context let online = context
.chat_server() .chat_server()
.send(GetPostUsersOnline { post_id: data.id }) .send(GetPostUsersOnline { post_id: data.id })
@ -210,6 +220,7 @@ impl Perform for GetPost {
// Return the jwt // Return the jwt
Ok(GetPostResponse { Ok(GetPostResponse {
post_view, post_view,
community_view,
comments, comments,
moderators, moderators,
online, online,

View file

@ -167,14 +167,14 @@ impl<'a> CommentReportQueryBuilder<'a> {
.offset(offset) .offset(offset)
.load::<CommentReportViewTuple>(self.conn)?; .load::<CommentReportViewTuple>(self.conn)?;
Ok(CommentReportView::to_vec(res)) Ok(CommentReportView::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for CommentReportView { impl ViewToVec for CommentReportView {
type DbTuple = CommentReportViewTuple; type DbTuple = CommentReportViewTuple;
fn to_vec(posts: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
posts items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
comment_report: a.0.to_owned(), comment_report: a.0.to_owned(),

View file

@ -391,14 +391,14 @@ impl<'a> CommentQueryBuilder<'a> {
.offset(offset) .offset(offset)
.load::<CommentViewTuple>(self.conn)?; .load::<CommentViewTuple>(self.conn)?;
Ok(CommentView::to_vec(res)) Ok(CommentView::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for CommentView { impl ViewToVec for CommentView {
type DbTuple = CommentViewTuple; type DbTuple = CommentViewTuple;
fn to_vec(posts: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
posts items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
comment: a.0.to_owned(), comment: a.0.to_owned(),

View file

@ -27,7 +27,7 @@ impl CommunityFollowerView {
.order_by(community_follower::published) .order_by(community_follower::published)
.load::<CommunityFollowerViewTuple>(conn)?; .load::<CommunityFollowerViewTuple>(conn)?;
Ok(Self::to_vec(res)) Ok(Self::from_tuple_to_vec(res))
} }
pub fn for_user(conn: &PgConnection, user_id: i32) -> Result<Vec<Self>, Error> { pub fn for_user(conn: &PgConnection, user_id: i32) -> Result<Vec<Self>, Error> {
@ -39,14 +39,14 @@ impl CommunityFollowerView {
.order_by(community_follower::published) .order_by(community_follower::published)
.load::<CommunityFollowerViewTuple>(conn)?; .load::<CommunityFollowerViewTuple>(conn)?;
Ok(Self::to_vec(res)) Ok(Self::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for CommunityFollowerView { impl ViewToVec for CommunityFollowerView {
type DbTuple = CommunityFollowerViewTuple; type DbTuple = CommunityFollowerViewTuple;
fn to_vec(users: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
users items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
community: a.0.to_owned(), community: a.0.to_owned(),

View file

@ -27,7 +27,7 @@ impl CommunityModeratorView {
.order_by(community_moderator::published) .order_by(community_moderator::published)
.load::<CommunityModeratorViewTuple>(conn)?; .load::<CommunityModeratorViewTuple>(conn)?;
Ok(Self::to_vec(res)) Ok(Self::from_tuple_to_vec(res))
} }
pub fn for_user(conn: &PgConnection, user_id: i32) -> Result<Vec<Self>, Error> { pub fn for_user(conn: &PgConnection, user_id: i32) -> Result<Vec<Self>, Error> {
@ -39,14 +39,14 @@ impl CommunityModeratorView {
.order_by(community_moderator::published) .order_by(community_moderator::published)
.load::<CommunityModeratorViewTuple>(conn)?; .load::<CommunityModeratorViewTuple>(conn)?;
Ok(Self::to_vec(res)) Ok(Self::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for CommunityModeratorView { impl ViewToVec for CommunityModeratorView {
type DbTuple = CommunityModeratorViewTuple; type DbTuple = CommunityModeratorViewTuple;
fn to_vec(community_moderators: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
community_moderators items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
community: a.0.to_owned(), community: a.0.to_owned(),

View file

@ -185,14 +185,14 @@ impl<'a> CommunityQueryBuilder<'a> {
.filter(community::deleted.eq(false)) .filter(community::deleted.eq(false))
.load::<CommunityViewTuple>(self.conn)?; .load::<CommunityViewTuple>(self.conn)?;
Ok(CommunityView::to_vec(res)) Ok(CommunityView::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for CommunityView { impl ViewToVec for CommunityView {
type DbTuple = CommunityViewTuple; type DbTuple = CommunityViewTuple;
fn to_vec(communities: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
communities items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
community: a.0.to_owned(), community: a.0.to_owned(),

View file

@ -11,7 +11,7 @@ pub mod user_view;
pub(crate) trait ViewToVec { pub(crate) trait ViewToVec {
type DbTuple; type DbTuple;
fn to_vec(tuple: Vec<Self::DbTuple>) -> Vec<Self> fn from_tuple_to_vec(tuple: Vec<Self::DbTuple>) -> Vec<Self>
where where
Self: Sized; Self: Sized;
} }

View file

@ -56,14 +56,14 @@ impl ModAddCommunityView {
.order_by(mod_add_community::when_.desc()) .order_by(mod_add_community::when_.desc())
.load::<ModAddCommunityViewTuple>(conn)?; .load::<ModAddCommunityViewTuple>(conn)?;
Ok(Self::to_vec(res)) Ok(Self::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for ModAddCommunityView { impl ViewToVec for ModAddCommunityView {
type DbTuple = ModAddCommunityViewTuple; type DbTuple = ModAddCommunityViewTuple;
fn to_vec(mrp: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
mrp items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
mod_add_community: a.0.to_owned(), mod_add_community: a.0.to_owned(),

View file

@ -47,14 +47,14 @@ impl ModAddView {
.order_by(mod_add::when_.desc()) .order_by(mod_add::when_.desc())
.load::<ModAddViewTuple>(conn)?; .load::<ModAddViewTuple>(conn)?;
Ok(Self::to_vec(res)) Ok(Self::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for ModAddView { impl ViewToVec for ModAddView {
type DbTuple = ModAddViewTuple; type DbTuple = ModAddViewTuple;
fn to_vec(mrp: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
mrp items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
mod_add: a.0.to_owned(), mod_add: a.0.to_owned(),

View file

@ -56,14 +56,14 @@ impl ModBanFromCommunityView {
.order_by(mod_ban_from_community::when_.desc()) .order_by(mod_ban_from_community::when_.desc())
.load::<ModBanFromCommunityViewTuple>(conn)?; .load::<ModBanFromCommunityViewTuple>(conn)?;
Ok(Self::to_vec(res)) Ok(Self::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for ModBanFromCommunityView { impl ViewToVec for ModBanFromCommunityView {
type DbTuple = ModBanFromCommunityViewTuple; type DbTuple = ModBanFromCommunityViewTuple;
fn to_vec(mrp: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
mrp items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
mod_ban_from_community: a.0.to_owned(), mod_ban_from_community: a.0.to_owned(),

View file

@ -47,14 +47,14 @@ impl ModBanView {
.order_by(mod_ban::when_.desc()) .order_by(mod_ban::when_.desc())
.load::<ModBanViewTuple>(conn)?; .load::<ModBanViewTuple>(conn)?;
Ok(Self::to_vec(res)) Ok(Self::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for ModBanView { impl ViewToVec for ModBanView {
type DbTuple = ModBanViewTuple; type DbTuple = ModBanViewTuple;
fn to_vec(mrp: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
mrp items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
mod_ban: a.0.to_owned(), mod_ban: a.0.to_owned(),

View file

@ -57,14 +57,14 @@ impl ModLockPostView {
.order_by(mod_lock_post::when_.desc()) .order_by(mod_lock_post::when_.desc())
.load::<ModLockPostViewTuple>(conn)?; .load::<ModLockPostViewTuple>(conn)?;
Ok(Self::to_vec(res)) Ok(Self::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for ModLockPostView { impl ViewToVec for ModLockPostView {
type DbTuple = ModLockPostViewTuple; type DbTuple = ModLockPostViewTuple;
fn to_vec(mrp: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
mrp items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
mod_lock_post: a.0.to_owned(), mod_lock_post: a.0.to_owned(),

View file

@ -71,14 +71,14 @@ impl ModRemoveCommentView {
.order_by(mod_remove_comment::when_.desc()) .order_by(mod_remove_comment::when_.desc())
.load::<ModRemoveCommentViewTuple>(conn)?; .load::<ModRemoveCommentViewTuple>(conn)?;
Ok(Self::to_vec(res)) Ok(Self::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for ModRemoveCommentView { impl ViewToVec for ModRemoveCommentView {
type DbTuple = ModRemoveCommentViewTuple; type DbTuple = ModRemoveCommentViewTuple;
fn to_vec(mrp: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
mrp items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
mod_remove_comment: a.0.to_owned(), mod_remove_comment: a.0.to_owned(),

View file

@ -48,14 +48,14 @@ impl ModRemoveCommunityView {
.order_by(mod_remove_community::when_.desc()) .order_by(mod_remove_community::when_.desc())
.load::<ModRemoveCommunityTuple>(conn)?; .load::<ModRemoveCommunityTuple>(conn)?;
Ok(Self::to_vec(res)) Ok(Self::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for ModRemoveCommunityView { impl ViewToVec for ModRemoveCommunityView {
type DbTuple = ModRemoveCommunityTuple; type DbTuple = ModRemoveCommunityTuple;
fn to_vec(mrp: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
mrp items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
mod_remove_community: a.0.to_owned(), mod_remove_community: a.0.to_owned(),

View file

@ -57,14 +57,14 @@ impl ModRemovePostView {
.order_by(mod_remove_post::when_.desc()) .order_by(mod_remove_post::when_.desc())
.load::<ModRemovePostViewTuple>(conn)?; .load::<ModRemovePostViewTuple>(conn)?;
Ok(Self::to_vec(res)) Ok(Self::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for ModRemovePostView { impl ViewToVec for ModRemovePostView {
type DbTuple = ModRemovePostViewTuple; type DbTuple = ModRemovePostViewTuple;
fn to_vec(mrp: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
mrp items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
mod_remove_post: a.0.to_owned(), mod_remove_post: a.0.to_owned(),

View file

@ -57,14 +57,14 @@ impl ModStickyPostView {
.order_by(mod_sticky_post::when_.desc()) .order_by(mod_sticky_post::when_.desc())
.load::<ModStickyPostViewTuple>(conn)?; .load::<ModStickyPostViewTuple>(conn)?;
Ok(Self::to_vec(res)) Ok(Self::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for ModStickyPostView { impl ViewToVec for ModStickyPostView {
type DbTuple = ModStickyPostViewTuple; type DbTuple = ModStickyPostViewTuple;
fn to_vec(mrp: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
mrp items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
mod_sticky_post: a.0.to_owned(), mod_sticky_post: a.0.to_owned(),

View file

@ -153,14 +153,14 @@ impl<'a> PostReportQueryBuilder<'a> {
.offset(offset) .offset(offset)
.load::<PostReportViewTuple>(self.conn)?; .load::<PostReportViewTuple>(self.conn)?;
Ok(PostReportView::to_vec(res)) Ok(PostReportView::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for PostReportView { impl ViewToVec for PostReportView {
type DbTuple = PostReportViewTuple; type DbTuple = PostReportViewTuple;
fn to_vec(posts: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
posts items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
post_report: a.0.to_owned(), post_report: a.0.to_owned(),

View file

@ -380,14 +380,14 @@ impl<'a> PostQueryBuilder<'a> {
.filter(community::deleted.eq(false)) .filter(community::deleted.eq(false))
.load::<PostViewTuple>(self.conn)?; .load::<PostViewTuple>(self.conn)?;
Ok(PostView::to_vec(res)) Ok(PostView::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for PostView { impl ViewToVec for PostView {
type DbTuple = PostViewTuple; type DbTuple = PostViewTuple;
fn to_vec(posts: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
posts items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
post: a.0.to_owned(), post: a.0.to_owned(),

View file

@ -109,14 +109,15 @@ impl<'a> PrivateMessageQueryBuilder<'a> {
.order_by(private_message::published.desc()) .order_by(private_message::published.desc())
.load::<PrivateMessageViewTuple>(self.conn)?; .load::<PrivateMessageViewTuple>(self.conn)?;
Ok(PrivateMessageView::to_vec(res)) Ok(PrivateMessageView::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for PrivateMessageView { impl ViewToVec for PrivateMessageView {
type DbTuple = PrivateMessageViewTuple; type DbTuple = PrivateMessageViewTuple;
fn to_vec(pm: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
pm.iter() items
.iter()
.map(|a| Self { .map(|a| Self {
private_message: a.0.to_owned(), private_message: a.0.to_owned(),
creator: a.1.to_owned(), creator: a.1.to_owned(),

View file

@ -293,14 +293,14 @@ impl<'a> UserMentionQueryBuilder<'a> {
.offset(offset) .offset(offset)
.load::<UserMentionViewTuple>(self.conn)?; .load::<UserMentionViewTuple>(self.conn)?;
Ok(UserMentionView::to_vec(res)) Ok(UserMentionView::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for UserMentionView { impl ViewToVec for UserMentionView {
type DbTuple = UserMentionViewTuple; type DbTuple = UserMentionViewTuple;
fn to_vec(posts: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
posts items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
user_mention: a.0.to_owned(), user_mention: a.0.to_owned(),

View file

@ -58,7 +58,7 @@ impl UserViewSafe {
.order_by(user_::published) .order_by(user_::published)
.load::<UserViewSafeTuple>(conn)?; .load::<UserViewSafeTuple>(conn)?;
Ok(Self::to_vec(admins)) Ok(Self::from_tuple_to_vec(admins))
} }
pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> { pub fn banned(conn: &PgConnection) -> Result<Vec<Self>, Error> {
@ -68,7 +68,7 @@ impl UserViewSafe {
.filter(user_::banned.eq(true)) .filter(user_::banned.eq(true))
.load::<UserViewSafeTuple>(conn)?; .load::<UserViewSafeTuple>(conn)?;
Ok(Self::to_vec(banned)) Ok(Self::from_tuple_to_vec(banned))
} }
} }
@ -149,14 +149,14 @@ impl<'a> UserQueryBuilder<'a> {
let res = query.load::<UserViewSafeTuple>(self.conn)?; let res = query.load::<UserViewSafeTuple>(self.conn)?;
Ok(UserViewSafe::to_vec(res)) Ok(UserViewSafe::from_tuple_to_vec(res))
} }
} }
impl ViewToVec for UserViewSafe { impl ViewToVec for UserViewSafe {
type DbTuple = UserViewSafeTuple; type DbTuple = UserViewSafeTuple;
fn to_vec(users: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
users items
.iter() .iter()
.map(|a| Self { .map(|a| Self {
user: a.0.to_owned(), user: a.0.to_owned(),

View file

@ -1,6 +1,6 @@
use lemmy_db::views::{ use lemmy_db::views::{
comment_view::CommentView, comment_view::CommentView,
community::community_moderator_view::CommunityModeratorView, community::{community_moderator_view::CommunityModeratorView, community_view::CommunityView},
post_report_view::PostReportView, post_report_view::PostReportView,
post_view::PostView, post_view::PostView,
}; };
@ -30,6 +30,7 @@ pub struct GetPost {
#[derive(Serialize)] #[derive(Serialize)]
pub struct GetPostResponse { pub struct GetPostResponse {
pub post_view: PostView, pub post_view: PostView,
pub community_view: CommunityView,
pub comments: Vec<CommentView>, pub comments: Vec<CommentView>,
pub moderators: Vec<CommunityModeratorView>, pub moderators: Vec<CommunityModeratorView>,
pub online: usize, pub online: usize,