Passport/pkg/internal/grpc/notify.go

58 lines
1.5 KiB
Go
Raw Normal View History

package grpc
import (
"context"
2024-06-07 12:05:56 +00:00
jsoniter "github.com/json-iterator/go"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"git.solsynth.dev/hydrogen/passport/pkg/proto"
"github.com/samber/lo"
)
func (v *Server) NotifyUser(_ context.Context, in *proto.NotifyRequest) (*proto.NotifyReply, error) {
client, err := services.GetThirdClientWithSecret(in.GetClientId(), in.GetClientSecret())
if err != nil {
return nil, err
}
var user models.Account
if user, err = services.GetAccount(uint(in.GetRecipientId())); err != nil {
return nil, err
}
2024-06-07 12:05:56 +00:00
var metadata map[string]any
_ = jsoniter.Unmarshal(in.GetMetadata(), &metadata)
links := lo.Map(in.GetLinks(), func(item *proto.NotifyLink, index int) models.NotificationLink {
return models.NotificationLink{
Label: item.Label,
Url: item.Url,
}
})
notification := models.Notification{
2024-06-07 12:24:32 +00:00
Type: lo.Ternary(len(in.GetType()) > 0, in.GetType(), "common"),
Subject: in.GetSubject(),
Content: in.GetContent(),
2024-06-07 12:05:56 +00:00
Metadata: metadata,
Links: links,
2024-03-31 08:03:59 +00:00
IsRealtime: in.GetIsRealtime(),
2024-06-07 12:05:56 +00:00
IsForcePush: in.GetIsForcePush(),
RecipientID: user.ID,
SenderID: &client.ID,
}
2024-03-31 08:03:59 +00:00
if in.GetIsRealtime() {
if err := services.PushNotification(notification); err != nil {
return nil, err
}
} else {
if err := services.NewNotification(notification); err != nil {
return nil, err
}
}
return &proto.NotifyReply{IsSent: true}, nil
}