Passport/pkg/internal/grpc/realms.go

168 lines
4.7 KiB
Go
Raw Normal View History

2024-05-03 17:47:44 +00:00
package grpc
import (
"context"
"fmt"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
2024-05-03 17:47:44 +00:00
"github.com/samber/lo"
)
func (v *Server) ListCommunityRealm(ctx context.Context, empty *proto.ListRealmRequest) (*proto.ListRealmResponse, error) {
2024-05-03 17:47:44 +00:00
realms, err := services.ListCommunityRealm()
if err != nil {
return nil, err
}
return &proto.ListRealmResponse{
Data: lo.Map(realms, func(item models.Realm, index int) *proto.RealmInfo {
return &proto.RealmInfo{
2024-05-04 08:59:31 +00:00
Id: uint64(item.ID),
2024-05-03 17:47:44 +00:00
Alias: item.Alias,
Name: item.Name,
Description: item.Description,
IsPublic: item.IsPublic,
IsCommunity: item.IsCommunity,
}
}),
}, nil
}
func (v *Server) ListAvailableRealm(ctx context.Context, request *proto.LookupUserRealmRequest) (*proto.ListRealmResponse, error) {
2024-05-03 17:47:44 +00:00
account, err := services.GetAccount(uint(request.GetUserId()))
if err != nil {
return nil, fmt.Errorf("unable to find target account: %v", err)
}
realms, err := services.ListAvailableRealm(account)
if err != nil {
return nil, err
}
return &proto.ListRealmResponse{
Data: lo.Map(realms, func(item models.Realm, index int) *proto.RealmInfo {
return &proto.RealmInfo{
2024-05-04 08:59:31 +00:00
Id: uint64(item.ID),
2024-05-03 17:47:44 +00:00
Alias: item.Alias,
Name: item.Name,
Description: item.Description,
IsPublic: item.IsPublic,
IsCommunity: item.IsCommunity,
}
}),
}, nil
}
func (v *Server) ListOwnedRealm(ctx context.Context, request *proto.LookupUserRealmRequest) (*proto.ListRealmResponse, error) {
2024-05-03 17:47:44 +00:00
account, err := services.GetAccount(uint(request.GetUserId()))
if err != nil {
return nil, fmt.Errorf("unable to find target account: %v", err)
}
realms, err := services.ListOwnedRealm(account)
if err != nil {
return nil, err
}
return &proto.ListRealmResponse{
Data: lo.Map(realms, func(item models.Realm, index int) *proto.RealmInfo {
return &proto.RealmInfo{
2024-05-04 08:59:31 +00:00
Id: uint64(item.ID),
2024-05-03 17:47:44 +00:00
Alias: item.Alias,
Name: item.Name,
Description: item.Description,
IsPublic: item.IsPublic,
IsCommunity: item.IsCommunity,
}
}),
}, nil
}
func (v *Server) GetRealm(ctx context.Context, request *proto.LookupRealmRequest) (*proto.RealmInfo, error) {
2024-05-03 17:47:44 +00:00
var realm models.Realm
tx := database.C.Model(&models.Realm{})
if request.Id != nil {
tx = tx.Where("id = ?", request.GetId())
}
if request.Alias != nil {
tx = tx.Where("alias = ?", request.GetAlias())
}
if request.IsPublic != nil {
tx = tx.Where("is_public = ?", request.GetIsPublic())
}
if request.IsCommunity != nil {
tx = tx.Where("is_community = ?", request.GetIsCommunity())
}
if err := tx.First(&realm).Error; err != nil {
return nil, err
}
return &proto.RealmInfo{
2024-05-04 08:59:31 +00:00
Id: uint64(realm.ID),
2024-05-03 17:47:44 +00:00
Alias: realm.Alias,
Name: realm.Name,
Description: realm.Description,
IsPublic: realm.IsPublic,
IsCommunity: realm.IsCommunity,
}, nil
}
2024-05-04 02:53:24 +00:00
func (v *Server) ListRealmMember(ctx context.Context, request *proto.RealmMemberLookupRequest) (*proto.ListRealmMemberResponse, error) {
var members []models.RealmMember
tx := database.C.Where("realm_id = ?", request.GetRealmId())
if request.UserId != nil {
tx = tx.Where("account_id = ?", request.GetUserId())
}
if err := tx.Find(&members).Error; err != nil {
return nil, err
}
return &proto.ListRealmMemberResponse{
Data: lo.Map(members, func(item models.RealmMember, index int) *proto.MemberInfo {
return &proto.MemberInfo{
2024-05-04 02:53:24 +00:00
RealmId: uint64(item.RealmID),
UserId: uint64(item.AccountID),
PowerLevel: int32(item.PowerLevel),
}
}),
}, nil
}
func (v *Server) GetRealmMember(ctx context.Context, request *proto.RealmMemberLookupRequest) (*proto.MemberInfo, error) {
2024-05-04 02:53:24 +00:00
var member models.RealmMember
tx := database.C.Where("realm_id = ?", request.GetRealmId())
if request.UserId != nil {
tx = tx.Where("account_id = ?", request.GetUserId())
}
if err := tx.First(&member).Error; err != nil {
return nil, err
}
return &proto.MemberInfo{
2024-05-04 02:53:24 +00:00
RealmId: uint64(member.RealmID),
UserId: uint64(member.AccountID),
PowerLevel: int32(member.PowerLevel),
}, nil
}
func (v *Server) CheckRealmMemberPerm(ctx context.Context, request *proto.CheckRealmPermRequest) (*proto.CheckRealmPermResponse, error) {
var member models.RealmMember
tx := database.C.
Where("realm_id = ?", request.GetRealmId()).
Where("account_id = ?", request.GetUserId())
if err := tx.First(&member).Error; err != nil {
return &proto.CheckRealmPermResponse{
IsSuccess: false,
}, nil
}
return &proto.CheckRealmPermResponse{
IsSuccess: member.PowerLevel >= int(request.GetPowerLevel()),
}, nil
}