Migration of ChannelStore Part 1 (#15235)
* Lint: remove unnecessary use of sprintf * Fix i18n * Returning the right error (InternalServerError) * Doing some suggestions * Fix store layers * Fix missed translation Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
f2a8e10216
Коммит
2cb655ed67
@@ -1085,10 +1085,10 @@ func (s SqlChannelStore) GetMoreChannels(teamId string, userId string, offset in
|
||||
return channels, nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) GetPrivateChannelsForTeam(teamId string, offset int, limit int) (*model.ChannelList, *model.AppError) {
|
||||
func (s SqlChannelStore) GetPrivateChannelsForTeam(teamId string, offset int, limit int) (*model.ChannelList, error) {
|
||||
channels := &model.ChannelList{}
|
||||
|
||||
query := s.getQueryBuilder().
|
||||
builder := s.getQueryBuilder().
|
||||
Select("*").
|
||||
From("Channels").
|
||||
Where(sq.Eq{"Type": model.CHANNEL_PRIVATE, "TeamId": teamId, "DeleteAt": 0}).
|
||||
@@ -1096,19 +1096,19 @@ func (s SqlChannelStore) GetPrivateChannelsForTeam(teamId string, offset int, li
|
||||
Limit(uint64(limit)).
|
||||
Offset(uint64(offset))
|
||||
|
||||
sql, args, err := query.ToSql()
|
||||
query, args, err := builder.ToSql()
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("SqlChannelStore.GetPrivateChannelsForTeam", "store.sql_channel.get_private_channels.get.app_error", nil, "teamId="+teamId+", err="+err.Error(), http.StatusInternalServerError)
|
||||
return nil, errors.Wrap(err, "channels_tosql")
|
||||
}
|
||||
|
||||
_, err = s.GetReplica().Select(channels, sql, args...)
|
||||
_, err = s.GetReplica().Select(channels, query, args...)
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("SqlChannelStore.GetPrivateChannelsForTeam", "store.sql_channel.get_private_channels.get.app_error", nil, "teamId="+teamId+", err="+err.Error(), http.StatusInternalServerError)
|
||||
return nil, errors.Wrapf(err, "failed to find chaneld with teamId=%s", teamId)
|
||||
}
|
||||
return channels, nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) GetPublicChannelsForTeam(teamId string, offset int, limit int) (*model.ChannelList, *model.AppError) {
|
||||
func (s SqlChannelStore) GetPublicChannelsForTeam(teamId string, offset int, limit int) (*model.ChannelList, error) {
|
||||
channels := &model.ChannelList{}
|
||||
_, err := s.GetReplica().Select(channels, `
|
||||
SELECT
|
||||
@@ -1130,13 +1130,13 @@ func (s SqlChannelStore) GetPublicChannelsForTeam(teamId string, offset int, lim
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("SqlChannelStore.GetPublicChannelsForTeam", "store.sql_channel.get_public_channels.get.app_error", nil, "teamId="+teamId+", err="+err.Error(), http.StatusInternalServerError)
|
||||
return nil, errors.Wrapf(err, "failed to find chaneld with teamId=%s", teamId)
|
||||
}
|
||||
|
||||
return channels, nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) (*model.ChannelList, *model.AppError) {
|
||||
func (s SqlChannelStore) GetPublicChannelsByIdsForTeam(teamId string, channelIds []string) (*model.ChannelList, error) {
|
||||
props := make(map[string]interface{})
|
||||
props["teamId"] = teamId
|
||||
|
||||
@@ -1167,11 +1167,11 @@ func (s SqlChannelStore) GetPublicChannelsByIdsForTeam(teamId string, channelIds
|
||||
`, props)
|
||||
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("SqlChannelStore.GetPublicChannelsByIdsForTeam", "store.sql_channel.get_channels_by_ids.get.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return nil, errors.Wrap(err, "failed to find Channels")
|
||||
}
|
||||
|
||||
if len(*data) == 0 {
|
||||
return nil, model.NewAppError("SqlChannelStore.GetPublicChannelsByIdsForTeam", "store.sql_channel.get_channels_by_ids.not_found.app_error", nil, "", http.StatusNotFound)
|
||||
return nil, store.NewErrNotFound("Channel", fmt.Sprintf("teamId=%s, channelIds=%v", teamId, channelIds))
|
||||
}
|
||||
|
||||
return data, nil
|
||||
@@ -1183,12 +1183,12 @@ type channelIdWithCountAndUpdateAt struct {
|
||||
UpdateAt int64
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) GetChannelCounts(teamId string, userId string) (*model.ChannelCounts, *model.AppError) {
|
||||
func (s SqlChannelStore) GetChannelCounts(teamId string, userId string) (*model.ChannelCounts, error) {
|
||||
var data []channelIdWithCountAndUpdateAt
|
||||
_, err := s.GetReplica().Select(&data, "SELECT Id, TotalMsgCount, UpdateAt FROM Channels WHERE Id IN (SELECT ChannelId FROM ChannelMembers WHERE UserId = :UserId) AND (TeamId = :TeamId OR TeamId = '') AND DeleteAt = 0 ORDER BY DisplayName", map[string]interface{}{"TeamId": teamId, "UserId": userId})
|
||||
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("SqlChannelStore.GetChannelCounts", "store.sql_channel.get_channel_counts.get.app_error", nil, "teamId="+teamId+", userId="+userId+", err="+err.Error(), http.StatusInternalServerError)
|
||||
return nil, errors.Wrapf(err, "failed to get channels count with teamId=%s and userId=%s", teamId, userId)
|
||||
}
|
||||
|
||||
counts := &model.ChannelCounts{Counts: make(map[string]int64), UpdateTimes: make(map[string]int64)}
|
||||
@@ -1201,18 +1201,16 @@ func (s SqlChannelStore) GetChannelCounts(teamId string, userId string) (*model.
|
||||
return counts, nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) GetTeamChannels(teamId string) (*model.ChannelList, *model.AppError) {
|
||||
func (s SqlChannelStore) GetTeamChannels(teamId string) (*model.ChannelList, error) {
|
||||
data := &model.ChannelList{}
|
||||
_, err := s.GetReplica().Select(data, "SELECT * FROM Channels WHERE TeamId = :TeamId And Type != 'D' ORDER BY DisplayName", map[string]interface{}{"TeamId": teamId})
|
||||
|
||||
if err != nil {
|
||||
// TODO: This error key would go away once this store method is migrated to return plain errors
|
||||
return nil, model.NewAppError("SqlChannelStore.GetTeamChannels", "app.channel.get_channels.get.app_error", nil, "teamId="+teamId+", err="+err.Error(), http.StatusInternalServerError)
|
||||
return nil, errors.Wrapf(err, "failed to find Channels with teamId=%s", teamId)
|
||||
}
|
||||
|
||||
if len(*data) == 0 {
|
||||
// TODO: This error key would go away once this store method is migrated to return plain errors
|
||||
return nil, model.NewAppError("SqlChannelStore.GetTeamChannels", "app.channel.get_channels.not_found.app_error", nil, "teamId="+teamId, http.StatusNotFound)
|
||||
return nil, store.NewErrNotFound("Channel", fmt.Sprintf("teamId=%s", teamId))
|
||||
}
|
||||
|
||||
return data, nil
|
||||
@@ -2282,18 +2280,18 @@ func (s SqlChannelStore) IncrementMentionCount(channelId string, userId string)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) GetAll(teamId string) ([]*model.Channel, *model.AppError) {
|
||||
func (s SqlChannelStore) GetAll(teamId string) ([]*model.Channel, error) {
|
||||
var data []*model.Channel
|
||||
_, err := s.GetReplica().Select(&data, "SELECT * FROM Channels WHERE TeamId = :TeamId AND Type != 'D' ORDER BY Name", map[string]interface{}{"TeamId": teamId})
|
||||
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("SqlChannelStore.GetAll", "store.sql_channel.get_all.app_error", nil, "teamId="+teamId+", err="+err.Error(), http.StatusInternalServerError)
|
||||
return nil, errors.Wrapf(err, "failed to find Channels with teamId=%s", teamId)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) GetChannelsByIds(channelIds []string, includeDeleted bool) ([]*model.Channel, *model.AppError) {
|
||||
func (s SqlChannelStore) GetChannelsByIds(channelIds []string, includeDeleted bool) ([]*model.Channel, error) {
|
||||
keys, params := MapStringsToQueryParams(channelIds, "Channel")
|
||||
query := `SELECT * FROM Channels WHERE Id IN ` + keys + ` ORDER BY Name`
|
||||
if !includeDeleted {
|
||||
@@ -2304,13 +2302,12 @@ func (s SqlChannelStore) GetChannelsByIds(channelIds []string, includeDeleted bo
|
||||
_, err := s.GetReplica().Select(&channels, query, params)
|
||||
|
||||
if err != nil {
|
||||
mlog.Error("Query error getting channels by ids", mlog.Err(err))
|
||||
return nil, model.NewAppError("SqlChannelStore.GetChannelsByIds", "store.sql_channel.get_channels_by_ids.app_error", nil, "", http.StatusInternalServerError)
|
||||
return nil, errors.Wrap(err, "failed to find Channels")
|
||||
}
|
||||
return channels, nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) GetForPost(postId string) (*model.Channel, *model.AppError) {
|
||||
func (s SqlChannelStore) GetForPost(postId string) (*model.Channel, error) {
|
||||
channel := &model.Channel{}
|
||||
if err := s.GetReplica().SelectOne(
|
||||
channel,
|
||||
@@ -2322,7 +2319,7 @@ func (s SqlChannelStore) GetForPost(postId string) (*model.Channel, *model.AppEr
|
||||
WHERE
|
||||
Channels.Id = Posts.ChannelId
|
||||
AND Posts.Id = :PostId`, map[string]interface{}{"PostId": postId}); err != nil {
|
||||
return nil, model.NewAppError("SqlChannelStore.GetForPost", "store.sql_channel.get_for_post.app_error", nil, "postId="+postId+", err="+err.Error(), http.StatusInternalServerError)
|
||||
return nil, errors.Wrapf(err, "failed to get Channel with postId=%s", postId)
|
||||
|
||||
}
|
||||
return channel, nil
|
||||
|
||||
@@ -899,15 +899,15 @@ func (s SqlChannelStore) DeleteSidebarChannelsByPreferences(preferences *model.P
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) UpdateSidebarChannelCategoryOnMove(channel *model.Channel, newTeamId string) *model.AppError {
|
||||
func (s SqlChannelStore) UpdateSidebarChannelCategoryOnMove(channel *model.Channel, newTeamId string) error {
|
||||
// if channel is being moved, remove it from the categories, since it's possible that there's no matching category in the new team
|
||||
if _, err := s.GetMaster().Exec("DELETE FROM SidebarChannels WHERE ChannelId=:ChannelId", map[string]interface{}{"ChannelId": channel.Id}); err != nil {
|
||||
return model.NewAppError("SqlChannelStore.UpdateSidebarChannelCategoryOnMove", "store.sql_channel.sidebar_categories.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return errors.Wrapf(err, "failed to delete SidebarChannels with channelId=%s", channel.Id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SqlChannelStore) ClearSidebarOnTeamLeave(userId, teamId string) *model.AppError {
|
||||
func (s SqlChannelStore) ClearSidebarOnTeamLeave(userId, teamId string) error {
|
||||
// if user leaves the team, clean his team related entries in sidebar channels and categories
|
||||
params := map[string]interface{}{
|
||||
"UserId": userId,
|
||||
@@ -921,10 +921,10 @@ func (s SqlChannelStore) ClearSidebarOnTeamLeave(userId, teamId string) *model.A
|
||||
deleteQuery = "DELETE FROM SidebarChannels USING SidebarChannels AS chan LEFT OUTER JOIN SidebarCategories AS cat ON cat.Id = chan.CategoryId WHERE cat.UserId = :UserId AND cat.TeamId = :TeamId"
|
||||
}
|
||||
if _, err := s.GetMaster().Exec(deleteQuery, params); err != nil {
|
||||
return model.NewAppError("SqlChannelStore.ClearSidebarOnTeamLeave", "store.sql_channel.sidebar_categories.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return errors.Wrap(err, "failed to delete from SidebarChannels")
|
||||
}
|
||||
if _, err := s.GetMaster().Exec("DELETE FROM SidebarCategories WHERE SidebarCategories.TeamId = :TeamId AND SidebarCategories.UserId = :UserId", params); err != nil {
|
||||
return model.NewAppError("SqlChannelStore.ClearSidebarOnTeamLeave", "store.sql_channel.sidebar_categories.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
return errors.Wrap(err, "failed to delete from SidebarCategories")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user