Move lib files inside a discourse_chat directory, and make the notifications run on a delay after post creation

This commit is contained in:
David Taylor 2017-06-30 11:10:11 +01:00
parent 8d70b4ad46
commit 4ad9ad6a7a
10 changed files with 72 additions and 6 deletions

View File

@ -2,4 +2,6 @@ plugins:
chat_enabled:
default: false
chat_discourse_username:
default: system
default: system
chat_delay_seconds:
default: 20

View File

@ -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

View File

@ -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