* Migration completed

* Several corrections in tests

* Fix imports

* Fix some errors after testing

* Trigger CI

* Fix tests

* Suggestions

* Suggestions

* Add license

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Rodrigo Villablanca
2020-09-15 14:48:30 -03:00
коммит произвёл GitHub
родитель 7abc4f5383
Коммит 9ee9c78412
28 изменённых файлов: 1326 добавлений и 901 удалений

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

@@ -744,12 +744,12 @@ func (s SqlChannelStore) Get(id string, allowFromCache bool) (*model.Channel, er
return s.get(id, false, allowFromCache)
}
func (s SqlChannelStore) GetPinnedPosts(channelId string) (*model.PostList, *model.AppError) {
func (s SqlChannelStore) GetPinnedPosts(channelId string) (*model.PostList, error) {
pl := model.NewPostList()
var posts []*model.Post
if _, err := s.GetReplica().Select(&posts, "SELECT *, (SELECT count(Posts.Id) FROM Posts WHERE Posts.RootId = (CASE WHEN p.RootId = '' THEN p.Id ELSE p.RootId END) AND Posts.DeleteAt = 0) as ReplyCount FROM Posts p WHERE IsPinned = true AND ChannelId = :ChannelId AND DeleteAt = 0 ORDER BY CreateAt ASC", map[string]interface{}{"ChannelId": channelId}); err != nil {
return nil, model.NewAppError("SqlPostStore.GetPinnedPosts", "store.sql_channel.pinned_posts.app_error", nil, err.Error(), http.StatusInternalServerError)
return nil, errors.Wrap(err, "failed to find Posts")
}
for _, post := range posts {
pl.AddPost(post)
@@ -918,10 +918,10 @@ func (s SqlChannelStore) permanentDeleteT(transaction *gorp.Transaction, channel
return nil
}
func (s SqlChannelStore) PermanentDeleteMembersByChannel(channelId string) *model.AppError {
func (s SqlChannelStore) PermanentDeleteMembersByChannel(channelId string) error {
_, err := s.GetMaster().Exec("DELETE FROM ChannelMembers WHERE ChannelId = :ChannelId", map[string]interface{}{"ChannelId": channelId})
if err != nil {
return model.NewAppError("SqlChannelStore.RemoveAllMembersByChannel", "store.sql_channel.remove_member.app_error", nil, "channel_id="+channelId+", "+err.Error(), http.StatusInternalServerError)
return errors.Wrapf(err, "failed to delete Channel with channelId=%s", channelId)
}
return nil
@@ -1382,46 +1382,33 @@ var CHANNEL_MEMBERS_WITH_SCHEME_SELECT_QUERY = `
Schemes TeamScheme ON Teams.SchemeId = TeamScheme.Id
`
func (s SqlChannelStore) SaveMultipleMembers(members []*model.ChannelMember) ([]*model.ChannelMember, *model.AppError) {
func (s SqlChannelStore) SaveMultipleMembers(members []*model.ChannelMember) ([]*model.ChannelMember, error) {
for _, member := range members {
defer s.InvalidateAllChannelMembersForUser(member.UserId)
}
transaction, err := s.GetMaster().Begin()
if err != nil {
return nil, model.NewAppError("SqlChannelStore.SaveMember", "store.sql_channel.save_member.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
return nil, errors.Wrap(err, "begin_transaction")
}
defer finalizeTransaction(transaction)
newMembers, err := s.saveMultipleMembersT(transaction, members)
if err != nil { // TODO: this will go away once SaveMultipleMembers is migrated too.
var cErr *store.ErrConflict
var appErr *model.AppError
switch {
case errors.As(err, &cErr):
switch cErr.Resource {
case "ChannelMembers":
return nil, model.NewAppError("CreateChannel", "store.sql_channel.save_member.exists.app_error", nil, cErr.Error(), http.StatusBadRequest)
}
case errors.As(err, &appErr): // in case we haven't converted to plain error.
return nil, appErr
default: // last fallback in case it doesn't map to an existing app error.
// TODO: This error key would go away once this store method is migrated to return plain errors
return nil, model.NewAppError("CreateDirectChannel", "app.channel.create_direct_channel.internal_error", nil, err.Error(), http.StatusInternalServerError)
}
if err != nil {
return nil, err
}
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, errors.Wrap(err, "commit_transaction")
}
return newMembers, nil
}
func (s SqlChannelStore) SaveMember(member *model.ChannelMember) (*model.ChannelMember, *model.AppError) {
newMembers, appErr := s.SaveMultipleMembers([]*model.ChannelMember{member})
if appErr != nil {
return nil, appErr
func (s SqlChannelStore) SaveMember(member *model.ChannelMember) (*model.ChannelMember, error) {
newMembers, err := s.SaveMultipleMembers([]*model.ChannelMember{member})
if err != nil {
return nil, err
}
return newMembers[0], nil
}
@@ -1575,7 +1562,7 @@ func (s SqlChannelStore) saveMemberT(transaction *gorp.Transaction, member *mode
return members[0], nil
}
func (s SqlChannelStore) UpdateMultipleMembers(members []*model.ChannelMember) ([]*model.ChannelMember, *model.AppError) {
func (s SqlChannelStore) UpdateMultipleMembers(members []*model.ChannelMember) ([]*model.ChannelMember, error) {
for _, member := range members {
member.PreUpdate()
@@ -1588,34 +1575,34 @@ func (s SqlChannelStore) UpdateMultipleMembers(members []*model.ChannelMember) (
var err error
if transaction, err = s.GetMaster().Begin(); err != nil {
return nil, model.NewAppError("SqlChannelStore.MigrateChannelMembers", "store.sql_channel.migrate_channel_members.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
return nil, errors.Wrap(err, "begin_transaction")
}
defer finalizeTransaction(transaction)
updatedMembers := []*model.ChannelMember{}
for _, member := range members {
if _, err := transaction.Update(NewChannelMemberFromModel(member)); err != nil {
return nil, model.NewAppError("SqlChannelStore.UpdateMember", "store.sql_channel.update_member.app_error", nil, "channel_id="+member.ChannelId+", "+"user_id="+member.UserId+", "+err.Error(), http.StatusInternalServerError)
return nil, errors.Wrap(err, "failed to update ChannelMember")
}
// TODO: Get this out of the transaction when is possible
var dbMember channelMemberWithSchemeRoles
if err := transaction.SelectOne(&dbMember, CHANNEL_MEMBERS_WITH_SCHEME_SELECT_QUERY+"WHERE ChannelMembers.ChannelId = :ChannelId AND ChannelMembers.UserId = :UserId", map[string]interface{}{"ChannelId": member.ChannelId, "UserId": member.UserId}); err != nil {
if err == sql.ErrNoRows {
return nil, model.NewAppError("SqlChannelStore.GetMember", store.MISSING_CHANNEL_MEMBER_ERROR, nil, "channel_id="+member.ChannelId+"user_id="+member.UserId+","+err.Error(), http.StatusNotFound)
return nil, store.NewErrNotFound("ChannelMember", fmt.Sprintf("channelId=%s, userId=%s", member.ChannelId, member.UserId))
}
return nil, model.NewAppError("SqlChannelStore.GetMember", "store.sql_channel.get_member.app_error", nil, "channel_id="+member.ChannelId+"user_id="+member.UserId+","+err.Error(), http.StatusInternalServerError)
return nil, errors.Wrapf(err, "failed to get ChannelMember with channelId=%s and userId=%s", member.ChannelId, member.UserId)
}
updatedMembers = append(updatedMembers, dbMember.ToModel())
}
if err := transaction.Commit(); err != nil {
return nil, model.NewAppError("SqlChannelStore.MigrateChannelMembers", "store.sql_channel.migrate_channel_members.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
return nil, errors.Wrap(err, "commit_transaction")
}
return updatedMembers, nil
}
func (s SqlChannelStore) UpdateMember(member *model.ChannelMember) (*model.ChannelMember, *model.AppError) {
func (s SqlChannelStore) UpdateMember(member *model.ChannelMember) (*model.ChannelMember, error) {
updatedMembers, err := s.UpdateMultipleMembers([]*model.ChannelMember{member})
if err != nil {
return nil, err
@@ -1623,17 +1610,17 @@ func (s SqlChannelStore) UpdateMember(member *model.ChannelMember) (*model.Chann
return updatedMembers[0], nil
}
func (s SqlChannelStore) GetMembers(channelId string, offset, limit int) (*model.ChannelMembers, *model.AppError) {
func (s SqlChannelStore) GetMembers(channelId string, offset, limit int) (*model.ChannelMembers, error) {
var dbMembers channelMemberWithSchemeRolesList
_, err := s.GetReplica().Select(&dbMembers, CHANNEL_MEMBERS_WITH_SCHEME_SELECT_QUERY+"WHERE ChannelId = :ChannelId LIMIT :Limit OFFSET :Offset", map[string]interface{}{"ChannelId": channelId, "Limit": limit, "Offset": offset})
if err != nil {
return nil, model.NewAppError("SqlChannelStore.GetMembers", "store.sql_channel.get_members.app_error", nil, "channel_id="+channelId+","+err.Error(), http.StatusInternalServerError)
return nil, errors.Wrapf(err, "failed to get ChannelMembers with channelId=%s", channelId)
}
return dbMembers.ToModel(), nil
}
func (s SqlChannelStore) GetChannelMembersTimezones(channelId string) ([]model.StringMap, *model.AppError) {
func (s SqlChannelStore) GetChannelMembersTimezones(channelId string) ([]model.StringMap, error) {
var dbMembersTimezone []model.StringMap
_, err := s.GetReplica().Select(&dbMembersTimezone, `
SELECT
@@ -1646,20 +1633,20 @@ func (s SqlChannelStore) GetChannelMembersTimezones(channelId string) ([]model.S
`, map[string]interface{}{"ChannelId": channelId})
if err != nil {
return nil, model.NewAppError("SqlChannelStore.GetChannelMembersTimezones", "store.sql_channel.get_members.app_error", nil, "channel_id="+channelId+","+err.Error(), http.StatusInternalServerError)
return nil, errors.Wrapf(err, "failed to find user timezones for users in channels with channelId=%s", channelId)
}
return dbMembersTimezone, nil
}
func (s SqlChannelStore) GetMember(channelId string, userId string) (*model.ChannelMember, *model.AppError) {
func (s SqlChannelStore) GetMember(channelId string, userId string) (*model.ChannelMember, error) {
var dbMember channelMemberWithSchemeRoles
if err := s.GetReplica().SelectOne(&dbMember, CHANNEL_MEMBERS_WITH_SCHEME_SELECT_QUERY+"WHERE ChannelMembers.ChannelId = :ChannelId AND ChannelMembers.UserId = :UserId", map[string]interface{}{"ChannelId": channelId, "UserId": userId}); err != nil {
if err == sql.ErrNoRows {
return nil, model.NewAppError("SqlChannelStore.GetMember", store.MISSING_CHANNEL_MEMBER_ERROR, nil, "channel_id="+channelId+"user_id="+userId+","+err.Error(), http.StatusNotFound)
return nil, store.NewErrNotFound("ChannelMember", fmt.Sprintf("channelId=%s, userId=%s", channelId, userId))
}
return nil, model.NewAppError("SqlChannelStore.GetMember", "store.sql_channel.get_member.app_error", nil, "channel_id="+channelId+"user_id="+userId+","+err.Error(), http.StatusInternalServerError)
return nil, errors.Wrapf(err, "failed to get ChannelMember with channelId=%s and userId=%s", channelId, userId)
}
return dbMember.ToModel(), nil
@@ -1702,7 +1689,7 @@ func (s SqlChannelStore) IsUserInChannelUseCache(userId string, channelId string
return false
}
func (s SqlChannelStore) GetMemberForPost(postId string, userId string) (*model.ChannelMember, *model.AppError) {
func (s SqlChannelStore) GetMemberForPost(postId string, userId string) (*model.ChannelMember, error) {
var dbMember channelMemberWithSchemeRoles
query := `
SELECT
@@ -1730,12 +1717,12 @@ func (s SqlChannelStore) GetMemberForPost(postId string, userId string) (*model.
AND
Posts.Id = :PostId`
if err := s.GetReplica().SelectOne(&dbMember, query, map[string]interface{}{"UserId": userId, "PostId": postId}); err != nil {
return nil, model.NewAppError("SqlChannelStore.GetMemberForPost", "store.sql_channel.get_member_for_post.app_error", nil, "postId="+postId+", err="+err.Error(), http.StatusInternalServerError)
return nil, errors.Wrapf(err, "failed to get ChannelMember with postId=%s and userId=%s", postId, userId)
}
return dbMember.ToModel(), nil
}
func (s SqlChannelStore) GetAllChannelMembersForUser(userId string, allowFromCache bool, includeDeleted bool) (map[string]string, *model.AppError) {
func (s SqlChannelStore) GetAllChannelMembersForUser(userId string, allowFromCache bool, includeDeleted bool) (map[string]string, error) {
cache_key := userId
if includeDeleted {
cache_key += "_deleted"
@@ -1754,17 +1741,6 @@ func (s SqlChannelStore) GetAllChannelMembersForUser(userId string, allowFromCac
s.metrics.IncrementMemCacheMissCounter("All Channel Members for User")
}
failure := func(err error) *model.AppError {
// TODO: This error key would go away once this store method is migrated to return plain errors
return model.NewAppError(
"SqlChannelStore.GetAllChannelMembersForUser",
"app.channel.get_channels.get.app_error",
nil,
"userId="+userId+", err="+err.Error(),
http.StatusInternalServerError,
)
}
query := s.getQueryBuilder().
Select(`
ChannelMembers.ChannelId, ChannelMembers.Roles, ChannelMembers.SchemeGuest,
@@ -1787,12 +1763,12 @@ func (s SqlChannelStore) GetAllChannelMembersForUser(userId string, allowFromCac
}
queryString, args, err := query.ToSql()
if err != nil {
return nil, failure(err)
return nil, errors.Wrap(err, "channel_tosql")
}
rows, err := s.GetReplica().Db.Query(queryString, args...)
if err != nil {
return nil, failure(err)
return nil, errors.Wrap(err, "failed to find ChannelMembers, TeamScheme and ChannelScheme data")
}
var data allChannelMembers
@@ -1806,12 +1782,12 @@ func (s SqlChannelStore) GetAllChannelMembersForUser(userId string, allowFromCac
&cm.ChannelSchemeDefaultUserRole, &cm.ChannelSchemeDefaultAdminRole,
)
if err != nil {
return nil, failure(err)
return nil, errors.Wrap(err, "unable to scan columns")
}
data = append(data, cm)
}
if err = rows.Err(); err != nil {
return nil, failure(err)
return nil, errors.Wrap(err, "error while iterating over rows")
}
ids := data.ToMapStringString()
@@ -1833,7 +1809,7 @@ type allChannelMemberNotifyProps struct {
NotifyProps model.StringMap
}
func (s SqlChannelStore) GetAllChannelMembersNotifyPropsForChannel(channelId string, allowFromCache bool) (map[string]model.StringMap, *model.AppError) {
func (s SqlChannelStore) GetAllChannelMembersNotifyPropsForChannel(channelId string, allowFromCache bool) (map[string]model.StringMap, error) {
if allowFromCache {
var cacheItem map[string]model.StringMap
if err := allChannelMembersNotifyPropsForChannelCache.Get(channelId, &cacheItem); err == nil {
@@ -1855,7 +1831,7 @@ func (s SqlChannelStore) GetAllChannelMembersNotifyPropsForChannel(channelId str
WHERE ChannelId = :ChannelId`, map[string]interface{}{"ChannelId": channelId})
if err != nil {
return nil, model.NewAppError("SqlChannelStore.GetAllChannelMembersPropsForChannel", "store.sql_channel.get_members.app_error", nil, "channelId="+channelId+", err="+err.Error(), http.StatusInternalServerError)
return nil, errors.Wrapf(err, "failed to find data from ChannelMembers with channelId=%s", channelId)
}
props := make(map[string]model.StringMap)
@@ -1876,7 +1852,7 @@ func (s SqlChannelStore) GetMemberCountFromCache(channelId string) int64 {
return count
}
func (s SqlChannelStore) GetMemberCount(channelId string, allowFromCache bool) (int64, *model.AppError) {
func (s SqlChannelStore) GetMemberCount(channelId string, allowFromCache bool) (int64, error) {
count, err := s.GetReplica().SelectInt(`
SELECT
count(*)
@@ -1888,7 +1864,7 @@ func (s SqlChannelStore) GetMemberCount(channelId string, allowFromCache bool) (
AND ChannelMembers.ChannelId = :ChannelId
AND Users.DeleteAt = 0`, map[string]interface{}{"ChannelId": channelId})
if err != nil {
return 0, model.NewAppError("SqlChannelStore.GetMemberCount", "store.sql_channel.get_member_count.app_error", nil, "channel_id="+channelId+", "+err.Error(), http.StatusInternalServerError)
return 0, errors.Wrapf(err, "failed to count ChanenelMembers with channelId=%s", channelId)
}
return count, nil
@@ -1896,7 +1872,7 @@ func (s SqlChannelStore) GetMemberCount(channelId string, allowFromCache bool) (
// GetMemberCountsByGroup returns a slice of ChannelMemberCountByGroup for a given channel
// which contains the number of channel members for each group and optionally the number of unique timezones present for each group in the channel
func (s SqlChannelStore) GetMemberCountsByGroup(channelID string, includeTimezones bool) ([]*model.ChannelMemberCountByGroup, *model.AppError) {
func (s SqlChannelStore) GetMemberCountsByGroup(channelID string, includeTimezones bool) ([]*model.ChannelMemberCountByGroup, error) {
selectStr := "GroupMembers.GroupId, COUNT(ChannelMembers.UserId) AS ChannelMemberCount"
if includeTimezones {
@@ -1954,11 +1930,11 @@ func (s SqlChannelStore) GetMemberCountsByGroup(channelID string, includeTimezon
queryString, args, err := query.ToSql()
if err != nil {
return nil, model.NewAppError("SqlChannelStore.GetMemberCountsByGroup", "store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
return nil, errors.Wrap(err, "channel_tosql")
}
var data []*model.ChannelMemberCountByGroup
if _, err = s.GetReplica().Select(&data, queryString, args...); err != nil {
return nil, model.NewAppError("SqlChannelStore.GetMemberCountsByGroup", "store.sql_channel.get_member_count.app_error", nil, "channel_id="+channelID+", "+err.Error(), http.StatusInternalServerError)
return nil, errors.Wrapf(err, "failed to count ChannelMembers with channelId=%s", channelID)
}
return data, nil
@@ -1967,7 +1943,7 @@ func (s SqlChannelStore) GetMemberCountsByGroup(channelID string, includeTimezon
func (s SqlChannelStore) InvalidatePinnedPostCount(channelId string) {
}
func (s SqlChannelStore) GetPinnedPostCount(channelId string, allowFromCache bool) (int64, *model.AppError) {
func (s SqlChannelStore) GetPinnedPostCount(channelId string, allowFromCache bool) (int64, error) {
count, err := s.GetReplica().SelectInt(`
SELECT count(*)
FROM Posts
@@ -1977,7 +1953,7 @@ func (s SqlChannelStore) GetPinnedPostCount(channelId string, allowFromCache boo
AND DeleteAt = 0`, map[string]interface{}{"ChannelId": channelId})
if err != nil {
return 0, model.NewAppError("SqlChannelStore.GetPinnedPostCount", "store.sql_channel.get_pinnedpost_count.app_error", nil, "channel_id="+channelId+", "+err.Error(), http.StatusInternalServerError)
return 0, errors.Wrapf(err, "failed to count pinned Posts with channelId=%s", channelId)
}
return count, nil
@@ -1986,7 +1962,7 @@ func (s SqlChannelStore) GetPinnedPostCount(channelId string, allowFromCache boo
func (s SqlChannelStore) InvalidateGuestCount(channelId string) {
}
func (s SqlChannelStore) GetGuestCount(channelId string, allowFromCache bool) (int64, *model.AppError) {
func (s SqlChannelStore) GetGuestCount(channelId string, allowFromCache bool) (int64, error) {
count, err := s.GetReplica().SelectInt(`
SELECT
count(*)
@@ -1999,43 +1975,43 @@ func (s SqlChannelStore) GetGuestCount(channelId string, allowFromCache bool) (i
AND ChannelMembers.SchemeGuest = TRUE
AND Users.DeleteAt = 0`, map[string]interface{}{"ChannelId": channelId})
if err != nil {
return 0, model.NewAppError("SqlChannelStore.GetGuestCount", "store.sql_channel.get_member_count.app_error", nil, "channel_id="+channelId+", "+err.Error(), http.StatusInternalServerError)
return 0, errors.Wrapf(err, "failed to count Guests with channelId=%s", channelId)
}
return count, nil
}
func (s SqlChannelStore) RemoveMembers(channelId string, userIds []string) *model.AppError {
query := s.getQueryBuilder().
func (s SqlChannelStore) RemoveMembers(channelId string, userIds []string) error {
builder := s.getQueryBuilder().
Delete("ChannelMembers").
Where(sq.Eq{"ChannelId": channelId}).
Where(sq.Eq{"UserId": userIds})
sql, args, err := query.ToSql()
query, args, err := builder.ToSql()
if err != nil {
return model.NewAppError("SqlChannelStore.RemoveMember", "store.sql_channel.remove_member.app_error", nil, "channel_id="+channelId+", "+err.Error(), http.StatusInternalServerError)
return errors.Wrap(err, "channel_tosql")
}
_, err = s.GetMaster().Exec(sql, args...)
_, err = s.GetMaster().Exec(query, args...)
if err != nil {
return model.NewAppError("SqlChannelStore.RemoveMember", "store.sql_channel.remove_member.app_error", nil, "channel_id="+channelId+", "+err.Error(), http.StatusInternalServerError)
return errors.Wrap(err, "failed to delete ChannelMembers")
}
// cleanup sidebarchannels table if the user is no longer a member of that channel
sql, args, err = s.getQueryBuilder().
query, args, err = s.getQueryBuilder().
Delete("SidebarChannels").
Where(sq.And{
sq.Eq{"ChannelId": channelId},
sq.Eq{"UserId": userIds},
}).ToSql()
if err != nil {
return model.NewAppError("SqlChannelStore.RemoveMember", "store.sql_channel.remove_member.app_error", nil, "channel_id="+channelId+", "+err.Error(), http.StatusInternalServerError)
return errors.Wrap(err, "channel_tosql")
}
_, err = s.GetMaster().Exec(sql, args...)
_, err = s.GetMaster().Exec(query, args...)
if err != nil {
return model.NewAppError("SqlChannelStore.RemoveMember", "store.sql_channel.remove_member.app_error", nil, "channel_id="+channelId+", "+err.Error(), http.StatusInternalServerError)
return errors.Wrap(err, "failed to delete SidebarChannels")
}
return nil
}
func (s SqlChannelStore) RemoveMember(channelId string, userId string) *model.AppError {
func (s SqlChannelStore) RemoveMember(channelId string, userId string) error {
return s.RemoveMembers(channelId, []string{userId})
}
@@ -2064,14 +2040,14 @@ func (s SqlChannelStore) RemoveAllDeactivatedMembers(channelId string) *model.Ap
return nil
}
func (s SqlChannelStore) PermanentDeleteMembersByUser(userId string) *model.AppError {
func (s SqlChannelStore) PermanentDeleteMembersByUser(userId string) error {
if _, err := s.GetMaster().Exec("DELETE FROM ChannelMembers WHERE UserId = :UserId", map[string]interface{}{"UserId": userId}); err != nil {
return model.NewAppError("SqlChannelStore.ChannelPermanentDeleteMembersByUser", "store.sql_channel.permanent_delete_members_by_user.app_error", nil, "user_id="+userId+", "+err.Error(), http.StatusInternalServerError)
return errors.Wrapf(err, "failed to permanent delete ChannelMembers with userId=%s", userId)
}
return nil
}
func (s SqlChannelStore) UpdateLastViewedAt(channelIds []string, userId string) (map[string]int64, *model.AppError) {
func (s SqlChannelStore) UpdateLastViewedAt(channelIds []string, userId string) (map[string]int64, error) {
keys, props := MapStringsToQueryParams(channelIds, "Channel")
props["UserId"] = userId
@@ -2101,21 +2077,12 @@ func (s SqlChannelStore) UpdateLastViewedAt(channelIds []string, userId string)
}
_, err := s.GetMaster().Select(&lastPostAtTimes, query, props)
if err != nil || len(lastPostAtTimes) == 0 {
status := http.StatusInternalServerError
var extra string
if err == nil {
status = http.StatusBadRequest
extra = "No channels found"
} else {
extra = err.Error()
}
if err != nil {
return nil, errors.Wrapf(err, "failed to find ChannelMembers data with userId=%s and channelId in %v", userId, channelIds)
}
return nil, model.NewAppError("SqlChannelStore.UpdateLastViewedAt",
"store.sql_channel.update_last_viewed_at.app_error",
nil,
"channel_ids="+strings.Join(channelIds, ",")+", user_id="+userId+", "+extra,
status)
if len(lastPostAtTimes) == 0 {
return nil, store.NewErrInvalidInput("Channel", "Id", fmt.Sprintf("%v", channelIds))
}
times := map[string]int64{}
@@ -2152,14 +2119,14 @@ func (s SqlChannelStore) UpdateLastViewedAt(channelIds []string, userId string)
AND ChannelId IN ` + keys
if _, err := s.GetMaster().Exec(updateQuery, props); err != nil {
return nil, model.NewAppError("SqlChannelStore.UpdateLastViewedAt", "store.sql_channel.update_last_viewed_at.app_error", nil, "channel_ids="+strings.Join(channelIds, ",")+", user_id="+userId+", "+err.Error(), http.StatusInternalServerError)
return nil, errors.Wrapf(err, "failed to update ChannelMembers with userId=%s and channelId in %v", userId, channelIds)
}
return times, nil
}
// CountPostsAfter returns the number of posts in the given channel created after but not including the given timestamp. If given a non-empty user ID, only counts posts made by that user.
func (s SqlChannelStore) CountPostsAfter(channelId string, timestamp int64, userId string) (int, *model.AppError) {
func (s SqlChannelStore) CountPostsAfter(channelId string, timestamp int64, userId string) (int, error) {
joinLeavePostTypes, params := MapStringsToQueryParams([]string{
// These types correspond to the ones checked by Post.IsJoinLeaveMessage
model.POST_JOIN_LEAVE,
@@ -2194,7 +2161,7 @@ func (s SqlChannelStore) CountPostsAfter(channelId string, timestamp int64, user
unread, err := s.GetReplica().SelectInt(query, params)
if err != nil {
return 0, model.NewAppError("SqlChannelStore.CountPostsAfter", "store.sql_channel.count_posts_since.app_error", nil, fmt.Sprintf("channel_id=%s, timestamp=%d, err=%s", channelId, timestamp, err), http.StatusInternalServerError)
return 0, errors.Wrap(err, "failed to count Posts")
}
return int(unread), nil
}
@@ -2202,12 +2169,12 @@ func (s SqlChannelStore) CountPostsAfter(channelId string, timestamp int64, user
// UpdateLastViewedAtPost updates a ChannelMember as if the user last read the channel at the time of the given post.
// If the provided mentionCount is -1, the given post and all posts after it are considered to be mentions. Returns
// an updated model.ChannelUnreadAt that can be returned to the client.
func (s SqlChannelStore) UpdateLastViewedAtPost(unreadPost *model.Post, userID string, mentionCount int) (*model.ChannelUnreadAt, *model.AppError) {
func (s SqlChannelStore) UpdateLastViewedAtPost(unreadPost *model.Post, userID string, mentionCount int) (*model.ChannelUnreadAt, error) {
unreadDate := unreadPost.CreateAt - 1
unread, appErr := s.CountPostsAfter(unreadPost.ChannelId, unreadDate, "")
if appErr != nil {
return nil, appErr
unread, err := s.CountPostsAfter(unreadPost.ChannelId, unreadDate, "")
if err != nil {
return nil, err
}
params := map[string]interface{}{
@@ -2233,9 +2200,9 @@ func (s SqlChannelStore) UpdateLastViewedAtPost(unreadPost *model.Post, userID s
UserId = :userId
AND ChannelId = :channelId
`
_, err := s.GetMaster().Exec(setUnreadQuery, params)
_, err = s.GetMaster().Exec(setUnreadQuery, params)
if err != nil {
return nil, model.NewAppError("SqlChannelStore.UpdateLastViewedAtPost", "store.sql_channel.update_last_viewed_at_post.app_error", params, "Error setting channel "+unreadPost.ChannelId+" as unread: "+err.Error(), http.StatusInternalServerError)
return nil, errors.Wrap(err, "failed to update ChannelMembers")
}
chanUnreadQuery := `
@@ -2257,12 +2224,12 @@ func (s SqlChannelStore) UpdateLastViewedAtPost(unreadPost *model.Post, userID s
`
result := &model.ChannelUnreadAt{}
if err = s.GetMaster().SelectOne(result, chanUnreadQuery, params); err != nil {
return nil, model.NewAppError("SqlChannelStore.UpdateLastViewedAtPost", "store.sql_channel.update_last_viewed_at_post.app_error", params, "Error retrieving unread status from channel "+unreadPost.ChannelId+", error was: "+err.Error(), http.StatusInternalServerError)
return nil, errors.Wrapf(err, "failed to get ChannelMember with channelId=%s", unreadPost.ChannelId)
}
return result, nil
}
func (s SqlChannelStore) IncrementMentionCount(channelId string, userId string) *model.AppError {
func (s SqlChannelStore) IncrementMentionCount(channelId string, userId string) error {
_, err := s.GetMaster().Exec(
`UPDATE
ChannelMembers
@@ -2274,7 +2241,7 @@ func (s SqlChannelStore) IncrementMentionCount(channelId string, userId string)
AND ChannelId = :ChannelId`,
map[string]interface{}{"ChannelId": channelId, "UserId": userId, "LastUpdateAt": model.GetMillis()})
if err != nil {
return model.NewAppError("SqlChannelStore.IncrementMentionCount", "store.sql_channel.increment_mention_count.app_error", nil, "channel_id="+channelId+", user_id="+userId+", "+err.Error(), http.StatusInternalServerError)
return errors.Wrapf(err, "failed to Update ChannelMembers with channelId=%s and userId=%s", channelId, userId)
}
return nil
@@ -2325,7 +2292,7 @@ func (s SqlChannelStore) GetForPost(postId string) (*model.Channel, error) {
return channel, nil
}
func (s SqlChannelStore) AnalyticsTypeCount(teamId string, channelType string) (int64, *model.AppError) {
func (s SqlChannelStore) AnalyticsTypeCount(teamId string, channelType string) (int64, error) {
query := "SELECT COUNT(Id) AS Value FROM Channels WHERE Type = :ChannelType"
if len(teamId) > 0 {
@@ -2334,7 +2301,7 @@ func (s SqlChannelStore) AnalyticsTypeCount(teamId string, channelType string) (
value, err := s.GetReplica().SelectInt(query, map[string]interface{}{"TeamId": teamId, "ChannelType": channelType})
if err != nil {
return int64(0), model.NewAppError("SqlChannelStore.AnalyticsTypeCount", "store.sql_channel.analytics_type_count.app_error", nil, err.Error(), http.StatusInternalServerError)
return int64(0), errors.Wrap(err, "failed to count Channels")
}
return value, nil
}
@@ -2354,23 +2321,23 @@ func (s SqlChannelStore) AnalyticsDeletedTypeCount(teamId string, channelType st
return v, nil
}
func (s SqlChannelStore) GetMembersForUser(teamId string, userId string) (*model.ChannelMembers, *model.AppError) {
func (s SqlChannelStore) GetMembersForUser(teamId string, userId string) (*model.ChannelMembers, error) {
var dbMembers channelMemberWithSchemeRolesList
_, err := s.GetReplica().Select(&dbMembers, CHANNEL_MEMBERS_WITH_SCHEME_SELECT_QUERY+"WHERE ChannelMembers.UserId = :UserId AND (Teams.Id = :TeamId OR Teams.Id = '' OR Teams.Id IS NULL)", map[string]interface{}{"TeamId": teamId, "UserId": userId})
if err != nil {
return nil, model.NewAppError("SqlChannelStore.GetMembersForUser", "store.sql_channel.get_members.app_error", nil, "teamId="+teamId+", userId="+userId+", err="+err.Error(), http.StatusInternalServerError)
return nil, errors.Wrapf(err, "failed to find ChannelMembers data with teamId=%s and userId=%s", teamId, userId)
}
return dbMembers.ToModel(), nil
}
func (s SqlChannelStore) GetMembersForUserWithPagination(teamId, userId string, page, perPage int) (*model.ChannelMembers, *model.AppError) {
func (s SqlChannelStore) GetMembersForUserWithPagination(teamId, userId string, page, perPage int) (*model.ChannelMembers, error) {
var dbMembers channelMemberWithSchemeRolesList
offset := page * perPage
_, err := s.GetReplica().Select(&dbMembers, CHANNEL_MEMBERS_WITH_SCHEME_SELECT_QUERY+"WHERE ChannelMembers.UserId = :UserId Limit :Limit Offset :Offset", map[string]interface{}{"TeamId": teamId, "UserId": userId, "Limit": perPage, "Offset": offset})
if err != nil {
return nil, model.NewAppError("SqlChannelStore.GetMembersForUserWithPagination", "store.sql_channel.get_members.app_error", nil, "teamId="+teamId+", userId="+userId+", err="+err.Error(), http.StatusInternalServerError)
return nil, errors.Wrapf(err, "failed to find ChannelMembers data with teamId=%s and userId=%s", teamId, userId)
}
return dbMembers.ToModel(), nil
@@ -3175,7 +3142,7 @@ func (s SqlChannelStore) GetChannelMembersForExport(userId string, teamId string
map[string]interface{}{"TeamId": teamId, "UserId": userId})
if err != nil {
return nil, model.NewAppError("SqlChannelStore.GetChannelMembersForExport", "store.sql_channel.get_members.app_error", nil, "teamId="+teamId+", userId="+userId+", err="+err.Error(), http.StatusInternalServerError)
return nil, model.NewAppError("SqlChannelStore.GetChannelMembersForExport", "app.channel.get_members.app_error", nil, "teamId="+teamId+", userId="+userId+", err="+err.Error(), http.StatusInternalServerError)
}
return members, nil