365 lines
11 KiB
Go
365 lines
11 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors"
|
|
"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 &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
|
|
}
|
|
|
|
if len(anthroveUserID) != 25 {
|
|
return &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"}
|
|
}
|
|
|
|
user := models.User{
|
|
BaseModel: models.BaseModel[models.AnthroveUserID]{
|
|
ID: anthroveUserID,
|
|
},
|
|
}
|
|
|
|
result := db.WithContext(ctx).FirstOrCreate(&user)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
|
return &errors2.EntityAlreadyExists{}
|
|
}
|
|
return result.Error
|
|
}
|
|
|
|
if result.RowsAffected == 0 {
|
|
return &errors2.NoDataWritten{}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error {
|
|
|
|
if anthroveUserID == "" {
|
|
return &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
|
|
}
|
|
|
|
if len(anthroveUserID) != 25 {
|
|
return &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"}
|
|
}
|
|
|
|
if accountId == "" {
|
|
return &errors2.EntityValidationFailed{Reason: "accountID cannot be empty"}
|
|
}
|
|
|
|
if accountUsername == "" {
|
|
return &errors2.EntityValidationFailed{Reason: "accountUsername cannot be empty"}
|
|
}
|
|
|
|
err := CreateUser(ctx, db, anthroveUserID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
source := models.Source{
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: sourceID},
|
|
}
|
|
|
|
result := db.WithContext(ctx).Where(&source).First(&source)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return &errors2.NoDataFound{}
|
|
}
|
|
return result.Error
|
|
}
|
|
|
|
userSource := models.UserSource{
|
|
User: models.User{BaseModel: models.BaseModel[models.AnthroveUserID]{ID: anthroveUserID}},
|
|
SourceID: string(source.ID),
|
|
AccountUsername: accountUsername,
|
|
AccountID: accountId,
|
|
UserID: string(anthroveUserID),
|
|
}
|
|
|
|
result = db.WithContext(ctx).FirstOrCreate(&userSource)
|
|
if result.Error != nil {
|
|
return errors.Join(result.Error, &errors2.NoRelationCreated{})
|
|
}
|
|
|
|
if result.RowsAffected == 0 {
|
|
return &errors2.NoDataWritten{}
|
|
}
|
|
|
|
log.WithFields(log.Fields{
|
|
"anthrove_user_id": anthroveUserID,
|
|
"source_id": sourceID,
|
|
"account_username": accountUsername,
|
|
"account_id": accountId,
|
|
}).Trace("database: created user-source relationship")
|
|
|
|
return nil
|
|
}
|
|
|
|
func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (int64, error) {
|
|
var count int64
|
|
|
|
if anthroveUserID == "" {
|
|
return 0, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
|
|
}
|
|
|
|
if len(anthroveUserID) != 25 {
|
|
return 0, &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"}
|
|
}
|
|
|
|
result := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Count(&count)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return 0, &errors2.NoDataFound{}
|
|
}
|
|
return 0, result.Error
|
|
}
|
|
|
|
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)
|
|
|
|
if anthroveUserID == "" {
|
|
return nil, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
|
|
}
|
|
|
|
if len(anthroveUserID) != 25 {
|
|
return nil, &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"}
|
|
}
|
|
|
|
result := db.WithContext(ctx).Model(&models.UserSource{}).Where("user_id = ?", string(anthroveUserID)).Find(&userSources)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, &errors2.NoDataFound{}
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
|
|
for _, userSource := range userSources {
|
|
var source models.Source
|
|
result = db.WithContext(ctx).Model(&models.Source{}).Where("id = ?", userSource.SourceID).First(&source)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, &errors2.NoDataFound{}
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
|
|
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 GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (map[string]models.UserSource, error) {
|
|
var userSources []models.UserSource
|
|
userSourceMap := make(map[string]models.UserSource)
|
|
|
|
if anthroveUserID == "" {
|
|
return nil, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
|
|
}
|
|
|
|
if len(anthroveUserID) != 25 {
|
|
return nil, &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"}
|
|
}
|
|
|
|
if sourceID == "" {
|
|
return nil, &errors2.EntityValidationFailed{Reason: "sourceID cannot be empty"}
|
|
}
|
|
|
|
if len(sourceID) != 25 {
|
|
return nil, &errors2.EntityValidationFailed{Reason: "sourceID needs to be 25 characters long"}
|
|
}
|
|
|
|
result := db.WithContext(ctx).Model(&models.UserSource{}).InnerJoins("Source", db.Where("id = ?", sourceID)).Where("user_id = ?", string(anthroveUserID)).First(&userSources)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, &errors2.NoDataFound{}
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
|
|
for _, userSource := range userSources {
|
|
var source models.Source
|
|
result = db.WithContext(ctx).Model(&models.Source{}).Where("id = ?", userSource.SourceID).First(&source)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, &errors2.NoDataFound{}
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
|
|
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_id": sourceID,
|
|
}).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
|
|
|
|
result := db.WithContext(ctx).Model(&models.User{}).Find(&users)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, &errors2.NoDataFound{}
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
|
|
for _, user := range users {
|
|
userIDs = append(userIDs, user.ID)
|
|
}
|
|
|
|
log.WithFields(log.Fields{
|
|
"anthrove_user_id_count": len(userIDs),
|
|
}).Trace("database: got all anthrove user IDs")
|
|
|
|
return userIDs, nil
|
|
}
|
|
|
|
func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) {
|
|
var userFavorites []models.UserFavorites
|
|
var favoritePosts []models.Post
|
|
|
|
if anthroveUserID == "" {
|
|
return nil, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
|
|
}
|
|
|
|
if len(anthroveUserID) != 25 {
|
|
return nil, &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"}
|
|
}
|
|
|
|
err := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Offset(skip).Limit(limit).Find(&userFavorites).Error
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, &errors2.NoDataFound{}
|
|
}
|
|
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 {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, &errors2.NoDataFound{}
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
favoritePosts = append(favoritePosts,
|
|
models.Post{
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{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 GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
|
|
var userFavorites []models.UserFavorites
|
|
|
|
if anthroveUserID == "" {
|
|
return nil, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
|
|
}
|
|
|
|
if len(anthroveUserID) != 25 {
|
|
return nil, &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"}
|
|
}
|
|
|
|
result := db.WithContext(ctx).Where("user_id = ?", string(anthroveUserID)).Find(&userFavorites)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, &errors2.NoDataFound{}
|
|
}
|
|
return nil, result.Error
|
|
}
|
|
|
|
tagFrequency := make(map[struct {
|
|
name string
|
|
typeName string
|
|
}]int)
|
|
|
|
for _, userFavorite := range userFavorites {
|
|
var post models.Post
|
|
result = db.WithContext(ctx).Preload("Tags").First(&post, "id = ?", userFavorite.PostID)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return nil, &errors2.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: 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
|
|
}
|