2023-06-20 08:38:36 +00:00
|
|
|
package neo4j
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-11-08 13:01:27 +00:00
|
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
|
2023-07-17 10:57:23 +00:00
|
|
|
"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 {
|
2023-07-26 13:27:18 +00:00
|
|
|
driver neo4j.DriverWithContext
|
|
|
|
neo4jDebug bool
|
2023-06-20 08:38:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 13:27:18 +00:00
|
|
|
func NewNeo4JConnection(neo4jDebug bool) logic.GraphConnection {
|
|
|
|
return &neo4jConnection{
|
|
|
|
driver: nil,
|
|
|
|
neo4jDebug: neo4jDebug,
|
|
|
|
}
|
2023-07-17 08:10:13 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 20:28:44 +00:00
|
|
|
func (c *neo4jConnection) GetUserFavoriteCount(ctx context.Context, userID model.UserID) (int64, error) {
|
|
|
|
return GetUserFavoritesCount(ctx, c.driver, userID)
|
|
|
|
}
|
|
|
|
|
2023-11-08 13:01:27 +00:00
|
|
|
func (c *neo4jConnection) CheckUserToPostLink(ctx context.Context, e621PostID model.PostID, e621UserID model.UserID) (bool, error) {
|
|
|
|
return CheckUserToPostLink(ctx, c.driver, e621PostID, e621UserID)
|
2023-06-20 08:38:36 +00:00
|
|
|
}
|
|
|
|
|
2023-11-08 13:01:27 +00:00
|
|
|
func (c *neo4jConnection) EstablishPostToTagLink(ctx context.Context, e621PostID model.PostID, e621Tag string) error {
|
2023-06-20 08:38:36 +00:00
|
|
|
return EstablishPostTagLink(ctx, c.driver, e621PostID, e621Tag)
|
|
|
|
}
|
|
|
|
|
2023-11-08 13:01:27 +00:00
|
|
|
func (c *neo4jConnection) EstablishPostToSourceLink(ctx context.Context, e621PostID model.PostID, sourceURL string) error {
|
2023-06-20 08:38:36 +00:00
|
|
|
return EstablishPostToSourceLink(ctx, c.driver, e621PostID, sourceURL)
|
|
|
|
}
|
|
|
|
|
2023-11-08 13:01:27 +00:00
|
|
|
func (c *neo4jConnection) EstablishUserToPostLink(ctx context.Context, e621PostID model.PostID, e621UserID model.UserID) error {
|
2023-06-20 08:38:36 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-11-08 13:01:27 +00:00
|
|
|
func (c *neo4jConnection) UploadPost(ctx context.Context, postID model.PostID) error {
|
|
|
|
return CreatePostNode(ctx, c.driver, postID)
|
2023-06-20 08:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *neo4jConnection) UploadSource(ctx context.Context, SourceURL string) error {
|
|
|
|
return CreateSourceNode(ctx, c.driver, SourceURL)
|
|
|
|
}
|
|
|
|
|
2023-11-08 13:01:27 +00:00
|
|
|
func (c *neo4jConnection) UploadUser(ctx context.Context, user model.User) 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 {
|
2023-07-26 13:27:18 +00:00
|
|
|
|
2023-06-20 08:38:36 +00:00
|
|
|
driver, err := neo4j.NewDriverWithContext(endpoint, neo4j.BasicAuth(username, password, ""),
|
2023-07-26 13:27:18 +00:00
|
|
|
logger(c.neo4jDebug))
|
|
|
|
|
2023-06-20 08:38:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-26 13:27:18 +00:00
|
|
|
err = driver.VerifyAuthentication(ctx, nil)
|
2023-06-20 08:38:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.driver = driver
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-26 13:27:18 +00:00
|
|
|
func logger(neo4jDebug bool) func(config *config.Config) {
|
2023-06-20 08:38:36 +00:00
|
|
|
return func(config *config.Config) {
|
2023-07-26 13:27:18 +00:00
|
|
|
config.Log = NewNeo4jLogger(neo4jDebug)
|
2023-06-20 08:38:36 +00:00
|
|
|
}
|
|
|
|
}
|