Messaging/pkg/internal/services/realms.go

115 lines
2.8 KiB
Go
Raw Normal View History

2024-05-04 16:39:59 +00:00
package services
import (
"context"
"errors"
"fmt"
2024-09-11 15:58:02 +00:00
"reflect"
2024-07-16 06:44:00 +00:00
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
2024-05-04 16:39:59 +00:00
"github.com/samber/lo"
"gorm.io/gorm"
)
func GetRealmWithExtID(id uint) (models.Realm, error) {
2024-05-04 16:39:59 +00:00
var realm models.Realm
2024-07-16 06:44:00 +00:00
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
if err != nil {
return realm, err
}
2024-07-16 06:44:00 +00:00
response, err := proto.NewRealmClient(pc).GetRealm(context.Background(), &proto.LookupRealmRequest{
2024-05-04 16:39:59 +00:00
Id: lo.ToPtr(uint64(id)),
})
if err != nil {
return realm, err
}
return LinkRealm(response)
}
func GetRealmWithAlias(alias string) (models.Realm, error) {
var realm models.Realm
2024-07-16 06:44:00 +00:00
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
if err != nil {
return realm, err
}
2024-07-16 06:44:00 +00:00
response, err := proto.NewRealmClient(pc).GetRealm(context.Background(), &proto.LookupRealmRequest{
2024-05-04 16:39:59 +00:00
Alias: &alias,
})
if err != nil {
return realm, err
}
return LinkRealm(response)
}
2024-07-16 06:44:00 +00:00
func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberInfo, error) {
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
if err != nil {
return nil, err
}
2024-07-16 06:44:00 +00:00
response, err := proto.NewRealmClient(pc).GetRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
2024-08-20 11:57:01 +00:00
RealmId: uint64(realmId),
2024-05-04 16:39:59 +00:00
UserId: lo.ToPtr(uint64(userId)),
})
if err != nil {
return nil, err
} else {
return response, nil
}
}
2024-07-16 06:44:00 +00:00
func ListRealmMember(realmId uint) ([]*proto.RealmMemberInfo, error) {
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
if err != nil {
return nil, err
}
2024-07-16 06:44:00 +00:00
response, err := proto.NewRealmClient(pc).ListRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
2024-05-04 16:39:59 +00:00
RealmId: uint64(realmId),
})
if err != nil {
return nil, err
} else {
return response.Data, nil
}
}
2024-07-16 06:44:00 +00:00
func LinkRealm(info *proto.RealmInfo) (models.Realm, error) {
2024-05-04 16:39:59 +00:00
var realm models.Realm
if info == nil {
return realm, fmt.Errorf("remote realm info was not found")
}
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-05-05 08:36:56 +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-05-04 16:39:59 +00:00
}