Messaging/pkg/internal/services/accounts.go

73 lines
1.9 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"
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-03-26 15:05:13 +00:00
"time"
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-16 06:44:00 +00:00
func NotifyAccountMessager(user models.Account, title, body string, subtitle *string, realtime bool, forcePush bool) 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.ID),
Notify: &proto.NotifyRequest{
Topic: "messaging.message",
Title: title,
Subtitle: subtitle,
Body: body,
IsRealtime: realtime,
IsForcePush: forcePush,
},
2024-03-26 15:05:13 +00:00
})
return err
}