Migrate to idiomatic error handling the first half to the app/channel.go (#9413)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
6c826e765f
Коммит
c2496911c0
153
app/channel.go
153
app/channel.go
@@ -38,11 +38,11 @@ func (a *App) JoinDefaultChannels(teamId string, user *model.User, shouldBeAdmin
|
||||
|
||||
var requestor *model.User
|
||||
if userRequestorId != "" {
|
||||
if u := <-a.Srv.Store.User().Get(userRequestorId); u.Err != nil {
|
||||
u := <-a.Srv.Store.User().Get(userRequestorId)
|
||||
if u.Err != nil {
|
||||
return u.Err
|
||||
} else {
|
||||
requestor = u.Data.(*model.User)
|
||||
}
|
||||
requestor = u.Data.(*model.User)
|
||||
}
|
||||
|
||||
defaultChannelList := []string{"town-square"}
|
||||
@@ -130,13 +130,14 @@ func (a *App) CreateChannelWithUser(channel *model.Channel, userId string) (*mod
|
||||
}
|
||||
|
||||
// Get total number of channels on current team
|
||||
if count, err := a.GetNumberOfChannelsOnTeam(channel.TeamId); err != nil {
|
||||
count, err := a.GetNumberOfChannelsOnTeam(channel.TeamId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
}
|
||||
|
||||
if int64(count+1) > *a.Config().TeamSettings.MaxChannelsPerTeam {
|
||||
return nil, model.NewAppError("CreateChannelWithUser", "api.channel.create_channel.max_channel_limit.app_error", map[string]interface{}{"MaxChannelsPerTeam": *a.Config().TeamSettings.MaxChannelsPerTeam}, "", http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
channel.CreatorId = userId
|
||||
|
||||
@@ -176,9 +177,11 @@ func (a *App) RenameChannel(channel *model.Channel, newChannelName string, newDi
|
||||
}
|
||||
|
||||
func (a *App) CreateChannel(channel *model.Channel, addMember bool) (*model.Channel, *model.AppError) {
|
||||
if result := <-a.Srv.Store.Channel().Save(channel, *a.Config().TeamSettings.MaxChannelsPerTeam); result.Err != nil {
|
||||
result := <-a.Srv.Store.Channel().Save(channel, *a.Config().TeamSettings.MaxChannelsPerTeam)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
}
|
||||
|
||||
sc := result.Data.(*model.Channel)
|
||||
|
||||
if addMember {
|
||||
@@ -212,16 +215,16 @@ func (a *App) CreateChannel(channel *model.Channel, addMember bool) (*model.Chan
|
||||
|
||||
return sc, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) CreateDirectChannel(userId string, otherUserId string) (*model.Channel, *model.AppError) {
|
||||
if channel, err := a.createDirectChannel(userId, otherUserId); err != nil {
|
||||
channel, err := a.createDirectChannel(userId, otherUserId)
|
||||
if err != nil {
|
||||
if err.Id == store.CHANNEL_EXISTS_ERROR {
|
||||
return channel, nil
|
||||
} else {
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
|
||||
a.WaitForChannelMembership(channel.Id, userId)
|
||||
|
||||
a.InvalidateCacheForUser(userId)
|
||||
@@ -243,7 +246,6 @@ func (a *App) CreateDirectChannel(userId string, otherUserId string) (*model.Cha
|
||||
|
||||
return channel, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) createDirectChannel(userId string, otherUserId string) (*model.Channel, *model.AppError) {
|
||||
uc1 := a.Srv.Store.User().Get(userId)
|
||||
@@ -257,13 +259,14 @@ func (a *App) createDirectChannel(userId string, otherUserId string) (*model.Cha
|
||||
return nil, model.NewAppError("CreateDirectChannel", "api.channel.create_direct_channel.invalid_user.app_error", nil, otherUserId, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if result := <-a.Srv.Store.Channel().CreateDirectChannel(userId, otherUserId); result.Err != nil {
|
||||
result := <-a.Srv.Store.Channel().CreateDirectChannel(userId, otherUserId)
|
||||
if result.Err != nil {
|
||||
if result.Err.Id == store.CHANNEL_EXISTS_ERROR {
|
||||
return result.Data.(*model.Channel), result.Err
|
||||
} else {
|
||||
}
|
||||
return nil, result.Err
|
||||
}
|
||||
} else {
|
||||
|
||||
channel := result.Data.(*model.Channel)
|
||||
|
||||
if result := <-a.Srv.Store.ChannelMemberHistory().LogJoinEvent(userId, channel.Id, model.GetMillis()); result.Err != nil {
|
||||
@@ -275,10 +278,12 @@ func (a *App) createDirectChannel(userId string, otherUserId string) (*model.Cha
|
||||
|
||||
return channel, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) WaitForChannelMembership(channelId string, userId string) {
|
||||
if len(a.Config().SqlSettings.DataSourceReplicas) > 0 {
|
||||
if len(a.Config().SqlSettings.DataSourceReplicas) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
now := model.GetMillis()
|
||||
|
||||
for model.GetMillis()-now < 12000 {
|
||||
@@ -300,16 +305,16 @@ func (a *App) WaitForChannelMembership(channelId string, userId string) {
|
||||
|
||||
mlog.Error(fmt.Sprintf("WaitForChannelMembership giving up channelId=%v userId=%v", channelId, userId), mlog.String("user_id", userId))
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) CreateGroupChannel(userIds []string, creatorId string) (*model.Channel, *model.AppError) {
|
||||
if channel, err := a.createGroupChannel(userIds, creatorId); err != nil {
|
||||
channel, err := a.createGroupChannel(userIds, creatorId)
|
||||
if err != nil {
|
||||
if err.Id == store.CHANNEL_EXISTS_ERROR {
|
||||
return channel, nil
|
||||
} else {
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
|
||||
for _, userId := range userIds {
|
||||
if userId == creatorId {
|
||||
a.WaitForChannelMembership(channel.Id, creatorId)
|
||||
@@ -324,19 +329,17 @@ func (a *App) CreateGroupChannel(userIds []string, creatorId string) (*model.Cha
|
||||
|
||||
return channel, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) createGroupChannel(userIds []string, creatorId string) (*model.Channel, *model.AppError) {
|
||||
if len(userIds) > model.CHANNEL_GROUP_MAX_USERS || len(userIds) < model.CHANNEL_GROUP_MIN_USERS {
|
||||
return nil, model.NewAppError("CreateGroupChannel", "api.channel.create_group.bad_size.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
var users []*model.User
|
||||
if result := <-a.Srv.Store.User().GetProfileByIds(userIds, true); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().GetProfileByIds(userIds, true)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
users = result.Data.([]*model.User)
|
||||
}
|
||||
users := result.Data.([]*model.User)
|
||||
|
||||
if len(users) != len(userIds) {
|
||||
return nil, model.NewAppError("CreateGroupChannel", "api.channel.create_group.bad_user.app_error", nil, "user_ids="+model.ArrayToJson(userIds), http.StatusBadRequest)
|
||||
@@ -348,13 +351,13 @@ func (a *App) createGroupChannel(userIds []string, creatorId string) (*model.Cha
|
||||
Type: model.CHANNEL_GROUP,
|
||||
}
|
||||
|
||||
if result := <-a.Srv.Store.Channel().Save(group, *a.Config().TeamSettings.MaxChannelsPerTeam); result.Err != nil {
|
||||
result = <-a.Srv.Store.Channel().Save(group, *a.Config().TeamSettings.MaxChannelsPerTeam)
|
||||
if result.Err != nil {
|
||||
if result.Err.Id == store.CHANNEL_EXISTS_ERROR {
|
||||
return result.Data.(*model.Channel), result.Err
|
||||
} else {
|
||||
}
|
||||
return nil, result.Err
|
||||
}
|
||||
} else {
|
||||
channel := result.Data.(*model.Channel)
|
||||
|
||||
for _, user := range users {
|
||||
@@ -375,19 +378,17 @@ func (a *App) createGroupChannel(userIds []string, creatorId string) (*model.Cha
|
||||
|
||||
return channel, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) GetGroupChannel(userIds []string) (*model.Channel, *model.AppError) {
|
||||
if len(userIds) > model.CHANNEL_GROUP_MAX_USERS || len(userIds) < model.CHANNEL_GROUP_MIN_USERS {
|
||||
return nil, model.NewAppError("GetGroupChannel", "api.channel.create_group.bad_size.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
var users []*model.User
|
||||
if result := <-a.Srv.Store.User().GetProfileByIds(userIds, true); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().GetProfileByIds(userIds, true)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
users = result.Data.([]*model.User)
|
||||
}
|
||||
users := result.Data.([]*model.User)
|
||||
|
||||
if len(users) != len(userIds) {
|
||||
return nil, model.NewAppError("GetGroupChannel", "api.channel.create_group.bad_user.app_error", nil, "user_ids="+model.ArrayToJson(userIds), http.StatusBadRequest)
|
||||
@@ -402,9 +403,11 @@ func (a *App) GetGroupChannel(userIds []string) (*model.Channel, *model.AppError
|
||||
}
|
||||
|
||||
func (a *App) UpdateChannel(channel *model.Channel) (*model.Channel, *model.AppError) {
|
||||
if result := <-a.Srv.Store.Channel().Update(channel); result.Err != nil {
|
||||
result := <-a.Srv.Store.Channel().Update(channel)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
}
|
||||
|
||||
a.InvalidateCacheForChannel(channel)
|
||||
|
||||
messageWs := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_UPDATED, "", channel.Id, "", nil)
|
||||
@@ -413,7 +416,6 @@ func (a *App) UpdateChannel(channel *model.Channel) (*model.Channel, *model.AppE
|
||||
|
||||
return channel, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) UpdateChannelScheme(channel *model.Channel) (*model.Channel, *model.AppError) {
|
||||
var oldChannel *model.Channel
|
||||
@@ -433,9 +435,11 @@ func (a *App) UpdateChannelScheme(channel *model.Channel) (*model.Channel, *mode
|
||||
}
|
||||
|
||||
func (a *App) UpdateChannelPrivacy(oldChannel *model.Channel, user *model.User) (*model.Channel, *model.AppError) {
|
||||
if channel, err := a.UpdateChannel(oldChannel); err != nil {
|
||||
channel, err := a.UpdateChannel(oldChannel)
|
||||
if err != nil {
|
||||
return channel, err
|
||||
} else {
|
||||
}
|
||||
|
||||
if err := a.postChannelPrivacyMessage(user, channel); err != nil {
|
||||
if channel.Type == model.CHANNEL_OPEN {
|
||||
channel.Type = model.CHANNEL_PRIVATE
|
||||
@@ -455,7 +459,6 @@ func (a *App) UpdateChannelPrivacy(oldChannel *model.Channel, user *model.User)
|
||||
|
||||
return channel, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) postChannelPrivacyMessage(user *model.User, channel *model.Channel) *model.AppError {
|
||||
message := (map[string]string{
|
||||
@@ -480,11 +483,11 @@ func (a *App) postChannelPrivacyMessage(user *model.User, channel *model.Channel
|
||||
}
|
||||
|
||||
func (a *App) RestoreChannel(channel *model.Channel) (*model.Channel, *model.AppError) {
|
||||
if result := <-a.Srv.Store.Channel().Restore(channel.Id, model.GetMillis()); result.Err != nil {
|
||||
result := <-a.Srv.Store.Channel().Restore(channel.Id, model.GetMillis())
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
return channel, nil
|
||||
}
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
func (a *App) PatchChannel(channel *model.Channel, patch *model.ChannelPatch, userId string) (*model.Channel, *model.AppError) {
|
||||
@@ -528,11 +531,11 @@ func (a *App) GetSchemeRolesForChannel(channelId string) (string, string, *model
|
||||
}
|
||||
|
||||
if channel.SchemeId != nil && len(*channel.SchemeId) != 0 {
|
||||
if scheme, err := a.GetScheme(*channel.SchemeId); err != nil {
|
||||
scheme, err := a.GetScheme(*channel.SchemeId)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
} else {
|
||||
return scheme.DefaultChannelUserRole, scheme.DefaultChannelAdminRole, nil
|
||||
}
|
||||
return scheme.DefaultChannelUserRole, scheme.DefaultChannelAdminRole, nil
|
||||
}
|
||||
|
||||
var team *model.Team
|
||||
@@ -542,11 +545,11 @@ func (a *App) GetSchemeRolesForChannel(channelId string) (string, string, *model
|
||||
}
|
||||
|
||||
if team.SchemeId != nil && len(*team.SchemeId) != 0 {
|
||||
if scheme, err := a.GetScheme(*team.SchemeId); err != nil {
|
||||
scheme, err := a.GetScheme(*team.SchemeId)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
} else {
|
||||
return scheme.DefaultChannelUserRole, scheme.DefaultChannelAdminRole, nil
|
||||
}
|
||||
return scheme.DefaultChannelUserRole, scheme.DefaultChannelAdminRole, nil
|
||||
}
|
||||
|
||||
return model.CHANNEL_USER_ROLE_ID, model.CHANNEL_ADMIN_ROLE_ID, nil
|
||||
@@ -569,10 +572,13 @@ func (a *App) UpdateChannelMemberRoles(channelId string, userId string, newRoles
|
||||
member.SchemeAdmin = false
|
||||
|
||||
for _, roleName := range strings.Fields(newRoles) {
|
||||
if role, err := a.GetRoleByName(roleName); err != nil {
|
||||
role, err := a.GetRoleByName(roleName)
|
||||
if err != nil {
|
||||
err.StatusCode = http.StatusBadRequest
|
||||
return nil, err
|
||||
} else if !role.SchemeManaged {
|
||||
}
|
||||
|
||||
if !role.SchemeManaged {
|
||||
// The role is not scheme-managed, so it's OK to apply it to the explicit roles field.
|
||||
newExplicitRoles = append(newExplicitRoles, roleName)
|
||||
} else {
|
||||
@@ -591,11 +597,11 @@ func (a *App) UpdateChannelMemberRoles(channelId string, userId string, newRoles
|
||||
|
||||
member.ExplicitRoles = strings.Join(newExplicitRoles, " ")
|
||||
|
||||
if result := <-a.Srv.Store.Channel().UpdateMember(member); result.Err != nil {
|
||||
result := <-a.Srv.Store.Channel().UpdateMember(member)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
member = result.Data.(*model.ChannelMember)
|
||||
}
|
||||
member = result.Data.(*model.ChannelMember)
|
||||
|
||||
a.InvalidateCacheForUser(userId)
|
||||
return member, nil
|
||||
@@ -615,11 +621,11 @@ func (a *App) UpdateChannelMemberSchemeRoles(channelId string, userId string, is
|
||||
member.ExplicitRoles = RemoveRoles([]string{model.CHANNEL_USER_ROLE_ID, model.CHANNEL_ADMIN_ROLE_ID}, member.ExplicitRoles)
|
||||
}
|
||||
|
||||
if result := <-a.Srv.Store.Channel().UpdateMember(member); result.Err != nil {
|
||||
result := <-a.Srv.Store.Channel().UpdateMember(member)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
member = result.Data.(*model.ChannelMember)
|
||||
}
|
||||
member = result.Data.(*model.ChannelMember)
|
||||
|
||||
a.InvalidateCacheForUser(userId)
|
||||
return member, nil
|
||||
@@ -649,9 +655,11 @@ func (a *App) UpdateChannelMemberNotifyProps(data map[string]string, channelId s
|
||||
member.NotifyProps[model.PUSH_NOTIFY_PROP] = push
|
||||
}
|
||||
|
||||
if result := <-a.Srv.Store.Channel().UpdateMember(member); result.Err != nil {
|
||||
result := <-a.Srv.Store.Channel().UpdateMember(member)
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
}
|
||||
|
||||
a.InvalidateCacheForUser(userId)
|
||||
a.InvalidateCacheForChannelMembersNotifyProps(channelId)
|
||||
// Notify the clients that the member notify props changed
|
||||
@@ -660,7 +668,6 @@ func (a *App) UpdateChannelMemberNotifyProps(data map[string]string, channelId s
|
||||
a.Publish(evt)
|
||||
return member, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) DeleteChannel(channel *model.Channel, userId string) *model.AppError {
|
||||
ihc := a.Srv.Store.Webhook().GetIncomingByChannel(channel.Id)
|
||||
@@ -676,11 +683,16 @@ func (a *App) DeleteChannel(channel *model.Channel, userId string) *model.AppErr
|
||||
user = uresult.Data.(*model.User)
|
||||
}
|
||||
|
||||
if ihcresult := <-ihc; ihcresult.Err != nil {
|
||||
ihcresult := <-ihc
|
||||
if ihcresult.Err != nil {
|
||||
return ihcresult.Err
|
||||
} else if ohcresult := <-ohc; ohcresult.Err != nil {
|
||||
}
|
||||
|
||||
ohcresult := <-ohc
|
||||
if ohcresult.Err != nil {
|
||||
return ohcresult.Err
|
||||
} else {
|
||||
}
|
||||
|
||||
incomingHooks := ihcresult.Data.([]*model.IncomingWebhook)
|
||||
outgoingHooks := ohcresult.Data.([]*model.OutgoingWebhook)
|
||||
|
||||
@@ -737,7 +749,6 @@ func (a *App) DeleteChannel(channel *model.Channel, userId string) *model.AppErr
|
||||
message.Add("channel_id", channel.Id)
|
||||
message.Add("delete_at", deleteAt)
|
||||
a.Publish(message)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -784,14 +795,14 @@ func (a *App) AddUserToChannel(user *model.User, channel *model.Channel) (*model
|
||||
tmchan := a.Srv.Store.Team().GetMember(channel.TeamId, user.Id)
|
||||
var teamMember *model.TeamMember
|
||||
|
||||
if result := <-tmchan; result.Err != nil {
|
||||
result := <-tmchan
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
} else {
|
||||
}
|
||||
teamMember = result.Data.(*model.TeamMember)
|
||||
if teamMember.DeleteAt > 0 {
|
||||
return nil, model.NewAppError("AddUserToChannel", "api.channel.add_user.to.channel.failed.deleted.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
newMember, err := a.addUserToChannel(user, channel, teamMember)
|
||||
if err != nil {
|
||||
@@ -861,11 +872,11 @@ func (a *App) AddChannelMember(userId string, channel *model.Channel, userReques
|
||||
|
||||
func (a *App) AddDirectChannels(teamId string, user *model.User) *model.AppError {
|
||||
var profiles []*model.User
|
||||
if result := <-a.Srv.Store.User().GetProfiles(teamId, 0, 100); result.Err != nil {
|
||||
result := <-a.Srv.Store.User().GetProfiles(teamId, 0, 100)
|
||||
if result.Err != nil {
|
||||
return model.NewAppError("AddDirectChannels", "api.user.add_direct_channels_and_forget.failed.error", map[string]interface{}{"UserId": user.Id, "TeamId": teamId, "Error": result.Err.Error()}, "", http.StatusInternalServerError)
|
||||
} else {
|
||||
profiles = result.Data.([]*model.User)
|
||||
}
|
||||
profiles = result.Data.([]*model.User)
|
||||
|
||||
var preferences model.Preferences
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user