2024-05-03 17:32:44 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-11-30 18:04:57 +00:00
|
|
|
"errors"
|
2024-05-03 17:32:44 +00:00
|
|
|
"fmt"
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
2024-05-03 17:32:44 +00:00
|
|
|
"github.com/samber/lo"
|
2024-11-30 18:04:57 +00:00
|
|
|
"gorm.io/gorm"
|
2024-11-16 13:36:34 +00:00
|
|
|
"strconv"
|
2024-05-03 17:32:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ListCommunityRealm() ([]models.Realm, error) {
|
|
|
|
var realms []models.Realm
|
|
|
|
if err := database.C.Where(&models.Realm{
|
|
|
|
IsCommunity: true,
|
|
|
|
}).Find(&realms).Error; err != nil {
|
|
|
|
return realms, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return realms, nil
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
func ListOwnedRealm(user models.Account) ([]models.Realm, error) {
|
2024-05-03 17:32:44 +00:00
|
|
|
var realms []models.Realm
|
2024-10-30 16:17:53 +00:00
|
|
|
if err := database.C.Where(&models.Realm{AccountID: user.ID}).Find(&realms).Error; err != nil {
|
2024-05-03 17:32:44 +00:00
|
|
|
return realms, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return realms, nil
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
func ListAvailableRealm(user models.Account) ([]models.Realm, error) {
|
2024-05-03 17:32:44 +00:00
|
|
|
var realms []models.Realm
|
|
|
|
var members []models.RealmMember
|
|
|
|
if err := database.C.Where(&models.RealmMember{
|
2024-10-30 16:17:53 +00:00
|
|
|
AccountID: user.ID,
|
2024-05-03 17:32:44 +00:00
|
|
|
}).Find(&members).Error; err != nil {
|
|
|
|
return realms, err
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := lo.Map(members, func(item models.RealmMember, index int) uint {
|
|
|
|
return item.RealmID
|
|
|
|
})
|
|
|
|
|
|
|
|
if err := database.C.Where("id IN ?", idx).Find(&realms).Error; err != nil {
|
|
|
|
return realms, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return realms, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRealmWithAlias(alias string) (models.Realm, error) {
|
2024-11-16 13:36:34 +00:00
|
|
|
tx := database.C.Where("alias = ?", alias)
|
|
|
|
|
|
|
|
numericId, err := strconv.Atoi(alias)
|
|
|
|
if err == nil {
|
|
|
|
tx.Or("id = ?", numericId)
|
|
|
|
}
|
|
|
|
|
2024-05-03 17:32:44 +00:00
|
|
|
var realm models.Realm
|
2024-11-16 13:36:34 +00:00
|
|
|
if err := tx.First(&realm).Error; err != nil {
|
2024-05-03 17:32:44 +00:00
|
|
|
return realm, err
|
|
|
|
}
|
|
|
|
return realm, nil
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
func NewRealm(realm models.Realm, user models.Account) (models.Realm, error) {
|
2024-05-05 03:48:39 +00:00
|
|
|
realm.Members = []models.RealmMember{
|
2024-10-30 16:17:53 +00:00
|
|
|
{AccountID: user.ID, PowerLevel: 100},
|
2024-05-05 03:48:39 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 17:32:44 +00:00
|
|
|
err := database.C.Save(&realm).Error
|
|
|
|
return realm, err
|
|
|
|
}
|
|
|
|
|
2024-12-01 03:32:54 +00:00
|
|
|
func CountRealmMember(realmId uint) (int64, error) {
|
|
|
|
var count int64
|
|
|
|
if err := database.C.Where(&models.RealmMember{
|
|
|
|
RealmID: realmId,
|
|
|
|
}).Model(&models.RealmMember{}).Count(&count).Error; err != nil {
|
|
|
|
return 0, err
|
|
|
|
} else {
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ListRealmMember(realmId uint, take int, offset int) ([]models.RealmMember, error) {
|
2024-05-03 17:32:44 +00:00
|
|
|
var members []models.RealmMember
|
|
|
|
|
|
|
|
if err := database.C.
|
2024-12-01 03:32:54 +00:00
|
|
|
Limit(take).Offset(offset).
|
2024-05-03 17:32:44 +00:00
|
|
|
Where(&models.RealmMember{RealmID: realmId}).
|
|
|
|
Preload("Account").
|
|
|
|
Find(&members).Error; err != nil {
|
|
|
|
return members, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return members, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRealmMember(userId uint, realmId uint) (models.RealmMember, error) {
|
|
|
|
var member models.RealmMember
|
|
|
|
if err := database.C.Where(&models.RealmMember{
|
|
|
|
AccountID: userId,
|
|
|
|
RealmID: realmId,
|
|
|
|
}).Find(&member).Error; err != nil {
|
|
|
|
return member, err
|
|
|
|
}
|
|
|
|
return member, nil
|
|
|
|
}
|
|
|
|
|
2024-10-30 16:17:53 +00:00
|
|
|
func AddRealmMember(user models.Account, affected models.Account, target models.Realm) error {
|
2024-11-30 18:04:57 +00:00
|
|
|
var member models.RealmMember
|
|
|
|
if err := database.C.Where(&models.RealmMember{
|
|
|
|
AccountID: affected.ID,
|
|
|
|
RealmID: target.ID,
|
2024-12-01 04:17:49 +00:00
|
|
|
}).First(&member).Error; err == nil || !errors.Is(err, gorm.ErrRecordNotFound) {
|
2024-11-30 18:04:57 +00:00
|
|
|
return fmt.Errorf("the user is already in the realm")
|
|
|
|
}
|
|
|
|
|
2024-05-03 17:32:44 +00:00
|
|
|
if !target.IsPublic && !target.IsCommunity {
|
2024-10-30 16:17:53 +00:00
|
|
|
if member, err := GetRealmMember(user.ID, target.ID); err != nil {
|
2024-05-03 17:32:44 +00:00
|
|
|
return fmt.Errorf("only realm member can add people: %v", err)
|
|
|
|
} else if member.PowerLevel < 50 {
|
2024-11-30 17:26:33 +00:00
|
|
|
return fmt.Errorf("only realm moderator can add member")
|
2024-05-03 17:32:44 +00:00
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
rel, err := GetRelationWithTwoNode(affected.ID, user.ID)
|
2024-07-15 16:02:28 +00:00
|
|
|
if err != nil || HasPermNodeWithDefault(
|
|
|
|
rel.PermNodes,
|
|
|
|
"RealmAdd",
|
|
|
|
true,
|
|
|
|
rel.Status == models.RelationshipFriend,
|
|
|
|
) {
|
|
|
|
return fmt.Errorf("you unable to add this user to your realm")
|
2024-05-03 17:32:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-30 18:04:57 +00:00
|
|
|
member = models.RealmMember{
|
2024-05-03 17:32:44 +00:00
|
|
|
RealmID: target.ID,
|
|
|
|
AccountID: affected.ID,
|
|
|
|
}
|
|
|
|
err := database.C.Save(&member).Error
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-11-30 17:26:33 +00:00
|
|
|
func RemoveRealmMember(user models.Account, affected models.RealmMember, target models.Realm) error {
|
|
|
|
if user.ID != affected.AccountID {
|
2024-10-30 16:17:53 +00:00
|
|
|
if member, err := GetRealmMember(user.ID, target.ID); err != nil {
|
2024-05-03 17:32:44 +00:00
|
|
|
return fmt.Errorf("only realm member can remove other member: %v", err)
|
|
|
|
} else if member.PowerLevel < 50 {
|
2024-11-30 17:26:33 +00:00
|
|
|
return fmt.Errorf("only realm moderator can kick member")
|
2024-05-03 17:32:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-30 17:26:33 +00:00
|
|
|
return database.C.Delete(&affected).Error
|
2024-05-03 17:32:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func EditRealm(realm models.Realm) (models.Realm, error) {
|
|
|
|
err := database.C.Save(&realm).Error
|
|
|
|
return realm, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteRealm(realm models.Realm) error {
|
|
|
|
return database.C.Delete(&realm).Error
|
|
|
|
}
|