From: Dessalines Date: Wed, 23 Dec 2020 21:56:20 +0000 (-0500) Subject: Adding community_view to PostResponse. X-Git-Url: http://these/git/%22https:/lemmy.ml/u/Liwott/static/%24%7Bargs.pageFn.next%7D?a=commitdiff_plain;h=58281208b921138a086136454f6408889209bafc;p=lemmy.git Adding community_view to PostResponse. - Changing to_vec function name. --- diff --git a/lemmy_api/src/post.rs b/lemmy_api/src/post.rs index 20b5b8d5..f3589462 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 7a260cd5..52a8bdb5 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 1b114e19..3ae14f64 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 e7ba0e4a..7ed94538 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 6800853e..a6162126 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 11962d79..0ea4388e 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 3cac0bd3..337e2bc3 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 302d37a0..6dbbe148 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 8f586a6f..f60a4edc 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 0ed52dd2..6bfba76f 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 98cf1969..2604f4a6 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 dbe81c5c..2fcc25f7 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 04aab30a..1493089b 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 37ffe540..2b9e0c8e 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 21bf1b86..6b1923d5 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 8512e079..fab8ddeb 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 37d6275c..ab6adc78 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 3cfee1b3..643c60a8 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 f439a75d..d6f1f63a 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 2cd1cd3c..0f9073b5 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 f3109011..36b52522 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 ac29d8f7..0c6c23d7 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,