Skip to content

Commit f6f5ef0

Browse files
committed
Add log for comment count sync
1 parent 1f29237 commit f6f5ef0

File tree

5 files changed

+48
-14
lines changed

5 files changed

+48
-14
lines changed

app/controllers/admin/dashboard_controller.rb

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@ def show
44
@blog_publish_count = Blog.with_status(:publish).count
55
@blog_draft_count = Blog.with_status(:draft).count
66
@comment_count = Blog.sum(:comment_count)
7-
@category_count = Category.count
8-
@attach_count = Attach.count
9-
@attach_size = Attach.sum('file_size')
10-
117
@total_visits = GaClient.get_total_visits if Setting.ga.chart_enable
128

139
render :json => {
1410
:blog => {:publish => @blog_publish_count, :draft => @blog_draft_count},
1511
:comment => @comment_count,
16-
:category => @category_count,
17-
:attach => {:count => @attach_count, :size => view_context.number_to_human_size(@attach_size)},
12+
:disqus_enable => Setting.disqus.enable,
1813
:total_visits => @total_visits
1914
}
2015
end
@@ -46,4 +41,31 @@ def hot_blogs
4641

4742
render :json => @hot_blogs
4843
end
44+
45+
# 附件统计
46+
def attach
47+
@attach_count = Attach.count
48+
@attach_size = Attach.sum('file_size')
49+
50+
render :json => {
51+
:count => @attach_count,
52+
:size => view_context.number_to_human_size(@attach_size),
53+
}
54+
end
55+
56+
# 评论数同步日志
57+
def sync_comment_logs
58+
@logs = Setting.sync_comment_logs || []
59+
@logs.map!(&:marshal_dump)
60+
61+
render :json => @logs
62+
end
63+
64+
# 同步评论数
65+
def sync_comment
66+
# 防御 get
67+
render :text => '', :status => 404 and return if request.get?
68+
Comment.sync_count
69+
head :no_content
70+
end
4971
end

app/controllers/feed_controller.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ class FeedController < ApplicationController
33
def show
44
expires_in 1.hours, :public=>true
55
@blogs = Blog.with_status(:publish).order('created_at DESC')
6-
7-
respond_to do |format|
8-
format.rss { render :layout => false }
9-
end
6+
render :layout => false
107
end
118

129
end

app/models/comment.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,22 @@ def self.remove(id)
2424

2525
# 从 disqus api 同步 BLOG 评论数
2626
def self.sync_count
27-
DisqusClient.all_thread.each do |th|
28-
blog = Blog.where(:id => th["identifiers"][0]).first
29-
blog.update_columns(:comment_count => th["posts"]) if blog
27+
return unless Disqus.find.enable?
28+
latest_log = {:at => Time.now}
29+
30+
begin
31+
DisqusClient.all_thread.each do |th|
32+
blog = Blog.where(:id => th["identifiers"][0]).first
33+
blog.update_columns(:comment_count => th["posts"]) if blog
34+
end
35+
latest_log[:status] = "success"
36+
rescue Exception => e
37+
latest_log[:error] = e.message
38+
latest_log[:status] = "failed"
39+
ensure
40+
sync_logs = Setting.sync_comment_logs || []
41+
sync_logs = sync_logs.push(latest_log).slice(-5..-1)
42+
Setting.sync_comment_logs = sync_logs
3043
end
3144
end
3245

app/models/setting.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def value
6969
# 使用 JSON 保存值
7070
def value=(new_value)
7171
new_value = new_value.marshal_dump if new_value.is_a? OpenStruct
72+
new_value.map! { |o| (o.is_a? OpenStruct) ? o.marshal_dump : o } if new_value.is_a? Array
7273
self[:value] = new_value.to_json
7374
end
7475
end

config/routes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
get 'categories/:id/page/:page' => 'categories#show'
1717
get 'tags/:id/page/:page' => 'tags#show'
1818

19-
get '/feed' => 'feed#show', :format => :rss, :as => :feed
19+
get '/feed' => 'feed#show', :as => :feed, :defaults => {:format => 'rss'}
2020
get '/archive.html' => 'archive#show', :as => :archive
2121
get '/page/:id.html' => 'pages#show', :as => :page
2222

2323
namespace :admin do
2424
get '/' => 'home#show'
2525
get '/dashboard' => 'dashboard#show'
26+
post '/dashboard/sync_comment' => 'dashboard#sync_comment'
2627
get '/dashboard/:action' => 'dashboard'
2728

2829
resource :session

0 commit comments

Comments
 (0)