SoXX
3be16a9277
First implementation of the new algorithm that got proposed in issue #5 Reviewed-on: anthrove/e621-to-graph#20 Reviewed-by: Lennard Brinkhaus <lennard.brinkhaus@noreply.localhost> Reviewed-by: daskadse <daskadse@noreply.localhost> Co-authored-by: SoXX <soxx@fenpa.ws> Co-committed-by: SoXX <soxx@fenpa.ws>
104 lines
2.9 KiB
Go
104 lines
2.9 KiB
Go
package neo4j
|
|
|
|
import (
|
|
"context"
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
|
|
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func EstablishPostTagLink(ctx context.Context, driver neo4j.DriverWithContext, e621PostID model.PostID, e621Tag string) error {
|
|
query := `
|
|
MATCH (p:e621Post {e621PostID: $e621PostID})
|
|
MATCH (t:e621Tag {e621Tag: $e621Tag})
|
|
MERGE (p)-[:HAS_TAG]->(t);
|
|
`
|
|
params := map[string]interface{}{
|
|
"e621PostID": e621PostID,
|
|
"e621Tag": e621Tag,
|
|
}
|
|
|
|
log.WithFields(log.Fields{
|
|
"e621_post_id": e621PostID,
|
|
"e621_tag": e621Tag,
|
|
}).Trace("neo4j: creating post to e621Tag link")
|
|
_, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func EstablishPostToSourceLink(ctx context.Context, driver neo4j.DriverWithContext, e621PostID model.PostID, sourceURL string) error {
|
|
query := `
|
|
MATCH (p:e621Post {e621PostID: $e621PostID})
|
|
MATCH (s:Source {URL: $sourceURL})
|
|
MERGE (p)-[:HAS_SOURCE]->(s)
|
|
`
|
|
params := map[string]interface{}{
|
|
"e621PostID": e621PostID,
|
|
"sourceURL": sourceURL,
|
|
}
|
|
|
|
log.WithFields(log.Fields{
|
|
"e621_post_id": e621PostID,
|
|
"source_url": sourceURL,
|
|
}).Trace("neo4j: creating post to source link")
|
|
_, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func EstablishUserToPostLink(ctx context.Context, driver neo4j.DriverWithContext, e621PostID model.PostID, e621UserID model.UserID) error {
|
|
query := `
|
|
MATCH (p:e621Post {e621PostID: $e621PostID})
|
|
MATCH (u:e621User {e621ID: $e621ID})
|
|
MERGE (u)-[:IS_FAVORITE]->(p)
|
|
`
|
|
params := map[string]interface{}{
|
|
"e621PostID": e621PostID,
|
|
"e621ID": e621UserID,
|
|
}
|
|
log.WithFields(log.Fields{
|
|
"e621_post_id": e621PostID,
|
|
"e621_user_id": e621UserID,
|
|
}).Trace("neo4j: creating user to post link")
|
|
_, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// 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) {
|
|
query := `
|
|
MATCH (user:e621User {e621ID: $e621ID})-[favorite:IS_FAVORITE]->(post:e621Post {e621PostID: $e621PostID})
|
|
RETURN COUNT(favorite) > 0 AS isFavorite
|
|
`
|
|
params := map[string]interface{}{
|
|
"e621PostID": e621PostID,
|
|
"e621ID": e621UserID,
|
|
}
|
|
|
|
log.WithFields(log.Fields{
|
|
"e621_post_id": e621PostID,
|
|
"e621_user_id": e621UserID,
|
|
}).Trace("neo4j: check user post relationship")
|
|
result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
exists, _, err := neo4j.GetRecordValue[bool](result.Records[0], "isFavorite")
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return exists, nil
|
|
}
|