2024-07-15 16:02:28 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-10-23 16:13:16 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
2024-10-13 04:46:14 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-07-19 15:35:15 +00:00
|
|
|
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
2024-07-17 06:04:55 +00:00
|
|
|
"github.com/samber/lo"
|
2024-07-15 16:02:28 +00:00
|
|
|
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/services"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/proto"
|
2024-07-15 16:02:28 +00:00
|
|
|
)
|
|
|
|
|
2024-10-27 04:50:07 +00:00
|
|
|
func (v *App) NotifyUser(_ context.Context, in *proto.NotifyUserRequest) (*proto.NotifyResponse, error) {
|
2024-07-15 16:02:28 +00:00
|
|
|
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-23 16:13:16 +00:00
|
|
|
metadata := nex.DecodeMap(in.GetNotify().GetMetadata())
|
2024-07-15 16:02:28 +00:00
|
|
|
|
|
|
|
notification := models.Notification{
|
2024-10-26 16:06:23 +00:00
|
|
|
Topic: in.GetNotify().GetTopic(),
|
|
|
|
Title: in.GetNotify().GetTitle(),
|
|
|
|
Subtitle: in.GetNotify().GetSubtitle(),
|
|
|
|
Body: in.GetNotify().GetBody(),
|
|
|
|
Metadata: metadata,
|
|
|
|
Priority: int(in.GetNotify().GetPriority()),
|
|
|
|
IsRealtime: in.GetNotify().GetIsRealtime(),
|
|
|
|
Account: user,
|
|
|
|
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
|
|
|
|
2024-10-27 04:50:07 +00:00
|
|
|
func (v *App) NotifyUserBatch(_ context.Context, in *proto.NotifyUserBatchRequest) (*proto.NotifyResponse, error) {
|
2024-07-17 06:04:55 +00:00
|
|
|
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-23 16:13:16 +00:00
|
|
|
metadata := nex.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{
|
2024-10-26 16:06:23 +00:00
|
|
|
Topic: in.GetNotify().GetTopic(),
|
|
|
|
Title: in.GetNotify().GetTitle(),
|
|
|
|
Subtitle: in.GetNotify().GetSubtitle(),
|
|
|
|
Body: in.GetNotify().GetBody(),
|
|
|
|
Metadata: metadata,
|
|
|
|
Priority: int(in.GetNotify().GetPriority()),
|
|
|
|
IsRealtime: in.GetNotify().GetIsRealtime(),
|
|
|
|
Account: user,
|
|
|
|
AccountID: user.ID,
|
2024-07-17 06:04:55 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-27 04:50:07 +00:00
|
|
|
func (v *App) NotifyAllUser(_ context.Context, in *proto.NotifyRequest) (*proto.NotifyResponse, error) {
|
2024-07-17 06:04:55 +00:00
|
|
|
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-23 16:13:16 +00:00
|
|
|
metadata := nex.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{
|
2024-10-26 16:06:23 +00:00
|
|
|
Topic: in.GetTopic(),
|
|
|
|
Title: in.GetTitle(),
|
|
|
|
Subtitle: in.GetSubtitle(),
|
|
|
|
Body: in.GetBody(),
|
|
|
|
Metadata: metadata,
|
|
|
|
Priority: int(in.GetPriority()),
|
|
|
|
IsRealtime: in.GetIsRealtime(),
|
|
|
|
Account: user,
|
|
|
|
AccountID: user.ID,
|
2024-07-17 06:04:55 +00:00
|
|
|
}
|
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
|
|
|
|
}
|