forked from Alex13658/SchoolServer
/
push-delete.go
67 lines (60 loc) · 2.09 KB
/
push-delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// push-delete
package restapi
import (
"bytes"
"encoding/json"
"net/http"
"github.com/masyagin1998/SchoolServer/libtelco/push"
db "github.com/masyagin1998/SchoolServer/libtelco/sql-db"
"github.com/pkg/errors"
)
// pushDelete достает девайсы пользователя и рассылает на них пуш удаления
func (rest *RestAPI) pushDelete(userName string, schoolID int, category string) error {
var (
usr db.User
devices []db.Device
)
// Получаем пользователя по логину и schoolID
where := db.User{Login: userName, SchoolID: uint(schoolID)}
err := rest.Db.SchoolServerDB.Where(where).First(&usr).Error
if err != nil {
return errors.Wrapf(err, "Error query user='%v'", where)
}
// Достанем все девайсы пользователя
err = rest.Db.SchoolServerDB.Model(&usr).Related(&devices).Error
if err != nil {
return errors.Wrapf(err, "Error when getting user='%v' devices list", usr)
}
// Гоним по девайсам
for _, dev := range devices {
err = rest.sendPushDelete(dev.SystemType, dev.Token, category)
if err != nil {
return err
}
}
return nil
}
// sendPushDelete посылает push-уведомление по web api gorush с приказом
// удалить уведомления заданной категории
func (rest *RestAPI) sendPushDelete(systemType int, token, category string) error {
var notifications []push.Notification
notifications = make([]push.Notification, 1)
notifications[0] = push.Notification{
Tokens: []string{token},
Platform: systemType,
Category: category,
ContentAvailable: true,
Topic: rest.Push.AppTopic,
}
req := push.GorushRequest{Notifications: notifications}
byt, err := json.Marshal(req)
if err != nil {
return errors.Wrap(err, "REST: Error marshalling notification")
}
resp, err := http.Post(rest.Push.GorushAddress, "application/json", bytes.NewBuffer(byt))
if err != nil {
return errors.Wrap(err, "REST: Error sending web api gorush request")
}
defer resp.Body.Close()
return nil
}