296 lines
8.8 KiB
Go
296 lines
8.8 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
|
log "github.com/sirupsen/logrus"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) error {
|
|
|
|
if anthroveUserID == "" {
|
|
return fmt.Errorf("anthroveUserID cannot be empty")
|
|
}
|
|
|
|
user := models.User{
|
|
BaseModel: models.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
|
|
}
|
|
|
|
func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error {
|
|
|
|
if anthroveUserID == "" || username == "" || userID == "" {
|
|
return fmt.Errorf("anthroveUserID cannot be empty")
|
|
}
|
|
|
|
source := models.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 := models.UserSource{
|
|
User: models.User{BaseModel: models.BaseModel{ID: string(anthroveUserID)}},
|
|
SourceID: source.ID,
|
|
AccountUsername: username,
|
|
AccountID: userID,
|
|
}
|
|
|
|
if err := db.WithContext(ctx).FirstOrCreate(&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
|
|
}
|
|
|
|
func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (int64, error) {
|
|
if anthroveUserID == "" {
|
|
return 0, fmt.Errorf("anthroveUserID cannot be empty")
|
|
}
|
|
|
|
var count int64
|
|
err := db.WithContext(ctx).Model(&models.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
|
|
}
|
|
|
|
func GetUserSourceLinks(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) {
|
|
var userSources []models.UserSource
|
|
userSourceMap := make(map[string]models.UserSource)
|
|
|
|
err := db.WithContext(ctx).Model(&models.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 models.Source
|
|
err = db.WithContext(ctx).Model(&models.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] = models.UserSource{
|
|
UserID: userSource.AccountID,
|
|
AccountUsername: userSource.AccountUsername,
|
|
Source: models.Source{
|
|
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
|
|
}
|
|
|
|
func GetSpecifiedUserSourceLink(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceDisplayName string) (map[string]models.UserSource, error) {
|
|
if anthroveUserID == "" || sourceDisplayName == "" {
|
|
return nil, fmt.Errorf("anthroveUserID or sourceDisplayName is empty")
|
|
}
|
|
|
|
var userSources []models.UserSource
|
|
userSourceMap := make(map[string]models.UserSource)
|
|
|
|
err := db.WithContext(ctx).Model(&models.UserSource{}).InnerJoins("Source", db.Where("display_name = ?", sourceDisplayName)).Where("user_id = ?", string(anthroveUserID)).First(&userSources).Error
|
|
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 models.Source
|
|
err = db.WithContext(ctx).Model(&models.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] = models.UserSource{
|
|
UserID: userSource.AccountID,
|
|
AccountUsername: userSource.AccountUsername,
|
|
Source: models.Source{
|
|
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
|
|
}
|
|
|
|
func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]models.AnthroveUserID, error) {
|
|
var users []models.User
|
|
var userIDs []models.AnthroveUserID
|
|
|
|
err := db.WithContext(ctx).Model(&models.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
|
|
}
|
|
|
|
func GetUserFavoriteNodeWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) {
|
|
var userFavorites []models.UserFavorite
|
|
var favoritePosts []models.Post
|
|
|
|
err := db.WithContext(ctx).Model(&models.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 models.Post
|
|
err = db.WithContext(ctx).Model(&models.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,
|
|
models.Post{
|
|
BaseModel: models.BaseModel{ID: 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 &models.FavoriteList{Posts: favoritePosts}, nil
|
|
}
|
|
|
|
func GetUserTagNodeWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
|
|
var userFavorites []models.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
|
|
}
|
|
|
|
tagFrequency := make(map[struct {
|
|
name string
|
|
typeName string
|
|
}]int)
|
|
for _, userFavorite := range userFavorites {
|
|
var post models.Post
|
|
err = db.WithContext(ctx).Preload("Tags").First(&post, "id = ?", userFavorite.PostID).Error
|
|
if err != nil {
|
|
log.WithFields(log.Fields{
|
|
"post_id": userFavorite.PostID,
|
|
}).Error("database: failed to get post tags")
|
|
return nil, err
|
|
}
|
|
|
|
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: data.name,
|
|
Type: models.TagType(data.typeName),
|
|
},
|
|
})
|
|
}
|
|
|
|
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
|
|
}
|