MM-20649 Fix incorrect mention count when marking a DM channel as unread (#13245)

* MM-20649 Fix incorrect mention count when marking a DM channel as unread

* Satisfy govet

* Update missed test
Этот коммит содержится в:
Harrison Healey
2019-12-02 09:30:08 -05:00
коммит произвёл GitHub
родитель 4e5369e759
Коммит a0130b86d7
6 изменённых файлов: 70 добавлений и 47 удалений

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

@@ -1019,7 +1019,7 @@ func (s SqlChannelStore) GetPublicChannelsForTeam(teamId string, offset int, lim
PublicChannels pc ON (pc.Id = Channels.Id)
WHERE
pc.TeamId = :TeamId
AND pc.DeleteAt = 0
AND pc.DeleteAt = 0
ORDER BY pc.DisplayName
LIMIT :Limit
OFFSET :Offset
@@ -1229,15 +1229,15 @@ func (s SqlChannelStore) GetDeleted(teamId string, offset int, limit int, userId
channels := &model.ChannelList{}
query := `
SELECT * FROM Channels
WHERE (TeamId = :TeamId OR TeamId = '')
AND DeleteAt != 0
SELECT * FROM Channels
WHERE (TeamId = :TeamId OR TeamId = '')
AND DeleteAt != 0
AND Type != 'P'
UNION
SELECT * FROM Channels
WHERE (TeamId = :TeamId OR TeamId = '')
AND DeleteAt != 0
AND Type = 'P'
SELECT * FROM Channels
WHERE (TeamId = :TeamId OR TeamId = '')
AND DeleteAt != 0
AND Type = 'P'
AND Id IN (SELECT ChannelId FROM ChannelMembers WHERE UserId = :UserId)
ORDER BY DisplayName LIMIT :Limit OFFSET :Offset
`
@@ -1758,25 +1758,30 @@ func (s SqlChannelStore) UpdateLastViewedAt(channelIds []string, userId string)
return times, nil
}
// countPostsAfter returns the number of posts in the given channel created after but not including the given timestamp.
func (s SqlChannelStore) countPostsAfter(channelID string, since int64) (int64, *model.AppError) {
// 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) (int64, *model.AppError) {
countUnreadQuery := `
SELECT count(*)
FROM Posts
WHERE
ChannelId = :channelId
AND CreateAt > :createAt
AND Type = ''
ChannelId = :ChannelId
AND CreateAt > :CreateAt
AND Type = '' -- This line causes MM-20681
AND DeleteAt = 0
`
countParams := map[string]interface{}{
"channelId": channelID,
"createAt": since,
"ChannelId": channelId,
"CreateAt": timestamp,
}
if userId != "" {
countUnreadQuery += " AND UserId = :UserId"
countParams["UserId"] = userId
}
unread, err := s.GetReplica().SelectInt(countUnreadQuery, countParams)
if err != nil {
return 0, model.NewAppError("SqlChannelStore.countPostsAfter", "store.sql_channel.count_posts_since.app_error", countParams, fmt.Sprintf("channel_id=%s, since=%d, err=%s", channelID, since, err), http.StatusInternalServerError)
return 0, model.NewAppError("SqlChannelStore.CountPostsAfter", "store.sql_channel.count_posts_since.app_error", countParams, fmt.Sprintf("channel_id=%s, timestamp=%d, err=%s", channelId, timestamp, err), http.StatusInternalServerError)
}
return unread, nil
}
@@ -1787,16 +1792,11 @@ func (s SqlChannelStore) countPostsAfter(channelID string, since int64) (int64,
func (s SqlChannelStore) UpdateLastViewedAtPost(unreadPost *model.Post, userID string, mentionCount int) (*model.ChannelUnreadAt, *model.AppError) {
unreadDate := unreadPost.CreateAt - 1
unread, appErr := s.countPostsAfter(unreadPost.ChannelId, unreadDate)
unread, appErr := s.CountPostsAfter(unreadPost.ChannelId, unreadDate, "")
if appErr != nil {
return nil, appErr
}
if mentionCount == store.MentionAllPosts {
// Treat every unread post as a mention (like in a DM channel)
mentionCount = int(unread)
}
params := map[string]interface{}{
"mentions": mentionCount,
"unreadCount": unread,

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

@@ -9,10 +9,6 @@ import (
"github.com/mattermost/mattermost-server/v5/model"
)
const (
MentionAllPosts = -1
)
type StoreResult struct {
Data interface{}
Err *model.AppError
@@ -166,6 +162,7 @@ type ChannelStore interface {
PermanentDeleteMembersByChannel(channelId string) *model.AppError
UpdateLastViewedAt(channelIds []string, userId string) (map[string]int64, *model.AppError)
UpdateLastViewedAtPost(unreadPost *model.Post, userID string, mentionCount int) (*model.ChannelUnreadAt, *model.AppError)
CountPostsAfter(channelId string, timestamp int64, userId string) (int64, *model.AppError)
IncrementMentionCount(channelId string, userId string) *model.AppError
AnalyticsTypeCount(teamId string, channelType string) (int64, *model.AppError)
GetMembersForUser(teamId string, userId string) (*model.ChannelMembers, *model.AppError)

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

@@ -132,6 +132,29 @@ func (_m *ChannelStore) ClearCaches() {
_m.Called()
}
// CountPostsAfter provides a mock function with given fields: channelId, timestamp, userId
func (_m *ChannelStore) CountPostsAfter(channelId string, timestamp int64, userId string) (int64, *model.AppError) {
ret := _m.Called(channelId, timestamp, userId)
var r0 int64
if rf, ok := ret.Get(0).(func(string, int64, string) int64); ok {
r0 = rf(channelId, timestamp, userId)
} else {
r0 = ret.Get(0).(int64)
}
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string, int64, string) *model.AppError); ok {
r1 = rf(channelId, timestamp, userId)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
}
// CreateDirectChannel provides a mock function with given fields: userId, otherUserId
func (_m *ChannelStore) CreateDirectChannel(userId *model.User, otherUserId *model.User) (*model.Channel, *model.AppError) {
ret := _m.Called(userId, otherUserId)
@@ -769,20 +792,6 @@ func (_m *ChannelStore) GetGuestCount(channelId string, allowFromCache bool) (in
return r0, r1
}
// GetGuestCountFromCache provides a mock function with given fields: channelId
func (_m *ChannelStore) GetGuestCountFromCache(channelId string) int64 {
ret := _m.Called(channelId)
var r0 int64
if rf, ok := ret.Get(0).(func(string) int64); ok {
r0 = rf(channelId)
} else {
r0 = ret.Get(0).(int64)
}
return r0
}
// GetMember provides a mock function with given fields: channelId, userId
func (_m *ChannelStore) GetMember(channelId string, userId string) (*model.ChannelMember, *model.AppError) {
ret := _m.Called(channelId, userId)