diff --git a/internal/relationships.go b/internal/relationships.go index 14f1b8d..7dd4d00 100644 --- a/internal/relationships.go +++ b/internal/relationships.go @@ -2,6 +2,7 @@ package internal import ( "context" + "fmt" "git.dragse.it/anthrove/anthrove-graph-sdk.git/pkg/models" "github.com/neo4j/neo4j-go-driver/v5/neo4j" @@ -62,3 +63,38 @@ func EstablishUserToPostLink(ctx context.Context, driver neo4j.DriverWithContext return nil } + +func CheckUserToPostLink(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID, sourcePostID string, sourcePostUrl string) (bool, error) { + query := ` + OPTIONAL MATCH (:User {user_id: $anthrove_user_id})-[f:FAV]->(:AnthrovePost)<-[:REFERENCE{source_post_id: $source_post_id}]-(:Source{domain: $source_domain}) + RETURN COUNT(f) > 0 AS hasRelationship + ` + + params := map[string]any{ + "anthrove_user_id": anthroveUserID, + "source_post_id": sourcePostID, + "source_domain": sourcePostUrl, + } + + result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) + if err != nil { + return false, err + } + + if len(result.Records) == 0 { + return false, fmt.Errorf("no records found") + } + + exists, _, err := neo4j.GetRecordValue[bool](result.Records[0], "hasRelationship") + if err != nil { + return false, err + } + + log.WithFields(log.Fields{ + "relationship_exists": exists, + "relationship_anthrove_user_id": anthroveUserID, + "relationship_e621_post_id": "", + }).Trace("graph: checked user post relationship") + + return exists, nil +} diff --git a/pkg/graph.go b/pkg/graph.go index 9a5dcb9..fe3c66d 100644 --- a/pkg/graph.go +++ b/pkg/graph.go @@ -28,8 +28,8 @@ type Graph interface { // LinkUserWithPost establishes a link between a user and a post in the graph LinkUserWithPost(ctx context.Context, anthroveUser *models.AnthroveUser, anthrovePost *models.AnthrovePost) error - // VerifyUserPostLink checks if a link between a user and a post exists in the graph - VerifyUserPostLink(ctx context.Context, anthrovePost *models.AnthrovePost, anthroveUser *models.AnthroveUser) (bool, error) + // CheckUserPostLink checks if a link between a user and a post exists in the graph + CheckUserPostLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourcePostUrl string) (bool, error) // CheckPostNodeExistsByAnthroveID checks if an Anthrove post node exists in the graph by its Anthrove ID CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.AnthrovePost) (bool, error) diff --git a/pkg/impl.go b/pkg/impl.go index 66c9827..07127c4 100644 --- a/pkg/impl.go +++ b/pkg/impl.go @@ -60,9 +60,8 @@ func (g *graphConnection) LinkUserWithPost(ctx context.Context, anthroveUser *mo return internal.EstablishUserToPostLink(ctx, g.driver, anthroveUser, anthrovePost) } -func (g *graphConnection) VerifyUserPostLink(ctx context.Context, anthrovePost *models.AnthrovePost, anthroveUser *models.AnthroveUser) (bool, error) { - //TODO implement me - panic("implement me") +func (g *graphConnection) CheckUserPostLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourcePostID string, sourcePostUrl string) (bool, error) { + return internal.CheckUserToPostLink(ctx, g.driver, anthroveUserID, sourcePostID, sourcePostUrl) } func (g *graphConnection) CheckPostNodeExistsByAnthroveID(ctx context.Context, anthrovePost *models.AnthrovePost) (bool, error) {