From bdcee4d979cfb0e544c14c678bb7c37284132fc2 Mon Sep 17 00:00:00 2001 From: Bolarinwa Balogun Date: Tue, 28 May 2019 09:55:04 -0400 Subject: [PATCH] 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 --- app/notification_push.go | 6 +----- store/sqlstore/session_store.go | 25 ++++++++++++++++--------- store/store.go | 2 +- store/storetest/mocks/SessionStore.go | 19 ++++++++++++++----- store/storetest/session_store.go | 6 +++--- 5 files changed, 35 insertions(+), 23 deletions(-) diff --git a/app/notification_push.go b/app/notification_push.go index ceeac8d71c..35a202ec35 100644 --- a/app/notification_push.go +++ b/app/notification_push.go @@ -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 { diff --git a/store/sqlstore/session_store.go b/store/sqlstore/session_store.go index 4f949c7fef..9ef850b3af 100644 --- a/store/sqlstore/session_store.go +++ b/store/sqlstore/session_store.go @@ -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 { diff --git a/store/store.go b/store/store.go index ca4949e7a0..4d8be030b3 100644 --- a/store/store.go +++ b/store/store.go @@ -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 diff --git a/store/storetest/mocks/SessionStore.go b/store/storetest/mocks/SessionStore.go index 7cc005e006..6bff8311ef 100644 --- a/store/storetest/mocks/SessionStore.go +++ b/store/storetest/mocks/SessionStore.go @@ -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 diff --git a/store/storetest/session_store.go b/store/storetest/session_store.go index 5a69b215c4..bed9795af1 100644 --- a/store/storetest/session_store.go +++ b/store/storetest/session_store.go @@ -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") } }