Compare commits
No commits in common. "da4fda3597e39951b9de9e4a693b4d890a255b19" and "e29cea6e3cb8a768186984ba0aa736f8f5a0f04e" have entirely different histories.
da4fda3597
...
e29cea6e3c
@ -176,7 +176,6 @@ func TestGetPostBySourceURL(t *testing.T) {
|
||||
BaseModel: models.BaseModel[models.AnthrovePostID]{
|
||||
ID: models.AnthrovePostID(fmt.Sprintf("%025s", "1")),
|
||||
},
|
||||
|
||||
Rating: "safe",
|
||||
}
|
||||
|
||||
@ -199,7 +198,7 @@ func TestGetPostBySourceURL(t *testing.T) {
|
||||
t.Fatal("Could not create source", err)
|
||||
}
|
||||
|
||||
err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "http://test.org")
|
||||
err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain))
|
||||
if err != nil {
|
||||
t.Fatal("Could not create source reference", err)
|
||||
}
|
||||
@ -221,7 +220,7 @@ func TestGetPostBySourceURL(t *testing.T) {
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
db: gormDB,
|
||||
sourceURL: "http://test.org",
|
||||
sourceURL: source.Domain,
|
||||
},
|
||||
want: post,
|
||||
wantErr: false,
|
||||
@ -298,7 +297,7 @@ func TestGetPostBySourceID(t *testing.T) {
|
||||
t.Fatal("Could not create source", err)
|
||||
}
|
||||
|
||||
err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "http://test.otg")
|
||||
err = CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain))
|
||||
if err != nil {
|
||||
t.Fatal("Could not create source reference", err)
|
||||
}
|
||||
|
@ -73,10 +73,6 @@ func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUs
|
||||
return result.Error
|
||||
}
|
||||
|
||||
if result.RowsAffected == 0 {
|
||||
return &otterError.NoDataWritten{}
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"anthrove_user_id": anthroveUserID,
|
||||
"source_id": sourceID,
|
||||
@ -287,15 +283,8 @@ func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUse
|
||||
return &models.FavoriteList{Posts: favoritePosts}, nil
|
||||
}
|
||||
|
||||
// Workaround, should be changed later maybe, but its not that bad right now
|
||||
type selectFrequencyTag struct {
|
||||
tagName string `gorm:"tag_name"`
|
||||
count int64 `gorm:"count"`
|
||||
tagType models.TagType `gorm:"tag_type"`
|
||||
}
|
||||
|
||||
func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
|
||||
var queryUserFavorites []selectFrequencyTag
|
||||
var userFavorites []models.UserFavorites
|
||||
|
||||
if anthroveUserID == "" {
|
||||
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDIsEmpty}
|
||||
@ -305,38 +294,55 @@ func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthrov
|
||||
return nil, &otterError.EntityValidationFailed{Reason: otterError.AnthroveUserIDToShort}
|
||||
}
|
||||
|
||||
rows, err := db.WithContext(ctx).Raw(
|
||||
`WITH user_posts AS (
|
||||
SELECT post_id FROM "UserFavorites" WHERE user_id = $1
|
||||
)
|
||||
SELECT post_tags.tag_name AS tag_name, count(*) AS count, (SELECT tag_type FROM "Tag" WHERE "Tag".name = post_tags.tag_name LIMIT 1) AS tag_type FROM post_tags, user_posts WHERE post_tags.post_id IN (user_posts.post_id) GROUP BY post_tags.tag_name ORDER BY tag_type DESC, tag_name DESC`, anthroveUserID).Rows()
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
result := db.WithContext(ctx).Where("user_id = ?", string(anthroveUserID)).Find(&userFavorites)
|
||||
if result.Error != nil {
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return nil, &otterError.NoDataFound{}
|
||||
}
|
||||
return nil, err
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
var userFavoritesFrequency = make([]models.TagsWithFrequency, 0)
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var tagName string
|
||||
var count int64
|
||||
var tagType string
|
||||
rows.Scan(&tagName, &count, &tagType)
|
||||
userFavoritesFrequency = append(userFavoritesFrequency, models.TagsWithFrequency{
|
||||
Frequency: count,
|
||||
tagFrequency := make(map[struct {
|
||||
name string
|
||||
typeName string
|
||||
}]int)
|
||||
|
||||
for _, userFavorite := range userFavorites {
|
||||
var post models.Post
|
||||
result = db.WithContext(ctx).Preload("Tags", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Order("tag_type ASC")
|
||||
}).First(&post, "id = ?", userFavorite.PostID)
|
||||
|
||||
if result.Error != nil {
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return nil, &otterError.NoDataFound{}
|
||||
}
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
for _, tag := range post.Tags {
|
||||
tagFrequency[struct {
|
||||
name string
|
||||
typeName string
|
||||
}{name: tag.Name, typeName: string(tag.Type)}]++
|
||||
}
|
||||
}
|
||||
|
||||
var tagsWithFrequency []models.TagsWithFrequency
|
||||
for data, frequency := range tagFrequency {
|
||||
tagsWithFrequency = append(tagsWithFrequency, models.TagsWithFrequency{
|
||||
Frequency: int64(frequency),
|
||||
Tags: models.Tag{
|
||||
Name: tagName,
|
||||
Type: models.TagType(tagType),
|
||||
Name: data.name,
|
||||
Type: models.TagType(data.typeName),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"anthrove_user_id": anthroveUserID,
|
||||
"tag_amount": len(queryUserFavorites),
|
||||
"tag_amount": len(tagsWithFrequency),
|
||||
}).Trace("database: got user tag node with relation to faved posts")
|
||||
|
||||
return userFavoritesFrequency, nil
|
||||
return tagsWithFrequency, nil
|
||||
}
|
||||
|
@ -162,7 +162,7 @@ func TestCreateUserNodeWithSourceRelation(t *testing.T) {
|
||||
userID: "e1",
|
||||
username: "marius",
|
||||
},
|
||||
wantErr: false,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Test 5: no userID",
|
||||
@ -270,33 +270,30 @@ func TestGetUserSourceBySourceID(t *testing.T) {
|
||||
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
||||
invalidUserID := models.AnthroveUserID("XXX")
|
||||
|
||||
validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1"))
|
||||
|
||||
source := &models.Source{
|
||||
BaseModel: models.BaseModel[models.AnthroveSourceID]{
|
||||
ID: validSourceID,
|
||||
},
|
||||
DisplayName: "e621",
|
||||
Domain: "e621.net",
|
||||
}
|
||||
|
||||
expectedResult := make(map[string]models.UserSource)
|
||||
expectedResult["e621"] = models.UserSource{
|
||||
UserID: "e1",
|
||||
AccountUsername: "euser",
|
||||
Source: models.Source{
|
||||
DisplayName: source.DisplayName,
|
||||
Domain: source.Domain,
|
||||
Icon: source.Icon,
|
||||
DisplayName: "e621",
|
||||
Domain: "e621.net",
|
||||
},
|
||||
}
|
||||
|
||||
source := &models.Source{
|
||||
BaseModel: models.BaseModel[models.AnthroveSourceID]{
|
||||
ID: expectedResult["e621"].Source.ID,
|
||||
},
|
||||
DisplayName: expectedResult["e621"].Source.DisplayName,
|
||||
Domain: expectedResult["e621"].Source.Domain,
|
||||
}
|
||||
|
||||
err = CreateSource(ctx, gormDB, source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
|
||||
err = CreateUserWithRelationToSource(ctx, gormDB, validUserID, models.AnthroveSourceID(expectedResult["e621"].SourceID), expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -392,7 +389,6 @@ func TestGetUserSourceBySourceID(t *testing.T) {
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := GetUserSourceBySourceID(tt.args.ctx, tt.args.db, tt.args.anthroveUserID, tt.args.sourceID)
|
||||
@ -870,15 +866,15 @@ func TestGetUserTagNodeWitRelationToFavedPosts(t *testing.T) {
|
||||
{
|
||||
Frequency: 1,
|
||||
Tags: models.Tag{
|
||||
Name: tags[2].Name,
|
||||
Type: tags[2].Type,
|
||||
Name: tags[1].Name,
|
||||
Type: tags[1].Type,
|
||||
},
|
||||
},
|
||||
{
|
||||
Frequency: 1,
|
||||
Tags: models.Tag{
|
||||
Name: tags[1].Name,
|
||||
Type: tags[1].Type,
|
||||
Name: tags[2].Name,
|
||||
Type: tags[2].Type,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ type OtterSpace interface {
|
||||
GetPostByAnthroveID(ctx context.Context, anthrovePostID models.AnthrovePostID) (*models.Post, error)
|
||||
|
||||
// GetPostByURL retrieves a post by its source URL.
|
||||
GetPostByURL(ctx context.Context, postURL string) (*models.Post, error)
|
||||
GetPostByURL(ctx context.Context, sourceUrl string) (*models.Post, error)
|
||||
|
||||
// GetPostBySourceID retrieves a post by its source ID.
|
||||
GetPostBySourceID(ctx context.Context, sourceID models.AnthroveSourceID) (*models.Post, error)
|
||||
|
@ -469,7 +469,6 @@ func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(t *testing.T)
|
||||
ctx context.Context
|
||||
anthrovePostID models.AnthrovePostID
|
||||
sourceDomain models.AnthroveSourceDomain
|
||||
postURl models.AnthrovePostURL
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
@ -482,7 +481,6 @@ func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(t *testing.T)
|
||||
ctx: ctx,
|
||||
anthrovePostID: post.ID,
|
||||
sourceDomain: "e621.net",
|
||||
postURl: "http://e621.net/post/eeasd",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
@ -492,7 +490,6 @@ func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(t *testing.T)
|
||||
ctx: ctx,
|
||||
anthrovePostID: "123456",
|
||||
sourceDomain: "e621.net",
|
||||
postURl: "",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
@ -502,7 +499,6 @@ func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(t *testing.T)
|
||||
ctx: ctx,
|
||||
anthrovePostID: "1234",
|
||||
sourceDomain: "fa.banana",
|
||||
postURl: "",
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
@ -511,7 +507,7 @@ func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(t *testing.T)
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
anthrovePostID: "696969",
|
||||
postURl: "",
|
||||
sourceDomain: "hehe.funny.number",
|
||||
},
|
||||
wantErr: true,
|
||||
}}
|
||||
@ -521,7 +517,7 @@ func Test_postgresqlConnection_CreateReferenceBetweenPostAndSource(t *testing.T)
|
||||
db: gormDB,
|
||||
debug: true,
|
||||
}
|
||||
if err := p.CreateReferenceBetweenPostAndSource(tt.args.ctx, tt.args.anthrovePostID, tt.args.sourceDomain, tt.args.postURl); (err != nil) != tt.wantErr {
|
||||
if err := p.CreateReferenceBetweenPostAndSource(tt.args.ctx, tt.args.anthrovePostID, tt.args.sourceDomain); (err != nil) != tt.wantErr {
|
||||
t.Errorf("CreateReferenceBetweenPostAndSource() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
@ -834,7 +830,7 @@ func Test_postgresqlConnection_GetPostByURL(t *testing.T) {
|
||||
t.Fatal("Could not create source", err)
|
||||
}
|
||||
|
||||
err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "https://e62asdwad.com/asdas")
|
||||
err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain))
|
||||
if err != nil {
|
||||
t.Fatal("Could not create source reference", err)
|
||||
}
|
||||
@ -854,7 +850,7 @@ func Test_postgresqlConnection_GetPostByURL(t *testing.T) {
|
||||
name: "Test 1: Valid sourceUrl",
|
||||
args: args{
|
||||
ctx: ctx,
|
||||
sourceUrl: "https://e62asdwad.com/asdas",
|
||||
sourceUrl: source.Domain,
|
||||
},
|
||||
want: post,
|
||||
wantErr: false,
|
||||
@ -935,7 +931,7 @@ func Test_postgresqlConnection_GetPostBySourceID(t *testing.T) {
|
||||
t.Fatal("Could not create source", err)
|
||||
}
|
||||
|
||||
err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain), "https://easd15aed.de/asd")
|
||||
err = postgres.CreateReferenceBetweenPostAndSource(ctx, gormDB, post.ID, models.AnthroveSourceDomain(source.Domain))
|
||||
if err != nil {
|
||||
t.Fatal("Could not create source reference", err)
|
||||
}
|
||||
@ -1232,34 +1228,30 @@ func Test_postgresqlConnection_GetUserSourceBySourceID(t *testing.T) {
|
||||
validUserID := models.AnthroveUserID(fmt.Sprintf("%025s", "User1"))
|
||||
invalidUserID := models.AnthroveUserID("XXX")
|
||||
|
||||
validSourceID := models.AnthroveSourceID(fmt.Sprintf("%025s", "Source1"))
|
||||
|
||||
expectedResult := make(map[string]models.UserSource)
|
||||
|
||||
source := &models.Source{
|
||||
BaseModel: models.BaseModel[models.AnthroveSourceID]{
|
||||
ID: validSourceID,
|
||||
},
|
||||
DisplayName: "e621",
|
||||
Domain: "e621.net",
|
||||
}
|
||||
|
||||
expectedResult["e621"] = models.UserSource{
|
||||
UserID: "e1",
|
||||
AccountUsername: "euser",
|
||||
Source: models.Source{
|
||||
DisplayName: source.DisplayName,
|
||||
Domain: source.Domain,
|
||||
Icon: source.Icon,
|
||||
DisplayName: "e621",
|
||||
Domain: "e621.net",
|
||||
},
|
||||
}
|
||||
|
||||
source := &models.Source{
|
||||
BaseModel: models.BaseModel[models.AnthroveSourceID]{
|
||||
ID: expectedResult["e621"].Source.ID,
|
||||
},
|
||||
DisplayName: expectedResult["e621"].Source.DisplayName,
|
||||
Domain: expectedResult["e621"].Source.Domain,
|
||||
}
|
||||
|
||||
err = postgres.CreateSource(ctx, gormDB, source)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, validSourceID, expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
|
||||
err = postgres.CreateUserWithRelationToSource(ctx, gormDB, validUserID, models.AnthroveSourceID(expectedResult["e621"].SourceID), expectedResult["e621"].UserID, expectedResult["e621"].AccountUsername)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -1613,15 +1605,15 @@ func Test_postgresqlConnection_GetUserTagWitRelationToFavedPosts(t *testing.T) {
|
||||
{
|
||||
Frequency: 1,
|
||||
Tags: models.Tag{
|
||||
Name: tags[2].Name,
|
||||
Type: tags[2].Type,
|
||||
Name: tags[1].Name,
|
||||
Type: tags[1].Type,
|
||||
},
|
||||
},
|
||||
{
|
||||
Frequency: 1,
|
||||
Tags: models.Tag{
|
||||
Name: tags[1].Name,
|
||||
Type: tags[1].Type,
|
||||
Name: tags[2].Name,
|
||||
Type: tags[2].Type,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
Reference in New Issue
Block a user