From f54d7244c5eee482560fa9dc1acaac3514349127 Mon Sep 17 00:00:00 2001 From: SoXX Date: Sun, 5 May 2024 00:17:18 +0200 Subject: [PATCH] feat: added GetSpecifiedUserSourceLink fucntion --- internal/user.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++ pkg/graph/graph.go | 4 +++ pkg/graph/impl.go | 4 +++ 3 files changed, 69 insertions(+) diff --git a/internal/user.go b/internal/user.go index 2a56afc..89c9aa2 100644 --- a/internal/user.go +++ b/internal/user.go @@ -149,6 +149,67 @@ func GetUserSourceLink(ctx context.Context, driver neo4j.DriverWithContext, anth return userSource, nil } +func GetSpecifiedUserSourceLink(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.AnthroveUserRelationship, error) { + + userSource := make(map[string]models.AnthroveUserRelationship) + + query := ` + MATCH (user:User{user_id: $anthrove_user_id})-[r:HAS_ACCOUNT_AT]->(s:Source{display_name: $source_display_name}) + RETURN toString(r.user_id) AS sourceUserID, toString(r.username) AS sourceUsername, s as source; + ` + params := map[string]any{ + "anthrove_user_id": anthroveUserID, + "source_display_name": sourceDisplayName, + } + + result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) + if err != nil { + return nil, err + } + + if len(result.Records) == 0 { + return nil, fmt.Errorf("user has no relations with the source %s", sourceDisplayName) + } + + for i := range result.Records { + record := result.Records[i] + source, _, err := neo4j.GetRecordValue[neo4j.Node](record, "source") + if err != nil { + return nil, err + } + sourceUserID, _, err := neo4j.GetRecordValue[string](record, "sourceUserID") + if err != nil { + return nil, err + } + sourceUsername, _, err := neo4j.GetRecordValue[string](record, "sourceUsername") + if err != nil { + return nil, err + } + + displayName := source.Props["display_name"].(string) + domain := source.Props["domain"].(string) + icon := source.Props["icon"].(string) + + anthroveSourceUser := models.AnthroveUserRelationship{ + UserID: sourceUserID, + Username: sourceUsername, + Source: models.AnthroveSource{ + DisplayName: displayName, + Domain: domain, + Icon: icon, + }, + } + userSource[displayName] = anthroveSourceUser + } + + log.WithFields(log.Fields{ + "anthrove_user_id": anthroveUserID, + "anthrove_data": userSource, + }).Trace("graph: got user favorite count") + + return userSource, nil +} + func GetAnthroveUser(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) (*models.AnthroveUser, error) { var err error var anthroveUser models.AnthroveUser diff --git a/pkg/graph/graph.go b/pkg/graph/graph.go index 92d9bb2..140b8cd 100644 --- a/pkg/graph/graph.go +++ b/pkg/graph/graph.go @@ -86,6 +86,10 @@ type OtterSpace interface { // It returns a map of source domains to user-source relationships, and an error if the operation fails. GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.AnthroveUserRelationship, error) + // GetUserSourceLinks retrieves the links between a user and sources in the OtterSpace graph. + // It returns a map of source domains to user-source relationships, and an error if the operation fails. + GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.AnthroveUserRelationship, error) + // GetAnthroveUser retrieves a user from the OtterSpace graph by their ID. // It returns the user and an error if the operation fails. GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*models.AnthroveUser, error) diff --git a/pkg/graph/impl.go b/pkg/graph/impl.go index cc3eafc..ca5fd15 100644 --- a/pkg/graph/impl.go +++ b/pkg/graph/impl.go @@ -84,6 +84,10 @@ func (g *graphConnection) GetUserSourceLinks(ctx context.Context, anthroveUserID return internal.GetUserSourceLink(ctx, g.driver, anthroveUserID) } +func (g *graphConnection) GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.AnthroveUserRelationship, error) { + return internal.GetSpecifiedUserSourceLink(ctx, g.driver, anthroveUserID, sourceDisplayName) +} + func (g *graphConnection) GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*models.AnthroveUser, error) { return internal.GetAnthroveUser(ctx, g.driver, anthroveUserID) }