fix feed error
This commit is contained in:
1 parent
c5f95c9968
commit
234ed6cfea
10 files changed
+232
-172
No files matched your search
@@ -159,7 +159,7 @@ class Admin::AnnouncementsController < OrbitAdminController
|
||||
|
||||
def feedform
|
||||
if params[:type] == "new"
|
||||
@announcement_feed = BulletinFeed.new
|
||||
@announcement_feed = BulletinFeed.new(id: nil)
|
||||
render :partial => "feed_form"
|
||||
else params[:type] == "edit"
|
||||
@announcement_feed = BulletinFeed.find(params[:id])
|
||||
@@ -170,7 +170,7 @@ class Admin::AnnouncementsController < OrbitAdminController
|
||||
def createfeed
|
||||
announcement_feed = BulletinFeed.new(feed_params)
|
||||
announcement_feed.save
|
||||
create_feed_cache(nil,announcement_feed)
|
||||
#BulletinFeed.create_feed_cache(nil,announcement_feed)
|
||||
feeds = BulletinFeed.all.asc(:created_at)
|
||||
render :partial => "feed", :collection => feeds
|
||||
end
|
||||
@@ -179,7 +179,7 @@ class Admin::AnnouncementsController < OrbitAdminController
|
||||
ann_feed = BulletinFeed.find(params[:id])
|
||||
ann_feed.update_attributes(feed_params)
|
||||
ann_feed.save
|
||||
create_feed_cache(nil,ann_feed)
|
||||
#BulletinFeed.create_feed_cache(nil,ann_feed)
|
||||
feeds = BulletinFeed.all.asc(:created_at)
|
||||
render :partial => "feed", :collection => feeds
|
||||
end
|
||||
@@ -243,7 +243,7 @@ class Admin::AnnouncementsController < OrbitAdminController
|
||||
|
||||
bulletin.save
|
||||
build_email(bulletin,I18n.locale)
|
||||
create_feed_cache(bulletin)
|
||||
#BulletinFeed.create_feed_cache(bulletin)
|
||||
redirect_to admin_announcements_path
|
||||
end
|
||||
|
||||
@@ -261,7 +261,7 @@ class Admin::AnnouncementsController < OrbitAdminController
|
||||
send_rejection_email(bulletin,I18n.locale)
|
||||
end
|
||||
bulletin.save
|
||||
create_feed_cache(bulletin)
|
||||
#BulletinFeed.create_feed_cache(bulletin)
|
||||
redirect_to admin_announcements_path
|
||||
end
|
||||
|
||||
@@ -320,7 +320,7 @@ class Admin::AnnouncementsController < OrbitAdminController
|
||||
bulletin.save
|
||||
end
|
||||
build_email(bulletin,I18n.locale)
|
||||
create_feed_cache(bulletin)
|
||||
#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
|
||||
now_bulletin_page = now_bulletin_page.nil? ? 0 : ((now_bulletin_page+1).to_f/10).ceil
|
||||
@@ -403,7 +403,7 @@ class Admin::AnnouncementsController < OrbitAdminController
|
||||
|
||||
bulletin.is_preview = true
|
||||
bulletin.save
|
||||
create_feed_cache(bulletin)
|
||||
#BulletinFeed.create_feed_cache(bulletin)
|
||||
render :text=>page_for_bulletin(bulletin) + "?preview=true"
|
||||
end
|
||||
|
||||
@@ -508,27 +508,4 @@ class Admin::AnnouncementsController < OrbitAdminController
|
||||
end
|
||||
bps
|
||||
end
|
||||
def create_feed_cache(bulletin=nil,bulletin_feed=nil)
|
||||
if !bulletin.nil?
|
||||
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
|
||||
elsif !bulletin_feed.nil?
|
||||
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
|
||||
@@ -3,17 +3,19 @@ class AnnouncementFeedsController < ApplicationController
|
||||
include Admin::AnnouncementsHelper
|
||||
def feed
|
||||
uid = params[:uid]
|
||||
feed_cache = BulletinFeedCache.where(uid: uid).first
|
||||
startdt = params[:start].blank? ? nil : params[:start]
|
||||
enddt = params[:end].blank? ? nil : params[:end]
|
||||
dt = params[:date].blank? ? nil : params[:date]
|
||||
feed_cache = BulletinFeedCache.where(uid: uid, start: startdt, end: enddt, date: dt)
|
||||
feed_cache.where(: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 = ''
|
||||
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
|
||||
anns = BulletinFeed.where(uid: uid).first.generate_one_cache_timeout(startdt: startdt,enddt: enddt,dt: dt,base_url: request.base_url,timeout: 20)
|
||||
else
|
||||
anns = feed_cache.content
|
||||
end
|
||||
@@ -61,118 +63,7 @@ class AnnouncementFeedsController < ApplicationController
|
||||
render :json => {"feeds" => feeds}.to_json
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def smart_convertor(text)
|
||||
html_string = text
|
||||
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)
|
||||
bf = BulletinFeed.find_by(:uid => uid) rescue nil
|
||||
startdt = params[:start]
|
||||
enddt = params[:end]
|
||||
dt = params[:date]
|
||||
if !bf.nil?
|
||||
tags = bf.tag_ids
|
||||
categories = bf.category_ids
|
||||
if !(categories.empty? && tags.empty?)
|
||||
if !dt.nil?
|
||||
dt = DateTime.parse(dt)
|
||||
dtt = dt + 1.day
|
||||
announcements = Bulletin.where(:postdate.gt => dt, :postdate.lt => dtt).can_display_and_sorted.is_approved
|
||||
elsif !startdt.nil? && enddt.nil?
|
||||
startdt = DateTime.parse(startdt)
|
||||
enddt = DateTime.now
|
||||
announcements = Bulletin.where(:postdate.gt => startdt, :postdate.lt => enddt).can_display_and_sorted.is_approved
|
||||
elsif !startdt.nil? && !enddt.nil?
|
||||
startdt = DateTime.parse(startdt)
|
||||
enddt = DateTime.parse(enddt) + 1.day
|
||||
announcements = Bulletin.where(:postdate.gt => startdt, :postdate.lt => enddt).can_display_and_sorted.is_approved
|
||||
else
|
||||
announcements = Bulletin.all.can_display_and_sorted.is_approved
|
||||
end
|
||||
if !tags.empty?
|
||||
announcements = announcements.filter_by_tags(tags)
|
||||
end
|
||||
if !categories.empty?
|
||||
announcements = announcements.filter_by_categories(categories)
|
||||
end
|
||||
else
|
||||
announcements = []
|
||||
end
|
||||
end
|
||||
all_anns = []
|
||||
tag_names = []
|
||||
category_titles = []
|
||||
tag_ids = []
|
||||
category_ids = []
|
||||
announcements.each do |anns|
|
||||
user = User.find(anns.create_user_id) rescue nil
|
||||
if !user.nil?
|
||||
author = user.member_profile && user.member_profile.name == "" ? user.user_name : user.member_profile.name
|
||||
else
|
||||
author = ""
|
||||
end
|
||||
a = {}
|
||||
a["id"] = anns.uid
|
||||
a["title_translations"] = anns.title_translations
|
||||
a["subtitle_translations"] = anns.subtitle_translations
|
||||
a["text_translations"] = {}
|
||||
a["text_translations"]["en"] = smart_convertor(anns.text_translations["en"]) if !anns.text_translations["en"].blank?
|
||||
a["text_translations"]["zh_tw"] = smart_convertor(anns.text_translations["zh_tw"]) if !anns.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"] = ("#{request.base_url}" + anns.image.url rescue "")
|
||||
a["image"]["thumb"] = ("#{request.base_url}" + anns.image.thumb.url rescue "")
|
||||
a["image"]["mobile"] = ("#{request.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"=>"#{request.base_url}" + image.file.url,"description"=>image.description.to_s,"description_text"=>image.description_text }}
|
||||
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"] = ("#{request.base_url}" + bf.file.url rescue "")
|
||||
a["bulletin_files"] << b
|
||||
end
|
||||
all_anns << a
|
||||
end
|
||||
{
|
||||
"announcements" => all_anns,
|
||||
"tags" => tag_names,
|
||||
"categories" => category_titles
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -451,13 +451,14 @@ class AnnouncementsController < ApplicationController
|
||||
set_image_version_for_widget()
|
||||
devide_flag = (!(defined? SiteFeed).nil?)
|
||||
anns_cache.where(:invalid_date.lte => Time.now).destroy
|
||||
if anns_cache.count > 1
|
||||
anns_cache.skip(1).destroy
|
||||
count = anns_cache.count
|
||||
if count > 1
|
||||
anns_cache.limit(count-1).destroy
|
||||
end
|
||||
if anns_cache.count == 0 || is_random
|
||||
Bulletin.remove_expired_status
|
||||
uid = OrbitHelper.params[:uid] rescue ""
|
||||
anns_for_locale = Bulletin.where(:title.nin => ["",nil], :uid.ne => uid).is_approved.filter_cats_and_tags(cats,tags)
|
||||
anns_for_locale = Bulletin.where(:title.nin => ["",nil], :uid.ne => uid).is_approved_and_show.filter_cats_and_tags(cats,tags)
|
||||
sorted_anns = anns_for_locale.can_display_and_sorted
|
||||
if !is_random
|
||||
sorted_anns = sorted_anns.limit(widget_data_count)
|
||||
|
||||
Reference in new issue
Block a user