FIX: Polymorphic bookmarks for new user narrative bot (#16683)

This commit allows the new user narrative bot to work
with polymorphic bookmarks, gated behind the
use_polymorphic_bookmarks site setting.
This commit is contained in:
Martin Brennan 2022-05-09 16:19:18 +10:00 committed by GitHub
parent 222c8d9b6a
commit 4d0ac8636c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 6 deletions

View File

@ -243,7 +243,6 @@ module DiscourseNarrativeBot
post
end
# TODO (martin) [POLYBOOK] Fix up narrative bot bookmark interactions in a separate PR.
def missing_bookmark
return unless valid_topic?(@post.topic_id)
return if @post.user_id == self.discobot_user.id
@ -254,7 +253,6 @@ module DiscourseNarrativeBot
false
end
# TODO (martin) [POLYBOOK] Fix up narrative bot bookmark interactions in a separate PR.
def reply_to_bookmark
return unless valid_topic?(@post.topic_id)
return unless @post.user_id == self.discobot_user.id

View File

@ -281,10 +281,13 @@ after_initialize do
end
end
# TODO (martin) [POLYBOOK] Fix up narrative bot bookmark interactions in a separate PR.
self.add_model_callback(Bookmark, :after_commit, on: :create) do
if self.post && self.user.enqueue_narrative_bot_job?
Jobs.enqueue(:bot_input, user_id: self.user_id, post_id: self.post_id, input: "bookmark")
if self.user.enqueue_narrative_bot_job?
if SiteSetting.use_polymorphic_bookmarks && self.bookmarkable_type == "Post"
Jobs.enqueue(:bot_input, user_id: self.user_id, post_id: self.bookmarkable_id, input: "bookmark")
elsif !SiteSetting.use_polymorphic_bookmarks && self.post.present?
Jobs.enqueue(:bot_input, user_id: self.user_id, post_id: self.post_id, input: "bookmark")
end
end
end

View File

@ -248,7 +248,22 @@ describe DiscourseNarrativeBot::NewUserNarrative do
end
end
it 'should create the right reply when bookmarks with reminders are enabled' do
it 'adds an after commit model callback to bookmark' do
Jobs.run_later!
bookmark = Fabricate(:bookmark, post: Fabricate(:post))
expect_job_enqueued(job: :bot_input, args: { user_id: bookmark.user_id, post_id: bookmark.post_id, input: "bookmark" })
end
it 'adds an after commit model callback to bookmark for polymorphic bookmarks (but only for post polymorphic bookmarks)' do
SiteSetting.use_polymorphic_bookmarks = true
Jobs.run_later!
bookmark = Fabricate(:bookmark, bookmarkable: Fabricate(:post))
expect_job_enqueued(job: :bot_input, args: { user_id: bookmark.user_id, post_id: bookmark.bookmarkable_id, input: "bookmark" })
bookmark2 = Fabricate(:bookmark, bookmarkable: Fabricate(:topic))
expect_not_enqueued_with(job: :bot_input, args: { user_id: bookmark.user_id, post_id: kind_of(Integer), input: "bookmark" })
end
it 'should create the right reply when the bookmark is created' do
post.update!(user: discobot_user)
narrative.expects(:enqueue_timeout_job).with(user)