-
Notifications
You must be signed in to change notification settings - Fork 8
/
plugin.rb
116 lines (86 loc) · 2.53 KB
/
plugin.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
107
108
109
110
111
112
113
114
115
116
# name: discourse-hot_topics
# about: Adds ranking to topics
# version: 0.1
# author: Joe Buhlig joebuhlig.com
# url: https://github.com/danskdynamit/discourse-hot-topics
register_asset "stylesheets/upvote.scss"
enabled_site_setting :hot_topics_enabled
Discourse.top_menu_items.push(:hot)
Discourse.anonymous_top_menu_items.push(:hot)
Discourse.filters.push(:hot)
Discourse.anonymous_filters.push(:hot)
after_initialize do
if SiteSetting.hot_topics_enabled
require_dependency 'topic'
class ::Topic
def hot_likes
if SiteSetting.hot_topics_only_op_likes
self.first_post.like_count
else
self.like_count
end
end
def hot_time
((Time.now - self.created_at) / 1.hour)
end
def hot_gravity
SiteSetting.hot_topics_gravity_rate
end
def hot_boost
SiteSetting.hot_topics_boost_value
end
def hot_rating
(self.hot_likes + self.hot_boost) / ((self.hot_time + 2) ** self.hot_gravity)
end
def hot_rating_custom
self.custom_fields['upvote_hot']
end
end
require_dependency 'topic_view_serializer'
class ::TopicViewSerializer
attributes :hot_likes, :hot_time, :hot_gravity, :hot_boost, :hot_rating
def hot_likes
object.topic.hot_likes
end
def hot_time
object.topic.hot_time
end
def hot_gravity
object.topic.hot_gravity
end
def hot_boost
object.topic.hot_boost
end
def hot_rating
object.topic.hot_rating
end
def hot_rating_custom
object.topic.custom_fields['upvote_hot']
end
end
add_to_serializer(:topic_list_item, :hot_time) { object.hot_time }
add_to_serializer(:topic_list_item, :hot_likes) { object.hot_likes }
add_to_serializer(:topic_list_item, :hot_gravity) { object.hot_gravity }
add_to_serializer(:topic_list_item, :hot_boost) { object.hot_boost }
add_to_serializer(:topic_list_item, :hot_rating_custom) { object.hot_rating_custom }
require_dependency 'topic_query'
class ::TopicQuery
SORTABLE_MAPPING["hot"] = "custom_fields.upvote_hot"
def list_hot
topics = create_list(:hot, {order: "hot"})
end
end
module ::Jobs
class HotRating < Jobs::Scheduled
every SiteSetting.hot_topics_calc_period.minutes
def execute(args)
Topic.where(closed: false, archetype: 'regular').find_each do |topic|
topic.custom_fields['upvote_hot'] = (topic.hot_rating * 1000000000).to_i
topic.save
end
end
end
end
TopicList.preloaded_custom_fields << "upvote_hot" if TopicList.respond_to? :preloaded_custom_fields
end
end