MM-15796 Migrate "Session.GetSessionsWithActiveDeviceIds" to Sync by default (#10954)

* MM-15796 Migrate "Session.GetSessionsWithActiveDeviceIds" to Sync by default

* Change API Call to use Sync approach
This commit is contained in:
Bolarinwa Balogun
2019-05-28 09:55:04 -04:00
committed by Jesús Espino
parent d9969613f7
commit bdcee4d979
5 changed files with 35 additions and 23 deletions

View File

@@ -404,11 +404,7 @@ func (a *App) SendAckToPushProxy(ack *model.PushNotificationAck) error {
}
func (a *App) getMobileAppSessions(userId string) ([]*model.Session, *model.AppError) {
result := <-a.Srv.Store.Session().GetSessionsWithActiveDeviceIds(userId)
if result.Err != nil {
return nil, result.Err
}
return result.Data.([]*model.Session), nil
return a.Srv.Store.Session().GetSessionsWithActiveDeviceIds(userId)
}
func ShouldSendPushNotification(user *model.User, channelNotifyProps model.StringMap, wasMentioned bool, status *model.Status, post *model.Post) bool {

View File

@@ -136,17 +136,24 @@ func (me SqlSessionStore) GetSessions(userId string) store.StoreChannel {
})
}
func (me SqlSessionStore) GetSessionsWithActiveDeviceIds(userId string) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
var sessions []*model.Session
func (me SqlSessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, *model.AppError) {
query :=
`SELECT *
FROM
Sessions
WHERE
UserId = :UserId AND
ExpiresAt != 0 AND
:ExpiresAt <= ExpiresAt AND
DeviceId != ''`
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.NewAppError("SqlSessionStore.GetActiveSessionsWithDeviceIds", "store.sql_session.get_sessions.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
var sessions []*model.Session
result.Data = sessions
}
})
_, err := me.GetReplica().Select(&sessions, query, map[string]interface{}{"UserId": userId, "ExpiresAt": model.GetMillis()})
if err != nil {
return nil, model.NewAppError("SqlSessionStore.GetActiveSessionsWithDeviceIds", "store.sql_session.get_sessions.app_error", nil, err.Error(), http.StatusInternalServerError)
}
return sessions, nil
}
func (me SqlSessionStore) Remove(sessionIdOrToken string) store.StoreChannel {

View File

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

View File

@@ -74,19 +74,28 @@ func (_m *SessionStore) GetSessions(userId string) store.StoreChannel {
}
// GetSessionsWithActiveDeviceIds provides a mock function with given fields: userId
func (_m *SessionStore) GetSessionsWithActiveDeviceIds(userId string) store.StoreChannel {
func (_m *SessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, *model.AppError) {
ret := _m.Called(userId)
var r0 store.StoreChannel
if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok {
var r0 []*model.Session
if rf, ok := ret.Get(0).(func(string) []*model.Session); ok {
r0 = rf(userId)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel)
r0 = ret.Get(0).([]*model.Session)
}
}
return r0
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string) *model.AppError); ok {
r1 = rf(userId)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
}
// PermanentDeleteSessionsByUser provides a mock function with given fields: teamId

View File

@@ -87,10 +87,10 @@ func testSessionGetWithDeviceId(t *testing.T, ss store.Store) {
s3.DeviceId = model.NewId()
store.Must(ss.Session().Save(&s3))
if rs1 := (<-ss.Session().GetSessionsWithActiveDeviceIds(s1.UserId)); rs1.Err != nil {
t.Fatal(rs1.Err)
if data, err := ss.Session().GetSessionsWithActiveDeviceIds(s1.UserId); err != nil {
t.Fatal(err)
} else {
if len(rs1.Data.([]*model.Session)) != 1 {
if len(data) != 1 {
t.Fatal("should match len")
}
}