PLT-4359 fixing push notification for more than 1 device (#4318)

* PLT-4359 fixing push notification for more than 1 device

* Addressing feedback
Этот коммит содержится в:
Corey Hulen
2016-10-24 17:04:11 -07:00
коммит произвёл enahum
родитель 4d9f5173bc
Коммит 9071553165
4 изменённых файлов: 69 добавлений и 22 удалений

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

@@ -901,9 +901,9 @@ func getMessageForNotification(post *model.Post, translateFunc i18n.TranslateFun
} }
func sendPushNotification(post *model.Post, user *model.User, channel *model.Channel, senderName string, wasMentioned bool) { func sendPushNotification(post *model.Post, user *model.User, channel *model.Channel, senderName string, wasMentioned bool) {
session := getMobileAppSession(user.Id) sessions := getMobileAppSessions(user.Id)
if session == nil { if sessions == nil {
return return
} }
@@ -928,8 +928,6 @@ func sendPushNotification(post *model.Post, user *model.User, channel *model.Cha
msg.ChannelId = channel.Id msg.ChannelId = channel.Id
msg.ChannelName = channel.Name msg.ChannelName = channel.Name
msg.SetDeviceIdAndPlatform(session.DeviceId)
if *utils.Cfg.EmailSettings.PushNotificationContents == model.FULL_NOTIFICATION { if *utils.Cfg.EmailSettings.PushNotificationContents == model.FULL_NOTIFICATION {
if channel.Type == model.CHANNEL_DIRECT { if channel.Type == model.CHANNEL_DIRECT {
msg.Category = model.CATEGORY_DM msg.Category = model.CATEGORY_DM
@@ -949,12 +947,17 @@ func sendPushNotification(post *model.Post, user *model.User, channel *model.Cha
} }
l4g.Debug(utils.T("api.post.send_notifications_and_forget.push_notification.debug"), msg.DeviceId, msg.Message) l4g.Debug(utils.T("api.post.send_notifications_and_forget.push_notification.debug"), msg.DeviceId, msg.Message)
sendToPushProxy(msg)
for _, session := range sessions {
tmpMessage := *model.PushNotificationFromJson(strings.NewReader(msg.ToJson()))
tmpMessage.SetDeviceIdAndPlatform(session.DeviceId)
sendToPushProxy(tmpMessage)
}
} }
func clearPushNotification(userId string, channelId string) { func clearPushNotification(userId string, channelId string) {
session := getMobileAppSession(userId) sessions := getMobileAppSessions(userId)
if session == nil { if sessions == nil {
return return
} }
@@ -969,10 +972,12 @@ func clearPushNotification(userId string, channelId string) {
msg.Badge = int(badge.Data.(int64)) msg.Badge = int(badge.Data.(int64))
} }
msg.SetDeviceIdAndPlatform(session.DeviceId)
l4g.Debug(utils.T("api.post.send_notifications_and_forget.clear_push_notification.debug"), msg.DeviceId, msg.ChannelId) l4g.Debug(utils.T("api.post.send_notifications_and_forget.clear_push_notification.debug"), msg.DeviceId, msg.ChannelId)
sendToPushProxy(msg) for _, session := range sessions {
tmpMessage := *model.PushNotificationFromJson(strings.NewReader(msg.ToJson()))
tmpMessage.SetDeviceIdAndPlatform(session.DeviceId)
sendToPushProxy(tmpMessage)
}
} }
func sendToPushProxy(msg model.PushNotification) { func sendToPushProxy(msg model.PushNotification) {
@@ -992,22 +997,13 @@ func sendToPushProxy(msg model.PushNotification) {
} }
} }
func getMobileAppSession(userId string) *model.Session { func getMobileAppSessions(userId string) []*model.Session {
var sessions []*model.Session if result := <-Srv.Store.Session().GetSessionsWithActiveDeviceIds(userId); result.Err != nil {
if result := <-Srv.Store.Session().GetSessions(userId); result.Err != nil {
l4g.Error(utils.T("api.post.send_notifications_and_forget.sessions.error"), userId, result.Err) l4g.Error(utils.T("api.post.send_notifications_and_forget.sessions.error"), userId, result.Err)
return nil return nil
} else { } else {
sessions = result.Data.([]*model.Session) return result.Data.([]*model.Session)
} }
for _, session := range sessions {
if session.IsMobileApp() {
return session
}
}
return nil
} }
func sendOutOfChannelMentions(c *Context, post *model.Post, profiles map[string]*model.User) { func sendOutOfChannelMentions(c *Context, post *model.Post, profiles map[string]*model.User) {

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

@@ -168,6 +168,28 @@ func (me SqlSessionStore) GetSessions(userId string) StoreChannel {
return storeChannel return storeChannel
} }
func (me SqlSessionStore) GetSessionsWithActiveDeviceIds(userId string) StoreChannel {
storeChannel := make(StoreChannel, 1)
go func() {
result := StoreResult{}
var sessions []*model.Session
if _, err := me.GetReplica().Select(&sessions, "SELECT * FROM Sessions WHERE UserId = :UserId AND ExpiresAt != 0 AND :ExpiresAt <= ExpiresAt AND DeviceId != ''", map[string]interface{}{"UserId": userId, "ExpiresAt": model.GetMillis()}); err != nil {
result.Err = model.NewLocAppError("SqlSessionStore.GetActiveSessionsWithDeviceIds", "store.sql_session.get_sessions.app_error", nil, err.Error())
} else {
result.Data = sessions
}
storeChannel <- result
close(storeChannel)
}()
return storeChannel
}
func (me SqlSessionStore) Remove(sessionIdOrToken string) StoreChannel { func (me SqlSessionStore) Remove(sessionIdOrToken string) StoreChannel {
storeChannel := make(StoreChannel, 1) storeChannel := make(StoreChannel, 1)

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

@@ -50,7 +50,35 @@ func TestSessionGet(t *testing.T) {
t.Fatal("should match len") t.Fatal("should match len")
} }
} }
}
func TestSessionGetWithDeviceId(t *testing.T) {
Setup()
s1 := model.Session{}
s1.UserId = model.NewId()
s1.ExpiresAt = model.GetMillis() + 10000
Must(store.Session().Save(&s1))
s2 := model.Session{}
s2.UserId = s1.UserId
s2.DeviceId = model.NewId()
s2.ExpiresAt = model.GetMillis() + 10000
Must(store.Session().Save(&s2))
s3 := model.Session{}
s3.UserId = s1.UserId
s3.ExpiresAt = 1
s3.DeviceId = model.NewId()
Must(store.Session().Save(&s3))
if rs1 := (<-store.Session().GetSessionsWithActiveDeviceIds(s1.UserId)); rs1.Err != nil {
t.Fatal(rs1.Err)
} else {
if len(rs1.Data.([]*model.Session)) != 1 {
t.Fatal("should match len")
}
}
} }
func TestSessionRemove(t *testing.T) { func TestSessionRemove(t *testing.T) {

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

@@ -172,6 +172,7 @@ type SessionStore interface {
Save(session *model.Session) StoreChannel Save(session *model.Session) StoreChannel
Get(sessionIdOrToken string) StoreChannel Get(sessionIdOrToken string) StoreChannel
GetSessions(userId string) StoreChannel GetSessions(userId string) StoreChannel
GetSessionsWithActiveDeviceIds(userId string) StoreChannel
Remove(sessionIdOrToken string) StoreChannel Remove(sessionIdOrToken string) StoreChannel
RemoveAllSessions() StoreChannel RemoveAllSessions() StoreChannel
PermanentDeleteSessionsByUser(teamId string) StoreChannel PermanentDeleteSessionsByUser(teamId string) StoreChannel