From 4ad9ad6a7aedb711cdcf254caee54008c47b8b12 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 30 Jun 2017 11:10:11 +0100 Subject: [PATCH] Move lib files inside a discourse_chat directory, and make the notifications run on a delay after post creation --- config/settings.yml | 4 +- lib/{ => discourse_chat}/manager.rb | 0 lib/{ => discourse_chat}/provider.rb | 0 .../provider/slack/slack_provider.rb | 0 .../provider/telegram/telegram_provider.rb | 0 lib/{ => discourse_chat}/rule.rb | 0 plugin.rb | 24 +++++++-- spec/jobs/notify_chats_spec.rb | 50 +++++++++++++++++++ spec/lib/{ => discourse_chat}/manager_spec.rb | 0 spec/lib/{ => discourse_chat}/rule_spec.rb | 0 10 files changed, 72 insertions(+), 6 deletions(-) rename lib/{ => discourse_chat}/manager.rb (100%) rename lib/{ => discourse_chat}/provider.rb (100%) rename lib/{ => discourse_chat}/provider/slack/slack_provider.rb (100%) rename lib/{ => discourse_chat}/provider/telegram/telegram_provider.rb (100%) rename lib/{ => discourse_chat}/rule.rb (100%) create mode 100644 spec/jobs/notify_chats_spec.rb rename spec/lib/{ => discourse_chat}/manager_spec.rb (100%) rename spec/lib/{ => discourse_chat}/rule_spec.rb (100%) diff --git a/config/settings.yml b/config/settings.yml index b0754d5..2709ce3 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -2,4 +2,6 @@ plugins: chat_enabled: default: false chat_discourse_username: - default: system \ No newline at end of file + default: system + chat_delay_seconds: + default: 20 \ No newline at end of file diff --git a/lib/manager.rb b/lib/discourse_chat/manager.rb similarity index 100% rename from lib/manager.rb rename to lib/discourse_chat/manager.rb diff --git a/lib/provider.rb b/lib/discourse_chat/provider.rb similarity index 100% rename from lib/provider.rb rename to lib/discourse_chat/provider.rb diff --git a/lib/provider/slack/slack_provider.rb b/lib/discourse_chat/provider/slack/slack_provider.rb similarity index 100% rename from lib/provider/slack/slack_provider.rb rename to lib/discourse_chat/provider/slack/slack_provider.rb diff --git a/lib/provider/telegram/telegram_provider.rb b/lib/discourse_chat/provider/telegram/telegram_provider.rb similarity index 100% rename from lib/provider/telegram/telegram_provider.rb rename to lib/discourse_chat/provider/telegram/telegram_provider.rb diff --git a/lib/rule.rb b/lib/discourse_chat/rule.rb similarity index 100% rename from lib/rule.rb rename to lib/discourse_chat/rule.rb diff --git a/plugin.rb b/plugin.rb index 9af6f25..a26197a 100644 --- a/plugin.rb +++ b/plugin.rb @@ -33,13 +33,27 @@ after_initialize do end end - require_relative "lib/provider" - require_relative "lib/manager" - require_relative "lib/rule" + require_relative "lib/discourse_chat/provider" + require_relative "lib/discourse_chat/manager" + require_relative "lib/discourse_chat/rule" - DiscourseEvent.on(:post_created) do |post| + module ::Jobs + class NotifyChats < Jobs::Base + def execute(args) + return if not SiteSetting.chat_enabled? # Plugin may have been disabled since job triggered + + ::DiscourseChat::Manager.trigger_notifications(args[:post_id]) + end + end + end + + DiscourseEvent.on(:post_created) do |post| if SiteSetting.chat_enabled? - ::DiscourseChat::Manager.trigger_notifications(post.id) + # This will run for every post, even PMs. Don't worry, they're filtered out later. + Jobs.enqueue_in(SiteSetting.chat_delay_seconds.seconds, + :notify_chats, + post_id: post.id + ) end end diff --git a/spec/jobs/notify_chats_spec.rb b/spec/jobs/notify_chats_spec.rb new file mode 100644 index 0000000..0da3f96 --- /dev/null +++ b/spec/jobs/notify_chats_spec.rb @@ -0,0 +1,50 @@ +require 'rails_helper' + +RSpec.describe PostCreator do + let(:first_post) { Fabricate(:post) } + let(:topic) { Fabricate(:topic, posts: [first_post]) } + + before do + SiteSetting.queue_jobs = true + Jobs::NotifyChats.jobs.clear + end + + describe 'when a post is created' do + describe 'when plugin is enabled' do + before do + SiteSetting.chat_enabled = true + end + + it 'should schedule a chat notification job' do + Timecop.freeze do + post = PostCreator.new(topic.user, + raw: 'Some post content', + topic_id: topic.id + ).create! + + job = Jobs::NotifyChats.jobs.last + + expect(job['at']) + .to eq((Time.zone.now + SiteSetting.chat_delay_seconds.seconds).to_f) + + expect(job['args'].first['post_id']).to eq(post.id) + end + end + end + + describe 'when plugin is not enabled' do + before do + SiteSetting.chat_enabled = false + end + + it 'should not schedule a job for chat notifications' do + PostCreator.new(topic.user, + raw: 'Some post content', + topic_id: topic.id + ).create! + + expect(Jobs::NotifyChats.jobs).to eq([]) + end + end + end +end diff --git a/spec/lib/manager_spec.rb b/spec/lib/discourse_chat/manager_spec.rb similarity index 100% rename from spec/lib/manager_spec.rb rename to spec/lib/discourse_chat/manager_spec.rb diff --git a/spec/lib/rule_spec.rb b/spec/lib/discourse_chat/rule_spec.rb similarity index 100% rename from spec/lib/rule_spec.rb rename to spec/lib/discourse_chat/rule_spec.rb