MM-25394 session expired push notifications (#14732)

* new job type created that checks for expired mobile sessions and pushes notifications.

* only send session expired notifications if ExtendSessionLengthWithActivity is enabled.

* includes schema change:  field added to Sessions table
Этот коммит содержится в:
Doug Lauder
2020-06-17 14:47:54 -04:00
коммит произвёл GitHub
родитель 2bb6071f73
Коммит b317ee5cf2
29 изменённых файлов: 694 добавлений и 4 удалений

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

@@ -5755,6 +5755,24 @@ func (s *OpenTracingLayerSessionStore) GetSessions(userId string) ([]*model.Sess
return resultVar0, resultVar1
}
func (s *OpenTracingLayerSessionStore) GetSessionsExpired(thresholdMillis int64, mobileOnly bool, unnotifiedOnly bool) ([]*model.Session, *model.AppError) {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.GetSessionsExpired")
s.Root.Store.SetContext(newCtx)
defer func() {
s.Root.Store.SetContext(origCtx)
}()
defer span.Finish()
resultVar0, resultVar1 := s.SessionStore.GetSessionsExpired(thresholdMillis, mobileOnly, unnotifiedOnly)
if resultVar1 != nil {
span.LogFields(spanlog.Error(resultVar1))
ext.Error.Set(span, true)
}
return resultVar0, resultVar1
}
func (s *OpenTracingLayerSessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, *model.AppError) {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.GetSessionsWithActiveDeviceIds")
@@ -5863,6 +5881,24 @@ func (s *OpenTracingLayerSessionStore) UpdateDeviceId(id string, deviceId string
return resultVar0, resultVar1
}
func (s *OpenTracingLayerSessionStore) UpdateExpiredNotify(sessionid string, notified bool) *model.AppError {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.UpdateExpiredNotify")
s.Root.Store.SetContext(newCtx)
defer func() {
s.Root.Store.SetContext(origCtx)
}()
defer span.Finish()
resultVar0 := s.SessionStore.UpdateExpiredNotify(sessionid, notified)
if resultVar0 != nil {
span.LogFields(spanlog.Error(resultVar0))
ext.Error.Set(span, true)
}
return resultVar0
}
func (s *OpenTracingLayerSessionStore) UpdateExpiresAt(sessionId string, time int64) *model.AppError {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "SessionStore.UpdateExpiresAt")

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

@@ -7,6 +7,7 @@ import (
"net/http"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/mattermost/mattermost-server/v5/mlog"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/store"
@@ -135,6 +136,52 @@ func (me SqlSessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*mode
return sessions, nil
}
func (me SqlSessionStore) GetSessionsExpired(thresholdMillis int64, mobileOnly bool, unnotifiedOnly bool) ([]*model.Session, *model.AppError) {
now := model.GetMillis()
builder := me.getQueryBuilder().
Select("*").
From("Sessions").
Where(sq.NotEq{"ExpiresAt": 0}).
Where(sq.Lt{"ExpiresAt": now}).
Where(sq.Gt{"ExpiresAt": now - thresholdMillis})
if mobileOnly {
builder = builder.Where(sq.NotEq{"DeviceId": ""})
}
if unnotifiedOnly {
builder = builder.Where(sq.NotEq{"ExpiredNotify": true})
}
query, args, err := builder.ToSql()
if err != nil {
return nil, model.NewAppError("SqlSessionStore.GetSessionsExpired", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
}
var sessions []*model.Session
_, err = me.GetReplica().Select(&sessions, query, args...)
if err != nil {
return nil, model.NewAppError("SqlSessionStore.GetSessionsExpired", "store.sql_session.get_sessions.app_error", nil, err.Error(), http.StatusInternalServerError)
}
return sessions, nil
}
func (me SqlSessionStore) UpdateExpiredNotify(sessionId string, notified bool) *model.AppError {
query, args, err := me.getQueryBuilder().
Update("Sessions").
Set("ExpiredNotify", notified).
Where(sq.Eq{"Id": sessionId}).
ToSql()
if err != nil {
return model.NewAppError("SqlSessionStore.UpdateExpiredNotifyAt", "store.sql.build_query.app_error", nil, "sessionId="+sessionId, http.StatusInternalServerError)
}
_, err = me.GetMaster().Exec(query, args...)
if err != nil {
return model.NewAppError("SqlSessionStore.UpdateExpiredNotifyAt", "store.sql_session.update_expired_notify.app_error", nil, "sessionId="+sessionId, http.StatusInternalServerError)
}
return nil
}
func (me SqlSessionStore) Remove(sessionIdOrToken string) *model.AppError {
_, err := me.GetMaster().Exec("DELETE FROM Sessions WHERE Id = :Id Or Token = :Token", map[string]interface{}{"Id": sessionIdOrToken, "Token": sessionIdOrToken})
if err != nil {
@@ -161,7 +208,7 @@ func (me SqlSessionStore) PermanentDeleteSessionsByUser(userId string) *model.Ap
}
func (me SqlSessionStore) UpdateExpiresAt(sessionId string, time int64) *model.AppError {
_, err := me.GetMaster().Exec("UPDATE Sessions SET ExpiresAt = :ExpiresAt WHERE Id = :Id", map[string]interface{}{"ExpiresAt": time, "Id": sessionId})
_, err := me.GetMaster().Exec("UPDATE Sessions SET ExpiresAt = :ExpiresAt, ExpiredNotify = false WHERE Id = :Id", map[string]interface{}{"ExpiresAt": time, "Id": sessionId})
if err != nil {
return model.NewAppError("SqlSessionStore.UpdateExpiresAt", "store.sql_session.update_expires_at.app_error", nil, "sessionId="+sessionId, http.StatusInternalServerError)
}
@@ -187,7 +234,7 @@ func (me SqlSessionStore) UpdateRoles(userId, roles string) (string, *model.AppE
}
func (me SqlSessionStore) UpdateDeviceId(id string, deviceId string, expiresAt int64) (string, *model.AppError) {
query := "UPDATE Sessions SET DeviceId = :DeviceId, ExpiresAt = :ExpiresAt WHERE Id = :Id"
query := "UPDATE Sessions SET DeviceId = :DeviceId, ExpiresAt = :ExpiresAt, ExpiredNotify = false WHERE Id = :Id"
_, err := me.GetMaster().Exec(query, map[string]interface{}{"DeviceId": deviceId, "Id": id, "ExpiresAt": expiresAt})
if err != nil {

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

@@ -19,6 +19,8 @@ import (
const (
CURRENT_SCHEMA_VERSION = VERSION_5_24_0
VERSION_5_26_0 = "5.26.0"
VERSION_5_25_0 = "5.25.0"
VERSION_5_24_0 = "5.24.0"
VERSION_5_23_0 = "5.23.0"
VERSION_5_22_0 = "5.22.0"
@@ -179,6 +181,8 @@ func upgradeDatabase(sqlStore SqlStore, currentModelVersionString string) error
upgradeDatabaseToVersion522(sqlStore)
upgradeDatabaseToVersion523(sqlStore)
upgradeDatabaseToVersion524(sqlStore)
upgradeDatabaseToVersion525(sqlStore)
upgradeDatabaseToVersion526(sqlStore)
return nil
}
@@ -803,3 +807,19 @@ func upgradeDatabaseToVersion524(sqlStore SqlStore) {
saveSchemaVersion(sqlStore, VERSION_5_24_0)
}
}
func upgradeDatabaseToVersion525(sqlStore SqlStore) {
// TODO: uncomment when the time arrive to upgrade the DB for 5.25
//if shouldPerformUpgrade(sqlStore, VERSION_5_24_0, VERSION_5_25_0) {
//saveSchemaVersion(sqlStore, VERSION_5_25_0)
//}
}
func upgradeDatabaseToVersion526(sqlStore SqlStore) {
// TODO: uncomment when the time arrive to upgrade the DB for 5.26
//if shouldPerformUpgrade(sqlStore, VERSION_5_25_0, VERSION_5_26_0) {
sqlStore.CreateColumnIfNotExists("Sessions", "ExpiredNotify", "boolean", "boolean", "0")
//saveSchemaVersion(sqlStore, VERSION_5_26_0)
//}
}

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

@@ -353,6 +353,8 @@ type SessionStore interface {
Save(session *model.Session) (*model.Session, *model.AppError)
GetSessions(userId string) ([]*model.Session, *model.AppError)
GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, *model.AppError)
GetSessionsExpired(thresholdMillis int64, mobileOnly bool, unnotifiedOnly bool) ([]*model.Session, *model.AppError)
UpdateExpiredNotify(sessionid string, notified bool) *model.AppError
Remove(sessionIdOrToken string) *model.AppError
RemoveAllSessions() *model.AppError
PermanentDeleteSessionsByUser(teamId string) *model.AppError

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

@@ -92,6 +92,31 @@ func (_m *SessionStore) GetSessions(userId string) ([]*model.Session, *model.App
return r0, r1
}
// GetSessionsExpired provides a mock function with given fields: thresholdMillis, mobileOnly, unnotifiedOnly
func (_m *SessionStore) GetSessionsExpired(thresholdMillis int64, mobileOnly bool, unnotifiedOnly bool) ([]*model.Session, *model.AppError) {
ret := _m.Called(thresholdMillis, mobileOnly, unnotifiedOnly)
var r0 []*model.Session
if rf, ok := ret.Get(0).(func(int64, bool, bool) []*model.Session); ok {
r0 = rf(thresholdMillis, mobileOnly, unnotifiedOnly)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*model.Session)
}
}
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(int64, bool, bool) *model.AppError); ok {
r1 = rf(thresholdMillis, mobileOnly, unnotifiedOnly)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
}
// GetSessionsWithActiveDeviceIds provides a mock function with given fields: userId
func (_m *SessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, *model.AppError) {
ret := _m.Called(userId)
@@ -213,6 +238,22 @@ func (_m *SessionStore) UpdateDeviceId(id string, deviceId string, expiresAt int
return r0, r1
}
// UpdateExpiredNotify provides a mock function with given fields: sessionid, notified
func (_m *SessionStore) UpdateExpiredNotify(sessionid string, notified bool) *model.AppError {
ret := _m.Called(sessionid, notified)
var r0 *model.AppError
if rf, ok := ret.Get(0).(func(string, bool) *model.AppError); ok {
r0 = rf(sessionid, notified)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*model.AppError)
}
}
return r0
}
// UpdateExpiresAt provides a mock function with given fields: sessionId, time
func (_m *SessionStore) UpdateExpiresAt(sessionId string, time int64) *model.AppError {
ret := _m.Called(sessionId, time)

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

@@ -13,6 +13,10 @@ import (
"github.com/stretchr/testify/require"
)
const (
TenMinutes = 600000
)
func TestSessionStore(t *testing.T, ss store.Store) {
// Run serially to prevent interfering with other tests
testSessionCleanup(t, ss)
@@ -29,6 +33,8 @@ func TestSessionStore(t *testing.T, ss store.Store) {
t.Run("UpdateExpiresAt", func(t *testing.T) { testSessionStoreUpdateExpiresAt(t, ss) })
t.Run("UpdateLastActivityAt", func(t *testing.T) { testSessionStoreUpdateLastActivityAt(t, ss) })
t.Run("SessionCount", func(t *testing.T) { testSessionCount(t, ss) })
t.Run("GetSessionsExpired", func(t *testing.T) { testGetSessionsExpired(t, ss) })
t.Run("UpdateExpiredNotify", func(t *testing.T) { testUpdateExpiredNotify(t, ss) })
}
func testSessionStoreSave(t *testing.T, ss store.Store) {
@@ -307,3 +313,81 @@ func testSessionCleanup(t *testing.T, ss store.Store) {
removeErr = ss.Session().Remove(s2.Id)
require.Nil(t, removeErr)
}
func testGetSessionsExpired(t *testing.T, ss store.Store) {
now := model.GetMillis()
// Clear existing sessions.
err := ss.Session().RemoveAllSessions()
require.Nil(t, err)
s1 := &model.Session{}
s1.UserId = model.NewId()
s1.DeviceId = model.NewId()
s1.ExpiresAt = 0 // never expires
s1, err = ss.Session().Save(s1)
require.Nil(t, err)
s2 := &model.Session{}
s2.UserId = model.NewId()
s2.DeviceId = model.NewId()
s2.ExpiresAt = now - TenMinutes // expired within threshold
s2, err = ss.Session().Save(s2)
require.Nil(t, err)
s3 := &model.Session{}
s3.UserId = model.NewId()
s3.DeviceId = model.NewId()
s3.ExpiresAt = now - (TenMinutes * 100) // expired outside threshold
s3, err = ss.Session().Save(s3)
require.Nil(t, err)
s4 := &model.Session{}
s4.UserId = model.NewId()
s4.ExpiresAt = now - TenMinutes // expired within threshold, but not mobile
s4, err = ss.Session().Save(s4)
require.Nil(t, err)
s5 := &model.Session{}
s5.UserId = model.NewId()
s5.DeviceId = model.NewId()
s5.ExpiresAt = now + (TenMinutes * 100000) // not expired
s5, err = ss.Session().Save(s5)
require.Nil(t, err)
sessions, err := ss.Session().GetSessionsExpired(TenMinutes*2, true, true) // mobile only
require.Nil(t, err)
require.Len(t, sessions, 1)
require.Equal(t, s2.Id, sessions[0].Id)
sessions, err = ss.Session().GetSessionsExpired(TenMinutes*2, false, true) // all client types
require.Nil(t, err)
require.Len(t, sessions, 2)
expected := []string{s2.Id, s4.Id}
for _, sess := range sessions {
require.Contains(t, expected, sess.Id)
}
}
func testUpdateExpiredNotify(t *testing.T, ss store.Store) {
s1 := &model.Session{}
s1.UserId = model.NewId()
s1.DeviceId = model.NewId()
s1.ExpiresAt = model.GetMillis() + TenMinutes
s1, err := ss.Session().Save(s1)
require.Nil(t, err)
session, err := ss.Session().Get(s1.Id)
require.Nil(t, err)
require.False(t, session.ExpiredNotify)
ss.Session().UpdateExpiredNotify(session.Id, true)
session, err = ss.Session().Get(s1.Id)
require.Nil(t, err)
require.True(t, session.ExpiredNotify)
ss.Session().UpdateExpiredNotify(session.Id, false)
session, err = ss.Session().Get(s1.Id)
require.Nil(t, err)
require.False(t, session.ExpiredNotify)
}

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

@@ -5211,6 +5211,22 @@ func (s *TimerLayerSessionStore) GetSessions(userId string) ([]*model.Session, *
return resultVar0, resultVar1
}
func (s *TimerLayerSessionStore) GetSessionsExpired(thresholdMillis int64, mobileOnly bool, unnotifiedOnly bool) ([]*model.Session, *model.AppError) {
start := timemodule.Now()
resultVar0, resultVar1 := s.SessionStore.GetSessionsExpired(thresholdMillis, mobileOnly, unnotifiedOnly)
elapsed := float64(timemodule.Since(start)) / float64(timemodule.Second)
if s.Root.Metrics != nil {
success := "false"
if resultVar1 == nil {
success = "true"
}
s.Root.Metrics.ObserveStoreMethodDuration("SessionStore.GetSessionsExpired", success, elapsed)
}
return resultVar0, resultVar1
}
func (s *TimerLayerSessionStore) GetSessionsWithActiveDeviceIds(userId string) ([]*model.Session, *model.AppError) {
start := timemodule.Now()
@@ -5307,6 +5323,22 @@ func (s *TimerLayerSessionStore) UpdateDeviceId(id string, deviceId string, expi
return resultVar0, resultVar1
}
func (s *TimerLayerSessionStore) UpdateExpiredNotify(sessionid string, notified bool) *model.AppError {
start := timemodule.Now()
resultVar0 := s.SessionStore.UpdateExpiredNotify(sessionid, notified)
elapsed := float64(timemodule.Since(start)) / float64(timemodule.Second)
if s.Root.Metrics != nil {
success := "false"
if resultVar0 == nil {
success = "true"
}
s.Root.Metrics.ObserveStoreMethodDuration("SessionStore.UpdateExpiredNotify", success, elapsed)
}
return resultVar0
}
func (s *TimerLayerSessionStore) UpdateExpiresAt(sessionId string, time int64) *model.AppError {
start := timemodule.Now()