Merge pull request #5294 from tgxworld/onceoff_job_remap_images_link

Add onceoff job to remap bot images link.
This commit is contained in:
Neil Lalonde 2017-11-07 11:04:29 -05:00 committed by GitHub
commit faf8bba9a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,36 @@
module Jobs
module DiscourseNarrativeBot
class RemapOldBotImages < ::Jobs::Onceoff
def execute_onceoff(args)
paths = [
"/images/font-awesome-link.png",
"/images/unicorn.png",
"/images/font-awesome-ellipsis.png",
"/images/font-awesome-bookmark.png",
"/images/font-awesome-smile.png",
"/images/font-awesome-flag.png",
"/images/font-awesome-search.png",
"/images/capybara-eating.gif",
"/images/font-awesome-pencil.png",
"/images/font-awesome-trash.png",
"/images/font-awesome-rotate-left.png",
"/images/font-awesome-gear.png",
]
Post.raw_match("/images/").where(user_id: -2).find_each do |post|
if (matches = post.raw.scan(/(?<!\/plugins\/discourse-narrative-bot)(#{paths.join("|")})/)).present?
new_raw = post.raw
matches.each do |match|
path = match.first
new_raw = new_raw.gsub(path, "/plugins/discourse-narrative-bot#{path}")
end
post.update_columns(raw: new_raw)
post.rebake!
end
end
end
end
end
end

View File

@ -19,6 +19,7 @@ after_initialize do
'../jobs/narrative_init.rb',
'../jobs/send_default_welcome_message.rb',
'../jobs/onceoff/grant_badges.rb',
'../jobs/onceoff/remap_old_bot_images.rb',
'../lib/discourse_narrative_bot/actions.rb',
'../lib/discourse_narrative_bot/base.rb',
'../lib/discourse_narrative_bot/new_user_narrative.rb',

View File

@ -0,0 +1,42 @@
require 'rails_helper'
RSpec.describe Jobs::DiscourseNarrativeBot::RemapOldBotImages do
context "when bot's post contains an old link" do
let(:post) do
Fabricate(:post,
user_id: -2,
raw: 'If youd like to learn more, select <img src="/images/font-awesome-gear.png" width="16" height="16"> <img src="/images/font-awesome-ellipsis.png" width="16" height="16"> below and <img src="/images/font-awesome-bookmark.png" width="16" height="16"> **bookmark this private message**. If you do, there may be a :gift: in your future!'
)
end
before do
post
end
it 'should remap the links correctly' do
expected_raw = 'If youd like to learn more, select <img src="/plugins/discourse-narrative-bot/images/font-awesome-gear.png" width="16" height="16"> <img src="/plugins/discourse-narrative-bot/images/font-awesome-ellipsis.png" width="16" height="16"> below and <img src="/plugins/discourse-narrative-bot/images/font-awesome-bookmark.png" width="16" height="16"> **bookmark this private message**. If you do, there may be a :gift: in your future!'
2.times do
described_class.new.execute_onceoff({})
expect(post.reload.raw).to eq(expected_raw)
end
end
context 'subfolder' do
let(:post) do
Fabricate(:post,
user_id: -2,
raw: 'If youd like to learn more, select <img src="/community/images/font-awesome-ellipsis.png" width="16" height="16"> below and <img src="/community/images/font-awesome-bookmark.png" width="16" height="16"> **bookmark this private message**. If you do, there may be a :gift: in your future!'
)
end
it 'should remap the links correctly' do
described_class.new.execute_onceoff({})
expect(post.reload.raw).to eq(
'If youd like to learn more, select <img src="/community/plugins/discourse-narrative-bot/images/font-awesome-ellipsis.png" width="16" height="16"> below and <img src="/community/plugins/discourse-narrative-bot/images/font-awesome-bookmark.png" width="16" height="16"> **bookmark this private message**. If you do, there may be a :gift: in your future!'
)
end
end
end
end