Messaging/pkg/internal/services/realms.go

79 lines
2.2 KiB
Go
Raw Normal View History

2024-05-04 16:39:59 +00:00
package services
import (
"context"
2024-09-11 15:58:02 +00:00
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"
2024-09-17 08:52:42 +00:00
"github.com/spf13/viper"
2024-05-04 16:39:59 +00:00
)
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
}
2024-09-17 08:52:42 +00:00
prefix := viper.GetString("database.prefix")
rm, err := hyper.LinkRealm(database.C, prefix+"realms", response)
return models.Realm{BaseRealm: rm}, err
2024-05-04 16:39:59 +00:00
}
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
}
2024-09-17 08:52:42 +00:00
prefix := viper.GetString("database.prefix")
rm, err := hyper.LinkRealm(database.C, prefix+"realms", response)
return models.Realm{BaseRealm: rm}, err
2024-05-04 16:39:59 +00:00
}
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-09-17 08:52:42 +00:00
RealmId: lo.ToPtr(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-09-17 08:52:42 +00:00
RealmId: lo.ToPtr(uint64(realmId)),
2024-05-04 16:39:59 +00:00
})
if err != nil {
return nil, err
} else {
return response.Data, nil
}
}