diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index e9fb486..8ac7404 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -236,6 +236,7 @@ class Admin::AnnouncementsController < OrbitAdminController bulletin.save build_email(bulletin,I18n.locale) + create_feed_cache(bulletin) redirect_to params['referer_url'] end @@ -253,6 +254,7 @@ class Admin::AnnouncementsController < OrbitAdminController send_rejection_email(bulletin,I18n.locale) end bulletin.save + create_feed_cache(bulletin) redirect_to admin_announcements_path end @@ -322,6 +324,7 @@ class Admin::AnnouncementsController < OrbitAdminController bulletin.save end build_email(bulletin,I18n.locale) + create_feed_cache(bulletin) now_bulletin_page = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]) .order_by(sort).map(&:id).map.with_index.select{|v,i| v==bulletin.id}[0][1] rescue nil now_bulletin_page = now_bulletin_page.nil? ? 0 : ((now_bulletin_page+1).to_f/10).ceil @@ -404,6 +407,7 @@ class Admin::AnnouncementsController < OrbitAdminController bulletin.is_preview = true bulletin.save + create_feed_cache(bulletin) render :text=>page_for_bulletin(bulletin) + "?preview=true" end @@ -476,4 +480,17 @@ class Admin::AnnouncementsController < OrbitAdminController def settings_params params.require(:announcement_setting).permit! end + + def create_feed_cache(bulletin) + BulletinFeed.where(:tag_ids.in => Array(bulletin.tag_ids).collect{|v| v.to_s}).each do |bulletin_feed| + uid = bulletin_feed.uid + uri = URI(request.protocol + request.host_with_port + "/xhr/announcements/feed/#{uid}.json") + Thread.new do + res_net = Net::HTTP.start(uri.host, uri.port,:use_ssl => uri.scheme == 'https',open_timeout: 60,read_timeout: 60) do |http| + req = Net::HTTP::Get.new(uri) + http.request(req) + end + end + end + end end diff --git a/app/controllers/announcement_feeds_controller.rb b/app/controllers/announcement_feeds_controller.rb index edfc61c..54fde71 100644 --- a/app/controllers/announcement_feeds_controller.rb +++ b/app/controllers/announcement_feeds_controller.rb @@ -3,8 +3,21 @@ class AnnouncementFeedsController < ApplicationController include Admin::AnnouncementsHelper def feed uid = params[:uid] - anns = get_announcements(uid) - render :json => anns.to_json + feed_cache = BulletinFeedCache.where(uid: uid).first + anns = '' + if feed_cache.nil? + BulletinFeedCache.create(uid: uid,content: '') + Thread.new do + anns = get_announcements(uid).to_json + feed_cache = BulletinFeedCache.where(uid: uid).first + if !feed_cache.nil? + feed_cache.update_attributes(content: anns) + end + end + else + anns = feed_cache.content + end + render :json => anns end def rssfeed @@ -46,21 +59,10 @@ class AnnouncementFeedsController < ApplicationController def smart_convertor(text) html_string = text - links = html_string.scan(/img.*?src="(.*?)"/i) - links = links + html_string.scan(/a.*?href="(.*?)"/i) - links.uniq! - links.each do |link| - l = link.first - new_link = nil - if l.starts_with?("/") - new_link = request.protocol + request.host_with_port + l - elsif l.starts_with?("..") - l1 = l.gsub("../","") - new_link = request.protocol + request.host_with_port + "/" + l1 - end - html_string = html_string.gsub(l,new_link) if !new_link.nil? - end - return html_string + url = request.protocol + request.host_with_port + html_string = html_string.gsub(/img.*?src="(?=\/)(.*?)|a.*?href="(?=\/)(.*?)/i){|w| w+url} + html_string = html_string.gsub(/img.*?src="\.\.(?=\/)(.*?)|a.*?href="\.\.(?=\/)(.*?)/i){|w| w[0...-2]+url} + return html_string end def get_announcements(uid) diff --git a/app/models/bulletin_feed_cache.rb b/app/models/bulletin_feed_cache.rb new file mode 100644 index 0000000..09f7e57 --- /dev/null +++ b/app/models/bulletin_feed_cache.rb @@ -0,0 +1,8 @@ +class BulletinFeedCache + include Mongoid::Document + include Mongoid::Timestamps + + field :content, type: String, default: '' + field :uid + +end \ No newline at end of file diff --git a/lib/bulletin_model/cache.rb b/lib/bulletin_model/cache.rb index a8bb061..cbb38d7 100644 --- a/lib/bulletin_model/cache.rb +++ b/lib/bulletin_model/cache.rb @@ -9,6 +9,9 @@ module BulletinModel if self.class == SubPart AnnsCache.where(parent_id:self.id).destroy elsif self.class == Bulletin || (self.class == Page && self.module == "announcement") + if self.class == Bulletin + BulletinFeedCache.where(:tag_ids.in => Array(self.tag_ids).collect{|v| v.to_s}).destroy + end AnnsCache.all.destroy end end