✨ WebSocket listen notification API
This commit is contained in:
@ -27,8 +27,24 @@ func (v *Server) NotifyUser(_ context.Context, in *proto.NotifyRequest) (*proto.
|
||||
}
|
||||
})
|
||||
|
||||
if err := services.NewNotification(client, user, in.Subject, in.Content, links, in.IsImportant); err != nil {
|
||||
return nil, err
|
||||
notification := models.Notification{
|
||||
Subject: in.GetSubject(),
|
||||
Content: in.GetContent(),
|
||||
Links: links,
|
||||
IsImportant: in.GetIsImportant(),
|
||||
ReadAt: nil,
|
||||
RecipientID: user.ID,
|
||||
SenderID: &client.ID,
|
||||
}
|
||||
|
||||
if in.GetRealtime() {
|
||||
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
|
||||
|
@ -1,6 +1,6 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.32.0
|
||||
// protoc-gen-go v1.33.0
|
||||
// protoc v4.25.3
|
||||
// source: notify.proto
|
||||
|
||||
@ -87,6 +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"`
|
||||
}
|
||||
|
||||
func (x *NotifyRequest) Reset() {
|
||||
@ -170,6 +171,13 @@ func (x *NotifyRequest) GetClientSecret() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *NotifyRequest) GetRealtime() bool {
|
||||
if x != nil {
|
||||
return x.Realtime
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type NotifyReply struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
@ -224,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, 0xf4, 0x01, 0x0a, 0x0d,
|
||||
0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x75, 0x72, 0x6c, 0x22, 0x90, 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,
|
||||
@ -240,15 +248,16 @@ 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, 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, 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,
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -21,6 +21,7 @@ message NotifyRequest {
|
||||
uint64 recipient_id = 5;
|
||||
string client_id = 6;
|
||||
string client_secret = 7;
|
||||
bool realtime = 8;
|
||||
}
|
||||
|
||||
message NotifyReply {
|
||||
|
@ -17,6 +17,9 @@ func authMiddleware(c *fiber.Ctx) error {
|
||||
tk := strings.Replace(header, "Bearer", "", 1)
|
||||
token = strings.TrimSpace(tk)
|
||||
}
|
||||
if query := c.Query("tk"); len(query) > 0 {
|
||||
token = strings.TrimSpace(query)
|
||||
}
|
||||
|
||||
c.Locals("token", token)
|
||||
|
||||
|
@ -31,7 +31,17 @@ func notifyUser(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
if err := services.NewNotification(client, user, data.Subject, data.Content, data.Links, data.IsImportant); err != nil {
|
||||
notification := models.Notification{
|
||||
Subject: data.Subject,
|
||||
Content: data.Subject,
|
||||
Links: data.Links,
|
||||
IsImportant: data.IsImportant,
|
||||
ReadAt: nil,
|
||||
RecipientID: user.ID,
|
||||
SenderID: &client.ID,
|
||||
}
|
||||
|
||||
if err := services.NewNotification(notification); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
|
32
pkg/server/notify_ws.go
Normal file
32
pkg/server/notify_ws.go
Normal file
@ -0,0 +1,32 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/identity/pkg/models"
|
||||
"git.solsynth.dev/hydrogen/identity/pkg/services"
|
||||
"github.com/gofiber/contrib/websocket"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func listenNotifications(c *websocket.Conn) {
|
||||
user := c.Locals("principal").(models.Account)
|
||||
|
||||
// Push connection
|
||||
services.WsConn[user.ID] = append(services.WsConn[user.ID], c)
|
||||
|
||||
// Event loop
|
||||
var err error
|
||||
for {
|
||||
message := services.WsNotifyQueue[user.ID]
|
||||
|
||||
if message != nil {
|
||||
if err = c.WriteMessage(1, message); err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Pop connection
|
||||
services.WsConn[user.ID] = lo.Filter(services.WsConn[user.ID], func(item *websocket.Conn, idx int) bool {
|
||||
return item != c
|
||||
})
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/gofiber/contrib/websocket"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@ -60,9 +61,14 @@ func NewServer() {
|
||||
{
|
||||
api.Get("/avatar/:avatarId", getAvatar)
|
||||
|
||||
api.Get("/notifications", authMiddleware, getNotifications)
|
||||
api.Put("/notifications/:notificationId/read", authMiddleware, markNotificationRead)
|
||||
api.Post("/notifications/subscribe", authMiddleware, addNotifySubscriber)
|
||||
notify := api.Group("/notifications").Name("Notifications API")
|
||||
{
|
||||
notify.Get("/", authMiddleware, getNotifications)
|
||||
notify.Put("/:notificationId/read", authMiddleware, markNotificationRead)
|
||||
notify.Post("/subscribe", authMiddleware, addNotifySubscriber)
|
||||
|
||||
notify.Get("/listen", authMiddleware, websocket.New(listenNotifications))
|
||||
}
|
||||
|
||||
me := api.Group("/users/me").Name("Myself Operations")
|
||||
{
|
||||
|
13
pkg/services/connections.go
Normal file
13
pkg/services/connections.go
Normal file
@ -0,0 +1,13 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/identity/pkg/models"
|
||||
"github.com/gofiber/contrib/websocket"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
@ -2,6 +2,7 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
|
||||
"firebase.google.com/go/messaging"
|
||||
"git.solsynth.dev/hydrogen/identity/pkg/database"
|
||||
@ -23,34 +24,27 @@ func AddNotifySubscriber(user models.Account, provider, device, ua string) (mode
|
||||
return subscriber, err
|
||||
}
|
||||
|
||||
func NewNotification(
|
||||
user models.ThirdClient,
|
||||
target models.Account,
|
||||
subject, content string,
|
||||
links []models.NotificationLink,
|
||||
important bool,
|
||||
) error {
|
||||
notification := models.Notification{
|
||||
Subject: subject,
|
||||
Content: content,
|
||||
Links: links,
|
||||
IsImportant: important,
|
||||
ReadAt: nil,
|
||||
SenderID: &user.ID,
|
||||
RecipientID: target.ID,
|
||||
}
|
||||
|
||||
func NewNotification(notification models.Notification) error {
|
||||
if err := database.C.Save(¬ification).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
go func() {
|
||||
err := PushNotification(notification)
|
||||
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)
|
||||
|
||||
var subscribers []models.NotificationSubscriber
|
||||
if err := database.C.Where(&models.NotificationSubscriber{
|
||||
AccountID: target.ID,
|
||||
AccountID: notification.RecipientID,
|
||||
}).Find(&subscribers).Error; err != nil {
|
||||
// I don't know why cannot get subscribers list, but whatever, the notifications has created
|
||||
log.Error().Err(err).Msg("Unexpected error occurred during the notification.")
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
|
||||
for _, subscriber := range subscribers {
|
||||
|
@ -1,7 +1,6 @@
|
||||
{
|
||||
"name": "views",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"name": "@hydrogen/identity",
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
|
BIN
pkg/views/public/favicon.png
Executable file → Normal file
BIN
pkg/views/public/favicon.png
Executable file → Normal file
Binary file not shown.
Before Width: | Height: | Size: 68 KiB After Width: | Height: | Size: 75 KiB |
@ -2,7 +2,6 @@ html,
|
||||
body,
|
||||
#app,
|
||||
.v-application {
|
||||
overflow: auto !important;
|
||||
font-family: "Roboto Sans", ui-sans-serif, system-ui, sans-serif;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user