[MM-8497] Ability to set Do Not Disturb for a specified period of time (#16067)
* Add support for timed DND status - accept a date time value in api query when dnd mode for user needs to be unset - Create a new function to handle SetDNDStatus calls - Create a scheduled task to unset dnd mode to wahtever mode was before setting it to DND * update schema version * Model changes to make fields more intuitive - move dndendtime to status model - add new field prev status in status to keep track of previous status of user - update db migration function - make use of prevstatus and dndendtime from status model * set prev status and dndendtime appropriately after unsetting dnd mode * add json tag for dndendtime * unset dnd status only if not changed manually by user * update dnd statuses after server restart * make app-layers * fix failing tests * don't create sched task when setting status to DND * get only expired statuses from db - convert end time from any timezone to utc - store dnd end time in unix format for usability reasons * run update dnd status only on leader * make mocks * fix tests * run UpdateDNDStatusOfUsers as recurring task * save all statuses at once in db and update UpdateDNDStatusOfUsers logic * add app method to get timezone of user * store dnd end time in context.Params * set max size of prevstatus * update status model to take endtime input as string and store in db as unix time(int64) * Add tests for SetStatusDoNotDisturbTimed * if dnd_end_time is not passed the call old api to set dnd mode * fix tests * new plugin api to use new timed dnd mode * get and update rows in a single db query * dnd end time will be stored in request body and not route param * exclude statuses which has dndendtimeunix < 0 * update and get the updated dnd statuses in single db query * add updated status to cache * DNDEndTimeUnix and PrevStatus need not to be visible to users * update db schema version for migration * Keep Status and PrevStatus varchar size same * add test to verify status is restored after dnd end time expires * expect endtime in utc from client - remove store method GetTimezone as no longer needed - add documentation for SetStatusDoNotDisturbTimed * reduce sleep time for dnd timed restore test * more appropriate name for new api to update user status * update db migration function * parse and validate time before potentially triggering db query to get status of user * add migration changes in to existing upgrade function * not supporting un-timed dnd status via api * don't call Srv.Store directly, call via app layer * rename dndendtime to statuscleartime to make it suitable for custom status usage as well * Revert "rename dndendtime to statuscleartime to make it suitable for custom status usage as well" This reverts commit fa69152d9a3db18f1c59b34c878fb7ce494440b5. * mysql doesn't support RETURNING clause so add tx to get and update statuses * add UpdateDNDStatusOfUsers mock in tests * update store mock import path * add mock in storelib * Add status mocks to empty store * Close the task during server shutdown * Do not cancel a nil task * update squirrel queries * remove untimed dnd test * start recurring task to unset statuses on leadership change * set dndTask to nil after cancelling it upon server shutdown * new recurring task which starts at nearest rounded time of the interval * mock Get() call for status * return updated statuses in case of mysql * remove unneccessary code * add Get() mock to empty store * fix mocking for once and all * address review comments fix mysql updateStatus fn protect dndTask with mutex minor refactors * move runDNDStatusExpireJob to server.go and pass App as arg instead of method receiver * frontend will send endtime in unix epoch format so get rid of double representation * scan for all fields and not just two * add some tests and fix review comments * remove extra sql query and create needed result in go * add storetest for UpdateExpiredDNDStatuses * add migrations to latest version * update min supported version * add comment to fix a bug in future * update test to expect 1 status in return * rename UpdateUserStatusWithDNDTimeout to SetUserStatusTimedDND * rename DNDEndTimeUnix to DNDEndTime * cast int to int64 for equality * fix tests and error handling * move updating values to retrieved statuses fields outside sql transaction * move migrations to 5.36 Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in> Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
@@ -7,10 +7,12 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
sq "github.com/Masterminds/squirrel"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/mattermost/gorp"
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/store"
|
||||
)
|
||||
@@ -27,6 +29,7 @@ func newSqlStatusStore(sqlStore *SqlStore) store.StatusStore {
|
||||
table.ColMap("UserId").SetMaxSize(26)
|
||||
table.ColMap("Status").SetMaxSize(32)
|
||||
table.ColMap("ActiveChannel").SetMaxSize(26)
|
||||
table.ColMap("PrevStatus").SetMaxSize(32)
|
||||
}
|
||||
|
||||
return s
|
||||
@@ -99,6 +102,119 @@ func (s SqlStatusStore) GetByIds(userIds []string) ([]*model.Status, error) {
|
||||
return statuses, nil
|
||||
}
|
||||
|
||||
// MySQL doesn't have support for RETURNING clause, so we use a transaction to get the updated rows.
|
||||
func (s SqlStatusStore) updateExpiredStatuses(t *gorp.Transaction) ([]*model.Status, error) {
|
||||
var statuses []*model.Status
|
||||
currUnixTime := time.Now().UTC().Unix()
|
||||
selectQuery, selectParams, err := s.getQueryBuilder().
|
||||
Select("*").
|
||||
From("Status").
|
||||
Where(
|
||||
sq.And{
|
||||
sq.Eq{"Status": model.STATUS_DND},
|
||||
sq.Gt{"DNDEndTime": 0},
|
||||
sq.LtOrEq{"DNDEndTime": currUnixTime},
|
||||
},
|
||||
).ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "status_tosql")
|
||||
}
|
||||
_, err = t.Select(&statuses, selectQuery, selectParams...)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "updateExpiredStatusesT: failed to get expired dnd statuses")
|
||||
}
|
||||
updateQuery, args, err := s.getQueryBuilder().
|
||||
Update("Status").
|
||||
Where(
|
||||
sq.And{
|
||||
sq.Eq{"Status": model.STATUS_DND},
|
||||
sq.Gt{"DNDEndTime": 0},
|
||||
sq.LtOrEq{"DNDEndTime": currUnixTime},
|
||||
},
|
||||
).
|
||||
Set("Status", sq.Expr("PrevStatus")).
|
||||
Set("PrevStatus", model.STATUS_DND).
|
||||
Set("DNDEndTime", 0).
|
||||
Set("Manual", false).
|
||||
ToSql()
|
||||
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "status_tosql")
|
||||
}
|
||||
|
||||
if _, err := t.Exec(updateQuery, args...); err != nil {
|
||||
return nil, errors.Wrapf(err, "updateExpiredStatusesT: failed to update statuses")
|
||||
}
|
||||
|
||||
return statuses, nil
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) UpdateExpiredDNDStatuses() ([]*model.Status, error) {
|
||||
if s.DriverName() == model.DATABASE_DRIVER_MYSQL {
|
||||
transaction, err := s.GetMaster().Begin()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "UpdateExpiredDNDStatuses: begin_transaction")
|
||||
}
|
||||
defer finalizeTransaction(transaction)
|
||||
statuses, err := s.updateExpiredStatuses(transaction)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "UpdateExpiredDNDStatuses: updateExpiredDNDStatusesT")
|
||||
}
|
||||
if err := transaction.Commit(); err != nil {
|
||||
return nil, errors.Wrap(err, "UpdateExpiredDNDStatuses: commit_transaction")
|
||||
}
|
||||
|
||||
for _, status := range statuses {
|
||||
status.Status = status.PrevStatus
|
||||
status.PrevStatus = model.STATUS_DND
|
||||
status.DNDEndTime = 0
|
||||
status.Manual = false
|
||||
}
|
||||
|
||||
return statuses, nil
|
||||
}
|
||||
|
||||
queryString, args, err := s.getQueryBuilder().
|
||||
Update("Status").
|
||||
Where(
|
||||
sq.And{
|
||||
sq.Eq{"Status": model.STATUS_DND},
|
||||
sq.Gt{"DNDEndTime": 0},
|
||||
sq.LtOrEq{"DNDEndTime": time.Now().UTC().Unix()},
|
||||
},
|
||||
).
|
||||
Set("Status", sq.Expr("PrevStatus")).
|
||||
Set("PrevStatus", model.STATUS_DND).
|
||||
Set("DNDEndTime", 0).
|
||||
Set("Manual", false).
|
||||
Suffix("RETURNING *").
|
||||
ToSql()
|
||||
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "status_tosql")
|
||||
}
|
||||
|
||||
rows, err := s.GetMaster().Query(queryString, args...)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to find Statuses")
|
||||
}
|
||||
defer rows.Close()
|
||||
var statuses []*model.Status
|
||||
for rows.Next() {
|
||||
var status model.Status
|
||||
if err = rows.Scan(&status.UserId, &status.Status, &status.Manual, &status.LastActivityAt,
|
||||
&status.DNDEndTime, &status.PrevStatus); err != nil {
|
||||
return nil, errors.Wrap(err, "unable to scan from rows")
|
||||
}
|
||||
statuses = append(statuses, &status)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, errors.Wrap(err, "failed while iterating over rows")
|
||||
}
|
||||
|
||||
return statuses, nil
|
||||
}
|
||||
|
||||
func (s SqlStatusStore) ResetAll() error {
|
||||
if _, err := s.GetMaster().Exec("UPDATE Status SET Status = :Status WHERE Manual = false", map[string]interface{}{"Status": model.STATUS_OFFLINE}); err != nil {
|
||||
return errors.Wrap(err, "failed to update Statuses")
|
||||
|
||||
@@ -953,6 +953,7 @@ func upgradeDatabaseToVersion530(sqlStore *SqlStore) {
|
||||
sqlStore.CreateColumnIfNotExistsNoDefault("FileInfo", "Content", "longtext", "text")
|
||||
|
||||
sqlStore.CreateColumnIfNotExists("SidebarCategories", "Muted", "tinyint(1)", "boolean", "0")
|
||||
|
||||
saveSchemaVersion(sqlStore, Version5300)
|
||||
}
|
||||
}
|
||||
@@ -1067,6 +1068,10 @@ func upgradeDatabaseToVersion536(sqlStore *SqlStore) {
|
||||
|
||||
sqlStore.CreateColumnIfNotExists("SharedChannelUsers", "ChannelId", "VARCHAR(26)", "VARCHAR(26)", "")
|
||||
|
||||
// timed dnd status support
|
||||
sqlStore.CreateColumnIfNotExistsNoDefault("Status", "DNDEndTime", "BIGINT", "BIGINT")
|
||||
sqlStore.CreateColumnIfNotExistsNoDefault("Status", "PrevStatus", "VARCHAR(32)", "VARCHAR(32)")
|
||||
|
||||
//saveSchemaVersion(sqlStore, Version5360)
|
||||
//}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user