2024-06-14 12:48:23 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-25 19:55:43 +00:00
|
|
|
"errors"
|
2024-06-26 07:10:04 +00:00
|
|
|
|
2024-06-25 19:55:43 +00:00
|
|
|
errors2 "git.dragse.it/anthrove/otter-space-sdk/pkg/errors"
|
2024-06-14 12:48:23 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
func CreateUser(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) error {
|
2024-06-19 21:32:42 +00:00
|
|
|
|
2024-06-20 13:24:55 +00:00
|
|
|
if anthroveUserID == "" {
|
2024-06-25 19:55:43 +00:00
|
|
|
return &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(anthroveUserID) != 25 {
|
|
|
|
return &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"}
|
2024-06-20 13:24:55 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
user := models.User{
|
2024-06-25 07:09:18 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthroveUserID]{
|
|
|
|
ID: anthroveUserID,
|
2024-06-19 21:32:42 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-25 19:55:43 +00:00
|
|
|
result := db.WithContext(ctx).FirstOrCreate(&user)
|
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
|
|
|
return &errors2.EntityAlreadyExists{}
|
|
|
|
}
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2024-06-19 21:32:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
func CreateUserWithRelationToSource(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID, accountId string, accountUsername string) error {
|
2024-06-20 13:24:55 +00:00
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
if anthroveUserID == "" {
|
2024-06-25 19:55:43 +00:00
|
|
|
return &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(anthroveUserID) != 25 {
|
|
|
|
return &errors2.EntityValidationFailed{Reason: "anthroveUserID needs to be 25 characters long"}
|
2024-06-20 13:24:55 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
if accountId == "" {
|
2024-06-25 19:55:43 +00:00
|
|
|
return &errors2.EntityValidationFailed{Reason: "accountID cannot be empty"}
|
2024-06-23 20:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if accountUsername == "" {
|
2024-06-25 19:55:43 +00:00
|
|
|
return &errors2.EntityValidationFailed{Reason: "accountUsername cannot be empty"}
|
2024-06-23 20:35:46 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 23:18:23 +00:00
|
|
|
err := CreateUser(ctx, db, anthroveUserID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
source := models.Source{
|
2024-06-25 07:09:18 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthroveSourceID]{ID: sourceID},
|
2024-06-14 12:48:23 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 19:55:43 +00:00
|
|
|
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
|
2024-06-14 12:48:23 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
userSource := models.UserSource{
|
2024-06-25 07:09:18 +00:00
|
|
|
User: models.User{BaseModel: models.BaseModel[models.AnthroveUserID]{ID: anthroveUserID}},
|
|
|
|
SourceID: string(source.ID),
|
2024-06-23 20:35:46 +00:00
|
|
|
AccountUsername: accountUsername,
|
|
|
|
AccountID: accountId,
|
|
|
|
UserID: string(anthroveUserID),
|
2024-06-14 12:48:23 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 19:55:43 +00:00
|
|
|
result = db.WithContext(ctx).FirstOrCreate(&userSource)
|
|
|
|
if result.Error != nil {
|
|
|
|
return errors.Join(result.Error, &errors2.NoRelationCreated{})
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.RowsAffected == 0 {
|
|
|
|
return &errors2.NoDataWritten{}
|
2024-06-14 12:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
2024-06-23 20:35:46 +00:00
|
|
|
"source_id": sourceID,
|
|
|
|
"account_username": accountUsername,
|
|
|
|
"account_id": accountId,
|
2024-06-14 12:48:23 +00:00
|
|
|
}).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-25 19:55:43 +00:00
|
|
|
var count int64
|
|
|
|
|
2024-06-20 20:45:03 +00:00
|
|
|
if anthroveUserID == "" {
|
2024-06-25 19:55:43 +00:00
|
|
|
return 0, &errors2.EntityValidationFailed{Reason: "anthroveUserID cannot be empty"}
|
2024-06-20 20:45:03 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 19:55:43 +00:00
|
|
|
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
|
2024-06-14 12:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-06-22 21:23:23 +00:00
|
|
|
func GetUserSourceLinks(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (map[string]models.UserSource, error) {
|
2024-06-22 20:06:36 +00:00
|
|
|
var userSources []models.UserSource
|
2024-06-22 21:23:23 +00:00
|
|
|
userSourceMap := make(map[string]models.UserSource)
|
2024-06-14 13:03:34 +00:00
|
|
|
|
2024-06-25 19:55:43 +00:00
|
|
|
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
|
2024-06-14 13:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, userSource := range userSources {
|
2024-06-22 20:06:36 +00:00
|
|
|
var source models.Source
|
2024-06-25 19:55:43 +00:00
|
|
|
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
|
2024-06-14 13:03:34 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 21:23:23 +00:00
|
|
|
userSourceMap[source.DisplayName] = models.UserSource{
|
|
|
|
UserID: userSource.AccountID,
|
|
|
|
AccountUsername: userSource.AccountUsername,
|
|
|
|
Source: models.Source{
|
2024-06-14 13:03:34 +00:00
|
|
|
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
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
func GetUserSourceBySourceID(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceID models.AnthroveSourceID) (map[string]models.UserSource, error) {
|
2024-06-25 19:55:43 +00:00
|
|
|
var userSources []models.UserSource
|
|
|
|
userSourceMap := make(map[string]models.UserSource)
|
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
if anthroveUserID == "" {
|
2024-06-25 19:55:43 +00:00
|
|
|
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"}
|
2024-06-23 19:23:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if sourceID == "" {
|
2024-06-25 19:55:43 +00:00
|
|
|
return nil, &errors2.EntityValidationFailed{Reason: "sourceID cannot be empty"}
|
2024-06-20 20:45:03 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 19:55:43 +00:00
|
|
|
if len(sourceID) != 25 {
|
|
|
|
return nil, &errors2.EntityValidationFailed{Reason: "sourceID needs to be 25 characters long"}
|
|
|
|
}
|
2024-06-14 13:04:45 +00:00
|
|
|
|
2024-06-25 19:55:43 +00:00
|
|
|
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
|
2024-06-14 13:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, userSource := range userSources {
|
2024-06-22 20:06:36 +00:00
|
|
|
var source models.Source
|
2024-06-25 19:55:43 +00:00
|
|
|
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
|
2024-06-14 13:04:45 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 21:23:23 +00:00
|
|
|
userSourceMap[source.DisplayName] = models.UserSource{
|
|
|
|
UserID: userSource.AccountID,
|
|
|
|
AccountUsername: userSource.AccountUsername,
|
|
|
|
Source: models.Source{
|
2024-06-14 13:04:45 +00:00
|
|
|
DisplayName: source.DisplayName,
|
|
|
|
Domain: source.Domain,
|
|
|
|
Icon: source.Icon,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
2024-06-23 19:23:38 +00:00
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
"source_id": sourceID,
|
2024-06-14 13:04:45 +00:00
|
|
|
}).Trace("database: got specified user source link")
|
|
|
|
|
|
|
|
return userSourceMap, nil
|
|
|
|
}
|
2024-06-14 13:07:23 +00:00
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
func GetAllAnthroveUserIDs(ctx context.Context, db *gorm.DB) ([]models.AnthroveUserID, error) {
|
2024-06-22 20:06:36 +00:00
|
|
|
var users []models.User
|
2024-06-23 20:35:46 +00:00
|
|
|
var userIDs []models.AnthroveUserID
|
2024-06-14 13:09:11 +00:00
|
|
|
|
2024-06-25 19:55:43 +00:00
|
|
|
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
|
2024-06-14 13:09:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, user := range users {
|
2024-06-25 19:55:43 +00:00
|
|
|
userIDs = append(userIDs, user.ID)
|
2024-06-14 13:09:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
func GetUserFavoriteWithPagination(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, skip int, limit int) (*models.FavoriteList, error) {
|
2024-06-24 15:07:41 +00:00
|
|
|
var userFavorites []models.UserFavorites
|
2024-06-22 21:23:23 +00:00
|
|
|
var favoritePosts []models.Post
|
2024-06-14 13:11:13 +00:00
|
|
|
|
2024-06-25 19:55:43 +00:00
|
|
|
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"}
|
|
|
|
}
|
|
|
|
|
2024-06-24 15:07:41 +00:00
|
|
|
err := db.WithContext(ctx).Model(&models.UserFavorites{}).Where("user_id = ?", string(anthroveUserID)).Offset(skip).Limit(limit).Find(&userFavorites).Error
|
2024-06-14 13:11:13 +00:00
|
|
|
if err != nil {
|
2024-06-25 19:55:43 +00:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil, &errors2.NoDataFound{}
|
|
|
|
}
|
2024-06-14 13:11:13 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, userFavorite := range userFavorites {
|
2024-06-22 20:06:36 +00:00
|
|
|
var post models.Post
|
|
|
|
err = db.WithContext(ctx).Model(&models.Post{}).Where("id = ?", userFavorite.PostID).First(&post).Error
|
2024-06-14 13:11:13 +00:00
|
|
|
if err != nil {
|
2024-06-25 19:55:43 +00:00
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
return nil, &errors2.NoDataFound{}
|
|
|
|
}
|
2024-06-14 13:11:13 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-22 21:23:23 +00:00
|
|
|
favoritePosts = append(favoritePosts,
|
|
|
|
models.Post{
|
2024-06-25 07:09:18 +00:00
|
|
|
BaseModel: models.BaseModel[models.AnthrovePostID]{ID: post.ID},
|
2024-06-22 21:23:23 +00:00
|
|
|
Rating: post.Rating,
|
|
|
|
})
|
2024-06-14 13:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"anthrove_user_id": anthroveUserID,
|
|
|
|
"anthrove_user_fav_count": len(favoritePosts),
|
|
|
|
}).Trace("database: got all anthrove user favorites")
|
|
|
|
|
2024-06-22 21:23:23 +00:00
|
|
|
return &models.FavoriteList{Posts: favoritePosts}, nil
|
2024-06-14 13:11:13 +00:00
|
|
|
}
|
2024-06-14 13:16:31 +00:00
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
func GetUserTagWitRelationToFavedPosts(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) ([]models.TagsWithFrequency, error) {
|
2024-06-24 15:07:41 +00:00
|
|
|
var userFavorites []models.UserFavorites
|
2024-06-25 19:55:43 +00:00
|
|
|
|
|
|
|
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
|
2024-06-14 13:16:31 +00:00
|
|
|
}
|
|
|
|
|
2024-06-20 20:45:03 +00:00
|
|
|
tagFrequency := make(map[struct {
|
|
|
|
name string
|
|
|
|
typeName string
|
|
|
|
}]int)
|
2024-06-22 23:18:23 +00:00
|
|
|
|
2024-06-14 13:16:31 +00:00
|
|
|
for _, userFavorite := range userFavorites {
|
2024-06-22 20:06:36 +00:00
|
|
|
var post models.Post
|
2024-06-25 19:55:43 +00:00
|
|
|
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
|
2024-06-14 13:16:31 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 21:23:23 +00:00
|
|
|
var tagsWithFrequency []models.TagsWithFrequency
|
2024-06-20 20:45:03 +00:00
|
|
|
for data, frequency := range tagFrequency {
|
2024-06-22 21:23:23 +00:00
|
|
|
tagsWithFrequency = append(tagsWithFrequency, models.TagsWithFrequency{
|
2024-06-14 13:16:31 +00:00
|
|
|
Frequency: int64(frequency),
|
2024-06-22 21:23:23 +00:00
|
|
|
Tags: models.Tag{
|
2024-06-20 20:45:03 +00:00
|
|
|
Name: data.name,
|
2024-06-22 21:23:23 +00:00
|
|
|
Type: models.TagType(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
|
|
|
|
}
|