2024-02-05 07:51:31 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-05-04 14:22:58 +00:00
|
|
|
"context"
|
2024-09-11 15:42:46 +00:00
|
|
|
|
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-09-17 08:53:43 +00:00
|
|
|
"github.com/spf13/viper"
|
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
|
|
|
|
}
|
2024-09-17 08:53:43 +00:00
|
|
|
prefix := viper.GetString("database.prefix")
|
|
|
|
rm, err := hyper.LinkRealm(database.C, prefix+"realms", response)
|
|
|
|
return models.Realm{BaseRealm: rm}, err
|
2024-06-22 17:18:12 +00:00
|
|
|
}
|
|
|
|
|
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-09-17 08:53:43 +00:00
|
|
|
prefix := viper.GetString("database.prefix")
|
|
|
|
rm, err := hyper.LinkRealm(database.C, prefix+"realms", response)
|
|
|
|
return models.Realm{BaseRealm: rm}, err
|
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-09-17 08:53:43 +00:00
|
|
|
RealmId: lo.ToPtr(uint64(realm.ID)),
|
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
|
|
|
}
|
|
|
|
}
|