forked from publify/publify
/
dashboard_controller.rb
106 lines (87 loc) · 4.07 KB
/
dashboard_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
class Admin::DashboardController < Admin::BaseController
require 'open-uri'
require 'time'
require 'rexml/document'
def index
t = Time.new
today = t.strftime("%Y-%m-%d 00:00")
# Since last venue
@newposts_count = Article.published_since(current_user.last_venue).count
@newcomments_count = Feedback.published_since(current_user.last_venue).count
# Today
@statposts = Article.published.where("published_at > ?", today).count
@statsdrafts = Article.drafts.where("created_at > ?", today).count
@statspages = Page.where("published_at > ?", today).count
@statuses = Status.where("published_at > ?", today).count
@statuserposts = Article.published.where("published_at > ?", today).count(conditions: {user_id: current_user.id})
@statcomments = Comment.where("created_at > ?", today).count
@presumedspam = Comment.presumed_spam.where("created_at > ?", today).count
@confirmed = Comment.ham.where("created_at > ?", today).count
@unconfirmed = Comment.unconfirmed.where("created_at > ?", today).count
@comments = Comment.last_published
@recent_posts = Article.published.limit(5)
@bestof = Article.bestof
@statspam = Comment.spam.count
@inbound_links = inbound_links
@publify_links = publify_dev
publify_version
end
def publify_version
publify_version = nil
version = PUBLIFY_VERSION.to_s.split('.')
begin
url = "http://blog.publify.co/version.txt"
open(url) do |http|
publify_version = http.read[0..5]
version = publify_version.split('.')
end
rescue
end
if version[0].to_i > TYPO_MAJOR.to_i
flash.now[:error] = _("You are late from at least one major version of Publify. You should upgrade immediately. Download and install %s", "<a href='http://publify.co/stable.tgz'>#{_("the latest Publify version")}</a>").html_safe
elsif version[1].to_i > TYPO_SUB.to_i
flash.now[:warning] = _("There's a new version of Publify available which may contain important bug fixes. Why don't you upgrade to %s ?", "<a href='http://publify.co/stable.tgz'>#{_("the latest Publify version")}</a>").html_safe
elsif version[2].to_i > TYPO_MINOR.to_i
flash.now[:notice] = _("There's a new version of Publify available. Why don't you upgrade to %s ?", "<a href='http://publify.co/stable.tgz'>#{_("the latest Publify version")}</a>").html_safe
end
end
private
def inbound_links
host = URI.parse(this_blog.base_url).host
return [] if (host.downcase == 'localhost' || host =~ /\.local$/) # don't try to fetch links for local blogs
url = "http://www.google.com/search?q=links:#{host}&tbm=blg&output=rss"
parse(url).reverse.compact
end
def publify_dev
url = "http://blog.publify.co/articles.rss"
parse(url)[0..4]
end
def parse(url)
open(url) do |http|
return parse_rss(http.read)
end
rescue
[]
end
class RssItem < Struct.new(:link, :title, :description, :description_link, :date, :author)
def to_s; title end
end
def parse_rss(body)
xml = REXML::Document.new(body)
items = []
link = REXML::XPath.match(xml, "//channel/link/text()").first.value rescue ""
title = REXML::XPath.match(xml, "//channel/title/text()").first.value rescue ""
REXML::XPath.each(xml, "//item/") do |elem|
item = RssItem.new
item.title = REXML::XPath.match(elem, "title/text()").first.value rescue ""
item.link = REXML::XPath.match(elem, "link/text()").first.value rescue ""
item.description = REXML::XPath.match(elem, "description/text()").first.value rescue ""
item.author = REXML::XPath.match(elem, "dc:publisher/text()").first.value rescue ""
item.date = Time.mktime(*ParseDate.parsedate(REXML::XPath.match(elem, "dc:date/text()").first.value)) rescue Date.parse(REXML::XPath.match(elem, "pubDate/text()").first.value) rescue Time.now
item.description_link = item.description
item.description.gsub!(/<\/?a\b.*?>/, "") # remove all <a> tags
items << item
end
items.sort_by { |item| item.date }
end
end