2024-03-26 15:05:13 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-08 08:34:14 +00:00
|
|
|
"fmt"
|
2024-07-17 06:10:04 +00:00
|
|
|
"time"
|
|
|
|
|
2024-07-16 06:44:00 +00:00
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
2024-06-22 10:05:41 +00:00
|
|
|
"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"
|
2024-06-22 10:05:41 +00:00
|
|
|
"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)
|
2024-06-22 10:05:41 +00:00
|
|
|
if err != nil {
|
2024-07-16 06:44:00 +00:00
|
|
|
return err
|
2024-06-22 10:05:41 +00:00
|
|
|
}
|
2024-07-16 06:44:00 +00:00
|
|
|
out, err := proto.NewAuthClient(pc).EnsureUserPermGranted(ctx, &proto.CheckUserPermRequest{
|
2024-09-11 15:54:18 +00:00
|
|
|
UserId: uint64(user.ID),
|
|
|
|
OtherId: uint64(other.ID),
|
2024-07-16 06:44:00 +00:00
|
|
|
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)
|
2024-06-22 10:05:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-16 06:44:00 +00:00
|
|
|
_, err = proto.NewNotifierClient(pc).NotifyUser(ctx, &proto.NotifyUserRequest{
|
2024-09-11 15:54:18 +00:00
|
|
|
UserId: uint64(user.ID),
|
2024-07-20 08:02:16 +00:00
|
|
|
Notify: notification,
|
2024-07-17 06:10:04 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-20 08:02:16 +00:00
|
|
|
func NotifyAccountMessagerBatch(users []models.Account, notification *proto.NotifyRequest) error {
|
2024-07-17 06:10:04 +00:00
|
|
|
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 {
|
2024-09-11 15:54:18 +00:00
|
|
|
return uint64(item.ID)
|
2024-07-20 08:02:16 +00:00
|
|
|
}),
|
|
|
|
Notify: notification,
|
2024-03-26 15:05:13 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|