[MM-17069] Api endpoint to unread a channel (#11794)
* [MM-17069] endpoint to unread a channel from post * [MM-17069] update mock * [MM-17069] first passing test * [MM-17069] fix SQL typo * [MM-17069] fix msgCount * add tests * [MM-17069] Fix tests * [MM-16069] Remove trash, add comments * [MM-16069] Add message to errors * [MM-17069] fix go fmt * [MM-17069] return an UnreadChannel response * [MM-17069] added unauthorized test * [MM-17069] fix operator * [MM-17069] refactor tests * [MM-16069] back to green tests * [MM-17069] change url to include user * [MM-17069] Fixing code review comments * [MM-17069] One shouldn't fix manually what a machine can fix better * [MM-17069] change response type, update tests * [MM-17069] fix permission error * [MM-17069] Add tests for edit_other_users permission * [MM-17069] no magic numbers
Этот коммит содержится в:
коммит произвёл
Harrison Healey
родитель
bd152c6534
Коммит
6b0f4f1aee
@@ -1754,6 +1754,18 @@ func (a *App) UpdateChannelLastViewedAt(channelIds []string, userId string) *mod
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarkChanelAsUnreadFromPost will take a post and set the channel as unread from that one.
|
||||
func (a *App) MarkChannelAsUnreadFromPost(postID string, userID string) (*model.ChannelUnreadAt, *model.AppError) {
|
||||
|
||||
post, err := a.GetSinglePost(postID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
unreadMentions := 0 // TODO: calculate this value, setting it to zero for now.
|
||||
return a.Srv.Store.Channel().UpdateLastViewedAtPost(post, userID, unreadMentions)
|
||||
}
|
||||
|
||||
func (a *App) esAutocompleteChannels(teamId, term string, includeDeleted bool) (*model.ChannelList, *model.AppError) {
|
||||
channelIds, err := a.Elasticsearch.SearchChannels(teamId, term)
|
||||
if err != nil {
|
||||
|
||||
@@ -1041,3 +1041,103 @@ func TestSearchChannelsForUser(t *testing.T) {
|
||||
searchAndCheck(t, "dev", []string{"test-dev-1", "test-dev-2", "dev-3"})
|
||||
})
|
||||
}
|
||||
|
||||
func TestMarkChannelAsUnreadFromPost(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
u1 := th.BasicUser
|
||||
u2 := th.BasicUser2
|
||||
c1 := th.BasicChannel
|
||||
pc1 := th.CreatePrivateChannel(th.BasicTeam)
|
||||
th.AddUserToChannel(u2, c1)
|
||||
th.AddUserToChannel(u1, pc1)
|
||||
th.AddUserToChannel(u2, pc1)
|
||||
|
||||
p1 := th.CreatePost(c1)
|
||||
p2 := th.CreatePost(c1)
|
||||
p3 := th.CreatePost(c1)
|
||||
|
||||
pp1 := th.CreatePost(pc1)
|
||||
require.NotNil(t, pp1)
|
||||
pp2 := th.CreatePost(pc1)
|
||||
|
||||
unread, err := th.App.GetChannelUnread(c1.Id, u1.Id)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(4), unread.MsgCount)
|
||||
unread, err = th.App.GetChannelUnread(c1.Id, u2.Id)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(4), unread.MsgCount)
|
||||
err = th.App.UpdateChannelLastViewedAt([]string{c1.Id, pc1.Id}, u1.Id)
|
||||
require.Nil(t, err)
|
||||
err = th.App.UpdateChannelLastViewedAt([]string{c1.Id, pc1.Id}, u2.Id)
|
||||
require.Nil(t, err)
|
||||
unread, err = th.App.GetChannelUnread(c1.Id, u2.Id)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, int64(0), unread.MsgCount)
|
||||
|
||||
t.Run("Unread but last one", func(t *testing.T) {
|
||||
response, err := th.App.MarkChannelAsUnreadFromPost(p2.Id, u1.Id)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, response)
|
||||
assert.Equal(t, int64(3), response.MsgCount)
|
||||
unread, err := th.App.GetChannelUnread(c1.Id, u1.Id)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, int64(1), unread.MsgCount)
|
||||
assert.Equal(t, p2.CreateAt, response.LastViewedAt)
|
||||
})
|
||||
|
||||
t.Run("Unread last one", func(t *testing.T) {
|
||||
response, err := th.App.MarkChannelAsUnreadFromPost(p3.Id, u1.Id)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, response)
|
||||
assert.Equal(t, int64(4), response.MsgCount)
|
||||
unread, err := th.App.GetChannelUnread(c1.Id, u1.Id)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, int64(0), unread.MsgCount)
|
||||
assert.Equal(t, p3.CreateAt, response.LastViewedAt)
|
||||
})
|
||||
|
||||
t.Run("Unread first one", func(t *testing.T) {
|
||||
response, err := th.App.MarkChannelAsUnreadFromPost(p1.Id, u1.Id)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, response)
|
||||
assert.Equal(t, int64(2), response.MsgCount)
|
||||
unread, err := th.App.GetChannelUnread(c1.Id, u1.Id)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, int64(2), unread.MsgCount)
|
||||
assert.Equal(t, p1.CreateAt, response.LastViewedAt)
|
||||
})
|
||||
|
||||
t.Run("Other users are unaffected", func(t *testing.T) {
|
||||
unread, err := th.App.GetChannelUnread(c1.Id, u2.Id)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, int64(0), unread.MsgCount)
|
||||
|
||||
})
|
||||
|
||||
t.Run("Unread on a private channel", func(t *testing.T) {
|
||||
response, err := th.App.MarkChannelAsUnreadFromPost(pp1.Id, u1.Id)
|
||||
require.Nil(t, err)
|
||||
require.NotNil(t, response)
|
||||
assert.Equal(t, int64(1), response.MsgCount)
|
||||
unread, err := th.App.GetChannelUnread(pc1.Id, u1.Id)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, int64(1), unread.MsgCount)
|
||||
assert.Equal(t, pp1.CreateAt, response.LastViewedAt)
|
||||
|
||||
response, err = th.App.MarkChannelAsUnreadFromPost(pp2.Id, u1.Id)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, int64(2), response.MsgCount)
|
||||
unread, err = th.App.GetChannelUnread(pc1.Id, u1.Id)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, int64(0), unread.MsgCount)
|
||||
assert.Equal(t, pp2.CreateAt, response.LastViewedAt)
|
||||
})
|
||||
|
||||
t.Run("Can't unread an imaginary post", func(t *testing.T) {
|
||||
response, err := th.App.MarkChannelAsUnreadFromPost("invalid4ofngungryquinj976y", u1.Id)
|
||||
assert.NotNil(t, err)
|
||||
assert.Nil(t, response)
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user