Compare commits

..

No commits in common. "5fb2a1770a8eb3981bba0885ae3c372d42404b7c" and "302b4b5f9cedb7f5201873a4648a45f4d88118f1" have entirely different histories.

5 changed files with 17 additions and 71 deletions

1
.gitignore vendored
View File

@ -192,4 +192,3 @@ $RECYCLE.BIN/
.env .env
main.go

View File

@ -3,7 +3,6 @@ package internal
import ( import (
"context" "context"
"fmt" "fmt"
"git.dragse.it/anthrove/otter-space-sdk/internal/utils" "git.dragse.it/anthrove/otter-space-sdk/internal/utils"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
"github.com/neo4j/neo4j-go-driver/v5/neo4j" "github.com/neo4j/neo4j-go-driver/v5/neo4j"
@ -317,22 +316,16 @@ func GetAllAnthroveUserIDs(ctx context.Context, driver neo4j.DriverWithContext)
} }
func GetUserFavoriteNodeWithPagination(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) { func GetUserFavoriteNodeWithPagination(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID, skip int, limit int) ([]models.AnthrovePost, error) {
var err error var err error
var favoritePosts []models.FavoritePost var anthrovePosts []models.AnthrovePost
query := ` query := `
CALL {
MATCH (user:User{user_id: $anthrove_user_id})-[r:FAV]->(p:AnthrovePost) MATCH (user:User{user_id: $anthrove_user_id})-[r:FAV]->(p:AnthrovePost)
RETURN p.post_id AS post_id RETURN p as anthrovePost
ORDER BY id(p) ASC ORDER BY id(r) DESC
SKIP $skip SKIP $skip
LIMIT $limit LIMIT $limit;
}
WITH collect(post_id) AS faves
MATCH (a:AnthrovePost)<-[r:REFERENCE]-(s:Source)
WHERE a.post_id in faves
RETURN a AS anthrovePost, r AS postRelation, s AS Source
` `
params := map[string]any{ params := map[string]any{
"anthrove_user_id": anthroveUserID, "anthrove_user_id": anthroveUserID,
@ -352,52 +345,22 @@ func GetUserFavoriteNodeWithPagination(ctx context.Context, driver neo4j.DriverW
for i := range result.Records { for i := range result.Records {
record := result.Records[i] record := result.Records[i]
anthrovePost, _, err := neo4j.GetRecordValue[neo4j.Node](record, "anthrovePost") post, _, err := neo4j.GetRecordValue[neo4j.Node](record, "anthrovePost")
if err != nil { if err != nil {
return nil, err return nil, err
} }
postRelation, _, err := neo4j.GetRecordValue[neo4j.Relationship](record, "postRelation") anthrovePosts = append(anthrovePosts, models.AnthrovePost{
if err != nil { PostID: models.AnthrovePostID(post.Props["post_id"].(string)),
return nil, err Rating: models.AnthroveRating(post.Props["rating"].(string)),
}
source, _, err := neo4j.GetRecordValue[neo4j.Node](record, "Source")
if err != nil {
return nil, err
}
if len(favoritePosts) != 0 && favoritePosts[len(favoritePosts)-1].AnthrovePost.PostID == models.AnthrovePostID(anthrovePost.Props["post_id"].(string)) {
favoritePosts[len(favoritePosts)-1].Relations = append(favoritePosts[len(favoritePosts)-1].Relations, models.FavoriteRelations{
SourcesID: source.Props["display_name"].(string),
Relations: models.AnthrovePostRelationship{
PostID: postRelation.Props["source_post_id"].(string),
Url: postRelation.Props["url"].(string),
},
}) })
} else {
favoritePosts = append(favoritePosts, models.FavoritePost{
AnthrovePost: models.AnthrovePost{
PostID: models.AnthrovePostID(anthrovePost.Props["post_id"].(string)),
Rating: models.AnthroveRating(anthrovePost.Props["rating"].(string)),
},
Relations: []models.FavoriteRelations{{
SourcesID: source.Props["display_name"].(string),
Relations: models.AnthrovePostRelationship{
PostID: postRelation.Props["source_post_id"].(string),
Url: postRelation.Props["url"].(string),
},
}},
})
}
} }
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"anthrove_user_fav_count": len(favoritePosts), "anthrove_user_fav_count": len(anthrovePosts),
}).Trace("graph: got al anthrove user favorites") }).Trace("graph: got al anthrove user favorites")
return &models.FavoriteList{Posts: favoritePosts}, nil return anthrovePosts, nil
} }

View File

@ -27,7 +27,6 @@ package graph
import ( import (
"context" "context"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models" "git.dragse.it/anthrove/otter-space-sdk/pkg/models"
) )
@ -87,7 +86,7 @@ type OtterSpace interface {
// It returns a map of source domains to user-source relationships, and an error if the operation fails. // It returns a map of source domains to user-source relationships, and an error if the operation fails.
GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.AnthroveUserRelationship, error) GetUserSourceLinks(ctx context.Context, anthroveUserID models.AnthroveUserID) (map[string]models.AnthroveUserRelationship, error)
// GetSpecifiedUserSourceLink GetUserSourceLinks retrieves the links between a user and a specific source in the OtterSpace graph. // GetUserSourceLinks retrieves the links between a user and sources in the OtterSpace graph.
// It returns a map of source domains to user-source relationships, and an error if the operation fails. // It returns a map of source domains to user-source relationships, and an error if the operation fails.
GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.AnthroveUserRelationship, error) GetSpecifiedUserSourceLink(ctx context.Context, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.AnthroveUserRelationship, error)
@ -99,6 +98,6 @@ type OtterSpace interface {
// It returns a slice of user IDs and an error if the operation fails. // It returns a slice of user IDs and an error if the operation fails.
GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error) GetAllAnthroveUserIDs(ctx context.Context) ([]models.AnthroveUserID, error)
// GetUserFavoritePostsWithPagination gets all user favorites with relation and sources for the given user //TODO
GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) ([]models.AnthrovePost, error)
} }

View File

@ -96,7 +96,7 @@ func (g *graphConnection) GetAllAnthroveUserIDs(ctx context.Context) ([]models.A
return internal.GetAllAnthroveUserIDs(ctx, g.driver) return internal.GetAllAnthroveUserIDs(ctx, g.driver)
} }
func (g *graphConnection) GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) { func (g *graphConnection) GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) ([]models.AnthrovePost, error) {
return internal.GetUserFavoriteNodeWithPagination(ctx, g.driver, anthroveUserID, skip, limit) return internal.GetUserFavoriteNodeWithPagination(ctx, g.driver, anthroveUserID, skip, limit)
} }

View File

@ -1,15 +0,0 @@
package models
type FavoriteRelations struct {
SourcesID string `json:"sources_id"`
Relations AnthrovePostRelationship `json:"relations"`
}
type FavoritePost struct {
AnthrovePost AnthrovePost `json:"anthrove_post"`
Relations []FavoriteRelations `json:"relations"`
}
type FavoriteList struct {
Posts []FavoritePost `json:"posts,omitempty"`
}