From b0f9b3537e7f62b9cd54153adc09cf4d6f713993 Mon Sep 17 00:00:00 2001 From: silverpill Date: Mon, 12 Dec 2022 23:50:40 +0000 Subject: [PATCH] Add activity_id column to follow_request table --- ...V0036__follow_request__add_activity_id.sql | 1 + migrations/schema.sql | 1 + src/mastodon_api/accounts/views.rs | 6 +++- src/models/relationships/queries.rs | 32 +++++++------------ src/models/relationships/types.rs | 3 +- 5 files changed, 21 insertions(+), 22 deletions(-) create mode 100644 migrations/V0036__follow_request__add_activity_id.sql diff --git a/migrations/V0036__follow_request__add_activity_id.sql b/migrations/V0036__follow_request__add_activity_id.sql new file mode 100644 index 0000000..16eafb1 --- /dev/null +++ b/migrations/V0036__follow_request__add_activity_id.sql @@ -0,0 +1 @@ +ALTER TABLE follow_request ADD COLUMN activity_id VARCHAR(250) UNIQUE; diff --git a/migrations/schema.sql b/migrations/schema.sql index c9c9798..10bc695 100644 --- a/migrations/schema.sql +++ b/migrations/schema.sql @@ -69,6 +69,7 @@ CREATE TABLE follow_request ( id UUID PRIMARY KEY, source_id UUID NOT NULL REFERENCES actor_profile (id) ON DELETE CASCADE, target_id UUID NOT NULL REFERENCES actor_profile (id) ON DELETE CASCADE, + activity_id VARCHAR(250) UNIQUE, request_status SMALLINT NOT NULL, UNIQUE (source_id, target_id) ); diff --git a/src/mastodon_api/accounts/views.rs b/src/mastodon_api/accounts/views.rs index c6b8a20..8efe4f2 100644 --- a/src/mastodon_api/accounts/views.rs +++ b/src/mastodon_api/accounts/views.rs @@ -609,7 +609,11 @@ async fn follow_account( let target = get_profile_by_id(db_client, &account_id).await?; if let Some(remote_actor) = target.actor_json { // Create follow request if target is remote - match create_follow_request(db_client, ¤t_user.id, &target.id).await { + match create_follow_request( + db_client, + ¤t_user.id, + &target.id, + ).await { Ok(follow_request) => { prepare_follow( &config.instance(), diff --git a/src/models/relationships/queries.rs b/src/models/relationships/queries.rs index 71afd84..2e68c91 100644 --- a/src/models/relationships/queries.rs +++ b/src/models/relationships/queries.rs @@ -140,26 +140,23 @@ pub async fn create_follow_request( source_id: &Uuid, target_id: &Uuid, ) -> Result { - let request = DbFollowRequest { - id: new_uuid(), - source_id: source_id.to_owned(), - target_id: target_id.to_owned(), - request_status: FollowRequestStatus::Pending, - }; - db_client.execute( + let request_id = new_uuid(); + let row = db_client.query_one( " INSERT INTO follow_request ( id, source_id, target_id, request_status ) VALUES ($1, $2, $3, $4) + RETURNING follow_request ", &[ - &request.id, - &request.source_id, - &request.target_id, - &request.request_status, + &request_id, + &source_id, + &target_id, + &FollowRequestStatus::Pending, ], ).await.map_err(catch_unique_violation("follow request"))?; + let request = row.try_get("follow_request")?; Ok(request) } @@ -517,7 +514,7 @@ mod tests { #[tokio::test] #[serial] - async fn test_remote_follow() { + async fn test_follow_remote_profile() { let db_client = &mut create_test_database().await; let source_data = UserCreateData { username: "test".to_string(), @@ -536,20 +533,15 @@ mod tests { .await.unwrap(); assert_eq!(follow_request.source_id, source.id); assert_eq!(follow_request.target_id, target.id); - assert!(matches!( - follow_request.request_status, - FollowRequestStatus::Pending, - )); + assert_eq!(follow_request.activity_id, None); + assert_eq!(follow_request.request_status, FollowRequestStatus::Pending); let following = get_following(db_client, &source.id).await.unwrap(); assert!(following.is_empty()); // Accept follow request follow_request_accepted(db_client, &follow_request.id).await.unwrap(); let follow_request = get_follow_request_by_id(db_client, &follow_request.id) .await.unwrap(); - assert!(matches!( - follow_request.request_status, - FollowRequestStatus::Accepted, - )); + assert_eq!(follow_request.request_status, FollowRequestStatus::Accepted); let following = get_following(db_client, &source.id).await.unwrap(); assert_eq!(following[0].id, target.id); // Unfollow diff --git a/src/models/relationships/types.rs b/src/models/relationships/types.rs index a275bd7..0a87fd6 100644 --- a/src/models/relationships/types.rs +++ b/src/models/relationships/types.rs @@ -87,7 +87,7 @@ impl TryFrom<&Row> for DbRelationship { } } -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum FollowRequestStatus { Pending, Accepted, @@ -127,6 +127,7 @@ pub struct DbFollowRequest { pub id: Uuid, pub source_id: Uuid, pub target_id: Uuid, + pub activity_id: Option, pub request_status: FollowRequestStatus, }