✨ Friends api
This commit is contained in:
23
pkg/services/cleaner.go
Normal file
23
pkg/services/cleaner.go
Normal file
@ -0,0 +1,23 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/identity/pkg/database"
|
||||
"github.com/rs/zerolog/log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func DoAutoDatabaseCleanup() {
|
||||
deadline := time.Now().Add(60 * time.Minute)
|
||||
log.Debug().Time("deadline", deadline).Msg("Now cleaning up entire database...")
|
||||
|
||||
var count int64
|
||||
for _, model := range database.DatabaseAutoActionRange {
|
||||
tx := database.C.Unscoped().Delete(model, "deleted_at >= ?", deadline)
|
||||
if tx.Error != nil {
|
||||
log.Error().Err(tx.Error).Msg("An error occurred when running auth context cleanup...")
|
||||
}
|
||||
count += tx.RowsAffected
|
||||
}
|
||||
|
||||
log.Debug().Int64("affected", count).Msg("Clean up entire database accomplished.")
|
||||
}
|
89
pkg/services/friendships.go
Normal file
89
pkg/services/friendships.go
Normal file
@ -0,0 +1,89 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/identity/pkg/database"
|
||||
"git.solsynth.dev/hydrogen/identity/pkg/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func ListFriend(anyside models.Account, status models.FriendshipStatus) ([]models.AccountFriendship, error) {
|
||||
var relationships []models.AccountFriendship
|
||||
if err := database.C.
|
||||
Where(&models.AccountFriendship{Status: status}).
|
||||
Where(&models.AccountFriendship{AccountID: anyside.ID}).
|
||||
Or(&models.AccountFriendship{RelatedID: anyside.ID}).
|
||||
Find(&relationships).Error; err != nil {
|
||||
return relationships, err
|
||||
}
|
||||
|
||||
return relationships, nil
|
||||
}
|
||||
|
||||
func GetFriend(anysideId uint) (models.AccountFriendship, error) {
|
||||
var relationship models.AccountFriendship
|
||||
if err := database.C.
|
||||
Where(&models.AccountFriendship{AccountID: anysideId}).
|
||||
Or(&models.AccountFriendship{RelatedID: anysideId}).
|
||||
Preload("Account").
|
||||
Preload("Related").
|
||||
First(&relationship).Error; err != nil {
|
||||
return relationship, err
|
||||
}
|
||||
|
||||
return relationship, nil
|
||||
}
|
||||
|
||||
func GetFriendWithTwoSides(userId, relatedId uint, noPreload ...bool) (models.AccountFriendship, error) {
|
||||
var tx *gorm.DB
|
||||
if len(noPreload) > 0 && noPreload[0] {
|
||||
tx = database.C
|
||||
} else {
|
||||
tx = database.C.Preload("Account").Preload("Related")
|
||||
}
|
||||
|
||||
var relationship models.AccountFriendship
|
||||
if err := tx.
|
||||
Where(&models.AccountFriendship{AccountID: userId, RelatedID: relatedId}).
|
||||
Or(&models.AccountFriendship{RelatedID: userId, AccountID: relatedId}).
|
||||
First(&relationship).Error; err != nil {
|
||||
return relationship, err
|
||||
}
|
||||
|
||||
return relationship, nil
|
||||
}
|
||||
|
||||
func NewFriend(user models.Account, related models.Account, status models.FriendshipStatus) (models.AccountFriendship, error) {
|
||||
relationship := models.AccountFriendship{
|
||||
AccountID: user.ID,
|
||||
RelatedID: related.ID,
|
||||
Status: status,
|
||||
}
|
||||
|
||||
if user.ID == related.ID {
|
||||
return relationship, fmt.Errorf("you cannot make friendship with yourself")
|
||||
} else if _, err := GetFriendWithTwoSides(user.ID, related.ID, true); err == nil || !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return relationship, fmt.Errorf("you already have a friendship with him or her")
|
||||
}
|
||||
|
||||
if err := database.C.Save(&relationship).Error; err != nil {
|
||||
return relationship, err
|
||||
}
|
||||
|
||||
return relationship, nil
|
||||
}
|
||||
|
||||
func EditFriend(relationship models.AccountFriendship) (models.AccountFriendship, error) {
|
||||
if err := database.C.Save(&relationship).Error; err != nil {
|
||||
return relationship, err
|
||||
}
|
||||
return relationship, nil
|
||||
}
|
||||
|
||||
func DeleteFriend(relationship models.AccountFriendship) error {
|
||||
if err := database.C.Delete(&relationship).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
@ -27,7 +27,7 @@ func DoAutoSignoff() {
|
||||
duration := time.Duration(viper.GetInt64("security.auto_signoff_duration")) * time.Second
|
||||
divider := time.Now().Add(-duration)
|
||||
|
||||
log.Debug().Time("before", divider).Msg("Now auto signing off sessions...")
|
||||
log.Debug().Time("before", divider).Msg("Now signing off sessions...")
|
||||
|
||||
if tx := database.C.
|
||||
Where("last_grant_at < ?", divider).
|
||||
@ -39,7 +39,7 @@ func DoAutoSignoff() {
|
||||
}
|
||||
|
||||
func DoAutoAuthCleanup() {
|
||||
log.Debug().Msg("Now auto cleaning up cached auth context...")
|
||||
log.Debug().Msg("Now cleaning up cached auth context...")
|
||||
|
||||
count := 0
|
||||
err := database.B.Batch(func(tx *bbolt.Tx) error {
|
||||
|
Reference in New Issue
Block a user