Merge pull request #2248 from LemmyNet/expose_pending

Add pending, and change use specific API response for FollowCommunity…
revert-2248-expose_pending
Nutomic 2022-05-20 16:31:56 +00:00 committed by GitHub
commit 3af4a27a88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 95 additions and 45 deletions

View File

@ -16,7 +16,7 @@
"eslint": "^7.30.0", "eslint": "^7.30.0",
"eslint-plugin-jane": "^9.0.3", "eslint-plugin-jane": "^9.0.3",
"jest": "^27.0.6", "jest": "^27.0.6",
"lemmy-js-client": "0.16.0-rc.1", "lemmy-js-client": "0.17.0-rc.3",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"prettier": "^2.3.2", "prettier": "^2.3.2",
"ts-jest": "^27.0.3", "ts-jest": "^27.0.3",

View File

@ -329,12 +329,12 @@ test('A and G subscribe to B (center) A posts, G mentions B, it gets announced t
test('Check that activity from another instance is sent to third instance', async () => { test('Check that activity from another instance is sent to third instance', async () => {
// Alpha and gamma users follow beta community // Alpha and gamma users follow beta community
let alphaFollow = await followBeta(alpha); let alphaFollow = await followBeta(alpha);
expect(alphaFollow.community_view.community.local).toBe(false); expect(alphaFollow.community_follower_view.community.local).toBe(false);
expect(alphaFollow.community_view.community.name).toBe('main'); expect(alphaFollow.community_follower_view.community.name).toBe('main');
let gammaFollow = await followBeta(gamma); let gammaFollow = await followBeta(gamma);
expect(gammaFollow.community_view.community.local).toBe(false); expect(gammaFollow.community_follower_view.community.local).toBe(false);
expect(gammaFollow.community_view.community.name).toBe('main'); expect(gammaFollow.community_follower_view.community.name).toBe('main');
// Create a post on beta // Create a post on beta
let betaPost = await createPost(beta, 2); let betaPost = await createPost(beta, 2);
@ -407,8 +407,8 @@ test('Fetch in_reply_tos: A is unsubbed from B, B makes a post, and some embedde
// Follow beta again // Follow beta again
let follow = await followBeta(alpha); let follow = await followBeta(alpha);
expect(follow.community_view.community.local).toBe(false); expect(follow.community_follower_view.community.local).toBe(false);
expect(follow.community_view.community.name).toBe('main'); expect(follow.community_follower_view.community.name).toBe('main');
// An update to the child comment on beta, should push the post, parent, and child to alpha now // An update to the child comment on beta, should push the post, parent, and child to alpha now
let updatedCommentContent = 'An update child comment from beta'; let updatedCommentContent = 'An update child comment from beta';

View File

@ -67,7 +67,7 @@ test('Delete community', async () => {
); );
// Make sure the follow response went through // Make sure the follow response went through
expect(follow.community_view.community.local).toBe(false); expect(follow.community_follower_view.community.local).toBe(false);
let deleteCommunityRes = await deleteCommunity( let deleteCommunityRes = await deleteCommunity(
beta, beta,
@ -118,7 +118,7 @@ test('Remove community', async () => {
); );
// Make sure the follow response went through // Make sure the follow response went through
expect(follow.community_view.community.local).toBe(false); expect(follow.community_follower_view.community.local).toBe(false);
let removeCommunityRes = await removeCommunity( let removeCommunityRes = await removeCommunity(
beta, beta,

View File

@ -25,8 +25,9 @@ test('Follow federated community', async () => {
); );
// Make sure the follow response went through // Make sure the follow response went through
expect(follow.community_view.community.local).toBe(false); expect(follow.community_follower_view.community.local).toBe(false);
expect(follow.community_view.community.name).toBe('main'); expect(follow.community_follower_view.community.name).toBe('main');
expect(follow.community_follower_view.pending).toBe(true);
// Check it from local // Check it from local
let site = await getSite(alpha); let site = await getSite(alpha);
@ -37,7 +38,7 @@ test('Follow federated community', async () => {
// Test an unfollow // Test an unfollow
let unfollow = await followCommunity(alpha, false, remoteCommunityId); let unfollow = await followCommunity(alpha, false, remoteCommunityId);
expect(unfollow.community_view.community.local).toBe(false); expect(unfollow.community_follower_view).toBeNull()
// Make sure you are unsubbed locally // Make sure you are unsubbed locally
let siteUnfollowCheck = await getSite(alpha); let siteUnfollowCheck = await getSite(alpha);

View File

@ -261,7 +261,7 @@ test('Remove a post from admin and community on same instance', async () => {
expect(removePostRes.post_view.post.removed).toBe(true); expect(removePostRes.post_view.post.removed).toBe(true);
// Make sure lemmy alpha sees post is removed // Make sure lemmy alpha sees post is removed
let alphaPost = await getPost(alpha, postRes.post_view.post.id); // let alphaPost = await getPost(alpha, postRes.post_view.post.id);
// expect(alphaPost.post_view.post.removed).toBe(true); // TODO this shouldn't be commented // expect(alphaPost.post_view.post.removed).toBe(true); // TODO this shouldn't be commented
// assertPostFederation(alphaPost.post_view, removePostRes.post_view); // assertPostFederation(alphaPost.post_view, removePostRes.post_view);

View File

@ -11,6 +11,7 @@ import {
PostResponse, PostResponse,
SearchResponse, SearchResponse,
FollowCommunity, FollowCommunity,
FollowCommunityResponse,
CommunityResponse, CommunityResponse,
GetPostResponse, GetPostResponse,
Register, Register,
@ -298,7 +299,7 @@ export async function banPersonFromSite(
api: API, api: API,
person_id: number, person_id: number,
ban: boolean, ban: boolean,
remove_data: boolean, remove_data: boolean
): Promise<BanPersonResponse> { ): Promise<BanPersonResponse> {
// Make sure lemmy-beta/c/main is cached on lemmy_alpha // Make sure lemmy-beta/c/main is cached on lemmy_alpha
let form: BanPerson = { let form: BanPerson = {
@ -331,7 +332,7 @@ export async function followCommunity(
api: API, api: API,
follow: boolean, follow: boolean,
community_id: number community_id: number
): Promise<CommunityResponse> { ): Promise<FollowCommunityResponse> {
let form: FollowCommunity = { let form: FollowCommunity = {
community_id, community_id,
follow, follow,
@ -558,7 +559,7 @@ export async function saveUserSettings(
} }
export async function deleteUser( export async function deleteUser(
api: API, api: API
): Promise<DeleteAccountResponse> { ): Promise<DeleteAccountResponse> {
let form: DeleteAccount = { let form: DeleteAccount = {
auth: api.auth, auth: api.auth,
@ -602,7 +603,7 @@ export async function unfollowRemotes(
return siteRes; return siteRes;
} }
export async function followBeta(api: API): Promise<CommunityResponse> { export async function followBeta(api: API): Promise<FollowCommunityResponse> {
let betaCommunity = (await resolveBetaCommunity(api)).community; let betaCommunity = (await resolveBetaCommunity(api)).community;
if (betaCommunity) { if (betaCommunity) {
let follow = await followCommunity(api, true, betaCommunity.community.id); let follow = await followCommunity(api, true, betaCommunity.community.id);
@ -613,7 +614,7 @@ export async function followBeta(api: API): Promise<CommunityResponse> {
export async function reportPost( export async function reportPost(
api: API, api: API,
post_id: number, post_id: number,
reason: string, reason: string
): Promise<PostReportResponse> { ): Promise<PostReportResponse> {
let form: CreatePostReport = { let form: CreatePostReport = {
post_id, post_id,
@ -633,7 +634,7 @@ export async function listPostReports(api: API): Promise<ListPostReportsResponse
export async function reportComment( export async function reportComment(
api: API, api: API,
comment_id: number, comment_id: number,
reason: string, reason: string
): Promise<CommentReportResponse> { ): Promise<CommentReportResponse> {
let form: CreateCommentReport = { let form: CreateCommentReport = {
comment_id, comment_id,

View File

@ -7,7 +7,7 @@ import {
saveUserSettings, saveUserSettings,
getSite, getSite,
createPost, createPost,
gamma, // gamma,
resolveCommunity, resolveCommunity,
createComment, createComment,
resolveBetaCommunity, resolveBetaCommunity,

View File

@ -3076,10 +3076,10 @@ language-tags@^1.0.5:
dependencies: dependencies:
language-subtag-registry "~0.3.2" language-subtag-registry "~0.3.2"
lemmy-js-client@0.16.0-rc.1: lemmy-js-client@0.17.0-rc.3:
version "0.16.0-rc.1" version "0.17.0-rc.3"
resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.16.0-rc.1.tgz#14c4a526abf4b171c8afe4efbe2a62dcaf6a6f17" resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.0-rc.3.tgz#3c9b3d5e3346eed8cb8096dad527e72096052223"
integrity sha512-0hR/gHHsokp46whIHGMBQO2zBKWM7bT6mwKNMZxPvyJo+YW9EbKTO5edjF5E4v8nf3FuIE+gFtm5NFAjCaeWJg== integrity sha512-VwKSkjqZhsTrtlKTKs9DEVaj9rlmMEjzn/BxkizU9v03Km7OIs9Z7cOEfperOR9A6q5gh2hWfTDp5eV/0kYkVg==
leven@^3.1.0: leven@^3.1.0:
version "3.1.0" version "3.1.0"

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,14 @@ 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?
.ok();
// 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,12 @@ pub struct CommunityResponse {
pub community_view: CommunityView, pub community_view: CommunityView,
} }
#[derive(Debug, Serialize, Deserialize, Clone)]
/// An unfollow will return None
pub struct FollowCommunityResponse {
pub community_follower_view: Option<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

@ -390,7 +390,7 @@ mod tests {
id: inserted_community_follower.id, id: inserted_community_follower.id,
community_id: inserted_community.id, community_id: inserted_community.id,
person_id: inserted_person.id, person_id: inserted_person.id,
pending: Some(false), pending: false,
published: inserted_community_follower.published, published: inserted_community_follower.published,
}; };

View File

@ -119,7 +119,7 @@ table! {
community_id -> Int4, community_id -> Int4,
person_id -> Int4, person_id -> Int4,
published -> Timestamp, published -> Timestamp,
pending -> Nullable<Bool>, pending -> Bool,
} }
} }

View File

@ -128,7 +128,7 @@ pub struct CommunityFollower {
pub community_id: CommunityId, pub community_id: CommunityId,
pub person_id: PersonId, pub person_id: PersonId,
pub published: chrono::NaiveDateTime, pub published: chrono::NaiveDateTime,
pub pending: Option<bool>, pub pending: bool,
} }
#[derive(Clone)] #[derive(Clone)]

View File

@ -10,7 +10,7 @@ use lemmy_db_schema::{
traits::{ToSafe, ViewToVec}, traits::{ToSafe, ViewToVec},
}; };
type CommunityFollowerViewTuple = (CommunitySafe, PersonSafe); type CommunityFollowerViewTuple = (CommunitySafe, PersonSafe, 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: bool,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]

View File

@ -0,0 +1,5 @@
-- This file should undo anything in `up.sql`
alter table community_follower
alter column pending drop not null,
alter column pending set default false;

View File

@ -0,0 +1,8 @@
-- Make the pending column not null
update community_follower set pending = true where pending is null;
alter table community_follower
alter column pending set not null,
alter column pending drop default;