2023-06-20 08:38:36 +00:00
|
|
|
package neo4j
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-17 10:57:23 +00:00
|
|
|
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/model"
|
|
|
|
"git.dragse.it/anthrove/e621-to-graph/pkg/logic"
|
2023-06-20 08:38:36 +00:00
|
|
|
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
|
|
|
|
"github.com/neo4j/neo4j-go-driver/v5/neo4j/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type neo4jConnection struct {
|
|
|
|
driver neo4j.DriverWithContext
|
|
|
|
}
|
|
|
|
|
2023-07-17 10:57:23 +00:00
|
|
|
func NewNeo4JConnection() logic.GraphConnection {
|
2023-07-17 08:10:13 +00:00
|
|
|
return &neo4jConnection{}
|
|
|
|
}
|
|
|
|
|
2023-06-20 08:38:36 +00:00
|
|
|
func (c *neo4jConnection) CheckUserToPostLink(ctx context.Context, e621PostID int64, e621UserID int64) (bool, error) {
|
|
|
|
return RelationshipCheckUserToPost(ctx, c.driver, e621PostID, e621UserID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *neo4jConnection) EstablishPostToTagLink(ctx context.Context, e621PostID int64, e621Tag string) error {
|
|
|
|
return EstablishPostTagLink(ctx, c.driver, e621PostID, e621Tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *neo4jConnection) EstablishPostToSourceLink(ctx context.Context, e621PostID int64, sourceURL string) error {
|
|
|
|
return EstablishPostToSourceLink(ctx, c.driver, e621PostID, sourceURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *neo4jConnection) EstablishUserToPostLink(ctx context.Context, e621PostID int64, e621UserID int64) error {
|
|
|
|
return EstablishUserToPostLink(ctx, c.driver, e621PostID, e621UserID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *neo4jConnection) UploadTag(ctx context.Context, name string, tagType string) error {
|
|
|
|
return CreateTagNode(ctx, c.driver, name, tagType)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *neo4jConnection) UploadPost(ctx context.Context, e621ID int64) error {
|
|
|
|
return CreatePostNode(ctx, c.driver, e621ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *neo4jConnection) UploadSource(ctx context.Context, SourceURL string) error {
|
|
|
|
return CreateSourceNode(ctx, c.driver, SourceURL)
|
|
|
|
}
|
|
|
|
|
2023-07-17 10:57:23 +00:00
|
|
|
func (c *neo4jConnection) UploadUser(ctx context.Context, user model.E621User) error {
|
2023-06-20 08:38:36 +00:00
|
|
|
return CreateUserNode(ctx, c.driver, user)
|
|
|
|
}
|
|
|
|
func (c *neo4jConnection) Connect(ctx context.Context, endpoint string, username string, password string) error {
|
|
|
|
driver, err := neo4j.NewDriverWithContext(endpoint, neo4j.BasicAuth(username, password, ""),
|
|
|
|
useConsoleLogger(neo4j.INFO))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = driver.VerifyAuthentication(context.Background(), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.driver = driver
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func useConsoleLogger(level neo4j.LogLevel) func(config *config.Config) {
|
|
|
|
return func(config *config.Config) {
|
|
|
|
config.Log = neo4j.ConsoleLogger(level)
|
|
|
|
}
|
|
|
|
}
|