preferencestore (#15018)
* Starting migration * Migration finished * Fix i18n * Fix some tests * Fix typos * Remove overwite of http status * Add i18n string * Fix i18n * fix breakages * fix tests Co-authored-by: Rodrigo Villablanca <villa061004@gmail.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
8f42177870
Коммит
83974d8a8d
@@ -3958,7 +3958,7 @@ func (s SqlChannelStore) UpdateSidebarCategories(userId, teamId string, categori
|
||||
for _, channelID := range category.Channels {
|
||||
// This breaks the PreferenceStore abstraction, but it should be safe to assume that everything is a SQL
|
||||
// store in this package.
|
||||
if err := s.Preference().(*SqlPreferenceStore).save(transaction, &model.Preference{
|
||||
if err = s.Preference().(*SqlPreferenceStore).save(transaction, &model.Preference{
|
||||
Name: channelID,
|
||||
UserId: userId,
|
||||
Category: model.PREFERENCE_CATEGORY_FAVORITE_CHANNEL,
|
||||
|
||||
@@ -4,10 +4,11 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/mattermost/gorp"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v5/mlog"
|
||||
"github.com/mattermost/mattermost-server/v5/model"
|
||||
"github.com/mattermost/mattermost-server/v5/store"
|
||||
@@ -51,14 +52,17 @@ func (s SqlPreferenceStore) deleteUnusedFeatures() {
|
||||
"Category": model.PREFERENCE_CATEGORY_ADVANCED_SETTINGS,
|
||||
"Value": "false",
|
||||
}
|
||||
s.GetMaster().Exec(sql, queryParams)
|
||||
_, err := s.GetMaster().Exec(sql, queryParams)
|
||||
if err != nil {
|
||||
mlog.Warn("Failed to delete unused features", mlog.Err(err))
|
||||
}
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) Save(preferences *model.Preferences) *model.AppError {
|
||||
func (s SqlPreferenceStore) Save(preferences *model.Preferences) error {
|
||||
// wrap in a transaction so that if one fails, everything fails
|
||||
transaction, err := s.GetMaster().Begin()
|
||||
if err != nil {
|
||||
return model.NewAppError("SqlPreferenceStore.Save", "store.sql_preference.save.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return errors.Wrap(err, "begin_transaction")
|
||||
}
|
||||
|
||||
defer finalizeTransaction(transaction)
|
||||
@@ -71,12 +75,12 @@ func (s SqlPreferenceStore) Save(preferences *model.Preferences) *model.AppError
|
||||
|
||||
if err := transaction.Commit(); err != nil {
|
||||
// don't need to rollback here since the transaction is already closed
|
||||
return model.NewAppError("SqlPreferenceStore.Save", "store.sql_preference.save.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return errors.Wrap(err, "commit_transaction")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *model.Preference) *model.AppError {
|
||||
func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *model.Preference) error {
|
||||
preference.PreUpdate()
|
||||
|
||||
if err := preference.IsValid(); err != nil {
|
||||
@@ -99,7 +103,7 @@ func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *mode
|
||||
(:UserId, :Category, :Name, :Value)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
Value = :Value`, params); err != nil {
|
||||
return model.NewAppError("SqlPreferenceStore.save", "store.sql_preference.save.updating.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return errors.Wrap(err, "failed to save Preference")
|
||||
}
|
||||
return nil
|
||||
} else if s.DriverName() == model.DATABASE_DRIVER_POSTGRES {
|
||||
@@ -114,7 +118,7 @@ func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *mode
|
||||
AND Category = :Category
|
||||
AND Name = :Name`, params)
|
||||
if err != nil {
|
||||
return model.NewAppError("SqlPreferenceStore.save", "store.sql_preference.save.updating.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return errors.Wrap(err, "failed to count Preferences")
|
||||
}
|
||||
|
||||
if count == 1 {
|
||||
@@ -122,32 +126,29 @@ func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *mode
|
||||
}
|
||||
return s.insert(transaction, preference)
|
||||
}
|
||||
return model.NewAppError("SqlPreferenceStore.save", "store.sql_preference.save.missing_driver.app_error", nil, "Failed to update preference because of missing driver", http.StatusNotImplemented)
|
||||
return store.NewErrNotImplemented("failed to update preference because of missing driver")
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) insert(transaction *gorp.Transaction, preference *model.Preference) *model.AppError {
|
||||
func (s SqlPreferenceStore) insert(transaction *gorp.Transaction, preference *model.Preference) error {
|
||||
if err := transaction.Insert(preference); err != nil {
|
||||
if IsUniqueConstraintError(err, []string{"UserId", "preferences_pkey"}) {
|
||||
return model.NewAppError("SqlPreferenceStore.insert", "store.sql_preference.insert.exists.app_error", nil,
|
||||
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error(), http.StatusBadRequest)
|
||||
return store.NewErrInvalidInput("Preference", "<userId, category, name>", fmt.Sprintf("<%s, %s, %s>", preference.UserId, preference.Category, preference.Name))
|
||||
}
|
||||
return model.NewAppError("SqlPreferenceStore.insert", "store.sql_preference.insert.save.app_error", nil,
|
||||
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error(), http.StatusInternalServerError)
|
||||
return errors.Wrapf(err, "failed to save Preference with userId=%s, category=%s, name=%s", preference.UserId, preference.Category, preference.Name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) update(transaction *gorp.Transaction, preference *model.Preference) *model.AppError {
|
||||
func (s SqlPreferenceStore) update(transaction *gorp.Transaction, preference *model.Preference) error {
|
||||
if _, err := transaction.Update(preference); err != nil {
|
||||
return model.NewAppError("SqlPreferenceStore.update", "store.sql_preference.update.app_error", nil,
|
||||
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error(), http.StatusInternalServerError)
|
||||
return errors.Wrapf(err, "failed to update Preference with userId=%s, category=%s, name=%s", preference.UserId, preference.Category, preference.Name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) Get(userId string, category string, name string) (*model.Preference, *model.AppError) {
|
||||
func (s SqlPreferenceStore) Get(userId string, category string, name string) (*model.Preference, error) {
|
||||
var preference *model.Preference
|
||||
|
||||
if err := s.GetReplica().SelectOne(&preference,
|
||||
@@ -159,12 +160,12 @@ func (s SqlPreferenceStore) Get(userId string, category string, name string) (*m
|
||||
UserId = :UserId
|
||||
AND Category = :Category
|
||||
AND Name = :Name`, map[string]interface{}{"UserId": userId, "Category": category, "Name": name}); err != nil {
|
||||
return nil, model.NewAppError("SqlPreferenceStore.Get", "store.sql_preference.get.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return nil, errors.Wrapf(err, "failed to find Preference with userId=%s, category=%s, name=%s", userId, category, name)
|
||||
}
|
||||
return preference, nil
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) GetCategory(userId string, category string) (model.Preferences, *model.AppError) {
|
||||
func (s SqlPreferenceStore) GetCategory(userId string, category string) (model.Preferences, error) {
|
||||
var preferences model.Preferences
|
||||
|
||||
if _, err := s.GetReplica().Select(&preferences,
|
||||
@@ -175,14 +176,14 @@ func (s SqlPreferenceStore) GetCategory(userId string, category string) (model.P
|
||||
WHERE
|
||||
UserId = :UserId
|
||||
AND Category = :Category`, map[string]interface{}{"UserId": userId, "Category": category}); err != nil {
|
||||
return nil, model.NewAppError("SqlPreferenceStore.GetCategory", "store.sql_preference.get_category.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return nil, errors.Wrapf(err, "failed to find Preferences with userId=%s and category=%s", userId, category)
|
||||
}
|
||||
|
||||
return preferences, nil
|
||||
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) GetAll(userId string) (model.Preferences, *model.AppError) {
|
||||
func (s SqlPreferenceStore) GetAll(userId string) (model.Preferences, error) {
|
||||
var preferences model.Preferences
|
||||
|
||||
if _, err := s.GetReplica().Select(&preferences,
|
||||
@@ -192,12 +193,12 @@ func (s SqlPreferenceStore) GetAll(userId string) (model.Preferences, *model.App
|
||||
Preferences
|
||||
WHERE
|
||||
UserId = :UserId`, map[string]interface{}{"UserId": userId}); err != nil {
|
||||
return nil, model.NewAppError("SqlPreferenceStore.GetAll", "store.sql_preference.get_all.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return nil, errors.Wrapf(err, "failed to find Preferences with userId=%s", userId)
|
||||
}
|
||||
return preferences, nil
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) PermanentDeleteByUser(userId string) *model.AppError {
|
||||
func (s SqlPreferenceStore) PermanentDeleteByUser(userId string) error {
|
||||
query :=
|
||||
`DELETE FROM
|
||||
Preferences
|
||||
@@ -205,13 +206,13 @@ func (s SqlPreferenceStore) PermanentDeleteByUser(userId string) *model.AppError
|
||||
UserId = :UserId`
|
||||
|
||||
if _, err := s.GetMaster().Exec(query, map[string]interface{}{"UserId": userId}); err != nil {
|
||||
return model.NewAppError("SqlPreferenceStore.Delete", "store.sql_preference.permanent_delete_by_user.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return errors.Wrapf(err, "failed to delete Preference with userId=%s", userId)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) Delete(userId, category, name string) *model.AppError {
|
||||
func (s SqlPreferenceStore) Delete(userId, category, name string) error {
|
||||
query :=
|
||||
`DELETE FROM Preferences
|
||||
WHERE
|
||||
@@ -222,13 +223,13 @@ func (s SqlPreferenceStore) Delete(userId, category, name string) *model.AppErro
|
||||
_, err := s.GetMaster().Exec(query, map[string]interface{}{"UserId": userId, "Category": category, "Name": name})
|
||||
|
||||
if err != nil {
|
||||
return model.NewAppError("SqlPreferenceStore.Delete", "store.sql_preference.delete.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return errors.Wrapf(err, "failed to delete Preference with userId=%s, category=%s and name=%s", userId, category, name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) DeleteCategory(userId string, category string) *model.AppError {
|
||||
func (s SqlPreferenceStore) DeleteCategory(userId string, category string) error {
|
||||
_, err := s.GetMaster().Exec(
|
||||
`DELETE FROM
|
||||
Preferences
|
||||
@@ -237,13 +238,13 @@ func (s SqlPreferenceStore) DeleteCategory(userId string, category string) *mode
|
||||
AND Category = :Category`, map[string]interface{}{"UserId": userId, "Category": category})
|
||||
|
||||
if err != nil {
|
||||
return model.NewAppError("SqlPreferenceStore.DeleteCategory", "store.sql_preference.delete.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return errors.Wrapf(err, "failed to delete Preference with userId=%s and category=%s", userId, category)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) DeleteCategoryAndName(category string, name string) *model.AppError {
|
||||
func (s SqlPreferenceStore) DeleteCategoryAndName(category string, name string) error {
|
||||
_, err := s.GetMaster().Exec(
|
||||
`DELETE FROM
|
||||
Preferences
|
||||
@@ -252,13 +253,13 @@ func (s SqlPreferenceStore) DeleteCategoryAndName(category string, name string)
|
||||
AND Category = :Category`, map[string]interface{}{"Name": name, "Category": category})
|
||||
|
||||
if err != nil {
|
||||
return model.NewAppError("SqlPreferenceStore.DeleteCategoryAndName", "store.sql_preference.delete.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return errors.Wrapf(err, "failed to delete Preference with category=%s and name=%s", category, name)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) CleanupFlagsBatch(limit int64) (int64, *model.AppError) {
|
||||
func (s SqlPreferenceStore) CleanupFlagsBatch(limit int64) (int64, error) {
|
||||
query :=
|
||||
`DELETE FROM
|
||||
Preferences
|
||||
@@ -287,12 +288,12 @@ func (s SqlPreferenceStore) CleanupFlagsBatch(limit int64) (int64, *model.AppErr
|
||||
|
||||
sqlResult, err := s.GetMaster().Exec(query, map[string]interface{}{"Category": model.PREFERENCE_CATEGORY_FLAGGED_POST, "Limit": limit})
|
||||
if err != nil {
|
||||
return int64(0), model.NewAppError("SqlPostStore.CleanupFlagsBatch", "store.sql_preference.cleanup_flags_batch.app_error", nil, ""+err.Error(), http.StatusInternalServerError)
|
||||
return int64(0), errors.Wrap(err, "failed to delete Preference")
|
||||
}
|
||||
|
||||
rowsAffected, err := sqlResult.RowsAffected()
|
||||
if err != nil {
|
||||
return int64(0), model.NewAppError("SqlPostStore.CleanupFlagsBatch", "store.sql_preference.cleanup_flags_batch.app_error", nil, ""+err.Error(), http.StatusInternalServerError)
|
||||
return int64(0), errors.Wrap(err, "unable to get rows affected")
|
||||
}
|
||||
|
||||
return rowsAffected, nil
|
||||
|
||||
Ссылка в новой задаче
Block a user