feeds/app/controllers/feeds_controller.rb

83 lines
3.1 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
locales = Site.first.in_use_locales rescue I18n.available_locales
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
if params[:type] == 'create'
locales.each do |locale|
trans = {}
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
locale_sym = locale.to_sym
params[:data].each do |a|
a = JSON.parse(a)
postdate = Time.parse(a["postdate"])
insert_idx = 0
if postdate
insert_idx = feed_annc[:all_contents_for_feed][locale_sym].index{|aa| aa["postdate"] <= postdate}
insert_idx = 0 if insert_idx.nil?
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
feed_annc.instance_variable_set(:@skip_callback, true)
feed_annc.save!
elsif params[:type] == 'update'
locales.each do |locale|
trans = {}
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
locale_sym = locale.to_sym
params[:data].each do |a|
a = JSON.parse(a)
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
break
end
end
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!
end
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
end
render :json => {success: true}
end
end