Add notify edit contents feature.
This commit is contained in:
parent
6ecd58de1d
commit
850956c09d
|
@ -0,0 +1,83 @@
|
||||||
|
class FeedsController < ApplicationController
|
||||||
|
skip_before_action :verify_authenticity_token
|
||||||
|
def notify_change
|
||||||
|
feed = SiteFeed.where(:feed_uid=>params[:uid]).first
|
||||||
|
feed_annc = SiteFeedAnnc.where(:feed_id=>feed.id).first
|
||||||
|
if feed_annc
|
||||||
|
locales = Site.first.in_use_locales rescue I18n.available_locales
|
||||||
|
site_root_url = Site.first.root_url rescue ""
|
||||||
|
main_directory = File.join("#{Rails.root}","public","site_feeds")
|
||||||
|
feed_directory = File.join(main_directory.to_s, feed.id.to_s)
|
||||||
|
feed_data = JSON.parse(File.read(File.join(feed_directory.to_s, feed.feed_uid + ".json")))
|
||||||
|
channel_key_pluralize = feed_annc.channel_key.pluralize
|
||||||
|
if params[:type] == 'create'
|
||||||
|
locales.each do |locale|
|
||||||
|
trans = {}
|
||||||
|
locale = locale.to_s
|
||||||
|
trans[locale] = {}
|
||||||
|
I18n.with_locale(locale) do
|
||||||
|
trans[locale]['top'] = I18n.t(:top)
|
||||||
|
trans[locale]['hot'] = I18n.t(:hot)
|
||||||
|
trans[locale]['more_plus'] = I18n.t("feed.more")
|
||||||
|
end
|
||||||
|
locale_sym = locale.to_sym
|
||||||
|
params[:data].each do |a|
|
||||||
|
a = JSON.parse(a)
|
||||||
|
postdate = Time.parse(a["postdate"])
|
||||||
|
insert_idx = 0
|
||||||
|
if postdate
|
||||||
|
insert_idx = feed_annc[:all_contents_for_feed][locale_sym].index{|aa| aa["postdate"] <= postdate}
|
||||||
|
insert_idx = 0 if insert_idx.nil?
|
||||||
|
end
|
||||||
|
feed_annc.all_contents_for_feed_will_change!
|
||||||
|
feed_annc[:all_contents_for_feed][locale_sym].insert(insert_idx, feed_annc.process_tmp(a,locale,trans,site_root_url))
|
||||||
|
feed_data[channel_key_pluralize].insert(insert_idx, a)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
feed_annc.instance_variable_set(:@skip_callback, true)
|
||||||
|
feed_annc.save!
|
||||||
|
elsif params[:type] == 'update'
|
||||||
|
locales.each do |locale|
|
||||||
|
trans = {}
|
||||||
|
locale = locale.to_s
|
||||||
|
trans[locale] = {}
|
||||||
|
I18n.with_locale(locale) do
|
||||||
|
trans[locale]['top'] = I18n.t(:top)
|
||||||
|
trans[locale]['hot'] = I18n.t(:hot)
|
||||||
|
trans[locale]['more_plus'] = I18n.t("feed.more")
|
||||||
|
end
|
||||||
|
locale_sym = locale.to_sym
|
||||||
|
params[:data].each do |a|
|
||||||
|
a = JSON.parse(a)
|
||||||
|
feed_annc[:all_contents_for_feed][locale_sym].each_with_index do |aa, i|
|
||||||
|
if aa["id"] == a["id"]
|
||||||
|
feed_annc.all_contents_for_feed_will_change!
|
||||||
|
feed_annc[:all_contents_for_feed][locale_sym][i] = feed_annc.process_tmp(a,locale,trans,site_root_url)
|
||||||
|
feed_data[channel_key_pluralize][i] = a
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
feed_annc.instance_variable_set(:@skip_callback, true)
|
||||||
|
feed_annc.save!
|
||||||
|
elsif params[:type] == 'destroy'
|
||||||
|
locales.each do |locale|
|
||||||
|
locale_sym = locale.to_sym
|
||||||
|
feed_annc.all_contents_for_feed_will_change!
|
||||||
|
feed_annc[:all_contents_for_feed][locale_sym].reject!{|a| params[:data].include?(a["id"]) }
|
||||||
|
feed_data[channel_key_pluralize].reject!{|a| params[:data].include?(a["id"]) }
|
||||||
|
end
|
||||||
|
feed_annc.instance_variable_set(:@skip_callback, true)
|
||||||
|
feed_annc.save!
|
||||||
|
end
|
||||||
|
feed_data = feed_data.to_json
|
||||||
|
FileUtils.mkdir_p(feed_directory) if !File.exists?(feed_directory)
|
||||||
|
File.open(File.join(feed_directory.to_s,feed.feed_uid + ".json"),"w") do |file|
|
||||||
|
feed_data.force_encoding("utf-8")
|
||||||
|
file.write(feed_data)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
render :json => {success: true}
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,11 +10,13 @@ class SiteFeed
|
||||||
field :disabled, type: Boolean, default: false
|
field :disabled, type: Boolean, default: false
|
||||||
field :feed_url
|
field :feed_url
|
||||||
field :feed_uid
|
field :feed_uid
|
||||||
|
field :enable_notify, type: Boolean, default: false
|
||||||
require 'feed_model/cache'
|
require 'feed_model/cache'
|
||||||
require 'fileutils'
|
require 'fileutils'
|
||||||
include FeedModel::Cache
|
include FeedModel::Cache
|
||||||
Category.send(:include,FeedModel::Cache)
|
Category.send(:include,FeedModel::Cache)
|
||||||
after_create do
|
after_create do
|
||||||
|
self.add_notify
|
||||||
crontab_list = `crontab -l`.split("\n") rescue []
|
crontab_list = `crontab -l`.split("\n") rescue []
|
||||||
site_root = Rails.root.to_s
|
site_root = Rails.root.to_s
|
||||||
if crontab_list.select{|s| s.include?(site_root) && s.include?("feeds_module:make_cache")} == []
|
if crontab_list.select{|s| s.include?(site_root) && s.include?("feeds_module:make_cache")} == []
|
||||||
|
@ -22,6 +24,7 @@ class SiteFeed
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
before_destroy do
|
before_destroy do
|
||||||
|
self.remove_notify
|
||||||
tmp = SiteFeedAnnc.where(feed_id: self.id)
|
tmp = SiteFeedAnnc.where(feed_id: self.id)
|
||||||
main_directory = File.join("#{Rails.root}","public","site_feeds")
|
main_directory = File.join("#{Rails.root}","public","site_feeds")
|
||||||
feed_directory = File.join(main_directory.to_s, self.id.to_s)
|
feed_directory = File.join(main_directory.to_s, self.id.to_s)
|
||||||
|
@ -83,4 +86,38 @@ class SiteFeed
|
||||||
def channel_title_for_cache
|
def channel_title_for_cache
|
||||||
!self[:channel_title].to_s.empty? ? self[:channel_title] : I18n.t("feed.source")
|
!self[:channel_title].to_s.empty? ? self[:channel_title] : I18n.t("feed.source")
|
||||||
end
|
end
|
||||||
|
def add_notify
|
||||||
|
unless self.enable_notify
|
||||||
|
root_url = Site.first.root_url rescue ""
|
||||||
|
if root_url.present?
|
||||||
|
uri = URI(self.remote_site_url)
|
||||||
|
http_req = Net::HTTP.new(uri.host, uri.port)
|
||||||
|
if self.remote_site_url.include?('https')
|
||||||
|
http_req.use_ssl = true
|
||||||
|
end
|
||||||
|
http_req.open_timeout = 10
|
||||||
|
request = Net::HTTP::Post.new("/xhr/#{self.channel_key.pluralize}/feed_add_remote/#{self.feed_uid}", 'Content-Type' => 'application/json')
|
||||||
|
request.body = {"url"=>root_url}.to_json
|
||||||
|
response = http_req.request(request)
|
||||||
|
self.update(:enable_notify=>true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
def remove_notify
|
||||||
|
if self.enable_notify
|
||||||
|
root_url = Site.first.root_url rescue ""
|
||||||
|
if root_url.present?
|
||||||
|
uri = URI(self.remote_site_url)
|
||||||
|
http_req = Net::HTTP.new(uri.host, uri.port)
|
||||||
|
if self.remote_site_url.include?('https')
|
||||||
|
http_req.use_ssl = true
|
||||||
|
end
|
||||||
|
http_req.open_timeout = 10
|
||||||
|
request = Net::HTTP::Post.new("/xhr/#{self.channel_key.pluralize}/feed_remove_remote/#{self.feed_uid}", 'Content-Type' => 'application/json')
|
||||||
|
request.body = {"url"=>root_url}.to_json
|
||||||
|
response = http_req.request(request)
|
||||||
|
self.update(:enable_notify=>false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
|
@ -22,17 +22,15 @@ class SiteFeedAnnc
|
||||||
Array(self[:all_contents_for_feed][I18n.locale.to_s]).select{|v| v['id']==annc_uid}[0] rescue {}
|
Array(self[:all_contents_for_feed][I18n.locale.to_s]).select{|v| v['id']==annc_uid}[0] rescue {}
|
||||||
end
|
end
|
||||||
before_save do
|
before_save do
|
||||||
|
unless @skip_callback
|
||||||
if self.top_list_changed? || self.hot_list_changed? || self.hidden_annc_changed? || self.category_title_changed?
|
if self.top_list_changed? || self.hot_list_changed? || self.hidden_annc_changed? || self.category_title_changed?
|
||||||
self[:all_contents_for_feed] = self.cache_annc
|
self[:all_contents_for_feed] = self.cache_annc
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
def cache_annc(force_refresh=false,locales=nil,trans=nil)
|
|
||||||
feed = SiteFeed.find(self.feed_id)
|
|
||||||
anns = feed.get_annc(force_refresh)
|
|
||||||
cat = self[:category_title]
|
|
||||||
if locales.nil?
|
|
||||||
locales = Site.first.in_use_locales rescue I18n.available_locales
|
|
||||||
end
|
end
|
||||||
|
def process_tmp(a, locale=nil, trans=nil, site_root_url=nil)
|
||||||
|
cat = self[:category_title]
|
||||||
|
locale = I18n.locale.to_s if locale.nil?
|
||||||
if trans.nil?
|
if trans.nil?
|
||||||
trans = {}
|
trans = {}
|
||||||
locales.each do |locale|
|
locales.each do |locale|
|
||||||
|
@ -45,11 +43,9 @@ class SiteFeedAnnc
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
cache_data = {}
|
if site_root_url.nil?
|
||||||
site_root_url = Site.first.root_url rescue ""
|
site_root_url = Site.first.root_url rescue ""
|
||||||
locales.each do |locale|
|
end
|
||||||
locale = locale.to_s
|
|
||||||
data_for_locale = anns.collect do |a|
|
|
||||||
tmp = a.deep_dup
|
tmp = a.deep_dup
|
||||||
tmp[:is_hidden] = self.hidden_annc.include?(tmp['id'])
|
tmp[:is_hidden] = self.hidden_annc.include?(tmp['id'])
|
||||||
if self.channel_key == "announcement"
|
if self.channel_key == "announcement"
|
||||||
|
@ -97,6 +93,7 @@ class SiteFeedAnnc
|
||||||
tmp["link_to_show"] = !tmp["external_link"].blank? ? tmp["external_link"] : nil
|
tmp["link_to_show"] = !tmp["external_link"].blank? ? tmp["external_link"] : nil
|
||||||
tmp["target"] = (site_root_url.blank? || tmp["link_to_show"].blank?) ? '_blank' : (tmp["link_to_show"].include?(site_root_url) ? '_self' : '_blank')
|
tmp["target"] = (site_root_url.blank? || tmp["link_to_show"].blank?) ? '_blank' : (tmp["link_to_show"].include?(site_root_url) ? '_self' : '_blank')
|
||||||
tmp["img_src"] = tmp["image"]["thumb"] || "/assets/announcement-default.jpg"
|
tmp["img_src"] = tmp["image"]["thumb"] || "/assets/announcement-default.jpg"
|
||||||
|
tmp["img_src"] = "/assets/announcement-default.jpg" if tmp["img_src"] == ""
|
||||||
tmp["img_description"] = tmp["image_description_translations"][locale]
|
tmp["img_description"] = tmp["image_description_translations"][locale]
|
||||||
tmp["more"] = trans[locale]['more_plus']
|
tmp["more"] = trans[locale]['more_plus']
|
||||||
tmp["view_count"] = ""
|
tmp["view_count"] = ""
|
||||||
|
@ -155,7 +152,33 @@ class SiteFeedAnnc
|
||||||
end
|
end
|
||||||
tmp = BSON::Document.new(new_tmp)
|
tmp = BSON::Document.new(new_tmp)
|
||||||
end
|
end
|
||||||
tmp
|
return tmp
|
||||||
|
end
|
||||||
|
def cache_annc(force_refresh=false,locales=nil,trans=nil)
|
||||||
|
feed = SiteFeed.find(self.feed_id)
|
||||||
|
anns = feed.get_annc(force_refresh)
|
||||||
|
cat = self[:category_title]
|
||||||
|
if locales.nil?
|
||||||
|
locales = Site.first.in_use_locales rescue I18n.available_locales
|
||||||
|
end
|
||||||
|
if trans.nil?
|
||||||
|
trans = {}
|
||||||
|
locales.each do |locale|
|
||||||
|
locale = locale.to_s
|
||||||
|
trans[locale] = {}
|
||||||
|
I18n.with_locale(locale) do
|
||||||
|
trans[locale]['top'] = I18n.t(:top)
|
||||||
|
trans[locale]['hot'] = I18n.t(:hot)
|
||||||
|
trans[locale]['more_plus'] = I18n.t("feed.more")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
cache_data = {}
|
||||||
|
site_root_url = Site.first.root_url rescue ""
|
||||||
|
locales.each do |locale|
|
||||||
|
locale = locale.to_s
|
||||||
|
data_for_locale = anns.collect do |a|
|
||||||
|
tmp = process_tmp(a, locale, trans, site_root_url)
|
||||||
end
|
end
|
||||||
cache_data[locale.to_s] = data_for_locale
|
cache_data[locale.to_s] = data_for_locale
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,6 +16,7 @@ Rails.application.routes.draw do
|
||||||
end
|
end
|
||||||
SiteFeedAnnc.where(:feed_id.nin=>SiteFeed.all.pluck(:id)).destroy
|
SiteFeedAnnc.where(:feed_id.nin=>SiteFeed.all.pluck(:id)).destroy
|
||||||
SiteFeed.each do |site_feed|
|
SiteFeed.each do |site_feed|
|
||||||
|
site_feed.add_notify
|
||||||
count = SiteFeedAnnc.where(feed_id: site_feed.id).count
|
count = SiteFeedAnnc.where(feed_id: site_feed.id).count
|
||||||
if count>1
|
if count>1
|
||||||
SiteFeedAnnc.where(feed_id: site_feed.id).limit(count-1).destroy
|
SiteFeedAnnc.where(feed_id: site_feed.id).limit(count-1).destroy
|
||||||
|
@ -61,5 +62,5 @@ Rails.application.routes.draw do
|
||||||
post "/feeds/force_refresh", to: 'feeds#force_refresh'
|
post "/feeds/force_refresh", to: 'feeds#force_refresh'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
post "/xhr/feeds/notify_change" => "feeds#notify_change"
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue