2024-06-14 12:48:23 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-20 13:24:55 +00:00
|
|
|
"fmt"
|
2024-06-14 12:48:23 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
2024-06-14 13:03:34 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels"
|
2024-06-14 12:48:23 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-06-19 21:32:42 +00:00
|
|
|
func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) error {
|
|
|
|
|
2024-06-20 13:24:55 +00:00
|
|
|
if anthroveUserID == "" {
|
|
|
|
return fmt.Errorf("anthroveUserID cannot be empty")
|
|
|
|
}
|
|
|
|
|
2024-06-19 21:32:42 +00:00
|
|
|
user := pgModels.User{
|
|
|
|
BaseModel: pgModels.BaseModel{
|
|
|
|
ID: string(anthroveUserID),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := db.WithContext(ctx).Create(&user).Error
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
}).Error("database: failed to create user")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-14 12:48:23 +00:00
|
|
|
func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error {
|
2024-06-20 13:24:55 +00:00
|
|
|
|
|
|
|
if anthroveUserID == "" || username == "" || userID == "" {
|
|
|
|
return fmt.Errorf("anthroveUserID cannot be empty")
|
|
|
|
}
|
|
|
|
|
2024-06-14 12:48:23 +00:00
|
|
|
user := pgModels.User{
|
|
|
|
BaseModel: pgModels.BaseModel{
|
|
|
|
ID: string(anthroveUserID),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.WithContext(ctx).FirstOrCreate(&user).Error; err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
}).Error("database: failed to find or create user")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
source := pgModels.Source{
|
|
|
|
Domain: sourceDomain,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.WithContext(ctx).Where(&source).First(&source).Error; err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"source_domain": sourceDomain,
|
|
|
|
}).Error("database: failed to find source")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
userSource := pgModels.UserSource{
|
|
|
|
UserID: user.ID,
|
|
|
|
SourceID: source.ID,
|
|
|
|
AccountUsername: username,
|
|
|
|
AccountID: userID,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := db.WithContext(ctx).Create(&userSource).Error; err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
"source_domain": sourceDomain,
|
|
|
|
"account_username": username,
|
|
|
|
"account_id": userID,
|
|
|
|
}).Error("database: failed to create user-source relationship")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
"source_domain": sourceDomain,
|
|
|
|
"account_username": username,
|
|
|
|
"account_id": userID,
|
|
|
|
}).Trace("database: created user-source relationship")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-14 12:50:29 +00:00
|
|
|
|
|
|
|
func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (int64, error) {
|
2024-06-20 20:45:03 +00:00
|
|
|
if anthroveUserID == "" {
|
|
|
|
return 0, fmt.Errorf("anthroveUserID cannot be empty")
|
|
|
|
}
|
|
|
|
|
2024-06-14 12:50:29 +00:00
|
|
|
var count int64
|
|
|
|
err := db.WithContext(ctx).Model(&pgModels.UserFavorite{}).Where("user_id = ?", string(anthroveUserID)).Count(&count).Error
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
}).Error("database: failed to get user favorites count")
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
"anthrove_user_fav_count": count,
|
|
|
|
}).Trace("database: got user favorite count")
|
|
|
|
|
|
|
|
return count, nil
|
|
|
|
}
|
2024-06-14 13:03:34 +00:00
|
|
|
|
|
|
|
func GetUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (map[string]graphModels.AnthroveUserRelationship, error) {
|
|
|
|
var userSources []pgModels.UserSource
|
|
|
|
userSourceMap := make(map[string]graphModels.AnthroveUserRelationship)
|
|
|
|
|
|
|
|
err := db.WithContext(ctx).Model(&pgModels.UserSource{}).Where("user_id = ?", string(anthroveUserID)).Find(&userSources).Error
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
}).Error("database: failed to get user source link")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, userSource := range userSources {
|
|
|
|
var source pgModels.Source
|
|
|
|
err = db.WithContext(ctx).Model(&pgModels.Source{}).Where("id = ?", userSource.SourceID).First(&source).Error
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"source_id": userSource.SourceID,
|
|
|
|
}).Error("database: failed to get source")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
userSourceMap[source.DisplayName] = graphModels.AnthroveUserRelationship{
|
|
|
|
UserID: userSource.AccountID,
|
|
|
|
Username: userSource.AccountUsername,
|
|
|
|
Source: graphModels.AnthroveSource{
|
|
|
|
DisplayName: source.DisplayName,
|
|
|
|
Domain: source.Domain,
|
|
|
|
Icon: source.Icon,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
}).Trace("database: got user source link")
|
|
|
|
|
|
|
|
return userSourceMap, nil
|
|
|
|
}
|
2024-06-14 13:04:45 +00:00
|
|
|
|
|
|
|
func GetSpecifiedUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]graphModels.AnthroveUserRelationship, error) {
|
2024-06-20 20:45:03 +00:00
|
|
|
if anthroveUserID == "" || sourceDisplayName == "" {
|
|
|
|
return nil, fmt.Errorf("anthroveUserID or sourceDisplayName is empty")
|
|
|
|
}
|
|
|
|
|
2024-06-14 13:04:45 +00:00
|
|
|
var userSources []pgModels.UserSource
|
|
|
|
userSourceMap := make(map[string]graphModels.AnthroveUserRelationship)
|
|
|
|
|
2024-06-20 20:45:03 +00:00
|
|
|
err := db.WithContext(ctx).Model(&pgModels.UserSource{}).InnerJoins("Source", db.Where("display_name = ?", sourceDisplayName)).Where("user_id = ?", string(anthroveUserID)).First(&userSources).Error
|
2024-06-14 13:04:45 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
"source_display_name": sourceDisplayName,
|
|
|
|
}).Error("database: failed to get specified user source link")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, userSource := range userSources {
|
|
|
|
var source pgModels.Source
|
|
|
|
err = db.WithContext(ctx).Model(&pgModels.Source{}).Where("id = ?", userSource.SourceID).First(&source).Error
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"source_id": userSource.SourceID,
|
|
|
|
}).Error("database: failed to get source")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
userSourceMap[source.DisplayName] = graphModels.AnthroveUserRelationship{
|
|
|
|
UserID: userSource.AccountID,
|
|
|
|
Username: userSource.AccountUsername,
|
|
|
|
Source: graphModels.AnthroveSource{
|
|
|
|
DisplayName: source.DisplayName,
|
|
|
|
Domain: source.Domain,
|
|
|
|
Icon: source.Icon,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
"source_display_name": sourceDisplayName,
|
|
|
|
}).Trace("database: got specified user source link")
|
|
|
|
|
|
|
|
return userSourceMap, nil
|
|
|
|
}
|
2024-06-14 13:07:23 +00:00
|
|
|
|
|
|
|
func GetAnthroveUser(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (*graphModels.AnthroveUser, error) {
|
2024-06-20 13:24:55 +00:00
|
|
|
|
|
|
|
if anthroveUserID == "" {
|
|
|
|
return nil, fmt.Errorf("anthroveUserID cannot be empty")
|
|
|
|
}
|
|
|
|
|
2024-06-14 13:07:23 +00:00
|
|
|
var user pgModels.User
|
|
|
|
var userSources []pgModels.UserSource
|
|
|
|
anthroveUser := &graphModels.AnthroveUser{
|
|
|
|
UserID: anthroveUserID,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := db.WithContext(ctx).First(&user, "id = ?", string(anthroveUserID)).Error
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
}).Error("database: failed to get user")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.WithContext(ctx).Model(&pgModels.UserSource{}).Where("user_id = ?", string(anthroveUserID)).Find(&userSources).Error
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
}).Error("database: failed to get user sources")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, userSource := range userSources {
|
|
|
|
var source pgModels.Source
|
|
|
|
err = db.WithContext(ctx).Model(&pgModels.Source{}).Where("id = ?", userSource.SourceID).First(&source).Error
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"source_id": userSource.SourceID,
|
|
|
|
}).Error("database: failed to get source")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
anthroveUser.Relationship = append(anthroveUser.Relationship, graphModels.AnthroveUserRelationship{
|
|
|
|
UserID: userSource.AccountID,
|
|
|
|
Username: userSource.AccountUsername,
|
|
|
|
Source: graphModels.AnthroveSource{
|
|
|
|
DisplayName: source.DisplayName,
|
|
|
|
Domain: source.Domain,
|
|
|
|
Icon: source.Icon,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
}).Trace("database: got anthrove user")
|
|
|
|
|
|
|
|
return anthroveUser, nil
|
|
|
|
}
|
2024-06-14 13:09:11 +00:00
|
|
|
|
|
|
|
func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]models.AnthroveUserID, error) {
|
|
|
|
var users []pgModels.User
|
|
|
|
var userIDs []models.AnthroveUserID
|
|
|
|
|
|
|
|
err := db.WithContext(ctx).Model(&pgModels.User{}).Find(&users).Error
|
|
|
|
if err != nil {
|
|
|
|
log.Error("database: failed to get all anthrove user IDs")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range users {
|
|
|
|
userIDs = append(userIDs, models.AnthroveUserID(user.ID))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id_count": len(userIDs),
|
|
|
|
}).Trace("database: got all anthrove user IDs")
|
|
|
|
|
|
|
|
return userIDs, nil
|
|
|
|
}
|
2024-06-14 13:11:13 +00:00
|
|
|
|
|
|
|
func GetUserFavoriteNodeWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, skip int, limit int) (*graphModels.FavoriteList, error) {
|
|
|
|
var userFavorites []pgModels.UserFavorite
|
|
|
|
var favoritePosts []graphModels.FavoritePost
|
|
|
|
|
|
|
|
err := db.WithContext(ctx).Model(&pgModels.UserFavorite{}).Where("user_id = ?", string(anthroveUserID)).Offset(skip).Limit(limit).Find(&userFavorites).Error
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
"skip": skip,
|
|
|
|
"limit": limit,
|
|
|
|
}).Error("database: failed to get user favorites with pagination")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, userFavorite := range userFavorites {
|
|
|
|
var post pgModels.Post
|
|
|
|
err = db.WithContext(ctx).Model(&pgModels.Post{}).Where("id = ?", userFavorite.PostID).First(&post).Error
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"post_id": userFavorite.PostID,
|
|
|
|
}).Error("database: failed to get post")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
favoritePosts = append(favoritePosts, graphModels.FavoritePost{
|
|
|
|
AnthrovePost: graphModels.AnthrovePost{
|
|
|
|
PostID: models.AnthrovePostID(post.ID),
|
|
|
|
Rating: post.Rating,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
"anthrove_user_fav_count": len(favoritePosts),
|
|
|
|
}).Trace("database: got all anthrove user favorites")
|
|
|
|
|
|
|
|
return &graphModels.FavoriteList{Posts: favoritePosts}, nil
|
|
|
|
}
|
2024-06-14 13:16:31 +00:00
|
|
|
|
|
|
|
func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) ([]graphModels.TagsWithFrequency, error) {
|
|
|
|
var userFavorites []pgModels.UserFavorite
|
|
|
|
err := db.WithContext(ctx).Where("user_id = ?", string(anthroveUserID)).Find(&userFavorites).Error
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
}).Error("database: failed to get user favorites")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-20 20:45:03 +00:00
|
|
|
tagFrequency := make(map[struct {
|
|
|
|
name string
|
|
|
|
typeName string
|
|
|
|
}]int)
|
2024-06-14 13:16:31 +00:00
|
|
|
for _, userFavorite := range userFavorites {
|
2024-06-20 20:45:03 +00:00
|
|
|
var post pgModels.Post
|
|
|
|
err = db.WithContext(ctx).Preload("Tags").First(&post, "id = ?", userFavorite.PostID).Error
|
2024-06-14 13:16:31 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"post_id": userFavorite.PostID,
|
|
|
|
}).Error("database: failed to get post tags")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-20 20:45:03 +00:00
|
|
|
for _, tag := range post.Tags {
|
|
|
|
tagFrequency[struct {
|
|
|
|
name string
|
|
|
|
typeName string
|
|
|
|
}{name: tag.Name, typeName: string(tag.Type)}]++
|
2024-06-14 13:16:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var tagsWithFrequency []graphModels.TagsWithFrequency
|
2024-06-20 20:45:03 +00:00
|
|
|
for data, frequency := range tagFrequency {
|
2024-06-14 13:16:31 +00:00
|
|
|
tagsWithFrequency = append(tagsWithFrequency, graphModels.TagsWithFrequency{
|
|
|
|
Frequency: int64(frequency),
|
|
|
|
Tags: graphModels.AnthroveTag{
|
2024-06-20 20:45:03 +00:00
|
|
|
Name: data.name,
|
|
|
|
Type: data.typeName,
|
2024-06-14 13:16:31 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
"tag_amount": len(tagsWithFrequency),
|
|
|
|
}).Trace("database: got user tag node with relation to faved posts")
|
|
|
|
|
|
|
|
return tagsWithFrequency, nil
|
|
|
|
}
|