forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes zammad#2715 - Twitter DM URLs are broken
When importing Twitter DMs (articles of type "twitter direct-message"), Zammad generates a source link URL of an incorrect format: https://twitter.com/statuses/:id This format is for statuses (or "tweets"). The appropriate URL scheme for direct messages is: https://twitter.com/messages/:recipient_id-:current_user_id This commit adds conditional logic for setting Twitter link URLs based on message type. It also provides a DB migration to rectify existing, broken DM URLs. For performance purposes, this migration is performed in the background and limited to the latest 10,000 DMs. GitHub: zammad#2715
- Loading branch information
1 parent
9409d17
commit 147cbc2
Showing
8 changed files
with
119 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Issue2715FixBrokenTwitterUrlsJob < ApplicationJob | ||
def perform | ||
Ticket::Article.joins(:type) | ||
.where(ticket_article_types: { name: 'twitter direct-message' }) | ||
.order(created_at: :desc) | ||
.limit(10_000) | ||
.find_each do |dm| | ||
dm.preferences[:links]&.each do |link| | ||
link[:url] = "https://twitter.com/messages/#{dm.preferences[:twitter][:recipient_id]}-#{dm.preferences[:twitter][:sender_id]}" | ||
end | ||
|
||
dm.save! | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
db/migrate/20191107181428_issue_2715_fix_broken_twitter_urls.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require_dependency 'issue_2715_fix_broken_twitter_urls_job' # Rails autoloading expects `issue2715_fix...` | ||
|
||
class Issue2715FixBrokenTwitterUrls < ActiveRecord::Migration[5.2] | ||
def up | ||
return if !Setting.find_by(name: 'system_init_done') | ||
|
||
Issue2715FixBrokenTwitterUrlsJob.perform_later | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require 'rails_helper' | ||
require_dependency 'issue_2715_fix_broken_twitter_urls_job' # Rails autoloading expects `issue2715_fix...` | ||
|
||
RSpec.describe Issue2715FixBrokenTwitterUrls, type: :db_migration do | ||
it 'invokes the corresponding job', :performs_jobs do | ||
expect { migrate } | ||
.to have_enqueued_job(Issue2715FixBrokenTwitterUrlsJob) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'rails_helper' | ||
require_dependency 'issue_2715_fix_broken_twitter_urls_job' # Rails autoloading expects `issue2715_fix...` | ||
|
||
RSpec.describe Issue2715FixBrokenTwitterUrlsJob, type: :job do | ||
context 'with existing Twitter articles' do | ||
let!(:dm) { create(:twitter_dm_article, preferences: dm_preferences) } | ||
|
||
let(:dm_preferences) do | ||
{ | ||
links: Array.new(5, &link_hash), | ||
twitter: { | ||
recipient_id: recipient_id, | ||
sender_id: sender_id, | ||
}, | ||
} | ||
end | ||
|
||
# NOTE: Faker 2.0+ has deprecated the `#number(20)` syntax in favor of `#number(digits: 20)`. | ||
let(:link_hash) { ->(_) { { url: "https://twitter.com/statuses/#{Faker::Number.number(20)}" } } } | ||
let(:recipient_id) { '1234567890' } | ||
let(:sender_id) { '0987654321' } | ||
|
||
it 'reformats all Twitter DM URLs' do | ||
expect { described_class.perform_now } | ||
.to change { urls_of(dm) } | ||
.to all(match(%r{^https://twitter.com/messages/#{recipient_id}-#{sender_id}$})) | ||
end | ||
|
||
def urls_of(article) | ||
article.reload.preferences[:links].map { |link| link[:url] } | ||
end | ||
end | ||
end |