2022-02-21 22:32:36 +00:00
|
|
|
use tokio_postgres::GenericClient;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2022-04-16 18:48:00 +00:00
|
|
|
use crate::activitypub::actor::Actor;
|
2022-02-21 22:32:36 +00:00
|
|
|
use crate::errors::DatabaseError;
|
2022-04-16 18:48:00 +00:00
|
|
|
use crate::models::relationships::queries::{get_followers, get_relationships};
|
2022-02-21 22:32:36 +00:00
|
|
|
use crate::models::relationships::types::RelationshipType;
|
|
|
|
use super::types::RelationshipMap;
|
|
|
|
|
2022-04-16 18:48:00 +00:00
|
|
|
pub async fn get_profile_update_recipients(
|
|
|
|
db_client: &impl GenericClient,
|
|
|
|
current_user_id: &Uuid,
|
|
|
|
) -> Result<Vec<Actor>, DatabaseError> {
|
|
|
|
let followers = get_followers(db_client, current_user_id, None, None).await?;
|
|
|
|
let mut recipients: Vec<Actor> = Vec::new();
|
|
|
|
for profile in followers {
|
|
|
|
if let Some(remote_actor) = profile.actor_json {
|
|
|
|
recipients.push(remote_actor);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
Ok(recipients)
|
|
|
|
}
|
|
|
|
|
2022-02-21 22:32:36 +00:00
|
|
|
pub async fn get_relationship(
|
|
|
|
db_client: &impl GenericClient,
|
|
|
|
source_id: &Uuid,
|
|
|
|
target_id: &Uuid,
|
|
|
|
) -> Result<RelationshipMap, DatabaseError> {
|
|
|
|
// NOTE: this method returns relationship map even if target does not exist
|
|
|
|
let relationships = get_relationships(db_client, source_id, target_id).await?;
|
|
|
|
let mut relationship_map = RelationshipMap { id: *target_id, ..Default::default() };
|
|
|
|
for relationship in relationships {
|
|
|
|
match relationship.relationship_type {
|
|
|
|
RelationshipType::Follow => {
|
|
|
|
if relationship.is_direct(source_id, target_id)? {
|
|
|
|
relationship_map.following = true;
|
|
|
|
} else {
|
|
|
|
relationship_map.followed_by = true;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
RelationshipType::FollowRequest => {
|
|
|
|
if relationship.is_direct(source_id, target_id)? {
|
|
|
|
relationship_map.requested = true;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
RelationshipType::Subscription => {
|
|
|
|
if relationship.is_direct(source_id, target_id)? {
|
|
|
|
relationship_map.subscription_to = true;
|
|
|
|
} else {
|
|
|
|
relationship_map.subscription_from = true;
|
|
|
|
};
|
|
|
|
},
|
2022-02-21 15:20:18 +00:00
|
|
|
RelationshipType::HideReposts => {
|
|
|
|
if relationship.is_direct(source_id, target_id)? {
|
|
|
|
relationship_map.showing_reblogs = false;
|
|
|
|
};
|
|
|
|
},
|
2022-03-09 20:37:14 +00:00
|
|
|
RelationshipType::HideReplies => {
|
|
|
|
if relationship.is_direct(source_id, target_id)? {
|
|
|
|
relationship_map.showing_replies = false;
|
|
|
|
};
|
|
|
|
},
|
2022-02-21 22:32:36 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
Ok(relationship_map)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use serial_test::serial;
|
|
|
|
use crate::database::test_utils::create_test_database;
|
|
|
|
use crate::models::relationships::queries::{
|
|
|
|
create_follow_request,
|
|
|
|
follow,
|
|
|
|
follow_request_accepted,
|
2022-02-21 15:20:18 +00:00
|
|
|
hide_reposts,
|
|
|
|
show_reposts,
|
2022-02-21 22:32:36 +00:00
|
|
|
subscribe,
|
|
|
|
unfollow,
|
|
|
|
unsubscribe,
|
|
|
|
};
|
|
|
|
use crate::models::users::queries::create_user;
|
|
|
|
use crate::models::users::types::{User, UserCreateData};
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
async fn create_users(db_client: &mut impl GenericClient)
|
|
|
|
-> Result<(User, User), DatabaseError>
|
|
|
|
{
|
|
|
|
let user_data_1 = UserCreateData {
|
|
|
|
username: "user".to_string(),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let user_1 = create_user(db_client, user_data_1).await.unwrap();
|
|
|
|
let user_data_2 = UserCreateData {
|
|
|
|
username: "another-user".to_string(),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let user_2 = create_user(db_client, user_data_2).await.unwrap();
|
|
|
|
Ok((user_1, user_2))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_follow_unfollow() {
|
|
|
|
let db_client = &mut create_test_database().await;
|
|
|
|
let (user_1, user_2) = create_users(db_client).await.unwrap();
|
|
|
|
// Initial state
|
|
|
|
let relationship = get_relationship(db_client, &user_1.id, &user_2.id).await.unwrap();
|
|
|
|
assert_eq!(relationship.id, user_2.id);
|
|
|
|
assert_eq!(relationship.following, false);
|
|
|
|
assert_eq!(relationship.followed_by, false);
|
|
|
|
assert_eq!(relationship.requested, false);
|
|
|
|
assert_eq!(relationship.subscription_to, false);
|
|
|
|
assert_eq!(relationship.subscription_from, false);
|
2022-02-21 15:20:18 +00:00
|
|
|
assert_eq!(relationship.showing_reblogs, true);
|
2022-03-09 20:37:14 +00:00
|
|
|
assert_eq!(relationship.showing_replies, true);
|
2022-02-21 22:32:36 +00:00
|
|
|
// Follow request
|
|
|
|
let follow_request = create_follow_request(db_client, &user_1.id, &user_2.id).await.unwrap();
|
|
|
|
let relationship = get_relationship(db_client, &user_1.id, &user_2.id).await.unwrap();
|
|
|
|
assert_eq!(relationship.following, false);
|
|
|
|
assert_eq!(relationship.followed_by, false);
|
|
|
|
assert_eq!(relationship.requested, true);
|
|
|
|
// Mutual follow
|
|
|
|
follow_request_accepted(db_client, &follow_request.id).await.unwrap();
|
|
|
|
follow(db_client, &user_2.id, &user_1.id).await.unwrap();
|
|
|
|
let relationship = get_relationship(db_client, &user_1.id, &user_2.id).await.unwrap();
|
|
|
|
assert_eq!(relationship.following, true);
|
|
|
|
assert_eq!(relationship.followed_by, true);
|
|
|
|
assert_eq!(relationship.requested, false);
|
|
|
|
// Unfollow
|
|
|
|
unfollow(db_client, &user_1.id, &user_2.id).await.unwrap();
|
|
|
|
let relationship = get_relationship(db_client, &user_1.id, &user_2.id).await.unwrap();
|
|
|
|
assert_eq!(relationship.following, false);
|
|
|
|
assert_eq!(relationship.followed_by, true);
|
|
|
|
assert_eq!(relationship.requested, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_subscribe_unsubscribe() {
|
|
|
|
let db_client = &mut create_test_database().await;
|
|
|
|
let (user_1, user_2) = create_users(db_client).await.unwrap();
|
|
|
|
|
|
|
|
subscribe(db_client, &user_1.id, &user_2.id).await.unwrap();
|
|
|
|
let relationship = get_relationship(db_client, &user_1.id, &user_2.id).await.unwrap();
|
|
|
|
assert_eq!(relationship.subscription_to, true);
|
|
|
|
assert_eq!(relationship.subscription_from, false);
|
|
|
|
|
|
|
|
unsubscribe(db_client, &user_1.id, &user_2.id).await.unwrap();
|
|
|
|
let relationship = get_relationship(db_client, &user_1.id, &user_2.id).await.unwrap();
|
|
|
|
assert_eq!(relationship.subscription_to, false);
|
|
|
|
assert_eq!(relationship.subscription_from, false);
|
|
|
|
}
|
2022-02-21 15:20:18 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_hide_reblogs() {
|
|
|
|
let db_client = &mut create_test_database().await;
|
|
|
|
let (user_1, user_2) = create_users(db_client).await.unwrap();
|
|
|
|
follow(db_client, &user_1.id, &user_2.id).await.unwrap();
|
|
|
|
let relationship = get_relationship(db_client, &user_1.id, &user_2.id).await.unwrap();
|
|
|
|
assert_eq!(relationship.following, true);
|
|
|
|
assert_eq!(relationship.showing_reblogs, true);
|
|
|
|
|
|
|
|
hide_reposts(db_client, &user_1.id, &user_2.id).await.unwrap();
|
|
|
|
let relationship = get_relationship(db_client, &user_1.id, &user_2.id).await.unwrap();
|
|
|
|
assert_eq!(relationship.following, true);
|
|
|
|
assert_eq!(relationship.showing_reblogs, false);
|
|
|
|
|
|
|
|
show_reposts(db_client, &user_1.id, &user_2.id).await.unwrap();
|
|
|
|
let relationship = get_relationship(db_client, &user_1.id, &user_2.id).await.unwrap();
|
|
|
|
assert_eq!(relationship.following, true);
|
|
|
|
assert_eq!(relationship.showing_reblogs, true);
|
|
|
|
}
|
2022-02-21 22:32:36 +00:00
|
|
|
}
|