From 58281208b921138a086136454f6408889209bafc Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 23 Dec 2020 16:56:20 -0500 Subject: [PATCH] Adding community_view to PostResponse. - Changing to_vec function name. --- lemmy_api/src/post.rs | 13 ++++++++++++- lemmy_db/src/views/comment_report_view.rs | 6 +++--- lemmy_db/src/views/comment_view.rs | 6 +++--- .../src/views/community/community_follower_view.rs | 8 ++++---- .../src/views/community/community_moderator_view.rs | 8 ++++---- lemmy_db/src/views/community/community_view.rs | 6 +++--- lemmy_db/src/views/mod.rs | 2 +- .../src/views/moderator/mod_add_community_view.rs | 6 +++--- lemmy_db/src/views/moderator/mod_add_view.rs | 6 +++--- .../views/moderator/mod_ban_from_community_view.rs | 6 +++--- lemmy_db/src/views/moderator/mod_ban_view.rs | 6 +++--- lemmy_db/src/views/moderator/mod_lock_post_view.rs | 6 +++--- .../src/views/moderator/mod_remove_comment_view.rs | 6 +++--- .../views/moderator/mod_remove_community_view.rs | 6 +++--- .../src/views/moderator/mod_remove_post_view.rs | 6 +++--- .../src/views/moderator/mod_sticky_post_view.rs | 6 +++--- lemmy_db/src/views/post_report_view.rs | 6 +++--- lemmy_db/src/views/post_view.rs | 6 +++--- lemmy_db/src/views/private_message_view.rs | 7 ++++--- lemmy_db/src/views/user_mention_view.rs | 6 +++--- lemmy_db/src/views/user_view.rs | 10 +++++----- lemmy_structs/src/post.rs | 3 ++- 22 files changed, 77 insertions(+), 64 deletions(-) diff --git a/lemmy_api/src/post.rs b/lemmy_api/src/post.rs index 20b5b8d55..f35894626 100644 --- a/lemmy_api/src/post.rs +++ b/lemmy_api/src/post.rs @@ -14,7 +14,7 @@ use lemmy_db::{ source::post::Post_, views::{ comment_view::CommentQueryBuilder, - community::community_moderator_view::CommunityModeratorView, + community::{community_moderator_view::CommunityModeratorView, community_view::CommunityView}, post_report_view::{PostReportQueryBuilder, PostReportView}, post_view::{PostQueryBuilder, PostView}, }, @@ -201,6 +201,16 @@ impl Perform for GetPost { }) .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 .chat_server() .send(GetPostUsersOnline { post_id: data.id }) @@ -210,6 +220,7 @@ impl Perform for GetPost { // Return the jwt Ok(GetPostResponse { post_view, + community_view, comments, moderators, online, diff --git a/lemmy_db/src/views/comment_report_view.rs b/lemmy_db/src/views/comment_report_view.rs index 7a260cd59..52a8bdb50 100644 --- a/lemmy_db/src/views/comment_report_view.rs +++ b/lemmy_db/src/views/comment_report_view.rs @@ -167,14 +167,14 @@ impl<'a> CommentReportQueryBuilder<'a> { .offset(offset) .load::(self.conn)?; - Ok(CommentReportView::to_vec(res)) + Ok(CommentReportView::from_tuple_to_vec(res)) } } impl ViewToVec for CommentReportView { type DbTuple = CommentReportViewTuple; - fn to_vec(posts: Vec) -> Vec { - posts + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { comment_report: a.0.to_owned(), diff --git a/lemmy_db/src/views/comment_view.rs b/lemmy_db/src/views/comment_view.rs index 1b114e190..3ae14f64c 100644 --- a/lemmy_db/src/views/comment_view.rs +++ b/lemmy_db/src/views/comment_view.rs @@ -391,14 +391,14 @@ impl<'a> CommentQueryBuilder<'a> { .offset(offset) .load::(self.conn)?; - Ok(CommentView::to_vec(res)) + Ok(CommentView::from_tuple_to_vec(res)) } } impl ViewToVec for CommentView { type DbTuple = CommentViewTuple; - fn to_vec(posts: Vec) -> Vec { - posts + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { comment: a.0.to_owned(), diff --git a/lemmy_db/src/views/community/community_follower_view.rs b/lemmy_db/src/views/community/community_follower_view.rs index e7ba0e4a6..7ed945380 100644 --- a/lemmy_db/src/views/community/community_follower_view.rs +++ b/lemmy_db/src/views/community/community_follower_view.rs @@ -27,7 +27,7 @@ impl CommunityFollowerView { .order_by(community_follower::published) .load::(conn)?; - Ok(Self::to_vec(res)) + Ok(Self::from_tuple_to_vec(res)) } pub fn for_user(conn: &PgConnection, user_id: i32) -> Result, Error> { @@ -39,14 +39,14 @@ impl CommunityFollowerView { .order_by(community_follower::published) .load::(conn)?; - Ok(Self::to_vec(res)) + Ok(Self::from_tuple_to_vec(res)) } } impl ViewToVec for CommunityFollowerView { type DbTuple = CommunityFollowerViewTuple; - fn to_vec(users: Vec) -> Vec { - users + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { community: a.0.to_owned(), diff --git a/lemmy_db/src/views/community/community_moderator_view.rs b/lemmy_db/src/views/community/community_moderator_view.rs index 6800853ea..a61621266 100644 --- a/lemmy_db/src/views/community/community_moderator_view.rs +++ b/lemmy_db/src/views/community/community_moderator_view.rs @@ -27,7 +27,7 @@ impl CommunityModeratorView { .order_by(community_moderator::published) .load::(conn)?; - Ok(Self::to_vec(res)) + Ok(Self::from_tuple_to_vec(res)) } pub fn for_user(conn: &PgConnection, user_id: i32) -> Result, Error> { @@ -39,14 +39,14 @@ impl CommunityModeratorView { .order_by(community_moderator::published) .load::(conn)?; - Ok(Self::to_vec(res)) + Ok(Self::from_tuple_to_vec(res)) } } impl ViewToVec for CommunityModeratorView { type DbTuple = CommunityModeratorViewTuple; - fn to_vec(community_moderators: Vec) -> Vec { - community_moderators + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { community: a.0.to_owned(), diff --git a/lemmy_db/src/views/community/community_view.rs b/lemmy_db/src/views/community/community_view.rs index 11962d79a..0ea4388e1 100644 --- a/lemmy_db/src/views/community/community_view.rs +++ b/lemmy_db/src/views/community/community_view.rs @@ -185,14 +185,14 @@ impl<'a> CommunityQueryBuilder<'a> { .filter(community::deleted.eq(false)) .load::(self.conn)?; - Ok(CommunityView::to_vec(res)) + Ok(CommunityView::from_tuple_to_vec(res)) } } impl ViewToVec for CommunityView { type DbTuple = CommunityViewTuple; - fn to_vec(communities: Vec) -> Vec { - communities + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { community: a.0.to_owned(), diff --git a/lemmy_db/src/views/mod.rs b/lemmy_db/src/views/mod.rs index 3cac0bd3d..337e2bc3b 100644 --- a/lemmy_db/src/views/mod.rs +++ b/lemmy_db/src/views/mod.rs @@ -11,7 +11,7 @@ pub mod user_view; pub(crate) trait ViewToVec { type DbTuple; - fn to_vec(tuple: Vec) -> Vec + fn from_tuple_to_vec(tuple: Vec) -> Vec where Self: Sized; } diff --git a/lemmy_db/src/views/moderator/mod_add_community_view.rs b/lemmy_db/src/views/moderator/mod_add_community_view.rs index 302d37a0b..6dbbe1484 100644 --- a/lemmy_db/src/views/moderator/mod_add_community_view.rs +++ b/lemmy_db/src/views/moderator/mod_add_community_view.rs @@ -56,14 +56,14 @@ impl ModAddCommunityView { .order_by(mod_add_community::when_.desc()) .load::(conn)?; - Ok(Self::to_vec(res)) + Ok(Self::from_tuple_to_vec(res)) } } impl ViewToVec for ModAddCommunityView { type DbTuple = ModAddCommunityViewTuple; - fn to_vec(mrp: Vec) -> Vec { - mrp + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { mod_add_community: a.0.to_owned(), diff --git a/lemmy_db/src/views/moderator/mod_add_view.rs b/lemmy_db/src/views/moderator/mod_add_view.rs index 8f586a6ff..f60a4edc5 100644 --- a/lemmy_db/src/views/moderator/mod_add_view.rs +++ b/lemmy_db/src/views/moderator/mod_add_view.rs @@ -47,14 +47,14 @@ impl ModAddView { .order_by(mod_add::when_.desc()) .load::(conn)?; - Ok(Self::to_vec(res)) + Ok(Self::from_tuple_to_vec(res)) } } impl ViewToVec for ModAddView { type DbTuple = ModAddViewTuple; - fn to_vec(mrp: Vec) -> Vec { - mrp + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { mod_add: a.0.to_owned(), 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 index 0ed52dd25..6bfba76f7 100644 --- a/lemmy_db/src/views/moderator/mod_ban_from_community_view.rs +++ b/lemmy_db/src/views/moderator/mod_ban_from_community_view.rs @@ -56,14 +56,14 @@ impl ModBanFromCommunityView { .order_by(mod_ban_from_community::when_.desc()) .load::(conn)?; - Ok(Self::to_vec(res)) + Ok(Self::from_tuple_to_vec(res)) } } impl ViewToVec for ModBanFromCommunityView { type DbTuple = ModBanFromCommunityViewTuple; - fn to_vec(mrp: Vec) -> Vec { - mrp + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { mod_ban_from_community: a.0.to_owned(), diff --git a/lemmy_db/src/views/moderator/mod_ban_view.rs b/lemmy_db/src/views/moderator/mod_ban_view.rs index 98cf1969f..2604f4a60 100644 --- a/lemmy_db/src/views/moderator/mod_ban_view.rs +++ b/lemmy_db/src/views/moderator/mod_ban_view.rs @@ -47,14 +47,14 @@ impl ModBanView { .order_by(mod_ban::when_.desc()) .load::(conn)?; - Ok(Self::to_vec(res)) + Ok(Self::from_tuple_to_vec(res)) } } impl ViewToVec for ModBanView { type DbTuple = ModBanViewTuple; - fn to_vec(mrp: Vec) -> Vec { - mrp + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { mod_ban: a.0.to_owned(), diff --git a/lemmy_db/src/views/moderator/mod_lock_post_view.rs b/lemmy_db/src/views/moderator/mod_lock_post_view.rs index dbe81c5c3..2fcc25f7a 100644 --- a/lemmy_db/src/views/moderator/mod_lock_post_view.rs +++ b/lemmy_db/src/views/moderator/mod_lock_post_view.rs @@ -57,14 +57,14 @@ impl ModLockPostView { .order_by(mod_lock_post::when_.desc()) .load::(conn)?; - Ok(Self::to_vec(res)) + Ok(Self::from_tuple_to_vec(res)) } } impl ViewToVec for ModLockPostView { type DbTuple = ModLockPostViewTuple; - fn to_vec(mrp: Vec) -> Vec { - mrp + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { mod_lock_post: a.0.to_owned(), diff --git a/lemmy_db/src/views/moderator/mod_remove_comment_view.rs b/lemmy_db/src/views/moderator/mod_remove_comment_view.rs index 04aab30af..1493089bd 100644 --- a/lemmy_db/src/views/moderator/mod_remove_comment_view.rs +++ b/lemmy_db/src/views/moderator/mod_remove_comment_view.rs @@ -71,14 +71,14 @@ impl ModRemoveCommentView { .order_by(mod_remove_comment::when_.desc()) .load::(conn)?; - Ok(Self::to_vec(res)) + Ok(Self::from_tuple_to_vec(res)) } } impl ViewToVec for ModRemoveCommentView { type DbTuple = ModRemoveCommentViewTuple; - fn to_vec(mrp: Vec) -> Vec { - mrp + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { mod_remove_comment: a.0.to_owned(), diff --git a/lemmy_db/src/views/moderator/mod_remove_community_view.rs b/lemmy_db/src/views/moderator/mod_remove_community_view.rs index 37ffe5402..2b9e0c8e6 100644 --- a/lemmy_db/src/views/moderator/mod_remove_community_view.rs +++ b/lemmy_db/src/views/moderator/mod_remove_community_view.rs @@ -48,14 +48,14 @@ impl ModRemoveCommunityView { .order_by(mod_remove_community::when_.desc()) .load::(conn)?; - Ok(Self::to_vec(res)) + Ok(Self::from_tuple_to_vec(res)) } } impl ViewToVec for ModRemoveCommunityView { type DbTuple = ModRemoveCommunityTuple; - fn to_vec(mrp: Vec) -> Vec { - mrp + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { mod_remove_community: a.0.to_owned(), diff --git a/lemmy_db/src/views/moderator/mod_remove_post_view.rs b/lemmy_db/src/views/moderator/mod_remove_post_view.rs index 21bf1b867..6b1923d5d 100644 --- a/lemmy_db/src/views/moderator/mod_remove_post_view.rs +++ b/lemmy_db/src/views/moderator/mod_remove_post_view.rs @@ -57,14 +57,14 @@ impl ModRemovePostView { .order_by(mod_remove_post::when_.desc()) .load::(conn)?; - Ok(Self::to_vec(res)) + Ok(Self::from_tuple_to_vec(res)) } } impl ViewToVec for ModRemovePostView { type DbTuple = ModRemovePostViewTuple; - fn to_vec(mrp: Vec) -> Vec { - mrp + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { mod_remove_post: a.0.to_owned(), diff --git a/lemmy_db/src/views/moderator/mod_sticky_post_view.rs b/lemmy_db/src/views/moderator/mod_sticky_post_view.rs index 8512e0793..fab8ddeb5 100644 --- a/lemmy_db/src/views/moderator/mod_sticky_post_view.rs +++ b/lemmy_db/src/views/moderator/mod_sticky_post_view.rs @@ -57,14 +57,14 @@ impl ModStickyPostView { .order_by(mod_sticky_post::when_.desc()) .load::(conn)?; - Ok(Self::to_vec(res)) + Ok(Self::from_tuple_to_vec(res)) } } impl ViewToVec for ModStickyPostView { type DbTuple = ModStickyPostViewTuple; - fn to_vec(mrp: Vec) -> Vec { - mrp + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { mod_sticky_post: a.0.to_owned(), diff --git a/lemmy_db/src/views/post_report_view.rs b/lemmy_db/src/views/post_report_view.rs index 37d6275cd..ab6adc782 100644 --- a/lemmy_db/src/views/post_report_view.rs +++ b/lemmy_db/src/views/post_report_view.rs @@ -153,14 +153,14 @@ impl<'a> PostReportQueryBuilder<'a> { .offset(offset) .load::(self.conn)?; - Ok(PostReportView::to_vec(res)) + Ok(PostReportView::from_tuple_to_vec(res)) } } impl ViewToVec for PostReportView { type DbTuple = PostReportViewTuple; - fn to_vec(posts: Vec) -> Vec { - posts + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { post_report: a.0.to_owned(), diff --git a/lemmy_db/src/views/post_view.rs b/lemmy_db/src/views/post_view.rs index 3cfee1b37..643c60a84 100644 --- a/lemmy_db/src/views/post_view.rs +++ b/lemmy_db/src/views/post_view.rs @@ -380,14 +380,14 @@ impl<'a> PostQueryBuilder<'a> { .filter(community::deleted.eq(false)) .load::(self.conn)?; - Ok(PostView::to_vec(res)) + Ok(PostView::from_tuple_to_vec(res)) } } impl ViewToVec for PostView { type DbTuple = PostViewTuple; - fn to_vec(posts: Vec) -> Vec { - posts + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { post: a.0.to_owned(), diff --git a/lemmy_db/src/views/private_message_view.rs b/lemmy_db/src/views/private_message_view.rs index f439a75d5..d6f1f63ac 100644 --- a/lemmy_db/src/views/private_message_view.rs +++ b/lemmy_db/src/views/private_message_view.rs @@ -109,14 +109,15 @@ impl<'a> PrivateMessageQueryBuilder<'a> { .order_by(private_message::published.desc()) .load::(self.conn)?; - Ok(PrivateMessageView::to_vec(res)) + Ok(PrivateMessageView::from_tuple_to_vec(res)) } } impl ViewToVec for PrivateMessageView { type DbTuple = PrivateMessageViewTuple; - fn to_vec(pm: Vec) -> Vec { - pm.iter() + fn from_tuple_to_vec(items: Vec) -> Vec { + items + .iter() .map(|a| Self { private_message: a.0.to_owned(), creator: a.1.to_owned(), diff --git a/lemmy_db/src/views/user_mention_view.rs b/lemmy_db/src/views/user_mention_view.rs index 2cd1cd3c8..0f9073b5a 100644 --- a/lemmy_db/src/views/user_mention_view.rs +++ b/lemmy_db/src/views/user_mention_view.rs @@ -293,14 +293,14 @@ impl<'a> UserMentionQueryBuilder<'a> { .offset(offset) .load::(self.conn)?; - Ok(UserMentionView::to_vec(res)) + Ok(UserMentionView::from_tuple_to_vec(res)) } } impl ViewToVec for UserMentionView { type DbTuple = UserMentionViewTuple; - fn to_vec(posts: Vec) -> Vec { - posts + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { user_mention: a.0.to_owned(), diff --git a/lemmy_db/src/views/user_view.rs b/lemmy_db/src/views/user_view.rs index f3109011e..36b525227 100644 --- a/lemmy_db/src/views/user_view.rs +++ b/lemmy_db/src/views/user_view.rs @@ -58,7 +58,7 @@ impl UserViewSafe { .order_by(user_::published) .load::(conn)?; - Ok(Self::to_vec(admins)) + Ok(Self::from_tuple_to_vec(admins)) } pub fn banned(conn: &PgConnection) -> Result, Error> { @@ -68,7 +68,7 @@ impl UserViewSafe { .filter(user_::banned.eq(true)) .load::(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::(self.conn)?; - Ok(UserViewSafe::to_vec(res)) + Ok(UserViewSafe::from_tuple_to_vec(res)) } } impl ViewToVec for UserViewSafe { type DbTuple = UserViewSafeTuple; - fn to_vec(users: Vec) -> Vec { - users + fn from_tuple_to_vec(items: Vec) -> Vec { + items .iter() .map(|a| Self { user: a.0.to_owned(), diff --git a/lemmy_structs/src/post.rs b/lemmy_structs/src/post.rs index ac29d8f78..0c6c23d7b 100644 --- a/lemmy_structs/src/post.rs +++ b/lemmy_structs/src/post.rs @@ -1,6 +1,6 @@ use lemmy_db::views::{ comment_view::CommentView, - community::community_moderator_view::CommunityModeratorView, + community::{community_moderator_view::CommunityModeratorView, community_view::CommunityView}, post_report_view::PostReportView, post_view::PostView, }; @@ -30,6 +30,7 @@ pub struct GetPost { #[derive(Serialize)] pub struct GetPostResponse { pub post_view: PostView, + pub community_view: CommunityView, pub comments: Vec, pub moderators: Vec, pub online: usize,