From b59593aa390d49fc1b8245ef3d5aae168c0126dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Sun, 13 Oct 2024 21:46:48 +0800 Subject: [PATCH] 1. accelerate feed and search 2. fix feed error --- .../admin/event_news_controller.rb | 6 + .../event_news_feeds_controller.rb | 4 +- app/controllers/event_news_mods_controller.rb | 50 ++- app/helpers/event_news_helper.rb | 326 ++++++++---------- app/models/event_news.rb | 21 +- app/models/event_news_setting.rb | 3 + config/routes.rb | 5 - lib/event_news_mod.rb | 2 + .../cache.rb | 2 +- lib/event_news_mod/engine.rb | 12 + lib/event_news_mod/migrate.rb | 34 ++ 11 files changed, 250 insertions(+), 215 deletions(-) rename lib/{event_news_model => event_news_mod}/cache.rb (95%) create mode 100644 lib/event_news_mod/migrate.rb diff --git a/app/controllers/admin/event_news_controller.rb b/app/controllers/admin/event_news_controller.rb index aed6f73..f77f428 100644 --- a/app/controllers/admin/event_news_controller.rb +++ b/app/controllers/admin/event_news_controller.rb @@ -585,6 +585,12 @@ class Admin::EventNewsController < OrbitAdminController def update_is_postdate_sort_first(setting) if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash OrbitHelper::SharedHash['event_news_mod'][:is_postdate_sort_first] = setting.is_postdate_sort_first + feeds_time_field = (EventNewsHelper.is_postdate_sort_first ? ['postdate', 'event_date'] : ['event_date', 'postdate']) + @module_app.feeds_time_field = feeds_time_field + @module_app.save + if defined?(Feeds) + Feeds::Migrate.sync_module_apps + end end end end diff --git a/app/controllers/event_news_feeds_controller.rb b/app/controllers/event_news_feeds_controller.rb index d3cb8e8..91fbbfc 100644 --- a/app/controllers/event_news_feeds_controller.rb +++ b/app/controllers/event_news_feeds_controller.rb @@ -51,9 +51,9 @@ class EventNewsFeedsController < ApplicationController if !@bf.nil? tags = @bf.tag_ids if !tags.empty? - @event_news = EventNews.can_display_and_sorted.is_approved.filter_by_tags(tags) + @event_news = EventNews.can_display_and_sorted.filter_by_tags(tags) else - @event_news = EventNews.can_display_and_sorted.is_approved + @event_news = EventNews.can_display_and_sorted end categories = @bf.category_ids if !categories.empty? diff --git a/app/controllers/event_news_mods_controller.rb b/app/controllers/event_news_mods_controller.rb index b171081..db61a09 100644 --- a/app/controllers/event_news_mods_controller.rb +++ b/app/controllers/event_news_mods_controller.rb @@ -24,8 +24,8 @@ class EventNewsModsController < ApplicationController desc = (desc.blank? ? "EventNews image" : desc) link_to_show = a.is_external_link ? a.external_link : OrbitHelper.url_to_show(a.to_param) target = a.is_external_link ? "_blank" : "_self" - doc = Nokogiri::HTML(a.title) - title = doc.text.empty? ? 'no content' : doc.text + title = a.title_plain_text + title = 'no content' if title.blank? event_time = a.event_time_formated_for_frontend event_time_formated = a.event_time_formated { @@ -142,6 +142,8 @@ class EventNewsModsController < ApplicationController get_tabs_option event_news = [] use_tag = false + tags = tags.collect{|tag| tag.to_s} + cats = cats.collect{|cat| cat.to_s} if @tab_option == 0 event_news = get_event_news_for_pack_data(cats,tags,nil,is_random) else @@ -164,26 +166,18 @@ class EventNewsModsController < ApplicationController else cats = ["all"] + cats end - event_news = event_news.sort_by { |a| tmp=a["event_date"].blank?;[tmp ? 0 : 1, tmp ? nil : a["event_date"].to_time] }.reverse + event_news = sort_event_news(event_news) end cats = cats.uniq tags = tags.uniq - tags_translations = tags.map{|tag_id| - if tag_id == "all" - t = I18n.t(:all) - else - t = Tag.find(tag_id).name rescue "" - end - [tag_id,t] - }.to_h - cats_translations = cats.map{|cat_id| - if cat_id == "all" - t = I18n.t(:all) - else - t = Category.find(cat_id).title rescue "" - end - [cat_id,t] - }.to_h + tags_translations = {'all' => I18n.t(:all)} + Tag.where(:id.in => tags).pluck(:id, :title).each{|id, title_translations| + tags_translations[id.to_s] = title_translations[I18n.locale] + } + cats_translations = {'all' => I18n.t(:all)} + Category.where(:id.in => cats).pluck(:id, :title).each{|id, title_translations| + cats_translations[id.to_s] = title_translations[I18n.locale] + } cats_relations = cats_translations.map{|cat_id,t| if cat_id == "all" t = "all" @@ -420,9 +414,11 @@ class EventNewsModsController < ApplicationController @all_setting_option = 0 @image_version = 'thumb' @show_today_data_first = false + @show_options = nil if subpart.methods.include? 'select_options'.to_sym - ModuleApp.all.select{|tmp| tmp.key.to_s=='event_news_mod'}.each do |modile_app| - @show_options = modile_app.show_options rescue nil + module_app = ModuleApp.where(key: 'event_news_mod').first + if module_app + @show_options = module_app.show_options end subpart.select_options.each do |select_option| if !(@show_options.nil?) @@ -528,15 +524,15 @@ class EventNewsModsController < ApplicationController event_news = event_news_cache.first.filter_result end if devide_flag - feeds_event_news = get_feed_event_news("widget",nil,cats,widget_data_count - top_event_news.count) - top_event_news = top_event_news + feeds_event_news.select{|v| v['is_top'] == true} - top_event_news = top_event_news.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse + feeds_event_news, _ = get_feed_event_news("widget",nil,cats,widget_data_count - top_event_news.count) + top_event_news += feeds_event_news.select{|v| v['is_top'] == true} + top_event_news = sort_event_news(top_event_news) rest_count = widget_data_count - top_event_news.count if rest_count <= 0 event_news = top_event_news.take(widget_data_count) else rest_all_event_news = feeds_event_news.select{|v| v['is_top'] != true} + not_top_event_news.take(rest_count) - rest_event_news = rest_all_event_news.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse.take(rest_count) + rest_event_news = sort_event_news(rest_all_event_news).take(rest_count) event_news = top_event_news + rest_event_news end end @@ -835,8 +831,8 @@ class EventNewsModsController < ApplicationController desc = (desc.blank? ? "EventNews image" : desc) link_to_show = (a.is_external_link ? a.external_link : OrbitHelper.url_to_show(a.to_param)) rescue "" target = a.is_external_link ? "_blank" : "_self" - doc = Nokogiri::HTML(a.title) - title = doc.text.empty? ? 'no content' : doc.text + title = a.title_plain_text + title = 'no content' if title.blank? event_time = a.event_time_formated_for_frontend event_time_formated = a.event_time_formated { diff --git a/app/helpers/event_news_helper.rb b/app/helpers/event_news_helper.rb index 31496c0..8967748 100644 --- a/app/helpers/event_news_helper.rb +++ b/app/helpers/event_news_helper.rb @@ -1,25 +1,4 @@ module EventNewsHelper - def self.complementaryColor(my_hex) - if my_hex[0] == '#' - my_hex = my_hex[1..-1] - end - rgb = my_hex.split(//).each_slice(my_hex.length/3).map{|v| v.join} - comp = rgb.map{|a| (255 - a.to_i(16)).to_s(16).rjust(2,'0')} - '#'+comp.join - end - def self.lighten_color(my_hex,percent) - if my_hex[0] == '#' - my_hex = my_hex[1..-1] - end - rgb = my_hex.split(//).each_slice(my_hex.length/3).map{|v| v.join} - comp = rgb.collect do |a| - tmp = a.to_i(16)*(1+percent/100.0) - tmp = 255 if tmp>255 - tmp = 0 if tmp < 0 - tmp.to_i.to_s(16).rjust(2,'0') - end - '#'+comp.join - end def data_to_human_type(a,set_tag_ids=nil) statuses = a.statuses_with_classname.collect do |status| { @@ -75,7 +54,7 @@ module EventNewsHelper "img_description" => desc } end - def get_feed_annc(type,site_source,locale,categories=nil,max_len=nil) + def get_feed_annc(type,site_source,locale,categories=nil,max_len=nil,sort_maps=nil,extra_match_cond=nil) ma_key = 'event_news_mod' if categories.nil? if type == "index" @@ -87,116 +66,58 @@ module EventNewsHelper end end categories = ["all"] if categories.length==0 - data = SiteFeedAnnc.get_feed_cache(ma_key,categories,site_source,locale,type=='widget',max_len) + data = SiteFeedAnnc.get_feed_cache( + ma_key, + categories, + site_source, + locale, + type=='widget', + max_len, + sort_maps, + extra_match_cond + ) data end - def get_feed_event_news(type,site_source=nil,categories=nil,max_len=nil) + def get_feed_event_news(type,site_source=nil,categories=nil,max_len=nil,extra_match_cond=[]) locale = OrbitHelper.get_site_locale.to_s + feeds = [] + feeds_count = 0 if !(defined? SiteFeedAnnc).nil? - fans = get_feed_annc(type,site_source,locale,categories,max_len) - else - feed_anns = OrbitHelper.get_feed_for_module(type) - fans = [] - feed_anns.each do |fa| - next if !site_source.nil? && site_source != fa["source-site-title"] - status = { - "status" => "#{fa["source-site-title"]}", - "status-class" => "status-source" - } - - 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 - date_parse_format = fa["all_day"] ? '%Y-%m-%d' : '%Y-%m-%d %H:%M' - if s.blank? && e.blank? - event_time_formated = "" - elsif s.blank? - event_time_formated = s.strftime(date_parse_format) - elsif e.blank? - event_time_formated = "~ " + e.strftime(date_parse_format) + sort_maps = nil + if @show_today_data_first + sort_maps = {event_date: :asc, is_top: :desc, postdate: :asc, id: :asc} + else + sort_maps = {is_top: :desc} + if EventNewsHelper.is_postdate_sort_first + sort_maps = sort_maps.merge({postdate: :desc, event_date: :desc, id: :desc}) else - if s.to_date == e.to_date - date_str = s.strftime('%Y-%m-%d') - s_time = s.strftime('%H:%M') - e_time = e.strftime('%H:%M') - event_time_formated = "#{date_str} #{s_time} ~ #{e_time}" - else - event_time_formated = s.strftime(date_parse_format) + " ~ " + e.strftime(date_parse_format) - end - end - x = { - "event_news_links" => links, - "event_news_files" => files, - "event_carousel_images" => fa["event_carousel_images"].to_a, - "title" => fa["title_translations"][locale], - "speaker-css" => (fa["speaker_translations"][locale].blank? ? "display: none;" : ""), - "host-css" => (fa["host_translations"][locale].blank? ? "display: none;" : ""), - "place-css" => (fa["place_translations"][locale].blank? ? "display: none;" : ""), - "event-time-css" => (event_time_formated.blank? ? "display: none;" : ""), - "notes-css" => (fa["notes_translations"][locale].blank? ? "display: none;" : ""), - "event-time-formated" => event_time_formated, - "speaker" => (fa["speaker_translations"][locale].to_s rescue ""), - "place" => (fa["place_translations"][locale].to_s rescue ""), - "host" => (fa["host_translations"][locale].to_s rescue ""), - "notes" => (fa["notes_translations"][locale].to_s rescue ""), - "subtitle" => fa["subtitle_translations"][locale], - "statuses" => [status], - "category" => fa["category"], - "postdate" => (DateTime.parse(fa["event_date"]) rescue DateTime.now), - "event_date" => (DateTime.parse(fa["event_date"]) rescue DateTime.now), - "author" => fa["author"], - "source-site" => "#{fa["source-site-title"]}", - "source-site-title" => fa["source-site-title"], - "source-site-link" => fa["source-site"], - "link_to_show" => OrbitHelper.url_to_show(fa["params"]), - "target" => "_self", - "img_src" => fa["image"]["thumb"] || "/assets/event_news-default.jpg", - "img_description" => fa["image_description_translations"][locale], - "more" => t("event_news.more"), - "view_count" => "" - } - if (!x["title"].empty? rescue false) - fans << x + sort_maps = sort_maps.merge({event_date: :desc, postdate: :desc, id: :desc}) end end - end - fans - end - def filter_by_keywords(sorted,keywords,stime,etime) - kflag = keywords.blank? - sflag = stime.blank? - eflag = etime.blank? - stime = stime.to_s.split('/') - stime = Time.zone.local(*stime) rescue nil - etime = etime.to_s.split('/') - etime = Time.zone.local(*etime) rescue nil - if !kflag || !sflag || !eflag - sorted.select{|anns| - if kflag - flag = true - else - if anns["source-site"].present? - title = Nokogiri::HTML(anns["title"].to_s).text - else - title = Nokogiri::HTML(anns.title.to_s).text - end - flag = title.include?(keywords.to_s) - end - if sflag && !eflag - flag = flag && (anns.event_date<=etime) - elsif !sflag && eflag - flag = flag && (anns.event_date>=stime) - elsif !sflag && !eflag - flag = flag && (anns.event_date>=stime) && (anns.event_date<=etime) - end - flag + match_cond = { + "is_hidden" => {"$ne" => true}, + "$and" => [ + {"postdate" => {"$lte"=> Time.now}}, + { + "$or" => [ + {"deadline" => {"$gte"=> Time.now}}, + {"deadline" => nil} + ] + } + ], + "title" => {"$gt"=>""} } - else - sorted + if !extra_match_cond.empty? + match_cond["$and"] += extra_match_cond + end + if @show_today_data_first + match_cond["event_date"] = {"$gte" => Date.today.to_time} + end + feeds, feeds_count = get_feed_annc(type,site_source,locale,categories,max_len,sort_maps,match_cond) end + return feeds, feeds_count end + def get_sorted_event_news(data_count=nil) params = OrbitHelper.params locale = OrbitHelper.get_site_locale.to_s @@ -208,9 +129,7 @@ module EventNewsHelper tags = @tags categories = @categories else - page = OrbitHelper.page rescue nil - page = Page.where(url:params['url']).first if page.nil? - + page = OrbitHelper.page tags = page.tags tags = params[:tags] if params[:tags].present? categories = params['category']=='all' ? (page.categories || []) : (Array(params['category']) rescue (page.categories || [])) @@ -241,22 +160,47 @@ module EventNewsHelper end end - if categories.include? 'all' - @categories = module_app.categories - else - cat_maps = Category.where(:id.in => categories).collect{|cat| [cat.id.to_s, cat]}.to_h - @categories = categories.map{|v| cat_maps[v.to_s]}.compact + if @enable_search_flag + if categories.include? 'all' + @categories = module_app.categories + else + cat_maps = Category.where(:id.in => categories).collect{|cat| [cat.id.to_s, cat]}.to_h + @categories = categories.map{|v| cat_maps[v.to_s]}.compact + end end end event_news_list = [] + event_news = [] + feeds_count = 0 + extra_match_cond = [] + if !params[:keywords].blank? + extra_match_cond << { + "title_plain_text" => OrbitHelper.get_keyword_regex(params[:keywords]) + } + end + if !params[:stime].blank? + stime = OrbitHelper.get_time_from_str(params[:stime]) + extra_match_cond << { + "event_date" => {"$gte" => stime} + } + end + if !params[:etime].blank? + etime = OrbitHelper.get_time_from_str(params[:etime]) + 1.days + extra_match_cond << { + "event_date" => {"$lt" => etime} + } + end + if !extra_match_cond.empty? + event_news = event_news.and(extra_match_cond) + end if !params["source"].present? event_news = @show_today_data_first ? EventNews.can_display_and_sorted_according_today : EventNews.can_display_and_sorted event_news = event_news + .filter_by_categories(categories, false).filter_by_tags(tags) .where(:title.nin => ["",nil]) - .filter_by_categories(categories,false).filter_by_tags(tags) if @type == "show_widget" if !params[:uids].blank? member_profile = MemberProfile.any_in(:uid=>params[:uids]) @@ -264,58 +208,70 @@ module EventNewsHelper event_news = event_news.where(:create_user_id.in=>user_ids) end end + event_news = event_news.limit(page_number*page_data_count) event_news_list = event_news.to_a - if !(defined? SiteFeed).nil? - if @type != "show_widget" - feeds_anns = get_feed_event_news("index",nil,nil,page_number*page_data_count) - else - feeds_anns = [] - end - end - else - if @type != "show_widget" - feeds_anns = get_feed_event_news("index",params["source"],nil,page_number*page_data_count) - else - feeds_anns = [] + if !(defined? SiteFeed).nil? && @type != "show_widget" + feeds_anns, feeds_count = get_feed_event_news("index",nil,nil,page_number*page_data_count, extra_match_cond) end + elsif @type != "show_widget" + feeds_anns, feeds_count = get_feed_event_news("index",params["source"],nil,page_number*page_data_count, extra_match_cond) end if !feeds_anns.blank? - if event_news_list.count != 0 - top_anns = event_news_list.select{|v| v.is_top} + feeds_anns.select{|v| v['is_top']} - rest_all_anns = feeds_anns.select{|v| v['is_top'] != true} + event_news_list.select{|v| !v.is_top} - all_sorted = sort_event_news(top_anns) + sort_event_news(rest_all_anns) - else - all_sorted = sort_event_news(feeds_anns.select{|v| v['is_top']}) + sort_event_news(feeds_anns.select{|v| v['is_top'] != true}) - end - all_filter = filter_by_keywords(all_sorted,params[:keywords],params[:stime],params[:etime]) + top_anns = event_news_list.select{|v| v.is_top} + feeds_anns.select{|v| v['is_top']} + rest_all_anns = event_news_list.select{|v| !v.is_top} + feeds_anns.select{|v| v['is_top'] != true} + all_filter = sort_event_news(top_anns) + sort_event_news(rest_all_anns) else - all_filter = filter_by_keywords(event_news_list,params[:keywords],params[:stime],params[:etime]) + all_filter = event_news_list end if page_data_count != 0 sorted = all_filter[(page_number-1)*page_data_count...page_number*page_data_count] else sorted = all_filter end - annc_count = all_filter.count + annc_count = event_news.count + feeds_count total_pages = page_data_count == 0 ? 1 : (annc_count.to_f / page_data_count).ceil [sorted,total_pages] end def sort_event_news(event_news_list) + enable_manually_sort = enable_manually_sort if @show_today_data_first || !EventNewsHelper.is_postdate_sort_first - event_news_list = event_news_list.sort_by { |event_news| - tmp1 = event_news["event_date"].blank? - tmp2 = event_news["postdate"].blank? - [ - tmp1 ? 0 : 1, tmp1 ? nil : event_news["event_date"].to_time, - tmp2 ? 0 : 1, tmp2 ? nil : event_news["postdate"].to_time - ] - } + if enable_manually_sort + event_news_list = event_news_list.sort_by { |event_news| + tmp1 = event_news["event_date"].blank? + tmp2 = event_news["postdate"].blank? + [ + (@show_today_data_first ? a['sort_number'].to_i : -a['sort_number'].to_i), + tmp1 ? 0 : 1, tmp1 ? nil : event_news["event_date"].to_time, + tmp2 ? 0 : 1, tmp2 ? nil : event_news["postdate"].to_time + ] + } + else + event_news_list = event_news_list.sort_by { |event_news| + tmp1 = event_news["event_date"].blank? + tmp2 = event_news["postdate"].blank? + [ + tmp1 ? 0 : 1, tmp1 ? nil : event_news["event_date"].to_time, + tmp2 ? 0 : 1, tmp2 ? nil : event_news["postdate"].to_time + ] + } + end if !@show_today_data_first event_news_list = event_news_list.reverse end else - event_news_list = event_news_list.sort_by { |event_news| + if enable_manually_sort + event_news_list = event_news_list.sort_by { |event_news| + tmp1 = event_news["event_date"].blank? + tmp2 = event_news["postdate"].blank? + [ + -a['sort_number'].to_i, + tmp2 ? 0 : 1, tmp2 ? nil : event_news["postdate"].to_time, + tmp1 ? 0 : 1, tmp1 ? nil : event_news["event_date"].to_time + ] + }.reverse + else + event_news_list = event_news_list.sort_by { |event_news| tmp1 = event_news["event_date"].blank? tmp2 = event_news["postdate"].blank? [ @@ -323,6 +279,7 @@ module EventNewsHelper tmp1 ? 0 : 1, tmp1 ? nil : event_news["event_date"].to_time ] }.reverse + end end return event_news_list end @@ -594,18 +551,41 @@ module EventNewsHelper ").html_safe end - def self.enable_manually_sort - if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash - OrbitHelper::SharedHash['event_news_mod'][:enable_manually_sort] - else - EventNewsSetting.first.enable_manually_sort rescue false + class << self + def complementaryColor(my_hex) + if my_hex[0] == '#' + my_hex = my_hex[1..-1] + end + rgb = my_hex.split(//).each_slice(my_hex.length/3).map{|v| v.join} + comp = rgb.map{|a| (255 - a.to_i(16)).to_s(16).rjust(2,'0')} + '#'+comp.join end - end - def self.is_postdate_sort_first - if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash - OrbitHelper::SharedHash['event_news_mod'][:is_postdate_sort_first] - else - EventNewsSetting.first.is_postdate_sort_first rescue false + def lighten_color(my_hex,percent) + if my_hex[0] == '#' + my_hex = my_hex[1..-1] + end + rgb = my_hex.split(//).each_slice(my_hex.length/3).map{|v| v.join} + comp = rgb.collect do |a| + tmp = a.to_i(16)*(1+percent/100.0) + tmp = 255 if tmp>255 + tmp = 0 if tmp < 0 + tmp.to_i.to_s(16).rjust(2,'0') + end + '#'+comp.join + end + def enable_manually_sort + if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash + OrbitHelper::SharedHash['event_news_mod'][:enable_manually_sort] + else + EventNewsSetting.first.enable_manually_sort rescue false + end + end + def is_postdate_sort_first + if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash + OrbitHelper::SharedHash['event_news_mod'][:is_postdate_sort_first] + else + EventNewsSetting.first.is_postdate_sort_first rescue false + end end end end diff --git a/app/models/event_news.rb b/app/models/event_news.rb index 66b30bc..8694bf7 100644 --- a/app/models/event_news.rb +++ b/app/models/event_news.rb @@ -8,8 +8,8 @@ class EventNews include OrbitTag::Taggable include OrbitCategory::Categorizable include Slug - require 'event_news_model/cache' - include EventNewsModel::Cache + require 'event_news_mod/cache' + include EventNewsMod::Cache attr_accessor :org_tag_ids, :org_category_id def tags=(ids) self.org_tag_ids = self.tag_ids @@ -39,8 +39,8 @@ class EventNews end super(index,value) end - SubPart.class_eval { include EventNewsModel::Cache } - Page.class_eval { include EventNewsModel::Cache } + SubPart.class_eval { include EventNewsMod::Cache } + Page.class_eval { include EventNewsMod::Cache } before_destroy do EventNewsCache.all.destroy end @@ -63,6 +63,7 @@ class EventNews field :event_id field :place, type: String, localize: true field :title, as: :slug_title, type: String, localize: true + field :title_plain_text, type: String, localize: true field :speaker, type: String, localize: true field :host, type: String, localize: true field :subtitle, localize: true @@ -142,6 +143,7 @@ class EventNews if self.is_top_changed? && !self.is_top self.sort_number = nil end + self.migrate_title_plain_text end index({postdate: 1}, { unique: false, background: true }) index({is_top: -1, postdate: -1, event_date: -1, _id: -1}, { unique: false, background: true }) @@ -466,7 +468,7 @@ class EventNews a["notes_translations"] = self.notes_translations a["text_translations"] = {} text_translations = self.text_translations - self.text_translations.each do |l, text| + text_translations.each do |l, text| a["text_translations"][l] = self.class.smart_convertor(text,base_url) end a["event_date"] = self.event_date @@ -503,7 +505,7 @@ class EventNews 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 + b["link_title_translations"] = bl.title_translations.map{|k,v| [k, (v.blank? ? b["link_url"] : v)]}.to_h a["event_news_links"] << b end self.event_news_files.each do |bf| @@ -511,7 +513,7 @@ class EventNews 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 + b["file_title_translations"] = bf.title_translations.map{|k,v| [k, (v.blank? ? File.basename(b["file_url"]) : v)]}.to_h a["event_news_files"] << b end return a @@ -558,6 +560,11 @@ class EventNews end end end + + def migrate_title_plain_text + self.title_plain_text_translations = OrbitHelper.get_plain_text_translations(self.title_translations) + end + def self.notify_all_feed(force_update=false) related_feeds = EventNewsFeed.where(:remote_urls.nin=>[nil, []]).to_a related_feeds.each do |feed| diff --git a/app/models/event_news_setting.rb b/app/models/event_news_setting.rb index 403a806..183aae0 100644 --- a/app/models/event_news_setting.rb +++ b/app/models/event_news_setting.rb @@ -13,6 +13,9 @@ class EventNewsSetting field :hour_clock_24, type: Boolean, default: true field :enable_manually_sort, type: Boolean, default: false field :is_postdate_sort_first, type: Boolean, default: false + + field :migrate_flag, type: Array, default: [] + has_many :event_news_status_settings, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :event_news_status_settings, :allow_destroy => true diff --git a/config/routes.rb b/config/routes.rb index de9478d..7b19949 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,4 @@ Rails.application.routes.draw do - if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console') - Thread.new do - EventNewsFeedCache.regenerate_all - end - end locales = Site.first.in_use_locales rescue I18n.available_locales scope "(:locale)", locale: Regexp.new(locales.join("|")) do diff --git a/lib/event_news_mod.rb b/lib/event_news_mod.rb index ec461fd..58ec697 100644 --- a/lib/event_news_mod.rb +++ b/lib/event_news_mod.rb @@ -1,4 +1,6 @@ require "event_news_mod/engine" +require "event_news_mod/cache" +require "event_news_mod/migrate" module EventNewsMod end diff --git a/lib/event_news_model/cache.rb b/lib/event_news_mod/cache.rb similarity index 95% rename from lib/event_news_model/cache.rb rename to lib/event_news_mod/cache.rb index 51cf114..b8741a8 100644 --- a/lib/event_news_model/cache.rb +++ b/lib/event_news_mod/cache.rb @@ -1,4 +1,4 @@ -module EventNewsModel +module EventNewsMod module Cache require 'active_support/concern' extend ActiveSupport::Concern diff --git a/lib/event_news_mod/engine.rb b/lib/event_news_mod/engine.rb index 59ac3f4..d404ae3 100644 --- a/lib/event_news_mod/engine.rb +++ b/lib/event_news_mod/engine.rb @@ -92,6 +92,17 @@ module EventNewsMod rescue => e puts ['error in event_news',e,e.backtrace] end + + if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console') + Thread.new do + begin + Migrate.call + rescue => e + puts ['event_news_mod',e, e.backtrace] + end + end + end + OrbitApp.registration "event_news_mod", :type => "ModuleApp" do db = ::Mongoid::Sessions.default collection = db[:module_apps] @@ -117,6 +128,7 @@ module EventNewsMod authorizable frontend_enabled feeds_url "/xhr/event_news/feeds" + feeds_time_field (EventNewsHelper.is_postdate_sort_first ? ['postdate', 'event_date'] : ['event_date', 'postdate']) data_count 1..30 begin show_options data diff --git a/lib/event_news_mod/migrate.rb b/lib/event_news_mod/migrate.rb new file mode 100644 index 0000000..7488f3a --- /dev/null +++ b/lib/event_news_mod/migrate.rb @@ -0,0 +1,34 @@ +module EventNewsMod + module Migrate + def self.call + puts ['event news migrate start'] + gem_root = EventNewsMod::Engine.root + require File.join(gem_root, 'app/models/event_news_setting') + require File.join(gem_root, 'app/models/event_news') + require File.join(gem_root, 'app/models/event_news_feed') + require File.join(gem_root, 'app/models/event_news_feed_cache') + + setting = EventNewsSetting.first + + if !setting.migrate_flag.include?("v1") + EventNews.all.pluck(:id, :title).each do |id, title_translations| + if title_translations.nil? + next + end + EventNews.where(id: id).view.update_many({ + "$set" => { + title_plain_text: OrbitHelper.get_plain_text_translations(title_translations) + } + }) + end + + setting.migrate_flag << "v1" + setting.save + end + + EventNewsFeedCache.regenerate_all + + puts ['event news migrate end'] + end + end +end \ No newline at end of file