feeds/app/controllers/feeds_controller.rb

166 lines
6.5 KiB
Ruby

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
locales = Site.first.in_use_locales rescue I18n.available_locales
locales.map!{|l| l.to_s}
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
need_write = true
if params[:type] == 'create'
trans = {}
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]
if feed_annc[:all_contents_for_feed][locale_sym].index{|aa| aa["id"] == a["id"]}
next
end
insert_idx = 0
if time_field_value
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
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
end
feed_annc.instance_variable_set(:@skip_callback, true)
feed_annc.save!
elsif params[:type] == 'update'
trans = {}
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]
has_create = false
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
has_create = true
break
end
end
unless has_create
insert_idx = 0
if time_field_value
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
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
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
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
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
end
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
end
render :json => {success: true}
end
end