216 lines
6.3 KiB
Go
216 lines
6.3 KiB
Go
package internal
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.dragse.it/anthrove/anthrove-graph-sdk.git/internal/utils"
|
|
|
|
"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
|
|
}
|
|
|
|
func GetUserFavoritesCount(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) (int64, error) {
|
|
var userFavoriteCount int64
|
|
|
|
query := `
|
|
MATCH (userNode:User {user_id: anthrove_user_id})
|
|
MATCH (userNode)-[:FAV]->(favPost:AnthrovePost)
|
|
MATCH (sourceNode)-[:REFERENCE]->(favPost)
|
|
RETURN count(favPost) AS FavoritePostsCount
|
|
`
|
|
|
|
params := map[string]any{
|
|
"anthrove_user_id": anthroveUserID,
|
|
}
|
|
|
|
result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if len(result.Records) == 0 {
|
|
// no matches -> user does not exist, return count 0
|
|
return userFavoriteCount, err
|
|
}
|
|
|
|
record := result.Records[0]
|
|
|
|
userFavoriteCount, _, err = neo4j.GetRecordValue[int64](record, "FavoritePostsCount")
|
|
if err != nil {
|
|
return userFavoriteCount, err
|
|
}
|
|
|
|
log.WithFields(log.Fields{
|
|
"anthrove_user_id": anthroveUserID,
|
|
"anthrove_user_fav_count": userFavoriteCount,
|
|
}).Trace("graph: got user favorite count")
|
|
|
|
return userFavoriteCount, nil
|
|
}
|
|
|
|
func GetUserSourceLink(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) (map[string]models.AnthroveUserRelationship, error) {
|
|
|
|
userSource := make(map[string]models.AnthroveUserRelationship)
|
|
|
|
query := `
|
|
MATCH (user:User{user_id: anthrove_user_id})-[r:HAS_ACCOUNT_AT]->(s:Source)
|
|
RETURN toString(r.user_id) AS sourceUserID, toString(r.username) AS sourceUsername, s.display_name as sourceDisplayName;
|
|
`
|
|
params := map[string]any{
|
|
"anthrove_user_id": anthroveUserID,
|
|
}
|
|
|
|
result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(result.Records) == 0 {
|
|
return nil, fmt.Errorf("user has no relations")
|
|
}
|
|
|
|
for i := range result.Records {
|
|
record := result.Records[i]
|
|
sourceName, _, err := neo4j.GetRecordValue[string](record, "sourceDisplayName")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
sourceUserID, _, err := neo4j.GetRecordValue[string](record, "sourceUserID")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
sourceUsername, _, err := neo4j.GetRecordValue[string](record, "sourceUsername")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
anthroveSourceUser := models.AnthroveUserRelationship{
|
|
UserID: sourceUserID,
|
|
Username: sourceUsername,
|
|
}
|
|
userSource[sourceName] = anthroveSourceUser
|
|
}
|
|
|
|
log.WithFields(log.Fields{
|
|
"anthrove_user_id": anthroveUserID,
|
|
"anthrove_data": userSource,
|
|
}).Trace("graph: got user favorite count")
|
|
|
|
return userSource, nil
|
|
}
|
|
|
|
func GetAnthroveUser(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) (*models.AnthroveUser, error) {
|
|
var err error
|
|
var anthroveUser models.AnthroveUser
|
|
var userSources models.AnthroveSource
|
|
userRelationships := make([]models.AnthroveUserRelationship, 0)
|
|
|
|
query := `
|
|
MATCH (user:User{user_id: $anthrove_user_id})-[relation:HAS_ACCOUNT_AT]->(source:Source)
|
|
RETURN user as User, relation as Relation, source as Source;
|
|
`
|
|
params := map[string]any{
|
|
"anthrove_user_id": anthroveUserID,
|
|
}
|
|
|
|
result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(result.Records) == 0 {
|
|
return nil, fmt.Errorf("user has no relations")
|
|
}
|
|
|
|
for i := range result.Records {
|
|
record := result.Records[i]
|
|
|
|
user, _, err := neo4j.GetRecordValue[neo4j.Node](record, "User")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
relation, _, err := neo4j.GetRecordValue[neo4j.Relationship](record, "Relation")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
source, _, err := neo4j.GetRecordValue[neo4j.Node](record, "Source")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
userRelationships = append(userRelationships, models.AnthroveUserRelationship{
|
|
UserID: fmt.Sprintf("%v", utils.GetOrDefault(relation.Props, "user_id", "")),
|
|
Username: utils.GetOrDefault(relation.Props, "username", "").(string),
|
|
ScrapeTimeInterval: utils.GetOrDefault(relation.Props, "scrape_time_interval", "").(string),
|
|
})
|
|
|
|
userSources = models.AnthroveSource{
|
|
DisplayName: utils.GetOrDefault(source.Props, "display_name", "").(string),
|
|
Domain: utils.GetOrDefault(source.Props, "domain", "").(string),
|
|
Icon: utils.GetOrDefault(source.Props, "icon", "").(string),
|
|
}
|
|
|
|
anthroveUser.UserID = models.AnthroveUserID(utils.GetOrDefault(user.Props, "user_id", "").(string))
|
|
anthroveUser.Relationship = userRelationships
|
|
|
|
for j := range userRelationships {
|
|
anthroveUser.Relationship[j].Source = userSources
|
|
}
|
|
|
|
}
|
|
|
|
log.WithFields(log.Fields{
|
|
"anthrove_user_id": anthroveUserID,
|
|
}).Trace("graph: got anthrove user")
|
|
|
|
return &anthroveUser, nil
|
|
|
|
}
|