Update Remote annoucement, annoucement will be deleted when remote site disabled or deleted it

This commit is contained in:
Manson Wang 2013-10-18 17:51:15 +08:00
parent 47894ab6f3
commit 946dc0621b
1 changed files with 16 additions and 1 deletions

View File

@ -7,6 +7,7 @@ require 'date'
@coll_bulletin = @db["bulletins"]
@bulletin_links = @db["bulletin_links"]
@coll_cat = @db["bulletin_categories"]
@updated_bulletins = Array.new
@categories = @coll_cat.find().to_a.inject({}) do |categories, category|
categories[category['key']] = category['_id']
@ -70,6 +71,7 @@ def get_remote_bulletins_json(url)
is_hidden: false }
this_bulletin = @coll_bulletin.find_one(:$and => [{ :sn => bulletin[:sn]},{ :site => bulletin[:site]}])
@updated_bulletins.push({:sn => bulletin[:sn],:site => bulletin[:site] })
# p bulletin
unless this_bulletin
@ -133,3 +135,16 @@ end
get_remote_bulletins_json("http://president.fgu.edu.tw/remote_annoucement/get_news2.php")
get_remote_bulletins_json("http://car.fgu.edu.tw/remote_annoucement/get_news2.php")
# Delete bulletins that had been removed form remote sites
# Find all remote bulletins on server
local_bulletins = @coll_bulletin.find({ site: { :$exists => true } })
# Check if each remote bulletins on server exist in the updated bulletins
local_bulletins.each do |local_bulletin|
result = @updated_bulletins.select {|remote_bulletin| remote_bulletin[:sn] == local_bulletin["sn"] and remote_bulletin[:site] == local_bulletin["site"]}
if result.length == 0
@coll_bulletin.remove(:$and => [{ :sn => local_bulletin["sn"]},{ :site => local_bulletin["site"]}])
p "Delete: "+local_bulletin["site"]+"-"+local_bulletin["sn"]
end
end