Add pending, and change use specific API response for FollowCommunity. Fixes #2246

expose_pending
Dessalines 2022-05-06 17:37:53 -04:00 committed by Nutomic
parent 7566f325cd
commit 48f5a2ee5e
4 changed files with 50 additions and 17 deletions

View File

@ -1,7 +1,7 @@
use crate::Perform; use crate::Perform;
use actix_web::web::Data; use actix_web::web::Data;
use lemmy_api_common::{ use lemmy_api_common::{
community::{CommunityResponse, FollowCommunity}, community::{FollowCommunity, FollowCommunityResponse},
utils::{ utils::{
blocking, blocking,
check_community_ban, check_community_ban,
@ -20,20 +20,20 @@ use lemmy_db_schema::{
source::community::{Community, CommunityFollower, CommunityFollowerForm}, source::community::{Community, CommunityFollower, CommunityFollowerForm},
traits::{Crud, Followable}, traits::{Crud, Followable},
}; };
use lemmy_db_views_actor::structs::CommunityView; use lemmy_db_views_actor::structs::CommunityFollowerView;
use lemmy_utils::{ConnectionId, LemmyError}; use lemmy_utils::{ConnectionId, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for FollowCommunity { impl Perform for FollowCommunity {
type Response = CommunityResponse; type Response = FollowCommunityResponse;
#[tracing::instrument(skip(context, _websocket_id))] #[tracing::instrument(skip(context, _websocket_id))]
async fn perform( async fn perform(
&self, &self,
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>, _websocket_id: Option<ConnectionId>,
) -> Result<CommunityResponse, LemmyError> { ) -> Result<Self::Response, LemmyError> {
let data: &FollowCommunity = self; let data: &FollowCommunity = self;
let local_user_view = let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?; get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
@ -47,7 +47,7 @@ impl Perform for FollowCommunity {
let community_follower_form = CommunityFollowerForm { let community_follower_form = CommunityFollowerForm {
community_id: data.community_id, community_id: data.community_id,
person_id: local_user_view.person.id, person_id: local_user_view.person.id,
pending: false, pending: false, // Don't worry, this form isn't used for remote follows
}; };
if community.local { if community.local {
@ -82,18 +82,13 @@ impl Perform for FollowCommunity {
let community_id = data.community_id; let community_id = data.community_id;
let person_id = local_user_view.person.id; let person_id = local_user_view.person.id;
let mut community_view = blocking(context.pool(), move |conn| { let community_follower_view = blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, Some(person_id)) CommunityFollowerView::read(conn, community_id, person_id)
}) })
.await??; .await??;
// TODO: this needs to return a "pending" state, until Accept is received from the remote server Ok(Self::Response {
// For now, just assume that remote follows are accepted. community_follower_view,
// Otherwise, the subscribed will be null })
if !community.local {
community_view.subscribed = data.follow;
}
Ok(CommunityResponse { community_view })
} }
} }

View File

@ -5,7 +5,12 @@ use lemmy_db_schema::{
ListingType, ListingType,
SortType, SortType,
}; };
use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView, PersonViewSafe}; use lemmy_db_views_actor::structs::{
CommunityFollowerView,
CommunityModeratorView,
CommunityView,
PersonViewSafe,
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -41,6 +46,11 @@ pub struct CommunityResponse {
pub community_view: CommunityView, pub community_view: CommunityView,
} }
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct FollowCommunityResponse {
pub community_follower_view: CommunityFollowerView,
}
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct ListCommunities { pub struct ListCommunities {
pub type_: Option<ListingType>, pub type_: Option<ListingType>,

View File

@ -10,7 +10,7 @@ use lemmy_db_schema::{
traits::{ToSafe, ViewToVec}, traits::{ToSafe, ViewToVec},
}; };
type CommunityFollowerViewTuple = (CommunitySafe, PersonSafe); type CommunityFollowerViewTuple = (CommunitySafe, PersonSafe, Option<bool>);
impl CommunityFollowerView { impl CommunityFollowerView {
pub fn for_community(conn: &PgConnection, community_id: CommunityId) -> Result<Vec<Self>, Error> { pub fn for_community(conn: &PgConnection, community_id: CommunityId) -> Result<Vec<Self>, Error> {
@ -20,6 +20,7 @@ impl CommunityFollowerView {
.select(( .select((
Community::safe_columns_tuple(), Community::safe_columns_tuple(),
Person::safe_columns_tuple(), Person::safe_columns_tuple(),
community_follower::pending,
)) ))
.filter(community_follower::community_id.eq(community_id)) .filter(community_follower::community_id.eq(community_id))
.order_by(community::title) .order_by(community::title)
@ -35,6 +36,7 @@ impl CommunityFollowerView {
.select(( .select((
Community::safe_columns_tuple(), Community::safe_columns_tuple(),
Person::safe_columns_tuple(), Person::safe_columns_tuple(),
community_follower::pending,
)) ))
.filter(community_follower::person_id.eq(person_id)) .filter(community_follower::person_id.eq(person_id))
.order_by(community::title) .order_by(community::title)
@ -42,6 +44,30 @@ impl CommunityFollowerView {
Ok(Self::from_tuple_to_vec(res)) Ok(Self::from_tuple_to_vec(res))
} }
pub fn read(
conn: &PgConnection,
community_id: CommunityId,
person_id: PersonId,
) -> Result<Self, Error> {
let (community, follower, pending) = community_follower::table
.inner_join(community::table)
.inner_join(person::table)
.select((
Community::safe_columns_tuple(),
Person::safe_columns_tuple(),
community_follower::pending,
))
.filter(community_follower::person_id.eq(person_id))
.filter(community_follower::community_id.eq(community_id))
.first::<CommunityFollowerViewTuple>(conn)?;
Ok(Self {
community,
follower,
pending,
})
}
} }
impl ViewToVec for CommunityFollowerView { impl ViewToVec for CommunityFollowerView {
@ -52,6 +78,7 @@ impl ViewToVec for CommunityFollowerView {
.map(|a| Self { .map(|a| Self {
community: a.0.to_owned(), community: a.0.to_owned(),
follower: a.1.to_owned(), follower: a.1.to_owned(),
pending: a.2.to_owned(),
}) })
.collect::<Vec<Self>>() .collect::<Vec<Self>>()
} }

View File

@ -20,6 +20,7 @@ pub struct CommunityBlockView {
pub struct CommunityFollowerView { pub struct CommunityFollowerView {
pub community: CommunitySafe, pub community: CommunitySafe,
pub follower: PersonSafe, pub follower: PersonSafe,
pub pending: Option<bool>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]