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-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"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/proto"
|
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-05-04 14:22:58 +00:00
|
|
|
func GetRealm(id uint) (models.Realm, error) {
|
|
|
|
var realm models.Realm
|
2024-06-22 09:29:53 +00:00
|
|
|
pc, err := gap.H.DiscoverServiceGRPC("Hydrogen.Passport")
|
|
|
|
if err != nil {
|
|
|
|
return realm, err
|
|
|
|
}
|
|
|
|
response, err := proto.NewRealmsClient(pc).GetRealm(context.Background(), &proto.RealmLookupRequest{
|
2024-05-04 14:22:58 +00:00
|
|
|
Id: lo.ToPtr(uint64(id)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return realm, err
|
2024-02-05 11:25:56 +00:00
|
|
|
}
|
2024-05-04 14:22:58 +00:00
|
|
|
return LinkRealm(response)
|
2024-02-05 11:25:56 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 14:22:58 +00:00
|
|
|
func GetRealmWithAlias(alias string) (models.Realm, error) {
|
|
|
|
var realm models.Realm
|
2024-06-22 09:29:53 +00:00
|
|
|
pc, err := gap.H.DiscoverServiceGRPC("Hydrogen.Passport")
|
|
|
|
if err != nil {
|
|
|
|
return realm, err
|
|
|
|
}
|
|
|
|
response, err := proto.NewRealmsClient(pc).GetRealm(context.Background(), &proto.RealmLookupRequest{
|
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-05-04 14:22:58 +00:00
|
|
|
func GetRealmMember(realmId uint, userId uint) (*proto.RealmMemberResponse, error) {
|
2024-06-22 09:29:53 +00:00
|
|
|
pc, err := gap.H.DiscoverServiceGRPC("Hydrogen.Passport")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response, err := proto.NewRealmsClient(pc).GetRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
|
2024-05-04 14:22:58 +00:00
|
|
|
RealmId: uint64(realmId),
|
|
|
|
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-05-04 14:22:58 +00:00
|
|
|
func ListRealmMember(realmId uint) ([]*proto.RealmMemberResponse, error) {
|
2024-06-22 09:29:53 +00:00
|
|
|
pc, err := gap.H.DiscoverServiceGRPC("Hydrogen.Passport")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response, err := proto.NewRealmsClient(pc).ListRealmMember(context.Background(), &proto.RealmMemberLookupRequest{
|
2024-05-04 14:22:58 +00:00
|
|
|
RealmId: uint64(realmId),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return response.Data, nil
|
2024-02-05 07:51:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-04 14:22:58 +00:00
|
|
|
func LinkRealm(info *proto.RealmResponse) (models.Realm, error) {
|
|
|
|
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
|
|
|
}
|