MM-38132: Improve Auto Responder logic (#18264)
We properly truncate the date part from the createAt time. The SQL query is improved now to directly return the bool. Fixed the test to properly test the feature. https://mattermost.atlassian.net/browse/MM-38132 ```release-note NONE ```
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
71a811e993
Коммит
5b8720d539
@@ -13,7 +13,7 @@ import (
|
|||||||
|
|
||||||
// check if there is any auto_response type post in channel by the user in a calender day
|
// check if there is any auto_response type post in channel by the user in a calender day
|
||||||
func (a *App) checkIfRespondedToday(createdAt int64, channelId, userId string) (bool, error) {
|
func (a *App) checkIfRespondedToday(createdAt int64, channelId, userId string) (bool, error) {
|
||||||
y, m, d := time.Unix(int64(model.GetTimeForMillis(createdAt).Second()), 0).Date()
|
y, m, d := model.GetTimeForMillis(createdAt).Date()
|
||||||
since := model.GetMillisForTime(time.Date(y, m, d, 0, 0, 0, 0, time.UTC))
|
since := model.GetMillisForTime(time.Date(y, m, d, 0, 0, 0, 0, time.UTC))
|
||||||
return a.Srv().Store.Post().HasAutoResponsePostByUserSince(
|
return a.Srv().Store.Post().HasAutoResponsePostByUserSince(
|
||||||
model.GetPostsSinceOptions{ChannelId: channelId, Time: since},
|
model.GetPostsSinceOptions{ChannelId: channelId, Time: since},
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@@ -100,7 +101,7 @@ func TestSendAutoResponseIfNecessary(t *testing.T) {
|
|||||||
|
|
||||||
savedPost, _ := th.App.CreatePost(th.Context, &model.Post{
|
savedPost, _ := th.App.CreatePost(th.Context, &model.Post{
|
||||||
ChannelId: channel.Id,
|
ChannelId: channel.Id,
|
||||||
Message: "zz" + model.NewId() + "a",
|
Message: NewTestId(),
|
||||||
UserId: th.BasicUser.Id},
|
UserId: th.BasicUser.Id},
|
||||||
th.BasicChannel,
|
th.BasicChannel,
|
||||||
false, true)
|
false, true)
|
||||||
@@ -130,7 +131,7 @@ func TestSendAutoResponseIfNecessary(t *testing.T) {
|
|||||||
|
|
||||||
savedPost, _ := th.App.CreatePost(th.Context, &model.Post{
|
savedPost, _ := th.App.CreatePost(th.Context, &model.Post{
|
||||||
ChannelId: channel.Id,
|
ChannelId: channel.Id,
|
||||||
Message: "zz" + model.NewId() + "a",
|
Message: NewTestId(),
|
||||||
UserId: th.BasicUser.Id},
|
UserId: th.BasicUser.Id},
|
||||||
th.BasicChannel,
|
th.BasicChannel,
|
||||||
false, true)
|
false, true)
|
||||||
@@ -147,7 +148,7 @@ func TestSendAutoResponseIfNecessary(t *testing.T) {
|
|||||||
|
|
||||||
savedPost, _ := th.App.CreatePost(th.Context, &model.Post{
|
savedPost, _ := th.App.CreatePost(th.Context, &model.Post{
|
||||||
ChannelId: th.BasicChannel.Id,
|
ChannelId: th.BasicChannel.Id,
|
||||||
Message: "zz" + model.NewId() + "a",
|
Message: NewTestId(),
|
||||||
UserId: th.BasicUser.Id},
|
UserId: th.BasicUser.Id},
|
||||||
th.BasicChannel,
|
th.BasicChannel,
|
||||||
false, true)
|
false, true)
|
||||||
@@ -187,7 +188,7 @@ func TestSendAutoResponseIfNecessary(t *testing.T) {
|
|||||||
|
|
||||||
savedPost, _ := th.App.CreatePost(th.Context, &model.Post{
|
savedPost, _ := th.App.CreatePost(th.Context, &model.Post{
|
||||||
ChannelId: channel.Id,
|
ChannelId: channel.Id,
|
||||||
Message: "zz" + model.NewId() + "a",
|
Message: NewTestId(),
|
||||||
UserId: botUser.Id},
|
UserId: botUser.Id},
|
||||||
th.BasicChannel,
|
th.BasicChannel,
|
||||||
false, true)
|
false, true)
|
||||||
@@ -215,24 +216,27 @@ func TestSendAutoResponseIfNecessary(t *testing.T) {
|
|||||||
|
|
||||||
channel := th.CreateDmChannel(receiver)
|
channel := th.CreateDmChannel(receiver)
|
||||||
|
|
||||||
|
// Clean up all posts from this user.
|
||||||
|
// There are some dummy messages like "user joined team" etc.
|
||||||
|
// which needs to be cleaned up.
|
||||||
|
require.NoError(t, th.GetSqlStore().Post().PermanentDeleteByUser(th.BasicUser.Id))
|
||||||
|
|
||||||
savedPost, err := th.App.CreatePost(th.Context, &model.Post{
|
savedPost, err := th.App.CreatePost(th.Context, &model.Post{
|
||||||
ChannelId: channel.Id,
|
ChannelId: channel.Id,
|
||||||
Message: NewTestId(),
|
Message: patch.NotifyProps["auto_responder_message"],
|
||||||
UserId: th.BasicUser.Id},
|
UserId: receiver.Id,
|
||||||
|
CreateAt: model.GetMillisForTime(time.Now().Add(-48 * time.Hour)),
|
||||||
|
Type: model.PostTypeAutoResponder,
|
||||||
|
},
|
||||||
th.BasicChannel,
|
th.BasicChannel,
|
||||||
false, true)
|
false, true)
|
||||||
|
require.Nil(t, err)
|
||||||
assert.Nil(t, err)
|
savedPost.CreateAt = model.GetMillisForTime(time.Now())
|
||||||
|
|
||||||
sent, err := th.App.SendAutoResponseIfNecessary(th.Context, channel, th.BasicUser, savedPost)
|
sent, err := th.App.SendAutoResponseIfNecessary(th.Context, channel, th.BasicUser, savedPost)
|
||||||
|
|
||||||
require.Nil(t, err)
|
require.Nil(t, err)
|
||||||
assert.True(t, sent)
|
assert.True(t, sent)
|
||||||
|
|
||||||
sent, err = th.App.SendAutoResponseIfNecessary(th.Context, channel, th.BasicUser, savedPost)
|
|
||||||
|
|
||||||
require.Nil(t, err)
|
|
||||||
assert.False(t, sent)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
sq "github.com/Masterminds/squirrel"
|
sq "github.com/Masterminds/squirrel"
|
||||||
"github.com/mattermost/gorp"
|
"github.com/mattermost/gorp"
|
||||||
@@ -1050,20 +1049,21 @@ func (s *SqlPostStore) GetPostsSince(options model.GetPostsSinceOptions, allowFr
|
|||||||
|
|
||||||
func (s *SqlPostStore) HasAutoResponsePostByUserSince(options model.GetPostsSinceOptions, userId string) (bool, error) {
|
func (s *SqlPostStore) HasAutoResponsePostByUserSince(options model.GetPostsSinceOptions, userId string) (bool, error) {
|
||||||
query := `
|
query := `
|
||||||
SELECT 1
|
SELECT EXISTS (SELECT 1
|
||||||
FROM
|
FROM
|
||||||
Posts
|
Posts
|
||||||
WHERE
|
WHERE
|
||||||
UpdateAt >= :Time
|
UpdateAt >= :Time
|
||||||
AND
|
AND
|
||||||
ChannelId = :ChannelId
|
ChannelId = :ChannelId
|
||||||
AND
|
AND
|
||||||
UserId = :UserId
|
UserId = :UserId
|
||||||
AND
|
AND
|
||||||
Type = :Type
|
Type = :Type
|
||||||
LIMIT 1`
|
LIMIT 1)`
|
||||||
|
|
||||||
exist, err := s.GetReplica().SelectInt(query, map[string]interface{}{
|
var exist bool
|
||||||
|
err := s.GetReplica().SelectOne(&exist, query, map[string]interface{}{
|
||||||
"ChannelId": options.ChannelId,
|
"ChannelId": options.ChannelId,
|
||||||
"Time": options.Time,
|
"Time": options.Time,
|
||||||
"UserId": userId,
|
"UserId": userId,
|
||||||
@@ -1072,10 +1072,10 @@ func (s *SqlPostStore) HasAutoResponsePostByUserSince(options model.GetPostsSinc
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, errors.Wrapf(err,
|
return false, errors.Wrapf(err,
|
||||||
"failed to check if autoresponse posts in channelId=%s for userId=%s since %s", options.ChannelId, userId, time.Unix(options.Time, 0).Format(time.RFC3339))
|
"failed to check if autoresponse posts in channelId=%s for userId=%s since %s", options.ChannelId, userId, model.GetTimeForMillis(options.Time))
|
||||||
}
|
}
|
||||||
|
|
||||||
return exist > 0, nil
|
return exist, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SqlPostStore) GetPostsSinceForSync(options model.GetPostsSinceForSyncOptions, cursor model.GetPostsSinceForSyncCursor, limit int) ([]*model.Post, model.GetPostsSinceForSyncCursor, error) {
|
func (s *SqlPostStore) GetPostsSinceForSync(options model.GetPostsSinceForSyncOptions, cursor model.GetPostsSinceForSyncCursor, limit int) ([]*model.Post, model.GetPostsSinceForSyncCursor, error) {
|
||||||
|
|||||||
@@ -3145,6 +3145,8 @@ func testHasAutoResponsePostByUserSince(t *testing.T, ss store.Store) {
|
|||||||
Message: "message",
|
Message: "message",
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
// We need to sleep because SendAutoResponseIfNecessary
|
||||||
|
// runs in a goroutine.
|
||||||
time.Sleep(time.Millisecond)
|
time.Sleep(time.Millisecond)
|
||||||
|
|
||||||
post2, err := ss.Post().Save(&model.Post{
|
post2, err := ss.Post().Save(&model.Post{
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user