From ab902a8c0466e47a6a2306ffbbdd41d2e309d4b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Mon, 16 Sep 2024 11:30:37 +0800 Subject: [PATCH] Fix feed. --- .../event_news_feeds_controller.rb | 153 +++--------------- app/controllers/event_news_mods_controller.rb | 23 ++- app/helpers/event_news_helper.rb | 24 +-- app/models/event_news.rb | 120 +++++++++++++- app/models/event_news_feed.rb | 89 +++------- app/models/event_news_feed_cache.rb | 47 +++--- app/models/event_news_setting.rb | 15 ++ lib/event_news_model/cache.rb | 19 ++- 8 files changed, 238 insertions(+), 252 deletions(-) diff --git a/app/controllers/event_news_feeds_controller.rb b/app/controllers/event_news_feeds_controller.rb index f1d5698..66274ca 100644 --- a/app/controllers/event_news_feeds_controller.rb +++ b/app/controllers/event_news_feeds_controller.rb @@ -3,8 +3,25 @@ class EventNewsFeedsController < ApplicationController include Admin::EventNewsHelper def feed uid = params[:uid].to_s - anns = get_event_news(uid) - render :json => anns.to_json + startdt = params[:start].blank? ? nil : params[:start] + enddt = params[:end].blank? ? nil : params[:end] + dt = params[:date].blank? ? nil : params[:date] + feed_cache = EventNewsFeedCache.where(uid: uid, start: startdt, end: enddt, date: dt) + feed_cache_old = feed_cache.all_of([{:invalid_date.ne=>nil},{:invalid_date.lte => Time.now}]).last + feed_cache.all_of([{:invalid_date.ne=>nil},{: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? + anns = EventNewsFeed.where(uid: uid).first.generate_one_cache_timeout(startdt: startdt,enddt: enddt,dt: dt,base_url: request.base_url,timeout: 20) + anns = (feed_cache_old.content rescue "") if anns.nil? + else + anns = feed_cache.content + end + render :json => anns end def rssfeed @@ -14,6 +31,8 @@ class EventNewsFeedsController < ApplicationController tags = @bf.tag_ids if !tags.empty? @event_news = EventNews.can_display_and_sorted.is_approved.filter_by_tags(tags) + else + @event_news = EventNews.can_display_and_sorted.is_approved end categories = @bf.category_ids if !categories.empty? @@ -48,136 +67,6 @@ class EventNewsFeedsController < ApplicationController render :json => {"feeds" => feeds}.to_json end - private - - def smart_convertor(text) - html_string = text - links = html_string.scan(/img.*?src="(.*?)"/i) - links = links + html_string.scan(/a.*?href="(.*?)"/i) - links.uniq! - links.each do |link| - l = link.first - new_link = nil - if l.starts_with?("/") - new_link = request.protocol + request.host_with_port + l - elsif l.starts_with?("..") - l1 = l.gsub("../","") - new_link = request.protocol + request.host_with_port + "/" + l1 - end - html_string = html_string.gsub(l,new_link) if !new_link.nil? - end - return html_string - end - - def get_event_news(uid) - bf = EventNewsFeed.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 - event_news = EventNews.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 - event_news = EventNews.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 - event_news = EventNews.where(:postdate.gt => startdt, :postdate.lt => enddt).can_display_and_sorted.is_approved - else - event_news = EventNews.all.can_display_and_sorted.is_approved - end - if !tags.empty? - event_news = event_news.filter_by_tags(tags) - end - if !categories.empty? - event_news = event_news.filter_by_categories(categories) - end - else - event_news = [] - end - end - all_anns = [] - tag_names = [] - category_titles = [] - tag_ids = [] - category_ids = [] - event_news.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 - translations_fields = ["title","subtitle","speaker","place","host","notes"] - translations_fields.each do |translations_field| - a[translations_field+"_translations"] = anns.send(translations_field+"_translations") rescue {} - end - 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["event_date"] = anns.event_date - a["event_end_date"] = anns.event_end_date - a["postdate"] = anns.event_date#anns.postdate - a["image_description_translations"] = anns.image_description_translations - a["image"] = {} - 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["img_src"] = a["image"]["thumb"] || "/assets/announcement-default.jpg" - a["tags"] = [] - a["category"] = {} - a["author"] = author - a["params"] = anns.to_param - a["event_news_links"] = [] - a["event_news_files"] = [] - a["event_carousel_images"] = anns.event_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.event_news_links.each do |bl| - b = {} - b["url"] = bl.url - b["title_translations"] = bl.title_translations - b["link_url"] = b["url"] - b["link_title_translations"] = bl.title_translations.map{|k,v| [k,(v.blank? ? File.basename(b["url"]) : v rescue '')]}.to_h - a["event_news_links"] << b - end - anns.event_news_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 "") - b["file_url"] = b["url"] - b["file_title_translations"] = bf.title_translations.map{|k,v| [k,(v.blank? ? File.basename(b["url"]) : v rescue '')]}.to_h - a["event_news_files"] << b - end - all_anns << a - end - { - "event_news" => all_anns, - "tags" => tag_names, - "categories" => category_titles - } - end end diff --git a/app/controllers/event_news_mods_controller.rb b/app/controllers/event_news_mods_controller.rb index 1d6deed..4026953 100644 --- a/app/controllers/event_news_mods_controller.rb +++ b/app/controllers/event_news_mods_controller.rb @@ -709,14 +709,14 @@ class EventNewsModsController < ApplicationController } } - files = event_news["event_news_files"].map{|file| { "file_url" => file["url"], "file_title" => (file["title_translations"][locale] == "" ? URI.unescape(File.basename(file["url"])) : file["title_translations"][locale] rescue '') } } rescue [] + files = event_news["event_news_files"].map{|file| { "file_url" => file["file_url"], "file_title" => file["file_title_translations"][locale] } } rescue [] files.each do |file| if file["file_url"] =="" || file["file_url"] == nil files.delete(file) end end - links = event_news["event_news_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale] == "" ? link["url"] : link["title_translations"][locale]) } } rescue [] + links = event_news["event_news_links"].map{|link| { "link_url" => link["link_url"], "link_title" => link["link_title_translations"][locale] } } rescue [] update_user = event_news["author"] desc = event_news["image_description_translations"][locale] rescue "" @@ -733,18 +733,29 @@ class EventNewsModsController < ApplicationController meta_desc = "" end - OrbitHelper.render_meta_tags([{"property" => "og:title", "content" => event_news["title_translations"][locale]},{"property" => "og:site_name", "content" => Site.first.title},{"property" => "og:url", "content" => request.original_url.split("?").first},{"property" => "og:description", "content" => meta_desc},{"property" => "og:image", "content" => event_news["image"]["original"]},{"property" => "og:type", "content" => "Article"}]) + metas = [{"property" => "og:title", "content" => event_news["title_translations"][locale]},{"property" => "og:site_name", "content" => Site.first.title},{"property" => "og:url", "content" => request.original_url.split("?").first},{"property" => "og:description", "content" => meta_desc},{"property" => "og:type", "content" => "Article"}] + if event_news["image"]["original"].present? + image = event_news["image"] + metas << {"property" => "og:image", "content" => image["original"]} + if image["width"] + metas << {"property" => "og:image:width", "content" => image["width"].to_s} + end + if image["height"] + metas << {"property" => "og:image:height", "content" => image["height"].to_s} + end + end + OrbitHelper.render_meta_tags(metas) event_date = DateTime.parse(event_news["event_date"].to_s) rescue nil event_end_date = DateTime.parse(event_news["event_end_date"].to_s) rescue nil - event_time = EventNewsSetting.event_time_formated_for_frontend(event_news["event_date"],event_news["event_end_date"]) + event_time = EventNewsSetting.event_time_formated_for_frontend(event_date, event_end_date) event_carousel_images = Array(event_news["event_carousel_images"]) resume_btn_title = (I18n.locale.to_s =="zh_tw") ? "繼續播放" : "resume" pause_btn_title = (I18n.locale.to_s =="zh_tw") ? "暫停播放" : "pause" prev_btn_title = (I18n.locale.to_s =="zh_tw") ? "上一張" : "prev" next_btn_title = (I18n.locale.to_s =="zh_tw") ? "下一張" : "next" - speaker = event_news["speaker_translations"][locale] rescue "" - host = event_news["host_translations"][locale] rescue "" + speaker = event_news["speaker_translations"][locale] + host = event_news["host_translations"][locale] { "tags" => tags, "event_news_files" => files, diff --git a/app/helpers/event_news_helper.rb b/app/helpers/event_news_helper.rb index b98abe5..4ba42fc 100644 --- a/app/helpers/event_news_helper.rb +++ b/app/helpers/event_news_helper.rb @@ -75,8 +75,8 @@ module EventNewsHelper "img_description" => desc } end - def get_feed_annc(type,site_source,locale,categories=nil) - ma_key = 'event_news' + def get_feed_annc(type,site_source,locale,categories=nil,max_len=nil) + ma_key = 'event_news_mod' if categories.nil? if type == "index" categories = Array(OrbitHelper.page_categories) @@ -87,25 +87,13 @@ module EventNewsHelper end end categories = ["all"] if categories.length==0 - if categories.include?("all") - feeds = SiteFeedAnnc.where(:channel_key => ma_key) - else - feeds = SiteFeedAnnc.where(:channel_key => ma_key, :merge_with_category.in => categories) - end - if feeds.count > 0 - temp_ids = [] - data = feeds.collect do |feed| - feed.all_contents_for_feed(site_source,locale,type=='widget') - end.flatten.compact - else - data = [] - end + data = SiteFeedAnnc.get_feed_cache(ma_key,categories,site_source,locale,type=='widget',max_len) data end def get_feed_event_news(type,site_source=nil,categories=nil,max_len=nil) locale = OrbitHelper.get_site_locale.to_s if !(defined? SiteFeedAnnc).nil? - fans = get_feed_annc(type,site_source,locale,categories) + fans = get_feed_annc(type,site_source,locale,categories,max_len) else feed_anns = OrbitHelper.get_feed_for_module(type) fans = [] @@ -116,8 +104,8 @@ module EventNewsHelper "status-class" => "status-source" } - files = fa["event_news_files"].collect{|bf| { "file_url" => bf["url"], "file_title" => (fa["title_translations"][locale].blank? ? File.basename(fa["url"]) : fa["title_translations"][locale] rescue '') }} rescue [] - links = fa["event_news_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale].blank? ? link["url"] : link["title_translations"][locale]) } } rescue [] + files = fa["event_news_files"].collect{|bf| { "file_url" => bf["file_url"], "file_title" => fa["file_title_translations"][locale] }} rescue [] + links = fa["event_news_links"].map{|link| { "link_url" => link["link_url"], "link_title" => link["link_title_translations"][locale] } } rescue [] event_time_formated = a.event_time_formated s = DateTime.parse(fa["event_date"]) rescue nil e = DateTime.parse(fa["event_end_date"]) rescue nil diff --git a/app/models/event_news.rb b/app/models/event_news.rb index 4237bcb..1bf1ca9 100644 --- a/app/models/event_news.rb +++ b/app/models/event_news.rb @@ -10,14 +10,32 @@ class EventNews include Slug require 'event_news_model/cache' include EventNewsModel::Cache - attr_accessor :org_tag_ids + attr_accessor :org_tag_ids, :org_category_id + def tags=(ids) + self.org_tag_ids = self.tag_ids + super(ids) + end + def category=(cat) + self.org_category_id = self.category_id + super(cat) + end + def tag_ids=(ids) + self.org_tag_ids = self.tag_ids + super(ids) + end + def category_id=(cat_id) + self.org_category_id = self.category_id + super(cat_id) + end def tags=(ids) self.org_tag_ids = self.tag_ids super(ids) end def []=(index,value) - if index.to_s=='tags' + if index.to_s=='tags' || index.to_s=='tag_ids' self.org_tag_ids = self.tag_ids + elsif index.to_s=='category' || index.to_s=='category_id' + self.org_category_id = self.category_id end super(index,value) end @@ -379,4 +397,102 @@ class EventNews :end => re.event_end_date} end end + def self.smart_convertor(text,url) + html_string = text + 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_data(more_url=nil, base_url=nil, cat_ids=nil, tag_ids=nil, locale=nil) + locale = I18n.locale if locale.nil? + base_url = Site.first.root_url if base_url.nil? + user = User.find(self.create_user_id) rescue nil + if !user.nil? + author = user.member_name || user.user_name + else + author = "" + 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 => 'event_news_mod',: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["org_is_top"] = (self.is_top ? 1 : 0) + a["id"] = self.uid + a["title_translations"] = self.title_translations + a["subtitle_translations"] = self.subtitle_translations + a["speaker_translations"] = self.speaker_translations + a["host_translations"] = self.host_translations + a["place_translations"] = self.place_translations + a["notes_translations"] = self.notes_translations + a["text_translations"] = {} + text_translations = self.text_translations + self.text_translations.each do |l, text| + a["text_translations"][l] = self.class.smart_convertor(text,base_url) + end + a["event_date"] = self.event_date + a["event_end_date"] = self.event_end_date + a["postdate"] = self.postdate + a["img_description_translations"] = self.image_description_translations + a["image"] = {} + a["display_img"] = self.display_img + if self.image.present? + a["image"]["original"] = base_url + self.image.url + a["image"]["thumb"] = base_url + self.image.thumb.url + a["image"]["mobile"] = base_url + self.image.mobile.url + a["img_src"] = a["image"]["thumb"] || "/assets/event_news-default.jpg" + image = MiniMagick::Image.open(self.image.path) rescue nil + if image + a["image"]["width"] = image[:width] + a["image"]["height"] = image[:height] + end + end + a["tags"] = [] + a["category"] = {} + a["author"] = author + a["params"] = self.to_param + a["subtitle_ann"] = self.subtitle if self.display_subtitle? + a["event_news_links"] = [] + a["event_news_files"] = [] + a["event_carousel_images"] = self.event_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| + a["tags"] << {"name_translations" => tag.name_translations} + end + cat = self.category + a["category"] = cat ? {"title_translations" => cat.title_translations} : {"title_translations" => {}} + self.event_news_links.each do |bl| + b = {} + b["link_url"] = bl.url + b["link_title_translations"] = bl.title_translations.map{|k,v| [k, (v.blank? ? b["url"] : v)]}.to_h + a["event_news_links"] << b + end + self.event_news_files.each do |bf| + next if bf.file.blank? + b = {} + b["description_translations"] = bf.description_translations + b["file_url"] = base_url + bf.file.url + b["file_title_translations"] = bf.title_translations.map{|k,v| [k, (v.blank? ? File.basename(b["url"]) : v)]}.to_h + a["event_news_files"] << b + end + return a + end end diff --git a/app/models/event_news_feed.rb b/app/models/event_news_feed.rb index 0cc2889..9a7df3f 100644 --- a/app/models/event_news_feed.rb +++ b/app/models/event_news_feed.rb @@ -7,7 +7,7 @@ class EventNewsFeed field :tag_ids, type: Array, default: [] field :category_ids, type: Array, default: [] before_save do - EventNewsCache.where(uid: self.uid).each do |cache| + EventNewsFeedCache.where(uid: self.uid).each do |cache| cache.regenerate end end @@ -16,13 +16,13 @@ class EventNewsFeed if !event_news.nil? self.where(:tag_ids.in => Array(event_news.tag_ids).collect{|v| v.to_s}).each do |event_news_feed| uid = event_news_feed.uid - EventNewsCache.where(:uid => uid).each do |cache| + EventNewsFeedCache.where(:uid => uid).each do |cache| cache.regenerate end end elsif !event_news_feed.nil? uid = event_news_feed.uid - EventNewsCache.where(:uid => uid).each do |cache| + EventNewsFeedCache.where(:uid => uid).each do |cache| cache.regenerate end end @@ -82,13 +82,24 @@ class EventNewsFeed end end all_anns = [] - tag_names = [] - category_titles = [] - tag_ids = [] - category_ids = [] first_postdate = anns_before_filter.open_in_future.limit(1).pluck(:postdate)[0] 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 => 'event_news_mod',:enabled_for=>I18n.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 announcements.each do |anns| deadline = anns.deadline @@ -97,75 +108,15 @@ class EventNewsFeed first_deadline = deadline end end - 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"] = self.class.smart_convertor(anns.text_translations["en"],base_url) if !anns.text_translations["en"].blank? - a["text_translations"]["zh_tw"] = self.class.smart_convertor(anns.text_translations["zh_tw"],base_url) 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"] = ("#{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["event_news_links"] = [] - a["event_news_files"] = [] - a["event_carousel_images"] = anns.event_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.event_news_links.each do |bl| - b = {} - b["url"] = bl.url - b["title_translations"] = bl.title_translations - a["event_news_links"] << b - end - anns.event_news_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["event_news_files"] << b - end + a = anns.get_data(more_url, base_url, cat_ids, tag_ids) all_anns << a end invalid_date = [first_postdate,first_deadline].compact.sort[0] anns = { - "announcements" => all_anns, + "event_news_mods" => all_anns, "tags" => tag_names, "categories" => category_titles }.to_json feed_cache = EventNewsFeedCache.create(uid: uid,content: anns,start: startdt,end: enddt,date: dt,invalid_date: invalid_date) end - def self.smart_convertor(text,url) - html_string = text - 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 end \ No newline at end of file diff --git a/app/models/event_news_feed_cache.rb b/app/models/event_news_feed_cache.rb index a6b8f3b..bb6f7f8 100644 --- a/app/models/event_news_feed_cache.rb +++ b/app/models/event_news_feed_cache.rb @@ -1,29 +1,34 @@ class EventNewsFeedCache - include Mongoid::Document - include Mongoid::Timestamps + include Mongoid::Document + include Mongoid::Timestamps field :content, type: String, default: '' field :uid field :start field :end field :date - field :invalid_date, type: DateTime - def self.regenerate_all - caches = self.all.to_a - caches.each do |cache| - cache.regenerate - end - uids = EventNewsFeed.all.pluck(:uid) - caches.collect(&:uid) - EventNewsFeed.where(:uid.in=> uids).each do |feed| - feed.generate_one_cache() - end - end - def regenerate - st = self.start - et = self.end - dt = self.date - uid = self.uid - EventNewsFeed.where(uid: uid).first.generate_one_cache(startdt: st,enddt: et,dt: dt) rescue nil - self.destroy - end + field :invalid_date, type: DateTime + def self.regenerate_all + caches = self.all.to_a + time_now = Time.now + caches.each do |cache| + if cache.invalid_date && cache.invalid_date <= time_now + cache.destroy + else + cache.regenerate + end + end + uids = EventNewsFeed.all.pluck(:uid) - caches.collect(&:uid) + EventNewsFeed.where(:uid.in=> uids).each do |feed| + feed.generate_one_cache() + end + end + def regenerate + st = self.start + et = self.end + dt = self.date + uid = self.uid + EventNewsFeed.where(uid: uid).each{|v| v.generate_one_cache(startdt: st,enddt: et,dt: dt)} rescue nil + self.destroy + end end \ No newline at end of file diff --git a/app/models/event_news_setting.rb b/app/models/event_news_setting.rb index 2aa8002..141fde7 100644 --- a/app/models/event_news_setting.rb +++ b/app/models/event_news_setting.rb @@ -67,6 +67,21 @@ class EventNewsSetting end datetime_format end + def get_weekday(w) + trans = EventNews::Week_day_trans + if trans.keys.include?(I18n.locale) + trans[I18n.locale][w] + else + trans[:en][w] + end + end + def custom_strftime(dt, datetime_format) + if dt + dt.strftime(datetime_format.sub("%a","%%a")).sub("%a",get_weekday(dt.wday)) + else + "" + end + end def event_date_format @event_date_format ||= self.event_date_default_format end diff --git a/lib/event_news_model/cache.rb b/lib/event_news_model/cache.rb index e1b0492..51cf114 100644 --- a/lib/event_news_model/cache.rb +++ b/lib/event_news_model/cache.rb @@ -3,16 +3,27 @@ module EventNewsModel require 'active_support/concern' extend ActiveSupport::Concern included do - before_save :do_before_save + after_save :cache_tag_ids, :do_before_save + after_destroy :do_before_save + before_destroy :cache_tag_ids + end + def cache_tag_ids + if self.class == ::EventNews + @tag_ids = self.tag_ids + @org_tag_ids = self.org_tag_ids + @category_id = self.category_id + @org_category_id = self.org_category_id + end end def do_before_save if self.class == SubPart ::EventNewsCache.where(parent_id:self.id).destroy - elsif self.class == ::EventNews || (self.class == Page && self.module == "event_news") + elsif self.class == ::EventNews || (self.class == Page && self.module == "event_news_mod") if self.class == ::EventNews - tmp_tag_ids = (Array(self.tag_ids) + Array(self.org_tag_ids)).uniq + tmp_tag_ids = (Array(@tag_ids) + Array(@org_tag_ids)).uniq + tmp_cat_ids = (Array(@category_id) + Array(@org_category_id)).uniq Thread.new do - ::EventNewsCache.where(:uid.in => ::EventNewsFeed.where(:tag_ids.in => tmp_tag_ids.collect{|v| v.to_s}).pluck(:uid)).each do |cache| + ::EventNewsFeedCache.where(:uid.in => ::EventNewsFeed.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)).to_a.each do |cache| cache.regenerate end end