Add relationship type column to relationship table

This commit is contained in:
silverpill 2022-02-03 00:15:45 +00:00
parent 7de7f7a501
commit a3a0a6053d
5 changed files with 39 additions and 22 deletions

View file

@ -0,0 +1,5 @@
ALTER TABLE relationship ADD COLUMN relationship_type SMALLINT NOT NULL DEFAULT 1;
ALTER TABLE relationship ALTER COLUMN relationship_type DROP DEFAULT;
ALTER TABLE relationship DROP CONSTRAINT relationship_pkey;
ALTER TABLE relationship ADD PRIMARY KEY (id);
ALTER TABLE relationship ADD CONSTRAINT relationship_source_id_target_id_relationship_type_key UNIQUE (source_id, target_id, relationship_type);

View file

@ -57,10 +57,11 @@ CREATE TABLE post_reaction (
);
CREATE TABLE relationship (
id INTEGER GENERATED BY DEFAULT AS IDENTITY,
id INTEGER GENERATED BY DEFAULT AS IDENTITY 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,
PRIMARY KEY (source_id, target_id)
relationship_type SMALLINT NOT NULL,
UNIQUE (source_id, target_id, relationship_type)
);
CREATE TABLE follow_request (

View file

@ -20,6 +20,7 @@ use crate::models::notifications::queries::{
};
use crate::models::profiles::queries::update_post_count;
use crate::models::profiles::types::DbActorProfile;
use crate::models::relationships::types::RelationshipType;
use crate::utils::id::new_uuid;
use super::types::{DbPost, Post, PostCreateData, Visibility};
@ -216,12 +217,16 @@ fn build_visibility_filter() -> String {
)
OR post.visibility = {visibility_followers} AND EXISTS (
SELECT 1 FROM relationship
WHERE source_id = $current_user_id AND target_id = post.author_id
WHERE
source_id = $current_user_id
AND target_id = post.author_id
AND relationship_type = {relationship_follow}
)
)",
visibility_public=i16::from(&Visibility::Public),
visibility_direct=i16::from(&Visibility::Direct),
visibility_followers=i16::from(&Visibility::Followers),
relationship_follow=i16::from(&RelationshipType::Follow),
)
}

View file

@ -8,6 +8,7 @@ use crate::models::cleanup::{
find_orphaned_ipfs_objects,
DeletionQueue,
};
use crate::models::relationships::types::RelationshipType;
use crate::utils::id::new_uuid;
use super::types::{
get_currency_field_name,
@ -226,10 +227,11 @@ pub async fn delete_profile(
SET follower_count = follower_count - 1
FROM relationship
WHERE
actor_profile.id = relationship.target_id
AND relationship.source_id = $1
relationship.source_id = $1
AND relationship.target_id = actor_profile.id
AND relationship.relationship_type = $2
",
&[&profile_id],
&[&profile_id, &RelationshipType::Follow],
).await?;
transaction.execute(
"
@ -237,10 +239,11 @@ pub async fn delete_profile(
SET following_count = following_count - 1
FROM relationship
WHERE
actor_profile.id = relationship.source_id
relationship.source_id = actor_profile.id
AND relationship.target_id = $1
AND relationship.relationship_type = $2
",
&[&profile_id],
&[&profile_id, &RelationshipType::Follow],
).await?;
transaction.execute(
"

View file

@ -27,14 +27,14 @@ async fn get_relationships(
) -> Result<Vec<DbRelationship>, DatabaseError> {
let rows = db_client.query(
"
SELECT source_id, target_id, $4::smallint AS relationship_type
SELECT source_id, target_id, relationship_type
FROM relationship
WHERE
source_id = $1 AND target_id = $2
OR
source_id = $2 AND target_id = $1
UNION ALL
SELECT source_id, target_id, $5 AS relationship_type
SELECT source_id, target_id, $4
FROM follow_request
WHERE
source_id = $1 AND target_id = $2
@ -44,7 +44,6 @@ async fn get_relationships(
&source_id,
&target_id,
&FollowRequestStatus::Pending,
&RelationshipType::Follow,
&RelationshipType::FollowRequest,
],
).await?;
@ -89,10 +88,10 @@ pub async fn follow(
let transaction = db_client.transaction().await?;
transaction.execute(
"
INSERT INTO relationship (source_id, target_id)
VALUES ($1, $2)
INSERT INTO relationship (source_id, target_id, relationship_type)
VALUES ($1, $2, $3)
",
&[&source_id, &target_id],
&[&source_id, &target_id, &RelationshipType::Follow],
).await.map_err(catch_unique_violation("relationship"))?;
let target_profile = update_follower_count(&transaction, target_id, 1).await?;
update_following_count(&transaction, source_id, 1).await?;
@ -112,9 +111,11 @@ pub async fn unfollow(
let deleted_count = transaction.execute(
"
DELETE FROM relationship
WHERE source_id = $1 AND target_id = $2
WHERE
source_id = $1 AND target_id = $2
AND relationship_type = $3
",
&[&source_id, &target_id],
&[&source_id, &target_id, &RelationshipType::Follow],
).await?;
let relationship_deleted = deleted_count > 0;
// Delete follow request (for remote follows)
@ -268,11 +269,12 @@ pub async fn get_followers(
ON (actor_profile.id = relationship.source_id)
WHERE
relationship.target_id = $1
AND ($2::integer IS NULL OR relationship.id < $2)
AND relationship.relationship_type = $2
AND ($3::integer IS NULL OR relationship.id < $3)
ORDER BY relationship.id DESC
LIMIT $3
LIMIT $4
",
&[&profile_id, &max_relationship_id, &limit],
&[&profile_id, &RelationshipType::Follow, &max_relationship_id, &limit],
).await?;
let profiles = rows.iter()
.map(|row| row.try_get("actor_profile"))
@ -294,11 +296,12 @@ pub async fn get_following(
ON (actor_profile.id = relationship.target_id)
WHERE
relationship.source_id = $1
AND ($2::integer IS NULL OR relationship.id < $2)
AND relationship.relationship_type = $2
AND ($3::integer IS NULL OR relationship.id < $3)
ORDER BY relationship.id DESC
LIMIT $3
LIMIT $4
",
&[&profile_id, &max_relationship_id, &limit],
&[&profile_id, &RelationshipType::Follow, &max_relationship_id, &limit],
).await?;
let profiles = rows.iter()
.map(|row| row.try_get("actor_profile"))