feeds/app/controllers/admin/feeds_controller.rb

204 lines
6.2 KiB
Ruby

class Admin::FeedsController < OrbitAdminController
layout :compute_layout
helper_method :search_all_words
def compute_layout
if action_name== 'annc_content'
false
else
'back_end'
end
end
def index
@site_feeds = SiteFeed.all.group_by(&:remote_site_url)
@school_urls = @site_feeds.keys
redirect_to new_admin_feed_path and return if @site_feeds.count == 0
end
def new
end
def annc_content
site_feed_annc = SiteFeedAnnc.where(id: params['feed_annc_id']).first
@annc = site_feed_annc.get_annc(params['annc_uid'].to_s) rescue {}
end
def process_annc
now_process = params['process']
annc_uid = params['annc_uid'].to_s
site_feed_annc = SiteFeedAnnc.where(id: params['feed_annc_id']).first
if !site_feed_annc.nil?
case now_process
when /is_top|is_hot/
cmd = now_process.split(':')
f = cmd[0]=='is_top' ? 'top_list' : 'hot_list'
if cmd[1] == 'enable'
if site_feed_annc[f].exclude?(annc_uid)
tmp = site_feed_annc.send(f)
tmp << annc_uid
site_feed_annc.update_attributes(f.to_sym => tmp)
end
else
if site_feed_annc[f].include?(annc_uid)
tmp = site_feed_annc.send(f)
tmp -= [annc_uid]
site_feed_annc.update_attributes(f.to_sym => tmp)
end
end
when /hidden|display/
if now_process == 'hidden'
if site_feed_annc[:hidden_annc].exclude?(annc_uid)
tmp = site_feed_annc.hidden_annc
tmp << annc_uid
site_feed_annc.update_attributes(:hidden_annc =>tmp)
end
else
if site_feed_annc[:hidden_annc].include?(annc_uid)
tmp = site_feed_annc.hidden_annc
tmp -= [annc_uid]
site_feed_annc.update_attributes(:hidden_annc =>tmp)
end
end
end
end
render :text => 'success'
end
def announcements
@filter_fields = {}
@filter_fields['feed.source'] = SiteFeed.all.pluck(:channel_title,:remote_site_url).collect do |a,b|
tp = (a.blank? || a[I18n.locale].blank?) ? b.gsub(/http:\/\/|https:\/\//,'').gsub(/\./,'-') : a[I18n.locale]
{:title => tp,:id => tp}
end
@filter_fields['feed.module_name'] = SiteFeed.all.map{|s| s.channel_key}.uniq.map do |key|
{:title => I18n.t("module_name.#{key}"),:id => key}
end
@module_name = params[:filters][:module_name].to_a rescue []
@module_name = SiteFeed.all.map{|s| s.channel_key}.uniq if @module_name == []
@all_feed_annc = SiteFeedAnnc.where(:channel_key.in => @module_name).order(created_at: 1).to_a rescue []
@source = params[:filters][:source] rescue []
@keywords = params[:keywords] rescue nil
if request.xhr?
render :partial => "announcements"
end
end
def get_category_list
app_key = params[:channel]
ma = ModuleApp.find_by_key(app_key) rescue nil
categories = []
if !ma.nil?
ma.categories.each do |category|
cat = {}
cat["title"] = category.title
cat["id"] = category.id.to_s
categories << cat
end
end
render :json => {"categories" => categories}.to_json
end
def get_channel_list
puts params['url']
url = URI.decode(params['url']).chomp("/") + "/feeds/channel_lists"
puts url
uri = URI.parse(URI.decode(url).encode)
begin
res = Timeout.timeout(10){Net::HTTP.get_response(uri).body}
rescue
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if (uri.scheme == "https")
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
res = http.get(uri.request_uri).body rescue nil
puts uri.to_s
puts uri.hostname
end
data = JSON.parse(res) rescue {}
print data
render :json => data.to_json
end
def get_feed_list
url = params['url'].chomp("/") + params[:feed_list_url]
uri = URI.parse(url)
begin
res = Net::HTTP.start(uri.host,
uri.port,:use_ssl => uri.scheme == 'https',
open_timeout: 10,read_timeout: 10,
verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
req = Net::HTTP::Get.new(uri)
http.request(req)
end
res = res.code=='200' ? res.body : nil
rescue
res = nil
puts uri.to_s
puts uri.hostname
end
data = JSON.parse(res) rescue {}
data_to_send = {}
data_to_send["feeds"] = []
Array(data["feeds"]).each do |feed|
sf = SiteFeed.find_by(:feed_uid => feed["uid"]) rescue nil
if !sf.nil?
feed["subscribed"] = true
else
feed["subscribed"] = false
end
data_to_send["feeds"] << feed
end
render :json => data_to_send.to_json
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.save
end
render :json => {"success" => true, "title" => params["channel_title_translations"][I18n.locale.to_s]}.to_json
end
def subscribe
site_feed = SiteFeed.new
site_feed.remote_site_url = params[:url].chomp("/")
site_feed.merge_with_category = params[:category]
site_feed.channel_name = params[:channel]
site_feed.channel_key = params[:channel_key]
site_feed.feed_uid = params[:feed][:uid]
site_feed.feed_name_translations = params[:feed][:title_translations]
site_feed.disabled = false
site_feed.feed_url = params[:feed][:url]
site_feed.save
render :json => {"success" => true}.to_json
end
def disable
feed = SiteFeed.find(params[:feed_id]) rescue nil
if !feed.nil?
feed.disabled = params[:disable]
feed.save
end
render :json => {"success" => true}.to_json
end
def unsubscribe
sf = SiteFeed.find_by(:feed_uid => params[:feed_uid]) rescue nil
if !sf.nil?
sf.destroy
end
render :json => {"success" => true}.to_json
end
def force_refresh
sf = SiteFeed.find_by(:feed_uid => params[:feed_uid]) rescue nil
if !sf.nil?
FeedModel::Cache.recreate_annc_cache(sf)
end
render :json => {"success" => true}.to_json
end
def search_all_words(target, word)
target = target.upcase
words = word.upcase.split(' ')
words.select { |value| target.include? value } == words
end
end