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
This commit is contained in:
Doug Lauder
2020-06-17 14:47:54 -04:00
committed by GitHub
parent 2bb6071f73
commit b317ee5cf2
29 changed files with 694 additions and 4 deletions

View File

@@ -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 {

View File

@@ -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)
//}
}