121 lines
3.6 KiB
Go
121 lines
3.6 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/graphModels"
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels"
|
|
log "github.com/sirupsen/logrus"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func CreateUserNodeWithSourceRelation(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID, sourceDomain string, userID string, username string) error {
|
|
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
|
|
}
|
|
|
|
func GetUserFavoritesCount(ctx context.Context, db *gorm.DB, anthroveUserID models.AnthroveUserID) (int64, error) {
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|