Cleaning the store from functions returning StoreResult (#11602)

* Cleaning the store from functions returning StoreResult

* Removing unnecesary StoreChannel type
Этот коммит содержится в:
Jesús Espino
2019-07-29 12:38:46 +02:00
коммит произвёл GitHub
родитель c362f0e802
Коммит e067272e16
9 изменённых файлов: 109 добавлений и 210 удалений

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

@@ -56,8 +56,8 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo
close(privateChan) close(privateChan)
}() }()
var userChan store.StoreChannel var userChan chan store.StoreResult
var userInactiveChan store.StoreChannel var userInactiveChan chan store.StoreResult
if teamId == "" { if teamId == "" {
userInactiveChan = make(chan store.StoreResult, 1) userInactiveChan = make(chan store.StoreResult, 1)
go func() { go func() {
@@ -74,7 +74,7 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo
}() }()
} }
var postChan store.StoreChannel var postChan chan store.StoreResult
if !skipIntensiveQueries { if !skipIntensiveQueries {
postChan = make(chan store.StoreResult, 1) postChan = make(chan store.StoreResult, 1)
go func() { go func() {
@@ -257,8 +257,8 @@ func (a *App) GetAnalytics(name string, teamId string) (model.AnalyticsRows, *mo
close(sessionChan) close(sessionChan)
}() }()
var fileChan store.StoreChannel var fileChan chan store.StoreResult
var hashtagChan store.StoreChannel var hashtagChan chan store.StoreResult
if !skipIntensiveQueries { if !skipIntensiveQueries {
fileChan = make(chan store.StoreResult, 1) fileChan = make(chan store.StoreResult, 1)

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

@@ -158,9 +158,9 @@ func (a *App) CreatePost(post *model.Post, channel *model.Channel, triggerWebhoo
post.SanitizeProps() post.SanitizeProps()
var pchan store.StoreChannel var pchan chan store.StoreResult
if len(post.RootId) > 0 { if len(post.RootId) > 0 {
pchan = make(store.StoreChannel, 1) pchan = make(chan store.StoreResult, 1)
go func() { go func() {
r, pErr := a.Srv.Store.Post().Get(post.RootId) r, pErr := a.Srv.Store.Post().Get(post.RootId)
pchan <- store.StoreResult{Data: r, Err: pErr} pchan <- store.StoreResult{Data: r, Err: pErr}

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

@@ -588,7 +588,7 @@ func (a *App) HandleIncomingWebhook(hookId string, req *model.IncomingWebhookReq
} }
var channel *model.Channel var channel *model.Channel
var cchan store.StoreChannel var cchan chan store.StoreResult
if len(channelName) != 0 { if len(channelName) != 0 {
if channelName[0] == '@' { if channelName[0] == '@' {
@@ -602,14 +602,14 @@ func (a *App) HandleIncomingWebhook(hookId string, req *model.IncomingWebhookReq
} }
} }
} else if channelName[0] == '#' { } else if channelName[0] == '#' {
cchan = make(store.StoreChannel, 1) cchan = make(chan store.StoreResult, 1)
go func() { go func() {
chnn, chnnErr := a.Srv.Store.Channel().GetByName(hook.TeamId, channelName[1:], true) chnn, chnnErr := a.Srv.Store.Channel().GetByName(hook.TeamId, channelName[1:], true)
cchan <- store.StoreResult{Data: chnn, Err: chnnErr} cchan <- store.StoreResult{Data: chnn, Err: chnnErr}
close(cchan) close(cchan)
}() }()
} else { } else {
cchan = make(store.StoreChannel, 1) cchan = make(chan store.StoreResult, 1)
go func() { go func() {
chnn, chnnErr := a.Srv.Store.Channel().GetByName(hook.TeamId, channelName, true) chnn, chnnErr := a.Srv.Store.Channel().GetByName(hook.TeamId, channelName, true)
cchan <- store.StoreResult{Data: chnn, Err: chnnErr} cchan <- store.StoreResult{Data: chnn, Err: chnnErr}

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

@@ -58,17 +58,6 @@ func NewLayeredStore(db LayeredStoreDatabaseLayer, metrics einterfaces.MetricsIn
type QueryFunction func(LayeredStoreSupplier) *LayeredStoreSupplierResult type QueryFunction func(LayeredStoreSupplier) *LayeredStoreSupplierResult
func (s *LayeredStore) RunQuery(queryFunction QueryFunction) StoreChannel {
storeChannel := make(StoreChannel)
go func() {
result := queryFunction(s.LayerChainHead)
storeChannel <- result.StoreResult
}()
return storeChannel
}
func (s *LayeredStore) Team() TeamStore { func (s *LayeredStore) Team() TeamStore {
return s.DatabaseLayer.Team() return s.DatabaseLayer.Team()
} }

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

@@ -471,13 +471,7 @@ func (s SqlChannelStore) Save(channel *model.Channel, maxChannelsPerTeam int64)
} }
defer finalizeTransaction(transaction) defer finalizeTransaction(transaction)
channelResult := s.saveChannelT(transaction, channel, maxChannelsPerTeam) newChannel, appErr := s.saveChannelT(transaction, channel, maxChannelsPerTeam)
var newChannel *model.Channel
if channelResult.Data != nil {
newChannel = channelResult.Data.(*model.Channel)
}
appErr := channelResult.Err
if appErr != nil { if appErr != nil {
return newChannel, appErr return newChannel, appErr
} }
@@ -534,15 +528,7 @@ func (s SqlChannelStore) SaveDirectChannel(directchannel *model.Channel, member1
defer finalizeTransaction(transaction) defer finalizeTransaction(transaction)
directchannel.TeamId = "" directchannel.TeamId = ""
// After updating saveChannelT() should be: newChannel, appErr := s.saveChannelT(transaction, directchannel, 0)
// newChannel, appErr := s.saveChannelT(transaction, directchannel, 0)
channelResult := s.saveChannelT(transaction, directchannel, 0)
var newChannel *model.Channel
if channelResult.Data != nil {
newChannel = channelResult.Data.(*model.Channel)
}
appErr := channelResult.Err
if appErr != nil { if appErr != nil {
return newChannel, appErr return newChannel, appErr
} }
@@ -551,19 +537,19 @@ func (s SqlChannelStore) SaveDirectChannel(directchannel *model.Channel, member1
member1.ChannelId = newChannel.Id member1.ChannelId = newChannel.Id
member2.ChannelId = newChannel.Id member2.ChannelId = newChannel.Id
member1Result := s.saveMemberT(transaction, member1, newChannel) _, member1SaveErr := s.saveMemberT(transaction, member1, newChannel)
member2Result := member1Result member2SaveErr := member1SaveErr
if member1.UserId != member2.UserId { if member1.UserId != member2.UserId {
member2Result = s.saveMemberT(transaction, member2, newChannel) _, member2SaveErr = s.saveMemberT(transaction, member2, newChannel)
} }
if member1Result.Err != nil || member2Result.Err != nil { if member1SaveErr != nil || member2SaveErr != nil {
details := "" details := ""
if member1Result.Err != nil { if member1SaveErr != nil {
details += "Member1Err: " + member1Result.Err.Message details += "Member1Err: " + member1SaveErr.Message
} }
if member2Result.Err != nil { if member2SaveErr != nil {
details += "Member2Err: " + member2Result.Err.Message details += "Member2Err: " + member2SaveErr.Message
} }
return nil, model.NewAppError("SqlChannelStore.SaveDirectChannel", "store.sql_channel.save_direct_channel.add_members.app_error", nil, details, http.StatusInternalServerError) return nil, model.NewAppError("SqlChannelStore.SaveDirectChannel", "store.sql_channel.save_direct_channel.add_members.app_error", nil, details, http.StatusInternalServerError)
} }
@@ -576,26 +562,21 @@ func (s SqlChannelStore) SaveDirectChannel(directchannel *model.Channel, member1
} }
func (s SqlChannelStore) saveChannelT(transaction *gorp.Transaction, channel *model.Channel, maxChannelsPerTeam int64) store.StoreResult { func (s SqlChannelStore) saveChannelT(transaction *gorp.Transaction, channel *model.Channel, maxChannelsPerTeam int64) (*model.Channel, *model.AppError) {
result := store.StoreResult{}
if len(channel.Id) > 0 { if len(channel.Id) > 0 {
result.Err = model.NewAppError("SqlChannelStore.Save", "store.sql_channel.save_channel.existing.app_error", nil, "id="+channel.Id, http.StatusBadRequest) return nil, model.NewAppError("SqlChannelStore.Save", "store.sql_channel.save_channel.existing.app_error", nil, "id="+channel.Id, http.StatusBadRequest)
return result
} }
channel.PreSave() channel.PreSave()
if result.Err = channel.IsValid(); result.Err != nil { if err := channel.IsValid(); err != nil {
return result return nil, err
} }
if channel.Type != model.CHANNEL_DIRECT && channel.Type != model.CHANNEL_GROUP && maxChannelsPerTeam >= 0 { if channel.Type != model.CHANNEL_DIRECT && channel.Type != model.CHANNEL_GROUP && maxChannelsPerTeam >= 0 {
if count, err := transaction.SelectInt("SELECT COUNT(0) FROM Channels WHERE TeamId = :TeamId AND DeleteAt = 0 AND (Type = 'O' OR Type = 'P')", map[string]interface{}{"TeamId": channel.TeamId}); err != nil { if count, err := transaction.SelectInt("SELECT COUNT(0) FROM Channels WHERE TeamId = :TeamId AND DeleteAt = 0 AND (Type = 'O' OR Type = 'P')", map[string]interface{}{"TeamId": channel.TeamId}); err != nil {
result.Err = model.NewAppError("SqlChannelStore.Save", "store.sql_channel.save_channel.current_count.app_error", nil, "teamId="+channel.TeamId+", "+err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlChannelStore.Save", "store.sql_channel.save_channel.current_count.app_error", nil, "teamId="+channel.TeamId+", "+err.Error(), http.StatusInternalServerError)
return result
} else if count >= maxChannelsPerTeam { } else if count >= maxChannelsPerTeam {
result.Err = model.NewAppError("SqlChannelStore.Save", "store.sql_channel.save_channel.limit.app_error", nil, "teamId="+channel.TeamId, http.StatusBadRequest) return nil, model.NewAppError("SqlChannelStore.Save", "store.sql_channel.save_channel.limit.app_error", nil, "teamId="+channel.TeamId, http.StatusBadRequest)
return result
} }
} }
@@ -604,19 +585,13 @@ func (s SqlChannelStore) saveChannelT(transaction *gorp.Transaction, channel *mo
dupChannel := model.Channel{} dupChannel := model.Channel{}
s.GetMaster().SelectOne(&dupChannel, "SELECT * FROM Channels WHERE TeamId = :TeamId AND Name = :Name", map[string]interface{}{"TeamId": channel.TeamId, "Name": channel.Name}) s.GetMaster().SelectOne(&dupChannel, "SELECT * FROM Channels WHERE TeamId = :TeamId AND Name = :Name", map[string]interface{}{"TeamId": channel.TeamId, "Name": channel.Name})
if dupChannel.DeleteAt > 0 { if dupChannel.DeleteAt > 0 {
result.Err = model.NewAppError("SqlChannelStore.Save", "store.sql_channel.save_channel.previously.app_error", nil, "id="+channel.Id+", "+err.Error(), http.StatusBadRequest) return nil, model.NewAppError("SqlChannelStore.Save", "store.sql_channel.save_channel.previously.app_error", nil, "id="+channel.Id+", "+err.Error(), http.StatusBadRequest)
} else {
result.Err = model.NewAppError("SqlChannelStore.Save", store.CHANNEL_EXISTS_ERROR, nil, "id="+channel.Id+", "+err.Error(), http.StatusBadRequest)
result.Data = &dupChannel
} }
} else { return &dupChannel, model.NewAppError("SqlChannelStore.Save", store.CHANNEL_EXISTS_ERROR, nil, "id="+channel.Id+", "+err.Error(), http.StatusBadRequest)
result.Err = model.NewAppError("SqlChannelStore.Save", "store.sql_channel.save_channel.save.app_error", nil, "id="+channel.Id+", "+err.Error(), http.StatusInternalServerError)
} }
} else { return nil, model.NewAppError("SqlChannelStore.Save", "store.sql_channel.save_channel.save.app_error", nil, "id="+channel.Id+", "+err.Error(), http.StatusInternalServerError)
result.Data = channel
} }
return channel, nil
return result
} }
// Update writes the updated channel to the database. // Update writes the updated channel to the database.
@@ -790,9 +765,9 @@ func (s SqlChannelStore) SetDeleteAt(channelId string, deleteAt, updateAt int64)
} }
defer finalizeTransaction(transaction) defer finalizeTransaction(transaction)
var result = s.setDeleteAtT(transaction, channelId, deleteAt, updateAt) appErr := s.setDeleteAtT(transaction, channelId, deleteAt, updateAt)
if result.Err != nil { if appErr != nil {
return result.Err return appErr
} }
// Additionally propagate the write to the PublicChannels table. // Additionally propagate the write to the PublicChannels table.
@@ -817,16 +792,13 @@ func (s SqlChannelStore) SetDeleteAt(channelId string, deleteAt, updateAt int64)
return nil return nil
} }
func (s SqlChannelStore) setDeleteAtT(transaction *gorp.Transaction, channelId string, deleteAt, updateAt int64) store.StoreResult { func (s SqlChannelStore) setDeleteAtT(transaction *gorp.Transaction, channelId string, deleteAt, updateAt int64) *model.AppError {
result := store.StoreResult{}
_, err := transaction.Exec("Update Channels SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt WHERE Id = :ChannelId", map[string]interface{}{"DeleteAt": deleteAt, "UpdateAt": updateAt, "ChannelId": channelId}) _, err := transaction.Exec("Update Channels SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt WHERE Id = :ChannelId", map[string]interface{}{"DeleteAt": deleteAt, "UpdateAt": updateAt, "ChannelId": channelId})
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlChannelStore.Delete", "store.sql_channel.delete.channel.app_error", nil, "id="+channelId+", err="+err.Error(), http.StatusInternalServerError) return model.NewAppError("SqlChannelStore.Delete", "store.sql_channel.delete.channel.app_error", nil, "id="+channelId+", err="+err.Error(), http.StatusInternalServerError)
return result
} }
return result return nil
} }
// PermanentDeleteByTeam removes all channels for the given team from the database. // PermanentDeleteByTeam removes all channels for the given team from the database.
@@ -1311,49 +1283,42 @@ func (s SqlChannelStore) SaveMember(member *model.ChannelMember) (*model.Channel
} }
defer finalizeTransaction(transaction) defer finalizeTransaction(transaction)
storeResult := s.saveMemberT(transaction, member, channel) newMember, appErr := s.saveMemberT(transaction, member, channel)
if storeResult.Err != nil { if appErr != nil {
return nil, storeResult.Err return nil, appErr
} }
if err := transaction.Commit(); err != nil { if err := transaction.Commit(); err != nil {
return nil, model.NewAppError("SqlChannelStore.SaveMember", "store.sql_channel.save_member.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlChannelStore.SaveMember", "store.sql_channel.save_member.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
} }
return storeResult.Data.(*model.ChannelMember), nil return newMember, nil
} }
func (s SqlChannelStore) saveMemberT(transaction *gorp.Transaction, member *model.ChannelMember, channel *model.Channel) store.StoreResult { func (s SqlChannelStore) saveMemberT(transaction *gorp.Transaction, member *model.ChannelMember, channel *model.Channel) (*model.ChannelMember, *model.AppError) {
result := store.StoreResult{}
member.PreSave() member.PreSave()
if result.Err = member.IsValid(); result.Err != nil { if err := member.IsValid(); err != nil {
return result return nil, err
} }
dbMember := NewChannelMemberFromModel(member) dbMember := NewChannelMemberFromModel(member)
if err := transaction.Insert(dbMember); err != nil { if err := transaction.Insert(dbMember); err != nil {
if IsUniqueConstraintError(err, []string{"ChannelId", "channelmembers_pkey"}) { if IsUniqueConstraintError(err, []string{"ChannelId", "channelmembers_pkey"}) {
result.Err = model.NewAppError("SqlChannelStore.SaveMember", "store.sql_channel.save_member.exists.app_error", nil, "channel_id="+member.ChannelId+", user_id="+member.UserId+", "+err.Error(), http.StatusBadRequest) return nil, model.NewAppError("SqlChannelStore.SaveMember", "store.sql_channel.save_member.exists.app_error", nil, "channel_id="+member.ChannelId+", user_id="+member.UserId+", "+err.Error(), http.StatusBadRequest)
return result
} }
result.Err = model.NewAppError("SqlChannelStore.SaveMember", "store.sql_channel.save_member.save.app_error", nil, "channel_id="+member.ChannelId+", user_id="+member.UserId+", "+err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlChannelStore.SaveMember", "store.sql_channel.save_member.save.app_error", nil, "channel_id="+member.ChannelId+", user_id="+member.UserId+", "+err.Error(), http.StatusInternalServerError)
return result
} }
var retrievedMember channelMemberWithSchemeRoles var retrievedMember channelMemberWithSchemeRoles
if err := transaction.SelectOne(&retrievedMember, CHANNEL_MEMBERS_WITH_SCHEME_SELECT_QUERY+"WHERE ChannelMembers.ChannelId = :ChannelId AND ChannelMembers.UserId = :UserId", map[string]interface{}{"ChannelId": dbMember.ChannelId, "UserId": dbMember.UserId}); err != nil { if err := transaction.SelectOne(&retrievedMember, CHANNEL_MEMBERS_WITH_SCHEME_SELECT_QUERY+"WHERE ChannelMembers.ChannelId = :ChannelId AND ChannelMembers.UserId = :UserId", map[string]interface{}{"ChannelId": dbMember.ChannelId, "UserId": dbMember.UserId}); err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
result.Err = model.NewAppError("SqlChannelStore.GetMember", store.MISSING_CHANNEL_MEMBER_ERROR, nil, "channel_id="+dbMember.ChannelId+"user_id="+dbMember.UserId+","+err.Error(), http.StatusNotFound) return nil, model.NewAppError("SqlChannelStore.GetMember", store.MISSING_CHANNEL_MEMBER_ERROR, nil, "channel_id="+dbMember.ChannelId+"user_id="+dbMember.UserId+","+err.Error(), http.StatusNotFound)
return result
} }
result.Err = model.NewAppError("SqlChannelStore.GetMember", "store.sql_channel.get_member.app_error", nil, "channel_id="+dbMember.ChannelId+"user_id="+dbMember.UserId+","+err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlChannelStore.GetMember", "store.sql_channel.get_member.app_error", nil, "channel_id="+dbMember.ChannelId+"user_id="+dbMember.UserId+","+err.Error(), http.StatusInternalServerError)
return result
} }
result.Data = retrievedMember.ToModel() return retrievedMember.ToModel(), nil
return result
} }
func (s SqlChannelStore) UpdateMember(member *model.ChannelMember) (*model.ChannelMember, *model.AppError) { func (s SqlChannelStore) UpdateMember(member *model.ChannelMember) (*model.ChannelMember, *model.AppError) {

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

@@ -464,8 +464,18 @@ func (s *SqlPostStore) GetPosts(channelId string, offset int, limit int, allowFr
s.metrics.IncrementMemCacheMissCounter("Last Posts Cache") s.metrics.IncrementMemCacheMissCounter("Last Posts Cache")
} }
rpc := s.getRootPosts(channelId, offset, limit) rpc := make(chan store.StoreResult, 1)
cpc := s.getParentsPosts(channelId, offset, limit) go func() {
posts, err := s.getRootPosts(channelId, offset, limit)
rpc <- store.StoreResult{Data: posts, Err: err}
close(rpc)
}()
cpc := make(chan store.StoreResult, 1)
go func() {
posts, err := s.getParentsPosts(channelId, offset, limit)
cpc <- store.StoreResult{Data: posts, Err: err}
close(cpc)
}()
var err *model.AppError var err *model.AppError
list := model.NewPostList() list := model.NewPostList()
@@ -735,52 +745,46 @@ func (s *SqlPostStore) GetPostAfterTime(channelId string, time int64) (*model.Po
return post, nil return post, nil
} }
func (s *SqlPostStore) getRootPosts(channelId string, offset int, limit int) store.StoreChannel { func (s *SqlPostStore) getRootPosts(channelId string, offset int, limit int) ([]*model.Post, *model.AppError) {
return store.Do(func(result *store.StoreResult) { var posts []*model.Post
var posts []*model.Post _, err := s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE ChannelId = :ChannelId AND DeleteAt = 0 ORDER BY CreateAt DESC LIMIT :Limit OFFSET :Offset", map[string]interface{}{"ChannelId": channelId, "Offset": offset, "Limit": limit})
_, err := s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE ChannelId = :ChannelId AND DeleteAt = 0 ORDER BY CreateAt DESC LIMIT :Limit OFFSET :Offset", map[string]interface{}{"ChannelId": channelId, "Offset": offset, "Limit": limit}) if err != nil {
if err != nil { return nil, model.NewAppError("SqlPostStore.GetLinearPosts", "store.sql_post.get_root_posts.app_error", nil, "channelId="+channelId+err.Error(), http.StatusInternalServerError)
result.Err = model.NewAppError("SqlPostStore.GetLinearPosts", "store.sql_post.get_root_posts.app_error", nil, "channelId="+channelId+err.Error(), http.StatusInternalServerError) }
} else { return posts, nil
result.Data = posts
}
})
} }
func (s *SqlPostStore) getParentsPosts(channelId string, offset int, limit int) store.StoreChannel { func (s *SqlPostStore) getParentsPosts(channelId string, offset int, limit int) ([]*model.Post, *model.AppError) {
return store.Do(func(result *store.StoreResult) { var posts []*model.Post
var posts []*model.Post _, err := s.GetReplica().Select(&posts,
_, err := s.GetReplica().Select(&posts, `SELECT
`SELECT q2.*
q2.* FROM
Posts q2
INNER JOIN
(SELECT DISTINCT
q3.RootId
FROM FROM
Posts q2 (SELECT
INNER JOIN RootId
(SELECT DISTINCT FROM
q3.RootId Posts
FROM WHERE
(SELECT ChannelId = :ChannelId1
RootId AND DeleteAt = 0
FROM ORDER BY CreateAt DESC
Posts LIMIT :Limit OFFSET :Offset) q3
WHERE WHERE q3.RootId != '') q1
ChannelId = :ChannelId1 ON q1.RootId = q2.Id OR q1.RootId = q2.RootId
AND DeleteAt = 0 WHERE
ORDER BY CreateAt DESC ChannelId = :ChannelId2
LIMIT :Limit OFFSET :Offset) q3 AND DeleteAt = 0
WHERE q3.RootId != '') q1 ORDER BY CreateAt`,
ON q1.RootId = q2.Id OR q1.RootId = q2.RootId map[string]interface{}{"ChannelId1": channelId, "Offset": offset, "Limit": limit, "ChannelId2": channelId})
WHERE if err != nil {
ChannelId = :ChannelId2 return nil, model.NewAppError("SqlPostStore.GetLinearPosts", "store.sql_post.get_parents_posts.app_error", nil, "channelId="+channelId+" err="+err.Error(), http.StatusInternalServerError)
AND DeleteAt = 0 }
ORDER BY CreateAt`, return posts, nil
map[string]interface{}{"ChannelId1": channelId, "Offset": offset, "Limit": limit, "ChannelId2": channelId})
if err != nil {
result.Err = model.NewAppError("SqlPostStore.GetLinearPosts", "store.sql_post.get_parents_posts.app_error", nil, "channelId="+channelId+" err="+err.Error(), http.StatusInternalServerError)
} else {
result.Data = posts
}
})
} }
var specialSearchChar = []string{ var specialSearchChar = []string{

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

@@ -1161,11 +1161,7 @@ func (us SqlUserStore) Search(teamId string, term string, options *model.UserSea
if teamId != "" { if teamId != "" {
query = query.Join("TeamMembers tm ON ( tm.UserId = u.Id AND tm.DeleteAt = 0 AND tm.TeamId = ? )", teamId) query = query.Join("TeamMembers tm ON ( tm.UserId = u.Id AND tm.DeleteAt = 0 AND tm.TeamId = ? )", teamId)
} }
result := us.performSearch(query, term, options) return us.performSearch(query, term, options)
if result.Err != nil {
return nil, result.Err
}
return result.Data.([]*model.User), nil
} }
func (us SqlUserStore) SearchWithoutTeam(term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) { func (us SqlUserStore) SearchWithoutTeam(term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
@@ -1182,11 +1178,7 @@ func (us SqlUserStore) SearchWithoutTeam(term string, options *model.UserSearchO
OrderBy("u.Username ASC"). OrderBy("u.Username ASC").
Limit(uint64(options.Limit)) Limit(uint64(options.Limit))
result := us.performSearch(query, term, options) return us.performSearch(query, term, options)
if result.Err != nil {
return nil, result.Err
}
return result.Data.([]*model.User), nil
} }
func (us SqlUserStore) SearchNotInTeam(notInTeamId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) { func (us SqlUserStore) SearchNotInTeam(notInTeamId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
@@ -1200,11 +1192,7 @@ func (us SqlUserStore) SearchNotInTeam(notInTeamId string, term string, options
query = applyTeamGroupConstrainedFilter(query, notInTeamId) query = applyTeamGroupConstrainedFilter(query, notInTeamId)
} }
result := us.performSearch(query, term, options) return us.performSearch(query, term, options)
if result.Err != nil {
return nil, result.Err
}
return result.Data.([]*model.User), nil
} }
func (us SqlUserStore) SearchNotInChannel(teamId string, channelId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) { func (us SqlUserStore) SearchNotInChannel(teamId string, channelId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
@@ -1222,11 +1210,7 @@ func (us SqlUserStore) SearchNotInChannel(teamId string, channelId string, term
query = applyChannelGroupConstrainedFilter(query, channelId) query = applyChannelGroupConstrainedFilter(query, channelId)
} }
result := us.performSearch(query, term, options) return us.performSearch(query, term, options)
if result.Err != nil {
return nil, result.Err
}
return result.Data.([]*model.User), nil
} }
func (us SqlUserStore) SearchInChannel(channelId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) { func (us SqlUserStore) SearchInChannel(channelId string, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
@@ -1235,11 +1219,7 @@ func (us SqlUserStore) SearchInChannel(channelId string, term string, options *m
OrderBy("Username ASC"). OrderBy("Username ASC").
Limit(uint64(options.Limit)) Limit(uint64(options.Limit))
result := us.performSearch(query, term, options) return us.performSearch(query, term, options)
if result.Err != nil {
return nil, result.Err
}
return result.Data.([]*model.User), nil
} }
var escapeLikeSearchChar = []string{ var escapeLikeSearchChar = []string{
@@ -1284,9 +1264,7 @@ func generateSearchQuery(query sq.SelectBuilder, terms []string, fields []string
return query return query
} }
func (us SqlUserStore) performSearch(query sq.SelectBuilder, term string, options *model.UserSearchOptions) store.StoreResult { func (us SqlUserStore) performSearch(query sq.SelectBuilder, term string, options *model.UserSearchOptions) ([]*model.User, *model.AppError) {
result := store.StoreResult{}
// These chars must be removed from the like query. // These chars must be removed from the like query.
for _, c := range ignoreLikeSearchChar { for _, c := range ignoreLikeSearchChar {
term = strings.Replace(term, c, "", -1) term = strings.Replace(term, c, "", -1)
@@ -1328,23 +1306,19 @@ func (us SqlUserStore) performSearch(query sq.SelectBuilder, term string, option
queryString, args, err := query.ToSql() queryString, args, err := query.ToSql()
if err != nil { if err != nil {
result.Err = model.NewAppError("SqlUserStore.Search", "store.sql_user.app_error", nil, err.Error(), http.StatusInternalServerError) return nil, model.NewAppError("SqlUserStore.Search", "store.sql_user.app_error", nil, err.Error(), http.StatusInternalServerError)
return result
} }
var users []*model.User var users []*model.User
if _, err := us.GetReplica().Select(&users, queryString, args...); err != nil { if _, err := us.GetReplica().Select(&users, queryString, args...); err != nil {
result.Err = model.NewAppError("SqlUserStore.Search", "store.sql_user.search.app_error", nil, return nil, model.NewAppError("SqlUserStore.Search", "store.sql_user.search.app_error", nil,
fmt.Sprintf("term=%v, search_type=%v, %v", term, searchType, err.Error()), http.StatusInternalServerError) fmt.Sprintf("term=%v, search_type=%v, %v", term, searchType, err.Error()), http.StatusInternalServerError)
} else { }
for _, u := range users { for _, u := range users {
u.Sanitize(map[string]bool{}) u.Sanitize(map[string]bool{})
}
result.Data = users
} }
return result return users, nil
} }
func (us SqlUserStore) AnalyticsGetInactiveUsersCount() (int64, *model.AppError) { func (us SqlUserStore) AnalyticsGetInactiveUsersCount() (int64, *model.AppError) {

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

@@ -4,8 +4,6 @@
package store package store
import ( import (
"time"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
) )
@@ -14,30 +12,6 @@ type StoreResult struct {
Err *model.AppError Err *model.AppError
} }
type StoreChannel chan StoreResult
func Do(f func(result *StoreResult)) StoreChannel {
storeChannel := make(StoreChannel, 1)
go func() {
result := StoreResult{}
f(&result)
storeChannel <- result
close(storeChannel)
}()
return storeChannel
}
func Must(sc StoreChannel) interface{} {
r := <-sc
if r.Err != nil {
time.Sleep(time.Second)
panic(r.Err)
}
return r.Data
}
type Store interface { type Store interface {
Team() TeamStore Team() TeamStore
Channel() ChannelStore Channel() ChannelStore

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

@@ -10,13 +10,6 @@ import (
"github.com/mattermost/mattermost-server/store/storetest/mocks" "github.com/mattermost/mattermost-server/store/storetest/mocks"
) )
// NewStoreChannel returns a channel that will receive the given result.
func NewStoreChannel(result store.StoreResult) store.StoreChannel {
ch := make(store.StoreChannel, 1)
ch <- result
return ch
}
// Store can be used to provide mock stores for testing. // Store can be used to provide mock stores for testing.
type Store struct { type Store struct {
TeamStore mocks.TeamStore TeamStore mocks.TeamStore