2024-07-15 16:02:28 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-10-30 16:15:25 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/authkit/models"
|
2024-07-15 16:02:28 +00:00
|
|
|
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-10-30 15:26:13 +00:00
|
|
|
func ListAllRelationship(user uint) ([]models.AccountRelationship, error) {
|
2024-07-15 16:02:28 +00:00
|
|
|
var relationships []models.AccountRelationship
|
|
|
|
if err := database.C.
|
2024-10-30 15:26:13 +00:00
|
|
|
Where("account_id = ?", user).
|
2024-07-15 16:02:28 +00:00
|
|
|
Preload("Account").
|
|
|
|
Preload("Related").
|
|
|
|
Find(&relationships).Error; err != nil {
|
|
|
|
return relationships, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return relationships, nil
|
|
|
|
}
|
|
|
|
|
2024-10-30 15:26:13 +00:00
|
|
|
func ListRelationshipWithFilter(user uint, status models.RelationshipStatus) ([]models.AccountRelationship, error) {
|
2024-07-15 16:02:28 +00:00
|
|
|
var relationships []models.AccountRelationship
|
|
|
|
if err := database.C.
|
2024-10-30 15:26:13 +00:00
|
|
|
Where("account_id = ? AND status = ?", user, status).
|
2024-07-15 16:02:28 +00:00
|
|
|
Preload("Account").
|
|
|
|
Preload("Related").
|
|
|
|
Find(&relationships).Error; err != nil {
|
|
|
|
return relationships, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return relationships, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRelationship(otherId uint) (models.AccountRelationship, error) {
|
|
|
|
var relationship models.AccountRelationship
|
|
|
|
if err := database.C.
|
|
|
|
Where(&models.AccountRelationship{AccountID: otherId}).
|
|
|
|
Preload("Account").
|
|
|
|
Preload("Related").
|
|
|
|
First(&relationship).Error; err != nil {
|
|
|
|
return relationship, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return relationship, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRelationWithTwoNode(userId, relatedId uint, noPreload ...bool) (models.AccountRelationship, 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.AccountRelationship
|
|
|
|
if err := tx.
|
|
|
|
Where(&models.AccountRelationship{AccountID: userId, RelatedID: relatedId}).
|
|
|
|
First(&relationship).Error; err != nil {
|
|
|
|
return relationship, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return relationship, nil
|
|
|
|
}
|
|
|
|
|
2024-07-23 15:50:05 +00:00
|
|
|
func EditRelationship(relationship models.AccountRelationship) (models.AccountRelationship, error) {
|
|
|
|
if err := database.C.Save(&relationship).Error; err != nil {
|
|
|
|
return relationship, err
|
|
|
|
}
|
|
|
|
return relationship, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteRelationship(relationship models.AccountRelationship) error {
|
|
|
|
if err := database.C.Delete(&relationship).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-26 14:09:39 +00:00
|
|
|
func NewBlockship(userA models.Account, userB models.Account) (models.AccountRelationship, error) {
|
|
|
|
var err error
|
|
|
|
var rel models.AccountRelationship
|
|
|
|
if rel, err = GetRelationWithTwoNode(userA.ID, userB.ID, true); err == nil {
|
|
|
|
rel.Status = models.RelationshipBlocked
|
|
|
|
} else {
|
|
|
|
rel = models.AccountRelationship{
|
|
|
|
AccountID: userA.ID,
|
|
|
|
RelatedID: userB.ID,
|
|
|
|
Status: models.RelationshipBlocked,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := database.C.Save(&rel).Error; err != nil {
|
|
|
|
return rel, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rel, nil
|
|
|
|
}
|
|
|
|
|
2024-07-15 16:02:28 +00:00
|
|
|
func NewFriend(userA models.Account, userB models.Account, skipPending ...bool) (models.AccountRelationship, error) {
|
|
|
|
relA := models.AccountRelationship{
|
|
|
|
AccountID: userA.ID,
|
|
|
|
RelatedID: userB.ID,
|
2024-07-23 15:50:05 +00:00
|
|
|
Status: models.RelationshipWaiting,
|
2024-07-15 16:02:28 +00:00
|
|
|
}
|
|
|
|
relB := models.AccountRelationship{
|
|
|
|
AccountID: userB.ID,
|
|
|
|
RelatedID: userA.ID,
|
|
|
|
Status: models.RelationshipPending,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(skipPending) > 0 && skipPending[0] {
|
2024-07-23 15:50:05 +00:00
|
|
|
relA.Status = models.RelationshipFriend
|
2024-07-15 16:02:28 +00:00
|
|
|
relB.Status = models.RelationshipFriend
|
|
|
|
}
|
|
|
|
|
|
|
|
if userA.ID == userB.ID {
|
2024-07-23 15:50:05 +00:00
|
|
|
return relA, fmt.Errorf("unable to make relationship with yourself")
|
2024-07-15 16:02:28 +00:00
|
|
|
} else if _, err := GetRelationWithTwoNode(userA.ID, userB.ID, true); err == nil || !errors.Is(err, gorm.ErrRecordNotFound) {
|
2024-07-23 15:50:05 +00:00
|
|
|
return relA, fmt.Errorf("unable to recreate a relationship with that user")
|
2024-07-15 16:02:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := database.C.Save(&relA).Error; err != nil {
|
|
|
|
return relA, err
|
|
|
|
} else if err = database.C.Save(&relB).Error; err != nil {
|
|
|
|
return relA, err
|
|
|
|
} else {
|
|
|
|
_ = NewNotification(models.Notification{
|
2024-07-23 15:50:05 +00:00
|
|
|
Title: "New Friend Request",
|
2024-10-26 16:06:23 +00:00
|
|
|
Subtitle: fmt.Sprintf("New friend request from %s", userA.Name),
|
2024-07-23 15:50:05 +00:00
|
|
|
Body: fmt.Sprintf("You got a new friend request from %s. Go to your account page and decide how to deal it.", userA.Nick),
|
2024-09-17 06:50:05 +00:00
|
|
|
Account: userB,
|
2024-07-15 16:05:09 +00:00
|
|
|
AccountID: userB.ID,
|
2024-07-15 16:02:28 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return relA, nil
|
|
|
|
}
|
|
|
|
|
2024-07-23 15:50:05 +00:00
|
|
|
func HandleFriend(userA models.Account, userB models.Account, isAccept bool) error {
|
|
|
|
relA, err := GetRelationWithTwoNode(userA.ID, userB.ID, true)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("relationship was not found: %v", err)
|
|
|
|
} else if relA.Status != models.RelationshipPending {
|
|
|
|
return fmt.Errorf("relationship already handled")
|
2024-07-15 16:02:28 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 15:50:05 +00:00
|
|
|
if isAccept {
|
|
|
|
relA.Status = models.RelationshipFriend
|
|
|
|
} else {
|
|
|
|
relA.Status = models.RelationshipBlocked
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := database.C.Save(&relA).Error; err != nil {
|
2024-07-15 16:02:28 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-07-23 15:50:05 +00:00
|
|
|
|
|
|
|
relB, err := GetRelationWithTwoNode(userB.ID, userA.ID, true)
|
|
|
|
if err == nil && relB.Status == models.RelationshipWaiting {
|
|
|
|
relB.Status = models.RelationshipFriend
|
|
|
|
if err := database.C.Save(&relB).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = NewNotification(models.Notification{
|
|
|
|
Title: "Friend Request Processed",
|
2024-10-26 16:06:23 +00:00
|
|
|
Subtitle: fmt.Sprintf("Your friend request to %s has been processsed.", userA.Name),
|
2024-07-23 15:50:05 +00:00
|
|
|
Body: fmt.Sprintf("Your relationship status with %s has been updated, go check it out!", userA.Nick),
|
2024-09-17 06:50:05 +00:00
|
|
|
Account: userB,
|
2024-07-23 15:50:05 +00:00
|
|
|
AccountID: userB.ID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-15 16:02:28 +00:00
|
|
|
return nil
|
|
|
|
}
|