Move some feed feature to model.
This commit is contained in:
BoHung Chiu 2022-05-22 18:44:30 +08:00
parent dc737e3bd2
commit 842eac72f0
5 changed files with 83 additions and 103 deletions

View File

@ -53,21 +53,6 @@ class Admin::AnnouncementsController < OrbitAdminController
@table_feed_fields = ["announcement.time", "announcement.comment", "announcement.account","ip","is_hidden"] @table_feed_fields = ["announcement.time", "announcement.comment", "announcement.account","ip","is_hidden"]
@comments = @bulletin.bulletin_comments.reverse rescue [] @comments = @bulletin.bulletin_comments.reverse rescue []
end 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 def index
Bulletin.remove_expired_status Bulletin.remove_expired_status
@tags = @module_app.tags @tags = @module_app.tags
@ -329,29 +314,7 @@ class Admin::AnnouncementsController < OrbitAdminController
bulletin.save bulletin.save
build_email(bulletin,I18n.locale) build_email(bulletin,I18n.locale)
Thread.new do Thread.new do
begin bulletin.notify_feed("create")
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
end end
#BulletinFeed.create_feed_cache(bulletin) #BulletinFeed.create_feed_cache(bulletin)
redirect_to admin_announcements_path redirect_to admin_announcements_path
@ -439,25 +402,7 @@ class Admin::AnnouncementsController < OrbitAdminController
end end
build_email(bulletin,I18n.locale) build_email(bulletin,I18n.locale)
Thread.new do Thread.new do
related_feeds = bulletin.get_related_feeds.select{|feed| feed.remote_urls.count != 0} bulletin.notify_feed("update")
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
end end
#BulletinFeed.create_feed_cache(bulletin) #BulletinFeed.create_feed_cache(bulletin)
now_bulletin_page = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil]) now_bulletin_page = Bulletin.where(:title.ne => "",:is_preview.in=>[false,nil])
@ -467,26 +412,9 @@ class Admin::AnnouncementsController < OrbitAdminController
end end
def destroy def destroy
related_feeds = @bulletin.get_related_feeds.select{|feed| feed.remote_urls.count != 0}
@bulletin.destroy @bulletin.destroy
Thread.new do Thread.new do
if related_feeds.count != 0 @bulletin.notify_feed("destroy")
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
end end
redirect_to admin_announcements_path redirect_to admin_announcements_path
end end
@ -495,31 +423,7 @@ class Admin::AnnouncementsController < OrbitAdminController
if params[:ids] if params[:ids]
Bulletin.any_in(:uid => params[:ids]).destroy_all Bulletin.any_in(:uid => params[:ids]).destroy_all
Thread.new do Thread.new do
all_feeds = BulletinFeed.all.select{|feed| feed.remote_urls.count != 0} Bulletin.notify_feed_delete(params[:ids])
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
end end
end end
redirect_to admin_announcements_path redirect_to admin_announcements_path

View File

@ -407,12 +407,14 @@ class Bulletin
self.tags.each do |tag| self.tags.each do |tag|
if !tag_ids.include?(tag.id.to_s) if !tag_ids.include?(tag.id.to_s)
tag_ids << tag.id.to_s tag_ids << tag.id.to_s
tag_names << {"name_translations" => tag.name_translations}
end end
a["tags"] << {"name_translations" => tag.name_translations} a["tags"] << {"name_translations" => tag.name_translations}
end end
cat = self.category cat = self.category
if (!category_ids.include?(cat.id.to_s) rescue false) if (!category_ids.include?(cat.id.to_s) rescue false)
category_ids << cat.id.to_s category_ids << cat.id.to_s
category_titles << {"title_translations" => cat.title_translations}
end end
a["category"] = {"title_translations" => (cat.title_translations rescue {})} a["category"] = {"title_translations" => (cat.title_translations rescue {})}
self.bulletin_links.each do |bl| self.bulletin_links.each do |bl|
@ -433,4 +435,69 @@ class Bulletin
def get_related_feeds 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 related_feeds = BulletinFeed.any_of({:category_ids=>self.category_id.to_s}, {:tag_ids.in=>self.tag_ids.map(&:to_s)}).to_a
end 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 end

View File

@ -2,7 +2,7 @@ xml.instruct! :xml, :version => "1.0"
xml.rss :version => "2.0" do xml.rss :version => "2.0" do
xml.channel do xml.channel do
xml.title @bf.title xml.title @bf.title
xml.link "/xhr/announcements/rssfeed/73999228.rss" xml.link "/xhr/announcements/rssfeed/#{@bf.uid}.rss"
for announcement in @announcements for announcement in @announcements
xml.item do xml.item do

View File

@ -98,6 +98,13 @@ module Announcement
rescue => e rescue => e
puts ['there_was_no_show_option_method',e] puts ['there_was_no_show_option_method',e]
end 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 side_bar do
head_label_i18n 'announcement.announcement', icon_class: "icons-megaphone" head_label_i18n 'announcement.announcement', icon_class: "icons-megaphone"
available_for "users" available_for "users"

View File

@ -11,15 +11,17 @@ module BulletinModel
if self.class == Bulletin if self.class == Bulletin
@tag_ids = self.tag_ids @tag_ids = self.tag_ids
@org_tag_ids = self.org_tag_ids @org_tag_ids = self.org_tag_ids
@category_id = self.category_id
@org_category_id = self.org_category_id
end end
end end
def do_before_save def do_before_save
if self.class == SubPart 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") elsif self.class == Bulletin || (self.class == Page && self.module == "announcement")
if self.class == Bulletin if self.class == Bulletin
tmp_tag_ids = (Array(@tag_ids) + Array(@org_tag_ids)).uniq 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 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| 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 cache.regenerate