github.com/status-im/status-go@v1.1.0/protocol/pushnotificationserver/gorush.go (about) 1 package pushnotificationserver 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "io/ioutil" 7 "net/http" 8 9 "go.uber.org/zap" 10 11 "github.com/status-im/status-go/eth-node/types" 12 "github.com/status-im/status-go/protocol/protobuf" 13 ) 14 15 const defaultNewMessageNotificationText = "You have a new message" 16 const defaultMentionNotificationText = "Someone mentioned you" 17 const defaultRequestToJoinCommunityNotificationText = "Someone requested to join a community you are an admin of" 18 19 type GoRushRequestData struct { 20 EncryptedMessage string `json:"encryptedMessage"` 21 ChatID string `json:"chatId"` 22 PublicKey string `json:"publicKey"` 23 } 24 25 type GoRushRequestNotification struct { 26 Tokens []string `json:"tokens"` 27 Platform uint `json:"platform"` 28 Message string `json:"message"` 29 Topic string `json:"topic"` 30 Data *GoRushRequestData `json:"data"` 31 } 32 33 type GoRushRequest struct { 34 Notifications []*GoRushRequestNotification `json:"notifications"` 35 } 36 37 type RequestAndRegistration struct { 38 Request *protobuf.PushNotification 39 Registration *protobuf.PushNotificationRegistration 40 } 41 42 func tokenTypeToGoRushPlatform(tokenType protobuf.PushNotificationRegistration_TokenType) uint { 43 switch tokenType { 44 case protobuf.PushNotificationRegistration_APN_TOKEN: 45 return 1 46 case protobuf.PushNotificationRegistration_FIREBASE_TOKEN: 47 return 2 48 } 49 return 0 50 } 51 52 func PushNotificationRegistrationToGoRushRequest(requestAndRegistrations []*RequestAndRegistration) *GoRushRequest { 53 goRushRequests := &GoRushRequest{} 54 for _, requestAndRegistration := range requestAndRegistrations { 55 request := requestAndRegistration.Request 56 registration := requestAndRegistration.Registration 57 var text string 58 if request.Type == protobuf.PushNotification_MESSAGE { 59 text = defaultNewMessageNotificationText 60 } else if request.Type == protobuf.PushNotification_REQUEST_TO_JOIN_COMMUNITY { 61 text = defaultRequestToJoinCommunityNotificationText 62 } else { 63 text = defaultMentionNotificationText 64 } 65 goRushRequests.Notifications = append(goRushRequests.Notifications, 66 &GoRushRequestNotification{ 67 Tokens: []string{registration.DeviceToken}, 68 Platform: tokenTypeToGoRushPlatform(registration.TokenType), 69 Message: text, 70 Topic: registration.ApnTopic, 71 Data: &GoRushRequestData{ 72 EncryptedMessage: types.EncodeHex(request.Message), 73 ChatID: types.EncodeHex(request.ChatId), 74 PublicKey: types.EncodeHex(request.PublicKey), 75 }, 76 }) 77 } 78 return goRushRequests 79 } 80 81 func sendGoRushNotification(request *GoRushRequest, url string, logger *zap.Logger) error { 82 payload, err := json.Marshal(request) 83 if err != nil { 84 return err 85 } 86 87 response, err := http.Post(url+"/api/push", "application/json", bytes.NewReader(payload)) 88 if err != nil { 89 return err 90 } 91 defer response.Body.Close() 92 body, _ := ioutil.ReadAll(response.Body) 93 94 logger.Info("Sent gorush request", zap.String("response", string(body))) 95 96 return nil 97 }