From 6c4ee9d5b52b55667264e2b39fe77f072306b9d1 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Wed, 20 Dec 2017 17:47:30 +0800 Subject: [PATCH] FEATURE: Trigger user webhook when a user logs in/out. https://meta.discourse.org/t/how-do-you-learn-how-to-build-these-plugins/57946/10?u=tgxworld --- config/initializers/012-web_hook_events.rb | 8 +++++++- config/locales/client.en.yml | 2 +- spec/models/web_hook_spec.rb | 12 ++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/config/initializers/012-web_hook_events.rb b/config/initializers/012-web_hook_events.rb index 93edb4e00e5..ad5d94dcce1 100644 --- a/config/initializers/012-web_hook_events.rb +++ b/config/initializers/012-web_hook_events.rb @@ -22,7 +22,13 @@ DiscourseEvent.on(:post_edited) do |post, topic_changed| WebHook.enqueue_topic_hooks(:topic_edited, post.topic) if post.is_first_post? && topic_changed end -%i(user_created user_approved user_updated).each do |event| +%i( + user_logged_out + user_created + user_logged_in + user_approved + user_updated +).each do |event| DiscourseEvent.on(event) do |user| WebHook.enqueue_hooks(:user, user_id: user.id, event_name: event.to_s) end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 3c8e5aa4c04..a7fc27bf47a 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2813,7 +2813,7 @@ en: details: "When there is a new reply, edit, deleted or recovered." user_event: name: "User Event" - details: "When a user is created, approved or updated." + details: "When a user logs in, logs out, is created, approved or updated." delivery_status: title: "Delivery Status" inactive: "Inactive" diff --git a/spec/models/web_hook_spec.rb b/spec/models/web_hook_spec.rb index 6a2fef7f396..0f9af9d966f 100644 --- a/spec/models/web_hook_spec.rb +++ b/spec/models/web_hook_spec.rb @@ -207,6 +207,18 @@ describe WebHook do expect(job_args["event_name"]).to eq("user_updated") expect(job_args["user_id"]).to eq(user.id) + + user.logged_out + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + + expect(job_args["event_name"]).to eq("user_logged_out") + expect(job_args["user_id"]).to eq(user.id) + + user.logged_in + job_args = Jobs::EmitWebHookEvent.jobs.last["args"].first + + expect(job_args["event_name"]).to eq("user_logged_in") + expect(job_args["user_id"]).to eq(user.id) end end end