feeds/lib/feed_model/migrate.rb

66 lines
2.9 KiB
Ruby

module Migrate
def self.call
puts ['feeds migrate start']
gem_root = Feeds::Engine.root
require File.join(gem_root, 'app/models/site_feed')
require File.join(gem_root, 'app/models/site_feed_annc')
locales = Site.first.in_use_locales rescue I18n.available_locales
trans = {}
locales.each do |locale|
locale = locale.to_s
trans[locale] = {}
I18n.with_locale(locale) do
trans[locale]['top'] = I18n.t(:top)
trans[locale]['hot'] = I18n.t(:hot)
trans[locale]['more_plus'] = I18n.t("feed.more")
end
end
org_remote_site_urls = SiteFeed.all.pluck(:remote_site_url).uniq
all_remote_site_urls = org_remote_site_urls.map{|a| a.split('://')[1]}.uniq
all_remote_site_urls.each do |remote_site_url|
match_urls = org_remote_site_urls.select{|s| s.include?("://#{remote_site_url}")}
if match_urls.count == 2
SiteFeed.where(:remote_site_url.in=>match_urls).update_all(:remote_site_url=>"https://#{remote_site_url}")
SiteFeedAnnc.where(:remote_site_url.in=>match_urls).update_all(:remote_site_url=>"https://#{remote_site_url}")
end
end
SiteFeed.all.order_by(:channel_title=>-1).to_a.group_by(&:remote_site_url).each do |url, site_feeds|
site_feed = site_feeds.first
channel_title_translations = site_feed.channel_title_translations
SiteFeed.where(:id.in=>site_feeds[1..-1].map{|sf| sf.id}).update_all(:channel_title_translations => channel_title_translations)
end
SiteFeedAnnc.where(:feed_id.nin=>SiteFeed.all.pluck(:id)).destroy
SiteFeed.each do |site_feed|
site_feed.add_notify
count = SiteFeedAnnc.where(feed_id: site_feed.id).count
if count>1
SiteFeedAnnc.where(feed_id: site_feed.id).limit(count-1).destroy
end
tmp = SiteFeedAnnc.where(feed_id: site_feed.id).first
if site_feed.disabled != true
if tmp.nil?
tmp = SiteFeedAnnc.new(feed_id: site_feed.id)
end
site_feed.sync_data_to_annc(tmp)
tmp.raw_all_contents_for_feed_translations = tmp.cache_annc(false,locales,trans)
tmp.save
if site_feed.feeds_model && site_feed.feeds_finish_callback
feeds_model = site_feed.feeds_model.constantize
feeds_model.send(site_feed.feeds_finish_callback, "update_all", {"data"=>site_feed.get_annc(false),"feed_id"=>site_feed.id,"category_id"=>site_feed.merge_with_category})
end
elsif !tmp.nil?
tmp.destroy
end
end
SiteFeedAnnc.create_indexes
sync_fields = SiteFeed::ModuleAppSyncFields
ModuleApp.where(:feeds_model.ne=>nil).each do |module_app|
sync_fields_data = sync_fields.map{|f| [f, module_app[f]] }.to_h
SiteFeedAnnc.where(:channel_key=>module_app.key).update_all(sync_fields_data)
SiteFeed.where(:channel_key=>module_app.key).update_all(sync_fields_data)
end
SiteFeedAnnc.update_all({"$unset" => {"all_contents_for_feed" => ""}})
puts ['feeds migrate finished']
end
end