(feature) New MessagesWillBeConsumed Server Plugin Hook (#23305)

* feature: implemented basic MessageWillBeConsumed hook and applied on GetSinglePost method.

* use hook in get methods

* chore: refactored hook usage and created utils functions to apply hook

* bugfix: single post not updating

* chore: adjusted hook to return post

* chore: reverted some uneeded  changes

* chore: updated hook to accept slice of posts

* bugfix: slice filled with niil values

* chore: MessageWillBeConsumed ranamed to MessagesWillBeConsumed

* Update plugin/hooks.go

Co-authored-by: Jesse Hallam <jesse@thehallams.ca>

* Add feature flag

* Update min version

* update tests to account for feature flag

* fix linting issues

---------

Co-authored-by: Matej Topolovac <>
Co-authored-by: mtopolovac <43346061+mtopolovac@users.noreply.github.com>
Co-authored-by: Jesse Hallam <jesse@thehallams.ca>
Co-authored-by: Kevin Hsieh <kevinh@qrypt.com>
Co-authored-by: Jesse Hallam <jesse.hallam@gmail.com>
Этот коммит содержится в:
Qrypt
2023-10-23 10:12:46 -04:00
коммит произвёл GitHub
родитель bdacc97454
Коммит a46ad3169c
9 изменённых файлов: 237 добавлений и 0 удалений

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

@@ -386,6 +386,8 @@ func (a *App) CreatePost(c request.CTX, post *model.Post, channel *model.Channel
// so we just return the one that was passed with post
rpost = a.PreparePostForClient(c, rpost, true, false, false)
a.applyPostWillBeConsumedHook(&rpost)
if rpost.RootId != "" {
if appErr := a.ResolvePersistentNotification(c, parentPostList.Posts[post.RootId], rpost.UserId); appErr != nil {
return nil, appErr
@@ -870,6 +872,8 @@ func (a *App) GetPostsPage(options model.GetPostsOptions) (*model.PostList, *mod
return nil, appErr
}
a.applyPostsWillBeConsumedHook(postList.Posts)
return postList, nil
}
@@ -889,6 +893,8 @@ func (a *App) GetPosts(channelID string, offset int, limit int) (*model.PostList
return nil, appErr
}
a.applyPostsWillBeConsumedHook(postList.Posts)
return postList, nil
}
@@ -906,6 +912,8 @@ func (a *App) GetPostsSince(options model.GetPostsSinceOptions) (*model.PostList
return nil, appErr
}
a.applyPostsWillBeConsumedHook(postList.Posts)
return postList, nil
}
@@ -929,6 +937,8 @@ func (a *App) GetSinglePost(postID string, includeDeleted bool) (*model.Post, *m
return nil, model.NewAppError("GetSinglePost", "app.post.cloud.get.app_error", nil, "", http.StatusForbidden)
}
a.applyPostWillBeConsumedHook(&post)
return post, nil
}
@@ -959,6 +969,8 @@ func (a *App) GetPostThread(postID string, opts model.GetPostsOptions, userID st
return nil, appErr
}
a.applyPostsWillBeConsumedHook(posts.Posts)
return posts, nil
}
@@ -972,6 +984,8 @@ func (a *App) GetFlaggedPosts(userID string, offset int, limit int) (*model.Post
return nil, appErr
}
a.applyPostsWillBeConsumedHook(postList.Posts)
return postList, nil
}
@@ -985,6 +999,8 @@ func (a *App) GetFlaggedPostsForTeam(userID, teamID string, offset int, limit in
return nil, appErr
}
a.applyPostsWillBeConsumedHook(postList.Posts)
return postList, nil
}
@@ -998,6 +1014,8 @@ func (a *App) GetFlaggedPostsForChannel(userID, channelID string, offset int, li
return nil, appErr
}
a.applyPostsWillBeConsumedHook(postList.Posts)
return postList, nil
}
@@ -1034,6 +1052,8 @@ func (a *App) GetPermalinkPost(c request.CTX, postID string, userID string) (*mo
return nil, appErr
}
a.applyPostsWillBeConsumedHook(list.Posts)
return list, nil
}
@@ -1062,6 +1082,8 @@ func (a *App) GetPostsBeforePost(options model.GetPostsOptions) (*model.PostList
return nil, appErr
}
a.applyPostsWillBeConsumedHook(postList.Posts)
return postList, nil
}
@@ -1090,6 +1112,8 @@ func (a *App) GetPostsAfterPost(options model.GetPostsOptions) (*model.PostList,
return nil, appErr
}
a.applyPostsWillBeConsumedHook(postList.Posts)
return postList, nil
}
@@ -1126,6 +1150,8 @@ func (a *App) GetPostsAroundPost(before bool, options model.GetPostsOptions) (*m
return nil, appErr
}
a.applyPostsWillBeConsumedHook(postList.Posts)
return postList, nil
}
@@ -1135,6 +1161,8 @@ func (a *App) GetPostAfterTime(channelID string, time int64, collapsedThreads bo
return nil, model.NewAppError("GetPostAfterTime", "app.post.get_post_after_time.app_error", nil, "", http.StatusInternalServerError).Wrap(err)
}
a.applyPostWillBeConsumedHook(&post)
return post, nil
}
@@ -2249,3 +2277,37 @@ func (a *App) GetPostInfo(c request.CTX, postID string) (*model.PostInfo, *model
}
return &info, nil
}
func (a *App) applyPostsWillBeConsumedHook(posts map[string]*model.Post) {
if !a.Config().FeatureFlags.ConsumePostHook {
return
}
postsSlice := make([]*model.Post, 0, len(posts))
for _, post := range posts {
postsSlice = append(postsSlice, post.ForPlugin())
}
a.ch.RunMultiHook(func(hooks plugin.Hooks) bool {
postReplacements := hooks.MessagesWillBeConsumed(postsSlice)
for _, postReplacement := range postReplacements {
posts[postReplacement.Id] = postReplacement
}
return true
}, plugin.MessagesWillBeConsumedID)
}
func (a *App) applyPostWillBeConsumedHook(post **model.Post) {
if !a.Config().FeatureFlags.ConsumePostHook {
return
}
ps := []*model.Post{*post}
a.ch.RunMultiHook(func(hooks plugin.Hooks) bool {
rp := hooks.MessagesWillBeConsumed(ps)
if len(rp) > 0 {
(*post) = rp[0]
}
return true
}, plugin.MessagesWillBeConsumedID)
}