Support users related rpc calls

This commit is contained in:
2024-10-31 21:07:53 +08:00
parent 8ff2648e4c
commit 8326c716e3
17 changed files with 698 additions and 490 deletions

View File

@ -5,19 +5,12 @@ import (
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
"github.com/samber/lo"
"git.solsynth.dev/hypernet/passport/pkg/internal/services"
jsoniter "github.com/json-iterator/go"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
)
type authenticateServer struct {
proto.UnimplementedAuthServiceServer
}
func (v *App) Authenticate(_ context.Context, in *proto.AuthRequest) (*proto.AuthReply, error) {
ticket, perms, err := services.Authenticate(uint(in.GetSessionId()))
if err != nil {
@ -81,55 +74,3 @@ func (v *App) EnsureUserPermGranted(_ context.Context, in *proto.CheckUserPermRe
IsValid: valid,
}, nil
}
func (v *App) ListUserFriends(_ context.Context, in *proto.ListUserRelativeRequest) (*proto.ListUserRelativeResponse, error) {
tx := database.C.Preload("Account").Where("status = ?", models.RelationshipFriend)
if in.GetIsRelated() {
tx = tx.Where("related_id = ?", in.GetUserId())
} else {
tx = tx.Where("account_id = ?", in.GetUserId())
}
var data []models.AccountRelationship
if err := tx.Find(&data).Error; err != nil {
return nil, err
}
return &proto.ListUserRelativeResponse{
Data: lo.Map(data, func(item models.AccountRelationship, index int) *proto.UserInfo {
val := &proto.UserInfo{
Id: uint64(item.AccountID),
Name: item.Account.Name,
}
return val
}),
}, nil
}
func (v *App) ListUserBlocklist(_ context.Context, in *proto.ListUserRelativeRequest) (*proto.ListUserRelativeResponse, error) {
tx := database.C.Preload("Account").Where("status = ?", models.RelationshipBlocked)
if in.GetIsRelated() {
tx = tx.Where("related_id = ?", in.GetUserId())
} else {
tx = tx.Where("account_id = ?", in.GetUserId())
}
var data []models.AccountRelationship
if err := tx.Find(&data).Error; err != nil {
return nil, err
}
return &proto.ListUserRelativeResponse{
Data: lo.Map(data, func(item models.AccountRelationship, index int) *proto.UserInfo {
val := &proto.UserInfo{
Id: uint64(item.AccountID),
Name: item.Account.Name,
}
return val
}),
}, nil
}

View File

@ -16,9 +16,10 @@ import (
type App struct {
nroto.UnimplementedAuthServiceServer
nroto.UnimplementedDirectoryServiceServer
proto.UnimplementedNotifierServer
proto.UnimplementedRealmServer
proto.UnimplementedEventRecorderServer
nroto.UnimplementedUserServiceServer
proto.UnimplementedRealmServiceServer
proto.UnimplementedAuditServiceServer
proto.UnimplementedNotifyServiceServer
health.UnimplementedHealthServer
srv *grpc.Server
@ -30,9 +31,10 @@ func NewServer() *App {
}
nroto.RegisterAuthServiceServer(server.srv, server)
proto.RegisterNotifierServer(server.srv, server)
proto.RegisterRealmServer(server.srv, server)
proto.RegisterEventRecorderServer(server.srv, server)
nroto.RegisterUserServiceServer(server.srv, server)
proto.RegisterNotifyServiceServer(server.srv, server)
proto.RegisterRealmServiceServer(server.srv, server)
proto.RegisterAuditServiceServer(server.srv, server)
health.RegisterHealthServer(server.srv, server)
reflection.Register(server.srv)

66
pkg/internal/grpc/user.go Normal file
View File

@ -0,0 +1,66 @@
package grpc
import (
"context"
"fmt"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
"github.com/samber/lo"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (v *App) GetUser(ctx context.Context, request *proto.GetUserRequest) (*proto.UserInfo, error) {
var account models.Account
if err := database.C.Where("id = ?", uint(request.GetUserId())).First(&account).Error; err != nil {
return nil, status.Errorf(codes.NotFound, fmt.Sprintf("requested user with id %d was not found", request.GetUserId()))
}
return account.EncodeToUserInfo(), nil
}
func (v *App) ListUser(ctx context.Context, request *proto.ListUserRequest) (*proto.MultipleUserInfo, error) {
var accounts []models.Account
if err := database.C.
Where("id IN ?", lo.Map(request.GetUserId(), func(id uint64, _ int) interface{} { return id })).
Find(&accounts).Error; err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to list users: %v", err))
}
return &proto.MultipleUserInfo{
Data: lo.Map(request.GetUserId(), func(item uint64, index int) *proto.UserInfo {
val, ok := lo.Find(accounts, func(x models.Account) bool {
return uint(item) == x.ID
})
if !ok {
return nil
}
return val.EncodeToUserInfo()
}),
}, nil
}
func (v *App) ListUserRelative(ctx context.Context, request *proto.ListUserRelativeRequest) (*proto.ListUserRelativeResponse, error) {
tx := database.C.Preload("Account").Where("status = ?", request.GetStatus())
if request.GetIsRelated() {
tx = tx.Where("related_id = ?", request.GetUserId())
} else {
tx = tx.Where("account_id = ?", request.GetUserId())
}
var data []models.AccountRelationship
if err := tx.Find(&data).Error; err != nil {
return nil, err
}
return &proto.ListUserRelativeResponse{
Data: lo.Map(data, func(item models.AccountRelationship, index int) *proto.UserInfo {
val := &proto.UserInfo{
Id: uint64(item.AccountID),
Name: item.Account.Name,
}
return val
}),
}, nil
}