feeds/app/controllers/feeds_controller.rb

165 lines
6.5 KiB
Ruby
Raw Normal View History

2022-05-09 12:10:29 +00:00
class FeedsController < ApplicationController
skip_before_action :verify_authenticity_token
def notify_change
feed = SiteFeed.where(:feed_uid=>params[:uid]).first
feed_annc = SiteFeedAnnc.where(:feed_id=>feed.id).first
if feed_annc
feeds_model = feed.feeds_model
if feeds_model.present?
feeds_model = feeds_model.constantize
end
feed_id = feed.id
feeds_uid_field = feed.feeds_uid_field rescue nil
feeds_update_callback = feed.feeds_update_callback rescue nil
feeds_time_field = feed.feeds_time_field
feeds_finish_callback = feed.feeds_finish_callback
category_id = feed.merge_with_category
can_create_record = feeds_model && feeds_uid_field && feeds_update_callback
2022-05-09 12:10:29 +00:00
locales = Site.first.in_use_locales rescue I18n.available_locales
locales.map!{|l| l.to_s}
2022-05-09 12:10:29 +00:00
site_root_url = Site.first.root_url rescue ""
main_directory = File.join("#{Rails.root}","public","site_feeds")
feed_directory = File.join(main_directory.to_s, feed.id.to_s)
feed_data = JSON.parse(File.read(File.join(feed_directory.to_s, feed.feed_uid + ".json")))
channel_key_pluralize = feed_annc.channel_key.pluralize
2022-06-21 03:12:37 +00:00
need_write = true
2022-05-09 12:10:29 +00:00
if params[:type] == 'create'
trans = {}
2022-05-09 12:10:29 +00:00
locales.each do |locale|
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
params[:data].each do |a|
a = JSON.parse(a)
a["category_id"] = category_id
locales.each do |locale|
locale_sym = locale.to_sym
time_field_value = Time.parse(a[feeds_time_field]) rescue a[feeds_time_field]
2022-05-23 04:33:46 +00:00
if feed_annc[:all_contents_for_feed][locale_sym].index{|aa| aa["id"] == a["id"]}
next
end
2022-05-09 12:10:29 +00:00
insert_idx = 0
if time_field_value
2022-05-23 05:08:00 +00:00
insert_idx = feed_annc[:all_contents_for_feed][locale_sym].index{|aa| (aa["org_is_top"] < a["org_is_top"] rescue false) || ((aa["org_is_top"] == a["org_is_top"]) && (aa[feeds_time_field] <= time_field_value))}
if insert_idx.nil?
insert_idx = ((a["org_is_top"] == 1) ? 0 : -1)
end
2022-05-09 12:10:29 +00:00
end
feed_annc.all_contents_for_feed_will_change!
feed_annc[:all_contents_for_feed][locale_sym].insert(insert_idx, feed_annc.process_tmp(a,locale,trans,site_root_url))
feed_data[channel_key_pluralize].insert(insert_idx, a)
end
if can_create_record
record = feeds_model.where(feeds_uid_field=>a["id"], :site_feed_id=>feed_id).first
if record.nil?
record = feeds_model.new
record.instance_variable_set(:@skip_callback, true)
record[feeds_uid_field] = a["id"]
record[:site_feed_id] = feed_id
record.save
record.instance_variable_set(:@skip_callback, true)
end
record.send(feeds_update_callback, a)
end
2022-05-09 12:10:29 +00:00
end
feed_annc.instance_variable_set(:@skip_callback, true)
feed_annc.save!
elsif params[:type] == 'update'
trans = {}
2022-05-09 12:10:29 +00:00
locales.each do |locale|
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
params[:data].each do |a|
a = JSON.parse(a)
a["category_id"] = category_id
locales.each do |locale|
locale_sym = locale.to_sym
2022-05-23 04:33:46 +00:00
has_create = false
2022-05-09 12:10:29 +00:00
feed_annc[:all_contents_for_feed][locale_sym].each_with_index do |aa, i|
if aa["id"] == a["id"]
feed_annc.all_contents_for_feed_will_change!
feed_annc[:all_contents_for_feed][locale_sym][i] = feed_annc.process_tmp(a,locale,trans,site_root_url)
feed_data[channel_key_pluralize][i] = a
2022-05-23 04:33:46 +00:00
has_create = true
2022-05-09 12:10:29 +00:00
break
end
end
2022-05-23 04:33:46 +00:00
unless has_create
insert_idx = 0
if time_field_value
2022-05-23 05:08:00 +00:00
insert_idx = feed_annc[:all_contents_for_feed][locale_sym].index{|aa| (aa["org_is_top"] < a["org_is_top"] rescue false) || ((aa["org_is_top"] == a["org_is_top"]) && (aa[feeds_time_field] <= time_field_value))}
if insert_idx.nil?
insert_idx = ((a["org_is_top"] == 1) ? 0 : -1)
end
2022-05-23 04:33:46 +00:00
end
feed_annc.all_contents_for_feed_will_change!
feed_annc[:all_contents_for_feed][locale_sym].insert(insert_idx, feed_annc.process_tmp(a,locale,trans,site_root_url))
feed_data[channel_key_pluralize].insert(insert_idx, a)
end
2022-05-09 12:10:29 +00:00
end
if can_create_record
record = feeds_model.where(feeds_uid_field=>a["id"], :site_feed_id=>feed_id).first
if record.nil?
record = feeds_model.new
record.instance_variable_set(:@skip_callback, true)
record[feeds_uid_field] = a["id"]
record[:site_feed_id] = feed_id
record.save
record.instance_variable_set(:@skip_callback, true)
end
record.send(feeds_update_callback, a)
end
2022-05-09 12:10:29 +00:00
end
feed_annc.instance_variable_set(:@skip_callback, true)
feed_annc.save!
elsif params[:type] == 'destroy'
locales.each do |locale|
locale_sym = locale.to_sym
feed_annc.all_contents_for_feed_will_change!
feed_annc[:all_contents_for_feed][locale_sym].reject!{|a| params[:data].include?(a["id"]) }
feed_data[channel_key_pluralize].reject!{|a| params[:data].include?(a["id"]) }
end
feed_annc.instance_variable_set(:@skip_callback, true)
feed_annc.save!
if can_create_record
feeds_model.where(feeds_uid_field.to_sym.in =>params[:data].map{|a| a["id"]}, :site_feed_id=>feed_id).destroy
end
2022-06-21 03:12:37 +00:00
elsif params[:type] == 'update_all'
feed_data = params[:data]
FileUtils.mkdir_p(feed_directory) if !File.exists?(feed_directory)
File.open(File.join(feed_directory.to_s,feed.feed_uid + ".json"),"w") do |file|
feed_data.force_encoding("utf-8")
file.write(feed_data)
end
need_write = false
feed_annc.all_contents_for_feed_will_change!
feed_annc[:all_contents_for_feed] = feed_annc.cache_annc
feed_annc.save!
if feeds_finish_callback
feeds_model.send(feeds_finish_callback, 'update_all', {"data"=>feed.get_annc(false),"feed_id"=>feed.id,"category_id"=>feed.merge_with_category})
end
2022-05-09 12:10:29 +00:00
end
2022-06-21 03:12:37 +00:00
if need_write
feed_data = feed_data.to_json
FileUtils.mkdir_p(feed_directory) if !File.exists?(feed_directory)
File.open(File.join(feed_directory.to_s,feed.feed_uid + ".json"),"w") do |file|
feed_data.force_encoding("utf-8")
file.write(feed_data)
end
if feeds_finish_callback
feeds_model.send(feeds_finish_callback, params[:type])
end
end
2022-05-09 12:10:29 +00:00
end
render :json => {success: true}
end
end