2024-02-05 07:51:31 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-05-04 14:22:58 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2024-04-06 04:05:54 +00:00
|
|
|
"fmt"
|
2024-07-16 02:53:02 +00:00
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
2024-06-22 09:29:53 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
2024-02-11 05:12:37 +00:00
|
|
|
"github.com/samber/lo"
|
2024-05-04 14:22:58 +00:00
|
|
|
"gorm.io/gorm"
|
2024-05-05 08:35:41 +00:00
|
|
|
"reflect"
|
2024-02-05 07:51:31 +00:00
|
|
|
)
|
|
|
|
|
2024-06-22 17:18:12 +00:00
|
|
|
func GetRealmWithExtID(id uint) (models.Realm, error) {
|
|
|
|
var realm models.Realm
|
2024-07-16 02:53:02 +00:00
|
|
|
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
2024-06-22 17:18:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return realm, err
|
|
|
|
}
|
2024-07-16 02:53:02 +00:00
|
|
|
response, err := proto.NewRealmClient(pc).GetRealm(context.Background(), &proto.LookupRealmRequest{
|
2024-06-22 17:18:12 +00:00
|
|
|
Id: lo.ToPtr(uint64(id)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return realm, err
|
|
|
|
}
|
|
|
|
return LinkRealm(response)
|
|
|
|
}
|
|
|
|
|
2024-05-04 14:22:58 +00:00
|
|
|
func GetRealmWithAlias(alias string) (models.Realm, error) {
|
|
|
|
var realm models.Realm
|
2024-07-16 02:53:02 +00:00
|
|
|
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
2024-06-22 09:29:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return realm, err
|
|
|
|
}
|
2024-07-16 02:53:02 +00:00
|
|
|
response, err := proto.NewRealmClient(pc).GetRealm(context.Background(), &proto.LookupRealmRequest{
|
2024-05-04 14:22:58 +00:00
|
|
|
Alias: &alias,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return realm, err
|
2024-02-05 07:51:31 +00:00
|
|
|
}
|
2024-05-04 14:22:58 +00:00
|
|
|
return LinkRealm(response)
|
2024-02-05 07:51:31 +00:00
|
|
|
}
|
|
|
|
|
2024-07-16 02:53:02 +00:00
|
|
|
func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberInfo, error) {
|
2024-06-22 17:07:03 +00:00
|
|
|
var realm models.Realm
|
|
|
|
if err := database.C.Where("id = ?", realmId).First(&realm).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-16 02:53:02 +00:00
|
|
|
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
|
2024-06-22 09:29:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-16 02:53:02 +00:00
|
|
|
response, err := proto.NewRealmClient(pc).GetRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
|
2024-06-22 17:07:03 +00:00
|
|
|
RealmId: uint64(realm.ExternalID),
|
2024-05-04 14:22:58 +00:00
|
|
|
UserId: lo.ToPtr(uint64(userId)),
|
2024-02-11 05:12:37 +00:00
|
|
|
})
|
2024-05-04 14:22:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return response, nil
|
2024-02-11 05:12:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-16 02:53:02 +00:00
|
|
|
func LinkRealm(info *proto.RealmInfo) (models.Realm, error) {
|
2024-05-04 14:22:58 +00:00
|
|
|
var realm models.Realm
|
|
|
|
if info == nil {
|
|
|
|
return realm, fmt.Errorf("remote realm info was not found")
|
2024-03-23 05:05:05 +00:00
|
|
|
}
|
2024-05-04 14:22:58 +00:00
|
|
|
if err := database.C.Where(&models.Realm{
|
|
|
|
ExternalID: uint(info.Id),
|
|
|
|
}).First(&realm).Error; err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
realm = models.Realm{
|
|
|
|
Alias: info.Alias,
|
|
|
|
Name: info.Name,
|
|
|
|
Description: info.Description,
|
|
|
|
IsPublic: info.IsPublic,
|
|
|
|
IsCommunity: info.IsCommunity,
|
|
|
|
ExternalID: uint(info.Id),
|
|
|
|
}
|
|
|
|
return realm, database.C.Save(&realm).Error
|
|
|
|
}
|
|
|
|
return realm, err
|
2024-02-09 07:19:43 +00:00
|
|
|
}
|
2024-05-05 08:35:41 +00:00
|
|
|
|
|
|
|
prev := realm
|
|
|
|
realm.Alias = info.Alias
|
|
|
|
realm.Name = info.Name
|
|
|
|
realm.Description = info.Description
|
|
|
|
realm.IsPublic = info.IsPublic
|
|
|
|
realm.IsCommunity = info.IsCommunity
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if !reflect.DeepEqual(prev, realm) {
|
|
|
|
err = database.C.Save(&realm).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return realm, err
|
2024-02-05 07:51:31 +00:00
|
|
|
}
|