From 842eac72f0a808da376330a373541c6bd96fd3d9 Mon Sep 17 00:00:00 2001 From: bohung Date: Sun, 22 May 2022 18:44:30 +0800 Subject: [PATCH] Fix bug. Move some feed feature to model. --- .../admin/announcements_controller.rb | 104 +----------------- app/models/bulletin.rb | 67 +++++++++++ .../announcement_feeds/rssfeed.rss.builder | 2 +- lib/announcement/engine.rb | 7 ++ lib/bulletin_model/cache.rb | 6 +- 5 files changed, 83 insertions(+), 103 deletions(-) diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 97f16bd..7b2eda2 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -53,21 +53,6 @@ class Admin::AnnouncementsController < OrbitAdminController @table_feed_fields = ["announcement.time", "announcement.comment", "announcement.account","ip","is_hidden"] @comments = @bulletin.bulletin_comments.reverse rescue [] end - def 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_NONE - end - request.instance_variable_set(:@path, new_uri.path) - response = http_request(http, request) - end - response - end def index Bulletin.remove_expired_status @tags = @module_app.tags @@ -329,29 +314,7 @@ class Admin::AnnouncementsController < OrbitAdminController bulletin.save build_email(bulletin,I18n.locale) Thread.new do - begin - related_feeds = bulletin.get_related_feeds.select{|feed| feed.remote_urls.count != 0} - if related_feeds.count != 0 - bulletin_data = bulletin.get_data - tmp_data = {'type'=>'create', 'data'=>[bulletin_data.to_json]} - 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 - http_req.verify_mode = OpenSSL::SSL::VERIFY_NONE - end - response = http_request( http_req , request ) - end - end - end - rescue => e - puts [e.to_s, e.backtrace] - end + bulletin.notify_feed("create") end #BulletinFeed.create_feed_cache(bulletin) redirect_to admin_announcements_path @@ -439,25 +402,7 @@ class Admin::AnnouncementsController < OrbitAdminController end build_email(bulletin,I18n.locale) Thread.new do - related_feeds = bulletin.get_related_feeds.select{|feed| feed.remote_urls.count != 0} - if related_feeds.count != 0 - bulletin_data = bulletin.get_data - tmp_data = {'type'=>'update', 'data'=>[bulletin_data.to_json]} - 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 - http_req.verify_mode = OpenSSL::SSL::VERIFY_NONE - end - response = http_request( http_req , request ) - end - end - end + bulletin.notify_feed("update") end #BulletinFeed.create_feed_cache(bulletin) now_bulletin_page = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]) @@ -467,26 +412,9 @@ class Admin::AnnouncementsController < OrbitAdminController end def destroy - related_feeds = @bulletin.get_related_feeds.select{|feed| feed.remote_urls.count != 0} @bulletin.destroy Thread.new do - if related_feeds.count != 0 - tmp_data = {'type'=>'destroy', 'data'=>[@bulletin.uid]} - 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 - http_req.verify_mode = OpenSSL::SSL::VERIFY_NONE - end - response = http_request( http_req , request ) - end - end - end + @bulletin.notify_feed("destroy") end redirect_to admin_announcements_path end @@ -495,31 +423,7 @@ class Admin::AnnouncementsController < OrbitAdminController if params[:ids] Bulletin.any_in(:uid => params[:ids]).destroy_all Thread.new do - all_feeds = BulletinFeed.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 = BulletinFeedCache.where(:uid=>feed_uid).first - if feed_cache - tmp_data['uid'] = feed_uid - tmp_data['data'] = params[: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 - http_req.verify_mode = OpenSSL::SSL::VERIFY_NONE - end - response = http_request( http_req , request ) - end - end - end - end - end + Bulletin.notify_feed_delete(params[:ids]) end end redirect_to admin_announcements_path diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index e1c5b08..0f6b13d 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -407,12 +407,14 @@ class Bulletin self.tags.each do |tag| if !tag_ids.include?(tag.id.to_s) tag_ids << tag.id.to_s + tag_names << {"name_translations" => tag.name_translations} end a["tags"] << {"name_translations" => tag.name_translations} end cat = self.category if (!category_ids.include?(cat.id.to_s) rescue false) category_ids << cat.id.to_s + category_titles << {"title_translations" => cat.title_translations} end a["category"] = {"title_translations" => (cat.title_translations rescue {})} self.bulletin_links.each do |bl| @@ -433,4 +435,69 @@ class Bulletin def get_related_feeds related_feeds = BulletinFeed.any_of({:category_ids=>self.category_id.to_s}, {:tag_ids.in=>self.tag_ids.map(&:to_s)}).to_a end + def notify_feed(type="create") + 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_feed_delete(ids) + all_feeds = BulletinFeed.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 = BulletinFeedCache.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_NONE + end + request.instance_variable_set(:@path, new_uri.path) + response = self.http_request(http, request) + end + response + end end diff --git a/app/views/announcement_feeds/rssfeed.rss.builder b/app/views/announcement_feeds/rssfeed.rss.builder index a4c7219..8321c53 100644 --- a/app/views/announcement_feeds/rssfeed.rss.builder +++ b/app/views/announcement_feeds/rssfeed.rss.builder @@ -2,7 +2,7 @@ xml.instruct! :xml, :version => "1.0" xml.rss :version => "2.0" do xml.channel do xml.title @bf.title - xml.link "/xhr/announcements/rssfeed/73999228.rss" + xml.link "/xhr/announcements/rssfeed/#{@bf.uid}.rss" for announcement in @announcements xml.item do diff --git a/lib/announcement/engine.rb b/lib/announcement/engine.rb index a3f7b16..65c574d 100644 --- a/lib/announcement/engine.rb +++ b/lib/announcement/engine.rb @@ -98,6 +98,13 @@ module Announcement rescue => e puts ['there_was_no_show_option_method',e] end + begin + avoid_page_cache AnnsCache + avoid_page_cache BulletinFeedCache + avoid_page_cache BulletinFeed + rescue => e + puts ["avoid_page_cache", e.to_s] + end side_bar do head_label_i18n 'announcement.announcement', icon_class: "icons-megaphone" available_for "users" diff --git a/lib/bulletin_model/cache.rb b/lib/bulletin_model/cache.rb index 355e086..569461a 100644 --- a/lib/bulletin_model/cache.rb +++ b/lib/bulletin_model/cache.rb @@ -11,15 +11,17 @@ module BulletinModel if self.class == Bulletin @tag_ids = self.tag_ids @org_tag_ids = self.org_tag_ids + @category_id = self.category_id + @org_category_id = self.org_category_id end end def do_before_save if self.class == SubPart - AnnsCache.where(parent_id: self.id).delete + AnnsCache.where(parent_id: /^#{self.id}/).delete elsif self.class == Bulletin || (self.class == Page && self.module == "announcement") if self.class == Bulletin tmp_tag_ids = (Array(@tag_ids) + Array(@org_tag_ids)).uniq - tmp_cat_ids = (Array(self.category_id) + Array(self.org_category_id)).uniq + tmp_cat_ids = (Array(@category_id) + Array(@org_category_id)).uniq Thread.new do BulletinFeedCache.where(:uid.in => BulletinFeed.any_of([{:tag_ids.in => tmp_tag_ids.collect{|v| v.to_s}},{:category_ids.in => tmp_cat_ids.collect{|v| v.to_s}}]).pluck(:uid)).each do |cache| cache.regenerate