2021-12-03 03:54:59 +00:00
|
|
|
package notification
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2022-10-10 03:27:52 +00:00
|
|
|
"git.selly.red/Selly-Modules/natsio"
|
2021-12-03 03:54:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
SendByTopic = "topic"
|
|
|
|
SendByUsers = "users"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
SubjectPushNotification = "push_notification"
|
|
|
|
SubjectGetNotification = "get_notification"
|
|
|
|
SubjectReadNotification = "read_notification"
|
|
|
|
SubjectCountUnreadNotification = "count_unread_notification"
|
2021-12-06 04:02:29 +00:00
|
|
|
SubjectSubscribeTopic = "subscribe_topic"
|
|
|
|
SubjectUnsubscribeTopic = "unsubscribe_topic"
|
2021-12-03 03:54:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client ...
|
|
|
|
type Client struct {
|
|
|
|
Config Config
|
|
|
|
natsServer natsio.Server
|
|
|
|
natsJetStream natsio.JetStream
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient ...
|
|
|
|
func NewClient(cfg Config) (*Client, error) {
|
|
|
|
if cfg.APIKey == "" {
|
|
|
|
return nil, errors.New("api key is required")
|
|
|
|
}
|
|
|
|
if cfg.Nats.URL == "" {
|
|
|
|
return nil, errors.New("nats url is required")
|
|
|
|
}
|
|
|
|
if err := natsio.Connect(cfg.Nats); err != nil {
|
|
|
|
return nil, fmt.Errorf("nats connect failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := &Client{
|
|
|
|
Config: cfg,
|
|
|
|
natsServer: natsio.GetServer(),
|
|
|
|
natsJetStream: natsio.GetJetStream(),
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2021-12-03 04:09:25 +00:00
|
|
|
// PushToUsers push notification to list user id
|
2021-12-03 03:54:59 +00:00
|
|
|
func (c *Client) PushToUsers(payload PushRequest) (requestID string, err error) {
|
|
|
|
p := pushRequest{
|
2021-12-03 04:58:55 +00:00
|
|
|
APIKey: c.Config.APIKey,
|
|
|
|
Title: payload.Title,
|
|
|
|
Body: payload.Body,
|
|
|
|
Data: payload.Data,
|
|
|
|
SendBy: SendByUsers,
|
|
|
|
Users: payload.Users,
|
|
|
|
Label: payload.Label,
|
|
|
|
Category: payload.Category,
|
2021-12-21 10:22:42 +00:00
|
|
|
Sound: payload.Sound,
|
2021-12-03 03:54:59 +00:00
|
|
|
}
|
|
|
|
msg, err := c.natsServer.Request(SubjectPushNotification, toBytes(p))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-12-03 09:45:23 +00:00
|
|
|
var res struct {
|
|
|
|
Data PushResponse `json:"data"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
2021-12-03 03:54:59 +00:00
|
|
|
if err := json.Unmarshal(msg.Data, &res); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if res.Error != "" {
|
|
|
|
return "", errors.New(res.Error)
|
|
|
|
}
|
2021-12-03 09:45:23 +00:00
|
|
|
return res.Data.RequestID, nil
|
2021-12-03 03:54:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 04:09:25 +00:00
|
|
|
// Query get list notification by user id
|
|
|
|
func (c *Client) Query(q Query) (ListNotificationResponse, error) {
|
|
|
|
p := query{
|
|
|
|
APIKey: c.Config.APIKey,
|
|
|
|
User: q.User,
|
|
|
|
Category: q.Category,
|
|
|
|
Page: q.Page,
|
|
|
|
Limit: q.Limit,
|
|
|
|
}
|
2021-12-03 04:25:50 +00:00
|
|
|
msg, err := c.natsServer.Request(SubjectGetNotification, toBytes(p))
|
2021-12-03 04:09:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return ListNotificationResponse{}, err
|
|
|
|
}
|
2021-12-03 09:45:23 +00:00
|
|
|
var res struct {
|
|
|
|
Data ListNotificationResponse `json:"data"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
2021-12-03 04:09:25 +00:00
|
|
|
if err := json.Unmarshal(msg.Data, &res); err != nil {
|
|
|
|
return ListNotificationResponse{}, err
|
|
|
|
}
|
2021-12-03 09:45:23 +00:00
|
|
|
if res.Error != "" {
|
|
|
|
return ListNotificationResponse{}, errors.New(res.Error)
|
|
|
|
}
|
|
|
|
return res.Data, nil
|
2021-12-03 04:09:25 +00:00
|
|
|
}
|
|
|
|
|
2021-12-03 04:25:50 +00:00
|
|
|
// CountUnread count total unread notification
|
|
|
|
func (c *Client) CountUnread(q CountUnread) (int64, error) {
|
|
|
|
p := countUnread{
|
|
|
|
APIKey: c.Config.APIKey,
|
|
|
|
User: q.User,
|
|
|
|
Category: q.Category,
|
|
|
|
}
|
|
|
|
msg, err := c.natsServer.Request(SubjectCountUnreadNotification, toBytes(p))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-12-03 09:45:23 +00:00
|
|
|
var res struct {
|
|
|
|
Data CountUnreadResponse `json:"data"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
2021-12-03 04:25:50 +00:00
|
|
|
if err := json.Unmarshal(msg.Data, &res); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-12-03 09:45:23 +00:00
|
|
|
if res.Error != "" {
|
|
|
|
return 0, errors.New(res.Error)
|
|
|
|
}
|
|
|
|
return res.Data.Total, nil
|
2021-12-03 04:25:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read mark notification as read
|
|
|
|
func (c *Client) Read(notificationID string) error {
|
|
|
|
p := read{
|
|
|
|
APIKey: c.Config.APIKey,
|
|
|
|
ID: notificationID,
|
|
|
|
}
|
|
|
|
msg, err := c.natsServer.Request(SubjectReadNotification, toBytes(p))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-06 04:02:29 +00:00
|
|
|
var res CommonError
|
|
|
|
if err := json.Unmarshal(msg.Data, &res); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if res.Error != "" {
|
|
|
|
err = errors.New(res.Error)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe tokens to topic
|
|
|
|
func (c *Client) Subscribe(topic string, tokens []string) error {
|
|
|
|
p := subscribe{
|
|
|
|
Subscribe: Subscribe{
|
|
|
|
Tokens: tokens,
|
|
|
|
Topic: topic,
|
|
|
|
},
|
|
|
|
APIKey: c.Config.APIKey,
|
|
|
|
}
|
|
|
|
msg, err := c.natsServer.Request(SubjectSubscribeTopic, toBytes(p))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var res CommonError
|
|
|
|
if err := json.Unmarshal(msg.Data, &res); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if res.Error != "" {
|
|
|
|
err = errors.New(res.Error)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unsubscribe tokens from topic
|
|
|
|
func (c *Client) Unsubscribe(topic string, tokens []string) error {
|
|
|
|
p := subscribe{
|
|
|
|
Subscribe: Subscribe{
|
|
|
|
Tokens: tokens,
|
|
|
|
Topic: topic,
|
|
|
|
},
|
|
|
|
APIKey: c.Config.APIKey,
|
|
|
|
}
|
|
|
|
msg, err := c.natsServer.Request(SubjectUnsubscribeTopic, toBytes(p))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var res CommonError
|
2021-12-03 04:25:50 +00:00
|
|
|
if err := json.Unmarshal(msg.Data, &res); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if res.Error != "" {
|
|
|
|
err = errors.New(res.Error)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-03 03:54:59 +00:00
|
|
|
func toBytes(data interface{}) []byte {
|
|
|
|
b, _ := json.Marshal(data)
|
|
|
|
return b
|
|
|
|
}
|