Commit 74e703f5 authored by Chris Duarte's avatar Chris Duarte Committed by Joram Wilander

Timezone feature (#8185)

* Add supported timezones into config

Remove Timezone list creation in favor of timezone from configs

Add Timezone field to Users table

Clean up format of SupportedTimezones in config

* Remove unwanted change

* Add test for updating user timezone

* Add empty map[string]string if Timezone is null

* Add EnableTimezoneSelection config

* Revert back to map[string]string for ClientConfig

* Refactor SupportedTimezones into timezones.json

* Include timezones.json in TestConfigFlag

* Add timezone api endpoint

* Bump varchar size to 256 and setMaxSize in user_store

* Refactor LoadConfig to LoadConfig and LoadTimezoneConfig

* Remove unnecessary argument in LoadConfig, mail_test

* Add test for timezone endpoint

* Add license header

* Refactor timezones endpoint to system.go

* Add system base route to timezone endpoint

* db timezone upgrade in db v4.9

* Avoid saving SupportedTimezones to config.json

* Add timezonePath support in config

* Remove EnableTimezoneSelection from config

* Use return statement without return parameter

* Refactor test for SupportedTimezones

* Check for supportedTimezone != nil instead of using len

* Decouple SupportedTimezones out of Config

* Fix failing test

* Add LastTeamIconUpdate back in upgrade.go

* Write timezone config in config_flag_test

* Add code fallback for default timezone support
parent 9d701c70
......@@ -17,6 +17,8 @@ import (
func (api *API) InitSystem() {
api.BaseRoutes.System.Handle("/ping", api.ApiHandler(getSystemPing)).Methods("GET")
api.BaseRoutes.System.Handle("/timezones", api.ApiSessionRequired(getSupportedTimezones)).Methods("GET")
api.BaseRoutes.ApiRoot.Handle("/config", api.ApiSessionRequired(getConfig)).Methods("GET")
api.BaseRoutes.ApiRoot.Handle("/config", api.ApiSessionRequired(updateConfig)).Methods("PUT")
api.BaseRoutes.ApiRoot.Handle("/config/reload", api.ApiSessionRequired(configReload)).Methods("POST")
......@@ -378,6 +380,18 @@ func getAnalytics(c *Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte(rows.ToJson()))
}
func getSupportedTimezones(c *Context, w http.ResponseWriter, r *http.Request) {
supportedTimezones := c.App.Timezones()
if supportedTimezones != nil {
w.Write([]byte(model.TimezonesToJson(supportedTimezones)))
return
}
emptyTimezones := make([]string, 0)
w.Write([]byte(model.TimezonesToJson(emptyTimezones)))
}
func testS3(c *Context, w http.ResponseWriter, r *http.Request) {
cfg := model.ConfigFromJson(r.Body)
if cfg == nil {
......
......@@ -527,3 +527,15 @@ func TestS3TestConnection(t *testing.T) {
t.Fatal("should return error ")
}
}
func TestSupportedTimezones(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
Client := th.Client
supportedTimezonesFromConfig := th.App.Timezones()
supportedTimezones, resp := Client.GetSupportedTimezone()
CheckNoError(t, resp)
assert.Equal(t, supportedTimezonesFromConfig, supportedTimezones)
}
......@@ -996,6 +996,10 @@ func TestPatchUser(t *testing.T) {
patch.Position = new(string)
patch.NotifyProps = model.StringMap{}
patch.NotifyProps["comment"] = "somethingrandom"
patch.Timezone = model.StringMap{}
patch.Timezone["useAutomaticTimezone"] = "true"
patch.Timezone["automaticTimezone"] = "America/New_York"
patch.Timezone["manualTimezone"] = ""
ruser, resp := Client.PatchUser(user.Id, patch)
CheckNoError(t, resp)
......@@ -1019,6 +1023,15 @@ func TestPatchUser(t *testing.T) {
if ruser.NotifyProps["comment"] != "somethingrandom" {
t.Fatal("NotifyProps did not update properly")
}
if ruser.Timezone["useAutomaticTimezone"] != "true" {
t.Fatal("useAutomaticTimezone did not update properly")
}
if ruser.Timezone["automaticTimezone"] != "America/New_York" {
t.Fatal("automaticTimezone did not update properly")
}
if ruser.Timezone["manualTimezone"] != "" {
t.Fatal("manualTimezone did not update properly")
}
patch.Username = model.NewString(th.BasicUser2.Username)
_, resp = Client.PatchUser(user.Id, patch)
......
......@@ -63,6 +63,8 @@ type App struct {
clientLicenseValue atomic.Value
licenseListeners map[string]func()
timezones atomic.Value
siteURL string
newStore func() store.Store
......@@ -125,6 +127,9 @@ func New(options ...Option) (outApp *App, outErr error) {
return nil, err
}
app.EnableConfigWatch()
app.LoadTimezones()
if err := utils.InitTranslations(app.Config().LocalizationSettings); err != nil {
return nil, errors.Wrapf(err, "unable to load Mattermost translation files")
}
......
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
)
func (a *App) Timezones() model.SupportedTimezones {
if cfg := a.timezones.Load(); cfg != nil {
return cfg.(model.SupportedTimezones)
}
return model.SupportedTimezones{}
}
func (a *App) LoadTimezones() {
timezonePath := "timezones.json"
if a.Config().TimezoneSettings.SupportedTimezonesPath != nil && len(*a.Config().TimezoneSettings.SupportedTimezonesPath) > 0 {
timezonePath = *a.Config().TimezoneSettings.SupportedTimezonesPath
}
timezoneCfg := utils.LoadTimezones(timezonePath)
a.timezones.Store(timezoneCfg)
}
......@@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/require"
"encoding/json"
"github.com/mattermost/mattermost-server/cmd"
"github.com/mattermost/mattermost-server/utils"
)
......@@ -26,6 +27,11 @@ func TestConfigFlag(t *testing.T) {
configPath := filepath.Join(dir, "foo.json")
require.NoError(t, ioutil.WriteFile(configPath, []byte(config.ToJson()), 0600))
timezones := utils.LoadTimezones("timezones.json")
tzConfigPath := filepath.Join(dir, "timezones.json")
timezoneData, _ := json.Marshal(timezones)
require.NoError(t, ioutil.WriteFile(tzConfigPath, timezoneData, 0600))
i18n, ok := utils.FindDir("i18n")
require.True(t, ok)
require.NoError(t, utils.CopyDir(i18n, filepath.Join(dir, "i18n")))
......
......@@ -211,6 +211,9 @@
"AllowCustomThemes": true,
"AllowedThemes": []
},
"TimezoneSettings": {
"SupportedTimezonesPath": "timezones.json"
},
"GitLabSettings": {
"Enable": false,
"Secret": "",
......
This diff is collapsed.
......@@ -3770,6 +3770,18 @@
"id": "app.team.join_user_to_team.max_accounts.app_error",
"translation": "This team has reached the maximum number of allowed accounts. Contact your systems administrator to set a higher limit."
},
{
"id": "app.timezones.failed_deserialize.app_error",
"translation": "Failed to deserialize Timezone config file={{.Filename}}, err={{.Error}}"
},
{
"id": "app.timezones.load_config.app_error",
"translation": "Timezone config file does not exists file={{.Filename}}"
},
{
"id": "app.timezones.read_config.app_error",
"translation": "Failed to read Timezone config file={{.Filename}}, err={{.Error}}"
},
{
"id": "app.user_access_token.disabled",
"translation": "Personal access tokens are disabled on this server. Please contact your system administrator for details."
......
......@@ -314,6 +314,10 @@ func (c *Client4) GetAnalyticsRoute() string {
return fmt.Sprintf("/analytics")
}
func (c *Client4) GetTimezonesRoute() string {
return fmt.Sprintf(c.GetSystemRoute() + "/timezones")
}
func (c *Client4) DoApiGet(url string, etag string) (*http.Response, *AppError) {
return c.DoApiRequest(http.MethodGet, c.ApiUrl+url, "", etag)
}
......@@ -3169,6 +3173,18 @@ func (c *Client4) DeleteReaction(reaction *Reaction) (bool, *Response) {
}
}
// Timezone Section
// GetSupportedTimezone returns a page of supported timezones on the system.
func (c *Client4) GetSupportedTimezone() (SupportedTimezones, *Response) {
if r, err := c.DoApiGet(c.GetTimezonesRoute(), ""); err != nil {
return nil, BuildErrorResponse(r, err)
} else {
defer closeBody(r)
return TimezonesFromJson(r.Body), BuildResponse(r)
}
}
// Open Graph Metadata Section
// OpenGraph return the open graph metadata for a particular url if the site have the metadata
......
......@@ -1700,6 +1700,16 @@ func (s *MessageExportSettings) SetDefaults() {
}
}
type TimezoneSettings struct {
SupportedTimezonesPath *string
}
func (s *TimezoneSettings) SetDefaults() {
if s.SupportedTimezonesPath == nil {
s.SupportedTimezonesPath = NewString("timezones.json")
}
}
type ConfigFunc func() *Config
type Config struct {
......@@ -1733,6 +1743,7 @@ type Config struct {
MessageExportSettings MessageExportSettings
JobSettings JobSettings
PluginSettings PluginSettings
TimezoneSettings TimezoneSettings
}
func (o *Config) Clone() *Config {
......@@ -1802,6 +1813,7 @@ func (o *Config) SetDefaults() {
o.JobSettings.SetDefaults()
o.WebrtcSettings.SetDefaults()
o.MessageExportSettings.SetDefaults()
o.TimezoneSettings.SetDefaults()
}
func (o *Config) IsValid() *AppError {
......
This diff is collapsed.
......@@ -71,6 +71,7 @@ type User struct {
LastPictureUpdate int64 `json:"last_picture_update,omitempty"`
FailedAttempts int `json:"failed_attempts,omitempty"`
Locale string `json:"locale"`
Timezone StringMap `json:"timezone"`
MfaActive bool `json:"mfa_active,omitempty"`
MfaSecret string `json:"mfa_secret,omitempty"`
LastActivityAt int64 `db:"-" json:"last_activity_at,omitempty"`
......@@ -86,6 +87,7 @@ type UserPatch struct {
Props StringMap `json:"props,omitempty"`
NotifyProps StringMap `json:"notify_props,omitempty"`
Locale *string `json:"locale"`
Timezone StringMap `json:"timezone"`
}
type UserAuth struct {
......@@ -208,6 +210,10 @@ func (u *User) PreSave() {
u.SetDefaultNotifications()
}
if u.Timezone == nil {
u.Props = make(map[string]string)
}
if len(u.Password) > 0 {
u.Password = HashPassword(u.Password)
}
......@@ -302,6 +308,10 @@ func (u *User) Patch(patch *UserPatch) {
if patch.Locale != nil {
u.Locale = *patch.Locale
}
if patch.Timezone != nil {
u.Timezone = patch.Timezone
}
}
// ToJson convert a User to a json string
......
......@@ -4,6 +4,7 @@
package sqlstore
import (
"encoding/json"
"os"
"strings"
"time"
......@@ -376,6 +377,12 @@ func UpgradeDatabaseToVersion49(sqlStore SqlStore) {
//TODO: Uncomment the following condition when version 4.9.0 is released
//if shouldPerformUpgrade(sqlStore, VERSION_4_8_0, VERSION_4_9_0) {
sqlStore.CreateColumnIfNotExists("Teams", "LastTeamIconUpdate", "bigint", "bigint", "0")
defaultTimezone := model.DefaultUserTimezone()
defaultTimezoneValue, err := json.Marshal(defaultTimezone)
if err != nil {
l4g.Critical(err)
}
sqlStore.CreateColumnIfNotExists("Users", "Timezone", "varchar(256)", "varchar(256)", string(defaultTimezoneValue))
// saveSchemaVersion(sqlStore, VERSION_4_9_0)
//}
}
......@@ -79,6 +79,7 @@ func NewSqlUserStore(sqlStore SqlStore, metrics einterfaces.MetricsInterface) st
table.ColMap("Locale").SetMaxSize(5)
table.ColMap("MfaSecret").SetMaxSize(128)
table.ColMap("Position").SetMaxSize(128)
table.ColMap("Timezone").SetMaxSize(256)
}
return us
......
......@@ -21,6 +21,15 @@ func TestConfig(t *testing.T) {
InitTranslations(cfg.LocalizationSettings)
}
func TestTimezoneConfig(t *testing.T) {
TranslationsPreInit()
supportedTimezones := LoadTimezones("timezones.json")
assert.Equal(t, len(supportedTimezones) > 0, true)
supportedTimezones2 := LoadTimezones("timezones_file_does_not_exists.json")
assert.Equal(t, len(supportedTimezones2) > 0, true)
}
func TestFindConfigFile(t *testing.T) {
dir, err := ioutil.TempDir("", "")
require.NoError(t, err)
......
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package utils
import (
"encoding/json"
"io/ioutil"
"github.com/mattermost/mattermost-server/model"
)
func LoadTimezones(fileName string) model.SupportedTimezones {
var supportedTimezones model.SupportedTimezones
if timezoneFile := FindConfigFile(fileName); timezoneFile == "" {
return model.DefaultSupportedTimezones
} else if raw, err := ioutil.ReadFile(timezoneFile); err != nil {
return model.DefaultSupportedTimezones
} else if err := json.Unmarshal(raw, &supportedTimezones); err != nil {
return model.DefaultSupportedTimezones
} else {
return supportedTimezones
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment