feat: added connection & creating user

This commit is contained in:
SoXX 2024-02-16 15:54:36 +01:00
parent d13d244edf
commit c4b2eee03b
10 changed files with 154 additions and 11 deletions

7
go.mod
View File

@ -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

15
go.sum
View File

@ -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=

46
internal/logger.go Normal file
View File

@ -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...))
}
}

1
internal/post.go Normal file
View File

@ -0,0 +1 @@
package internal

View File

@ -0,0 +1 @@
package internal

1
internal/source.go Normal file
View File

@ -0,0 +1 @@
package internal

1
internal/tag.go Normal file
View File

@ -0,0 +1 @@
package internal

55
internal/user.go Normal file
View File

@ -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
}

View File

@ -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

View File

@ -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)
}
}