diff --git a/lib/remote_announcement.rb b/lib/remote_announcement.rb index 75fd43d20..40c927e57 100644 --- a/lib/remote_announcement.rb +++ b/lib/remote_announcement.rb @@ -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 @@ -132,4 +134,17 @@ def get_remote_bulletins_json(url) 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") \ No newline at end of file +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 \ No newline at end of file