From 86447177ca31e0fdd8300d20bf506177084e312a Mon Sep 17 00:00:00 2001 From: Sinh Date: Mon, 14 Nov 2022 15:54:11 +0700 Subject: [PATCH 1/4] update count and read notification --- example/main.go | 22 ++++++++--------- model.go | 41 +++++++++++++++---------------- notification.go | 65 +++++++++++++++++++++++++++++-------------------- 3 files changed, 70 insertions(+), 58 deletions(-) diff --git a/example/main.go b/example/main.go index 6cb3935..a1bfcab 100644 --- a/example/main.go +++ b/example/main.go @@ -24,37 +24,37 @@ func main() { userID := "61a499ad8d5770f8872b03d8" requestID, err := c.PushToUsers(notification.PushRequest{ - Title: "Notification 1", - Body: "nats stream view notification", + Title: "Notification campaign 111", + Body: "Notification campaign 11", Data: "{}", Users: []string{userID}, - Label: "tracking-label", - Category: "order", + Label: "tracking-campaign", + Category: "campaign", }) if err != nil { fmt.Println("Push err: ", err) } fmt.Println("Request id: ", requestID) - total, err := c.CountUnread(notification.CountUnread{ + unread, err := c.CountUnread(notification.CountUnread{ 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{ User: userID, - Category: "order", Page: 0, Limit: 20, + Category: "", // get all if not specify category }) fmt.Println("Query : ", res, err) - total, err = c.CountUnread(notification.CountUnread{ + unread, err = c.CountUnread(notification.CountUnread{ 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{ "eX1gEc7WokSHh-zJ3WR5Hn:APA91bFZDuzkjjFFL6TNpMg0ot93a0wsypWi4aCdm7M2x6AihgjS_QWsbKSFCT4hNhv_d8wKGy-DG6_3e8OlwPiWiJB4R33xLbbUekgxKcfCiiFooIC1E1OE3XWkvUtn4egn8aLG5jqv", diff --git a/model.go b/model.go index ffa5639..91f2d1e 100644 --- a/model.go +++ b/model.go @@ -32,18 +32,10 @@ type PushResponse struct { } type pushRequest struct { - Version string `json:"version"` - APIKey string `json:"apiKey"` - Title string `json:"title"` - Body string `json:"body"` - 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"` + PushRequest + Version string `json:"version"` + APIKey string `json:"apiKey"` + SendBy string `json:"sendBy"` } // Query ... @@ -55,11 +47,8 @@ type Query struct { } type query struct { - APIKey string `json:"apiKey"` - User string `json:"user"` - Category string `json:"category,omitempty"` - Page int64 `json:"page,omitempty"` - Limit int64 `json:"limit,omitempty"` + APIKey string `json:"apiKey"` + Query } // Notification ... @@ -99,13 +88,17 @@ type CountUnread struct { // CountUnreadResponse ... 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 { - APIKey string `json:"apiKey"` - User string `json:"user"` - Category string `json:"category"` + CountUnread + APIKey string `json:"apiKey"` } // CommonError ... @@ -123,3 +116,9 @@ type subscribe struct { Subscribe APIKey string `json:"apiKey"` } + +// ReadAll ... +type ReadAll struct { + User string `json:"user"` + Category string `json:"category,omitempty"` +} diff --git a/notification.go b/notification.go index 5359d35..d23ba49 100644 --- a/notification.go +++ b/notification.go @@ -9,7 +9,7 @@ import ( ) // version specify current version of client -const version = "1.0.2" +const version = "1.0.3" const ( SendByTopic = "topic" @@ -20,6 +20,7 @@ const ( SubjectPushNotification = "push_notification" SubjectGetNotification = "get_notification" SubjectReadNotification = "read_notification" + SubjectReadAllNotification = "read_all_notification" SubjectCountUnreadNotification = "count_unread_notification" SubjectSubscribeTopic = "subscribe_topic" SubjectUnsubscribeTopic = "unsubscribe_topic" @@ -56,17 +57,10 @@ func NewClient(cfg Config) (*Client, error) { // PushToUsers push notification to list user id func (c *Client) PushToUsers(payload PushRequest) (requestID string, err error) { p := pushRequest{ - Version: version, - APIKey: c.Config.APIKey, - Title: payload.Title, - Body: payload.Body, - Data: payload.Data, - SendBy: SendByUsers, - Users: payload.Users, - Label: payload.Label, - Category: payload.Category, - Sound: payload.Sound, - Link: payload.Link, + Version: version, + APIKey: c.Config.APIKey, + SendBy: SendByUsers, + PushRequest: payload, } msg, err := c.natsServer.Request(SubjectPushNotification, toBytes(p)) if err != nil { @@ -88,11 +82,8 @@ func (c *Client) PushToUsers(payload PushRequest) (requestID string, err error) // 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, + APIKey: c.Config.APIKey, + Query: q, } msg, err := c.natsServer.Request(SubjectGetNotification, toBytes(p)) if err != nil { @@ -112,27 +103,26 @@ func (c *Client) Query(q Query) (ListNotificationResponse, error) { } // CountUnread count total unread notification -func (c *Client) CountUnread(q CountUnread) (int64, error) { +func (c *Client) CountUnread(q CountUnread) ([]CategoryCount, error) { p := countUnread{ - APIKey: c.Config.APIKey, - User: q.User, - Category: q.Category, + APIKey: c.Config.APIKey, + CountUnread: q, } msg, err := c.natsServer.Request(SubjectCountUnreadNotification, toBytes(p)) if err != nil { - return 0, err + return nil, err } var res struct { Data CountUnreadResponse `json:"data"` Error string `json:"error"` } - if err := json.Unmarshal(msg.Data, &res); err != nil { - return 0, err + if err = json.Unmarshal(msg.Data, &res); err != nil { + return nil, err } 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 @@ -155,6 +145,29 @@ func (c *Client) Read(notificationID string) error { 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 func (c *Client) Subscribe(topic string, tokens []string) error { p := subscribe{ From 43af89bf216e36b1d4a30fcd274f6365d338d5b3 Mon Sep 17 00:00:00 2001 From: Sinh Date: Tue, 15 Nov 2022 11:46:41 +0700 Subject: [PATCH 2/4] update notification query --- model.go | 1 + 1 file changed, 1 insertion(+) diff --git a/model.go b/model.go index 91f2d1e..c31e912 100644 --- a/model.go +++ b/model.go @@ -42,6 +42,7 @@ type pushRequest struct { type Query struct { User string `json:"user"` Category string `json:"category,omitempty"` + Status string `json:"status"` Page int64 `json:"page,omitempty"` Limit int64 `json:"limit,omitempty"` } From 615aa4e96367da8f715f3d55574a39351debbe94 Mon Sep 17 00:00:00 2001 From: Sinh Date: Mon, 21 Nov 2022 15:33:31 +0700 Subject: [PATCH 3/4] update version --- const.go | 7 +++++++ model.go | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 const.go diff --git a/const.go b/const.go new file mode 100644 index 0000000..81e597a --- /dev/null +++ b/const.go @@ -0,0 +1,7 @@ +package notification + +const ( + SortOrderDesc = "-order" + SortOrderAsc = "order" + SortDefault = "-lastPushAt" +) diff --git a/model.go b/model.go index c31e912..1db232f 100644 --- a/model.go +++ b/model.go @@ -17,7 +17,8 @@ type PushRequest struct { Label string `json:"label,omitempty"` Category string `json:"category,omitempty"` Sound *Sound `json:"sound,omitempty"` - Link string `json:"link"` // for webpush + Link string `json:"link"` // for webpush + Order int `json:"order"` // for sort } // Sound ... @@ -45,6 +46,7 @@ type Query struct { Status string `json:"status"` Page int64 `json:"page,omitempty"` Limit int64 `json:"limit,omitempty"` + Sort string `json:"sort,omitempty"` // SortDefault, SortOrderDesc, SortOrderAsc } type query struct { From 9a5bdda1d926598cdae9af85f4fdda25e5c65427 Mon Sep 17 00:00:00 2001 From: Sinh Date: Tue, 22 Nov 2022 14:06:24 +0700 Subject: [PATCH 4/4] update module --- model.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/model.go b/model.go index 1db232f..2136744 100644 --- a/model.go +++ b/model.go @@ -10,15 +10,16 @@ type Config struct { // PushRequest ... type PushRequest struct { - Title string `json:"title"` - Body string `json:"body"` - Data string `json:"data"` - Users []string `json:"users"` - Label string `json:"label,omitempty"` - Category string `json:"category,omitempty"` - Sound *Sound `json:"sound,omitempty"` - Link string `json:"link"` // for webpush - Order int `json:"order"` // for sort + Title string `json:"title"` + Body string `json:"body"` + Data string `json:"data"` + Users []string `json:"users"` + Label string `json:"label,omitempty"` + Category string `json:"category,omitempty"` + Sound *Sound `json:"sound,omitempty"` + Link string `json:"link"` // for webpush + Order int `json:"order"` // for sort + DisplayUntil int64 `json:"displayUntil"` // time.Unix } // Sound ...