[MM-15297] Migrate "Preference.Get" to Sync by default #10721 (#10811)

* modify Get method in preference_store

* modify interface to match changes on preference store Get method

* modify variable initialization

* fix behavior on GetPreferenceByCategoryAndNameForUser
Этот коммит содержится в:
Ivana Irene Thomas
2019-05-15 19:20:26 +07:00
коммит произвёл Miguel de la Cruz
родитель b3e09715df
Коммит a68ad55151
8 изменённых файлов: 56 добавлений и 52 удалений

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

@@ -171,13 +171,11 @@ func (job *EmailBatchingJob) checkPendingNotifications(now time.Time, handler fu
// get how long we need to wait to send notifications to the user // get how long we need to wait to send notifications to the user
var interval int64 var interval int64
pchan := job.server.Store.Preference().Get(userId, model.PREFERENCE_CATEGORY_NOTIFICATIONS, model.PREFERENCE_NAME_EMAIL_INTERVAL) preference, err := job.server.Store.Preference().Get(userId, model.PREFERENCE_CATEGORY_NOTIFICATIONS, model.PREFERENCE_NAME_EMAIL_INTERVAL)
if result := <-pchan; result.Err != nil { if err != nil {
// use the default batching interval if an error ocurrs while fetching user preferences // use the default batching interval if an error ocurrs while fetching user preferences
interval, _ = strconv.ParseInt(model.PREFERENCE_EMAIL_INTERVAL_BATCHING_SECONDS, 10, 64) interval, _ = strconv.ParseInt(model.PREFERENCE_EMAIL_INTERVAL_BATCHING_SECONDS, 10, 64)
} else { } else {
preference := result.Data.(model.Preference)
if value, err := strconv.ParseInt(preference.Value, 10, 64); err != nil { if value, err := strconv.ParseInt(preference.Value, 10, 64); err != nil {
// // use the default batching interval if an error ocurrs while deserializing user preferences // // use the default batching interval if an error ocurrs while deserializing user preferences
interval, _ = strconv.ParseInt(model.PREFERENCE_EMAIL_INTERVAL_BATCHING_SECONDS, 10, 64) interval, _ = strconv.ParseInt(model.PREFERENCE_EMAIL_INTERVAL_BATCHING_SECONDS, 10, 64)

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

@@ -48,12 +48,12 @@ func (a *App) sendNotificationEmail(notification *postNotification, user *model.
if *a.Config().EmailSettings.EnableEmailBatching { if *a.Config().EmailSettings.EnableEmailBatching {
var sendBatched bool var sendBatched bool
if result := <-a.Srv.Store.Preference().Get(user.Id, model.PREFERENCE_CATEGORY_NOTIFICATIONS, model.PREFERENCE_NAME_EMAIL_INTERVAL); result.Err != nil { if data, err := a.Srv.Store.Preference().Get(user.Id, model.PREFERENCE_CATEGORY_NOTIFICATIONS, model.PREFERENCE_NAME_EMAIL_INTERVAL); err != nil {
// if the call fails, assume that the interval has not been explicitly set and batch the notifications // if the call fails, assume that the interval has not been explicitly set and batch the notifications
sendBatched = true sendBatched = true
} else { } else {
// if the user has chosen to receive notifications immediately, don't batch them // if the user has chosen to receive notifications immediately, don't batch them
sendBatched = result.Data.(model.Preference).Value != model.PREFERENCE_EMAIL_INTERVAL_NO_BATCHING_SECONDS sendBatched = data.Value != model.PREFERENCE_EMAIL_INTERVAL_NO_BATCHING_SECONDS
} }
if sendBatched { if sendBatched {
@@ -68,17 +68,17 @@ func (a *App) sendNotificationEmail(notification *postNotification, user *model.
translateFunc := utils.GetUserTranslations(user.Locale) translateFunc := utils.GetUserTranslations(user.Locale)
var useMilitaryTime bool var useMilitaryTime bool
if result := <-a.Srv.Store.Preference().Get(user.Id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS, model.PREFERENCE_NAME_USE_MILITARY_TIME); result.Err != nil { if data, err := a.Srv.Store.Preference().Get(user.Id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS, model.PREFERENCE_NAME_USE_MILITARY_TIME); err != nil {
useMilitaryTime = true useMilitaryTime = true
} else { } else {
useMilitaryTime = result.Data.(model.Preference).Value == "true" useMilitaryTime = data.Value == "true"
} }
var nameFormat string var nameFormat string
if result := <-a.Srv.Store.Preference().Get(user.Id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS, model.PREFERENCE_NAME_NAME_FORMAT); result.Err != nil { if data, err := a.Srv.Store.Preference().Get(user.Id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS, model.PREFERENCE_NAME_NAME_FORMAT); err != nil {
nameFormat = *a.Config().TeamSettings.TeammateNameDisplay nameFormat = *a.Config().TeamSettings.TeammateNameDisplay
} else { } else {
nameFormat = result.Data.(model.Preference).Value nameFormat = data.Value
} }
channelName := notification.GetChannelName(nameFormat, "") channelName := notification.GetChannelName(nameFormat, "")

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

@@ -5,11 +5,12 @@ package app
import ( import (
"fmt" "fmt"
"github.com/pkg/errors"
"hash/fnv" "hash/fnv"
"net/http" "net/http"
"strings" "strings"
"github.com/pkg/errors"
"github.com/mattermost/go-i18n/i18n" "github.com/mattermost/go-i18n/i18n"
"github.com/mattermost/mattermost-server/mlog" "github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
@@ -151,10 +152,10 @@ func (a *App) sendPushNotification(notification *postNotification, user *model.U
post := notification.post post := notification.post
var nameFormat string var nameFormat string
if result := <-a.Srv.Store.Preference().Get(user.Id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS, model.PREFERENCE_NAME_NAME_FORMAT); result.Err != nil { if data, err := a.Srv.Store.Preference().Get(user.Id, model.PREFERENCE_CATEGORY_DISPLAY_SETTINGS, model.PREFERENCE_NAME_NAME_FORMAT); err != nil {
nameFormat = *a.Config().TeamSettings.TeammateNameDisplay nameFormat = *a.Config().TeamSettings.TeammateNameDisplay
} else { } else {
nameFormat = result.Data.(model.Preference).Value nameFormat = data.Value
} }
channelName := notification.GetChannelName(nameFormat, user.Id) channelName := notification.GetChannelName(nameFormat, user.Id)

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

@@ -32,13 +32,12 @@ func (a *App) GetPreferenceByCategoryForUser(userId string, category string) (mo
} }
func (a *App) GetPreferenceByCategoryAndNameForUser(userId string, category string, preferenceName string) (*model.Preference, *model.AppError) { func (a *App) GetPreferenceByCategoryAndNameForUser(userId string, category string, preferenceName string) (*model.Preference, *model.AppError) {
result := <-a.Srv.Store.Preference().Get(userId, category, preferenceName) res, err := a.Srv.Store.Preference().Get(userId, category, preferenceName)
if result.Err != nil { if err != nil {
result.Err.StatusCode = http.StatusBadRequest err.StatusCode = http.StatusBadRequest
return nil, result.Err return nil, err
} }
data := result.Data.(model.Preference) return res, nil
return &data, nil
} }
func (a *App) UpdatePreferences(userId string, preferences model.Preferences) *model.AppError { func (a *App) UpdatePreferences(userId string, preferences model.Preferences) *model.AppError {

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

@@ -163,24 +163,21 @@ func (s SqlPreferenceStore) update(transaction *gorp.Transaction, preference *mo
return result return result
} }
func (s SqlPreferenceStore) Get(userId string, category string, name string) store.StoreChannel { func (s SqlPreferenceStore) Get(userId string, category string, name string) (*model.Preference, *model.AppError) {
return store.Do(func(result *store.StoreResult) { var preference *model.Preference
var preference model.Preference
if err := s.GetReplica().SelectOne(&preference, if err := s.GetReplica().SelectOne(&preference,
`SELECT `SELECT
* *
FROM FROM
Preferences Preferences
WHERE WHERE
UserId = :UserId UserId = :UserId
AND Category = :Category AND Category = :Category
AND Name = :Name`, map[string]interface{}{"UserId": userId, "Category": category, "Name": name}); err != nil { AND Name = :Name`, map[string]interface{}{"UserId": userId, "Category": category, "Name": name}); err != nil {
result.Err = model.NewAppError("SqlPreferenceStore.Get", "store.sql_preference.get.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlPreferenceStore.Get", "store.sql_preference.get.app_error", nil, err.Error(), http.StatusInternalServerError)
} else { }
result.Data = preference return preference, nil
}
})
} }
func (s SqlPreferenceStore) GetCategory(userId string, category string) store.StoreChannel { func (s SqlPreferenceStore) GetCategory(userId string, category string) store.StoreChannel {

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

@@ -429,7 +429,7 @@ type CommandWebhookStore interface {
type PreferenceStore interface { type PreferenceStore interface {
Save(preferences *model.Preferences) StoreChannel Save(preferences *model.Preferences) StoreChannel
Get(userId string, category string, name string) StoreChannel Get(userId string, category string, name string) (*model.Preference, *model.AppError)
GetCategory(userId string, category string) StoreChannel GetCategory(userId string, category string) StoreChannel
GetAll(userId string) StoreChannel GetAll(userId string) StoreChannel
Delete(userId, category, name string) StoreChannel Delete(userId, category, name string) StoreChannel

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

@@ -78,19 +78,28 @@ func (_m *PreferenceStore) DeleteCategoryAndName(category string, name string) s
} }
// Get provides a mock function with given fields: userId, category, name // Get provides a mock function with given fields: userId, category, name
func (_m *PreferenceStore) Get(userId string, category string, name string) store.StoreChannel { func (_m *PreferenceStore) Get(userId string, category string, name string) (*model.Preference, *model.AppError) {
ret := _m.Called(userId, category, name) ret := _m.Called(userId, category, name)
var r0 store.StoreChannel var r0 *model.Preference
if rf, ok := ret.Get(0).(func(string, string, string) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string, string, string) *model.Preference); ok {
r0 = rf(userId, category, name) r0 = rf(userId, category, name)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.Preference)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, string, string) *model.AppError); ok {
r1 = rf(userId, category, name)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// GetAll provides a mock function with given fields: userId // GetAll provides a mock function with given fields: userId

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

@@ -47,7 +47,7 @@ func testPreferenceSave(t *testing.T, ss store.Store) {
} }
for _, preference := range preferences { for _, preference := range preferences {
if data := store.Must(ss.Preference().Get(preference.UserId, preference.Category, preference.Name)).(model.Preference); preference != data { if data, _ := ss.Preference().Get(preference.UserId, preference.Category, preference.Name); preference.ToJson() != data.ToJson() {
t.Fatal("got incorrect preference after first Save") t.Fatal("got incorrect preference after first Save")
} }
} }
@@ -59,7 +59,7 @@ func testPreferenceSave(t *testing.T, ss store.Store) {
} }
for _, preference := range preferences { for _, preference := range preferences {
if data := store.Must(ss.Preference().Get(preference.UserId, preference.Category, preference.Name)).(model.Preference); preference != data { if data, _ := ss.Preference().Get(preference.UserId, preference.Category, preference.Name); preference.ToJson() != data.ToJson() {
t.Fatal("got incorrect preference after second Save") t.Fatal("got incorrect preference after second Save")
} }
} }
@@ -95,14 +95,14 @@ func testPreferenceGet(t *testing.T, ss store.Store) {
store.Must(ss.Preference().Save(&preferences)) store.Must(ss.Preference().Save(&preferences))
if result := <-ss.Preference().Get(userId, category, name); result.Err != nil { if data, err := ss.Preference().Get(userId, category, name); err != nil {
t.Fatal(result.Err) t.Fatal(err)
} else if data := result.Data.(model.Preference); data != preferences[0] { } else if data.ToJson() != preferences[0].ToJson() {
t.Fatal("got incorrect preference") t.Fatal("got incorrect preference")
} }
// make sure getting a missing preference fails // make sure getting a missing preference fails
if result := <-ss.Preference().Get(model.NewId(), model.NewId(), model.NewId()); result.Err == nil { if _, err := ss.Preference().Get(model.NewId(), model.NewId(), model.NewId()); err == nil {
t.Fatal("no error on getting a missing preference") t.Fatal("no error on getting a missing preference")
} }
} }
@@ -439,9 +439,9 @@ func testPreferenceCleanupFlagsBatch(t *testing.T, ss store.Store) {
result := <-ss.Preference().CleanupFlagsBatch(10000) result := <-ss.Preference().CleanupFlagsBatch(10000)
assert.Nil(t, result.Err) assert.Nil(t, result.Err)
result = <-ss.Preference().Get(userId, category, preference1.Name) _, err := ss.Preference().Get(userId, category, preference1.Name)
assert.Nil(t, result.Err) assert.Nil(t, err)
result = <-ss.Preference().Get(userId, category, preference2.Name) _, err = ss.Preference().Get(userId, category, preference2.Name)
assert.NotNil(t, result.Err) assert.NotNil(t, err)
} }