From ce3989fc6aee743e0f37650a993b4720e3c028e2 Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Thu, 7 Dec 2017 11:37:43 +0900 Subject: [PATCH] Add embed_url to preview cards (#5775) --- .../features/status/components/card.js | 2 +- app/models/preview_card.rb | 1 + .../rest/preview_card_serializer.rb | 2 +- app/services/fetch_link_card_service.rb | 9 +++------ ...130000000_add_embed_url_to_preview_cards.rb | 18 ++++++++++++++++++ db/schema.rb | 3 ++- lib/tasks/mastodon.rake | 12 ++++++++++++ 7 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20171130000000_add_embed_url_to_preview_cards.rb diff --git a/app/javascript/mastodon/features/status/components/card.js b/app/javascript/mastodon/features/status/components/card.js index 680bf63ab7..d3e322c369 100644 --- a/app/javascript/mastodon/features/status/components/card.js +++ b/app/javascript/mastodon/features/status/components/card.js @@ -101,7 +101,7 @@ export default class Card extends React.PureComponent { onClick={this.handlePhotoClick} role='button' tabIndex='0' - src={card.get('url')} + src={card.get('embed_url')} alt={card.get('title')} width={card.get('width')} height={card.get('height')} diff --git a/app/models/preview_card.rb b/app/models/preview_card.rb index e2bf65d947..5baddba8ab 100644 --- a/app/models/preview_card.rb +++ b/app/models/preview_card.rb @@ -21,6 +21,7 @@ # height :integer default(0), not null # created_at :datetime not null # updated_at :datetime not null +# embed_url :string default(""), not null # class PreviewCard < ApplicationRecord diff --git a/app/serializers/rest/preview_card_serializer.rb b/app/serializers/rest/preview_card_serializer.rb index 9c460332c2..2df9d07a7c 100644 --- a/app/serializers/rest/preview_card_serializer.rb +++ b/app/serializers/rest/preview_card_serializer.rb @@ -6,7 +6,7 @@ class REST::PreviewCardSerializer < ActiveModel::Serializer attributes :url, :title, :description, :type, :author_name, :author_url, :provider_name, :provider_url, :html, :width, :height, - :image + :image, :embed_url def image object.image? ? full_asset_url(object.image.url(:original)) : nil diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb index 37cf75379f..cec96d9277 100644 --- a/app/services/fetch_link_card_service.rb +++ b/app/services/fetch_link_card_service.rb @@ -74,9 +74,6 @@ class FetchLinkCardService < BaseService return false unless response.respond_to?(:type) - # The photo will change the URL. So, to avoid duplication of URLs, PreviewCard needs to be checked again. - @card = PreviewCard.find_by(url: response.url) || @card if response.type == 'photo' - @card.type = response.type @card.title = response.respond_to?(:title) ? response.title : '' @card.author_name = response.respond_to?(:author_name) ? response.author_name : '' @@ -90,9 +87,9 @@ class FetchLinkCardService < BaseService when 'link' @card.image = URI.parse(response.thumbnail_url) if response.respond_to?(:thumbnail_url) when 'photo' - @card.url = response.url - @card.width = response.width.presence || 0 - @card.height = response.height.presence || 0 + @card.embed_url = response.url + @card.width = response.width.presence || 0 + @card.height = response.height.presence || 0 when 'video' @card.width = response.width.presence || 0 @card.height = response.height.presence || 0 diff --git a/db/migrate/20171130000000_add_embed_url_to_preview_cards.rb b/db/migrate/20171130000000_add_embed_url_to_preview_cards.rb new file mode 100644 index 0000000000..d19c0091bc --- /dev/null +++ b/db/migrate/20171130000000_add_embed_url_to_preview_cards.rb @@ -0,0 +1,18 @@ +require Rails.root.join('lib', 'mastodon', 'migration_helpers') + +class AddEmbedUrlToPreviewCards < ActiveRecord::Migration[5.1] + include Mastodon::MigrationHelpers + + disable_ddl_transaction! + + def up + safety_assured do + add_column_with_default :preview_cards, :embed_url, :string, default: '', allow_null: false + end + end + + def down + execute "UPDATE preview_cards SET url=embed_url WHERE embed_url!=''" + remove_column :preview_cards, :embed_url + end +end diff --git a/db/schema.rb b/db/schema.rb index 71674ece62..cf0deb8268 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20171129172043) do +ActiveRecord::Schema.define(version: 20171130000000) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -325,6 +325,7 @@ ActiveRecord::Schema.define(version: 20171129172043) do t.integer "height", default: 0, null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "embed_url", default: "", null: false t.index ["url"], name: "index_preview_cards_on_url", unique: true end diff --git a/lib/tasks/mastodon.rake b/lib/tasks/mastodon.rake index 995cf0d6f0..0f2cc536aa 100644 --- a/lib/tasks/mastodon.rake +++ b/lib/tasks/mastodon.rake @@ -326,5 +326,17 @@ namespace :mastodon do end end end + + desc 'Migrate photo preview cards made before 2.1' + task migrate_photo_preview_cards: :environment do + status_ids = Status.joins(:preview_cards) + .where(preview_cards: { embed_url: '', type: :photo }) + .reorder(nil) + .group(:id) + .pluck(:id) + + PreviewCard.where(embed_url: '', type: :photo).delete_all + LinkCrawlWorker.push_bulk status_ids + end end end