MM-29703 Mark threads as read when channels are marked (#15994)

Co-authored-by: Jesús Espino <jespinog@gmail.com>
Этот коммит содержится в:
Eli Yukelzon
2020-10-30 17:00:21 +02:00
коммит произвёл GitHub
родитель 1729239385
Коммит fe352ab57f
20 изменённых файлов: 451 добавлений и 92 удалений

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

@@ -2046,7 +2046,17 @@ func (s SqlChannelStore) PermanentDeleteMembersByUser(userId string) error {
return nil
}
func (s SqlChannelStore) UpdateLastViewedAt(channelIds []string, userId string) (map[string]int64, error) {
func (s SqlChannelStore) UpdateLastViewedAt(channelIds []string, userId string, updateThreads bool) (map[string]int64, error) {
var threadsToUpdate []string
now := model.GetMillis()
if updateThreads {
var err error
threadsToUpdate, err = s.Thread().CollectThreadsWithNewerReplies(userId, channelIds, now)
if err != nil {
return nil, err
}
}
keys, props := MapStringsToQueryParams(channelIds, "Channel")
props["UserId"] = userId
@@ -2089,11 +2099,15 @@ func (s SqlChannelStore) UpdateLastViewedAt(channelIds []string, userId string)
for _, t := range lastPostAtTimes {
times[t.Id] = t.LastPostAt
}
if updateThreads {
s.Thread().UpdateUnreadsByChannel(userId, threadsToUpdate, now)
}
return times, nil
}
msgCountQuery := ""
lastViewedQuery := ""
for index, t := range lastPostAtTimes {
times[t.Id] = t.LastPostAt
@@ -2121,6 +2135,9 @@ func (s SqlChannelStore) UpdateLastViewedAt(channelIds []string, userId string)
return nil, errors.Wrapf(err, "failed to update ChannelMembers with userId=%s and channelId in %v", userId, channelIds)
}
if updateThreads {
s.Thread().UpdateUnreadsByChannel(userId, threadsToUpdate, now)
}
return times, nil
}
@@ -2168,8 +2185,16 @@ 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, error) {
func (s SqlChannelStore) UpdateLastViewedAtPost(unreadPost *model.Post, userID string, mentionCount int, updateThreads bool) (*model.ChannelUnreadAt, error) {
var threadsToUpdate []string
unreadDate := unreadPost.CreateAt - 1
if updateThreads {
var err error
threadsToUpdate, err = s.Thread().CollectThreadsWithNewerReplies(userID, []string{unreadPost.ChannelId}, unreadDate)
if err != nil {
return nil, err
}
}
unread, err := s.CountPostsAfter(unreadPost.ChannelId, unreadDate, "")
if err != nil {
@@ -2225,10 +2250,24 @@ func (s SqlChannelStore) UpdateLastViewedAtPost(unreadPost *model.Post, userID s
if err = s.GetMaster().SelectOne(result, chanUnreadQuery, params); err != nil {
return nil, errors.Wrapf(err, "failed to get ChannelMember with channelId=%s", unreadPost.ChannelId)
}
if updateThreads {
s.Thread().UpdateUnreadsByChannel(userID, threadsToUpdate, unreadDate)
}
return result, nil
}
func (s SqlChannelStore) IncrementMentionCount(channelId string, userId string) error {
func (s SqlChannelStore) IncrementMentionCount(channelId string, userId string, updateThreads bool) error {
now := model.GetMillis()
var threadsToUpdate []string
if updateThreads {
var err error
threadsToUpdate, err = s.Thread().CollectThreadsWithNewerReplies(userId, []string{channelId}, now)
if err != nil {
return err
}
}
_, err := s.GetMaster().Exec(
`UPDATE
ChannelMembers
@@ -2238,11 +2277,13 @@ func (s SqlChannelStore) IncrementMentionCount(channelId string, userId string)
WHERE
UserId = :UserId
AND ChannelId = :ChannelId`,
map[string]interface{}{"ChannelId": channelId, "UserId": userId, "LastUpdateAt": model.GetMillis()})
map[string]interface{}{"ChannelId": channelId, "UserId": userId, "LastUpdateAt": now})
if err != nil {
return errors.Wrapf(err, "failed to Update ChannelMembers with channelId=%s and userId=%s", channelId, userId)
}
if updateThreads {
s.Thread().UpdateUnreadsByChannel(userId, threadsToUpdate, now)
}
return nil
}

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

@@ -1987,6 +1987,7 @@ func (s *SqlPostStore) updateThreadsFromPosts(transaction *gorp.Transaction, pos
// no metadata entry, create one
if err := transaction.Insert(&model.Thread{
PostId: rootId,
ChannelId: posts[0].ChannelId,
ReplyCount: count,
LastReplyAt: now,
Participants: participants,

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

@@ -5,11 +5,12 @@ package sqlstore
import (
"database/sql"
"time"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/store"
"github.com/mattermost/mattermost-server/v5/utils"
"github.com/pkg/errors"
"time"
sq "github.com/Masterminds/squirrel"
)
@@ -29,6 +30,7 @@ func newSqlThreadStore(sqlStore SqlStore) store.ThreadStore {
for _, db := range sqlStore.GetAllConns() {
tableThreads := db.AddTableWithName(model.Thread{}, "Threads").SetKeys(false, "PostId")
tableThreads.ColMap("PostId").SetMaxSize(26)
tableThreads.ColMap("ChannelId").SetMaxSize(26)
tableThreads.ColMap("Participants").SetMaxSize(0)
tableThreadMemberships := db.AddTableWithName(model.ThreadMembership{}, "ThreadMemberships").SetKeys(false, "PostId", "UserId")
tableThreadMemberships.ColMap("PostId").SetMaxSize(26)
@@ -39,12 +41,13 @@ func newSqlThreadStore(sqlStore SqlStore) store.ThreadStore {
}
func threadSliceColumns() []string {
return []string{"PostId", "LastReplyAt", "ReplyCount", "Participants"}
return []string{"PostId", "ChannelId", "LastReplyAt", "ReplyCount", "Participants"}
}
func threadToSlice(thread *model.Thread) []interface{} {
return []interface{}{
thread.PostId,
thread.ChannelId,
thread.LastReplyAt,
thread.ReplyCount,
thread.Participants,
@@ -52,13 +55,10 @@ func threadToSlice(thread *model.Thread) []interface{} {
}
func (s *SqlThreadStore) createIndexesIfNotExists() {
s.CreateIndexIfNotExists("idx_threads_last_reply_at", "Threads", "LastReplyAt")
s.CreateIndexIfNotExists("idx_threads_post_id", "Threads", "PostId")
s.CreateIndexIfNotExists("idx_thread_memberships_last_update_at", "ThreadMemberships", "LastUpdated")
s.CreateIndexIfNotExists("idx_thread_memberships_last_view_at", "ThreadMemberships", "LastViewed")
s.CreateIndexIfNotExists("idx_thread_memberships_post_id", "ThreadMemberships", "PostId")
s.CreateIndexIfNotExists("idx_thread_memberships_user_id", "ThreadMemberships", "UserId")
s.CreateIndexIfNotExists("idx_threads_channel_id", "Threads", "ChannelId")
}
func (s *SqlThreadStore) SaveMultiple(threads []*model.Thread) ([]*model.Thread, int, error) {
@@ -188,3 +188,41 @@ func (s *SqlThreadStore) CreateMembershipIfNeeded(userId, postId string) error {
})
return err
}
func (s *SqlThreadStore) CollectThreadsWithNewerReplies(userId string, channelIds []string, timestamp int64) ([]string, error) {
var changedThreads []string
query, args, _ := s.getQueryBuilder().
Select("Threads.PostId").
From("Threads").
LeftJoin("ChannelMembers ON ChannelMembers.ChannelId=Threads.ChannelId").
Where(sq.And{
sq.Eq{"Threads.ChannelId": channelIds},
sq.Eq{"ChannelMembers.UserId": userId},
sq.Or{
sq.Expr("Threads.LastReplyAt >= ChannelMembers.LastViewedAt"),
sq.GtOrEq{"Threads.LastReplyAt": timestamp},
},
}).
ToSql()
if _, err := s.GetReplica().Select(&changedThreads, query, args...); err != nil {
return nil, errors.Wrap(err, "failed to fetch threads")
}
return changedThreads, nil
}
func (s *SqlThreadStore) UpdateUnreadsByChannel(userId string, changedThreads []string, timestamp int64) error {
if len(changedThreads) == 0 {
return nil
}
updateQuery, updateArgs, _ := s.getQueryBuilder().
Update("ThreadMemberships").
Where(sq.Eq{"UserId": userId, "PostId": changedThreads}).
Set("LastUpdated", timestamp).
Set("LastViewed", timestamp).
ToSql()
if _, err := s.GetMaster().Exec(updateQuery, updateArgs...); err != nil {
return errors.Wrap(err, "failed to update thread membership")
}
return nil
}

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

@@ -925,6 +925,16 @@ func upgradeDatabaseToVersion529(sqlStore SqlStore) {
sqlStore.AlterColumnTypeIfExists("SidebarChannels", "CategoryId", "VARCHAR(128)", "VARCHAR(128)")
sqlStore.AlterColumnDefaultIfExists("SidebarChannels", "CategoryId", model.NewString(""), nil)
sqlStore.CreateColumnIfNotExistsNoDefault("Threads", "ChannelId", "VARCHAR(26)", "VARCHAR(26)")
updateThreadChannelsQuery := "UPDATE Threads INNER JOIN Posts ON Posts.Id=Threads.PostId SET Threads.ChannelId=Posts.ChannelId WHERE Threads.ChannelId IS NULL"
if sqlStore.DriverName() == model.DATABASE_DRIVER_POSTGRES {
updateThreadChannelsQuery = "UPDATE Threads SET ChannelId=Posts.ChannelId FROM Posts WHERE Posts.Id=Threads.PostId AND Threads.ChannelId IS NULL"
}
if _, err := sqlStore.GetMaster().Exec(updateThreadChannelsQuery); err != nil {
mlog.Error("Error updating ChannelId in Threads table", mlog.Err(err))
}
saveSchemaVersion(sqlStore, VERSION_5_29_0)
}
}