From 48f5a2ee5e44f381e86b11f148f2e2a99ac94ef6 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Fri, 6 May 2022 17:37:53 -0400 Subject: [PATCH 1/6] Add pending, and change use specific API response for FollowCommunity. Fixes #2246 --- crates/api/src/community/follow.rs | 25 +++++++--------- crates/api_common/src/community.rs | 12 +++++++- .../src/community_follower_view.rs | 29 ++++++++++++++++++- crates/db_views_actor/src/structs.rs | 1 + 4 files changed, 50 insertions(+), 17 deletions(-) diff --git a/crates/api/src/community/follow.rs b/crates/api/src/community/follow.rs index aab21a9c2..df9fae3cf 100644 --- a/crates/api/src/community/follow.rs +++ b/crates/api/src/community/follow.rs @@ -1,7 +1,7 @@ use crate::Perform; use actix_web::web::Data; use lemmy_api_common::{ - community::{CommunityResponse, FollowCommunity}, + community::{FollowCommunity, FollowCommunityResponse}, utils::{ blocking, check_community_ban, @@ -20,20 +20,20 @@ use lemmy_db_schema::{ source::community::{Community, CommunityFollower, CommunityFollowerForm}, traits::{Crud, Followable}, }; -use lemmy_db_views_actor::structs::CommunityView; +use lemmy_db_views_actor::structs::CommunityFollowerView; use lemmy_utils::{ConnectionId, LemmyError}; use lemmy_websocket::LemmyContext; #[async_trait::async_trait(?Send)] impl Perform for FollowCommunity { - type Response = CommunityResponse; + type Response = FollowCommunityResponse; #[tracing::instrument(skip(context, _websocket_id))] async fn perform( &self, context: &Data, _websocket_id: Option, - ) -> Result { + ) -> Result { let data: &FollowCommunity = self; let local_user_view = 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 { community_id: data.community_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 { @@ -82,18 +82,13 @@ impl Perform for FollowCommunity { let community_id = data.community_id; let person_id = local_user_view.person.id; - let mut community_view = blocking(context.pool(), move |conn| { - CommunityView::read(conn, community_id, Some(person_id)) + let community_follower_view = blocking(context.pool(), move |conn| { + CommunityFollowerView::read(conn, community_id, person_id) }) .await??; - // TODO: this needs to return a "pending" state, until Accept is received from the remote server - // For now, just assume that remote follows are accepted. - // Otherwise, the subscribed will be null - if !community.local { - community_view.subscribed = data.follow; - } - - Ok(CommunityResponse { community_view }) + Ok(Self::Response { + community_follower_view, + }) } } diff --git a/crates/api_common/src/community.rs b/crates/api_common/src/community.rs index 90c86f1c2..a4b5e2e6f 100644 --- a/crates/api_common/src/community.rs +++ b/crates/api_common/src/community.rs @@ -5,7 +5,12 @@ use lemmy_db_schema::{ ListingType, SortType, }; -use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView, PersonViewSafe}; +use lemmy_db_views_actor::structs::{ + CommunityFollowerView, + CommunityModeratorView, + CommunityView, + PersonViewSafe, +}; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize, Clone, Default)] @@ -41,6 +46,11 @@ pub struct CommunityResponse { pub community_view: CommunityView, } +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct FollowCommunityResponse { + pub community_follower_view: CommunityFollowerView, +} + #[derive(Debug, Serialize, Deserialize, Clone, Default)] pub struct ListCommunities { pub type_: Option, diff --git a/crates/db_views_actor/src/community_follower_view.rs b/crates/db_views_actor/src/community_follower_view.rs index 6d5d94a59..7e37e446a 100644 --- a/crates/db_views_actor/src/community_follower_view.rs +++ b/crates/db_views_actor/src/community_follower_view.rs @@ -10,7 +10,7 @@ use lemmy_db_schema::{ traits::{ToSafe, ViewToVec}, }; -type CommunityFollowerViewTuple = (CommunitySafe, PersonSafe); +type CommunityFollowerViewTuple = (CommunitySafe, PersonSafe, Option); impl CommunityFollowerView { pub fn for_community(conn: &PgConnection, community_id: CommunityId) -> Result, Error> { @@ -20,6 +20,7 @@ impl CommunityFollowerView { .select(( Community::safe_columns_tuple(), Person::safe_columns_tuple(), + community_follower::pending, )) .filter(community_follower::community_id.eq(community_id)) .order_by(community::title) @@ -35,6 +36,7 @@ impl CommunityFollowerView { .select(( Community::safe_columns_tuple(), Person::safe_columns_tuple(), + community_follower::pending, )) .filter(community_follower::person_id.eq(person_id)) .order_by(community::title) @@ -42,6 +44,30 @@ impl CommunityFollowerView { Ok(Self::from_tuple_to_vec(res)) } + + pub fn read( + conn: &PgConnection, + community_id: CommunityId, + person_id: PersonId, + ) -> Result { + 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::(conn)?; + + Ok(Self { + community, + follower, + pending, + }) + } } impl ViewToVec for CommunityFollowerView { @@ -52,6 +78,7 @@ impl ViewToVec for CommunityFollowerView { .map(|a| Self { community: a.0.to_owned(), follower: a.1.to_owned(), + pending: a.2.to_owned(), }) .collect::>() } diff --git a/crates/db_views_actor/src/structs.rs b/crates/db_views_actor/src/structs.rs index a6ec9710e..25d2c178e 100644 --- a/crates/db_views_actor/src/structs.rs +++ b/crates/db_views_actor/src/structs.rs @@ -20,6 +20,7 @@ pub struct CommunityBlockView { pub struct CommunityFollowerView { pub community: CommunitySafe, pub follower: PersonSafe, + pub pending: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] From ed9b66541e9a7208deb556434e441049f5cf6c34 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sat, 7 May 2022 19:04:47 -0400 Subject: [PATCH 2/6] Fixing API tests. --- api_tests/package.json | 2 +- api_tests/src/comment.spec.ts | 12 ++++++------ api_tests/src/community.spec.ts | 4 ++-- api_tests/src/follow.spec.ts | 7 ++++--- api_tests/src/post.spec.ts | 2 +- api_tests/src/shared.ts | 13 +++++++------ api_tests/src/user.spec.ts | 2 +- api_tests/yarn.lock | 8 ++++---- 8 files changed, 26 insertions(+), 24 deletions(-) diff --git a/api_tests/package.json b/api_tests/package.json index 3aa57d49c..7f3a80c92 100644 --- a/api_tests/package.json +++ b/api_tests/package.json @@ -16,7 +16,7 @@ "eslint": "^7.30.0", "eslint-plugin-jane": "^9.0.3", "jest": "^27.0.6", - "lemmy-js-client": "0.16.0-rc.1", + "lemmy-js-client": "0.17.0-rc.1", "node-fetch": "^2.6.1", "prettier": "^2.3.2", "ts-jest": "^27.0.3", diff --git a/api_tests/src/comment.spec.ts b/api_tests/src/comment.spec.ts index 314f18ecc..cfb09a517 100644 --- a/api_tests/src/comment.spec.ts +++ b/api_tests/src/comment.spec.ts @@ -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 () => { // Alpha and gamma users follow beta community let alphaFollow = await followBeta(alpha); - expect(alphaFollow.community_view.community.local).toBe(false); - expect(alphaFollow.community_view.community.name).toBe('main'); + expect(alphaFollow.community_follower_view.community.local).toBe(false); + expect(alphaFollow.community_follower_view.community.name).toBe('main'); let gammaFollow = await followBeta(gamma); - expect(gammaFollow.community_view.community.local).toBe(false); - expect(gammaFollow.community_view.community.name).toBe('main'); + expect(gammaFollow.community_follower_view.community.local).toBe(false); + expect(gammaFollow.community_follower_view.community.name).toBe('main'); // Create a post on beta 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 let follow = await followBeta(alpha); - expect(follow.community_view.community.local).toBe(false); - expect(follow.community_view.community.name).toBe('main'); + expect(follow.community_follower_view.community.local).toBe(false); + 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 let updatedCommentContent = 'An update child comment from beta'; diff --git a/api_tests/src/community.spec.ts b/api_tests/src/community.spec.ts index e1f6825ae..ba12de5c4 100644 --- a/api_tests/src/community.spec.ts +++ b/api_tests/src/community.spec.ts @@ -67,7 +67,7 @@ test('Delete community', async () => { ); // 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( beta, @@ -118,7 +118,7 @@ test('Remove community', async () => { ); // 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( beta, diff --git a/api_tests/src/follow.spec.ts b/api_tests/src/follow.spec.ts index 078c382c2..a78a7e73f 100644 --- a/api_tests/src/follow.spec.ts +++ b/api_tests/src/follow.spec.ts @@ -25,8 +25,9 @@ test('Follow federated community', async () => { ); // Make sure the follow response went through - expect(follow.community_view.community.local).toBe(false); - expect(follow.community_view.community.name).toBe('main'); + expect(follow.community_follower_view.community.local).toBe(false); + expect(follow.community_follower_view.community.name).toBe('main'); + expect(follow.community_follower_view.pending).toBe(true); // Check it from local let site = await getSite(alpha); @@ -37,7 +38,7 @@ test('Follow federated community', async () => { // Test an unfollow let unfollow = await followCommunity(alpha, false, remoteCommunityId); - expect(unfollow.community_view.community.local).toBe(false); + expect(unfollow.community_follower_view.community.local).toBe(false); // Make sure you are unsubbed locally let siteUnfollowCheck = await getSite(alpha); diff --git a/api_tests/src/post.spec.ts b/api_tests/src/post.spec.ts index b2f12298c..82545528e 100644 --- a/api_tests/src/post.spec.ts +++ b/api_tests/src/post.spec.ts @@ -261,7 +261,7 @@ test('Remove a post from admin and community on same instance', async () => { expect(removePostRes.post_view.post.removed).toBe(true); // 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 // assertPostFederation(alphaPost.post_view, removePostRes.post_view); diff --git a/api_tests/src/shared.ts b/api_tests/src/shared.ts index d5be61ac3..d4b75c7e4 100644 --- a/api_tests/src/shared.ts +++ b/api_tests/src/shared.ts @@ -11,6 +11,7 @@ import { PostResponse, SearchResponse, FollowCommunity, + FollowCommunityResponse, CommunityResponse, GetPostResponse, Register, @@ -298,7 +299,7 @@ export async function banPersonFromSite( api: API, person_id: number, ban: boolean, - remove_data: boolean, + remove_data: boolean ): Promise { // Make sure lemmy-beta/c/main is cached on lemmy_alpha let form: BanPerson = { @@ -331,7 +332,7 @@ export async function followCommunity( api: API, follow: boolean, community_id: number -): Promise { +): Promise { let form: FollowCommunity = { community_id, follow, @@ -558,7 +559,7 @@ export async function saveUserSettings( } export async function deleteUser( - api: API, + api: API ): Promise { let form: DeleteAccount = { auth: api.auth, @@ -602,7 +603,7 @@ export async function unfollowRemotes( return siteRes; } -export async function followBeta(api: API): Promise { +export async function followBeta(api: API): Promise { let betaCommunity = (await resolveBetaCommunity(api)).community; if (betaCommunity) { let follow = await followCommunity(api, true, betaCommunity.community.id); @@ -613,7 +614,7 @@ export async function followBeta(api: API): Promise { export async function reportPost( api: API, post_id: number, - reason: string, + reason: string ): Promise { let form: CreatePostReport = { post_id, @@ -633,7 +634,7 @@ export async function listPostReports(api: API): Promise { let form: CreateCommentReport = { comment_id, diff --git a/api_tests/src/user.spec.ts b/api_tests/src/user.spec.ts index 29029ac1e..7e45760e5 100644 --- a/api_tests/src/user.spec.ts +++ b/api_tests/src/user.spec.ts @@ -7,7 +7,7 @@ import { saveUserSettings, getSite, createPost, - gamma, + // gamma, resolveCommunity, createComment, resolveBetaCommunity, diff --git a/api_tests/yarn.lock b/api_tests/yarn.lock index c60da9dad..e618b26a1 100644 --- a/api_tests/yarn.lock +++ b/api_tests/yarn.lock @@ -3076,10 +3076,10 @@ language-tags@^1.0.5: dependencies: language-subtag-registry "~0.3.2" -lemmy-js-client@0.16.0-rc.1: - version "0.16.0-rc.1" - resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.16.0-rc.1.tgz#14c4a526abf4b171c8afe4efbe2a62dcaf6a6f17" - integrity sha512-0hR/gHHsokp46whIHGMBQO2zBKWM7bT6mwKNMZxPvyJo+YW9EbKTO5edjF5E4v8nf3FuIE+gFtm5NFAjCaeWJg== +lemmy-js-client@0.17.0-rc.1: + version "0.17.0-rc.1" + resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.0-rc.1.tgz#153fae5a06f047fe59a00847589b80736a30277b" + integrity sha512-dHkL5eXHTSxchyxn8yXj33iuh1UQ8OmebUJY6PstQPxL8ZOFBKlVEJs8UlXBJgeDkDTc2OObNpUd2uFqhEt6jg== leven@^3.1.0: version "3.1.0" From e6fe9d0325f01bd6f1c1b8c269366a676ad5cee1 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sat, 7 May 2022 20:21:30 -0400 Subject: [PATCH 3/6] Fixing unfollow --- api_tests/package.json | 2 +- api_tests/src/follow.spec.ts | 2 +- api_tests/yarn.lock | 8 ++++---- crates/api/src/community/follow.rs | 2 +- crates/api_common/src/community.rs | 3 ++- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/api_tests/package.json b/api_tests/package.json index 7f3a80c92..849b721e7 100644 --- a/api_tests/package.json +++ b/api_tests/package.json @@ -16,7 +16,7 @@ "eslint": "^7.30.0", "eslint-plugin-jane": "^9.0.3", "jest": "^27.0.6", - "lemmy-js-client": "0.17.0-rc.1", + "lemmy-js-client": "0.17.0-rc.2", "node-fetch": "^2.6.1", "prettier": "^2.3.2", "ts-jest": "^27.0.3", diff --git a/api_tests/src/follow.spec.ts b/api_tests/src/follow.spec.ts index a78a7e73f..d75735b00 100644 --- a/api_tests/src/follow.spec.ts +++ b/api_tests/src/follow.spec.ts @@ -38,7 +38,7 @@ test('Follow federated community', async () => { // Test an unfollow let unfollow = await followCommunity(alpha, false, remoteCommunityId); - expect(unfollow.community_follower_view.community.local).toBe(false); + expect(unfollow.community_follower_view).toBeNull() // Make sure you are unsubbed locally let siteUnfollowCheck = await getSite(alpha); diff --git a/api_tests/yarn.lock b/api_tests/yarn.lock index e618b26a1..8e0754863 100644 --- a/api_tests/yarn.lock +++ b/api_tests/yarn.lock @@ -3076,10 +3076,10 @@ language-tags@^1.0.5: dependencies: language-subtag-registry "~0.3.2" -lemmy-js-client@0.17.0-rc.1: - version "0.17.0-rc.1" - resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.0-rc.1.tgz#153fae5a06f047fe59a00847589b80736a30277b" - integrity sha512-dHkL5eXHTSxchyxn8yXj33iuh1UQ8OmebUJY6PstQPxL8ZOFBKlVEJs8UlXBJgeDkDTc2OObNpUd2uFqhEt6jg== +lemmy-js-client@0.17.0-rc.2: + version "0.17.0-rc.2" + resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.0-rc.2.tgz#4e6ff9a8d83ac922cd36eeaa01c657b3b93309e6" + integrity sha512-2YkZiAkq2ZUHPSl/B7pvMMkI19XRtTKwLFJ1u4NT2BlFkNdlvkvkOddnJ6aRwKAp/WBohxoKLoDHhlwePS5gqA== leven@^3.1.0: version "3.1.0" diff --git a/crates/api/src/community/follow.rs b/crates/api/src/community/follow.rs index df9fae3cf..b73e130d1 100644 --- a/crates/api/src/community/follow.rs +++ b/crates/api/src/community/follow.rs @@ -85,7 +85,7 @@ impl Perform for FollowCommunity { let community_follower_view = blocking(context.pool(), move |conn| { CommunityFollowerView::read(conn, community_id, person_id) }) - .await??; + .await?.ok(); Ok(Self::Response { community_follower_view, diff --git a/crates/api_common/src/community.rs b/crates/api_common/src/community.rs index a4b5e2e6f..3c0f33d1c 100644 --- a/crates/api_common/src/community.rs +++ b/crates/api_common/src/community.rs @@ -47,8 +47,9 @@ pub struct CommunityResponse { } #[derive(Debug, Serialize, Deserialize, Clone)] +/// An unfollow will return None pub struct FollowCommunityResponse { - pub community_follower_view: CommunityFollowerView, + pub community_follower_view: Option, } #[derive(Debug, Serialize, Deserialize, Clone, Default)] From 763dc9668c0f483bf84393d5d456d832e511b466 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Sat, 7 May 2022 22:08:53 -0400 Subject: [PATCH 4/6] Fix formatting. --- crates/api/src/community/follow.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/api/src/community/follow.rs b/crates/api/src/community/follow.rs index b73e130d1..a61383b21 100644 --- a/crates/api/src/community/follow.rs +++ b/crates/api/src/community/follow.rs @@ -85,7 +85,8 @@ impl Perform for FollowCommunity { let community_follower_view = blocking(context.pool(), move |conn| { CommunityFollowerView::read(conn, community_id, person_id) }) - .await?.ok(); + .await? + .ok(); Ok(Self::Response { community_follower_view, From d4ee171b085d8c7168b3c21b23177377360ff92f Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 10 May 2022 14:14:01 -0400 Subject: [PATCH 5/6] Making community_follower.pending column not null. --- api_tests/package.json | 2 +- api_tests/yarn.lock | 8 ++++---- crates/api/src/community/ban.rs | 2 +- crates/api/src/community/block.rs | 2 +- crates/api/src/community/follow.rs | 2 +- crates/api_crud/src/community/create.rs | 2 +- crates/api_crud/src/user/create.rs | 2 +- crates/apub/src/activities/block/block_user.rs | 2 +- crates/apub/src/activities/following/follow.rs | 4 ++-- crates/apub/src/activities/following/undo_follow.rs | 2 +- crates/db_schema/src/aggregates/community_aggregates.rs | 6 +++--- crates/db_schema/src/impls/community.rs | 4 ++-- crates/db_schema/src/schema.rs | 2 +- crates/db_schema/src/source/community.rs | 4 ++-- crates/db_views_actor/src/community_follower_view.rs | 2 +- crates/db_views_actor/src/structs.rs | 2 +- .../2022-05-10-173801_change_pending_to_notnull/down.sql | 5 +++++ .../2022-05-10-173801_change_pending_to_notnull/up.sql | 8 ++++++++ 18 files changed, 37 insertions(+), 24 deletions(-) create mode 100644 migrations/2022-05-10-173801_change_pending_to_notnull/down.sql create mode 100644 migrations/2022-05-10-173801_change_pending_to_notnull/up.sql diff --git a/api_tests/package.json b/api_tests/package.json index 849b721e7..4deffe4f6 100644 --- a/api_tests/package.json +++ b/api_tests/package.json @@ -16,7 +16,7 @@ "eslint": "^7.30.0", "eslint-plugin-jane": "^9.0.3", "jest": "^27.0.6", - "lemmy-js-client": "0.17.0-rc.2", + "lemmy-js-client": "0.17.0-rc.3", "node-fetch": "^2.6.1", "prettier": "^2.3.2", "ts-jest": "^27.0.3", diff --git a/api_tests/yarn.lock b/api_tests/yarn.lock index 8e0754863..3b198755d 100644 --- a/api_tests/yarn.lock +++ b/api_tests/yarn.lock @@ -3076,10 +3076,10 @@ language-tags@^1.0.5: dependencies: language-subtag-registry "~0.3.2" -lemmy-js-client@0.17.0-rc.2: - version "0.17.0-rc.2" - resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.0-rc.2.tgz#4e6ff9a8d83ac922cd36eeaa01c657b3b93309e6" - integrity sha512-2YkZiAkq2ZUHPSl/B7pvMMkI19XRtTKwLFJ1u4NT2BlFkNdlvkvkOddnJ6aRwKAp/WBohxoKLoDHhlwePS5gqA== +lemmy-js-client@0.17.0-rc.3: + version "0.17.0-rc.3" + resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.17.0-rc.3.tgz#3c9b3d5e3346eed8cb8096dad527e72096052223" + integrity sha512-VwKSkjqZhsTrtlKTKs9DEVaj9rlmMEjzn/BxkizU9v03Km7OIs9Z7cOEfperOR9A6q5gh2hWfTDp5eV/0kYkVg== leven@^3.1.0: version "3.1.0" diff --git a/crates/api/src/community/ban.rs b/crates/api/src/community/ban.rs index 60fd32270..f4a727ebe 100644 --- a/crates/api/src/community/ban.rs +++ b/crates/api/src/community/ban.rs @@ -76,7 +76,7 @@ impl Perform for BanFromCommunity { let community_follower_form = CommunityFollowerForm { community_id: data.community_id, person_id: banned_person_id, - pending: false, + pending: Some(false), }; blocking(context.pool(), move |conn: &'_ _| { CommunityFollower::unfollow(conn, &community_follower_form) diff --git a/crates/api/src/community/block.rs b/crates/api/src/community/block.rs index 4f740c916..a8672e3e2 100644 --- a/crates/api/src/community/block.rs +++ b/crates/api/src/community/block.rs @@ -47,7 +47,7 @@ impl Perform for BlockCommunity { let community_follower_form = CommunityFollowerForm { community_id: data.community_id, person_id, - pending: false, + pending: Some(false), }; blocking(context.pool(), move |conn: &'_ _| { CommunityFollower::unfollow(conn, &community_follower_form) diff --git a/crates/api/src/community/follow.rs b/crates/api/src/community/follow.rs index a61383b21..15a56b17b 100644 --- a/crates/api/src/community/follow.rs +++ b/crates/api/src/community/follow.rs @@ -47,7 +47,7 @@ impl Perform for FollowCommunity { let community_follower_form = CommunityFollowerForm { community_id: data.community_id, person_id: local_user_view.person.id, - pending: false, // Don't worry, this form isn't used for remote follows + pending: Some(false), // Don't worry, this form isn't used for remote follows }; if community.local { diff --git a/crates/api_crud/src/community/create.rs b/crates/api_crud/src/community/create.rs index a7582d2ec..451e9bfa0 100644 --- a/crates/api_crud/src/community/create.rs +++ b/crates/api_crud/src/community/create.rs @@ -123,7 +123,7 @@ impl PerformCrud for CreateCommunity { let community_follower_form = CommunityFollowerForm { community_id: inserted_community.id, person_id: local_user_view.person.id, - pending: false, + pending: Some(false), }; let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form); diff --git a/crates/api_crud/src/user/create.rs b/crates/api_crud/src/user/create.rs index 456617043..6752dcc84 100644 --- a/crates/api_crud/src/user/create.rs +++ b/crates/api_crud/src/user/create.rs @@ -232,7 +232,7 @@ impl PerformCrud for Register { let community_follower_form = CommunityFollowerForm { community_id: main_community.id, person_id: inserted_person.id, - pending: false, + pending: Some(false), }; let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form); diff --git a/crates/apub/src/activities/block/block_user.rs b/crates/apub/src/activities/block/block_user.rs index bcab3148a..c05f2f87a 100644 --- a/crates/apub/src/activities/block/block_user.rs +++ b/crates/apub/src/activities/block/block_user.rs @@ -200,7 +200,7 @@ impl ActivityHandler for BlockUser { let community_follower_form = CommunityFollowerForm { community_id: community.id, person_id: blocked_person.id, - pending: false, + pending: Some(false), }; blocking(context.pool(), move |conn: &'_ _| { CommunityFollower::unfollow(conn, &community_follower_form) diff --git a/crates/apub/src/activities/following/follow.rs b/crates/apub/src/activities/following/follow.rs index d46b3dc20..eb70cb91f 100644 --- a/crates/apub/src/activities/following/follow.rs +++ b/crates/apub/src/activities/following/follow.rs @@ -50,7 +50,7 @@ impl FollowCommunity { let community_follower_form = CommunityFollowerForm { community_id: community.id, person_id: actor.id, - pending: true, + pending: Some(true), }; blocking(context.pool(), move |conn| { CommunityFollower::follow(conn, &community_follower_form).ok() @@ -100,7 +100,7 @@ impl ActivityHandler for FollowCommunity { let community_follower_form = CommunityFollowerForm { community_id: community.id, person_id: person.id, - pending: false, + pending: Some(false), }; // This will fail if they're already a follower, but ignore the error. diff --git a/crates/apub/src/activities/following/undo_follow.rs b/crates/apub/src/activities/following/undo_follow.rs index f70acdf27..f579d2323 100644 --- a/crates/apub/src/activities/following/undo_follow.rs +++ b/crates/apub/src/activities/following/undo_follow.rs @@ -77,7 +77,7 @@ impl ActivityHandler for UndoFollowCommunity { let community_follower_form = CommunityFollowerForm { community_id: community.id, person_id: person.id, - pending: false, + pending: Some(false), }; // This will fail if they aren't a follower, but ignore the error. diff --git a/crates/db_schema/src/aggregates/community_aggregates.rs b/crates/db_schema/src/aggregates/community_aggregates.rs index 950e68d37..fc8cf0b06 100644 --- a/crates/db_schema/src/aggregates/community_aggregates.rs +++ b/crates/db_schema/src/aggregates/community_aggregates.rs @@ -66,7 +66,7 @@ mod tests { let first_person_follow = CommunityFollowerForm { community_id: inserted_community.id, person_id: inserted_person.id, - pending: false, + pending: Some(false), }; CommunityFollower::follow(&conn, &first_person_follow).unwrap(); @@ -74,7 +74,7 @@ mod tests { let second_person_follow = CommunityFollowerForm { community_id: inserted_community.id, person_id: another_inserted_person.id, - pending: false, + pending: Some(false), }; CommunityFollower::follow(&conn, &second_person_follow).unwrap(); @@ -82,7 +82,7 @@ mod tests { let another_community_follow = CommunityFollowerForm { community_id: another_inserted_community.id, person_id: inserted_person.id, - pending: false, + pending: Some(false), }; CommunityFollower::follow(&conn, &another_community_follow).unwrap(); diff --git a/crates/db_schema/src/impls/community.rs b/crates/db_schema/src/impls/community.rs index 14a670f51..18619de6f 100644 --- a/crates/db_schema/src/impls/community.rs +++ b/crates/db_schema/src/impls/community.rs @@ -380,7 +380,7 @@ mod tests { let community_follower_form = CommunityFollowerForm { community_id: inserted_community.id, person_id: inserted_person.id, - pending: false, + pending: Some(false), }; let inserted_community_follower = @@ -390,7 +390,7 @@ mod tests { id: inserted_community_follower.id, community_id: inserted_community.id, person_id: inserted_person.id, - pending: Some(false), + pending: false, published: inserted_community_follower.published, }; diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs index 666986a69..74285e3c6 100644 --- a/crates/db_schema/src/schema.rs +++ b/crates/db_schema/src/schema.rs @@ -119,7 +119,7 @@ table! { community_id -> Int4, person_id -> Int4, published -> Timestamp, - pending -> Nullable, + pending -> Bool, } } diff --git a/crates/db_schema/src/source/community.rs b/crates/db_schema/src/source/community.rs index 7693f2f41..9700e07a6 100644 --- a/crates/db_schema/src/source/community.rs +++ b/crates/db_schema/src/source/community.rs @@ -128,7 +128,7 @@ pub struct CommunityFollower { pub community_id: CommunityId, pub person_id: PersonId, pub published: chrono::NaiveDateTime, - pub pending: Option, + pub pending: bool, } #[derive(Clone)] @@ -137,5 +137,5 @@ pub struct CommunityFollower { pub struct CommunityFollowerForm { pub community_id: CommunityId, pub person_id: PersonId, - pub pending: bool, + pub pending: Option, } diff --git a/crates/db_views_actor/src/community_follower_view.rs b/crates/db_views_actor/src/community_follower_view.rs index 7e37e446a..7bece59d3 100644 --- a/crates/db_views_actor/src/community_follower_view.rs +++ b/crates/db_views_actor/src/community_follower_view.rs @@ -10,7 +10,7 @@ use lemmy_db_schema::{ traits::{ToSafe, ViewToVec}, }; -type CommunityFollowerViewTuple = (CommunitySafe, PersonSafe, Option); +type CommunityFollowerViewTuple = (CommunitySafe, PersonSafe, bool); impl CommunityFollowerView { pub fn for_community(conn: &PgConnection, community_id: CommunityId) -> Result, Error> { diff --git a/crates/db_views_actor/src/structs.rs b/crates/db_views_actor/src/structs.rs index 25d2c178e..a0b1ad4bc 100644 --- a/crates/db_views_actor/src/structs.rs +++ b/crates/db_views_actor/src/structs.rs @@ -20,7 +20,7 @@ pub struct CommunityBlockView { pub struct CommunityFollowerView { pub community: CommunitySafe, pub follower: PersonSafe, - pub pending: Option, + pub pending: bool, } #[derive(Debug, Serialize, Deserialize, Clone)] diff --git a/migrations/2022-05-10-173801_change_pending_to_notnull/down.sql b/migrations/2022-05-10-173801_change_pending_to_notnull/down.sql new file mode 100644 index 000000000..4ef1387af --- /dev/null +++ b/migrations/2022-05-10-173801_change_pending_to_notnull/down.sql @@ -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; diff --git a/migrations/2022-05-10-173801_change_pending_to_notnull/up.sql b/migrations/2022-05-10-173801_change_pending_to_notnull/up.sql new file mode 100644 index 000000000..c589ac62c --- /dev/null +++ b/migrations/2022-05-10-173801_change_pending_to_notnull/up.sql @@ -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 set default true; + From 3c111b3062e69b19e68a9b537c23a7e61ae34e94 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 17 May 2022 14:02:48 -0400 Subject: [PATCH 6/6] Dropping default on pending column. --- crates/api/src/community/ban.rs | 2 +- crates/api/src/community/block.rs | 2 +- crates/api/src/community/follow.rs | 2 +- crates/api_crud/src/community/create.rs | 2 +- crates/api_crud/src/user/create.rs | 2 +- crates/apub/src/activities/block/block_user.rs | 2 +- crates/apub/src/activities/following/follow.rs | 4 ++-- crates/apub/src/activities/following/undo_follow.rs | 2 +- crates/db_schema/src/aggregates/community_aggregates.rs | 6 +++--- crates/db_schema/src/impls/community.rs | 2 +- crates/db_schema/src/source/community.rs | 2 +- .../2022-05-10-173801_change_pending_to_notnull/up.sql | 2 +- 12 files changed, 15 insertions(+), 15 deletions(-) diff --git a/crates/api/src/community/ban.rs b/crates/api/src/community/ban.rs index f4a727ebe..60fd32270 100644 --- a/crates/api/src/community/ban.rs +++ b/crates/api/src/community/ban.rs @@ -76,7 +76,7 @@ impl Perform for BanFromCommunity { let community_follower_form = CommunityFollowerForm { community_id: data.community_id, person_id: banned_person_id, - pending: Some(false), + pending: false, }; blocking(context.pool(), move |conn: &'_ _| { CommunityFollower::unfollow(conn, &community_follower_form) diff --git a/crates/api/src/community/block.rs b/crates/api/src/community/block.rs index a8672e3e2..4f740c916 100644 --- a/crates/api/src/community/block.rs +++ b/crates/api/src/community/block.rs @@ -47,7 +47,7 @@ impl Perform for BlockCommunity { let community_follower_form = CommunityFollowerForm { community_id: data.community_id, person_id, - pending: Some(false), + pending: false, }; blocking(context.pool(), move |conn: &'_ _| { CommunityFollower::unfollow(conn, &community_follower_form) diff --git a/crates/api/src/community/follow.rs b/crates/api/src/community/follow.rs index 15a56b17b..a61383b21 100644 --- a/crates/api/src/community/follow.rs +++ b/crates/api/src/community/follow.rs @@ -47,7 +47,7 @@ impl Perform for FollowCommunity { let community_follower_form = CommunityFollowerForm { community_id: data.community_id, person_id: local_user_view.person.id, - pending: Some(false), // Don't worry, this form isn't used for remote follows + pending: false, // Don't worry, this form isn't used for remote follows }; if community.local { diff --git a/crates/api_crud/src/community/create.rs b/crates/api_crud/src/community/create.rs index 451e9bfa0..a7582d2ec 100644 --- a/crates/api_crud/src/community/create.rs +++ b/crates/api_crud/src/community/create.rs @@ -123,7 +123,7 @@ impl PerformCrud for CreateCommunity { let community_follower_form = CommunityFollowerForm { community_id: inserted_community.id, person_id: local_user_view.person.id, - pending: Some(false), + pending: false, }; let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form); diff --git a/crates/api_crud/src/user/create.rs b/crates/api_crud/src/user/create.rs index 6752dcc84..456617043 100644 --- a/crates/api_crud/src/user/create.rs +++ b/crates/api_crud/src/user/create.rs @@ -232,7 +232,7 @@ impl PerformCrud for Register { let community_follower_form = CommunityFollowerForm { community_id: main_community.id, person_id: inserted_person.id, - pending: Some(false), + pending: false, }; let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form); diff --git a/crates/apub/src/activities/block/block_user.rs b/crates/apub/src/activities/block/block_user.rs index c05f2f87a..bcab3148a 100644 --- a/crates/apub/src/activities/block/block_user.rs +++ b/crates/apub/src/activities/block/block_user.rs @@ -200,7 +200,7 @@ impl ActivityHandler for BlockUser { let community_follower_form = CommunityFollowerForm { community_id: community.id, person_id: blocked_person.id, - pending: Some(false), + pending: false, }; blocking(context.pool(), move |conn: &'_ _| { CommunityFollower::unfollow(conn, &community_follower_form) diff --git a/crates/apub/src/activities/following/follow.rs b/crates/apub/src/activities/following/follow.rs index eb70cb91f..d46b3dc20 100644 --- a/crates/apub/src/activities/following/follow.rs +++ b/crates/apub/src/activities/following/follow.rs @@ -50,7 +50,7 @@ impl FollowCommunity { let community_follower_form = CommunityFollowerForm { community_id: community.id, person_id: actor.id, - pending: Some(true), + pending: true, }; blocking(context.pool(), move |conn| { CommunityFollower::follow(conn, &community_follower_form).ok() @@ -100,7 +100,7 @@ impl ActivityHandler for FollowCommunity { let community_follower_form = CommunityFollowerForm { community_id: community.id, person_id: person.id, - pending: Some(false), + pending: false, }; // This will fail if they're already a follower, but ignore the error. diff --git a/crates/apub/src/activities/following/undo_follow.rs b/crates/apub/src/activities/following/undo_follow.rs index f579d2323..f70acdf27 100644 --- a/crates/apub/src/activities/following/undo_follow.rs +++ b/crates/apub/src/activities/following/undo_follow.rs @@ -77,7 +77,7 @@ impl ActivityHandler for UndoFollowCommunity { let community_follower_form = CommunityFollowerForm { community_id: community.id, person_id: person.id, - pending: Some(false), + pending: false, }; // This will fail if they aren't a follower, but ignore the error. diff --git a/crates/db_schema/src/aggregates/community_aggregates.rs b/crates/db_schema/src/aggregates/community_aggregates.rs index fc8cf0b06..950e68d37 100644 --- a/crates/db_schema/src/aggregates/community_aggregates.rs +++ b/crates/db_schema/src/aggregates/community_aggregates.rs @@ -66,7 +66,7 @@ mod tests { let first_person_follow = CommunityFollowerForm { community_id: inserted_community.id, person_id: inserted_person.id, - pending: Some(false), + pending: false, }; CommunityFollower::follow(&conn, &first_person_follow).unwrap(); @@ -74,7 +74,7 @@ mod tests { let second_person_follow = CommunityFollowerForm { community_id: inserted_community.id, person_id: another_inserted_person.id, - pending: Some(false), + pending: false, }; CommunityFollower::follow(&conn, &second_person_follow).unwrap(); @@ -82,7 +82,7 @@ mod tests { let another_community_follow = CommunityFollowerForm { community_id: another_inserted_community.id, person_id: inserted_person.id, - pending: Some(false), + pending: false, }; CommunityFollower::follow(&conn, &another_community_follow).unwrap(); diff --git a/crates/db_schema/src/impls/community.rs b/crates/db_schema/src/impls/community.rs index 18619de6f..073255cdb 100644 --- a/crates/db_schema/src/impls/community.rs +++ b/crates/db_schema/src/impls/community.rs @@ -380,7 +380,7 @@ mod tests { let community_follower_form = CommunityFollowerForm { community_id: inserted_community.id, person_id: inserted_person.id, - pending: Some(false), + pending: false, }; let inserted_community_follower = diff --git a/crates/db_schema/src/source/community.rs b/crates/db_schema/src/source/community.rs index 9700e07a6..25766b408 100644 --- a/crates/db_schema/src/source/community.rs +++ b/crates/db_schema/src/source/community.rs @@ -137,5 +137,5 @@ pub struct CommunityFollower { pub struct CommunityFollowerForm { pub community_id: CommunityId, pub person_id: PersonId, - pub pending: Option, + pub pending: bool, } diff --git a/migrations/2022-05-10-173801_change_pending_to_notnull/up.sql b/migrations/2022-05-10-173801_change_pending_to_notnull/up.sql index c589ac62c..659f321bc 100644 --- a/migrations/2022-05-10-173801_change_pending_to_notnull/up.sql +++ b/migrations/2022-05-10-173801_change_pending_to_notnull/up.sql @@ -4,5 +4,5 @@ update community_follower set pending = true where pending is null; alter table community_follower alter column pending set not null, - alter column pending set default true; + alter column pending drop default;