Messaging/pkg/internal/services/accounts.go

86 lines
2.3 KiB
Go
Raw Normal View History

2024-03-26 15:05:13 +00:00
package services
import (
"context"
2024-06-08 08:34:14 +00:00
"fmt"
"time"
2024-07-16 06:44:00 +00:00
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
2024-07-16 06:44:00 +00:00
jsoniter "github.com/json-iterator/go"
2024-07-20 08:02:16 +00:00
"github.com/samber/lo"
2024-03-26 15:05:13 +00:00
2024-07-16 06:44:00 +00:00
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
2024-03-26 15:05:13 +00:00
)
2024-07-16 06:44:00 +00:00
func CheckUserPerm(userId, otherId uint, key string, val any) error {
2024-04-06 15:22:27 +00:00
var user models.Account
if err := database.C.Where("id = ?", userId).First(&user).Error; err != nil {
2024-07-16 06:44:00 +00:00
return fmt.Errorf("account not found: %v", err)
2024-04-06 15:22:27 +00:00
}
2024-07-16 06:44:00 +00:00
var other models.Account
if err := database.C.Where("id = ?", otherId).First(&other).Error; err != nil {
return fmt.Errorf("other not found: %v", err)
2024-04-06 15:22:27 +00:00
}
2024-04-06 06:36:36 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
2024-07-16 06:44:00 +00:00
encodedData, _ := jsoniter.Marshal(val)
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
if err != nil {
2024-07-16 06:44:00 +00:00
return err
}
2024-07-16 06:44:00 +00:00
out, err := proto.NewAuthClient(pc).EnsureUserPermGranted(ctx, &proto.CheckUserPermRequest{
UserId: uint64(user.ExternalID),
OtherId: uint64(other.ExternalID),
Key: key,
Value: encodedData,
2024-04-06 06:36:36 +00:00
})
2024-07-16 06:44:00 +00:00
if err != nil {
return err
} else if !out.IsValid {
return fmt.Errorf("missing permission: %v", key)
}
return nil
2024-04-06 06:36:36 +00:00
}
2024-07-20 08:02:16 +00:00
func NotifyAccountMessager(user models.Account, notification *proto.NotifyRequest) error {
2024-03-26 15:05:13 +00:00
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
2024-07-16 06:44:00 +00:00
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
if err != nil {
return err
}
2024-07-16 06:44:00 +00:00
_, err = proto.NewNotifierClient(pc).NotifyUser(ctx, &proto.NotifyUserRequest{
UserId: uint64(user.ExternalID),
2024-07-20 08:02:16 +00:00
Notify: notification,
})
return err
}
2024-07-20 08:02:16 +00:00
func NotifyAccountMessagerBatch(users []models.Account, notification *proto.NotifyRequest) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
pc, err := gap.H.GetServiceGrpcConn(hyper.ServiceTypeAuthProvider)
if err != nil {
return err
}
_, err = proto.NewNotifierClient(pc).NotifyUserBatch(ctx, &proto.NotifyUserBatchRequest{
2024-07-20 08:02:16 +00:00
UserId: lo.Map(users, func(item models.Account, idx int) uint64 {
return uint64(item.ExternalID)
}),
Notify: notification,
2024-03-26 15:05:13 +00:00
})
return err
}