Add notify feed edit contents feature.

This commit is contained in:
BoHung Chiu 2022-05-09 20:13:29 +08:00
parent 9c8f032e1c
commit b162819874
5 changed files with 251 additions and 68 deletions

View File

@ -13,7 +13,6 @@ class Admin::AnnouncementsController < OrbitAdminController
super
@app_title = "announcement"
end
def update_sort
ids = params[:ids]
ids.each_with_index do |id,i|
@ -314,6 +313,30 @@ 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
end
response = http_req.request(request)
end
end
end
rescue => e
puts [e.to_s, e.backtrace]
end
end
#BulletinFeed.create_feed_cache(bulletin)
redirect_to admin_announcements_path
end
@ -399,6 +422,26 @@ class Admin::AnnouncementsController < OrbitAdminController
bulletin.save
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
end
response = http_req.request(request)
end
end
end
end
#BulletinFeed.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
@ -408,12 +451,57 @@ class Admin::AnnouncementsController < OrbitAdminController
def destroy
@bulletin.destroy
Thread.new do
related_feeds = @bulletin.get_related_feeds.select{|feed| feed.remote_urls.count != 0}
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
end
response = http_req.request(request)
end
end
end
end
redirect_to admin_announcements_path
end
def delete
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
end
response = http_req.request(request)
end
end
end
end
end
end
end
redirect_to admin_announcements_path
end

View File

@ -1,6 +1,27 @@
require "rss"
class AnnouncementFeedsController < ApplicationController
include Admin::AnnouncementsHelper
def feed_add_remote
if params[:url].present?
uid = params[:uid]
bulletin_feed = BulletinFeed.where(uid: uid).first
if !(bulletin_feed.remote_urls.include?(params[:url]))
bulletin_feed.remote_urls << params[:url]
bulletin_feed.save
end
end
render :json => {success: true}
end
def feed_remove_remote
if params[:url].present?
uid = params[:uid]
bulletin_feed = BulletinFeed.where(uid: uid).first
if bulletin_feed.remote_urls.delete(params[:url])
bulletin_feed.save
end
end
render :json => {success: true}
end
def feed
uid = params[:uid]
startdt = params[:start].blank? ? nil : params[:start]

View File

@ -300,4 +300,139 @@ class Bulletin
def display_postdate
self.postdate.present? ? self.postdate.strftime("%Y/%m/%d") : ""
end
def enable_annc_dept
if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash
OrbitHelper::SharedHash['announcement'][:enable_annc_dept] rescue AnnouncementSetting.first.enable_annc_dept
else
AnnouncementSetting.first.enable_annc_dept rescue false
end
end
def annc_depts_translations
if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash
OrbitHelper::SharedHash['announcement'][:annc_depts_translations] rescue AnnouncementSetting.first.annc_depts_translations
else
AnnouncementSetting.first.annc_depts_translations rescue {}
end
end
def self.smart_convertor(text,url)
doc = Nokogiri.HTML(text)
doc.search('a[href]').each do |link|
if link['href'].nil?
link.delete 'href'
elsif link['href'].start_with?('/')
link['href'] = url + link['href']
elsif link['href'].start_with?('../')
link['href'] = url + link['href'][3..-1]
end
end
doc.search('img[src]').each do |link|
if link['src'].nil?
link.delete 'src'
elsif link['src'].start_with?('/')
link['src'] = url + link['src']
elsif link['src'].start_with?('../')
link['src'] = url + link['src'][3..-1]
end
end
return doc.css('body').inner_html
end
def get_data(annc_depts=nil, tmp_enable_annc_dept=nil, more_url=nil, base_url=nil, cat_ids=nil, tag_ids=nil, locale=nil)
locale = I18n.locale if locale.nil?
if tmp_enable_annc_dept.nil? || annc_depts.nil?
annc_depts = []
tmp_enable_annc_dept = (enable_annc_dept rescue false)
if tmp_enable_annc_dept
annc_depts = annc_depts_translations[locale.to_s] rescue []
end
end
base_url = Site.first.root_url if base_url.nil?
author = ""
if tmp_enable_annc_dept
author = annc_depts[self.annc_dept] rescue ""
else
user = User.find(self.create_user_id) rescue nil
if !user.nil?
author = user.member_name || user.user_name
else
author = ""
end
end
a = {}
if more_url.nil?
if cat_ids.nil?
cat_ids = [self.category_id]
end
if tag_ids.nil?
tag_ids = self.tag_ids
end
basic_query = {:module => 'announcement',:enabled_for=>locale}
if !cat_ids.blank?
query = basic_query.merge({:categories.all => cat_ids})
else
query = basic_query.clone
end
if !tag_ids.blank?
query = query.merge({:tags.all => tag_ids})
end
page = Page.where(query).first || Page.where(basic_query).first
more_url = page ? page.get_url : nil
end
if more_url
a['show_url'] = "#{more_url}/#{self.to_param}"
end
a["id"] = self.uid
a["title_translations"] = self.title_translations
a["subtitle_translations"] = self.subtitle_translations
a["text_translations"] = {}
text_translations = self.text_translations
a["text_translations"]["en"] = self.class.smart_convertor(text_translations["en"],base_url) if !text_translations["en"].blank?
a["text_translations"]["zh_tw"] = self.class.smart_convertor(text_translations["zh_tw"],base_url) if !text_translations["zh_tw"].blank?
a["postdate"] = self.postdate
a["image_description_translations"] = self.image_description_translations
a["image"] = {}
a["display_img"] = self.display_img
a["image"]["original"] = ("#{base_url}" + self.image.url rescue "")
a["image"]["thumb"] = ("#{base_url}" + self.image.thumb.url rescue "")
a["image"]["mobile"] = ("#{base_url}" + self.image.mobile.url rescue "")
a["tags"] = []
a["category"] = {}
a["author"] = author
a["params"] = self.to_param
a["subtitle_ann"] = self.subtitle if self.display_subtitle?
a["bulletin_links"] = []
a["bulletin_files"] = []
a["bulletin_carousel_images"] = self.bulletin_carousel_images.map{|image| {"src"=>"#{base_url}" + image.file.url,"description"=>image.description.to_s,"description_text"=>image.description_text }}
a["external_link"] = self["is_external_link"] ? self.external_link : nil
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|
b = {}
b["url"] = bl.url
b["title_translations"] = bl.title_translations
a["bulletin_links"] << b
end
self.bulletin_files.each do |bf|
b = {}
b["description_translations"] = bf.description_translations
b["title_translations"] = bf.title_translations
b["url"] = ("#{base_url}" + bf.file.url rescue "")
a["bulletin_files"] << b
end
return a
end
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
end

View File

@ -6,6 +6,7 @@ class BulletinFeed
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
BulletinFeedCache.where(uid: self.uid).each do |cache|
cache.regenerate
@ -112,8 +113,6 @@ class BulletinFeed
end
end
all_anns = []
tag_names = []
category_titles = []
tag_ids = []
category_ids = []
@ -121,6 +120,8 @@ class BulletinFeed
first_deadline = nil
cat_ids = announcements.collect{|a| a.category_id.blank? ? nil : a.category_id.to_s}.compact.uniq
tag_ids = announcements.collect{|a| a.tag_ids.collect{|v| v.blank? ? nil : v.to_s}}.flatten.compact.uniq
tag_names = tag_ids.map{|tag_id| Tag.find(tag_id).name_translations rescue nil}.compact
category_titles = cat_ids.map{|cat_id| Category.find(cat_id).title_translations rescue nil}.compact
basic_query = {:module => 'announcement',:enabled_for=>I18n.locale}
if !cat_ids.blank?
query = basic_query.merge({:categories.all => cat_ids})
@ -140,70 +141,7 @@ class BulletinFeed
first_deadline = deadline
end
end
author = ""
if tmp_enable_annc_dept
author = annc_depts[anns.annc_dept] rescue ""
else
user = User.find(anns.create_user_id) rescue nil
if !user.nil?
author = user.member_name || user.user_name
else
author = ""
end
end
a = {}
if more_url
a['show_url'] = "#{more_url}/#{anns.to_param}"
end
a["id"] = anns.uid
a["title_translations"] = anns.title_translations
a["subtitle_translations"] = anns.subtitle_translations
a["text_translations"] = {}
text_translations = anns.text_translations
a["text_translations"]["en"] = self.class.smart_convertor(text_translations["en"],base_url) if !text_translations["en"].blank?
a["text_translations"]["zh_tw"] = self.class.smart_convertor(text_translations["zh_tw"],base_url) if !text_translations["zh_tw"].blank?
a["postdate"] = anns.postdate
a["image_description_translations"] = anns.image_description_translations
a["image"] = {}
a["display_img"] = anns.display_img
a["image"]["original"] = ("#{base_url}" + anns.image.url rescue "")
a["image"]["thumb"] = ("#{base_url}" + anns.image.thumb.url rescue "")
a["image"]["mobile"] = ("#{base_url}" + anns.image.mobile.url rescue "")
a["tags"] = []
a["category"] = {}
a["author"] = author
a["params"] = anns.to_param
a["subtitle_ann"] = anns.subtitle if anns.display_subtitle?
a["bulletin_links"] = []
a["bulletin_files"] = []
a["bulletin_carousel_images"] = anns.bulletin_carousel_images.map{|image| {"src"=>"#{base_url}" + image.file.url,"description"=>image.description.to_s,"description_text"=>image.description_text }}
a["external_link"] = anns["is_external_link"] ? anns.external_link : nil
anns.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 = anns.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 {})}
anns.bulletin_links.each do |bl|
b = {}
b["url"] = bl.url
b["title_translations"] = bl.title_translations
a["bulletin_links"] << b
end
anns.bulletin_files.each do |bf|
b = {}
b["description_translations"] = bf.description_translations
b["title_translations"] = bf.title_translations
b["url"] = ("#{base_url}" + bf.file.url rescue "")
a["bulletin_files"] << b
end
a = anns.get_data(annc_depts, tmp_enable_annc_dept, more_url, base_url, cat_ids, tag_ids)
all_anns << a
end
invalid_date = [first_postdate,first_deadline].compact.sort[0]

View File

@ -49,7 +49,8 @@ Rails.application.routes.draw do
get ':slug_title-:uid', to: 'announcements#show', as: :display
end
end
post "/xhr/announcements/feed_add_remote/:uid" => "announcement_feeds#feed_add_remote"
post "/xhr/announcements/feed_remove_remote/:uid" => "announcement_feeds#feed_remove_remote"
get "/xhr/announcements/feed/:uid" => "announcement_feeds#feed"
get "/xhr/announcements/rssfeed/:uid" => "announcement_feeds#rssfeed"
get "/xhr/announcements/feeds" => "announcement_feeds#feeds"