From 4db345a44408ead9c2c311d070f208611cf02681 Mon Sep 17 00:00:00 2001 From: SoXX Date: Wed, 15 May 2024 15:18:23 +0200 Subject: [PATCH 1/3] feat: added GetUserTagsTroughFavedPosts --- internal/user.go | 52 +++++++++++++++++++++++++++++++++++++++++++++- pkg/graph/graph.go | 3 +++ pkg/graph/impl.go | 5 +++++ pkg/models/api.go | 5 +++++ 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/internal/user.go b/internal/user.go index a65200e..3df4d74 100644 --- a/internal/user.go +++ b/internal/user.go @@ -295,7 +295,6 @@ func GetAllAnthroveUserIDs(ctx context.Context, driver neo4j.DriverWithContext) if len(result.Records) == 0 { log.Warnf("No users found, this should not be happening!") return []models.AnthroveUserID{}, nil - } for i := range result.Records { @@ -402,3 +401,54 @@ func GetUserFavoriteNodeWithPagination(ctx context.Context, driver neo4j.DriverW return &models.FavoriteList{Posts: favoritePosts}, nil } + +func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) ([]models.UserTagsTroughFavedPost, error) { + var userTags []models.UserTagsTroughFavedPost + + query := ` + MATCH (u:User {user_id: $anthrove_user_id})-[:FAV]->(:AnthrovePost)-[:HAS]->(t:Tag) + RETURN t as tag, COUNT(t) AS frequency + ORDER BY frequency DESC + ` + 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, nil + } + + for i := range result.Records { + record := result.Records[i] + + tag, _, err := neo4j.GetRecordValue[neo4j.Node](record, "tag") + if err != nil { + return nil, err + } + + frequency, _, err := neo4j.GetRecordValue[int64](record, "frequency") + if err != nil { + return nil, err + } + + userTags = append(userTags, models.UserTagsTroughFavedPost{ + Frequency: frequency, + Tags: models.AnthroveTag{ + Name: tag.Props["name"].(string), + Type: tag.Props["type"].(string), + }, + }) + + } + + log.WithFields(log.Fields{ + "tag_amount": len(userTags), + }).Trace("graph: created tag node") + + return userTags, nil +} diff --git a/pkg/graph/graph.go b/pkg/graph/graph.go index a824319..8acb1da 100644 --- a/pkg/graph/graph.go +++ b/pkg/graph/graph.go @@ -101,4 +101,7 @@ type OtterSpace interface { // GetUserFavoritePostsWithPagination gets all user favorites with relation and sources for the given user GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) + + // GetUserTagsTroughFavedPosts returns a list of Tags that the user hs favorites through a post + GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.UserTagsTroughFavedPost, error) } diff --git a/pkg/graph/impl.go b/pkg/graph/impl.go index 4db1b3f..5f77618 100644 --- a/pkg/graph/impl.go +++ b/pkg/graph/impl.go @@ -100,6 +100,11 @@ func (g *graphConnection) GetUserFavoritePostsWithPagination(ctx context.Context return internal.GetUserFavoriteNodeWithPagination(ctx, g.driver, anthroveUserID, skip, limit) } +func (g *graphConnection) GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.UserTagsTroughFavedPost, error) { + return internal.GetUserTagNodeWitRelationToFavedPosts(ctx, g.driver, anthroveUserID) + +} + func logger(graphDebug bool) func(config *config.Config) { return func(config *config.Config) { config.Log = internal.NewGraphLogger(graphDebug) diff --git a/pkg/models/api.go b/pkg/models/api.go index 0c2da29..772a327 100644 --- a/pkg/models/api.go +++ b/pkg/models/api.go @@ -13,3 +13,8 @@ type FavoritePost struct { type FavoriteList struct { Posts []FavoritePost `json:"posts,omitempty"` } + +type UserTagsTroughFavedPost struct { + Frequency int64 `json:"frequency"` + Tags AnthroveTag `json:"tags"` +} -- 2.45.2 From 9f2ebe90ee6467c5e284412bd607dfff6aa90db9 Mon Sep 17 00:00:00 2001 From: SoXX Date: Wed, 15 May 2024 15:31:17 +0200 Subject: [PATCH 2/3] feat: added GetTags --- internal/tag.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++ internal/user.go | 6 +++--- pkg/graph/graph.go | 5 ++++- pkg/graph/impl.go | 5 ++++- pkg/models/api.go | 2 +- 5 files changed, 61 insertions(+), 6 deletions(-) diff --git a/internal/tag.go b/internal/tag.go index ab26434..3e5a02d 100644 --- a/internal/tag.go +++ b/internal/tag.go @@ -33,3 +33,52 @@ func CreateTagNodeWitRelation(ctx context.Context, driver neo4j.DriverWithContex return nil } + +func GetTags(ctx context.Context, driver neo4j.DriverWithContext) ([]models.TagsWithFrequency, error) { + var userTags []models.TagsWithFrequency + + query := ` + MATCH (:AnthrovePost)-[:HAS]->(t:Tag) + RETURN t as tag, COUNT(t) AS frequency + ORDER BY frequency DESC + ` + params := map[string]any{} + + result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) + if err != nil { + return nil, err + } + + if len(result.Records) == 0 { + return nil, nil + } + + for i := range result.Records { + record := result.Records[i] + + tag, _, err := neo4j.GetRecordValue[neo4j.Node](record, "tag") + if err != nil { + return nil, err + } + + frequency, _, err := neo4j.GetRecordValue[int64](record, "frequency") + if err != nil { + return nil, err + } + + userTags = append(userTags, models.TagsWithFrequency{ + Frequency: frequency, + Tags: models.AnthroveTag{ + Name: tag.Props["name"].(string), + Type: tag.Props["type"].(string), + }, + }) + + } + + log.WithFields(log.Fields{ + "tag_amount": len(userTags), + }).Trace("graph: created tag node") + + return userTags, nil +} diff --git a/internal/user.go b/internal/user.go index 3df4d74..a1128ec 100644 --- a/internal/user.go +++ b/internal/user.go @@ -402,8 +402,8 @@ func GetUserFavoriteNodeWithPagination(ctx context.Context, driver neo4j.DriverW } -func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) ([]models.UserTagsTroughFavedPost, error) { - var userTags []models.UserTagsTroughFavedPost +func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, driver neo4j.DriverWithContext, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) { + var userTags []models.TagsWithFrequency query := ` MATCH (u:User {user_id: $anthrove_user_id})-[:FAV]->(:AnthrovePost)-[:HAS]->(t:Tag) @@ -436,7 +436,7 @@ func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, driver neo4j.Dri return nil, err } - userTags = append(userTags, models.UserTagsTroughFavedPost{ + userTags = append(userTags, models.TagsWithFrequency{ Frequency: frequency, Tags: models.AnthroveTag{ Name: tag.Props["name"].(string), diff --git a/pkg/graph/graph.go b/pkg/graph/graph.go index 8acb1da..ac3786f 100644 --- a/pkg/graph/graph.go +++ b/pkg/graph/graph.go @@ -103,5 +103,8 @@ type OtterSpace interface { GetUserFavoritePostsWithPagination(ctx context.Context, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) // GetUserTagsTroughFavedPosts returns a list of Tags that the user hs favorites through a post - GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.UserTagsTroughFavedPost, error) + GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) + + // GetAllTags returns a list of Tags that the user hs favorites through a post + GetAllTags(ctx context.Context) ([]models.TagsWithFrequency, error) } diff --git a/pkg/graph/impl.go b/pkg/graph/impl.go index 5f77618..9a47593 100644 --- a/pkg/graph/impl.go +++ b/pkg/graph/impl.go @@ -100,9 +100,12 @@ func (g *graphConnection) GetUserFavoritePostsWithPagination(ctx context.Context return internal.GetUserFavoriteNodeWithPagination(ctx, g.driver, anthroveUserID, skip, limit) } -func (g *graphConnection) GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.UserTagsTroughFavedPost, error) { +func (g *graphConnection) GetUserTagsTroughFavedPosts(ctx context.Context, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) { return internal.GetUserTagNodeWitRelationToFavedPosts(ctx, g.driver, anthroveUserID) +} +func (g *graphConnection) GetAllTags(ctx context.Context) ([]models.TagsWithFrequency, error) { + return internal.GetTags(ctx, g.driver) } func logger(graphDebug bool) func(config *config.Config) { diff --git a/pkg/models/api.go b/pkg/models/api.go index 772a327..a30b955 100644 --- a/pkg/models/api.go +++ b/pkg/models/api.go @@ -14,7 +14,7 @@ type FavoriteList struct { Posts []FavoritePost `json:"posts,omitempty"` } -type UserTagsTroughFavedPost struct { +type TagsWithFrequency struct { Frequency int64 `json:"frequency"` Tags AnthroveTag `json:"tags"` } -- 2.45.2 From 1cce71e330842979db22b59d14d5b00c8ab36cda Mon Sep 17 00:00:00 2001 From: SoXX Date: Wed, 15 May 2024 15:44:34 +0200 Subject: [PATCH 3/3] feat: added GetAllSources --- internal/source.go | 40 ++++++++++++++++++++++++++++++++++++++++ pkg/graph/graph.go | 3 +++ pkg/graph/impl.go | 4 ++++ 3 files changed, 47 insertions(+) diff --git a/internal/source.go b/internal/source.go index d78ffe2..fa6e0a4 100644 --- a/internal/source.go +++ b/internal/source.go @@ -33,3 +33,43 @@ func CreateSourceNode(ctx context.Context, driver neo4j.DriverWithContext, anthr return nil } +func GetAllSourceNodes(ctx context.Context, driver neo4j.DriverWithContext) ([]models.AnthroveSource, error) { + var sources []models.AnthroveSource + + query := ` + MATCH (s:Source) + RETURN s as source + ` + params := map[string]any{} + + result, err := neo4j.ExecuteQuery(ctx, driver, query, params, neo4j.EagerResultTransformer) + if err != nil { + return nil, err + } + + if len(result.Records) == 0 { + return nil, nil + } + + for i := range result.Records { + record := result.Records[i] + + source, _, err := neo4j.GetRecordValue[neo4j.Node](record, "source") + if err != nil { + return nil, err + } + + sources = append(sources, models.AnthroveSource{ + DisplayName: source.Props["display_name"].(string), + Domain: source.Props["domain"].(string), + Icon: source.Props["icon"].(string), + }) + + } + + log.WithFields(log.Fields{ + "tag_amount": len(sources), + }).Trace("graph: created tag node") + + return sources, nil +} diff --git a/pkg/graph/graph.go b/pkg/graph/graph.go index ac3786f..b933d11 100644 --- a/pkg/graph/graph.go +++ b/pkg/graph/graph.go @@ -107,4 +107,7 @@ type OtterSpace interface { // GetAllTags returns a list of Tags that the user hs favorites through a post GetAllTags(ctx context.Context) ([]models.TagsWithFrequency, error) + + // GetAllSources returns a list of Sources in the database + GetAllSources(ctx context.Context) ([]models.AnthroveSource, error) } diff --git a/pkg/graph/impl.go b/pkg/graph/impl.go index 9a47593..573d153 100644 --- a/pkg/graph/impl.go +++ b/pkg/graph/impl.go @@ -108,6 +108,10 @@ func (g *graphConnection) GetAllTags(ctx context.Context) ([]models.TagsWithFreq return internal.GetTags(ctx, g.driver) } +func (g *graphConnection) GetAllSources(ctx context.Context) ([]models.AnthroveSource, error) { + return internal.GetAllSourceNodes(ctx, g.driver) +} + func logger(graphDebug bool) func(config *config.Config) { return func(config *config.Config) { config.Log = internal.NewGraphLogger(graphDebug) -- 2.45.2