2023-06-20 08:38:36 +00:00
|
|
|
package neo4j
|
2023-05-22 20:10:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-11-08 13:01:27 +00:00
|
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
|
2023-05-22 20:10:05 +00:00
|
|
|
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
|
2023-07-26 13:27:18 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2023-05-22 20:10:05 +00:00
|
|
|
)
|
|
|
|
|
2023-11-08 13:01:27 +00:00
|
|
|
func EstablishPostTagLink(ctx context.Context, driver neo4j.DriverWithContext, e621PostID model.PostID, e621Tag string) error {
|
2023-05-22 20:10:05 +00:00
|
|
|
query := `
|
|
|
|
MATCH (p:e621Post {e621PostID: $e621PostID})
|
|
|
|
MATCH (t:e621Tag {e621Tag: $e621Tag})
|
2023-11-15 20:28:44 +00:00
|
|
|
MERGE (p)-[:HAS_TAG]->(t);
|
2023-05-22 20:10:05 +00:00
|
|
|
`
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"e621PostID": e621PostID,
|
|
|
|
"e621Tag": e621Tag,
|
|
|
|
}
|
|
|
|
|
2023-07-26 13:27:18 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"e621_post_id": e621PostID,
|
|
|
|
"e621_tag": e621Tag,
|
|
|
|
}).Trace("neo4j: creating post to e621Tag link")
|
2023-05-22 20:10:05 +00:00
|
|
|
_, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-08 13:01:27 +00:00
|
|
|
func EstablishPostToSourceLink(ctx context.Context, driver neo4j.DriverWithContext, e621PostID model.PostID, sourceURL string) error {
|
2023-05-22 20:10:05 +00:00
|
|
|
query := `
|
|
|
|
MATCH (p:e621Post {e621PostID: $e621PostID})
|
|
|
|
MATCH (s:Source {URL: $sourceURL})
|
|
|
|
MERGE (p)-[:HAS_SOURCE]->(s)
|
|
|
|
`
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"e621PostID": e621PostID,
|
|
|
|
"sourceURL": sourceURL,
|
|
|
|
}
|
|
|
|
|
2023-07-26 13:27:18 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"e621_post_id": e621PostID,
|
|
|
|
"source_url": sourceURL,
|
|
|
|
}).Trace("neo4j: creating post to source link")
|
2023-05-22 20:10:05 +00:00
|
|
|
_, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-08 13:01:27 +00:00
|
|
|
func EstablishUserToPostLink(ctx context.Context, driver neo4j.DriverWithContext, e621PostID model.PostID, e621UserID model.UserID) error {
|
2023-05-22 20:10:05 +00:00
|
|
|
query := `
|
|
|
|
MATCH (p:e621Post {e621PostID: $e621PostID})
|
|
|
|
MATCH (u:e621User {e621ID: $e621ID})
|
|
|
|
MERGE (u)-[:IS_FAVORITE]->(p)
|
|
|
|
`
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"e621PostID": e621PostID,
|
|
|
|
"e621ID": e621UserID,
|
|
|
|
}
|
2023-07-26 13:27:18 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"e621_post_id": e621PostID,
|
|
|
|
"e621_user_id": e621UserID,
|
|
|
|
}).Trace("neo4j: creating user to post link")
|
2023-05-22 20:10:05 +00:00
|
|
|
_, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-30 12:04:13 +00:00
|
|
|
|
2023-11-08 13:01:27 +00:00
|
|
|
// CheckUserToPostLink gives back a bool if the connection between the post and the user exists
|
|
|
|
func CheckUserToPostLink(ctx context.Context, driver neo4j.DriverWithContext, e621PostID model.PostID, e621UserID model.UserID) (bool, error) {
|
2023-05-30 12:04:13 +00:00
|
|
|
query := `
|
2023-06-17 17:13:25 +00:00
|
|
|
MATCH (user:e621User {e621ID: $e621ID})-[favorite:IS_FAVORITE]->(post:e621Post {e621PostID: $e621PostID})
|
2023-05-30 12:04:13 +00:00
|
|
|
RETURN COUNT(favorite) > 0 AS isFavorite
|
|
|
|
`
|
|
|
|
params := map[string]interface{}{
|
|
|
|
"e621PostID": e621PostID,
|
|
|
|
"e621ID": e621UserID,
|
|
|
|
}
|
|
|
|
|
2023-07-26 13:27:18 +00:00
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"e621_post_id": e621PostID,
|
|
|
|
"e621_user_id": e621UserID,
|
|
|
|
}).Trace("neo4j: check user post relationship")
|
2023-05-30 12:04:13 +00:00
|
|
|
result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
|
|
|
|
if err != nil {
|
2023-06-17 17:13:25 +00:00
|
|
|
return false, err
|
2023-05-30 12:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exists, _, err := neo4j.GetRecordValue[bool](result.Records[0], "isFavorite")
|
|
|
|
if err != nil {
|
2023-06-17 17:13:25 +00:00
|
|
|
return false, err
|
2023-05-30 12:04:13 +00:00
|
|
|
}
|
2023-06-17 17:13:25 +00:00
|
|
|
return exists, nil
|
2023-05-30 12:04:13 +00:00
|
|
|
}
|