1. accelerate feed and search

2. fix feed error
This commit is contained in:
邱博亞 2024-10-13 21:42:37 +08:00
parent 52ff00f452
commit 763a49028d
11 changed files with 220 additions and 210 deletions

View File

@ -5,8 +5,6 @@ class AnnouncementsController < ApplicationController
def initialize def initialize
super super
@app_title = 'announcement' @app_title = 'announcement'
@manually_sort = manually_sort
Bulletin.instance_variable_set('@manually_sort',@manually_sort)
#self.request = OrbitHelper.request #self.request = OrbitHelper.request
end end
def annc_depts_translations def annc_depts_translations
@ -23,13 +21,6 @@ class AnnouncementsController < ApplicationController
AnnouncementSetting.first.enable_annc_dept rescue false AnnouncementSetting.first.enable_annc_dept rescue false
end end
end end
def manually_sort
if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash
OrbitHelper::SharedHash['announcement'][:enable_manually_sort] rescue AnnouncementSetting.first.enable_manually_sort
else
AnnouncementSetting.first.enable_manually_sort rescue false
end
end
def render_400 def render_400
render(:file => "#{ViewRootDir}/announcements/400.html", :layout => false, :status => 400, :formats => [:html]) render(:file => "#{ViewRootDir}/announcements/400.html", :layout => false, :status => 400, :formats => [:html])
end end
@ -57,8 +48,10 @@ class AnnouncementsController < ApplicationController
@enable_search_flag = false @enable_search_flag = false
@image_version = 'thumb' @image_version = 'thumb'
if page.respond_to?(:select_option_items) if page.respond_to?(:select_option_items)
ModuleApp.all.select{|tmp| tmp.key.to_s=='announcement'}.each do |modile_app| module_app = ModuleApp.where(key: 'announcement').first
@show_option_items = modile_app.show_option_items rescue nil @show_option_items = nil
if module_app && page.respond_to?(:select_option_items)
@show_option_items = module_app.show_option_items
end end
I18n.with_locale(:en) do I18n.with_locale(:en) do
page.select_option_items.each do |select_option_item| page.select_option_items.each do |select_option_item|
@ -111,8 +104,8 @@ class AnnouncementsController < ApplicationController
desc = (desc.blank? ? "announcement image" : desc) desc = (desc.blank? ? "announcement image" : desc)
link_to_show = a.is_external_link ? a.external_link : OrbitHelper.url_to_show(a.to_param) link_to_show = a.is_external_link ? a.external_link : OrbitHelper.url_to_show(a.to_param)
target = a.is_external_link ? "_blank" : "_self" target = a.is_external_link ? "_blank" : "_self"
doc = Nokogiri::HTML(a.title) title = a.title_plain_text
title = doc.text.empty? ? 'no content' : doc.text title = 'no content' if title.blank?
if @image_version == 'thumb' if @image_version == 'thumb'
image_url = a.image.thumb.url image_url = a.image.thumb.url
elsif @image_version == 'mobile' elsif @image_version == 'mobile'
@ -256,11 +249,7 @@ class AnnouncementsController < ApplicationController
else else
cats = ["all"] + cats cats = ["all"] + cats
end end
if @manually_sort anns = sort_announcements(anns)
anns = anns.sort_by { |a| [ (a["is_top"] ? 1 : 0) , -a['sort_number'].to_i, a["postdate"].blank? ? nil : a["postdate"].to_time] }.reverse
else
anns = anns.sort_by { |a| [ (a["is_top"] ? 1 : 0) , a["postdate"].blank? ? nil : a["postdate"].to_time] }.reverse
end
end end
cats = cats.uniq cats = cats.uniq
tags = tags.uniq tags = tags.uniq
@ -509,9 +498,11 @@ class AnnouncementsController < ApplicationController
@read_more_option = 0 @read_more_option = 0
@all_setting_option = 0 @all_setting_option = 0
@image_version = 'thumb' @image_version = 'thumb'
@show_options = nil
if subpart.methods.include? 'select_options'.to_sym if subpart.methods.include? 'select_options'.to_sym
ModuleApp.all.select{|tmp| tmp.key.to_s=='announcement'}.each do |modile_app| module_app = ModuleApp.where(key: 'announcement').first
@show_options = modile_app.show_options rescue nil if module_app
@show_options = module_app.show_options
end end
I18n.with_locale(:en) do I18n.with_locale(:en) do
subpart.select_options.each do |select_option| subpart.select_options.each do |select_option|
@ -621,19 +612,15 @@ class AnnouncementsController < ApplicationController
@page_invalid_time = invalid_date @page_invalid_time = invalid_date
end end
if devide_flag if devide_flag
feeds_anns = get_feed_announcements("widget",nil,cats,widget_data_count - top_anns.count) feeds_anns, _ = get_feed_announcements("widget",nil,cats,widget_data_count - top_anns.count)
top_anns = top_anns + feeds_anns.select{|v| v['is_top'] == true} top_anns += feeds_anns.select{|v| v['is_top'] == true}
if @manually_sort top_anns = sort_announcements(top_anns)
top_anns = top_anns.sort_by { |a| tmp=a["postdate"].blank?;[-a['sort_number'].to_i,tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse
else
top_anns = top_anns.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse
end
rest_count = widget_data_count - top_anns.count rest_count = widget_data_count - top_anns.count
if rest_count <= 0 if rest_count <= 0
anns = top_anns.take(widget_data_count) anns = top_anns.take(widget_data_count)
else else
rest_all_anns = feeds_anns.select{|v| v['is_top'] != true} + not_top_anns.take(rest_count) rest_all_anns = feeds_anns.select{|v| v['is_top'] != true} + not_top_anns.take(rest_count)
rest_anns = rest_all_anns.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse.take(rest_count) rest_anns = sort_announcements(rest_all_anns).take(rest_count)
anns = top_anns + rest_anns anns = top_anns + rest_anns
end end
end end
@ -1003,8 +990,8 @@ class AnnouncementsController < ApplicationController
desc = (desc.blank? ? "announcement image" : desc) desc = (desc.blank? ? "announcement image" : desc)
link_to_show = (a.is_external_link ? a.external_link : OrbitHelper.url_to_show(a.to_param)) rescue "" 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" target = a.is_external_link ? "_blank" : "_self"
doc = Nokogiri::HTML(a.title) title = a.title_plain_text
title = doc.text.empty? ? 'no content' : doc.text title = 'no content' if title.blank?
{ {
"department" => author, "department" => author,
"bulletin_links" => links, "bulletin_links" => links,

View File

@ -15,13 +15,6 @@ class BulletinsController < ApplicationController
AnnouncementSetting.first.enable_annc_dept rescue false AnnouncementSetting.first.enable_annc_dept rescue false
end end
end end
def manually_sort
if defined?(OrbitHelper::SharedHash) && OrbitHelper::SharedHash
OrbitHelper::SharedHash['announcement'][:enable_manually_sort] rescue AnnouncementSetting.first.enable_manually_sort
else
AnnouncementSetting.first.enable_manually_sort rescue false
end
end
def get_bulletins def get_bulletins
page = Page.where(:module => "announcement").first rescue nil page = Page.where(:module => "announcement").first rescue nil
@ -38,8 +31,8 @@ class BulletinsController < ApplicationController
I18n.locale = :zh_tw I18n.locale = :zh_tw
keyword = params[:keyword].to_s keyword = params[:keyword].to_s
if keyword.present? if keyword.present?
keyword = Regexp.new(".*"+keyword+".*") keyword_regex = OrbitHelper.get_keyword_regex(keyword)
bulletins = Bulletin.any_of({:title=>keyword},{:subtitle=>keyword},{:text=>keyword}) bulletins = Bulletin.any_of({:title=>keyword_regex},{:subtitle=>keyword_regex},{:text=>keyword_regex})
else else
bulletins = Bulletin.all bulletins = Bulletin.all
end end
@ -56,7 +49,7 @@ class BulletinsController < ApplicationController
bulletins = bulletins.where(:is_preview.in=>[false,nil]) bulletins = bulletins.where(:is_preview.in=>[false,nil])
bulletins = bulletins.where(:approved.ne => false , :rejected.ne => true) bulletins = bulletins.where(:approved.ne => false , :rejected.ne => true)
bulletins = bulletins.where(:postdate.lt=>Time.now) bulletins = bulletins.where(:postdate.lt=>Time.now)
if (manually_sort rescue false) if (AnnouncementsHelper.enable_manually_sort rescue false)
bulletins = bulletins.order({:is_top => -1, :sort_number => 1, :postdate => -1}).page(page_num).per(per_page) bulletins = bulletins.order({:is_top => -1, :sort_number => 1, :postdate => -1}).page(page_num).per(per_page)
else else
bulletins = bulletins.desc( :is_top, :postdate).page(page_num).per(per_page) bulletins = bulletins.desc( :is_top, :postdate).page(page_num).per(per_page)
@ -112,10 +105,9 @@ class BulletinsController < ApplicationController
end end
# 計算總筆數 Start # 計算總筆數 Start
keyword = params[:keyword].to_s
if keyword.present? if keyword.present?
keyword = Regexp.new(".*"+keyword+".*") keyword_regex = OrbitHelper.get_keyword_regex(keyword)
bulletin_count = Bulletin.any_of({:title=>keyword},{:subtitle=>keyword},{:text=>keyword}) bulletin_count = Bulletin.any_of({:title=>keyword_regex},{:subtitle=>keyword_regex},{:text=>keyword_regex})
else else
bulletin_count = Bulletin.all bulletin_count = Bulletin.all
end end

View File

@ -1,25 +1,4 @@
module AnnouncementsHelper module AnnouncementsHelper
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,tmp_enable_annc_dept=false,annc_depts=[]) def data_to_human_type(a,set_tag_ids=nil,tmp_enable_annc_dept=false,annc_depts=[])
tmp_enable_annc_dept = @tmp_enable_annc_dept if @tmp_enable_annc_dept tmp_enable_annc_dept = @tmp_enable_annc_dept if @tmp_enable_annc_dept
annc_depts = @annc_depts if @annc_depts annc_depts = @annc_depts if @annc_depts
@ -67,7 +46,7 @@ module AnnouncementsHelper
"img_description" => desc "img_description" => desc
} }
end 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 = 'announcement' ma_key = 'announcement'
if categories.nil? if categories.nil?
if type == "index" if type == "index"
@ -79,88 +58,45 @@ module AnnouncementsHelper
end end
end end
categories = ["all"] if categories.length==0 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 data
end end
def get_feed_announcements(type,site_source=nil,categories=nil,max_len=nil) def get_feed_announcements(type,site_source=nil,categories=nil,max_len=nil,extra_match_cond=[])
locale = OrbitHelper.get_site_locale.to_s locale = OrbitHelper.get_site_locale.to_s
feeds = []
feeds_count = 0
if !(defined? SiteFeedAnnc).nil? if !(defined? SiteFeedAnnc).nil?
fans = get_feed_annc(type,site_source,locale,categories,max_len) sort_maps = {is_top: :desc, postdate: :desc, id: :desc}
else match_cond = {
feed_anns = OrbitHelper.get_feed_for_module(type) "is_hidden" => {"$ne" => true},
fans = [] "$and" => [
feed_anns.each do |fa| {"postdate" => {"$lte"=> Time.now}},
next if !site_source.nil? && site_source != fa["source-site-title"] {
status = { "$or" => [
"status" => "<a href='#{fa["source-site"]}' target='_blank' class='feed-source'>#{fa["source-site-title"]}</a>", {"deadline" => {"$gte"=> Time.now}},
"status-class" => "status-source" {"deadline" => nil}
]
} }
],
"title" => {"$gt"=>""}
}
if !extra_match_cond.empty?
match_cond["$and"] += extra_match_cond
end
feeds, feeds_count = get_feed_annc(type,site_source,locale,categories,max_len,sort_maps,match_cond)
end
return feeds, feeds_count
end
files = fa["bulletin_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["bulletin_links"].map{|link| { "link_url" => link["url"], "link_title" => (link["title_translations"][locale].blank? ? link["url"] : link["title_translations"][locale]) } } rescue []
x = {
"bulletin_links" => links,
"bulletin_files" => files,
"bulletin_carousel_images" => fa["bulletin_carousel_images"].to_a,
"title" => fa["title_translations"][locale],
"subtitle" => fa["subtitle_translations"][locale],
"statuses" => [status],
"category" => fa["category"],
"postdate" => fa["postdate"],
"author" => fa["author"],
"source-site" => "<a href='#{fa["source-site"]}' target='_blank' class='feed-source'>#{fa["source-site-title"]}</a>",
"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"] || AnnouncementsController::DefaultImgSrc,
"img_description" => fa["image_description_translations"][locale],
"more" => t("announcement.more"),
"view_count" => ""
}
if (!x["title"].empty? rescue false)
fans << x
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 anns.postdate
if sflag && !eflag
flag = flag && (anns.postdate<=etime)
elsif !sflag && eflag
flag = flag && (anns.postdate>=stime)
elsif !sflag && !eflag
flag = flag && (anns.postdate>=stime) && (anns.postdate<=etime)
end
end
flag
}
else
sorted
end
end
def get_sorted_annc(data_count=nil) def get_sorted_annc(data_count=nil)
params = OrbitHelper.params params = OrbitHelper.params
locale = OrbitHelper.get_site_locale.to_s locale = OrbitHelper.get_site_locale.to_s
@ -168,8 +104,7 @@ module AnnouncementsHelper
page_number = 1 if page_number == 0 page_number = 1 if page_number == 0
page_data_count = data_count || OrbitHelper.page_data_count.to_i page_data_count = data_count || OrbitHelper.page_data_count.to_i
feeds_anns = [] feeds_anns = []
page = OrbitHelper.page rescue nil page = OrbitHelper.page
page = Page.where(url:params['url']).first if page.nil?
if @type == "show_widget" if @type == "show_widget"
tags = @tags tags = @tags
categories = @categories categories = @categories
@ -181,71 +116,86 @@ module AnnouncementsHelper
tags = ["all"] tags = ["all"]
end end
end end
announcements_list = []
announcements = []
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 << {
"postdate" => {"$gte" => stime}
}
end
if !params[:etime].blank?
etime = OrbitHelper.get_time_from_str(params[:etime]) + 1.days
extra_match_cond << {
"postdate" => {"$lt" => etime}
}
end
if !params["source"].present? if !params["source"].present?
announcements = Bulletin.can_display_and_sorted
.filter_by_categories(categories, false).filter_by_tags(tags)
.where(:title.nin => ["",nil])
if !extra_match_cond.empty?
announcements = announcements.and(extra_match_cond)
end
if @type == "show_widget" if @type == "show_widget"
if params[:uids].blank? if !params[:uids].blank?
announcements = Bulletin.where(:title.nin => ["",nil],:is_preview.in=>[false,nil])
.can_display_and_sorted.is_approved
.filter_by_categories(categories,false).filter_by_tags(tags).to_a
else
member_profile = MemberProfile.any_in(:uid=>params[:uids]) member_profile = MemberProfile.any_in(:uid=>params[:uids])
user_ids = member_profile.map{|m| m.user.id rescue nil}.select{|id| !id.nil?} user_ids = member_profile.map{|m| m.user.id rescue nil}.select{|id| !id.nil?}
announcements = Bulletin.where(:title.nin => ["",nil],:is_preview.in=>[false,nil],:update_user_id.in=>user_ids) announcements = announcements.where(:update_user_id.in=>user_ids)
.can_display_and_sorted.is_approved
.filter_by_categories(categories,false).filter_by_tags(tags).to_a
end
else
announcements = Bulletin.where(:title.nin => ["",nil],:is_preview.in=>[false,nil])
.can_display_and_sorted.is_approved
.filter_by_categories(categories,false).filter_by_tags(tags).to_a
end
if !(defined? SiteFeed).nil?
if @type != "show_widget"
feeds_anns = get_feed_announcements("index",nil,categories,page_number*page_data_count)
else
feeds_anns = []
end end
end end
else if !(defined? SiteFeed).nil? && @type != "show_widget"
announcements = [] feeds_anns, feeds_count = get_feed_announcements("index",nil,categories,page_number*page_data_count,extra_match_cond)
if @type != "show_widget"
feeds_anns = get_feed_announcements("index",params["source"],categories,page_number*page_data_count)
else
feeds_anns = []
end end
elsif @type != "show_widget"
feeds_anns, feeds_count = get_feed_announcements("index",params["source"],categories,page_number*page_data_count,extra_match_cond)
end end
if !feeds_anns.blank? if !feeds_anns.blank?
if announcements.count != 0
top_anns = announcements.select{|v| v.is_top} + feeds_anns.select{|v| v['is_top']} top_anns = announcements.select{|v| v.is_top} + feeds_anns.select{|v| v['is_top']}
rest_all_anns = feeds_anns.select{|v| v['is_top'] != true} + announcements.select{|v| !v.is_top} rest_all_anns = announcements.select{|v| !v.is_top} + feeds_anns.select{|v| v['is_top'] != true}
rest_anns = rest_all_anns.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse
if @manually_sort all_filter = sort_announcements(top_anns) + sort_announcements(rest_all_anns)
all_sorted = top_anns.sort_by do |a|
tmp = a["postdate"].blank?
[-a['sort_number'].to_i,tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time]
end.reverse + rest_anns
else else
all_sorted = top_anns.sort_by do |a| all_filter = announcements
tmp = a["postdate"].blank?
[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time]
end.reverse + rest_anns
end
else
all_sorted = feeds_anns.select{|v| v['is_top']}.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse + feeds_anns.select{|v| v['is_top'] != true}.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse
end
all_filter = filter_by_keywords(all_sorted,params[:keywords],params[:stime],params[:etime])
else
all_filter = filter_by_keywords(announcements,params[:keywords],params[:stime],params[:etime])
end end
if page_data_count != 0 if page_data_count != 0
sorted = all_filter[(page_number-1)*page_data_count...page_number*page_data_count] sorted = all_filter[(page_number-1)*page_data_count...page_number*page_data_count]
else else
sorted = all_filter sorted = all_filter
end end
annc_count = all_filter.count annc_count = announcements.count + feeds_count
total_pages = page_data_count == 0 ? 1 : (annc_count.to_f / page_data_count).ceil total_pages = page_data_count == 0 ? 1 : (annc_count.to_f / page_data_count).ceil
[sorted,total_pages] [sorted,total_pages]
end end
def sort_announcements(announcements)
enable_manually_sort = enable_manually_sort
if enable_manually_sort
announcements = announcements.sort_by { |announcement|
tmp1 = announcement["postdate"].blank?
[
-a['sort_number'].to_i,
tmp1 ? 0 : 1, tmp1 ? nil : announcement["postdate"].to_time
]
}.reverse
else
announcements = announcements.sort_by { |announcement|
tmp1 = announcement["postdate"].blank?
[
tmp1 ? 0 : 1, tmp1 ? nil : announcement["postdate"].to_time
]
}.reverse
end
return announcements
end
def render_view_for_annc(overridehtml=nil) def render_view_for_annc(overridehtml=nil)
@key = Site.first.template @key = Site.first.template
def render_link_to_edit(html, url_to_edit) def render_link_to_edit(html, url_to_edit)
@ -513,4 +463,35 @@ module AnnouncementsHelper
</div> </div>
</div>").html_safe </div>").html_safe
end end
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
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['announcement'][:enable_manually_sort]
else
AnnouncementSetting.first.enable_manually_sort rescue false
end
end
end
end end

View File

@ -18,6 +18,9 @@ class AnnouncementSetting
field :annc_depts, type: Array, default: [], localize: true field :annc_depts, type: Array, default: [], localize: true
has_many :anns_status_settings, :autosave => true, :dependent => :destroy has_many :anns_status_settings, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :anns_status_settings, :allow_destroy => true accepts_nested_attributes_for :anns_status_settings, :allow_destroy => true
field :migrate_flag, type: Array, default: []
def self.check_limit_for_user(user_id, b_id = nil) def self.check_limit_for_user(user_id, b_id = nil)
limit = self.first.top_limit rescue 0 limit = self.first.top_limit rescue 0
return true if limit == 0 return true if limit == 0

View File

@ -8,8 +8,8 @@ class Bulletin
include OrbitTag::Taggable include OrbitTag::Taggable
include OrbitCategory::Categorizable include OrbitCategory::Categorizable
include Slug include Slug
require 'bulletin_model/cache' require 'announcement/cache'
include BulletinModel::Cache include Announcement::Cache
attr_accessor :org_tag_ids,:org_category_id attr_accessor :org_tag_ids,:org_category_id
def tags=(ids) def tags=(ids)
self.org_tag_ids = self.tag_ids self.org_tag_ids = self.tag_ids
@ -35,8 +35,8 @@ class Bulletin
end end
super(index,value) super(index,value)
end end
SubPart.class_eval { include BulletinModel::Cache } SubPart.class_eval { include Announcement::Cache }
Page.class_eval { include BulletinModel::Cache } Page.class_eval { include Announcement::Cache }
before_destroy do before_destroy do
if self.event_id.present? && !defined?(Calendar).nil? if self.event_id.present? && !defined?(Calendar).nil?
Event.where(:id=>self.event_id).destroy Event.where(:id=>self.event_id).destroy
@ -56,6 +56,7 @@ class Bulletin
field :event_id field :event_id
field :page_id field :page_id
field :title, as: :slug_title, type: String, localize: true field :title, as: :slug_title, type: String, localize: true
field :title_plain_text, type: String, localize: true
field :subtitle, localize: true field :subtitle, localize: true
field :text, localize: true field :text, localize: true
field :create_user_id field :create_user_id
@ -103,7 +104,7 @@ class Bulletin
scope :open_in_future, ->{where(:is_hidden.ne=>true,:is_preview.ne => true,:postdate.gt=>Time.now).order(postdate: :asc)} scope :open_in_future, ->{where(:is_hidden.ne=>true,:is_preview.ne => true,:postdate.gt=>Time.now).order(postdate: :asc)}
scope :can_display_and_sorted, ->{where(:approved => true,:is_hidden.ne=>true,:is_preview.ne => true).valid_time_range} scope :can_display_and_sorted, ->{where(:approved => true,:is_hidden.ne=>true,:is_preview.ne => true).valid_time_range}
scope :valid_time_range, ->{and_any_of([{"postdate"=>{"$lte"=> Time.now}, "deadline"=>{"$gte"=> Time.now}}, {"postdate"=>{"$lte"=> Time.now}, "deadline"=>nil}]).order((@manually_sort ? {is_top: :desc,sort_number: :asc,postdate: :desc,id: :desc} : {is_top: :desc,postdate: :desc,id: :desc}))} scope :valid_time_range, ->{and_any_of([{"postdate"=>{"$lte"=> Time.now}, "deadline"=>{"$gte"=> Time.now}}, {"postdate"=>{"$lte"=> Time.now}, "deadline"=>nil}]).order((AnnouncementsHelper.enable_manually_sort ? {is_top: :desc,sort_number: :asc,postdate: :desc,id: :desc} : {is_top: :desc,postdate: :desc,id: :desc}))}
scope :is_approved, ->{where(:approved => true)} scope :is_approved, ->{where(:approved => true)}
scope :is_approved_and_show, ->{where(:approved => true,:is_hidden.ne=>true,:is_preview.ne => true)} scope :is_approved_and_show, ->{where(:approved => true,:is_hidden.ne=>true,:is_preview.ne => true)}
scope :filter_cats_and_tags, ->(cats,tags) {filter_by_widget_categories(cats,false).filter_by_tags(tags)} scope :filter_cats_and_tags, ->(cats,tags) {filter_by_widget_categories(cats,false).filter_by_tags(tags)}
@ -116,6 +117,7 @@ class Bulletin
if self.is_top_changed? && !self.is_top if self.is_top_changed? && !self.is_top
self.sort_number = nil self.sort_number = nil
end end
self.migrate_title_plain_text
end end
index({postdate: 1}, { unique: false, background: true }) index({postdate: 1}, { unique: false, background: true })
index({is_top: -1,postdate: -1, _id: -1}, { unique: false, background: true }) index({is_top: -1,postdate: -1, _id: -1}, { unique: false, background: true })
@ -487,6 +489,11 @@ class Bulletin
end end
end 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) def self.notify_all_feed(force_update=false)
related_feeds = BulletinFeed.where(:remote_urls.nin=>[nil, []]).to_a related_feeds = BulletinFeed.where(:remote_urls.nin=>[nil, []]).to_a
related_feeds.each do |feed| related_feeds.each do |feed|

View File

@ -1,13 +1,4 @@
Rails.application.routes.draw do Rails.application.routes.draw do
if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console')
Bulletin
if BulletinFeedCache.count>BulletinFeed.count*5
BulletinFeedCache.collection.drop
end
Thread.new do
BulletinFeedCache.regenerate_all
end
end
locales = Site.first.in_use_locales rescue I18n.available_locales locales = Site.first.in_use_locales rescue I18n.available_locales
scope "(:locale)", locale: Regexp.new(locales.join("|")) do scope "(:locale)", locale: Regexp.new(locales.join("|")) do

View File

@ -1,4 +1,6 @@
require "announcement/engine" require "announcement/engine"
require "announcement/cache"
require "announcement/migrate"
module Announcement module Announcement
end end

View File

@ -1,4 +1,4 @@
module BulletinModel module Announcement
module Cache module Cache
require 'active_support/concern' require 'active_support/concern'
extend ActiveSupport::Concern extend ActiveSupport::Concern

View File

@ -73,15 +73,20 @@ module Announcement
data_item[key_item1] = [value_item1,value_item2,value_item3] data_item[key_item1] = [value_item1,value_item2,value_item3]
data_item[key_item2] = [value2_item1,value2_item2] data_item[key_item2] = [value2_item1,value2_item2]
data_item[key1] = key1_attr data_item[key1] = key1_attr
if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console')
require File.join(Announcement::Engine.root, 'app/models/anns_cache')
if defined?(AnnsCache)
AnnsCache.all.delete
end
end
rescue => e rescue => e
puts ['error in announcement',e] puts ['error in announcement',e]
end end
if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console')
Thread.new do
begin
Migrate.call
rescue => e
puts ['error in announcement', e, e.backtrace]
end
end
end
OrbitApp.registration "Announcement", :type => "ModuleApp" do OrbitApp.registration "Announcement", :type => "ModuleApp" do
module_label "announcement.announcement" module_label "announcement.announcement"
base_url File.expand_path File.dirname(__FILE__) base_url File.expand_path File.dirname(__FILE__)

View File

@ -0,0 +1,42 @@
module Announcement
module Migrate
def self.call
puts ['announcement migrate start']
gem_root = Announcement::Engine.root
require File.join(gem_root, 'app/models/announcement_setting')
require File.join(gem_root, 'app/models/bulletin')
require File.join(gem_root, 'app/models/bulletin_feed')
require File.join(gem_root, 'app/models/bulletin_feed_cache')
require File.join(Announcement::Engine.root, 'app/models/anns_cache')
setting = AnnouncementSetting.first
if !setting.migrate_flag.include?("v1")
Bulletin.all.pluck(:id, :title).each do |id, title_translations|
if title_translations.nil?
next
end
Bulletin.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
AnnsCache.all.delete
#solve bug for thousands of generated feed cache
if BulletinFeedCache.count > BulletinFeed.count*5
BulletinFeedCache.collection.drop
end
BulletinFeedCache.regenerate_all
puts ['announcement migrate end']
end
end
end