diff --git a/app/controllers/admin/feeds_controller.rb b/app/controllers/admin/feeds_controller.rb index 08ee76d..fff3ca4 100644 --- a/app/controllers/admin/feeds_controller.rb +++ b/app/controllers/admin/feeds_controller.rb @@ -192,12 +192,15 @@ class Admin::FeedsController < OrbitAdminController end def channel_title - site_feeds = SiteFeed.where(:remote_site_url => params["url"]) - site_feeds.each do |sf| - sf.channel_title_translations = params["channel_title_translations"] - sf.instance_variable_set(:@skip_callback, true) - sf.save - sf.sync_data_to_annc + Thread.new do + site_feeds = SiteFeed.where(:remote_site_url => params["url"]) + site_feeds.each do |sf| + sf.channel_title_translations = params["channel_title_translations"] + sf.instance_variable_set(:@skip_callback, true) + sf.save + sf.sync_data_to_annc + sf.update_channel_title + end end render :json => {"success" => true, "title" => params["channel_title_translations"][I18n.locale.to_s]}.to_json end @@ -212,8 +215,24 @@ class Admin::FeedsController < OrbitAdminController site_feed.feed_name_translations = params[:feed][:title_translations] site_feed.disabled = false site_feed.feed_url = params[:feed][:url] - other_site_feed = SiteFeed.where(:remote_site_url=>site_feed.remote_site_url).first + uri = URI(site_feed.remote_site_url) + site_host = uri.host + other_site_feeds = SiteFeed.where(:remote_site_url=>/:\/\/#{::Regexp.escape(site_host)}/) + other_site_feed = other_site_feeds.first site_feed.channel_title_translations = other_site_feed.channel_title_translations if other_site_feed + other_scheme = URI(other_site_feed.remote_site_url).scheme + site_scheme = uri.scheme + if (['http','https'] & [other_scheme, site_scheme]).count == 2 + if site_scheme == 'http' + site_feed.remote_site_url = site_feed.remote_site_url.sub('http://','https://') + else + remote_site_url = site_feed.remote_site_url + other_site_feeds.update_all(:remote_site_url=>remote_site_url) + site_feed_anncs = SiteFeedAnnc.where(:feed_id.in=>other_site_feeds.pluck(:id)) + site_feed_anncs.update_all(:remote_site_url=>remote_site_url) + site_feed_anncs.each{|sfa| sfa.update_remote_site_url} + end + end module_app = ModuleApp.where(:key=>site_feed.channel_key).first if module_app.feeds_model sync_fields_data = SiteFeed::ModuleAppSyncFields.map{|f| [f, module_app[f]] }.to_h diff --git a/app/models/site_feed.rb b/app/models/site_feed.rb index 6e30592..1eb6a7b 100644 --- a/app/models/site_feed.rb +++ b/app/models/site_feed.rb @@ -41,11 +41,30 @@ class SiteFeed end end after_save do - unless @skip_callback + if @skip_callback + unless @skip_fix_data + update_url = self.remote_site_url_changed? + if self.channel_title_changed? + self.update_channel_title(update_url) + elsif update_url + self.update_remote_site_url + end + end + else self.sync_data_to_annc end + @site_feed_annc = nil end scope :enabled, ->{where(:disabled => false)} + def site_feed_annc + @site_feed_annc ||= SiteFeedAnnc.where(:feed_id=>self.id).first + end + def update_channel_title(update_url=false) #update_url=true will also fix remote_site_url in data + site_feed_annc.update_channel_title(update_url) if site_feed_annc + end + def update_remote_site_url + site_feed_annc.update_remote_site_url if site_feed_annc + end def sync_data_to_annc(site_feed_annc=nil) category_title = self.category[:title] rescue {} tmp_channel_title = self.channel_title_for_cache diff --git a/app/models/site_feed_annc.rb b/app/models/site_feed_annc.rb index 73b3453..8e5b9e6 100644 --- a/app/models/site_feed_annc.rb +++ b/app/models/site_feed_annc.rb @@ -35,6 +35,95 @@ class SiteFeedAnnc end end end + def update_remote_site_url + update_fields = ['source_url','source-site'] + tmp_url = self.remote_site_url + self.all_contents_for_feed_will_change! + locales = Site.first.in_use_locales rescue I18n.available_locales + locales.each do |l| + anns = self[:all_contents_for_feed][l.to_s] + if anns + (0...anns.count).each do |i| + a = anns[i] + update_fields.each do |f| + a[f] = tmp_url + end + a['statuses'] = [] + if a[:is_top] + a['statuses'] << { + "status" => trans[locale]['top'], + "status-class" => "status-top" + } + end + if a[:is_hot] + a['statuses'] << { + "status" => trans[locale]['hot'], + "status-class" => "status-hot" + } + end + if !a["source-site-title"].blank? + a['statuses'] << { + "status" => "#{a["source-site-title"]}", + "status-class" => "status-source" + } + end + end + end + end + self.save + end + def update_channel_title(update_url=false) #update_url=true will also fix remote_site_url in data + update_fields = [] + if update_url + update_fields = ['source_url','source-site'] + end + tmp_url = self.remote_site_url + self.all_contents_for_feed_will_change! + 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 + locales.each do |locale| + source_site_title = (self[:channel_title][locale] rescue "") + anns = self[:all_contents_for_feed][locale.to_s] + if anns + (0...anns.count).each do |i| + a = anns[i] + a["source-site-title"] = source_site_title + update_fields.each do |f| + a[f] = tmp_url + end + a['statuses'] = [] + if a[:is_top] + a['statuses'] << { + "status" => trans[locale]['top'], + "status-class" => "status-top" + } + end + if a[:is_hot] + a['statuses'] << { + "status" => trans[locale]['hot'], + "status-class" => "status-hot" + } + end + if !a["source-site-title"].blank? + a['statuses'] << { + "status" => "#{a["source-site-title"]}", + "status-class" => "status-source" + } + end + end + end + end + self.save + end def process_tmp(a, locale=nil, trans=nil, site_root_url=nil) cat = self[:category_title] locale = I18n.locale.to_s if locale.nil? @@ -77,13 +166,12 @@ class SiteFeedAnnc end tmp["category"] = cat tmp["source_url"] = self.remote_site_url - tmp["source-site"] = self.remote_site_url tmp["source-site-title"] = (self[:channel_title][locale] rescue "") tmp["params"] = tmp["params"].to_s + "_" + self.feed_id.to_s + "h" if !tmp["source-site-title"].blank? tmp['statuses'] << { - "status" => "#{tmp["source-site-title"]}", + "status" => "#{tmp["source-site-title"]}", "status-class" => "status-source" } end @@ -95,7 +183,7 @@ class SiteFeedAnnc tmp["title"] = tmp["title_translations"][locale] tmp["subtitle"] = tmp["subtitle_translations"][locale] tmp["source-site-link"] = tmp["source-site"] - tmp["source-site"] = "#{tmp["source-site-title"]}" + tmp["source-site"] = "#{tmp["source-site-title"]}" tmp["link_to_show"] = !tmp["external_link"].blank? ? tmp["external_link"] : nil tmp["target"] = (site_root_url.blank? || tmp["link_to_show"].blank?) ? '_blank' : (tmp["link_to_show"].include?(site_root_url) ? '_self' : '_blank') tmp["img_src"] = tmp["image"]["thumb"] || "/assets/announcement-default.jpg" @@ -149,15 +237,16 @@ class SiteFeedAnnc } end tmp["category"] = cat + tmp["source_url"] = self.remote_site_url tmp["source-site"] = self.remote_site_url tmp["source-site-title"] = (self[:channel_title][locale] rescue "") tmp["params"] = tmp["params"].to_s + "_" + self.feed_id.to_s + "h" tmp['statuses'] << { - "status" => "#{tmp["source-site-title"]}", + "status" => "#{tmp["source-site-title"]}", "status-class" => "status-source" } tmp["source-site-link"] = tmp["source-site"] - tmp["source-site"] = "#{tmp["source-site-title"]}" + tmp["source-site"] = "#{tmp["source-site-title"]}" tmp["target"] = "_self" tmp["more"] = trans[locale]['more_plus'] tmp["view_count"] = "" diff --git a/app/views/admin/feeds/index.html.erb b/app/views/admin/feeds/index.html.erb index cded3da..fc2d910 100644 --- a/app/views/admin/feeds/index.html.erb +++ b/app/views/admin/feeds/index.html.erb @@ -46,8 +46,8 @@