MM-28249 Auto follow threads (#15878)
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
944841a237
Коммит
8844141df3
@@ -685,6 +685,7 @@ type AppIface interface {
|
||||
GetTeamsForUser(userId string) ([]*model.Team, *model.AppError)
|
||||
GetTeamsUnreadForUser(excludeTeamId string, userId string) ([]*model.TeamUnread, *model.AppError)
|
||||
GetTermsOfService(id string) (*model.TermsOfService, *model.AppError)
|
||||
GetThreadMembershipsForUser(userId string) ([]*model.ThreadMembership, error)
|
||||
GetUploadSession(uploadId string) (*model.UploadSession, *model.AppError)
|
||||
GetUploadSessionsForUser(userId string) ([]*model.UploadSession, *model.AppError)
|
||||
GetUser(userId string) (*model.User, *model.AppError)
|
||||
|
||||
@@ -159,20 +159,37 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod
|
||||
|
||||
mentionedUsersList := make([]string, 0, len(mentions.Mentions))
|
||||
updateMentionChans := []chan *model.AppError{}
|
||||
mentionAutofollowChans := []chan *model.AppError{}
|
||||
|
||||
// for each mention, make sure to update thread autofollow
|
||||
for id := range mentions.Mentions {
|
||||
mac := make(chan *model.AppError, 1)
|
||||
go func(userId string) {
|
||||
defer close(mac)
|
||||
if *a.Config().ServiceSettings.ThreadAutoFollow && post.RootId != "" {
|
||||
nErr := a.Srv().Store.Thread().CreateMembershipIfNeeded(userId, post.RootId)
|
||||
if nErr != nil {
|
||||
mac <- model.NewAppError("SendNotifications", "app.channel.autofollow.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
mac <- nil
|
||||
}(id)
|
||||
mentionAutofollowChans = append(mentionAutofollowChans, mac)
|
||||
}
|
||||
|
||||
for id := range mentions.Mentions {
|
||||
mentionedUsersList = append(mentionedUsersList, id)
|
||||
|
||||
umc := make(chan *model.AppError, 1)
|
||||
go func(userId string) {
|
||||
defer close(umc)
|
||||
nErr := a.Srv().Store.Channel().IncrementMentionCount(post.ChannelId, userId)
|
||||
if nErr != nil {
|
||||
umc <- model.NewAppError("SendNotifications", "app.channel.increment_mention_count.app_error", nil, nErr.Error(), http.StatusInternalServerError)
|
||||
} else {
|
||||
umc <- nil
|
||||
return
|
||||
}
|
||||
|
||||
close(umc)
|
||||
umc <- nil
|
||||
}(id)
|
||||
updateMentionChans = append(updateMentionChans, umc)
|
||||
}
|
||||
@@ -254,6 +271,17 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod
|
||||
}
|
||||
}
|
||||
|
||||
// Log the problems that might have occurred while auto following the thread
|
||||
for _, mac := range mentionAutofollowChans {
|
||||
if err := <-mac; err != nil {
|
||||
mlog.Warn(
|
||||
"Failed to update thread autofollow from mention",
|
||||
mlog.String("post_id", post.Id),
|
||||
mlog.String("channel_id", post.ChannelId),
|
||||
mlog.Err(err),
|
||||
)
|
||||
}
|
||||
}
|
||||
sendPushNotifications := false
|
||||
if *a.Config().EmailSettings.SendPushNotifications {
|
||||
pushServer := *a.Config().EmailSettings.PushNotificationServer
|
||||
|
||||
@@ -8397,6 +8397,28 @@ func (a *OpenTracingAppLayer) GetTermsOfService(id string) (*model.TermsOfServic
|
||||
return resultVar0, resultVar1
|
||||
}
|
||||
|
||||
func (a *OpenTracingAppLayer) GetThreadMembershipsForUser(userId string) ([]*model.ThreadMembership, error) {
|
||||
origCtx := a.ctx
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetThreadMembershipsForUser")
|
||||
|
||||
a.ctx = newCtx
|
||||
a.app.Srv().Store.SetContext(newCtx)
|
||||
defer func() {
|
||||
a.app.Srv().Store.SetContext(origCtx)
|
||||
a.ctx = origCtx
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
resultVar0, resultVar1 := a.app.GetThreadMembershipsForUser(userId)
|
||||
|
||||
if resultVar1 != nil {
|
||||
span.LogFields(spanlog.Error(resultVar1))
|
||||
ext.Error.Set(span, true)
|
||||
}
|
||||
|
||||
return resultVar0, resultVar1
|
||||
}
|
||||
|
||||
func (a *OpenTracingAppLayer) GetTotalUsersStats(viewRestrictions *model.ViewUsersRestrictions) (*model.UsersStats, *model.AppError) {
|
||||
origCtx := a.ctx
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetTotalUsersStats")
|
||||
|
||||
10
app/post.go
10
app/post.go
@@ -443,6 +443,12 @@ func (a *App) handlePostEvents(post *model.Post, user *model.User, channel *mode
|
||||
return err
|
||||
}
|
||||
|
||||
if *a.Config().ServiceSettings.ThreadAutoFollow && post.RootId != "" {
|
||||
if err := a.Srv().Store.Thread().CreateMembershipIfNeeded(post.UserId, post.RootId); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if post.Type != model.POST_AUTO_RESPONDER { // don't respond to an auto-responder
|
||||
a.Srv().Go(func() {
|
||||
_, err := a.SendAutoResponseIfNecessary(channel, user)
|
||||
@@ -1450,3 +1456,7 @@ func isPostMention(user *model.User, post *model.Post, keywords map[string][]str
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (a *App) GetThreadMembershipsForUser(userId string) ([]*model.ThreadMembership, error) {
|
||||
return a.Srv().Store.Thread().GetMembershipsForUser(userId)
|
||||
}
|
||||
|
||||
@@ -1846,3 +1846,60 @@ func TestFillInPostProps(t *testing.T) {
|
||||
assert.Equal(t, post1.Props, model.StringInterface{"disable_group_highlight": true})
|
||||
})
|
||||
}
|
||||
|
||||
func TestThreadMembership(t *testing.T) {
|
||||
t.Run("should update memberships for conversation participants", func(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
user1 := th.BasicUser
|
||||
user2 := th.BasicUser2
|
||||
|
||||
channel := th.CreateChannel(th.BasicTeam)
|
||||
th.AddUserToChannel(user2, channel)
|
||||
|
||||
postRoot, err := th.App.CreatePost(&model.Post{
|
||||
UserId: user1.Id,
|
||||
ChannelId: channel.Id,
|
||||
Message: "root post",
|
||||
}, channel, false, true)
|
||||
require.Nil(t, err)
|
||||
|
||||
_, err = th.App.CreatePost(&model.Post{
|
||||
UserId: user1.Id,
|
||||
ChannelId: channel.Id,
|
||||
RootId: postRoot.Id,
|
||||
Message: fmt.Sprintf("@%s", user2.Username),
|
||||
}, channel, false, true)
|
||||
require.Nil(t, err)
|
||||
|
||||
// first user should now be part of the thread since they replied to a post
|
||||
memberships, err2 := th.App.GetThreadMembershipsForUser(user1.Id)
|
||||
require.Nil(t, err2)
|
||||
require.Len(t, memberships, 1)
|
||||
// second user should also be part of a thread since they were mentioned
|
||||
memberships, err2 = th.App.GetThreadMembershipsForUser(user2.Id)
|
||||
require.Nil(t, err2)
|
||||
require.Len(t, memberships, 1)
|
||||
|
||||
post2, err := th.App.CreatePost(&model.Post{
|
||||
UserId: user2.Id,
|
||||
ChannelId: channel.Id,
|
||||
Message: "second post",
|
||||
}, channel, false, true)
|
||||
require.Nil(t, err)
|
||||
|
||||
_, err = th.App.CreatePost(&model.Post{
|
||||
UserId: user2.Id,
|
||||
ChannelId: channel.Id,
|
||||
RootId: post2.Id,
|
||||
Message: fmt.Sprintf("@%s", user1.Username),
|
||||
}, channel, false, true)
|
||||
require.Nil(t, err)
|
||||
|
||||
// first user should now be part of two threads
|
||||
memberships, err2 = th.App.GetThreadMembershipsForUser(user1.Id)
|
||||
require.Nil(t, err2)
|
||||
require.Len(t, memberships, 2)
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user