Move lib files inside a discourse_chat directory, and make the notifications run on a delay after post creation
This commit is contained in:
parent
8d70b4ad46
commit
4ad9ad6a7a
|
@ -3,3 +3,5 @@ plugins:
|
|||
default: false
|
||||
chat_discourse_username:
|
||||
default: system
|
||||
chat_delay_seconds:
|
||||
default: 20
|
22
plugin.rb
22
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"
|
||||
|
||||
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
|
||||
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue