orbit-feed/app/controllers/panel/feed/back_end/feeds_controller.rb

151 lines
4.2 KiB
Ruby

# encoding: utf-8
class Panel::Feed::BackEnd::FeedsController < OrbitBackendController
before_filter :skip_current_site, only: [:new, :create]
CATEGORIES = %w{全部 緊急 一般 研討會 演講 活動 徵才 招標 拖吊 校外 過期 H1N1 失物招領 近期}
# Sites available for subscription
SITES = [ ["總務處 - 總務處", "www.ga.ntu.edu.tw", "0"],
["總務處 - 總務長室暨總務處秘書室", "sec.ga.ntu.edu.tw", "1"],
["總務處 - 文書組", "doc.ga.ntu.edu.tw", "2"],
["總務處 - 事務組", "general.ga.ntu.edu.tw", "3"],
["總務處 - 保管組", "property.ga.ntu.edu.tw", "4"],
["總務處 - 營繕組", "construction.ga.ntu.edu.tw", "5"],
["總務處 - 出納組", "cashier.ga.ntu.edu.tw", "6"],
["總務處 - 採購組", "procurement.ga.ntu.edu.tw", "7"],
["總務處 - 經營管理組", "fss.ga.ntu.edu.tw", "8"],
["總務處 - 駐警隊", "police.ga.ntu.edu.tw", "9"],
["社科院總務分處", "social.ga.ntu.edu.tw", "10"],
["醫學院總務分處", "medicine.ga.ntu.edu.tw", "11"],
["[測試站]ntu-ga-sec","ntu-ga-sec-new.tp.rulingcom.com","12"],
["[測試站]ntu-ga-police","ntu-ga-police-new.tp.rulingcom.com","13"] ]
def index
@feeds = AnnouncementFeed.where({from_request: false})
@permission_feeds = AnnouncementFeed.where({from_request: true})
respond_to do |format|
format.html
end
end
def new
@feed = AnnouncementFeed.new
@categories = CATEGORIES
@sites = @filtered_sites
end
def edit
@feed = AnnouncementFeed.find(params[:id])
@current_categories = @feed.categories
@categories = CATEGORIES
end
def create
@feed = AnnouncementFeed.new(params[:announcement_feed])
@feed.name = SITES[ params[:site].to_i ][0]
@feed.link = SITES[ params[:site].to_i ][1]
@categories = CATEGORIES
@sites = @filtered_sites
# if @feed.save
## Testing(TO-DO) ##
if permission_request(@feed)
@feed.save
redirect_to panel_feed_back_end_feeds_url
else
flash.now[:notice] = t('feed.connection_fail')
render 'new'
end
end
def update
@feed = AnnouncementFeed.find(params[:id])
if @feed.update_attributes(params[:announcement_feed])
redirect_to panel_feed_back_end_feeds_url
else
render 'edit'
end
end
def destroy
@feed = AnnouncementFeed.find(params[:id])
@feed.destroy
redirect_to panel_feed_back_end_feeds_url
end
def agree
require 'net/http'
require 'uri'
require 'json'
@feed = AnnouncementFeed.find(params[:id], conditions: [ "from_request = ?", true])
uri_path = "/panel/feed/back_end/feed_requests/accepted_request"
http = Net::HTTP.new(@feed.from_request_link, 80)
request = Net::HTTP::Post.new(uri_path)
categories = @feed.categories.join(' ')
body = {
'link' => @feed.link,
'name' => @feed.name,
'categories' => categories
}
3.times do
request.set_form_data(body)
request.add_field 'X-Auth-Token', '3kjlfksjDFJ'
response = http.request(request)
if response.code == "200"
@feed.destroy
break
end
end
redirect_to panel_feed_back_end_feeds_url
end
def permission_request(feed)
require 'net/http'
require 'uri'
require 'json'
uri_path = "/panel/feed/back_end/feed_requests/transferred_request"
# http = Net::HTTP.new(feed.link, 80)
http = Net::HTTP.new(feed.link, 80)
request = Net::HTTP::Post.new(uri_path)
categories = feed.categories.join(' ') unless feed.categories.blank?
body = {
'link' => feed.link,
'name' => feed.name,
'from_host' => env["HTTP_ORIGIN"].sub("http://",""),
'categories' => categories
}
status = false
3.times do
request.set_form_data(body)
request.add_field 'X-Auth-Token', '3kjlfksjDFJ'
response = http.request(request)
if response.code == "200"
status = true
break
end
end
return status
end
private
def skip_current_site
@filtered_sites = SITES.collect {|site| site unless site[1] == request.host}
@filtered_sites.compact!
end
end