From c4b2eee03b3285f90b3227bc37b8b72c9caeed9f Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 16 Feb 2024 15:54:36 +0100 Subject: [PATCH] feat: added connection & creating user --- go.mod | 7 ++++- go.sum | 15 +++++++++++ internal/logger.go | 46 ++++++++++++++++++++++++++++++++ internal/post.go | 1 + internal/relationships.go | 1 + internal/source.go | 1 + internal/tag.go | 1 + internal/user.go | 55 +++++++++++++++++++++++++++++++++++++++ pkg/graph.go | 5 ++-- pkg/impl.go | 33 +++++++++++++++++------ 10 files changed, 154 insertions(+), 11 deletions(-) create mode 100644 internal/logger.go create mode 100644 internal/post.go create mode 100644 internal/relationships.go create mode 100644 internal/source.go create mode 100644 internal/tag.go create mode 100644 internal/user.go diff --git a/go.mod b/go.mod index 2cbf8ca..ef4ba65 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,9 @@ module git.dragse.it/anthrove/anthrove-graph-sdk.git go 1.22.0 -require github.com/neo4j/neo4j-go-driver/v5 v5.17.0 +require ( + github.com/neo4j/neo4j-go-driver/v5 v5.17.0 + github.com/sirupsen/logrus v1.9.3 +) + +require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect diff --git a/go.sum b/go.sum index 1a22c9b..b7d2bdf 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,17 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/neo4j/neo4j-go-driver/v5 v5.17.0 h1:Bdqg1Y8Hd3uLYToXtBjysDYXTdMiP7zeUNUEwfbJkSo= github.com/neo4j/neo4j-go-driver/v5 v5.17.0/go.mod h1:Vff8OwT7QpLm7L2yYr85XNWe9Rbqlbeb9asNXJTHO4k= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/logger.go b/internal/logger.go new file mode 100644 index 0000000..446869d --- /dev/null +++ b/internal/logger.go @@ -0,0 +1,46 @@ +package internal + +import ( + "fmt" + + neo4jLog "github.com/neo4j/neo4j-go-driver/v5/neo4j/log" + log "github.com/sirupsen/logrus" +) + +type graphLogger struct { + graphDebug bool +} + +func NewGraphLogger(graphDebug bool) neo4jLog.Logger { + return &graphLogger{graphDebug: graphDebug} +} + +func (n graphLogger) Error(name string, id string, err error) { + log.WithFields(log.Fields{ + "name": name, + "id": id, + }).Errorf("graph: %s", err) +} + +func (n graphLogger) Warnf(name string, id string, msg string, args ...any) { + log.WithFields(log.Fields{ + "name": name, + "id": id, + }).Warnf("graph: %v", fmt.Sprintf(msg, args...)) +} + +func (n graphLogger) Infof(name string, id string, msg string, args ...any) { + log.WithFields(log.Fields{ + "name": name, + "id": id, + }).Infof("graph: %v", fmt.Sprintf(msg, args...)) +} + +func (n graphLogger) Debugf(name string, id string, msg string, args ...any) { + if n.graphDebug { + log.WithFields(log.Fields{ + "name": name, + "id": id, + }).Debugf("graph: %v", fmt.Sprintf(msg, args...)) + } +} diff --git a/internal/post.go b/internal/post.go new file mode 100644 index 0000000..5bf0569 --- /dev/null +++ b/internal/post.go @@ -0,0 +1 @@ +package internal diff --git a/internal/relationships.go b/internal/relationships.go new file mode 100644 index 0000000..5bf0569 --- /dev/null +++ b/internal/relationships.go @@ -0,0 +1 @@ +package internal diff --git a/internal/source.go b/internal/source.go new file mode 100644 index 0000000..5bf0569 --- /dev/null +++ b/internal/source.go @@ -0,0 +1 @@ +package internal diff --git a/internal/tag.go b/internal/tag.go new file mode 100644 index 0000000..5bf0569 --- /dev/null +++ b/internal/tag.go @@ -0,0 +1 @@ +package internal diff --git a/internal/user.go b/internal/user.go new file mode 100644 index 0000000..08dff37 --- /dev/null +++ b/internal/user.go @@ -0,0 +1,55 @@ +package internal + +import ( + "context" + + "git.dragse.it/anthrove/anthrove-graph-sdk.git/pkg/models" + "github.com/neo4j/neo4j-go-driver/v5/neo4j" + log "github.com/sirupsen/logrus" +) + +func CreateUserNodeWithSourceRelation(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) (*models.AnthroveUser, error) { + query := ` + MATCH (userNode:User {user_id: $anthrove_user_id}) + MATCH (sourceNode:Source {domain: $source_domain}) + MERGE (userNode)-[r:HAS_ACCOUNT_AT {username: $source_user_name, user_id: $source_user_id}]->(sourceNode) + ` + params := map[string]any{ + "anthrove_user_id": anthroveUserID, + "source_user_id": userID, + "source_user_name": username, + "source_domain": sourceDomain, + } + + _, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) + if err != nil { + return nil, err + } + + var anthroveUserRelationship []models.AnthroveUserRelationship + + anthroveUserRelationship = append(anthroveUserRelationship, models.AnthroveUserRelationship{ + UserID: userID, + Username: username, + ScrapeTimeInterval: "", + Source: models.AnthroveSource{ + DisplayName: "", + Domain: sourceDomain, + Icon: "", + }, + }) + + anthroveUser := models.AnthroveUser{ + UserID: anthroveUserID, + Relationship: anthroveUserRelationship, + } + + log.WithFields(log.Fields{ + "anthrove_user_id": anthroveUserID, + "source_user_id": userID, + "source_user_name": username, + "source_domain": sourceDomain, + }).Trace("graph: crated user with relationship") + + return &anthroveUser, nil +} diff --git a/pkg/graph.go b/pkg/graph.go index 5d6401a..56461ea 100644 --- a/pkg/graph.go +++ b/pkg/graph.go @@ -1,9 +1,10 @@ -package logic +package pkg import ( "context" "git.dragse.it/anthrove/anthrove-graph-sdk.git/pkg/models" + "github.com/neo4j/neo4j-go-driver/v5/neo4j" ) type Graph interface { @@ -11,7 +12,7 @@ type Graph interface { Connect(ctx context.Context, endpoint string, username string, password string) error // AddUserToGraph uploads an Anthrove user to the graph - AddUserToGraph(ctx context.Context, anthroveUser *models.AnthroveUser) error + AddUserToGraph(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) (*models.AnthroveUser, error) // AddSourceToGraph uploads an Anthrove source to the graph AddSourceToGraph(ctx context.Context, anthroveSource *models.AnthroveSource) error diff --git a/pkg/impl.go b/pkg/impl.go index 077b59f..891d099 100644 --- a/pkg/impl.go +++ b/pkg/impl.go @@ -1,32 +1,43 @@ -package logic +package pkg import ( "context" + "git.dragse.it/anthrove/anthrove-graph-sdk.git/internal" "git.dragse.it/anthrove/anthrove-graph-sdk.git/pkg/models" "github.com/neo4j/neo4j-go-driver/v5/neo4j" + "github.com/neo4j/neo4j-go-driver/v5/neo4j/config" ) type graphConnection struct { driver neo4j.DriverWithContext - neo4jDebug bool + graphDebug bool } func NewGraphConnection(graphDebug bool) Graph { return &graphConnection{ driver: nil, - neo4jDebug: graphDebug, + graphDebug: graphDebug, } } func (g *graphConnection) Connect(ctx context.Context, endpoint string, username string, password string) error { - //TODO implement me - panic("implement me") + driver, err := neo4j.NewDriverWithContext(endpoint, neo4j.BasicAuth(username, password, ""), + logger(g.graphDebug)) + + if err != nil { + return err + } + err = driver.VerifyAuthentication(ctx, nil) + if err != nil { + return err + } + g.driver = driver + return nil } -func (g *graphConnection) AddUserToGraph(ctx context.Context, anthroveUser *models.AnthroveUser) error { - //TODO implement me - panic("implement me") +func (g *graphConnection) AddUserToGraph(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) (*models.AnthroveUser, error) { + return internal.CreateUserNodeWithSourceRelation(ctx, driver, anthroveUserID, sourceDomain, userID, username) } func (g *graphConnection) AddSourceToGraph(ctx context.Context, anthroveSource *models.AnthroveSource) error { @@ -93,3 +104,9 @@ func (g *graphConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]models.A //TODO implement me panic("implement me") } + +func logger(graphDebug bool) func(config *config.Config) { + return func(config *config.Config) { + config.Log = internal.NewGraphLogger(graphDebug) + } +}