* removed server side

* Updated store layer

* unused import

* Updated autogenerated code template

* Updated tests

* lint fix

* unused translations

* webapp side

* Updated i18n

* lint fix:

* type fix

* Updated snapshots

* Removed insights from API specs

* updated e2e

* Updated e2e tests

* Updated e2e tests

* Removed insights tests

* Removed Insights as possible channel to load in sidebar from test

* Removed more insights tests

* More e2e fixed

* More cleanup

* Lint

* More cleanup in client4 and boards api

* More cleanup

* Fixes

* lint fix

---------

Co-authored-by: maria.nunez <maria.nunez@mattermost.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Harshil Sharma
2023-07-25 12:34:38 +05:30
коммит произвёл GitHub
родитель e37459cd00
Коммит 26617fcbdc
192 изменённых файлов: 47 добавлений и 19885 удалений

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

@@ -12,7 +12,6 @@ import (
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
@@ -2495,523 +2494,3 @@ func TestIsCRTEnabledForUser(t *testing.T) {
})
}
}
func TestGetTopChannelsForTeamSince(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
th.ConfigStore.SetReadOnlyFF(false)
defer th.ConfigStore.SetReadOnlyFF(true)
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.InsightsEnabled = true })
channel2 := th.CreateChannel(th.Context, th.BasicTeam)
// add a bot post to ensure it's not counted
_, err := th.Server.Store().Post().Save(&model.Post{
Message: "hello from a bot",
ChannelId: channel2.Id,
UserId: th.BasicUser.Id,
Props: model.StringInterface{
"from_bot": true,
},
})
require.NoError(t, err)
channel3 := th.CreatePrivateChannel(th.Context, th.BasicTeam)
// add a webhook post to ensure it's not counted
_, err = th.Server.Store().Post().Save(&model.Post{
Message: "hello from a webhook",
ChannelId: channel3.Id,
UserId: th.BasicUser.Id,
Props: model.StringInterface{
"from_webhook": true,
},
})
require.NoError(t, err)
// add an oauth app post to ensure it's not counted
_, err = th.Server.Store().Post().Save(&model.Post{
Message: "hello from an ouath app",
ChannelId: channel3.Id,
UserId: th.BasicUser.Id,
Props: model.StringInterface{
"from_oauth_app": true,
},
})
require.NoError(t, err)
// add a plugin post to ensure it's not counted
_, err = th.Server.Store().Post().Save(&model.Post{
Message: "hello from a plugin",
ChannelId: channel3.Id,
UserId: th.BasicUser.Id,
Props: model.StringInterface{
"from_plugin": true,
},
})
require.NoError(t, err)
// add a system post to ensure it's not counted
_, err = th.Server.Store().Post().Save(&model.Post{
Message: "system message",
Type: "system_join_channel",
ChannelId: channel3.Id,
UserId: th.BasicUser.Id,
Props: model.StringInterface{
"from_oauth_app": true,
},
})
require.NoError(t, err)
channel4 := th.CreatePrivateChannel(th.Context, th.BasicTeam)
channel5 := th.CreateChannel(th.Context, th.BasicTeam)
channel6 := th.CreatePrivateChannel(th.Context, th.BasicTeam)
th.AddUserToChannel(th.BasicUser, channel2)
th.AddUserToChannel(th.BasicUser, channel3)
th.AddUserToChannel(th.BasicUser, channel4)
th.AddUserToChannel(th.BasicUser, channel5)
th.AddUserToChannel(th.BasicUser, channel6)
channels := [6]*model.Channel{th.BasicChannel, channel2, channel3, channel4, channel5, channel6}
i := len(channels)
for _, channel := range channels {
for j := i; j > 0; j-- {
th.CreatePost(channel)
}
i--
}
expectedTopChannels := []struct {
ID string
MessageCount int64
}{
{ID: th.BasicChannel.Id, MessageCount: 7},
{ID: channel2.Id, MessageCount: 5},
{ID: channel3.Id, MessageCount: 4},
{ID: channel4.Id, MessageCount: 3},
{ID: channel5.Id, MessageCount: 2},
}
timeRange, _ := model.GetStartOfDayForTimeRange(model.TimeRangeToday, time.Now().Location())
t.Run("get-top-channels-for-team-since", func(t *testing.T) {
topChannels, err := th.App.GetTopChannelsForTeamSince(th.Context, th.BasicChannel.TeamId, th.BasicUser.Id, &model.InsightsOpts{StartUnixMilli: timeRange.UnixMilli(), Page: 0, PerPage: 5})
require.Nil(t, err)
for i, channel := range topChannels.Items {
assert.Equal(t, expectedTopChannels[i].ID, channel.ID)
assert.Equal(t, expectedTopChannels[i].MessageCount, channel.MessageCount)
}
topChannels, err = th.App.GetTopChannelsForTeamSince(th.Context, th.BasicChannel.TeamId, th.BasicUser.Id, &model.InsightsOpts{StartUnixMilli: timeRange.UnixMilli(), Page: 1, PerPage: 5})
require.Nil(t, err)
assert.Equal(t, channel6.Id, topChannels.Items[0].ID)
assert.Equal(t, int64(1), topChannels.Items[0].MessageCount)
})
}
func TestGetTopChannelsForUserSince(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
th.ConfigStore.SetReadOnlyFF(false)
defer th.ConfigStore.SetReadOnlyFF(true)
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.InsightsEnabled = true })
channel2 := th.CreateChannel(th.Context, th.BasicTeam)
// add a bot post to ensure it's not counted
_, err := th.Server.Store().Post().Save(&model.Post{
Message: "hello from a bot",
ChannelId: channel2.Id,
UserId: th.BasicUser.Id,
Props: model.StringInterface{
"from_bot": true,
},
})
require.NoError(t, err)
channel3 := th.CreatePrivateChannel(th.Context, th.BasicTeam)
// add a webhook post to ensure it's not counted
_, err = th.Server.Store().Post().Save(&model.Post{
Message: "hello from a webhook",
ChannelId: channel3.Id,
UserId: th.BasicUser.Id,
Props: model.StringInterface{
"from_webhook": true,
},
})
require.NoError(t, err)
channel4 := th.CreatePrivateChannel(th.Context, th.BasicTeam)
channel5 := th.CreateChannel(th.Context, th.BasicTeam)
channel6 := th.CreatePrivateChannel(th.Context, th.BasicTeam)
th.AddUserToChannel(th.BasicUser, channel2)
th.AddUserToChannel(th.BasicUser, channel3)
th.AddUserToChannel(th.BasicUser, channel4)
th.AddUserToChannel(th.BasicUser, channel5)
th.AddUserToChannel(th.BasicUser, channel6)
channels := [6]*model.Channel{th.BasicChannel, channel2, channel3, channel4, channel5, channel6}
i := len(channels)
for _, channel := range channels {
for j := i; j > 0; j-- {
th.CreatePost(channel)
}
i--
}
expectedTopChannels := []struct {
ID string
MessageCount int64
}{
{ID: th.BasicChannel.Id, MessageCount: 7},
{ID: channel2.Id, MessageCount: 5},
{ID: channel3.Id, MessageCount: 4},
{ID: channel4.Id, MessageCount: 3},
{ID: channel5.Id, MessageCount: 2},
}
timeRange, _ := model.GetStartOfDayForTimeRange(model.TimeRangeToday, time.Now().Location())
t.Run("get-top-channels-for-user-since", func(t *testing.T) {
topChannels, err := th.App.GetTopChannelsForUserSince(th.Context, th.BasicUser.Id, "", &model.InsightsOpts{StartUnixMilli: timeRange.UnixMilli(), Page: 0, PerPage: 5})
require.Nil(t, err)
for i, channel := range topChannels.Items {
assert.Equal(t, expectedTopChannels[i].ID, channel.ID)
assert.Equal(t, expectedTopChannels[i].MessageCount, channel.MessageCount)
}
topChannels, err = th.App.GetTopChannelsForUserSince(th.Context, th.BasicUser.Id, th.BasicChannel.TeamId, &model.InsightsOpts{StartUnixMilli: timeRange.UnixMilli(), Page: 1, PerPage: 5})
require.Nil(t, err)
assert.Equal(t, channel6.Id, topChannels.Items[0].ID)
assert.Equal(t, int64(1), topChannels.Items[0].MessageCount)
})
}
func TestPostCountsByDuration(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
th.ConfigStore.SetReadOnlyFF(false)
defer th.ConfigStore.SetReadOnlyFF(true)
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.InsightsEnabled = true })
channel2 := th.CreateChannel(th.Context, th.BasicTeam)
channel3 := th.CreatePrivateChannel(th.Context, th.BasicTeam)
channel4 := th.CreatePrivateChannel(th.Context, th.BasicTeam)
channel5 := th.CreateChannel(th.Context, th.BasicTeam)
channel6 := th.CreatePrivateChannel(th.Context, th.BasicTeam)
defer func() {
th.App.PermanentDeleteChannel(th.Context, channel2)
th.App.PermanentDeleteChannel(th.Context, channel3)
th.App.PermanentDeleteChannel(th.Context, channel4)
th.App.PermanentDeleteChannel(th.Context, channel5)
th.App.PermanentDeleteChannel(th.Context, channel6)
}()
th.AddUserToChannel(th.BasicUser, channel2)
th.AddUserToChannel(th.BasicUser, channel3)
th.AddUserToChannel(th.BasicUser, channel4)
th.AddUserToChannel(th.BasicUser, channel5)
th.AddUserToChannel(th.BasicUser, channel6)
channels := [6]*model.Channel{th.BasicChannel, channel2, channel3, channel4, channel5, channel6}
channelIDs := []string{th.BasicChannel.Id, channel2.Id, channel3.Id, channel4.Id, channel5.Id, channel6.Id}
i := len(channels)
for ci, channel := range channels {
for j := i; j > 0; j-- {
d1 := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).AddDate(0, 0, ci)
d2 := time.Date(2009, time.November, 10, 9, 0, 0, 0, time.UTC).AddDate(0, 0, ci)
_, err := th.App.CreatePost(th.Context, &model.Post{
UserId: th.BasicUser.Id,
ChannelId: channel.Id,
CreateAt: d1.Unix() * 1000,
}, channel, false, false)
require.Nil(t, err)
_, err = th.App.CreatePost(th.Context, &model.Post{
UserId: th.BasicUser2.Id,
ChannelId: channel.Id,
CreateAt: d2.Unix() * 1000,
}, channel, false, false)
require.Nil(t, err)
}
i--
}
expectedDayGrouping := map[string]map[string]int{
"2009-11-10": {
th.BasicChannel.Id: 6,
},
"2009-11-11": {
channel2.Id: 5,
},
"2009-11-12": {
channel3.Id: 4,
},
"2009-11-13": {
channel4.Id: 3,
},
"2009-11-14": {
channel5.Id: 2,
},
"2009-11-15": {
channel6.Id: 1,
},
}
expectedHourGrouping := map[string]map[string]int{
"2009-11-15T09": {
channel6.Id: 1,
},
"2009-11-15T23": {
channel6.Id: 1,
},
}
sinceUnixMillis := time.Date(2009, time.November, 9, 23, 0, 0, 0, time.UTC).UnixMilli()
t.Run("get-post-counts-by-day scoped by user, grouped by day", func(t *testing.T) {
dailyPostCount, err := th.App.PostCountsByDuration(th.Context, channelIDs, sinceUnixMillis, &th.BasicUser.Id, model.PostsByDay, time.Now().UTC().Location())
require.Nil(t, err)
require.GreaterOrEqual(t, len(dailyPostCount), 6)
for _, item := range dailyPostCount {
if strings.HasPrefix(item.Duration, "2009") {
expectedCount := expectedDayGrouping[item.Duration][item.ChannelID]
assert.Equal(t, expectedCount, item.PostCount)
}
}
})
t.Run("get-post-counts-by-day all users, grouped by day", func(t *testing.T) {
dailyPostCount, err := th.App.PostCountsByDuration(th.Context, channelIDs, sinceUnixMillis, nil, model.PostsByDay, time.Now().UTC().Location())
require.Nil(t, err)
require.GreaterOrEqual(t, len(dailyPostCount), 6)
for _, item := range dailyPostCount {
if strings.HasPrefix(item.Duration, "2009") {
expectedCount := expectedDayGrouping[item.Duration][item.ChannelID]
assert.Equal(t, expectedCount*2, item.PostCount)
}
}
})
t.Run("get-post-counts-by-day all users, grouped by hour", func(t *testing.T) {
oneDaySince := time.Date(2009, time.November, 14, 23, 0, 0, 0, time.UTC).UnixMilli()
dailyPostCount, err := th.App.PostCountsByDuration(th.Context, channelIDs, oneDaySince, nil, model.PostsByHour, time.Now().UTC().Location())
require.Nil(t, err)
require.GreaterOrEqual(t, len(dailyPostCount), 1)
for _, item := range dailyPostCount {
if strings.HasPrefix(item.Duration, "2009") {
expectedCount := expectedHourGrouping[item.Duration][item.ChannelID]
assert.Equal(t, expectedCount, item.PostCount)
}
}
})
}
// Top inactive channels
func TestGetTopInactiveChannelsForTeamSince(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
th.ConfigStore.SetReadOnlyFF(false)
defer th.ConfigStore.SetReadOnlyFF(true)
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.InsightsEnabled = true })
channel2 := th.CreateChannel(th.Context, th.BasicTeam, WithCreateAt(1))
channel3 := th.CreateChannel(th.Context, th.BasicTeam, WithCreateAt(1))
channel4 := th.CreatePrivateChannel(th.Context, th.BasicTeam, WithCreateAt(1))
channel5 := th.CreateChannel(th.Context, th.BasicTeam, WithCreateAt(1))
channel6 := th.CreatePrivateChannel(th.Context, th.BasicTeam, WithCreateAt(1))
th.AddUserToChannel(th.BasicUser, channel2)
th.AddUserToChannel(th.BasicUser, channel3)
th.AddUserToChannel(th.BasicUser, channel4)
th.AddUserToChannel(th.BasicUser, channel5)
th.AddUserToChannel(th.BasicUser, channel6)
// delete offtopic, town square, basicChannel channel - which interferes with 'least' active channel results
offTopicChannel, appErr := th.App.GetChannelByName(th.Context, "off-topic", th.BasicTeam.Id, false)
require.Nil(t, appErr, "Expected nil, didn't receive nil")
appErr = th.App.PermanentDeleteChannel(th.Context, offTopicChannel)
require.Nil(t, appErr)
townSquareChannel, appErr := th.App.GetChannelByName(th.Context, "town-square", th.BasicTeam.Id, false)
require.Nil(t, appErr, "Expected nil, didn't receive nil")
appErr = th.App.PermanentDeleteChannel(th.Context, townSquareChannel)
require.Nil(t, appErr)
basicChannel, appErr := th.App.GetChannel(th.Context, th.BasicChannel.Id)
require.Nil(t, appErr, "Expected nil, didn't receive nil")
appErr = th.App.PermanentDeleteChannel(th.Context, basicChannel)
require.Nil(t, appErr)
// add a bot post to ensure it's counted
_, err := th.Server.Store().Post().Save(&model.Post{
Message: "hello from a bot",
ChannelId: channel2.Id,
UserId: th.BasicUser.Id,
Props: model.StringInterface{
"from_bot": true,
},
})
require.NoError(t, err)
// add a webhook post to ensure it's counted
_, err = th.Server.Store().Post().Save(&model.Post{
Message: "hello from a webhook",
ChannelId: channel3.Id,
UserId: th.BasicUser.Id,
Props: model.StringInterface{
"from_webhook": true,
},
})
require.NoError(t, err)
channels := [5]*model.Channel{channel2, channel3, channel4, channel5, channel6}
i := len(channels)
for _, channel := range channels {
for j := i; j > 0; j-- {
th.CreatePost(channel)
}
i--
}
expectedTopChannels := []struct {
ID string
MessageCount int64
}{
{ID: channel6.Id, MessageCount: 1},
{ID: channel5.Id, MessageCount: 2},
{ID: channel4.Id, MessageCount: 3},
{ID: channel3.Id, MessageCount: 5},
{ID: channel2.Id, MessageCount: 6},
}
timeRange, _ := model.GetStartOfDayForTimeRange(model.TimeRangeToday, time.Now().Location())
t.Run("get-top-channels-for-team-since", func(t *testing.T) {
topChannels, err := th.App.GetTopInactiveChannelsForTeamSince(th.Context, th.BasicChannel.TeamId, th.BasicUser.Id, &model.InsightsOpts{StartUnixMilli: timeRange.UnixMilli(), Page: 0, PerPage: 5})
require.Nil(t, err)
for i, channel := range topChannels.Items {
assert.Equal(t, expectedTopChannels[i].ID, channel.ID)
assert.Equal(t, expectedTopChannels[i].MessageCount, channel.MessageCount)
}
topChannels, err = th.App.GetTopInactiveChannelsForTeamSince(th.Context, th.BasicChannel.TeamId, th.BasicUser.Id, &model.InsightsOpts{StartUnixMilli: timeRange.UnixMilli(), Page: 1, PerPage: 4})
require.Nil(t, err)
assert.Equal(t, channel2.Id, topChannels.Items[0].ID)
assert.Equal(t, int64(6), topChannels.Items[0].MessageCount)
// it simulates channel being created recently
_ = th.CreatePrivateChannel(th.Context, th.BasicTeam)
topChannels, err = th.App.GetTopInactiveChannelsForTeamSince(th.Context, th.BasicChannel.TeamId, th.BasicUser.Id, &model.InsightsOpts{StartUnixMilli: timeRange.UnixMilli(), Page: 0, PerPage: 6})
require.Nil(t, err)
assert.Equal(t, 5, len(topChannels.Items))
})
}
func TestGetTopInactiveChannelsForUserSince(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
th.ConfigStore.SetReadOnlyFF(false)
defer th.ConfigStore.SetReadOnlyFF(true)
th.App.UpdateConfig(func(cfg *model.Config) { cfg.FeatureFlags.InsightsEnabled = true })
// delete offtopic, town-square, th.basicchannel channels - which interferes with 'least' active channel results
offTopicChannel, appErr := th.App.GetChannelByName(th.Context, "off-topic", th.BasicTeam.Id, false)
require.Nil(t, appErr, "Expected nil, didn't receive nil")
appErr = th.App.PermanentDeleteChannel(th.Context, offTopicChannel)
require.Nil(t, appErr)
townSquareChannel, appErr := th.App.GetChannelByName(th.Context, "town-square", th.BasicTeam.Id, false)
require.Nil(t, appErr, "Expected nil, didn't receive nil")
appErr = th.App.PermanentDeleteChannel(th.Context, townSquareChannel)
require.Nil(t, appErr)
basicChannel, appErr := th.App.GetChannel(th.Context, th.BasicChannel.Id)
require.Nil(t, appErr, "Expected nil, didn't receive nil")
appErr = th.App.PermanentDeleteChannel(th.Context, basicChannel)
require.Nil(t, appErr)
channel2 := th.CreateChannel(th.Context, th.BasicTeam, WithCreateAt(1))
// add a bot post to ensure it's counted
_, err := th.Server.Store().Post().Save(&model.Post{
Message: "hello from a bot",
ChannelId: channel2.Id,
UserId: th.BasicUser.Id,
Props: model.StringInterface{
"from_bot": true,
},
})
require.NoError(t, err)
channel3 := th.CreatePrivateChannel(th.Context, th.BasicTeam, WithCreateAt(1))
// add a webhook post to ensure it's counted
_, err = th.Server.Store().Post().Save(&model.Post{
Message: "hello from a webhook",
ChannelId: channel3.Id,
UserId: th.BasicUser.Id,
Props: model.StringInterface{
"from_webhook": true,
},
})
require.NoError(t, err)
channel4 := th.CreatePrivateChannel(th.Context, th.BasicTeam, WithCreateAt(1))
channel5 := th.CreateChannel(th.Context, th.BasicTeam, WithCreateAt(1))
channel6 := th.CreatePrivateChannel(th.Context, th.BasicTeam, WithCreateAt(1))
th.AddUserToChannel(th.BasicUser, channel2)
th.AddUserToChannel(th.BasicUser, channel3)
th.AddUserToChannel(th.BasicUser, channel4)
th.AddUserToChannel(th.BasicUser, channel5)
th.AddUserToChannel(th.BasicUser, channel6)
channels := [5]*model.Channel{channel2, channel3, channel4, channel5, channel6}
i := len(channels)
for _, channel := range channels {
for j := i; j > 0; j-- {
th.CreatePost(channel)
}
i--
}
expectedTopChannels := []struct {
ID string
MessageCount int64
}{
{ID: channel6.Id, MessageCount: 1},
{ID: channel5.Id, MessageCount: 2},
{ID: channel4.Id, MessageCount: 3},
{ID: channel3.Id, MessageCount: 5},
{ID: channel2.Id, MessageCount: 6},
}
timeRange, _ := model.GetStartOfDayForTimeRange(model.TimeRangeToday, time.Now().Location())
t.Run("get-top-channels-for-user-since", func(t *testing.T) {
topChannels, err := th.App.GetTopInactiveChannelsForUserSince(th.Context, th.BasicChannel.TeamId, th.BasicUser.Id, &model.InsightsOpts{StartUnixMilli: timeRange.UnixMilli(), Page: 0, PerPage: 4})
require.Nil(t, err)
require.Equal(t, len(topChannels.Items), 4)
for i, channel := range topChannels.Items {
assert.Equal(t, expectedTopChannels[i].ID, channel.ID)
assert.Equal(t, expectedTopChannels[i].MessageCount, channel.MessageCount)
}
topChannels, err = th.App.GetTopInactiveChannelsForUserSince(th.Context, th.BasicChannel.TeamId, th.BasicUser.Id, &model.InsightsOpts{StartUnixMilli: timeRange.UnixMilli(), Page: 1, PerPage: 4})
require.Nil(t, err)
require.Equal(t, len(topChannels.Items), 1)
assert.Equal(t, channel2.Id, topChannels.Items[0].ID)
assert.Equal(t, int64(6), topChannels.Items[0].MessageCount)
})
}