Migrates Channel.GetByName to sync by default (#11187)
* Channel.GetByName and Channel.GetByNameIncludedDeleted sync by default * Suggested changes * Fix some vars shadowing * Rename of vars inside goroutine * Shadow variable corrected
Этот коммит содержится в:
коммит произвёл
Jesús Espino
родитель
4494a56162
Коммит
59e7bf1c23
118
app/channel.go
118
app/channel.go
@@ -74,37 +74,38 @@ func (a *App) JoinDefaultChannels(teamId string, user *model.User, shouldBeAdmin
|
||||
|
||||
var err *model.AppError
|
||||
for _, channelName := range a.DefaultChannelNames() {
|
||||
if result := <-a.Srv.Store.Channel().GetByName(teamId, channelName, true); result.Err != nil {
|
||||
err = result.Err
|
||||
} else {
|
||||
channel := result.Data.(*model.Channel)
|
||||
|
||||
if channel.Type != model.CHANNEL_OPEN {
|
||||
continue
|
||||
}
|
||||
|
||||
cm := &model.ChannelMember{
|
||||
ChannelId: channel.Id,
|
||||
UserId: user.Id,
|
||||
SchemeGuest: user.IsGuest(),
|
||||
SchemeUser: !user.IsGuest(),
|
||||
SchemeAdmin: shouldBeAdmin,
|
||||
NotifyProps: model.GetDefaultChannelNotifyProps(),
|
||||
}
|
||||
|
||||
if cmResult := <-a.Srv.Store.Channel().SaveMember(cm); cmResult.Err != nil {
|
||||
err = cmResult.Err
|
||||
}
|
||||
if result = <-a.Srv.Store.ChannelMemberHistory().LogJoinEvent(user.Id, channel.Id, model.GetMillis()); result.Err != nil {
|
||||
mlog.Warn(fmt.Sprintf("Failed to update ChannelMemberHistory table %v", result.Err))
|
||||
}
|
||||
|
||||
if *a.Config().ServiceSettings.ExperimentalEnableDefaultChannelLeaveJoinMessages {
|
||||
a.postJoinMessageForDefaultChannel(user, requestor, channel)
|
||||
}
|
||||
|
||||
a.InvalidateCacheForChannelMembers(channel.Id)
|
||||
channel, channelErr := a.Srv.Store.Channel().GetByName(teamId, channelName, true)
|
||||
if channelErr != nil {
|
||||
err = channelErr
|
||||
continue
|
||||
}
|
||||
|
||||
if channel.Type != model.CHANNEL_OPEN {
|
||||
continue
|
||||
}
|
||||
|
||||
cm := &model.ChannelMember{
|
||||
ChannelId: channel.Id,
|
||||
UserId: user.Id,
|
||||
SchemeGuest: user.IsGuest(),
|
||||
SchemeUser: !user.IsGuest(),
|
||||
SchemeAdmin: shouldBeAdmin,
|
||||
NotifyProps: model.GetDefaultChannelNotifyProps(),
|
||||
}
|
||||
|
||||
if cmResult := <-a.Srv.Store.Channel().SaveMember(cm); cmResult.Err != nil {
|
||||
err = cmResult.Err
|
||||
}
|
||||
if result := <-a.Srv.Store.ChannelMemberHistory().LogJoinEvent(user.Id, channel.Id, model.GetMillis()); result.Err != nil {
|
||||
mlog.Warn(fmt.Sprintf("Failed to update ChannelMemberHistory table %v", result.Err))
|
||||
}
|
||||
|
||||
if *a.Config().ServiceSettings.ExperimentalEnableDefaultChannelLeaveJoinMessages {
|
||||
a.postJoinMessageForDefaultChannel(user, requestor, channel)
|
||||
}
|
||||
|
||||
a.InvalidateCacheForChannelMembers(channel.Id)
|
||||
|
||||
}
|
||||
|
||||
if a.IsESIndexingEnabled() {
|
||||
@@ -280,10 +281,10 @@ func (a *App) CreateChannel(channel *model.Channel, addMember bool) (*model.Chan
|
||||
}
|
||||
|
||||
func (a *App) GetOrCreateDirectChannel(userId, otherUserId string) (*model.Channel, *model.AppError) {
|
||||
result := <-a.Srv.Store.Channel().GetByName("", model.GetDMNameFromIds(userId, otherUserId), true)
|
||||
if result.Err != nil {
|
||||
if result.Err.Id == store.MISSING_CHANNEL_ERROR {
|
||||
channel, err := a.createDirectChannel(userId, otherUserId)
|
||||
channel, err := a.Srv.Store.Channel().GetByName("", model.GetDMNameFromIds(userId, otherUserId), true)
|
||||
if err != nil {
|
||||
if err.Id == store.MISSING_CHANNEL_ERROR {
|
||||
channel, err = a.createDirectChannel(userId, otherUserId)
|
||||
if err != nil {
|
||||
if err.Id == store.CHANNEL_EXISTS_ERROR {
|
||||
return channel, nil
|
||||
@@ -309,8 +310,8 @@ func (a *App) GetOrCreateDirectChannel(userId, otherUserId string) (*model.Chann
|
||||
if a.IsESIndexingEnabled() {
|
||||
a.Srv.Go(func() {
|
||||
for _, id := range []string{userId, otherUserId} {
|
||||
if err := a.indexUserFromId(id); err != nil {
|
||||
mlog.Error("Encountered error indexing user", mlog.String("user_id", id), mlog.Err(err))
|
||||
if indexUserErr := a.indexUserFromId(id); indexUserErr != nil {
|
||||
mlog.Error("Encountered error indexing user", mlog.String("user_id", id), mlog.Err(indexUserErr))
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -322,9 +323,9 @@ func (a *App) GetOrCreateDirectChannel(userId, otherUserId string) (*model.Chann
|
||||
|
||||
return channel, nil
|
||||
}
|
||||
return nil, model.NewAppError("GetOrCreateDMChannel", "web.incoming_webhook.channel.app_error", nil, "err="+result.Err.Message, result.Err.StatusCode)
|
||||
return nil, model.NewAppError("GetOrCreateDMChannel", "web.incoming_webhook.channel.app_error", nil, "err="+err.Message, err.StatusCode)
|
||||
}
|
||||
return result.Data.(*model.Channel), nil
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
func (a *App) createDirectChannel(userId string, otherUserId string) (*model.Channel, *model.AppError) {
|
||||
@@ -1161,25 +1162,26 @@ func (a *App) GetChannel(channelId string) (*model.Channel, *model.AppError) {
|
||||
}
|
||||
|
||||
func (a *App) GetChannelByName(channelName, teamId string, includeDeleted bool) (*model.Channel, *model.AppError) {
|
||||
var result store.StoreResult
|
||||
var channel *model.Channel
|
||||
var err *model.AppError
|
||||
|
||||
if includeDeleted {
|
||||
result = <-a.Srv.Store.Channel().GetByNameIncludeDeleted(teamId, channelName, false)
|
||||
channel, err = a.Srv.Store.Channel().GetByNameIncludeDeleted(teamId, channelName, false)
|
||||
} else {
|
||||
result = <-a.Srv.Store.Channel().GetByName(teamId, channelName, false)
|
||||
channel, err = a.Srv.Store.Channel().GetByName(teamId, channelName, false)
|
||||
}
|
||||
|
||||
if result.Err != nil && result.Err.Id == "store.sql_channel.get_by_name.missing.app_error" {
|
||||
result.Err.StatusCode = http.StatusNotFound
|
||||
return nil, result.Err
|
||||
if err != nil && err.Id == "store.sql_channel.get_by_name.missing.app_error" {
|
||||
err.StatusCode = http.StatusNotFound
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if result.Err != nil {
|
||||
result.Err.StatusCode = http.StatusBadRequest
|
||||
return nil, result.Err
|
||||
if err != nil {
|
||||
err.StatusCode = http.StatusBadRequest
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.Data.(*model.Channel), nil
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
func (a *App) GetChannelsByNames(channelNames []string, teamId string) ([]*model.Channel, *model.AppError) {
|
||||
@@ -1204,25 +1206,25 @@ func (a *App) GetChannelByNameForTeamName(channelName, teamName string, includeD
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result store.StoreResult
|
||||
var result *model.Channel
|
||||
|
||||
if includeDeleted {
|
||||
result = <-a.Srv.Store.Channel().GetByNameIncludeDeleted(team.Id, channelName, false)
|
||||
result, err = a.Srv.Store.Channel().GetByNameIncludeDeleted(team.Id, channelName, false)
|
||||
} else {
|
||||
result = <-a.Srv.Store.Channel().GetByName(team.Id, channelName, false)
|
||||
result, err = a.Srv.Store.Channel().GetByName(team.Id, channelName, false)
|
||||
}
|
||||
|
||||
if result.Err != nil && result.Err.Id == "store.sql_channel.get_by_name.missing.app_error" {
|
||||
result.Err.StatusCode = http.StatusNotFound
|
||||
return nil, result.Err
|
||||
if err != nil && err.Id == "store.sql_channel.get_by_name.missing.app_error" {
|
||||
err.StatusCode = http.StatusNotFound
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if result.Err != nil {
|
||||
result.Err.StatusCode = http.StatusBadRequest
|
||||
return nil, result.Err
|
||||
if err != nil {
|
||||
err.StatusCode = http.StatusBadRequest
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.Data.(*model.Channel), nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (a *App) GetChannelsForUser(teamId string, userId string, includeDeleted bool) (*model.ChannelList, *model.AppError) {
|
||||
|
||||
@@ -162,7 +162,9 @@ func TestJoinDefaultChannelsCreatesChannelMemberHistoryRecordTownSquare(t *testi
|
||||
defer th.TearDown()
|
||||
|
||||
// figure out the initial number of users in town square
|
||||
townSquareChannelId := store.Must(th.App.Srv.Store.Channel().GetByName(th.BasicTeam.Id, "town-square", true)).(*model.Channel).Id
|
||||
channel, err := th.App.Srv.Store.Channel().GetByName(th.BasicTeam.Id, "town-square", true)
|
||||
require.Nil(t, err)
|
||||
townSquareChannelId := channel.Id
|
||||
initialNumTownSquareUsers := len(store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, townSquareChannelId)).([]*model.ChannelMemberHistoryResult))
|
||||
|
||||
// create a new user that joins the default channels
|
||||
@@ -188,7 +190,9 @@ func TestJoinDefaultChannelsCreatesChannelMemberHistoryRecordOffTopic(t *testing
|
||||
defer th.TearDown()
|
||||
|
||||
// figure out the initial number of users in off-topic
|
||||
offTopicChannelId := store.Must(th.App.Srv.Store.Channel().GetByName(th.BasicTeam.Id, "off-topic", true)).(*model.Channel).Id
|
||||
channel, err := th.App.Srv.Store.Channel().GetByName(th.BasicTeam.Id, "off-topic", true)
|
||||
require.Nil(t, err)
|
||||
offTopicChannelId := channel.Id
|
||||
initialNumTownSquareUsers := len(store.Must(th.App.Srv.Store.ChannelMemberHistory().GetUsersInChannelDuring(model.GetMillis()-100, model.GetMillis()+100, offTopicChannelId)).([]*model.ChannelMemberHistoryResult))
|
||||
|
||||
// create a new user that joins the default channels
|
||||
@@ -453,7 +457,7 @@ func TestAddChannelMemberNoUserRequestor(t *testing.T) {
|
||||
assert.Equal(t, groupUserIds, channelMemberHistoryUserIds)
|
||||
|
||||
postList, err := th.App.Srv.Store.Post().GetPosts(channel.Id, 0, 1, false)
|
||||
require.Nil(t,err)
|
||||
require.Nil(t, err)
|
||||
|
||||
if assert.Len(t, postList.Order, 1) {
|
||||
post := postList.Posts[postList.Order[0]]
|
||||
|
||||
@@ -43,13 +43,11 @@ func (me *JoinProvider) DoCommand(a *App, args *model.CommandArgs, message strin
|
||||
channelName = message[1:]
|
||||
}
|
||||
|
||||
result := <-a.Srv.Store.Channel().GetByName(args.TeamId, channelName, true)
|
||||
if result.Err != nil {
|
||||
channel, err := a.Srv.Store.Channel().GetByName(args.TeamId, channelName, true)
|
||||
if err != nil {
|
||||
return &model.CommandResponse{Text: args.T("api.command_join.list.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||
}
|
||||
|
||||
channel := result.Data.(*model.Channel)
|
||||
|
||||
if channel.Name != channelName {
|
||||
return &model.CommandResponse{ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL, Text: args.T("api.command_join.missing.app_error")}
|
||||
}
|
||||
@@ -67,7 +65,7 @@ func (me *JoinProvider) DoCommand(a *App, args *model.CommandArgs, message strin
|
||||
return &model.CommandResponse{Text: args.T("api.command_join.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||
}
|
||||
|
||||
if err := a.JoinChannel(channel, args.UserId); err != nil {
|
||||
if err = a.JoinChannel(channel, args.UserId); err != nil {
|
||||
return &model.CommandResponse{Text: args.T("api.command_join.fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||
}
|
||||
|
||||
|
||||
@@ -64,8 +64,8 @@ func (me *msgProvider) DoCommand(a *App, args *model.CommandArgs, message string
|
||||
channelName := model.GetDMNameFromIds(args.UserId, userProfile.Id)
|
||||
|
||||
targetChannelId := ""
|
||||
if channel := <-a.Srv.Store.Channel().GetByName(args.TeamId, channelName, true); channel.Err != nil {
|
||||
if channel.Err.Id == "store.sql_channel.get_by_name.missing.app_error" {
|
||||
if channel, channelErr := a.Srv.Store.Channel().GetByName(args.TeamId, channelName, true); channelErr != nil {
|
||||
if channelErr.Id == "store.sql_channel.get_by_name.missing.app_error" {
|
||||
if !a.SessionHasPermissionTo(args.Session, model.PERMISSION_CREATE_DIRECT_CHANNEL) {
|
||||
return &model.CommandResponse{Text: args.T("api.command_msg.permission.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||
}
|
||||
@@ -77,11 +77,10 @@ func (me *msgProvider) DoCommand(a *App, args *model.CommandArgs, message string
|
||||
targetChannelId = directChannel.Id
|
||||
}
|
||||
} else {
|
||||
mlog.Error(channel.Err.Error())
|
||||
mlog.Error(channelErr.Error())
|
||||
return &model.CommandResponse{Text: args.T("api.command_msg.dm_fail.app_error"), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||
}
|
||||
} else {
|
||||
channel := channel.Data.(*model.Channel)
|
||||
targetChannelId = channel.Id
|
||||
}
|
||||
|
||||
|
||||
@@ -53,13 +53,11 @@ func (me *MuteProvider) DoCommand(a *App, args *model.CommandArgs, message strin
|
||||
}
|
||||
|
||||
if len(channelName) > 0 && len(message) > 0 {
|
||||
data := (<-a.Srv.Store.Channel().GetByName(channel.TeamId, channelName, true)).Data
|
||||
channel, _ = a.Srv.Store.Channel().GetByName(channel.TeamId, channelName, true)
|
||||
|
||||
if data == nil {
|
||||
if channel == nil {
|
||||
return &model.CommandResponse{Text: args.T("api.command_mute.error", map[string]interface{}{"Channel": channelName}), ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||
}
|
||||
|
||||
channel = data.(*model.Channel)
|
||||
}
|
||||
|
||||
channelMember := a.ToggleMuteChannel(channel.Id, args.UserId)
|
||||
|
||||
@@ -226,8 +226,8 @@ func (a *App) ImportChannel(data *ChannelImportData, dryRun bool) *model.AppErro
|
||||
}
|
||||
|
||||
var channel *model.Channel
|
||||
if result := <-a.Srv.Store.Channel().GetByNameIncludeDeleted(team.Id, *data.Name, true); result.Err == nil {
|
||||
channel = result.Data.(*model.Channel)
|
||||
if result, err := a.Srv.Store.Channel().GetByNameIncludeDeleted(team.Id, *data.Name, true); err == nil {
|
||||
channel = result
|
||||
} else {
|
||||
channel = &model.Channel{}
|
||||
}
|
||||
@@ -946,13 +946,12 @@ func (a *App) ImportPost(data *PostImportData, dryRun bool) *model.AppError {
|
||||
return model.NewAppError("BulkImport", "app.import.import_post.team_not_found.error", map[string]interface{}{"TeamName": *data.Team}, err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
result := <-a.Srv.Store.Channel().GetByName(team.Id, *data.Channel, false)
|
||||
if result.Err != nil {
|
||||
return model.NewAppError("BulkImport", "app.import.import_post.channel_not_found.error", map[string]interface{}{"ChannelName": *data.Channel}, result.Err.Error(), http.StatusBadRequest)
|
||||
channel, err := a.Srv.Store.Channel().GetByName(team.Id, *data.Channel, false)
|
||||
if err != nil {
|
||||
return model.NewAppError("BulkImport", "app.import.import_post.channel_not_found.error", map[string]interface{}{"ChannelName": *data.Channel}, err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
channel := result.Data.(*model.Channel)
|
||||
|
||||
result = <-a.Srv.Store.User().GetByUsername(*data.User)
|
||||
result := <-a.Srv.Store.User().GetByUsername(*data.User)
|
||||
if result.Err != nil {
|
||||
return model.NewAppError("BulkImport", "app.import.import_post.user_not_found.error", map[string]interface{}{"Username": *data.User}, result.Err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
@@ -506,9 +506,9 @@ func (a *App) SlackAddChannels(teamId string, slackchannels []SlackChannel, post
|
||||
newChannel = SlackSanitiseChannelProperties(newChannel)
|
||||
|
||||
var mChannel *model.Channel
|
||||
if result := <-a.Srv.Store.Channel().GetByName(teamId, sChannel.Name, true); result.Err == nil {
|
||||
var err *model.AppError
|
||||
if mChannel, err = a.Srv.Store.Channel().GetByName(teamId, sChannel.Name, true); err == nil {
|
||||
// The channel already exists as an active channel. Merge with the existing one.
|
||||
mChannel = result.Data.(*model.Channel)
|
||||
importerLog.WriteString(utils.T("api.slackimport.slack_add_channels.merge", map[string]interface{}{"DisplayName": newChannel.DisplayName}))
|
||||
} else if result := <-a.Srv.Store.Channel().GetDeletedByName(teamId, sChannel.Name); result.Err == nil {
|
||||
// The channel already exists but has been deleted. Generate a random string for the handle instead.
|
||||
|
||||
@@ -834,17 +834,16 @@ func (a *App) LeaveTeam(team *model.Team, user *model.User, requestorId string)
|
||||
for _, channel := range *channelList {
|
||||
if !channel.IsGroupOrDirect() {
|
||||
a.InvalidateCacheForChannelMembers(channel.Id)
|
||||
if err := a.Srv.Store.Channel().RemoveMember(channel.Id, user.Id); err != nil {
|
||||
if err = a.Srv.Store.Channel().RemoveMember(channel.Id, user.Id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result := <-a.Srv.Store.Channel().GetByName(team.Id, model.DEFAULT_CHANNEL, false)
|
||||
if result.Err != nil {
|
||||
return result.Err
|
||||
channel, err := a.Srv.Store.Channel().GetByName(team.Id, model.DEFAULT_CHANNEL, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
channel := result.Data.(*model.Channel)
|
||||
|
||||
if *a.Config().ServiceSettings.ExperimentalEnableDefaultChannelLeaveJoinMessages {
|
||||
if requestorId == user.Id {
|
||||
|
||||
@@ -599,9 +599,19 @@ func (a *App) HandleIncomingWebhook(hookId string, req *model.IncomingWebhookReq
|
||||
}
|
||||
}
|
||||
} else if channelName[0] == '#' {
|
||||
cchan = a.Srv.Store.Channel().GetByName(hook.TeamId, channelName[1:], true)
|
||||
cchan = make(store.StoreChannel, 1)
|
||||
go func() {
|
||||
chnn, chnnErr := a.Srv.Store.Channel().GetByName(hook.TeamId, channelName[1:], true)
|
||||
cchan <- store.StoreResult{Data: chnn, Err: chnnErr}
|
||||
close(cchan)
|
||||
}()
|
||||
} else {
|
||||
cchan = a.Srv.Store.Channel().GetByName(hook.TeamId, channelName, true)
|
||||
cchan = make(store.StoreChannel, 1)
|
||||
go func() {
|
||||
chnn, chnnErr := a.Srv.Store.Channel().GetByName(hook.TeamId, channelName, true)
|
||||
cchan <- store.StoreResult{Data: chnn, Err: chnnErr}
|
||||
close(cchan)
|
||||
}()
|
||||
}
|
||||
} else {
|
||||
var err *model.AppError
|
||||
|
||||
Ссылка в новой задаче
Block a user