diff --git a/pkg/grpc/notify.go b/pkg/grpc/notify.go index c867f48..28d1a7f 100644 --- a/pkg/grpc/notify.go +++ b/pkg/grpc/notify.go @@ -32,12 +32,13 @@ func (v *Server) NotifyUser(_ context.Context, in *proto.NotifyRequest) (*proto. Content: in.GetContent(), Links: links, IsImportant: in.GetIsImportant(), + IsRealtime: in.GetIsRealtime(), ReadAt: nil, RecipientID: user.ID, SenderID: &client.ID, } - if in.GetRealtime() { + if in.GetIsRealtime() { if err := services.PushNotification(notification); err != nil { return nil, err } diff --git a/pkg/grpc/proto/notify.pb.go b/pkg/grpc/proto/notify.pb.go index c85c839..4fadd06 100644 --- a/pkg/grpc/proto/notify.pb.go +++ b/pkg/grpc/proto/notify.pb.go @@ -87,7 +87,7 @@ type NotifyRequest struct { RecipientId uint64 `protobuf:"varint,5,opt,name=recipient_id,json=recipientId,proto3" json:"recipient_id,omitempty"` ClientId string `protobuf:"bytes,6,opt,name=client_id,json=clientId,proto3" json:"client_id,omitempty"` ClientSecret string `protobuf:"bytes,7,opt,name=client_secret,json=clientSecret,proto3" json:"client_secret,omitempty"` - Realtime bool `protobuf:"varint,8,opt,name=realtime,proto3" json:"realtime,omitempty"` + IsRealtime bool `protobuf:"varint,8,opt,name=is_realtime,json=isRealtime,proto3" json:"is_realtime,omitempty"` } func (x *NotifyRequest) Reset() { @@ -171,9 +171,9 @@ func (x *NotifyRequest) GetClientSecret() string { return "" } -func (x *NotifyRequest) GetRealtime() bool { +func (x *NotifyRequest) GetIsRealtime() bool { if x != nil { - return x.Realtime + return x.IsRealtime } return false } @@ -232,7 +232,7 @@ var file_notify_proto_rawDesc = []byte{ 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x34, 0x0a, 0x0a, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x4c, 0x69, 0x6e, 0x6b, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x6c, 0x61, 0x62, 0x65, 0x6c, 0x12, 0x10, 0x0a, 0x03, 0x75, 0x72, 0x6c, - 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72, 0x6c, 0x22, 0x90, 0x02, 0x0a, 0x0d, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72, 0x6c, 0x22, 0x95, 0x02, 0x0a, 0x0d, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x73, 0x75, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x73, 0x75, 0x62, 0x6a, 0x65, 0x63, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, @@ -248,16 +248,17 @@ var file_notify_proto_rawDesc = []byte{ 0x01, 0x28, 0x09, 0x52, 0x08, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x49, 0x64, 0x12, 0x23, 0x0a, 0x0d, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x5f, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x53, 0x65, 0x63, 0x72, - 0x65, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x72, 0x65, 0x61, 0x6c, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x08, - 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x72, 0x65, 0x61, 0x6c, 0x74, 0x69, 0x6d, 0x65, 0x22, 0x26, - 0x0a, 0x0b, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x17, 0x0a, - 0x07, 0x69, 0x73, 0x5f, 0x73, 0x65, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x06, - 0x69, 0x73, 0x53, 0x65, 0x6e, 0x74, 0x32, 0x42, 0x0a, 0x06, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, - 0x12, 0x38, 0x0a, 0x0a, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x55, 0x73, 0x65, 0x72, 0x12, 0x14, - 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x52, 0x65, 0x71, - 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x4e, 0x6f, 0x74, - 0x69, 0x66, 0x79, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x3b, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x65, 0x74, 0x12, 0x1f, 0x0a, 0x0b, 0x69, 0x73, 0x5f, 0x72, 0x65, 0x61, 0x6c, 0x74, 0x69, 0x6d, + 0x65, 0x18, 0x08, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0a, 0x69, 0x73, 0x52, 0x65, 0x61, 0x6c, 0x74, + 0x69, 0x6d, 0x65, 0x22, 0x26, 0x0a, 0x0b, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x52, 0x65, 0x70, + 0x6c, 0x79, 0x12, 0x17, 0x0a, 0x07, 0x69, 0x73, 0x5f, 0x73, 0x65, 0x6e, 0x74, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x08, 0x52, 0x06, 0x69, 0x73, 0x53, 0x65, 0x6e, 0x74, 0x32, 0x42, 0x0a, 0x06, 0x4e, + 0x6f, 0x74, 0x69, 0x66, 0x79, 0x12, 0x38, 0x0a, 0x0a, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x55, + 0x73, 0x65, 0x72, 0x12, 0x14, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x4e, 0x6f, 0x74, 0x69, + 0x66, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x2e, 0x4e, 0x6f, 0x74, 0x69, 0x66, 0x79, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x22, 0x00, 0x42, + 0x09, 0x5a, 0x07, 0x2e, 0x3b, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, } var ( diff --git a/pkg/grpc/proto/notify.proto b/pkg/grpc/proto/notify.proto index 1c3566a..1b44e24 100644 --- a/pkg/grpc/proto/notify.proto +++ b/pkg/grpc/proto/notify.proto @@ -21,7 +21,7 @@ message NotifyRequest { uint64 recipient_id = 5; string client_id = 6; string client_secret = 7; - bool realtime = 8; + bool is_realtime = 8; } message NotifyReply { diff --git a/pkg/models/notifications.go b/pkg/models/notifications.go index 700d444..5daf4dd 100644 --- a/pkg/models/notifications.go +++ b/pkg/models/notifications.go @@ -12,6 +12,7 @@ type Notification struct { Content string `json:"content"` Links datatypes.JSONSlice[NotificationLink] `json:"links"` IsImportant bool `json:"is_important"` + IsRealtime bool `json:"is_realtime" gorm:"-"` ReadAt *time.Time `json:"read_at"` SenderID *uint `json:"sender_id"` RecipientID uint `json:"recipient_id"` diff --git a/pkg/server/notify_api.go b/pkg/server/notify_api.go index 15925eb..50650b6 100644 --- a/pkg/server/notify_api.go +++ b/pkg/server/notify_api.go @@ -14,6 +14,7 @@ func notifyUser(c *fiber.Ctx) error { Content string `json:"content" validate:"required,max=3072"` Links []models.NotificationLink `json:"links"` IsImportant bool `json:"is_important"` + IsRealtime bool `json:"is_realtime"` UserID uint `json:"user_id" validate:"required"` } @@ -33,16 +34,23 @@ func notifyUser(c *fiber.Ctx) error { notification := models.Notification{ Subject: data.Subject, - Content: data.Subject, + Content: data.Content, Links: data.Links, IsImportant: data.IsImportant, + IsRealtime: data.IsRealtime, ReadAt: nil, RecipientID: user.ID, SenderID: &client.ID, } - if err := services.NewNotification(notification); err != nil { - return fiber.NewError(fiber.StatusBadRequest, err.Error()) + if data.IsRealtime { + if err := services.PushNotification(notification); err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } + } else { + if err := services.NewNotification(notification); err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } } return c.SendStatus(fiber.StatusOK) diff --git a/pkg/server/notify_ws.go b/pkg/server/notify_ws.go index 82783e7..105f46d 100644 --- a/pkg/server/notify_ws.go +++ b/pkg/server/notify_ws.go @@ -16,12 +16,8 @@ func listenNotifications(c *websocket.Conn) { // Event loop var err error for { - message := services.WsNotifyQueue[user.ID] - - if message != nil { - if err = c.WriteMessage(1, message); err != nil { - break - } + if _, _, err = c.ReadMessage(); err != nil { + break } } diff --git a/pkg/services/connections.go b/pkg/services/connections.go index 520650f..bb18ec0 100644 --- a/pkg/services/connections.go +++ b/pkg/services/connections.go @@ -5,8 +5,12 @@ import ( "github.com/gofiber/contrib/websocket" ) +type WsPushRequest struct { + Payload []byte + RecipientID uint +} + var WsConn = make(map[uint][]*websocket.Conn) -var WsNotifyQueue = make(map[uint][]byte) func CheckOnline(user models.Account) bool { return len(WsConn[user.ID]) > 0 diff --git a/pkg/services/notifications.go b/pkg/services/notifications.go index c745960..ca4efb0 100644 --- a/pkg/services/notifications.go +++ b/pkg/services/notifications.go @@ -31,14 +31,19 @@ func NewNotification(notification models.Notification) error { go func() { err := PushNotification(notification) - log.Error().Err(err).Msg("Unexpected error occurred during the notification.") + if err != nil { + log.Error().Err(err).Msg("Unexpected error occurred during the notification.") + } }() return nil } func PushNotification(notification models.Notification) error { - WsNotifyQueue[notification.RecipientID], _ = jsoniter.Marshal(notification) + raw, _ := jsoniter.Marshal(notification) + for _, conn := range WsConn[notification.RecipientID] { + _ = conn.WriteMessage(1, raw) + } var subscribers []models.NotificationSubscriber if err := database.C.Where(&models.NotificationSubscriber{ diff --git a/pkg/views/src/components/NotificationList.vue b/pkg/views/src/components/NotificationList.vue index c5cf0c5..7f3cfd8 100644 --- a/pkg/views/src/components/NotificationList.vue +++ b/pkg/views/src/components/NotificationList.vue @@ -1,8 +1,8 @@