Add activity_id column to follow_request table

This commit is contained in:
silverpill 2022-12-12 23:50:40 +00:00
parent 8c14b18d5b
commit b0f9b3537e
5 changed files with 21 additions and 22 deletions

View file

@ -0,0 +1 @@
ALTER TABLE follow_request ADD COLUMN activity_id VARCHAR(250) UNIQUE;

View file

@ -69,6 +69,7 @@ CREATE TABLE follow_request (
id UUID PRIMARY KEY, id UUID PRIMARY KEY,
source_id UUID NOT NULL REFERENCES actor_profile (id) ON DELETE CASCADE, source_id UUID NOT NULL REFERENCES actor_profile (id) ON DELETE CASCADE,
target_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, request_status SMALLINT NOT NULL,
UNIQUE (source_id, target_id) UNIQUE (source_id, target_id)
); );

View file

@ -609,7 +609,11 @@ async fn follow_account(
let target = get_profile_by_id(db_client, &account_id).await?; let target = get_profile_by_id(db_client, &account_id).await?;
if let Some(remote_actor) = target.actor_json { if let Some(remote_actor) = target.actor_json {
// Create follow request if target is remote // Create follow request if target is remote
match create_follow_request(db_client, &current_user.id, &target.id).await { match create_follow_request(
db_client,
&current_user.id,
&target.id,
).await {
Ok(follow_request) => { Ok(follow_request) => {
prepare_follow( prepare_follow(
&config.instance(), &config.instance(),

View file

@ -140,26 +140,23 @@ pub async fn create_follow_request(
source_id: &Uuid, source_id: &Uuid,
target_id: &Uuid, target_id: &Uuid,
) -> Result<DbFollowRequest, DatabaseError> { ) -> Result<DbFollowRequest, DatabaseError> {
let request = DbFollowRequest { let request_id = new_uuid();
id: new_uuid(), let row = db_client.query_one(
source_id: source_id.to_owned(),
target_id: target_id.to_owned(),
request_status: FollowRequestStatus::Pending,
};
db_client.execute(
" "
INSERT INTO follow_request ( INSERT INTO follow_request (
id, source_id, target_id, request_status id, source_id, target_id, request_status
) )
VALUES ($1, $2, $3, $4) VALUES ($1, $2, $3, $4)
RETURNING follow_request
", ",
&[ &[
&request.id, &request_id,
&request.source_id, &source_id,
&request.target_id, &target_id,
&request.request_status, &FollowRequestStatus::Pending,
], ],
).await.map_err(catch_unique_violation("follow request"))?; ).await.map_err(catch_unique_violation("follow request"))?;
let request = row.try_get("follow_request")?;
Ok(request) Ok(request)
} }
@ -517,7 +514,7 @@ mod tests {
#[tokio::test] #[tokio::test]
#[serial] #[serial]
async fn test_remote_follow() { async fn test_follow_remote_profile() {
let db_client = &mut create_test_database().await; let db_client = &mut create_test_database().await;
let source_data = UserCreateData { let source_data = UserCreateData {
username: "test".to_string(), username: "test".to_string(),
@ -536,20 +533,15 @@ mod tests {
.await.unwrap(); .await.unwrap();
assert_eq!(follow_request.source_id, source.id); assert_eq!(follow_request.source_id, source.id);
assert_eq!(follow_request.target_id, target.id); assert_eq!(follow_request.target_id, target.id);
assert!(matches!( assert_eq!(follow_request.activity_id, None);
follow_request.request_status, assert_eq!(follow_request.request_status, FollowRequestStatus::Pending);
FollowRequestStatus::Pending,
));
let following = get_following(db_client, &source.id).await.unwrap(); let following = get_following(db_client, &source.id).await.unwrap();
assert!(following.is_empty()); assert!(following.is_empty());
// Accept follow request // Accept follow request
follow_request_accepted(db_client, &follow_request.id).await.unwrap(); follow_request_accepted(db_client, &follow_request.id).await.unwrap();
let follow_request = get_follow_request_by_id(db_client, &follow_request.id) let follow_request = get_follow_request_by_id(db_client, &follow_request.id)
.await.unwrap(); .await.unwrap();
assert!(matches!( assert_eq!(follow_request.request_status, FollowRequestStatus::Accepted);
follow_request.request_status,
FollowRequestStatus::Accepted,
));
let following = get_following(db_client, &source.id).await.unwrap(); let following = get_following(db_client, &source.id).await.unwrap();
assert_eq!(following[0].id, target.id); assert_eq!(following[0].id, target.id);
// Unfollow // Unfollow

View file

@ -87,7 +87,7 @@ impl TryFrom<&Row> for DbRelationship {
} }
} }
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub enum FollowRequestStatus { pub enum FollowRequestStatus {
Pending, Pending,
Accepted, Accepted,
@ -127,6 +127,7 @@ pub struct DbFollowRequest {
pub id: Uuid, pub id: Uuid,
pub source_id: Uuid, pub source_id: Uuid,
pub target_id: Uuid, pub target_id: Uuid,
pub activity_id: Option<String>,
pub request_status: FollowRequestStatus, pub request_status: FollowRequestStatus,
} }