FEATURE: Remap group mentions when group name has been changed.

This commit is contained in:
Guo Xiang Tan 2017-01-18 13:39:34 +08:00
parent 59dfb51a35
commit 706b4f6b9f
4 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,14 @@
module Jobs
class UpdateGroupMentions < Jobs::Base
def execute(args)
group = Group.find_by(id: args[:group_id])
return unless group
previous_group_name = args[:previous_name]
GroupMentionsUpdater.update(group.name, previous_group_name)
end
end
end

View File

@ -22,6 +22,9 @@ class Group < ActiveRecord::Base
after_save :update_primary_group
after_save :update_title
after_save :enqueue_update_mentions_job,
if: Proc.new { |g| g.name_was && g.name_changed? }
after_save :expire_cache
after_destroy :expire_cache
@ -493,6 +496,15 @@ SQL
builder.exec
end
end
private
def enqueue_update_mentions_job
Jobs.enqueue(:update_group_mentions,
previous_name: self.name_was,
group_id: self.id
)
end
end
# == Schema Information

View File

@ -0,0 +1,14 @@
class GroupMentionsUpdater
def self.update(current_name, previous_name)
Post.where(
"cooked LIKE '%class=\"mention-group\"%' AND raw LIKE :previous_name",
previous_name: "%@#{previous_name}%"
).find_in_batches do |posts|
posts.each do |post|
post.raw.gsub!(/(^|\s)(@#{previous_name})(\s|$)/, "\\1@#{current_name}\\3")
post.save!
end
end
end
end

View File

@ -0,0 +1,39 @@
require 'rails_helper'
RSpec.describe GroupMentionsUpdater do
let(:post) { Fabricate(:post) }
describe '.update' do
it 'should update valid group mentions' do
new_group_name = 'awesome_team'
old_group_name = 'team'
[
["@#{old_group_name} is awesome!", "@#{new_group_name} is awesome!"],
["This @#{old_group_name} is awesome!", "This @#{new_group_name} is awesome!"],
["Mention us @ @#{old_group_name}", "Mention us @ @#{new_group_name}"],
].each do |raw, expected_raw|
group = Fabricate(:group, name: old_group_name)
post.update!(raw: raw)
group.update!(name: new_group_name)
post.reload
expect(post.raw_mentions).to eq([new_group_name])
expect(post.raw).to eq(expected_raw)
group.destroy!
end
end
it 'should not update invalid group mentions' do
group = Fabricate(:group, name: 'team')
post.update!(raw: 'This is not valid@team.com')
expect(post.reload.raw_mentions).to eq([])
group.update!(name: 'new_team_name')
expect(post.reload.raw_mentions).to eq([])
end
end
end