MM-42810: Using websocket broadcast hook for permalink preview (#28627)

We use the newly introduced websocket broadcast hook system
to implement permalink preview efficiently. This is essentially
a re-do of https://github.com/mattermost/mattermost/pull/23812
using the new system.

https://mattermost.atlassian.net/browse/MM-42810
```release-note
NONE
```

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Agniva De Sarker
2024-10-21 09:35:32 +05:30
коммит произвёл GitHub
родитель 6939623e11
Коммит 64677dd554
11 изменённых файлов: 226 добавлений и 126 удалений

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

@@ -208,3 +208,59 @@ func TestAddMentionsAndAddFollowersHooks(t *testing.T) {
assert.Equal(t, `["`+userID+`"]`, msg.Event().GetData()["mentions"])
})
}
func TestPermalinkBroadcastHook(t *testing.T) {
th := Setup(t).InitBasic()
defer th.TearDown()
session, err := th.Server.Platform().CreateSession(th.Context, &model.Session{
UserId: th.BasicUser.Id,
})
require.NoError(t, err)
wc := &platform.WebConn{
Platform: th.Server.Platform(),
Suite: th.App,
UserId: session.UserId,
}
hook := &permalinkBroadcastHook{}
refPost := th.CreatePost(th.BasicChannel)
th.BasicPost.Metadata.Embeds = append(th.BasicPost.Metadata.Embeds, &model.PostEmbed{Type: model.PostEmbedPermalink, Data: &model.Permalink{
PreviewPost: model.NewPreviewPost(refPost, th.BasicTeam, th.BasicChannel),
}})
originalJSON, err := th.BasicPost.ToJSON()
require.NoError(t, err)
wsEvent := model.NewWebSocketEvent(model.WebsocketEventPosted, "", th.BasicPost.ChannelId, "", nil, "")
th.BasicPost.Metadata.Embeds[0].Data = nil
removedJSON, err := th.BasicPost.ToJSON()
require.NoError(t, err)
wsEvent.Add("post", removedJSON)
msg := platform.MakeHookedWebSocketEvent(wsEvent)
// User has permission.
err = hook.Process(msg, wc, map[string]any{
"preview_channel": th.BasicChannel,
"post_json": originalJSON,
})
require.NoError(t, err)
gotJSON, ok := msg.Get("post").(string)
require.True(t, ok)
require.Equal(t, originalJSON, gotJSON)
msg = platform.MakeHookedWebSocketEvent(wsEvent)
// User does not exist, and thus won't have permission to the channel.
wc.UserId = "otheruser"
err = hook.Process(msg, wc, map[string]any{
"preview_channel": th.BasicChannel,
"post_json": originalJSON,
})
require.NoError(t, err)
gotJSON, ok = msg.Get("post").(string)
require.True(t, ok)
require.Equal(t, removedJSON, gotJSON)
}