Fix limit error.

This commit is contained in:
BoHung Chiu 2021-11-10 15:27:06 +08:00
parent c5a96e8917
commit e3ab735f43
1 changed files with 35 additions and 31 deletions

View File

@ -159,38 +159,42 @@ class SiteFeedAnnc
end
def self.get_feed_cache(channel_key,merge_with_category=nil,site_source=nil,locale=I18n.locale.to_s,is_widget=false,max_len=nil)
match_cond = {"channel_key"=>channel_key}
if !merge_with_category.blank? && merge_with_category.exclude?('all')
match_cond["merge_with_category"] = {"$in"=>merge_with_category}
end
if site_source
match_cond["channel_title.#{locale}"] = site_source
end
pipeline = [
{"$match"=>match_cond},
{"$project"=>{"data"=>"$all_contents_for_feed.#{locale}"}},
{"$unwind"=>"$data"},
{"$sort"=>{"data.is_hidden"=>-1,"data.is_top"=>-1,"data.postdate"=>-1}},
{"$match"=>{"data.is_hidden"=>{"$ne"=>true},
"data.postdate"=>{"$lte"=>Time.now},
"data.title" => {"$gt"=>""}
}
}
]
max_len = 0 if (max_len.to_i < 0 rescue true)
if max_len
pipeline << {"$limit"=> max_len}
end
self.collection.aggregate(pipeline).collect do |data|
tmp = data['data']
if tmp["link_to_show"].nil?
if !is_widget
tmp["link_to_show"] = OrbitHelper.url_to_show(tmp["params"]) rescue ''
else
tmp["link_to_show"] = OrbitHelper.widget_item_url(tmp["params"]) rescue ''
end
max_len = ((max_len.to_i < 0 rescue true) ? 0 : max_len.to_i)
if max_len > 0
match_cond = {"channel_key"=>channel_key}
if !merge_with_category.blank? && merge_with_category.exclude?('all')
match_cond["merge_with_category"] = {"$in"=>merge_with_category}
end
tmp
if site_source
match_cond["channel_title.#{locale}"] = site_source
end
pipeline = [
{"$match"=>match_cond},
{"$project"=>{"data"=>"$all_contents_for_feed.#{locale}"}},
{"$unwind"=>"$data"},
{"$sort"=>{"data.is_hidden"=>-1,"data.is_top"=>-1,"data.postdate"=>-1}},
{"$match"=>{"data.is_hidden"=>{"$ne"=>true},
"data.postdate"=>{"$lte"=>Time.now},
"data.title" => {"$gt"=>""}
}
}
]
if max_len
pipeline << {"$limit"=> max_len}
end
self.collection.aggregate(pipeline).collect do |data|
tmp = data['data']
if tmp["link_to_show"].nil?
if !is_widget
tmp["link_to_show"] = OrbitHelper.url_to_show(tmp["params"]) rescue ''
else
tmp["link_to_show"] = OrbitHelper.widget_item_url(tmp["params"]) rescue ''
end
end
tmp
end
else
[]
end
end