Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

FEATURE: Publish WebHook event when solving/unsolving #85

Merged
merged 8 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import PostCooked from "discourse/widgets/post-cooked";
import { formatUsername } from "discourse/lib/utilities";
import { iconHTML } from "discourse-common/lib/icon-library";
import { iconNode } from "discourse-common/lib/icon-library";
import SearchAdvancedOptions from "discourse/components/search-advanced-options";

function clearAccepted(topic) {
const posts = topic.get("postStream.posts");
Expand Down Expand Up @@ -262,6 +263,16 @@ export default {
})
});

SearchAdvancedOptions.reopen({
didInsertElement() {
this._super();
this.statusOptions.push({
name: I18n.t("search.advanced.statuses.solved"),
value: "solved"
});
}
});

withPluginApi("0.1", initializeWithApi);

withPluginApi("0.8.10", api => {
Expand Down
11 changes: 11 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ en:
topic_statuses:
solved:
help: "This topic has a solution"

search:
advanced:
statuses:
solved: "are solved"

admin:
web_hooks:
solved_event:
name: "Solved Event"
details: "When a user marks a post as the accepted or unaccepted answer."
31 changes: 29 additions & 2 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
register_asset 'stylesheets/mobile/solutions.scss', :mobile

after_initialize do

SeedFu.fixture_paths << Rails.root.join("plugins", "discourse-solved", "db", "fixtures").to_s

[
Expand Down Expand Up @@ -138,6 +139,11 @@ def self.accept_answer!(post, acting_user, topic: nil)
topic.save!
post.save!

if WebHook.active_web_hooks(:solved).exists?
payload = WebHook.generate_payload(:post, post)
WebHook.enqueue_solved_hooks(:accepted_solution, post, payload)
end

DiscourseEvent.trigger(:accepted_solution, post)
end

Expand Down Expand Up @@ -172,6 +178,12 @@ def self.unaccept_answer!(post, topic: nil)
)

notification.destroy! if notification

if WebHook.active_web_hooks(:solved).exists?
payload = WebHook.generate_payload(:post, post)
WebHook.enqueue_solved_hooks(:unaccepted_solution, post, payload)
end

DiscourseEvent.trigger(:unaccepted_solution, post)
end
end
Expand Down Expand Up @@ -348,6 +360,21 @@ def solved_count
end
end

class ::WebHook
def self.enqueue_solved_hooks(event, post, payload = nil)
if active_web_hooks('solved').exists? && post.present?
payload ||= WebHook.generate_payload(:post, post)

WebHook.enqueue_hooks(:solved, event,
id: post.id,
category_id: post.topic&.category_id,
tag_ids: post.topic&.tags&.pluck(:id),
payload: payload
)
end
end
end

require_dependency 'topic_view_serializer'
class ::TopicViewSerializer
attributes :accepted_answer
Expand Down Expand Up @@ -465,7 +492,7 @@ def accepted_answer

#TODO Remove when plugin is 1.0
if Search.respond_to? :advanced_filter
Search.advanced_filter(/in:solved/) do |posts|
Search.advanced_filter(/status:solved/) do |posts|
posts.where("topics.id IN (
SELECT tc.topic_id
FROM topic_custom_fields tc
Expand All @@ -475,7 +502,7 @@ def accepted_answer

end

Search.advanced_filter(/in:unsolved/) do |posts|
Search.advanced_filter(/status:unsolved/) do |posts|
posts.where("topics.id NOT IN (
SELECT tc.topic_id
FROM topic_custom_fields tc
Expand Down
9 changes: 9 additions & 0 deletions spec/fabricators/solved_hook_fabricator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

Fabricator(:solved_web_hook, from: :web_hook) do
transient solved_hook: WebHookEventType.find_by(name: 'solved')

after_build do |web_hook, transients|
web_hook.web_hook_event_types = [transients[:solved_hook]]
end
end
24 changes: 24 additions & 0 deletions spec/integration/solved_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'rails_helper'
require_relative '../fabricators/solved_hook_fabricator.rb'

RSpec.describe "Managing Posts solved status" do
let(:topic) { Fabricate(:topic) }
Expand Down Expand Up @@ -98,6 +99,17 @@
post "/solution/accept.json", params: { id: whisper.id }
expect(response.status).to eq(403)
end

it 'triggers a webhook' do
Fabricate(:solved_web_hook)
post "/solution/accept.json", params: { id: p1.id }

job_args = Jobs::EmitWebHookEvent.jobs[0]["args"].first

expect(job_args["event_name"]).to eq("accepted_solution")
payload = JSON.parse(job_args["payload"])
expect(payload["id"]).to eq(p1.id)
end
end

describe '#unaccept' do
Expand All @@ -122,6 +134,18 @@
expect(p1.custom_fields["is_accepted_answer"]).to eq(nil)
expect(p1.topic.custom_fields["accepted_answer_post_id"]).to eq(nil)
end

end

it 'triggers a webhook' do
Fabricate(:solved_web_hook)
post "/solution/unaccept.json", params: { id: p1.id }

job_args = Jobs::EmitWebHookEvent.jobs[0]["args"].first

expect(job_args["event_name"]).to eq("unaccepted_solution")
payload = JSON.parse(job_args["payload"])
expect(payload["id"]).to eq(p1.id)
end
end
end