[Gekidou] Add push notification check to ping (#19610)

* Add push notification check to ping

* Add test type and missing details

* Address feedback

* Fix function name

* Address feedback

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Daniel Espino García
2022-03-02 12:15:58 +01:00
коммит произвёл GitHub
родитель 7e2ae93231
Коммит 32e3c2f0b8
7 изменённых файлов: 131 добавлений и 29 удалений

Просмотреть файл

@@ -379,6 +379,32 @@ func (s *Server) StopPushNotificationsHubWorkers() {
s.PushNotificationsHub.stop()
}
func (a *App) rawSendToPushProxy(msg *model.PushNotification) (model.PushResponse, error) {
msgJSON, jsonErr := json.Marshal(msg)
if jsonErr != nil {
return nil, errors.Wrap(jsonErr, "failed to encode to JSON")
}
url := strings.TrimRight(*a.Config().EmailSettings.PushNotificationServer, "/") + model.APIURLSuffixV1 + "/send_push"
request, err := http.NewRequest("POST", url, bytes.NewReader(msgJSON))
if err != nil {
return nil, err
}
resp, err := a.Srv().pushNotificationClient.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var pushResponse model.PushResponse
if jsonErr := json.NewDecoder(resp.Body).Decode(&pushResponse); jsonErr != nil {
return nil, errors.Wrap(jsonErr, "failed to decode from JSON")
}
return pushResponse, nil
}
func (a *App) sendToPushProxy(msg *model.PushNotification, session *model.Session) error {
msg.ServerId = a.TelemetryId()
@@ -390,28 +416,11 @@ func (a *App) sendToPushProxy(msg *model.PushNotification, session *model.Sessio
mlog.String("status", model.PushSendPrepare),
)
msgJSON, jsonErr := json.Marshal(msg)
if jsonErr != nil {
return errors.Wrap(jsonErr, "failed to encode to JSON")
}
url := strings.TrimRight(*a.Config().EmailSettings.PushNotificationServer, "/") + model.APIURLSuffixV1 + "/send_push"
request, err := http.NewRequest("POST", url, bytes.NewReader(msgJSON))
pushResponse, err := a.rawSendToPushProxy(msg)
if err != nil {
return err
}
resp, err := a.Srv().pushNotificationClient.Do(request)
if err != nil {
return err
}
defer resp.Body.Close()
var pushResponse model.PushResponse
if jsonErr := json.NewDecoder(resp.Body).Decode(&pushResponse); jsonErr != nil {
return errors.Wrap(jsonErr, "failed to decode from JSON")
}
switch pushResponse[model.PushStatus] {
case model.PushStatusRemove:
a.AttachDeviceId(session.Id, "", session.ExpiresAt)
@@ -568,6 +577,40 @@ func (a *App) BuildPushNotificationMessage(contentsConfig string, post *model.Po
return msg, nil
}
func (a *App) SendTestPushNotification(deviceID string) string {
msg := &model.PushNotification{
Version: "2",
Type: model.PushTypeTest,
ServerId: a.TelemetryId(),
Badge: -1,
}
msg.SetDeviceIdAndPlatform(deviceID)
pushResponse, err := a.rawSendToPushProxy(msg)
if err != nil {
a.NotificationsLog().Error("Notification error",
mlog.String("type", msg.Type),
mlog.String("deviceId", msg.DeviceId),
mlog.String("status", err.Error()),
)
return "unknown"
}
switch pushResponse[model.PushStatus] {
case model.PushStatusRemove:
return "false"
case model.PushStatusFail:
a.NotificationsLog().Error("Notification error",
mlog.String("type", msg.Type),
mlog.String("deviceId", msg.DeviceId),
mlog.String("status", pushResponse[model.PushStatusErrorMsg]),
)
return "unknown"
}
return "true"
}
func (a *App) buildIdLoadedPushNotificationMessage(channel *model.Channel, post *model.Post, user *model.User) *model.PushNotification {
userLocale := i18n.GetUserTranslations(user.Locale)
msg := &model.PushNotification{