announcement-test/app/controllers/announcement_feeds_controll...

180 lines
5.6 KiB
Ruby
Raw Normal View History

2015-09-11 12:28:41 +00:00
require "rss"
2015-07-08 10:11:25 +00:00
class AnnouncementFeedsController < ApplicationController
2015-09-11 12:28:41 +00:00
include Admin::AnnouncementsHelper
2015-07-08 10:11:25 +00:00
def feed
uid = params[:uid]
2020-07-20 15:48:24 +00:00
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
2015-09-11 12:28:41 +00:00
end
def rssfeed
uid = params[:uid]
@bf = BulletinFeed.find_by(:uid => uid) rescue nil
if !@bf.nil?
tags = @bf.tag_ids
if !tags.empty?
@announcements = Bulletin.can_display_and_sorted.is_approved.filter_by_tags(tags)
2015-09-11 12:28:41 +00:00
end
categories = @bf.category_ids
if !categories.empty?
@announcements = @announcements.filter_by_categories(categories)
end
2015-09-11 12:28:41 +00:00
end
respond_to do |format|
format.html {redirect_to "/xhr/announcements/rssfeed/#{@bf.uid}.rss"}
2020-07-22 03:11:58 +00:00
format.rss
end
2015-09-11 12:28:41 +00:00
end
def feeds
feeds = []
BulletinFeed.all.each do |bf|
feed = {}
feed["title_translations"] = bf.title_translations
feed["uid"] = bf.uid
feed["url"] = "#{request.base_url}/xhr/announcements/feed/#{bf.uid}"
feed["xml_url"] = "#{request.base_url}/xhr/announcements/rssfeed/#{bf.uid}.rss"
feed["tags"] = []
bf.tag_ids.each do |t|
tag = Tag.find(t)
d = {}
d["name_translations"] = tag.name_translations
feed["tags"] << d
end
feeds << feed
end
render :json => {"feeds" => feeds}.to_json
end
private
2016-03-07 09:11:07 +00:00
def smart_convertor(text)
html_string = text
2020-07-20 15:48:24 +00:00
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
2016-03-07 09:11:07 +00:00
end
2015-09-11 12:28:41 +00:00
def get_announcements(uid)
2015-07-08 10:11:25 +00:00
bf = BulletinFeed.find_by(:uid => uid) rescue nil
startdt = params[:start]
enddt = params[:end]
dt = params[:date]
2015-07-08 10:11:25 +00:00
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
2015-11-09 18:30:44 +00:00
else
announcements = []
2015-07-08 10:11:25 +00:00
end
end
all_anns = []
tag_names = []
category_titles = []
2015-07-08 10:11:25 +00:00
tag_ids = []
category_ids = []
2015-07-08 10:11:25 +00:00
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
2016-03-07 10:19:56 +00:00
a["text_translations"] = {}
2016-03-07 10:11:58 +00:00
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?
2015-07-08 10:11:25 +00:00
a["postdate"] = anns.postdate
a["image_description_translations"] = anns.image_description_translations
a["image"] = {}
2020-07-23 13:01:31 +00:00
a["display_img"] = anns.display_img
2015-07-08 10:11:25 +00:00
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"] = {}
2015-07-08 10:11:25 +00:00
a["author"] = author
a["params"] = anns.to_param
2020-07-23 13:01:31 +00:00
a["subtitle_ann"] = anns.subtitle if anns.display_subtitle?
2015-07-08 10:11:25 +00:00
a["bulletin_links"] = []
a["bulletin_files"] = []
2021-04-11 03:29:54 +00:00
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 }}
2015-07-08 10:11:25 +00:00
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}
2015-07-08 10:11:25 +00:00
anns.bulletin_links.each do |bl|
b = {}
b["url"] = bl.url
2016-05-12 08:52:05 +00:00
b["title_translations"] = bl.title_translations
2015-07-08 10:11:25 +00:00
a["bulletin_links"] << b
end
anns.bulletin_files.each do |bf|
b = {}
b["description_translations"] = bf.description_translations
b["title_translations"] = bf.title_translations
2016-04-07 08:20:48 +00:00
b["url"] = ("#{request.base_url}" + bf.file.url rescue "")
2015-07-08 10:11:25 +00:00
a["bulletin_files"] << b
end
all_anns << a
end
2015-09-11 12:28:41 +00:00
{
"announcements" => all_anns,
"tags" => tag_names
}
2015-07-08 10:11:25 +00:00
end
end