Fixing removing push tokens and logging error messages (#5551)

This commit is contained in:
Corey Hulen
2017-02-28 20:19:19 -05:00
committed by enahum
parent 28c218db3b
commit fc9e5d8510
2 changed files with 65 additions and 6 deletions

View File

@@ -552,7 +552,7 @@ func ClearPushNotification(userId string, channelId string) *model.AppError {
return nil
}
func sendToPushProxy(msg model.PushNotification, session *model.Session) *model.AppError {
func sendToPushProxy(msg model.PushNotification, session *model.Session) {
msg.ServerId = utils.CfgDiagnosticId
tr := &http.Transport{
@@ -563,22 +563,24 @@ func sendToPushProxy(msg model.PushNotification, session *model.Session) *model.
request, _ := http.NewRequest("POST", *utils.Cfg.EmailSettings.PushNotificationServer+model.API_URL_SUFFIX_V1+"/send_push", strings.NewReader(msg.ToJson()))
if resp, err := httpClient.Do(request); err != nil {
return model.NewLocAppError("sendToPushProxy", "api.post.send_notifications_and_forget.push_notification.error", map[string]interface{}{"DeviceId": msg.DeviceId, "Error": err.Error()}, "")
l4g.Error("Device push reported as error for UserId=%v SessionId=%v message=%v", session.UserId, session.Id, err.Error())
} else {
m := model.MapFromJson(resp.Body)
pushResponse := model.PushResponseFromJson(resp.Body)
if resp.Body != nil {
ioutil.ReadAll(resp.Body)
resp.Body.Close()
}
if m[model.STATUS] == model.STATUS_REMOVE {
if pushResponse[model.PUSH_STATUS] == model.PUSH_STATUS_REMOVE {
l4g.Info("Device was reported as removed for UserId=%v SessionId=%v removing push for this session", session.UserId, session.Id)
AttachDeviceId(session.Id, "", session.ExpiresAt)
ClearSessionCacheForUser(session.UserId)
}
}
return nil
if pushResponse[model.PUSH_STATUS] == model.PUSH_STATUS_FAIL {
l4g.Error("Device push reported as error for UserId=%v SessionId=%v message=%v", session.UserId, session.Id, pushResponse[model.PUSH_STATUS_ERROR_MSG])
}
}
}
func getMobileAppSessions(userId string) ([]*model.Session, *model.AppError) {

57
model/push_response.go Normal file
View File

@@ -0,0 +1,57 @@
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package model
import (
"encoding/json"
"io"
)
const (
PUSH_STATUS = "status"
PUSH_STATUS_OK = "OK"
PUSH_STATUS_FAIL = "FAIL"
PUSH_STATUS_REMOVE = "REMOVE"
PUSH_STATUS_ERROR_MSG = "error"
)
type PushResponse map[string]string
func NewOkPushResponse() PushResponse {
m := make(map[string]string)
m[PUSH_STATUS] = PUSH_STATUS_OK
return m
}
func NewRemovePushResponse() PushResponse {
m := make(map[string]string)
m[PUSH_STATUS] = PUSH_STATUS_REMOVE
return m
}
func NewErrorPushResponse(message string) PushResponse {
m := make(map[string]string)
m[PUSH_STATUS] = PUSH_STATUS_FAIL
m[PUSH_STATUS_ERROR_MSG] = message
return m
}
func (me *PushResponse) ToJson() string {
if b, err := json.Marshal(me); err != nil {
return ""
} else {
return string(b)
}
}
func PushResponseFromJson(data io.Reader) PushResponse {
decoder := json.NewDecoder(data)
var objmap PushResponse
if err := decoder.Decode(&objmap); err != nil {
return make(map[string]string)
} else {
return objmap
}
}