PLT-7 adding loc for db calls audits and prefs

Этот коммит содержится в:
=Corey Hulen
2016-01-20 07:59:56 -06:00
родитель 1acd38b7b1
Коммит f5eb3c1bcb
15 изменённых файлов: 120 добавлений и 108 удалений

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

@@ -692,7 +692,7 @@ func updateLastViewedAt(c *Context, w http.ResponseWriter, r *http.Request) {
Value: id, Value: id,
} }
Srv.Store.Preference().Save(&model.Preferences{preference}) Srv.Store.Preference().Save(c.T, &model.Preferences{preference})
message := model.NewMessage(c.Session.TeamId, id, c.Session.UserId, model.ACTION_CHANNEL_VIEWED) message := model.NewMessage(c.Session.TeamId, id, c.Session.UserId, model.ACTION_CHANNEL_VIEWED)
message.Add("channel_id", id) message.Add("channel_id", id)

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

@@ -233,7 +233,7 @@ func GetProtocol(r *http.Request) string {
func (c *Context) LogAudit(extraInfo string) { func (c *Context) LogAudit(extraInfo string) {
audit := &model.Audit{UserId: c.Session.UserId, IpAddress: c.IpAddress, Action: c.Path, ExtraInfo: extraInfo, SessionId: c.Session.Id} audit := &model.Audit{UserId: c.Session.UserId, IpAddress: c.IpAddress, Action: c.Path, ExtraInfo: extraInfo, SessionId: c.Session.Id}
if r := <-Srv.Store.Audit().Save(audit); r.Err != nil { if r := <-Srv.Store.Audit().Save(c.T, audit); r.Err != nil {
c.LogError(r.Err) c.LogError(r.Err)
} }
} }
@@ -245,7 +245,7 @@ func (c *Context) LogAuditWithUserId(userId, extraInfo string) {
} }
audit := &model.Audit{UserId: userId, IpAddress: c.IpAddress, Action: c.Path, ExtraInfo: extraInfo, SessionId: c.Session.Id} audit := &model.Audit{UserId: userId, IpAddress: c.IpAddress, Action: c.Path, ExtraInfo: extraInfo, SessionId: c.Session.Id}
if r := <-Srv.Store.Audit().Save(audit); r.Err != nil { if r := <-Srv.Store.Audit().Save(c.T, audit); r.Err != nil {
c.LogError(r.Err) c.LogError(r.Err)
} }
} }

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

@@ -5,11 +5,6 @@ package api
import ( import (
"fmt" "fmt"
l4g "github.com/alecthomas/log4go"
"github.com/gorilla/mux"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/store"
"github.com/mattermost/platform/utils"
"net/http" "net/http"
"net/url" "net/url"
"path/filepath" "path/filepath"
@@ -17,6 +12,13 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
l4g "github.com/alecthomas/log4go"
"github.com/gorilla/mux"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/store"
"github.com/mattermost/platform/utils"
goi18n "github.com/nicksnyder/go-i18n/i18n"
) )
func InitPost(r *mux.Router) { func InitPost(r *mux.Router) {
@@ -238,12 +240,12 @@ func handlePostEventsAndForget(c *Context, post *model.Post, triggerWebhooks boo
} }
if channel.Type == model.CHANNEL_DIRECT { if channel.Type == model.CHANNEL_DIRECT {
go makeDirectChannelVisible(c.Session.TeamId, post.ChannelId) go makeDirectChannelVisible(c.T, c.Session.TeamId, post.ChannelId)
} }
}() }()
} }
func makeDirectChannelVisible(teamId string, channelId string) { func makeDirectChannelVisible(T goi18n.TranslateFunc, teamId string, channelId string) {
var members []model.ChannelMember var members []model.ChannelMember
if result := <-Srv.Store.Channel().GetMembers(channelId); result.Err != nil { if result := <-Srv.Store.Channel().GetMembers(channelId); result.Err != nil {
l4g.Error("Failed to get channel members channel_id=%v err=%v", channelId, result.Err.Message) l4g.Error("Failed to get channel members channel_id=%v err=%v", channelId, result.Err.Message)
@@ -261,7 +263,7 @@ func makeDirectChannelVisible(teamId string, channelId string) {
for i, member := range members { for i, member := range members {
otherUserId := members[1-i].UserId otherUserId := members[1-i].UserId
if result := <-Srv.Store.Preference().Get(member.UserId, model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, otherUserId); result.Err != nil { if result := <-Srv.Store.Preference().Get(T, member.UserId, model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, otherUserId); result.Err != nil {
// create a new preference since one doesn't exist yet // create a new preference since one doesn't exist yet
preference := &model.Preference{ preference := &model.Preference{
UserId: member.UserId, UserId: member.UserId,
@@ -270,7 +272,7 @@ func makeDirectChannelVisible(teamId string, channelId string) {
Value: "true", Value: "true",
} }
if saveResult := <-Srv.Store.Preference().Save(&model.Preferences{*preference}); saveResult.Err != nil { if saveResult := <-Srv.Store.Preference().Save(T, &model.Preferences{*preference}); saveResult.Err != nil {
l4g.Error("Failed to save direct channel preference user_id=%v other_user_id=%v err=%v", member.UserId, otherUserId, saveResult.Err.Message) l4g.Error("Failed to save direct channel preference user_id=%v other_user_id=%v err=%v", member.UserId, otherUserId, saveResult.Err.Message)
} else { } else {
message := model.NewMessage(teamId, channelId, member.UserId, model.ACTION_PREFERENCE_CHANGED) message := model.NewMessage(teamId, channelId, member.UserId, model.ACTION_PREFERENCE_CHANGED)
@@ -285,7 +287,7 @@ func makeDirectChannelVisible(teamId string, channelId string) {
// update the existing preference to make the channel visible // update the existing preference to make the channel visible
preference.Value = "true" preference.Value = "true"
if updateResult := <-Srv.Store.Preference().Save(&model.Preferences{preference}); updateResult.Err != nil { if updateResult := <-Srv.Store.Preference().Save(T, &model.Preferences{preference}); updateResult.Err != nil {
l4g.Error("Failed to update direct channel preference user_id=%v other_user_id=%v err=%v", member.UserId, otherUserId, updateResult.Err.Message) l4g.Error("Failed to update direct channel preference user_id=%v other_user_id=%v err=%v", member.UserId, otherUserId, updateResult.Err.Message)
} else { } else {
message := model.NewMessage(teamId, channelId, member.UserId, model.ACTION_PREFERENCE_CHANGED) message := model.NewMessage(teamId, channelId, member.UserId, model.ACTION_PREFERENCE_CHANGED)

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

@@ -837,7 +837,7 @@ func TestMakeDirectChannelVisible(t *testing.T) {
channel := Client.Must(Client.CreateDirectChannel(map[string]string{"user_id": user2.Id})).Data.(*model.Channel) channel := Client.Must(Client.CreateDirectChannel(map[string]string{"user_id": user2.Id})).Data.(*model.Channel)
makeDirectChannelVisible(team.Id, channel.Id) makeDirectChannelVisible(utils.T, team.Id, channel.Id)
if result, err := Client.GetPreference(model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, user2.Id); err != nil { if result, err := Client.GetPreference(model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW, user2.Id); err != nil {
t.Fatal("Errored trying to set direct channel to be visible for user1") t.Fatal("Errored trying to set direct channel to be visible for user1")

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

@@ -21,7 +21,7 @@ func InitPreference(r *mux.Router) {
} }
func getAllPreferences(c *Context, w http.ResponseWriter, r *http.Request) { func getAllPreferences(c *Context, w http.ResponseWriter, r *http.Request) {
if result := <-Srv.Store.Preference().GetAll(c.Session.UserId); result.Err != nil { if result := <-Srv.Store.Preference().GetAll(c.T, c.Session.UserId); result.Err != nil {
c.Err = result.Err c.Err = result.Err
} else { } else {
data := result.Data.(model.Preferences) data := result.Data.(model.Preferences)
@@ -46,7 +46,7 @@ func savePreferences(c *Context, w http.ResponseWriter, r *http.Request) {
} }
} }
if result := <-Srv.Store.Preference().Save(&preferences); result.Err != nil { if result := <-Srv.Store.Preference().Save(c.T, &preferences); result.Err != nil {
c.Err = result.Err c.Err = result.Err
return return
} }
@@ -58,7 +58,7 @@ func getPreferenceCategory(c *Context, w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r) params := mux.Vars(r)
category := params["category"] category := params["category"]
if result := <-Srv.Store.Preference().GetCategory(c.Session.UserId, category); result.Err != nil { if result := <-Srv.Store.Preference().GetCategory(c.T, c.Session.UserId, category); result.Err != nil {
c.Err = result.Err c.Err = result.Err
} else { } else {
data := result.Data.(model.Preferences) data := result.Data.(model.Preferences)
@@ -72,7 +72,7 @@ func getPreference(c *Context, w http.ResponseWriter, r *http.Request) {
category := params["category"] category := params["category"]
name := params["name"] name := params["name"]
if result := <-Srv.Store.Preference().Get(c.Session.UserId, category, name); result.Err != nil { if result := <-Srv.Store.Preference().Get(c.T, c.Session.UserId, category, name); result.Err != nil {
c.Err = result.Err c.Err = result.Err
} else { } else {
data := result.Data.(model.Preference) data := result.Data.(model.Preference)

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

@@ -6,16 +6,17 @@ package api
import ( import (
"bytes" "bytes"
"fmt" "fmt"
l4g "github.com/alecthomas/log4go"
"github.com/gorilla/mux"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/store"
"github.com/mattermost/platform/utils"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
"time" "time"
l4g "github.com/alecthomas/log4go"
"github.com/gorilla/mux"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/store"
"github.com/mattermost/platform/utils"
) )
func InitTeam(r *mux.Router) { func InitTeam(r *mux.Router) {
@@ -222,7 +223,7 @@ func createTeamFromSignup(c *Context, w http.ResponseWriter, r *http.Request) {
teamSignup.User.TeamId = rteam.Id teamSignup.User.TeamId = rteam.Id
teamSignup.User.EmailVerified = true teamSignup.User.EmailVerified = true
ruser, err := CreateUser(rteam, &teamSignup.User) ruser, err := CreateUser(c.T, rteam, &teamSignup.User)
if err != nil { if err != nil {
c.Err = err c.Err = err
return return

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

@@ -7,15 +7,6 @@ import (
"bytes" "bytes"
b64 "encoding/base64" b64 "encoding/base64"
"fmt" "fmt"
l4g "github.com/alecthomas/log4go"
"github.com/disintegration/imaging"
"github.com/golang/freetype"
"github.com/gorilla/mux"
"github.com/mattermost/platform/einterfaces"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/store"
"github.com/mattermost/platform/utils"
"github.com/mssola/user_agent"
"hash/fnv" "hash/fnv"
"image" "image"
"image/color" "image/color"
@@ -29,6 +20,17 @@ import (
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
l4g "github.com/alecthomas/log4go"
"github.com/disintegration/imaging"
"github.com/golang/freetype"
"github.com/gorilla/mux"
"github.com/mattermost/platform/einterfaces"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/store"
"github.com/mattermost/platform/utils"
"github.com/mssola/user_agent"
goi18n "github.com/nicksnyder/go-i18n/i18n"
) )
func InitUser(r *mux.Router) { func InitUser(r *mux.Router) {
@@ -127,14 +129,14 @@ func createUser(c *Context, w http.ResponseWriter, r *http.Request) {
return return
} }
ruser, err := CreateUser(team, user) ruser, err := CreateUser(c.T, team, user)
if err != nil { if err != nil {
c.Err = err c.Err = err
return return
} }
if sendWelcomeEmail { if sendWelcomeEmail {
sendWelcomeEmailAndForget(ruser.Id, ruser.Email, team.Name, team.DisplayName, c.GetSiteURL(), c.GetTeamURLFromTeam(team), ruser.EmailVerified) sendWelcomeEmailAndForget(c.T, ruser.Id, ruser.Email, team.Name, team.DisplayName, c.GetSiteURL(), c.GetTeamURLFromTeam(team), ruser.EmailVerified)
} }
w.Write([]byte(ruser.ToJson())) w.Write([]byte(ruser.ToJson()))
@@ -183,7 +185,7 @@ func IsVerifyHashRequired(user *model.User, team *model.Team, hash string) bool
return shouldVerifyHash return shouldVerifyHash
} }
func CreateUser(team *model.Team, user *model.User) (*model.User, *model.AppError) { func CreateUser(T goi18n.TranslateFunc, team *model.Team, user *model.User) (*model.User, *model.AppError) {
channelRole := "" channelRole := ""
if team.Email == user.Email { if team.Email == user.Email {
@@ -218,7 +220,7 @@ func CreateUser(team *model.Team, user *model.User) (*model.User, *model.AppErro
l4g.Error("Encountered an issue joining default channels user_id=%s, team_id=%s, err=%v", ruser.Id, ruser.TeamId, err) l4g.Error("Encountered an issue joining default channels user_id=%s, team_id=%s, err=%v", ruser.Id, ruser.TeamId, err)
} }
addDirectChannelsAndForget(ruser) addDirectChannelsAndForget(T, ruser)
if user.EmailVerified { if user.EmailVerified {
if cresult := <-Srv.Store.User().VerifyEmail(ruser.Id); cresult.Err != nil { if cresult := <-Srv.Store.User().VerifyEmail(ruser.Id); cresult.Err != nil {
@@ -227,7 +229,7 @@ func CreateUser(team *model.Team, user *model.User) (*model.User, *model.AppErro
} }
pref := model.Preference{UserId: ruser.Id, Category: model.PREFERENCE_CATEGORY_TUTORIAL_STEPS, Name: ruser.Id, Value: "0"} pref := model.Preference{UserId: ruser.Id, Category: model.PREFERENCE_CATEGORY_TUTORIAL_STEPS, Name: ruser.Id, Value: "0"}
if presult := <-Srv.Store.Preference().Save(&model.Preferences{pref}); presult.Err != nil { if presult := <-Srv.Store.Preference().Save(T, &model.Preferences{pref}); presult.Err != nil {
l4g.Error("Encountered error saving tutorial preference, err=%v", presult.Err.Message) l4g.Error("Encountered error saving tutorial preference, err=%v", presult.Err.Message)
} }
@@ -292,7 +294,7 @@ func CreateOAuthUser(c *Context, w http.ResponseWriter, r *http.Request, service
user.TeamId = team.Id user.TeamId = team.Id
user.EmailVerified = true user.EmailVerified = true
ruser, err := CreateUser(team, user) ruser, err := CreateUser(c.T, team, user)
if err != nil { if err != nil {
c.Err = err c.Err = err
return nil return nil
@@ -306,7 +308,7 @@ func CreateOAuthUser(c *Context, w http.ResponseWriter, r *http.Request, service
return ruser return ruser
} }
func sendWelcomeEmailAndForget(userId, email, teamName, teamDisplayName, siteURL, teamURL string, verified bool) { func sendWelcomeEmailAndForget(T goi18n.TranslateFunc, userId, email, teamName, teamDisplayName, siteURL, teamURL string, verified bool) {
go func() { go func() {
subjectPage := NewServerTemplatePage("welcome_subject") subjectPage := NewServerTemplatePage("welcome_subject")
@@ -326,7 +328,7 @@ func sendWelcomeEmailAndForget(userId, email, teamName, teamDisplayName, siteURL
}() }()
} }
func addDirectChannelsAndForget(user *model.User) { func addDirectChannelsAndForget(T goi18n.TranslateFunc, user *model.User) {
go func() { go func() {
var profiles map[string]*model.User var profiles map[string]*model.User
if result := <-Srv.Store.User().GetProfiles(user.TeamId); result.Err != nil { if result := <-Srv.Store.User().GetProfiles(user.TeamId); result.Err != nil {
@@ -359,7 +361,7 @@ func addDirectChannelsAndForget(user *model.User) {
} }
} }
if result := <-Srv.Store.Preference().Save(&preferences); result.Err != nil { if result := <-Srv.Store.Preference().Save(T, &preferences); result.Err != nil {
l4g.Error("Failed to add direct channel preferences for new user user_id=%s, eam_id=%s, err=%v", user.Id, user.TeamId, result.Err.Error()) l4g.Error("Failed to add direct channel preferences for new user user_id=%s, eam_id=%s, err=%v", user.Id, user.TeamId, result.Err.Error())
} }
}() }()
@@ -883,7 +885,7 @@ func getAudits(c *Context, w http.ResponseWriter, r *http.Request) {
} }
userChan := Srv.Store.User().Get(id) userChan := Srv.Store.User().Get(id)
auditChan := Srv.Store.Audit().Get(id, 20) auditChan := Srv.Store.Audit().Get(c.T, id, 20)
if c.Err = (<-userChan).Err; c.Err != nil { if c.Err = (<-userChan).Err; c.Err != nil {
return return
@@ -1450,7 +1452,7 @@ func PermanentDeleteUser(c *Context, user *model.User) *model.AppError {
return result.Err return result.Err
} }
if result := <-Srv.Store.Preference().PermanentDeleteByUser(user.Id); result.Err != nil { if result := <-Srv.Store.Preference().PermanentDeleteByUser(c.T, user.Id); result.Err != nil {
return result.Err return result.Err
} }
@@ -1466,7 +1468,7 @@ func PermanentDeleteUser(c *Context, user *model.User) *model.AppError {
return result.Err return result.Err
} }
if result := <-Srv.Store.Audit().PermanentDeleteByUser(user.Id); result.Err != nil { if result := <-Srv.Store.Audit().PermanentDeleteByUser(c.T, user.Id); result.Err != nil {
return result.Err return result.Err
} }

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

@@ -309,7 +309,7 @@ func cmdCreateUser() {
user.TeamId = team.Id user.TeamId = team.Id
} }
_, err := api.CreateUser(team, user) _, err := api.CreateUser(utils.T, team, user)
if err != nil { if err != nil {
if err.Message != "An account with that email already exists." { if err.Message != "An account with that email already exists." {
l4g.Error("%v", err) l4g.Error("%v", err)

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

@@ -5,6 +5,7 @@ package store
import ( import (
"github.com/mattermost/platform/model" "github.com/mattermost/platform/model"
goi18n "github.com/nicksnyder/go-i18n/i18n"
) )
type SqlAuditStore struct { type SqlAuditStore struct {
@@ -34,7 +35,7 @@ func (s SqlAuditStore) CreateIndexesIfNotExists() {
s.CreateIndexIfNotExists("idx_audits_user_id", "Audits", "UserId") s.CreateIndexIfNotExists("idx_audits_user_id", "Audits", "UserId")
} }
func (s SqlAuditStore) Save(audit *model.Audit) StoreChannel { func (s SqlAuditStore) Save(T goi18n.TranslateFunc, audit *model.Audit) StoreChannel {
storeChannel := make(StoreChannel) storeChannel := make(StoreChannel)
@@ -57,7 +58,7 @@ func (s SqlAuditStore) Save(audit *model.Audit) StoreChannel {
return storeChannel return storeChannel
} }
func (s SqlAuditStore) Get(user_id string, limit int) StoreChannel { func (s SqlAuditStore) Get(T goi18n.TranslateFunc, user_id string, limit int) StoreChannel {
storeChannel := make(StoreChannel) storeChannel := make(StoreChannel)
@@ -87,7 +88,7 @@ func (s SqlAuditStore) Get(user_id string, limit int) StoreChannel {
return storeChannel return storeChannel
} }
func (s SqlAuditStore) PermanentDeleteByUser(userId string) StoreChannel { func (s SqlAuditStore) PermanentDeleteByUser(T goi18n.TranslateFunc, userId string) StoreChannel {
storeChannel := make(StoreChannel) storeChannel := make(StoreChannel)

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

@@ -5,6 +5,7 @@ package store
import ( import (
"github.com/mattermost/platform/model" "github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
"testing" "testing"
"time" "time"
) )
@@ -13,19 +14,19 @@ func TestSqlAuditStore(t *testing.T) {
Setup() Setup()
audit := &model.Audit{UserId: model.NewId(), IpAddress: "ipaddress", Action: "Action"} audit := &model.Audit{UserId: model.NewId(), IpAddress: "ipaddress", Action: "Action"}
Must(store.Audit().Save(audit)) Must(store.Audit().Save(utils.T, audit))
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
Must(store.Audit().Save(audit)) Must(store.Audit().Save(utils.T, audit))
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
Must(store.Audit().Save(audit)) Must(store.Audit().Save(utils.T, audit))
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
audit.ExtraInfo = "extra" audit.ExtraInfo = "extra"
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
Must(store.Audit().Save(audit)) Must(store.Audit().Save(utils.T, audit))
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
c := store.Audit().Get(audit.UserId, 100) c := store.Audit().Get(utils.T, audit.UserId, 100)
result := <-c result := <-c
audits := result.Data.(model.Audits) audits := result.Data.(model.Audits)
@@ -37,7 +38,7 @@ func TestSqlAuditStore(t *testing.T) {
t.Fatal("Failed to save property for extra info") t.Fatal("Failed to save property for extra info")
} }
c = store.Audit().Get("missing", 100) c = store.Audit().Get(utils.T, "missing", 100)
result = <-c result = <-c
audits = result.Data.(model.Audits) audits = result.Data.(model.Audits)
@@ -45,7 +46,7 @@ func TestSqlAuditStore(t *testing.T) {
t.Fatal("Should have returned empty because user_id is missing") t.Fatal("Should have returned empty because user_id is missing")
} }
if r2 := <-store.Audit().PermanentDeleteByUser(audit.UserId); r2.Err != nil { if r2 := <-store.Audit().PermanentDeleteByUser(utils.T, audit.UserId); r2.Err != nil {
t.Fatal(r2.Err) t.Fatal(r2.Err)
} }
} }

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

@@ -8,6 +8,7 @@ import (
"github.com/go-gorp/gorp" "github.com/go-gorp/gorp"
"github.com/mattermost/platform/model" "github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils" "github.com/mattermost/platform/utils"
goi18n "github.com/nicksnyder/go-i18n/i18n"
) )
type SqlPreferenceStore struct { type SqlPreferenceStore struct {
@@ -41,7 +42,7 @@ func (s SqlPreferenceStore) CreateIndexesIfNotExists() {
s.CreateIndexIfNotExists("idx_preferences_name", "Preferences", "Name") s.CreateIndexIfNotExists("idx_preferences_name", "Preferences", "Name")
} }
func (s SqlPreferenceStore) DeleteUnusedFeatures() { func (s SqlPreferenceStore) DeleteUnusedFeatures(T goi18n.TranslateFunc) {
l4g.Debug("Deleting any unused pre-release features") l4g.Debug("Deleting any unused pre-release features")
sql := `DELETE sql := `DELETE
@@ -58,7 +59,7 @@ func (s SqlPreferenceStore) DeleteUnusedFeatures() {
s.GetMaster().Exec(sql, queryParams) s.GetMaster().Exec(sql, queryParams)
} }
func (s SqlPreferenceStore) Save(preferences *model.Preferences) StoreChannel { func (s SqlPreferenceStore) Save(T goi18n.TranslateFunc, preferences *model.Preferences) StoreChannel {
storeChannel := make(StoreChannel) storeChannel := make(StoreChannel)
go func() { go func() {
@@ -70,7 +71,7 @@ func (s SqlPreferenceStore) Save(preferences *model.Preferences) StoreChannel {
result.Err = model.NewAppError("SqlPreferenceStore.Save", "Unable to open transaction to save preferences", err.Error()) result.Err = model.NewAppError("SqlPreferenceStore.Save", "Unable to open transaction to save preferences", err.Error())
} else { } else {
for _, preference := range *preferences { for _, preference := range *preferences {
if upsertResult := s.save(transaction, &preference); upsertResult.Err != nil { if upsertResult := s.save(T, transaction, &preference); upsertResult.Err != nil {
result = upsertResult result = upsertResult
break break
} }
@@ -97,7 +98,7 @@ func (s SqlPreferenceStore) Save(preferences *model.Preferences) StoreChannel {
return storeChannel return storeChannel
} }
func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *model.Preference) StoreResult { func (s SqlPreferenceStore) save(T goi18n.TranslateFunc, transaction *gorp.Transaction, preference *model.Preference) StoreResult {
result := StoreResult{} result := StoreResult{}
if result.Err = preference.IsValid(); result.Err != nil { if result.Err = preference.IsValid(); result.Err != nil {
@@ -139,9 +140,9 @@ func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *mode
} }
if count == 1 { if count == 1 {
s.update(transaction, preference) s.update(T, transaction, preference)
} else { } else {
s.insert(transaction, preference) s.insert(T, transaction, preference)
} }
} else { } else {
result.Err = model.NewAppError("SqlPreferenceStore.save", "We encountered an error while updating preferences", result.Err = model.NewAppError("SqlPreferenceStore.save", "We encountered an error while updating preferences",
@@ -151,7 +152,7 @@ func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *mode
return result return result
} }
func (s SqlPreferenceStore) insert(transaction *gorp.Transaction, preference *model.Preference) StoreResult { func (s SqlPreferenceStore) insert(T goi18n.TranslateFunc, transaction *gorp.Transaction, preference *model.Preference) StoreResult {
result := StoreResult{} result := StoreResult{}
if err := transaction.Insert(preference); err != nil { if err := transaction.Insert(preference); err != nil {
@@ -167,7 +168,7 @@ func (s SqlPreferenceStore) insert(transaction *gorp.Transaction, preference *mo
return result return result
} }
func (s SqlPreferenceStore) update(transaction *gorp.Transaction, preference *model.Preference) StoreResult { func (s SqlPreferenceStore) update(T goi18n.TranslateFunc, transaction *gorp.Transaction, preference *model.Preference) StoreResult {
result := StoreResult{} result := StoreResult{}
if _, err := transaction.Update(preference); err != nil { if _, err := transaction.Update(preference); err != nil {
@@ -178,7 +179,7 @@ func (s SqlPreferenceStore) update(transaction *gorp.Transaction, preference *mo
return result return result
} }
func (s SqlPreferenceStore) Get(userId string, category string, name string) StoreChannel { func (s SqlPreferenceStore) Get(T goi18n.TranslateFunc, userId string, category string, name string) StoreChannel {
storeChannel := make(StoreChannel) storeChannel := make(StoreChannel)
go func() { go func() {
@@ -207,7 +208,7 @@ func (s SqlPreferenceStore) Get(userId string, category string, name string) Sto
return storeChannel return storeChannel
} }
func (s SqlPreferenceStore) GetCategory(userId string, category string) StoreChannel { func (s SqlPreferenceStore) GetCategory(T goi18n.TranslateFunc, userId string, category string) StoreChannel {
storeChannel := make(StoreChannel) storeChannel := make(StoreChannel)
go func() { go func() {
@@ -235,7 +236,7 @@ func (s SqlPreferenceStore) GetCategory(userId string, category string) StoreCha
return storeChannel return storeChannel
} }
func (s SqlPreferenceStore) GetAll(userId string) StoreChannel { func (s SqlPreferenceStore) GetAll(T goi18n.TranslateFunc, userId string) StoreChannel {
storeChannel := make(StoreChannel) storeChannel := make(StoreChannel)
go func() { go func() {
@@ -262,7 +263,7 @@ func (s SqlPreferenceStore) GetAll(userId string) StoreChannel {
return storeChannel return storeChannel
} }
func (s SqlPreferenceStore) PermanentDeleteByUser(userId string) StoreChannel { func (s SqlPreferenceStore) PermanentDeleteByUser(T goi18n.TranslateFunc, userId string) StoreChannel {
storeChannel := make(StoreChannel) storeChannel := make(StoreChannel)
go func() { go func() {
@@ -280,7 +281,7 @@ func (s SqlPreferenceStore) PermanentDeleteByUser(userId string) StoreChannel {
return storeChannel return storeChannel
} }
func (s SqlPreferenceStore) IsFeatureEnabled(feature, userId string) StoreChannel { func (s SqlPreferenceStore) IsFeatureEnabled(T goi18n.TranslateFunc, feature, userId string) StoreChannel {
storeChannel := make(StoreChannel) storeChannel := make(StoreChannel)
go func() { go func() {

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

@@ -5,6 +5,7 @@ package store
import ( import (
"github.com/mattermost/platform/model" "github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
"testing" "testing"
) )
@@ -27,24 +28,24 @@ func TestPreferenceSave(t *testing.T) {
Value: "value1b", Value: "value1b",
}, },
} }
if count := Must(store.Preference().Save(&preferences)); count != 2 { if count := Must(store.Preference().Save(utils.T, &preferences)); count != 2 {
t.Fatal("got incorrect number of rows saved") t.Fatal("got incorrect number of rows saved")
} }
for _, preference := range preferences { for _, preference := range preferences {
if data := Must(store.Preference().Get(preference.UserId, preference.Category, preference.Name)).(model.Preference); preference != data { if data := Must(store.Preference().Get(utils.T, preference.UserId, preference.Category, preference.Name)).(model.Preference); preference != data {
t.Fatal("got incorrect preference after first Save") t.Fatal("got incorrect preference after first Save")
} }
} }
preferences[0].Value = "value2a" preferences[0].Value = "value2a"
preferences[1].Value = "value2b" preferences[1].Value = "value2b"
if count := Must(store.Preference().Save(&preferences)); count != 2 { if count := Must(store.Preference().Save(utils.T, &preferences)); count != 2 {
t.Fatal("got incorrect number of rows saved") t.Fatal("got incorrect number of rows saved")
} }
for _, preference := range preferences { for _, preference := range preferences {
if data := Must(store.Preference().Get(preference.UserId, preference.Category, preference.Name)).(model.Preference); preference != data { if data := Must(store.Preference().Get(utils.T, preference.UserId, preference.Category, preference.Name)).(model.Preference); preference != data {
t.Fatal("got incorrect preference after second Save") t.Fatal("got incorrect preference after second Save")
} }
} }
@@ -80,16 +81,16 @@ func TestPreferenceGet(t *testing.T) {
}, },
} }
Must(store.Preference().Save(&preferences)) Must(store.Preference().Save(utils.T, &preferences))
if result := <-store.Preference().Get(userId, category, name); result.Err != nil { if result := <-store.Preference().Get(utils.T, userId, category, name); result.Err != nil {
t.Fatal(result.Err) t.Fatal(result.Err)
} else if data := result.Data.(model.Preference); data != preferences[0] { } else if data := result.Data.(model.Preference); data != preferences[0] {
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 := <-store.Preference().Get(model.NewId(), model.NewId(), model.NewId()); result.Err == nil { if result := <-store.Preference().Get(utils.T, model.NewId(), model.NewId(), model.NewId()); result.Err == nil {
t.Fatal("no error on getting a missing preference") t.Fatal("no error on getting a missing preference")
} }
} }
@@ -127,9 +128,9 @@ func TestPreferenceGetCategory(t *testing.T) {
}, },
} }
Must(store.Preference().Save(&preferences)) Must(store.Preference().Save(utils.T, &preferences))
if result := <-store.Preference().GetCategory(userId, category); result.Err != nil { if result := <-store.Preference().GetCategory(utils.T, userId, category); result.Err != nil {
t.Fatal(result.Err) t.Fatal(result.Err)
} else if data := result.Data.(model.Preferences); len(data) != 2 { } else if data := result.Data.(model.Preferences); len(data) != 2 {
t.Fatal("got the wrong number of preferences") t.Fatal("got the wrong number of preferences")
@@ -138,7 +139,7 @@ func TestPreferenceGetCategory(t *testing.T) {
} }
// make sure getting a missing preference category doesn't fail // make sure getting a missing preference category doesn't fail
if result := <-store.Preference().GetCategory(model.NewId(), model.NewId()); result.Err != nil { if result := <-store.Preference().GetCategory(utils.T, model.NewId(), model.NewId()); result.Err != nil {
t.Fatal(result.Err) t.Fatal(result.Err)
} else if data := result.Data.(model.Preferences); len(data) != 0 { } else if data := result.Data.(model.Preferences); len(data) != 0 {
t.Fatal("shouldn't have got any preferences") t.Fatal("shouldn't have got any preferences")
@@ -178,9 +179,9 @@ func TestPreferenceGetAll(t *testing.T) {
}, },
} }
Must(store.Preference().Save(&preferences)) Must(store.Preference().Save(utils.T, &preferences))
if result := <-store.Preference().GetAll(userId); result.Err != nil { if result := <-store.Preference().GetAll(utils.T, userId); result.Err != nil {
t.Fatal(result.Err) t.Fatal(result.Err)
} else if data := result.Data.(model.Preferences); len(data) != 3 { } else if data := result.Data.(model.Preferences); len(data) != 3 {
t.Fatal("got the wrong number of preferences") t.Fatal("got the wrong number of preferences")
@@ -226,9 +227,9 @@ func TestPreferenceDelete(t *testing.T) {
}, },
} }
Must(store.Preference().Save(&preferences)) Must(store.Preference().Save(utils.T, &preferences))
if result := <-store.Preference().PermanentDeleteByUser(userId); result.Err != nil { if result := <-store.Preference().PermanentDeleteByUser(utils.T, userId); result.Err != nil {
t.Fatal(result.Err) t.Fatal(result.Err)
} }
} }
@@ -276,29 +277,29 @@ func TestIsFeatureEnabled(t *testing.T) {
}, },
} }
Must(store.Preference().Save(&features)) Must(store.Preference().Save(utils.T, &features))
if result := <-store.Preference().IsFeatureEnabled(feature1, userId); result.Err != nil { if result := <-store.Preference().IsFeatureEnabled(utils.T, feature1, userId); result.Err != nil {
t.Fatal(result.Err) t.Fatal(result.Err)
} else if data := result.Data.(bool); data != true { } else if data := result.Data.(bool); data != true {
t.Fatalf("got incorrect setting for feature1, %v=%v", true, data) t.Fatalf("got incorrect setting for feature1, %v=%v", true, data)
} }
if result := <-store.Preference().IsFeatureEnabled(feature2, userId); result.Err != nil { if result := <-store.Preference().IsFeatureEnabled(utils.T, feature2, userId); result.Err != nil {
t.Fatal(result.Err) t.Fatal(result.Err)
} else if data := result.Data.(bool); data != false { } else if data := result.Data.(bool); data != false {
t.Fatalf("got incorrect setting for feature2, %v=%v", false, data) t.Fatalf("got incorrect setting for feature2, %v=%v", false, data)
} }
// make sure we get false if something different than "true" or "false" has been saved to database // make sure we get false if something different than "true" or "false" has been saved to database
if result := <-store.Preference().IsFeatureEnabled(feature3, userId); result.Err != nil { if result := <-store.Preference().IsFeatureEnabled(utils.T, feature3, userId); result.Err != nil {
t.Fatal(result.Err) t.Fatal(result.Err)
} else if data := result.Data.(bool); data != false { } else if data := result.Data.(bool); data != false {
t.Fatalf("got incorrect setting for feature3, %v=%v", false, data) t.Fatalf("got incorrect setting for feature3, %v=%v", false, data)
} }
// make sure false is returned if a non-existent feature is queried // make sure false is returned if a non-existent feature is queried
if result := <-store.Preference().IsFeatureEnabled("someOtherFeature", userId); result.Err != nil { if result := <-store.Preference().IsFeatureEnabled(utils.T, "someOtherFeature", userId); result.Err != nil {
t.Fatal(result.Err) t.Fatal(result.Err)
} else if data := result.Data.(bool); data != false { } else if data := result.Data.(bool); data != false {
t.Fatalf("got incorrect setting for non-existent feature 'someOtherFeature', %v=%v", false, data) t.Fatalf("got incorrect setting for non-existent feature 'someOtherFeature', %v=%v", false, data)
@@ -341,9 +342,9 @@ func TestDeleteUnusedFeatures(t *testing.T) {
}, },
} }
Must(store.Preference().Save(&features)) Must(store.Preference().Save(utils.T, &features))
store.(*SqlStore).preference.(*SqlPreferenceStore).DeleteUnusedFeatures() store.(*SqlStore).preference.(*SqlPreferenceStore).DeleteUnusedFeatures(utils.T)
//make sure features with value "false" have actually been deleted from the database //make sure features with value "false" have actually been deleted from the database
if val, err := store.(*SqlStore).preference.(*SqlPreferenceStore).GetReplica().SelectInt(`SELECT COUNT(*) if val, err := store.(*SqlStore).preference.(*SqlPreferenceStore).GetReplica().SelectInt(`SELECT COUNT(*)

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

@@ -15,7 +15,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
l4g "github.com/alecthomas/log4go"
"io" "io"
sqltrace "log" sqltrace "log"
"math/rand" "math/rand"
@@ -23,6 +22,7 @@ import (
"strings" "strings"
"time" "time"
l4g "github.com/alecthomas/log4go"
"github.com/go-gorp/gorp" "github.com/go-gorp/gorp"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq" _ "github.com/lib/pq"
@@ -148,7 +148,7 @@ func NewSqlStore() Store {
sqlStore.webhook.(*SqlWebhookStore).CreateIndexesIfNotExists() sqlStore.webhook.(*SqlWebhookStore).CreateIndexesIfNotExists()
sqlStore.preference.(*SqlPreferenceStore).CreateIndexesIfNotExists() sqlStore.preference.(*SqlPreferenceStore).CreateIndexesIfNotExists()
sqlStore.preference.(*SqlPreferenceStore).DeleteUnusedFeatures() sqlStore.preference.(*SqlPreferenceStore).DeleteUnusedFeatures(utils.T)
if model.IsPreviousVersion(schemaVersion) || isSchemaVersion07 || isSchemaVersion10 { if model.IsPreviousVersion(schemaVersion) || isSchemaVersion07 || isSchemaVersion10 {
sqlStore.system.Update(&model.System{Name: "Version", Value: model.CurrentVersion}) sqlStore.system.Update(&model.System{Name: "Version", Value: model.CurrentVersion})

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

@@ -4,9 +4,11 @@
package store package store
import ( import (
"time"
l4g "github.com/alecthomas/log4go" l4g "github.com/alecthomas/log4go"
"github.com/mattermost/platform/model" "github.com/mattermost/platform/model"
"time" goi18n "github.com/nicksnyder/go-i18n/i18n"
) )
type StoreResult struct { type StoreResult struct {
@@ -139,9 +141,9 @@ type SessionStore interface {
} }
type AuditStore interface { type AuditStore interface {
Save(audit *model.Audit) StoreChannel Save(T goi18n.TranslateFunc, audit *model.Audit) StoreChannel
Get(user_id string, limit int) StoreChannel Get(T goi18n.TranslateFunc, user_id string, limit int) StoreChannel
PermanentDeleteByUser(userId string) StoreChannel PermanentDeleteByUser(T goi18n.TranslateFunc, userId string) StoreChannel
} }
type OAuthStore interface { type OAuthStore interface {
@@ -183,10 +185,10 @@ type WebhookStore interface {
} }
type PreferenceStore interface { type PreferenceStore interface {
Save(preferences *model.Preferences) StoreChannel Save(T goi18n.TranslateFunc, preferences *model.Preferences) StoreChannel
Get(userId string, category string, name string) StoreChannel Get(T goi18n.TranslateFunc, userId string, category string, name string) StoreChannel
GetCategory(userId string, category string) StoreChannel GetCategory(T goi18n.TranslateFunc, userId string, category string) StoreChannel
GetAll(userId string) StoreChannel GetAll(T goi18n.TranslateFunc, userId string) StoreChannel
PermanentDeleteByUser(userId string) StoreChannel PermanentDeleteByUser(T goi18n.TranslateFunc, userId string) StoreChannel
IsFeatureEnabled(feature, userId string) StoreChannel IsFeatureEnabled(T goi18n.TranslateFunc, feature, userId string) StoreChannel
} }

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

@@ -5,6 +5,12 @@ package web
import ( import (
"fmt" "fmt"
"html/template"
"net/http"
"net/url"
"strconv"
"strings"
l4g "github.com/alecthomas/log4go" l4g "github.com/alecthomas/log4go"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/mattermost/platform/api" "github.com/mattermost/platform/api"
@@ -13,11 +19,6 @@ import (
"github.com/mattermost/platform/utils" "github.com/mattermost/platform/utils"
"github.com/mssola/user_agent" "github.com/mssola/user_agent"
"gopkg.in/fsnotify.v1" "gopkg.in/fsnotify.v1"
"html/template"
"net/http"
"net/url"
"strconv"
"strings"
) )
var Templates *template.Template var Templates *template.Template
@@ -241,7 +242,7 @@ func login(c *api.Context, w http.ResponseWriter, r *http.Request) {
if session != nil { if session != nil {
w.Header().Set(model.HEADER_TOKEN, session.Token) w.Header().Set(model.HEADER_TOKEN, session.Token)
lastViewChannelName := "town-square" lastViewChannelName := "town-square"
if lastViewResult := <-api.Srv.Store.Preference().Get(session.UserId, model.PREFERENCE_CATEGORY_LAST, model.PREFERENCE_NAME_LAST_CHANNEL); lastViewResult.Err == nil { if lastViewResult := <-api.Srv.Store.Preference().Get(c.T, session.UserId, model.PREFERENCE_CATEGORY_LAST, model.PREFERENCE_NAME_LAST_CHANNEL); lastViewResult.Err == nil {
if lastViewChannelResult := <-api.Srv.Store.Channel().Get(lastViewResult.Data.(model.Preference).Value); lastViewChannelResult.Err == nil { if lastViewChannelResult := <-api.Srv.Store.Channel().Get(lastViewResult.Data.(model.Preference).Value); lastViewChannelResult.Err == nil {
lastViewChannelName = lastViewChannelResult.Data.(*model.Channel).Name lastViewChannelName = lastViewChannelResult.Data.(*model.Channel).Name
} }