2024-07-15 16:02:28 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-10-15 17:01:39 +00:00
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
2024-10-13 04:46:14 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-07-19 15:35:15 +00:00
|
|
|
|
2024-07-17 06:04:55 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
|
|
|
"github.com/samber/lo"
|
2024-07-15 16:02:28 +00:00
|
|
|
|
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (v *Server) NotifyUser(_ context.Context, in *proto.NotifyUserRequest) (*proto.NotifyResponse, error) {
|
|
|
|
var err error
|
|
|
|
var user models.Account
|
|
|
|
if user, err = services.GetAccount(uint(in.GetUserId())); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to get account: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-10-15 17:01:39 +00:00
|
|
|
metadata := hyper.DecodeMap(in.GetNotify().GetMetadata())
|
2024-07-15 16:02:28 +00:00
|
|
|
|
|
|
|
notification := models.Notification{
|
|
|
|
Topic: in.GetNotify().GetTopic(),
|
|
|
|
Title: in.GetNotify().GetTitle(),
|
|
|
|
Subtitle: in.GetNotify().Subtitle,
|
|
|
|
Body: in.GetNotify().GetBody(),
|
|
|
|
Metadata: metadata,
|
2024-07-19 15:35:15 +00:00
|
|
|
Avatar: in.GetNotify().Avatar,
|
|
|
|
Picture: in.GetNotify().Picture,
|
2024-07-15 16:02:28 +00:00
|
|
|
IsRealtime: in.GetNotify().GetIsRealtime(),
|
|
|
|
IsForcePush: in.GetNotify().GetIsForcePush(),
|
2024-09-21 15:47:04 +00:00
|
|
|
Account: user,
|
2024-07-15 16:05:09 +00:00
|
|
|
AccountID: user.ID,
|
2024-07-15 16:02:28 +00:00
|
|
|
}
|
|
|
|
|
2024-10-13 04:46:14 +00:00
|
|
|
log.Debug().Str("topic", notification.Topic).Uint("uid", notification.AccountID).Msg("Notifying user...")
|
|
|
|
|
2024-07-15 16:02:28 +00:00
|
|
|
if notification.IsRealtime {
|
|
|
|
if err := services.PushNotification(notification); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := services.NewNotification(notification); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.NotifyResponse{
|
|
|
|
IsSuccess: true,
|
|
|
|
}, nil
|
|
|
|
}
|
2024-07-17 06:04:55 +00:00
|
|
|
|
|
|
|
func (v *Server) NotifyUserBatch(_ context.Context, in *proto.NotifyUserBatchRequest) (*proto.NotifyResponse, error) {
|
|
|
|
var err error
|
|
|
|
var users []models.Account
|
|
|
|
if users, err = services.GetAccountList(lo.Map(in.GetUserId(), func(item uint64, index int) uint {
|
|
|
|
return uint(item)
|
|
|
|
})); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to get account: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-10-15 17:01:39 +00:00
|
|
|
metadata := hyper.DecodeMap(in.GetNotify().GetMetadata())
|
2024-07-17 06:04:55 +00:00
|
|
|
|
2024-10-13 04:46:14 +00:00
|
|
|
var checklist = make(map[uint]bool, len(users))
|
2024-07-17 06:04:55 +00:00
|
|
|
var notifications []models.Notification
|
|
|
|
for _, user := range users {
|
2024-10-18 15:10:33 +00:00
|
|
|
if _, ok := checklist[user.ID]; ok {
|
2024-10-13 04:46:14 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-07-17 06:04:55 +00:00
|
|
|
notification := models.Notification{
|
|
|
|
Topic: in.GetNotify().GetTopic(),
|
|
|
|
Title: in.GetNotify().GetTitle(),
|
|
|
|
Subtitle: in.GetNotify().Subtitle,
|
|
|
|
Body: in.GetNotify().GetBody(),
|
|
|
|
Metadata: metadata,
|
2024-07-19 15:35:15 +00:00
|
|
|
Avatar: in.GetNotify().Avatar,
|
|
|
|
Picture: in.GetNotify().Picture,
|
2024-07-17 06:04:55 +00:00
|
|
|
IsRealtime: in.GetNotify().GetIsRealtime(),
|
|
|
|
IsForcePush: in.GetNotify().GetIsForcePush(),
|
2024-09-17 13:15:34 +00:00
|
|
|
Account: user,
|
2024-07-17 06:04:55 +00:00
|
|
|
AccountID: user.ID,
|
|
|
|
}
|
2024-10-13 04:46:14 +00:00
|
|
|
checklist[user.ID] = true
|
2024-07-17 06:04:55 +00:00
|
|
|
|
|
|
|
notifications = append(notifications, notification)
|
|
|
|
}
|
|
|
|
|
2024-10-13 04:46:14 +00:00
|
|
|
log.Debug().Str("topic", notifications[0].Topic).Any("uid", lo.Keys(checklist)).Msg("Notifying users...")
|
|
|
|
|
2024-07-17 06:04:55 +00:00
|
|
|
if in.GetNotify().GetIsRealtime() {
|
|
|
|
services.PushNotificationBatch(notifications)
|
|
|
|
} else {
|
|
|
|
if err := services.NewNotificationBatch(notifications); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.NotifyResponse{
|
|
|
|
IsSuccess: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Server) NotifyAllUser(_ context.Context, in *proto.NotifyRequest) (*proto.NotifyResponse, error) {
|
|
|
|
var users []models.Account
|
|
|
|
if err := database.C.Find(&users).Error; err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to get account: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-10-15 17:01:39 +00:00
|
|
|
metadata := hyper.DecodeMap(in.GetMetadata())
|
2024-07-17 06:04:55 +00:00
|
|
|
|
2024-10-13 04:46:14 +00:00
|
|
|
var checklist = make(map[uint]bool, len(users))
|
2024-07-17 06:04:55 +00:00
|
|
|
var notifications []models.Notification
|
|
|
|
for _, user := range users {
|
2024-10-13 04:46:14 +00:00
|
|
|
if checklist[user.ID] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-07-17 06:04:55 +00:00
|
|
|
notification := models.Notification{
|
|
|
|
Topic: in.GetTopic(),
|
|
|
|
Title: in.GetTitle(),
|
|
|
|
Subtitle: in.Subtitle,
|
|
|
|
Body: in.GetBody(),
|
|
|
|
Metadata: metadata,
|
2024-07-19 15:35:15 +00:00
|
|
|
Avatar: in.Avatar,
|
|
|
|
Picture: in.Picture,
|
2024-07-17 06:04:55 +00:00
|
|
|
IsRealtime: in.GetIsRealtime(),
|
|
|
|
IsForcePush: in.GetIsForcePush(),
|
2024-09-20 13:55:25 +00:00
|
|
|
Account: user,
|
2024-07-17 06:04:55 +00:00
|
|
|
AccountID: user.ID,
|
|
|
|
}
|
2024-10-13 04:46:14 +00:00
|
|
|
checklist[user.ID] = true
|
2024-07-17 06:04:55 +00:00
|
|
|
|
|
|
|
notifications = append(notifications, notification)
|
|
|
|
}
|
|
|
|
|
2024-10-13 04:46:14 +00:00
|
|
|
log.Debug().Str("topic", notifications[0].Topic).Any("uid", lo.Keys(checklist)).Msg("Notifying users...")
|
|
|
|
|
2024-07-17 06:04:55 +00:00
|
|
|
if in.GetIsRealtime() {
|
|
|
|
services.PushNotificationBatch(notifications)
|
|
|
|
} else {
|
|
|
|
if err := services.NewNotificationBatch(notifications); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.NotifyResponse{
|
|
|
|
IsSuccess: true,
|
|
|
|
}, nil
|
|
|
|
}
|