[MM-36268] Fix replication lag error on post reply (#17752)

* Fix replication lag error on post reply

* Improve further by using a db transaction
Этот коммит содержится в:
Claudio Costa
2021-06-16 16:45:34 +02:00
коммит произвёл GitHub
родитель d093e102a4
Коммит 6483abd263
11 изменённых файлов: 220 добавлений и 65 удалений

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

@@ -2482,11 +2482,28 @@ func (a *App) MarkChannelAsUnreadFromPost(postID string, userID string, collapse
threadId = post.Id
}
threadMembership, _ := a.Srv().Store.Thread().GetMembershipForUser(user.Id, threadId)
var nfErr *store.ErrNotFound
threadMembership, storeErr := a.Srv().Store.Thread().GetMembershipForUser(user.Id, threadId)
if storeErr != nil && !errors.As(storeErr, &nfErr) {
return nil, model.NewAppError("MarkChannelAsUnreadFromPost", "app.channel.update_last_viewed_at_post.app_error", nil, storeErr.Error(), http.StatusInternalServerError)
}
// if this post was not followed before, create thread membership and update mention count
if threadMembership == nil {
threadMembership, _ = a.Srv().Store.Thread().MaintainMembership(user.Id, threadId, true, true, true, true, false)
threadData, _ := a.Srv().Store.Thread().Get(threadId)
opts := store.ThreadMembershipOpts{
Following: true,
IncrementMentions: true,
UpdateFollowing: true,
UpdateViewedTimestamp: true,
UpdateParticipants: false,
}
threadMembership, storeErr = a.Srv().Store.Thread().MaintainMembership(user.Id, threadId, opts)
if storeErr != nil && !errors.As(storeErr, &nfErr) {
return nil, model.NewAppError("MarkChannelAsUnreadFromPost", "app.channel.update_last_viewed_at_post.app_error", nil, storeErr.Error(), http.StatusInternalServerError)
}
threadData, storeErr := a.Srv().Store.Thread().Get(threadId)
if storeErr != nil && !errors.As(storeErr, &nfErr) {
return nil, model.NewAppError("MarkChannelAsUnreadFromPost", "app.channel.update_last_viewed_at_post.app_error", nil, storeErr.Error(), http.StatusInternalServerError)
}
if threadData != nil && threadMembership != nil && threadMembership.Following {
channel, nErr := a.Srv().Store.Channel().Get(post.ChannelId, true)
if nErr != nil {
@@ -2500,7 +2517,10 @@ func (a *App) MarkChannelAsUnreadFromPost(postID string, userID string, collapse
if nErr != nil {
return nil, model.NewAppError("MarkChannelAsUnreadFromPost", "app.channel.update_last_viewed_at_post.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
thread, _ := a.Srv().Store.Thread().GetThreadForUser(channel.TeamId, threadMembership, true)
thread, nErr := a.Srv().Store.Thread().GetThreadForUser(channel.TeamId, threadMembership, true)
if nErr != nil {
return nil, model.NewAppError("MarkChannelAsUnreadFromPost", "app.channel.update_last_viewed_at_post.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}
a.sanitizeProfiles(thread.Participants, false)
thread.Post.SanitizeProps()
@@ -2586,7 +2606,14 @@ func (a *App) markChannelAsUnreadFromPostCRTUnsupported(postID string, userID st
}
// Follow thread if we're not already following it
if threadMembership == nil {
threadMembership, nErr = a.Srv().Store.Thread().MaintainMembership(user.Id, threadId, true, false, true, false, false)
opts := store.ThreadMembershipOpts{
Following: true,
IncrementMentions: false,
UpdateFollowing: true,
UpdateViewedTimestamp: false,
UpdateParticipants: false,
}
threadMembership, nErr = a.Srv().Store.Thread().MaintainMembership(user.Id, threadId, opts)
if nErr != nil {
return nil, model.NewAppError("MarkChannelAsUnreadFromPost", "app.channel.update_last_viewed_at_post.app_error", nil, nErr.Error(), http.StatusInternalServerError)
}

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

@@ -206,8 +206,15 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod
return
}
}
_, err := a.Srv().Store.Thread().MaintainMembership(userID, post.RootId, true, incrementMentions, *a.Config().ServiceSettings.ThreadAutoFollow, userID == post.UserId, userID == post.UserId)
opts := store.ThreadMembershipOpts{
Following: true,
IncrementMentions: incrementMentions,
UpdateFollowing: *a.Config().ServiceSettings.ThreadAutoFollow,
UpdateViewedTimestamp: userID == post.UserId,
UpdateParticipants: userID == post.UserId,
}
_, err := a.Srv().Store.Thread().MaintainMembership(userID, post.RootId, opts)
if err != nil {
mac <- model.NewAppError("SendNotifications", "app.channel.autofollow.app_error", nil, err.Error(), http.StatusInternalServerError)
return

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

@@ -2320,7 +2320,14 @@ func (a *App) UpdateThreadsReadForUser(userID, teamID string) *model.AppError {
}
func (a *App) UpdateThreadFollowForUser(userID, teamID, threadID string, state bool) *model.AppError {
_, err := a.Srv().Store.Thread().MaintainMembership(userID, threadID, state, false, true, state, false)
opts := store.ThreadMembershipOpts{
Following: state,
IncrementMentions: false,
UpdateFollowing: true,
UpdateViewedTimestamp: state,
UpdateParticipants: false,
}
_, err := a.Srv().Store.Thread().MaintainMembership(userID, threadID, opts)
if err != nil {
return model.NewAppError("UpdateThreadFollowForUser", "app.user.update_thread_follow_for_user.app_error", nil, err.Error(), http.StatusInternalServerError)
}