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

97 lines
2.7 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
2022-05-09 12:13:29 +00:00
def feed_add_remote
if params[:url].present?
2022-10-24 08:38:47 +00:00
uid = params[:uid].to_s
2022-05-09 12:13:29 +00:00
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?
2022-10-24 08:38:47 +00:00
uid = params[:uid].to_s
2022-05-09 12:13:29 +00:00
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
2015-07-08 10:11:25 +00:00
def feed
2022-10-24 08:38:47 +00:00
uid = params[:uid].to_s
2021-08-13 04:42:14 +00:00
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)
2022-03-29 08:49:13 +00:00
feed_cache_old = feed_cache.all_of([{:invalid_date.ne=>nil},{:invalid_date.lte => Time.now}]).last
2022-03-17 07:36:31 +00:00
feed_cache.all_of([{:invalid_date.ne=>nil},{:invalid_date.lte => Time.now}]).destroy
2021-08-13 04:42:14 +00:00
count = feed_cache.count
if count > 1
feed_cache.limit(count-1).destroy
end
feed_cache = feed_cache.first
2020-07-20 15:48:24 +00:00
anns = ''
if feed_cache.nil?
2021-08-13 04:42:14 +00:00
anns = BulletinFeed.where(uid: uid).first.generate_one_cache_timeout(startdt: startdt,enddt: enddt,dt: dt,base_url: request.base_url,timeout: 20)
2022-03-29 08:49:13 +00:00
anns = (feed_cache_old.content rescue "") if anns.nil?
2020-07-20 15:48:24 +00:00
else
anns = feed_cache.content
end
render :json => anns
2015-09-11 12:28:41 +00:00
end
def rssfeed
2022-10-24 08:38:47 +00:00
uid = params[:uid].to_s
2015-09-11 12:28:41 +00:00
@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)
2021-08-12 06:03:35 +00:00
else
@announcements = Bulletin.can_display_and_sorted.is_approved
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
2016-03-07 09:11:07 +00:00
2015-07-08 10:11:25 +00:00
end