Add relationship type column to relationship table
This commit is contained in:
parent
7de7f7a501
commit
a3a0a6053d
5 changed files with 39 additions and 22 deletions
|
@ -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);
|
|
@ -57,10 +57,11 @@ CREATE TABLE post_reaction (
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE relationship (
|
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,
|
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,
|
||||||
PRIMARY KEY (source_id, target_id)
|
relationship_type SMALLINT NOT NULL,
|
||||||
|
UNIQUE (source_id, target_id, relationship_type)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE follow_request (
|
CREATE TABLE follow_request (
|
||||||
|
|
|
@ -20,6 +20,7 @@ use crate::models::notifications::queries::{
|
||||||
};
|
};
|
||||||
use crate::models::profiles::queries::update_post_count;
|
use crate::models::profiles::queries::update_post_count;
|
||||||
use crate::models::profiles::types::DbActorProfile;
|
use crate::models::profiles::types::DbActorProfile;
|
||||||
|
use crate::models::relationships::types::RelationshipType;
|
||||||
use crate::utils::id::new_uuid;
|
use crate::utils::id::new_uuid;
|
||||||
use super::types::{DbPost, Post, PostCreateData, Visibility};
|
use super::types::{DbPost, Post, PostCreateData, Visibility};
|
||||||
|
|
||||||
|
@ -216,12 +217,16 @@ fn build_visibility_filter() -> String {
|
||||||
)
|
)
|
||||||
OR post.visibility = {visibility_followers} AND EXISTS (
|
OR post.visibility = {visibility_followers} AND EXISTS (
|
||||||
SELECT 1 FROM relationship
|
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_public=i16::from(&Visibility::Public),
|
||||||
visibility_direct=i16::from(&Visibility::Direct),
|
visibility_direct=i16::from(&Visibility::Direct),
|
||||||
visibility_followers=i16::from(&Visibility::Followers),
|
visibility_followers=i16::from(&Visibility::Followers),
|
||||||
|
relationship_follow=i16::from(&RelationshipType::Follow),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ use crate::models::cleanup::{
|
||||||
find_orphaned_ipfs_objects,
|
find_orphaned_ipfs_objects,
|
||||||
DeletionQueue,
|
DeletionQueue,
|
||||||
};
|
};
|
||||||
|
use crate::models::relationships::types::RelationshipType;
|
||||||
use crate::utils::id::new_uuid;
|
use crate::utils::id::new_uuid;
|
||||||
use super::types::{
|
use super::types::{
|
||||||
get_currency_field_name,
|
get_currency_field_name,
|
||||||
|
@ -226,10 +227,11 @@ pub async fn delete_profile(
|
||||||
SET follower_count = follower_count - 1
|
SET follower_count = follower_count - 1
|
||||||
FROM relationship
|
FROM relationship
|
||||||
WHERE
|
WHERE
|
||||||
actor_profile.id = relationship.target_id
|
relationship.source_id = $1
|
||||||
AND relationship.source_id = $1
|
AND relationship.target_id = actor_profile.id
|
||||||
|
AND relationship.relationship_type = $2
|
||||||
",
|
",
|
||||||
&[&profile_id],
|
&[&profile_id, &RelationshipType::Follow],
|
||||||
).await?;
|
).await?;
|
||||||
transaction.execute(
|
transaction.execute(
|
||||||
"
|
"
|
||||||
|
@ -237,10 +239,11 @@ pub async fn delete_profile(
|
||||||
SET following_count = following_count - 1
|
SET following_count = following_count - 1
|
||||||
FROM relationship
|
FROM relationship
|
||||||
WHERE
|
WHERE
|
||||||
actor_profile.id = relationship.source_id
|
relationship.source_id = actor_profile.id
|
||||||
AND relationship.target_id = $1
|
AND relationship.target_id = $1
|
||||||
|
AND relationship.relationship_type = $2
|
||||||
",
|
",
|
||||||
&[&profile_id],
|
&[&profile_id, &RelationshipType::Follow],
|
||||||
).await?;
|
).await?;
|
||||||
transaction.execute(
|
transaction.execute(
|
||||||
"
|
"
|
||||||
|
|
|
@ -27,14 +27,14 @@ async fn get_relationships(
|
||||||
) -> Result<Vec<DbRelationship>, DatabaseError> {
|
) -> Result<Vec<DbRelationship>, DatabaseError> {
|
||||||
let rows = db_client.query(
|
let rows = db_client.query(
|
||||||
"
|
"
|
||||||
SELECT source_id, target_id, $4::smallint AS relationship_type
|
SELECT source_id, target_id, relationship_type
|
||||||
FROM relationship
|
FROM relationship
|
||||||
WHERE
|
WHERE
|
||||||
source_id = $1 AND target_id = $2
|
source_id = $1 AND target_id = $2
|
||||||
OR
|
OR
|
||||||
source_id = $2 AND target_id = $1
|
source_id = $2 AND target_id = $1
|
||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT source_id, target_id, $5 AS relationship_type
|
SELECT source_id, target_id, $4
|
||||||
FROM follow_request
|
FROM follow_request
|
||||||
WHERE
|
WHERE
|
||||||
source_id = $1 AND target_id = $2
|
source_id = $1 AND target_id = $2
|
||||||
|
@ -44,7 +44,6 @@ async fn get_relationships(
|
||||||
&source_id,
|
&source_id,
|
||||||
&target_id,
|
&target_id,
|
||||||
&FollowRequestStatus::Pending,
|
&FollowRequestStatus::Pending,
|
||||||
&RelationshipType::Follow,
|
|
||||||
&RelationshipType::FollowRequest,
|
&RelationshipType::FollowRequest,
|
||||||
],
|
],
|
||||||
).await?;
|
).await?;
|
||||||
|
@ -89,10 +88,10 @@ pub async fn follow(
|
||||||
let transaction = db_client.transaction().await?;
|
let transaction = db_client.transaction().await?;
|
||||||
transaction.execute(
|
transaction.execute(
|
||||||
"
|
"
|
||||||
INSERT INTO relationship (source_id, target_id)
|
INSERT INTO relationship (source_id, target_id, relationship_type)
|
||||||
VALUES ($1, $2)
|
VALUES ($1, $2, $3)
|
||||||
",
|
",
|
||||||
&[&source_id, &target_id],
|
&[&source_id, &target_id, &RelationshipType::Follow],
|
||||||
).await.map_err(catch_unique_violation("relationship"))?;
|
).await.map_err(catch_unique_violation("relationship"))?;
|
||||||
let target_profile = update_follower_count(&transaction, target_id, 1).await?;
|
let target_profile = update_follower_count(&transaction, target_id, 1).await?;
|
||||||
update_following_count(&transaction, source_id, 1).await?;
|
update_following_count(&transaction, source_id, 1).await?;
|
||||||
|
@ -112,9 +111,11 @@ pub async fn unfollow(
|
||||||
let deleted_count = transaction.execute(
|
let deleted_count = transaction.execute(
|
||||||
"
|
"
|
||||||
DELETE FROM relationship
|
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?;
|
).await?;
|
||||||
let relationship_deleted = deleted_count > 0;
|
let relationship_deleted = deleted_count > 0;
|
||||||
// Delete follow request (for remote follows)
|
// Delete follow request (for remote follows)
|
||||||
|
@ -268,11 +269,12 @@ pub async fn get_followers(
|
||||||
ON (actor_profile.id = relationship.source_id)
|
ON (actor_profile.id = relationship.source_id)
|
||||||
WHERE
|
WHERE
|
||||||
relationship.target_id = $1
|
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
|
ORDER BY relationship.id DESC
|
||||||
LIMIT $3
|
LIMIT $4
|
||||||
",
|
",
|
||||||
&[&profile_id, &max_relationship_id, &limit],
|
&[&profile_id, &RelationshipType::Follow, &max_relationship_id, &limit],
|
||||||
).await?;
|
).await?;
|
||||||
let profiles = rows.iter()
|
let profiles = rows.iter()
|
||||||
.map(|row| row.try_get("actor_profile"))
|
.map(|row| row.try_get("actor_profile"))
|
||||||
|
@ -294,11 +296,12 @@ pub async fn get_following(
|
||||||
ON (actor_profile.id = relationship.target_id)
|
ON (actor_profile.id = relationship.target_id)
|
||||||
WHERE
|
WHERE
|
||||||
relationship.source_id = $1
|
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
|
ORDER BY relationship.id DESC
|
||||||
LIMIT $3
|
LIMIT $4
|
||||||
",
|
",
|
||||||
&[&profile_id, &max_relationship_id, &limit],
|
&[&profile_id, &RelationshipType::Follow, &max_relationship_id, &limit],
|
||||||
).await?;
|
).await?;
|
||||||
let profiles = rows.iter()
|
let profiles = rows.iter()
|
||||||
.map(|row| row.try_get("actor_profile"))
|
.map(|row| row.try_get("actor_profile"))
|
||||||
|
|
Loading…
Reference in a new issue