feeds/app/controllers/admin/feeds_controller.rb

102 lines
2.7 KiB
Ruby

class Admin::FeedsController < OrbitAdminController
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 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
url = params['url'].chomp("/") + "/feeds/channel_lists"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
data = response.body
data = JSON.parse(data)
render :json => data.to_json
end
def get_feed_list
url = params['url'].chomp("/") + params[:feed_list_url]
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
data = response.body
data = JSON.parse(data)
data_to_send = {}
data_to_send["feeds"] = []
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
end