Repository navigation
Expand file tree
/
Copy pathplugin.rb
More file actions
93 lines (74 loc) · 2.5 KB
/
Copy pathplugin.rb
File metadata and controls
93 lines (74 loc) · 2.5 KB
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
# frozen_string_literal: true
# name: discourse-news
# about: Adds a news stream to your Discourse instance
# version: 0.4
# authors: Angus McLeod
# url: https://github.com/paviliondev/discourse-news
register_asset "stylesheets/common/discourse-news.scss"
register_asset "stylesheets/mobile/discourse-news.scss", :mobile
enabled_site_setting :discourse_news_enabled
require_relative "lib/news/engine"
after_initialize do
require_relative "config/routes"
reloadable_patch do
::ListController.prepend(News::Extensions::ListController)
end
add_to_class(:list_controller, :news) do
if SiteSetting.discourse_news_source == "rss"
feed_url = SiteSetting.discourse_news_rss
feed = News::Rss.get_feed_items(feed_url) ## TODO implement caching: News::Rss.cached_feed(feed_url)
serialized =
ActiveModel::ArraySerializer.new(
feed,
each_serializer: News::RssSerializer,
root: false
)
respond_to do |format|
format.html do
@list = News::RssTopicList.new(feed, nil)
store_preloaded("topic_list_news_rss", MultiJson.dump(serialized))
render "list/list"
end
format.json { render json: serialized }
end
else
respond_with_list(TopicQuery.new(current_user).list_news)
end
end
add_to_class(:topic_query, :list_news) do
category_ids = [*SiteSetting.discourse_news_category.split("|")]
topics = Topic.joins(:category).where("categories.id IN (?)", category_ids)
topics =
topics.joins(
"
LEFT OUTER JOIN topic_users AS tu ON (
topics.id = tu.topic_id AND tu.user_id = #{@user.id.to_i}
)"
).references("tu") if @user
topics = topics.where("COALESCE(categories.topic_id, 0) <> topics.id")
topics = topics.order("topics.#{SiteSetting.discourse_news_sort} DESC")
create_list(:news, {}, topics)
end
add_to_class(:topic, :news_item) do
category_ids = SiteSetting.discourse_news_category.split("|").map(&:to_i)
category_ids.include? category_id.to_i
end
add_to_class(:topic, :news_body) do
@news_body ||=
begin
news_item ? News::Item.generate_body(first_post.cooked, image_url) : nil
end
end
module TopicNewsExtension
def reload(options = nil)
@news_body = nil
super(options)
end
end
reloadable_patch { ::Topic.prepend(TopicNewsExtension) }
add_to_serializer(
:topic_list_item,
:news_body,
include_condition: -> { object.news_item }
) { object.news_body }
end