diff --git a/app/controllers/admin/event_news_controller.rb b/app/controllers/admin/event_news_controller.rb index 14f260f..37cb7c6 100644 --- a/app/controllers/admin/event_news_controller.rb +++ b/app/controllers/admin/event_news_controller.rb @@ -307,6 +307,9 @@ class Admin::EventNewsController < OrbitAdminController event_news.save build_email(event_news,I18n.locale) + Thread.new do + event_news.notify_feed("create") + end redirect_to params['referer_url'] end @@ -393,6 +396,9 @@ class Admin::EventNewsController < OrbitAdminController event_news.save end build_email(event_news,I18n.locale) + Thread.new do + event_news.notify_feed("update") + end now_event_news_page = EventNews.where(:title.ne => "",:is_preview.in=>[false,nil]) .order_by(sort).map(&:id).map.with_index.select{|v,i| v==event_news.id}[0][1] rescue nil now_event_news_page = now_event_news_page.nil? ? 0 : ((now_event_news_page+1).to_f/10).ceil @@ -401,12 +407,18 @@ class Admin::EventNewsController < OrbitAdminController def destroy @event_news.destroy + Thread.new do + @event_news.notify_feed("destroy") + end redirect_to "/admin/event_news" end def delete if params[:ids] EventNews.any_in(:uid => params[:ids]).destroy_all + Thread.new do + EventNews.notify_feed_delete(params[:ids]) + end end redirect_to "/admin/event_news" end diff --git a/app/controllers/event_news_feeds_controller.rb b/app/controllers/event_news_feeds_controller.rb index 66274ca..d3cb8e8 100644 --- a/app/controllers/event_news_feeds_controller.rb +++ b/app/controllers/event_news_feeds_controller.rb @@ -1,6 +1,27 @@ require "rss" class EventNewsFeedsController < ApplicationController include Admin::EventNewsHelper + def feed_add_remote + if params[:url].present? + uid = params[:uid].to_s + event_news_feed = EventNewsFeed.where(uid: uid).first + if !(event_news_feed.remote_urls.include?(params[:url])) + event_news_feed.remote_urls << params[:url] + event_news_feed.save + end + end + render :json => {success: true} + end + def feed_remove_remote + if params[:url].present? + uid = params[:uid].to_s + event_news_feed = EventNewsFeed.where(uid: uid).first + if event_news_feed.remote_urls.delete(params[:url]) + event_news_feed.save + end + end + render :json => {success: true} + end def feed uid = params[:uid].to_s startdt = params[:start].blank? ? nil : params[:start] diff --git a/app/models/event_news.rb b/app/models/event_news.rb index 1bf1ca9..661abe8 100644 --- a/app/models/event_news.rb +++ b/app/models/event_news.rb @@ -495,4 +495,126 @@ class EventNews end return a end + def get_related_feeds + @category_id ||= self.category_id + @tag_ids ||= self.tag_ids + related_feeds = EventNewsFeed.any_of({:category_ids=>@category_id.to_s}, {:tag_ids.in=>@tag_ids.map(&:to_s)}).to_a + end + def notify_feed(type="create") + if @is_hidden_changed + if self.is_hidden + if type == 'create' + return [] + else + type = 'destroy' + end + else + type = 'create' + end + elsif self.is_hidden + return [] + end + related_feeds = self.get_related_feeds.select{|feed| feed.remote_urls.count != 0} + if related_feeds.count != 0 + bulletin_data = self.get_data + if type == "destroy" + tmp_data = {'type'=>'destroy', 'data'=>[self.uid]} + else + tmp_data = {'type'=>type, 'data'=>[bulletin_data.to_json]} + end + request = Net::HTTP::Post.new('/xhr/feeds/notify_change', 'Content-Type' => 'application/json') + related_feeds.each do |feed| + tmp_data['uid'] = feed.uid + request.body = tmp_data.to_json + feed.remote_urls.each do |remote_url| + uri = URI(remote_url) + http_req = Net::HTTP.new(uri.host, uri.port) + if remote_url.include?('https') + http_req.use_ssl = true + end + response = self.class.http_request( http_req , request ) + end + end + end + end + def self.notify_all_feed(force_update=false) + related_feeds = EventNewsFeed.where(:remote_urls.nin=>[nil, []]).to_a + related_feeds.each do |feed| + uid = feed.uid + startdt = nil + enddt = nil + dt = nil + feed_cache = EventNewsFeedCache.where(uid: uid, start: startdt, end: enddt, date: dt) + if force_update + feed_cache = nil + else + feed_cache_old = feed_cache.all_of([{:invalid_date.ne=>nil},{:invalid_date.lte => Time.now}]).last + feed_cache.all_of([{:invalid_date.ne=>nil},{:invalid_date.lte => Time.now}]).destroy + count = feed_cache.count + if count > 1 + feed_cache.limit(count-1).destroy + end + feed_cache = feed_cache.first + anns = '' + end + if feed_cache.nil? + anns = feed.generate_one_cache_timeout(startdt: startdt,enddt: enddt,dt: dt,timeout: 20) + anns = (feed_cache_old.content rescue "") if anns.nil? + else + anns = feed_cache.content + end + request = Net::HTTP::Post.new('/xhr/feeds/notify_change', 'Content-Type' => 'application/json') + tmp_data = {'type'=>'update_all', 'uid'=> uid, 'data'=>anns} + request.body = tmp_data.to_json + feed.remote_urls.each do |remote_url| + uri = URI(remote_url) + http_req = Net::HTTP.new(uri.host, uri.port) + if remote_url.include?('https') + http_req.use_ssl = true + end + response = self.http_request( http_req , request ) + end + end + end + def self.notify_feed_delete(ids) + all_feeds = EventNewsFeed.all.select{|feed| feed.remote_urls.count != 0} + if all_feeds.count != 0 + tmp_data = {'type'=>'destroy'} + request = Net::HTTP::Post.new('/xhr/feeds/notify_change', 'Content-Type' => 'application/json') + all_feeds.each do |feed| + feed_uid = feed.uid + feed_cache = EventNewsFeedCache.where(:uid=>feed_uid).first + if feed_cache + tmp_data['uid'] = feed_uid + tmp_data['data'] = ids & JSON.parse(feed_cache.content)["announcements"].map{|a| a["id"]} + request.body = tmp_data.to_json + if tmp_data['data'].count != 0 + feed.remote_urls.each do |remote_url| + uri = URI(remote_url) + http_req = Net::HTTP.new(uri.host, uri.port) + if remote_url.include?('https') + http_req.use_ssl = true + end + response = self.http_request( http_req , request ) + end + end + end + end + end + end + def self.http_request(http, request) + response = http.request(request) + if response.code.to_i == 301 || response.code.to_i == 302 + location = response["location"] + new_uri = URI(location) + http = Net::HTTP.new(new_uri.host, new_uri.port) + if location.include?('https') + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_PEER + end + request.instance_variable_set(:@path, new_uri.path) + response = self.http_request(http, request) + end + response + end end diff --git a/app/models/event_news_feed.rb b/app/models/event_news_feed.rb index 9a7df3f..d1d2e7d 100644 --- a/app/models/event_news_feed.rb +++ b/app/models/event_news_feed.rb @@ -6,6 +6,7 @@ class EventNewsFeed field :title, as: :slug_title, type: String, localize: true field :tag_ids, type: Array, default: [] field :category_ids, type: Array, default: [] + field :remote_urls, type: Array, default: [] before_save do EventNewsFeedCache.where(uid: self.uid).each do |cache| cache.regenerate diff --git a/config/routes.rb b/config/routes.rb index 499f0e3..de9478d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -43,6 +43,8 @@ Rails.application.routes.draw do get ':slug_title-:uid', to: 'event_news_mods#show', as: :display end end + post "/xhr/event_news_mods/feed_add_remote/:uid" => "event_news_feeds#feed_add_remote" + post "/xhr/event_news_mods/feed_remove_remote/:uid" => "event_news_feeds#feed_remove_remote" get '/xhr/event_news/agenda' => 'event_news_mods#agenda' get "/xhr/event_news/feed/:uid" => "event_news_feeds#feed" get "/xhr/event_news/rssfeed/:uid" => "event_news_feeds#rssfeed"