From 8d8ee83180ec004cfe104153003e470acbf2d559 Mon Sep 17 00:00:00 2001 From: soxx Date: Fri, 16 Feb 2024 21:36:50 +0100 Subject: [PATCH] feat: implemented getting entire anthrove user --- internal/user.go | 65 ++++++++++++++++++++++++++++++++++++++++ internal/utils/slices.go | 11 +++++++ pkg/graph.go | 2 +- pkg/impl.go | 5 ++-- 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 internal/utils/slices.go diff --git a/internal/user.go b/internal/user.go index d2882a7..d687f17 100644 --- a/internal/user.go +++ b/internal/user.go @@ -3,6 +3,7 @@ package internal import ( "context" "fmt" + "git.dragse.it/anthrove/anthrove-graph-sdk.git/internal/utils" "git.dragse.it/anthrove/anthrove-graph-sdk.git/pkg/models" "github.com/neo4j/neo4j-go-driver/v5/neo4j" @@ -144,3 +145,67 @@ func GetUserSourceLink(ctx context.Context, driver neo4j.DriverWithContext, anth return userSource, nil } + +func GetAnthroveUser(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) (*models.AnthroveUser, error) { + var err error + var anthroveUser models.AnthroveUser + var userSources models.AnthroveSource + userRelationships := make([]models.AnthroveUserRelationship, 0) + + query := ` + MATCH (user:User{user_id: $anthrove_user_id})-[relation:HAS_ACCOUNT_AT]->(source:Source) + RETURN user as User, relation as Relation, source as Source; + ` + params := map[string]any{ + "anthrove_user_id": anthroveUserID, + } + + 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") + } + + for i := range result.Records { + record := result.Records[i] + + user, _, err := neo4j.GetRecordValue[neo4j.Node](record, "User") + if err != nil { + return nil, err + } + relation, _, err := neo4j.GetRecordValue[neo4j.Relationship](record, "Relation") + if err != nil { + return nil, err + } + source, _, err := neo4j.GetRecordValue[neo4j.Node](record, "Source") + if err != nil { + return nil, err + } + + userRelationships = append(userRelationships, models.AnthroveUserRelationship{ + UserID: fmt.Sprintf("%v", utils.GetOrDefault(relation.Props, "user_id", "")), + Username: utils.GetOrDefault(relation.Props, "username", "").(string), + ScrapeTimeInterval: utils.GetOrDefault(relation.Props, "scrape_time_interval", "").(string), + }) + + userSources = models.AnthroveSource{ + DisplayName: utils.GetOrDefault(source.Props, "display_name", "").(string), + Domain: utils.GetOrDefault(source.Props, "domain", "").(string), + Icon: utils.GetOrDefault(source.Props, "icon", "").(string), + } + + anthroveUser.UserID = models.AnthroveUserID(utils.GetOrDefault(user.Props, "user_id", "").(string)) + anthroveUser.Relationship = userRelationships + + for j := range userRelationships { + anthroveUser.Relationship[j].Source = userSources + } + + } + + return &anthroveUser, nil + +} diff --git a/internal/utils/slices.go b/internal/utils/slices.go new file mode 100644 index 0000000..dfb5aed --- /dev/null +++ b/internal/utils/slices.go @@ -0,0 +1,11 @@ +package utils + +func GetOrDefault(data map[string]any, key string, defaultVal any) any { + val, ok := data[key] + + if !ok { + return defaultVal + } + + return val +} diff --git a/pkg/graph.go b/pkg/graph.go index 7e48b8c..c05a673 100644 --- a/pkg/graph.go +++ b/pkg/graph.go @@ -47,7 +47,7 @@ type Graph interface { GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.AnthroveUserRelationship, error) // GetAnthroveUser retrieves an Anthrove user from the graph by their ID - GetAnthroveUser(ctx context.Context, anthroveUser *models.AnthroveUser) (*models.AnthroveUser, error) + GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*models.AnthroveUser, error) // GetAllAnthroveUserIDs retrieves all Anthrove user IDs from the graph GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) diff --git a/pkg/impl.go b/pkg/impl.go index 7d78889..9b2ae41 100644 --- a/pkg/impl.go +++ b/pkg/impl.go @@ -84,9 +84,8 @@ func (g *graphConnection) GetUserSourceLinks(ctx context.Context, anthroveUserID return internal.GetUserSourceLink(ctx, g.driver, anthroveUserID) } -func (g *graphConnection) GetAnthroveUser(ctx context.Context, anthroveUser *models.AnthroveUser) (*models.AnthroveUser, error) { - //TODO implement me - panic("implement me") +func (g *graphConnection) GetAnthroveUser(ctx context.Context, anthroveUserID models.AnthroveUserID) (*models.AnthroveUser, error) { + return internal.GetAnthroveUser(ctx, g.driver, anthroveUserID) } func (g *graphConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) {