From 9007a0c51b575b9615bef964adde59368f1331c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Thu, 30 Dec 2021 17:46:30 +0800 Subject: [PATCH] add feature for setting whether to use source url for annc --- app/controllers/admin/feeds_controller.rb | 16 ++++++++++++++++ app/models/site_feed_annc.rb | 9 +++++---- app/models/site_feed_setting.rb | 6 ++++++ app/views/admin/feeds/settings.html.erb | 15 +++++++++++++++ config/locales/en.yml | 6 +++++- config/locales/zh_tw.yml | 6 +++++- config/routes.rb | 2 ++ lib/feeds/engine.rb | 16 +++++++++++++++- 8 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 app/models/site_feed_setting.rb create mode 100644 app/views/admin/feeds/settings.html.erb diff --git a/app/controllers/admin/feeds_controller.rb b/app/controllers/admin/feeds_controller.rb index dab5821..dd4cb7a 100644 --- a/app/controllers/admin/feeds_controller.rb +++ b/app/controllers/admin/feeds_controller.rb @@ -14,6 +14,22 @@ class Admin::FeedsController < OrbitAdminController redirect_to new_admin_feed_path and return if @site_feeds.count == 0 end + def settings + @setting = SiteFeedSetting.first + end + + def update_settings + @setting = SiteFeedSetting.first + p = params.require(:site_feed_setting).permit! + @setting.update_attributes(p) + Thread.new do + sleep 3 + content = "UNICORN_PID=\"`fuser tmp/pids/unicorn.sock tmp/sockets/unicorn.sock tmp/unicorn.sock` `cat tmp/pids/unicorn.pid `\" && kill -s USR2 $UNICORN_PID ; n=20; while (kill -0 $UNICORN_PID > /dev/null 2>&1) && test $n -ge 0; do printf '.' && sleep 1 && n=$(( $n - 1 )); done ; if test $n -lt 0; then kill -s TERM $UNICORN_PID; sleep 3; bundle exec unicorn_rails -c config/unicorn.rb -D -E #{Rails.env}; else kill -s QUIT $UNICORN_PID; fi" + system(content) + end + redirect_to admin_feeds_settings_path + end + def new end diff --git a/app/models/site_feed_annc.rb b/app/models/site_feed_annc.rb index 61138a3..32dda7e 100644 --- a/app/models/site_feed_annc.rb +++ b/app/models/site_feed_annc.rb @@ -1,6 +1,7 @@ class SiteFeedAnnc include Mongoid::Document include Mongoid::Timestamps + UseSourceUrl = SiteFeedSetting.first.use_source_url rescue false field :top_list,type: Array,default: [] field :hot_list,type: Array,default: [] field :all_contents_for_feed @@ -186,9 +187,9 @@ class SiteFeedAnnc tmp = data['data'] if tmp["link_to_show"].nil? if !is_widget - tmp["link_to_show"] = OrbitHelper.url_to_show(tmp["params"]) rescue '' + tmp["link_to_show"] = UseSourceUrl && tmp["show_url"] ? tmp["show_url"] : OrbitHelper.url_to_show(tmp["params"]) rescue '' else - tmp["link_to_show"] = OrbitHelper.widget_item_url(tmp["params"]) rescue '' + tmp["link_to_show"] = UseSourceUrl && tmp["show_url"] ? tmp["show_url"] : OrbitHelper.widget_item_url(tmp["params"]) rescue '' end end tmp @@ -208,9 +209,9 @@ class SiteFeedAnnc next if tmp["is_hidden"] || (!tmp["postdate"].nil? && tmp["postdate"] + +<%= form_for @setting, url: "/admin/feeds/update_settings", html: {class: "form-horizontal main-forms"} do |f| %> +
+
+ <%= f.label :top_limit, t("feed.use_source_url"), :class => "control-label muted" %> +
+ <%= f.check_box :use_source_url, "data-toggle" => "toggle", "data-onstyle"=>"primary","data-on"=>"#{t("feed.yes")}", "data-off"=> "#{t("feed.no")}", "data-height" => "25", "data-size" => "mini" %> +
+
+
+
+ <%= f.submit t('submit'), class: 'btn btn-primary' %> +
+<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 4708f80..bf08383 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -13,4 +13,8 @@ en: disable: Disable force_refresh: Force Refresh unsubscribe: Unsubscribe - successful: Successful! \ No newline at end of file + successful: Successful! + use_source_url: Use Source Url + 'yes': 'Yes' + 'no': 'No' + settings: Settings \ No newline at end of file diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 27e0cc4..36b0834 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -13,4 +13,8 @@ zh_tw: disable: 關閉 force_refresh: 強制刷新 unsubscribe: 解除訂閱 - successful: 刷新成功 \ No newline at end of file + successful: 刷新成功 + use_source_url: 使用來源連結 + 'yes': 是 + 'no': 否 + settings: 設定 \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 2d5caee..665acb2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -53,6 +53,8 @@ Rails.application.routes.draw do post "/feeds/disable", to: 'feeds#disable' post "/feeds/channel_title", to: 'feeds#channel_title' resources :feeds,only: ['new','index'] + get '/feeds/settings' => 'feeds#settings' + patch '/feeds/update_settings' => 'feeds#update_settings' get '/feeds/announcements' => 'feeds#announcements' post '/feeds/process_annc' => 'feeds#process_annc' get '/feeds/annc_content' => 'feeds#annc_content' diff --git a/lib/feeds/engine.rb b/lib/feeds/engine.rb index 2f9239b..836bf8f 100644 --- a/lib/feeds/engine.rb +++ b/lib/feeds/engine.rb @@ -1,6 +1,14 @@ module Feeds class Engine < ::Rails::Engine - initializer "feeds" do + initializer "feeds" do + if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console') + require File.expand_path('../../../app/models/site_feed_setting', __FILE__) + if defined?(SiteFeedSetting) + if SiteFeedSetting.count==0 + SiteFeedSetting.create + end + end + end OrbitApp.registration "Feeds", :type => "ModuleApp" do module_label "feed.feed" base_url File.expand_path File.dirname(__FILE__) @@ -28,6 +36,12 @@ module Feeds :priority=>3, :active_for_action=>{'admin/feeds'=>'announcements'}, :available_for => 'managers' + context_link 'feed.settings', + :link_path=>"admin_feeds_settings_path" , + :priority=>3, + :active_for_action=>{'admin/feeds'=>'settings'}, + :available_for => 'managers' + end end