2020-07-22 07:41:40 +00:00
|
|
|
package pushnotificationserver
|
2020-07-03 08:02:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2020-07-30 06:55:56 +00:00
|
|
|
"io/ioutil"
|
2020-07-03 08:02:28 +00:00
|
|
|
"net/http"
|
|
|
|
|
2020-07-30 06:55:56 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-08-25 09:46:01 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2020-07-03 08:02:28 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
)
|
|
|
|
|
|
|
|
const defaultNotificationMessage = "You have a new message"
|
|
|
|
|
|
|
|
type GoRushRequestData struct {
|
|
|
|
EncryptedMessage string `json:"encryptedMessage"`
|
|
|
|
ChatID string `json:"chatId"`
|
|
|
|
PublicKey string `json:"publicKey"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GoRushRequestNotification struct {
|
|
|
|
Tokens []string `json:"tokens"`
|
|
|
|
Platform uint `json:"platform"`
|
|
|
|
Message string `json:"message"`
|
2020-07-30 06:55:56 +00:00
|
|
|
Topic string `json:"topic"`
|
2020-07-03 08:02:28 +00:00
|
|
|
Data *GoRushRequestData `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GoRushRequest struct {
|
|
|
|
Notifications []*GoRushRequestNotification `json:"notifications"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type RequestAndRegistration struct {
|
|
|
|
Request *protobuf.PushNotification
|
|
|
|
Registration *protobuf.PushNotificationRegistration
|
|
|
|
}
|
|
|
|
|
|
|
|
func tokenTypeToGoRushPlatform(tokenType protobuf.PushNotificationRegistration_TokenType) uint {
|
|
|
|
switch tokenType {
|
|
|
|
case protobuf.PushNotificationRegistration_APN_TOKEN:
|
|
|
|
return 1
|
|
|
|
case protobuf.PushNotificationRegistration_FIREBASE_TOKEN:
|
|
|
|
return 2
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func PushNotificationRegistrationToGoRushRequest(requestAndRegistrations []*RequestAndRegistration) *GoRushRequest {
|
|
|
|
goRushRequests := &GoRushRequest{}
|
|
|
|
for _, requestAndRegistration := range requestAndRegistrations {
|
|
|
|
request := requestAndRegistration.Request
|
|
|
|
registration := requestAndRegistration.Registration
|
|
|
|
goRushRequests.Notifications = append(goRushRequests.Notifications,
|
|
|
|
&GoRushRequestNotification{
|
2020-07-22 07:41:40 +00:00
|
|
|
Tokens: []string{registration.DeviceToken},
|
2020-07-03 08:02:28 +00:00
|
|
|
Platform: tokenTypeToGoRushPlatform(registration.TokenType),
|
|
|
|
Message: defaultNotificationMessage,
|
2020-07-30 08:24:30 +00:00
|
|
|
Topic: registration.ApnTopic,
|
2020-07-03 08:02:28 +00:00
|
|
|
Data: &GoRushRequestData{
|
2020-08-26 05:54:00 +00:00
|
|
|
EncryptedMessage: types.EncodeHex(request.Message),
|
2020-08-25 09:46:01 +00:00
|
|
|
ChatID: types.EncodeHex(request.ChatId),
|
2020-08-26 05:54:00 +00:00
|
|
|
PublicKey: types.EncodeHex(request.PublicKey),
|
2020-07-03 08:02:28 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return goRushRequests
|
|
|
|
}
|
|
|
|
|
2020-07-30 06:55:56 +00:00
|
|
|
func sendGoRushNotification(request *GoRushRequest, url string, logger *zap.Logger) error {
|
2020-07-03 08:02:28 +00:00
|
|
|
payload, err := json.Marshal(request)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-30 06:55:56 +00:00
|
|
|
response, err := http.Post(url+"/api/push", "application/json", bytes.NewReader(payload))
|
2020-07-03 08:02:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-30 06:55:56 +00:00
|
|
|
defer response.Body.Close()
|
|
|
|
body, _ := ioutil.ReadAll(response.Body)
|
|
|
|
|
|
|
|
logger.Info("Sent gorush request", zap.String("response", string(body)))
|
|
|
|
|
2020-07-03 08:02:28 +00:00
|
|
|
return nil
|
|
|
|
}
|