Merge pull request 'admin-push-notification-supplier' (#1) from admin-push-notification-supplier into master
Reviewed-on: #1
This commit is contained in:
commit
58c1ec0f8e
|
@ -0,0 +1,7 @@
|
||||||
|
package notification
|
||||||
|
|
||||||
|
const (
|
||||||
|
SortOrderDesc = "-order"
|
||||||
|
SortOrderAsc = "order"
|
||||||
|
SortDefault = "-lastPushAt"
|
||||||
|
)
|
|
@ -24,37 +24,37 @@ func main() {
|
||||||
|
|
||||||
userID := "61a499ad8d5770f8872b03d8"
|
userID := "61a499ad8d5770f8872b03d8"
|
||||||
requestID, err := c.PushToUsers(notification.PushRequest{
|
requestID, err := c.PushToUsers(notification.PushRequest{
|
||||||
Title: "Notification 1",
|
Title: "Notification campaign 111",
|
||||||
Body: "nats stream view notification",
|
Body: "Notification campaign 11",
|
||||||
Data: "{}",
|
Data: "{}",
|
||||||
Users: []string{userID},
|
Users: []string{userID},
|
||||||
Label: "tracking-label",
|
Label: "tracking-campaign",
|
||||||
Category: "order",
|
Category: "campaign",
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Push err: ", err)
|
fmt.Println("Push err: ", err)
|
||||||
}
|
}
|
||||||
fmt.Println("Request id: ", requestID)
|
fmt.Println("Request id: ", requestID)
|
||||||
|
|
||||||
total, err := c.CountUnread(notification.CountUnread{
|
unread, err := c.CountUnread(notification.CountUnread{
|
||||||
User: userID,
|
User: userID,
|
||||||
Category: "order",
|
Category: "", // count all if not specify category
|
||||||
})
|
})
|
||||||
fmt.Println("Count: ", total, err)
|
fmt.Println("Count: ", unread, err)
|
||||||
|
|
||||||
res, err := c.Query(notification.Query{
|
res, err := c.Query(notification.Query{
|
||||||
User: userID,
|
User: userID,
|
||||||
Category: "order",
|
|
||||||
Page: 0,
|
Page: 0,
|
||||||
Limit: 20,
|
Limit: 20,
|
||||||
|
Category: "", // get all if not specify category
|
||||||
})
|
})
|
||||||
fmt.Println("Query : ", res, err)
|
fmt.Println("Query : ", res, err)
|
||||||
|
|
||||||
total, err = c.CountUnread(notification.CountUnread{
|
unread, err = c.CountUnread(notification.CountUnread{
|
||||||
User: userID,
|
User: userID,
|
||||||
Category: "order",
|
Category: "",
|
||||||
})
|
})
|
||||||
fmt.Println("Count: 2", total, err)
|
fmt.Printf("Count: 2 %+v, %v\n", unread, err)
|
||||||
|
|
||||||
err = c.Subscribe("test", []string{
|
err = c.Subscribe("test", []string{
|
||||||
"eX1gEc7WokSHh-zJ3WR5Hn:APA91bFZDuzkjjFFL6TNpMg0ot93a0wsypWi4aCdm7M2x6AihgjS_QWsbKSFCT4hNhv_d8wKGy-DG6_3e8OlwPiWiJB4R33xLbbUekgxKcfCiiFooIC1E1OE3XWkvUtn4egn8aLG5jqv",
|
"eX1gEc7WokSHh-zJ3WR5Hn:APA91bFZDuzkjjFFL6TNpMg0ot93a0wsypWi4aCdm7M2x6AihgjS_QWsbKSFCT4hNhv_d8wKGy-DG6_3e8OlwPiWiJB4R33xLbbUekgxKcfCiiFooIC1E1OE3XWkvUtn4egn8aLG5jqv",
|
||||||
|
|
61
model.go
61
model.go
|
@ -10,14 +10,16 @@ type Config struct {
|
||||||
|
|
||||||
// PushRequest ...
|
// PushRequest ...
|
||||||
type PushRequest struct {
|
type PushRequest struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Body string `json:"body"`
|
Body string `json:"body"`
|
||||||
Data string `json:"data"`
|
Data string `json:"data"`
|
||||||
Users []string `json:"users"`
|
Users []string `json:"users"`
|
||||||
Label string `json:"label,omitempty"`
|
Label string `json:"label,omitempty"`
|
||||||
Category string `json:"category,omitempty"`
|
Category string `json:"category,omitempty"`
|
||||||
Sound *Sound `json:"sound,omitempty"`
|
Sound *Sound `json:"sound,omitempty"`
|
||||||
Link string `json:"link"` // for webpush
|
Link string `json:"link"` // for webpush
|
||||||
|
Order int `json:"order"` // for sort
|
||||||
|
DisplayUntil int64 `json:"displayUntil"` // time.Unix
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sound ...
|
// Sound ...
|
||||||
|
@ -32,34 +34,25 @@ type PushResponse struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type pushRequest struct {
|
type pushRequest struct {
|
||||||
Version string `json:"version"`
|
PushRequest
|
||||||
APIKey string `json:"apiKey"`
|
Version string `json:"version"`
|
||||||
Title string `json:"title"`
|
APIKey string `json:"apiKey"`
|
||||||
Body string `json:"body"`
|
SendBy string `json:"sendBy"`
|
||||||
Data string `json:"data"`
|
|
||||||
SendBy string `json:"sendBy"`
|
|
||||||
Users []string `json:"users"`
|
|
||||||
Topic string `json:"topic"`
|
|
||||||
Label string `json:"label"`
|
|
||||||
Category string `json:"category"`
|
|
||||||
Sound *Sound `json:"sound,omitempty"`
|
|
||||||
Link string `json:"link"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Query ...
|
// Query ...
|
||||||
type Query struct {
|
type Query struct {
|
||||||
User string `json:"user"`
|
User string `json:"user"`
|
||||||
Category string `json:"category,omitempty"`
|
Category string `json:"category,omitempty"`
|
||||||
|
Status string `json:"status"`
|
||||||
Page int64 `json:"page,omitempty"`
|
Page int64 `json:"page,omitempty"`
|
||||||
Limit int64 `json:"limit,omitempty"`
|
Limit int64 `json:"limit,omitempty"`
|
||||||
|
Sort string `json:"sort,omitempty"` // SortDefault, SortOrderDesc, SortOrderAsc
|
||||||
}
|
}
|
||||||
|
|
||||||
type query struct {
|
type query struct {
|
||||||
APIKey string `json:"apiKey"`
|
APIKey string `json:"apiKey"`
|
||||||
User string `json:"user"`
|
Query
|
||||||
Category string `json:"category,omitempty"`
|
|
||||||
Page int64 `json:"page,omitempty"`
|
|
||||||
Limit int64 `json:"limit,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notification ...
|
// Notification ...
|
||||||
|
@ -99,13 +92,17 @@ type CountUnread struct {
|
||||||
|
|
||||||
// CountUnreadResponse ...
|
// CountUnreadResponse ...
|
||||||
type CountUnreadResponse struct {
|
type CountUnreadResponse struct {
|
||||||
Total int64 `json:"total"`
|
List []CategoryCount `json:"list"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CategoryCount struct {
|
||||||
|
Category string `json:"category"`
|
||||||
|
TotalUnread int64 `json:"totalUnread"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type countUnread struct {
|
type countUnread struct {
|
||||||
APIKey string `json:"apiKey"`
|
CountUnread
|
||||||
User string `json:"user"`
|
APIKey string `json:"apiKey"`
|
||||||
Category string `json:"category"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommonError ...
|
// CommonError ...
|
||||||
|
@ -123,3 +120,9 @@ type subscribe struct {
|
||||||
Subscribe
|
Subscribe
|
||||||
APIKey string `json:"apiKey"`
|
APIKey string `json:"apiKey"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadAll ...
|
||||||
|
type ReadAll struct {
|
||||||
|
User string `json:"user"`
|
||||||
|
Category string `json:"category,omitempty"`
|
||||||
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// version specify current version of client
|
// version specify current version of client
|
||||||
const version = "1.0.2"
|
const version = "1.0.3"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
SendByTopic = "topic"
|
SendByTopic = "topic"
|
||||||
|
@ -20,6 +20,7 @@ const (
|
||||||
SubjectPushNotification = "push_notification"
|
SubjectPushNotification = "push_notification"
|
||||||
SubjectGetNotification = "get_notification"
|
SubjectGetNotification = "get_notification"
|
||||||
SubjectReadNotification = "read_notification"
|
SubjectReadNotification = "read_notification"
|
||||||
|
SubjectReadAllNotification = "read_all_notification"
|
||||||
SubjectCountUnreadNotification = "count_unread_notification"
|
SubjectCountUnreadNotification = "count_unread_notification"
|
||||||
SubjectSubscribeTopic = "subscribe_topic"
|
SubjectSubscribeTopic = "subscribe_topic"
|
||||||
SubjectUnsubscribeTopic = "unsubscribe_topic"
|
SubjectUnsubscribeTopic = "unsubscribe_topic"
|
||||||
|
@ -56,17 +57,10 @@ func NewClient(cfg Config) (*Client, error) {
|
||||||
// PushToUsers push notification to list user id
|
// PushToUsers push notification to list user id
|
||||||
func (c *Client) PushToUsers(payload PushRequest) (requestID string, err error) {
|
func (c *Client) PushToUsers(payload PushRequest) (requestID string, err error) {
|
||||||
p := pushRequest{
|
p := pushRequest{
|
||||||
Version: version,
|
Version: version,
|
||||||
APIKey: c.Config.APIKey,
|
APIKey: c.Config.APIKey,
|
||||||
Title: payload.Title,
|
SendBy: SendByUsers,
|
||||||
Body: payload.Body,
|
PushRequest: payload,
|
||||||
Data: payload.Data,
|
|
||||||
SendBy: SendByUsers,
|
|
||||||
Users: payload.Users,
|
|
||||||
Label: payload.Label,
|
|
||||||
Category: payload.Category,
|
|
||||||
Sound: payload.Sound,
|
|
||||||
Link: payload.Link,
|
|
||||||
}
|
}
|
||||||
msg, err := c.natsServer.Request(SubjectPushNotification, toBytes(p))
|
msg, err := c.natsServer.Request(SubjectPushNotification, toBytes(p))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -88,11 +82,8 @@ func (c *Client) PushToUsers(payload PushRequest) (requestID string, err error)
|
||||||
// Query get list notification by user id
|
// Query get list notification by user id
|
||||||
func (c *Client) Query(q Query) (ListNotificationResponse, error) {
|
func (c *Client) Query(q Query) (ListNotificationResponse, error) {
|
||||||
p := query{
|
p := query{
|
||||||
APIKey: c.Config.APIKey,
|
APIKey: c.Config.APIKey,
|
||||||
User: q.User,
|
Query: q,
|
||||||
Category: q.Category,
|
|
||||||
Page: q.Page,
|
|
||||||
Limit: q.Limit,
|
|
||||||
}
|
}
|
||||||
msg, err := c.natsServer.Request(SubjectGetNotification, toBytes(p))
|
msg, err := c.natsServer.Request(SubjectGetNotification, toBytes(p))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -112,27 +103,26 @@ func (c *Client) Query(q Query) (ListNotificationResponse, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CountUnread count total unread notification
|
// CountUnread count total unread notification
|
||||||
func (c *Client) CountUnread(q CountUnread) (int64, error) {
|
func (c *Client) CountUnread(q CountUnread) ([]CategoryCount, error) {
|
||||||
p := countUnread{
|
p := countUnread{
|
||||||
APIKey: c.Config.APIKey,
|
APIKey: c.Config.APIKey,
|
||||||
User: q.User,
|
CountUnread: q,
|
||||||
Category: q.Category,
|
|
||||||
}
|
}
|
||||||
msg, err := c.natsServer.Request(SubjectCountUnreadNotification, toBytes(p))
|
msg, err := c.natsServer.Request(SubjectCountUnreadNotification, toBytes(p))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var res struct {
|
var res struct {
|
||||||
Data CountUnreadResponse `json:"data"`
|
Data CountUnreadResponse `json:"data"`
|
||||||
Error string `json:"error"`
|
Error string `json:"error"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(msg.Data, &res); err != nil {
|
if err = json.Unmarshal(msg.Data, &res); err != nil {
|
||||||
return 0, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if res.Error != "" {
|
if res.Error != "" {
|
||||||
return 0, errors.New(res.Error)
|
return nil, errors.New(res.Error)
|
||||||
}
|
}
|
||||||
return res.Data.Total, nil
|
return res.Data.List, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read mark notification as read
|
// Read mark notification as read
|
||||||
|
@ -155,6 +145,29 @@ func (c *Client) Read(notificationID string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadAll mark notification as read
|
||||||
|
func (c *Client) ReadAll(r ReadAll) error {
|
||||||
|
p := struct {
|
||||||
|
ReadAll
|
||||||
|
APIKey string `json:"apiKey"`
|
||||||
|
}{
|
||||||
|
ReadAll: r,
|
||||||
|
APIKey: c.Config.APIKey,
|
||||||
|
}
|
||||||
|
msg, err := c.natsServer.Request(SubjectReadAllNotification, 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
|
||||||
|
}
|
||||||
|
|
||||||
// Subscribe tokens to topic
|
// Subscribe tokens to topic
|
||||||
func (c *Client) Subscribe(topic string, tokens []string) error {
|
func (c *Client) Subscribe(topic string, tokens []string) error {
|
||||||
p := subscribe{
|
p := subscribe{
|
||||||
|
|
Loading…
Reference in New Issue