2024-07-15 16:02:28 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
2024-10-31 13:26:25 +00:00
|
|
|
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
|
|
|
|
"github.com/goccy/go-json"
|
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-31 13:26:25 +00:00
|
|
|
var nty pushkit.Notification
|
|
|
|
if err = json.Unmarshal(in.GetNotify().GetData(), &nty); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to unmarshal notification: %v", err)
|
2024-07-15 16:02:28 +00:00
|
|
|
}
|
|
|
|
|
2024-10-31 13:26:25 +00:00
|
|
|
notification := models.NewNotificationFromPushkit(nty)
|
|
|
|
notification.Account = user
|
|
|
|
notification.AccountID = user.ID
|
|
|
|
|
2024-10-13 04:46:14 +00:00
|
|
|
log.Debug().Str("topic", notification.Topic).Uint("uid", notification.AccountID).Msg("Notifying user...")
|
|
|
|
|
2024-10-31 13:26:25 +00:00
|
|
|
if in.GetNotify().GetUnsaved() {
|
2024-07-15 16:02:28 +00:00
|
|
|
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-31 13:26:25 +00:00
|
|
|
var nty pushkit.Notification
|
|
|
|
if err = json.Unmarshal(in.GetNotify().GetData(), &nty); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to unmarshal notification: %v", err)
|
|
|
|
}
|
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-10-31 13:26:25 +00:00
|
|
|
notification := models.NewNotificationFromPushkit(nty)
|
|
|
|
notification.Account = user
|
|
|
|
notification.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-11-02 17:47:50 +00:00
|
|
|
if len(notifications) == 0 {
|
|
|
|
return &proto.NotifyResponse{
|
|
|
|
IsSuccess: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-10-13 04:46:14 +00:00
|
|
|
log.Debug().Str("topic", notifications[0].Topic).Any("uid", lo.Keys(checklist)).Msg("Notifying users...")
|
|
|
|
|
2024-10-31 13:26:25 +00:00
|
|
|
if in.GetNotify().GetUnsaved() {
|
2024-07-17 06:04:55 +00:00
|
|
|
services.PushNotificationBatch(notifications)
|
|
|
|
} else {
|
|
|
|
if err := services.NewNotificationBatch(notifications); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.NotifyResponse{
|
|
|
|
IsSuccess: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-10-31 14:40:10 +00:00
|
|
|
func (v *App) NotifyAllUser(_ context.Context, in *proto.NotifyInfoPayload) (*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-31 13:26:25 +00:00
|
|
|
var nty pushkit.Notification
|
|
|
|
if err := json.Unmarshal(in.GetData(), &nty); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to unmarshal notification: %v", err)
|
|
|
|
}
|
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-10-31 13:26:25 +00:00
|
|
|
notification := models.NewNotificationFromPushkit(nty)
|
|
|
|
notification.Account = user
|
|
|
|
notification.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-10-31 13:26:25 +00:00
|
|
|
if in.GetUnsaved() {
|
2024-07-17 06:04:55 +00:00
|
|
|
services.PushNotificationBatch(notifications)
|
|
|
|
} else {
|
|
|
|
if err := services.NewNotificationBatch(notifications); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.NotifyResponse{
|
|
|
|
IsSuccess: true,
|
|
|
|
}, nil
|
|
|
|
}
|