Auto merge http and https remote_site_url.

Update cache when updating channel_title or remote_site_url.
This commit is contained in:
BoHung Chiu 2022-06-21 18:10:50 +08:00
parent 967f158612
commit 5ae52c6e5f
5 changed files with 155 additions and 19 deletions

View File

@ -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

View File

@ -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

View File

@ -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 href=\"#{a["source_url"]}\" target=\"_blank\" class=\"feed-source\">#{a["source-site-title"]}</a>",
"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 href=\"#{a["source_url"]}\" target=\"_blank\" class=\"feed-source\">#{a["source-site-title"]}</a>",
"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" => "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>",
"status" => "<a href=\"#{tmp["source-site"]}\" target=\"_blank\" class=\"feed-source\">#{tmp["source-site-title"]}</a>",
"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"] = "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>"
tmp["source-site"] = "<a href=\"#{tmp["source-site"]}\" target=\"_blank\" class=\"feed-source\">#{tmp["source-site-title"]}</a>"
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" => "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>",
"status" => "<a href=\"#{tmp["source-site"]}\" target=\"_blank\" class=\"feed-source\">#{tmp["source-site-title"]}</a>",
"status-class" => "status-source"
}
tmp["source-site-link"] = tmp["source-site"]
tmp["source-site"] = "<a href='#{tmp["source-site"]}' target='_blank' class='feed-source'>#{tmp["source-site-title"]}</a>"
tmp["source-site"] = "<a href=\"#{tmp["source-site"]}\" target=\"_blank\" class=\"feed-source\">#{tmp["source-site-title"]}</a>"
tmp["target"] = "_self"
tmp["more"] = trans[locale]['more_plus']
tmp["view_count"] = ""

View File

@ -46,8 +46,8 @@
<ul class="channel-list list-unstyled">
<li class="channel-item">
<span class="channel-name"><%= name %></span>
<% channels[name].each do |channel| %>
<ul class="channel-sublist">
<ul class="channel-sublist">
<% channels[name].each do |channel| %>
<li class="channel-subitem" >
<span class="channel-feedname"><%= channel.feed_name %></span>
<span class="label label-info"><%= channel.category.title rescue "" %></span>
@ -57,8 +57,8 @@
<button class="btn btn-danger btn-small unsubscribe-btn" data-feed-uid="<%= channel.feed_uid %>" data-feed-name="<%= channel.feed_name %>"><%= t("feed.unsubscribe") %></button>
</span>
</li>
</ul>
<% end %>
<% end %>
</ul>
</li>
</ul>
<% end %>
@ -135,7 +135,7 @@
dataType : "json",
type : "post"
}).done(function(){
el.parent().remove();
el.parents('.channel-subitem').remove();
})
}
});

View File

@ -14,10 +14,19 @@ Rails.application.routes.draw do
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.map{|l, s| ["channel_title.#{l}", s]}.to_h)
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|