Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 26 additions & 24 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GEM
remote: https://rubygems.org/
specs:
activesupport (8.1.2)
activesupport (8.1.3.1)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.3.1)
Expand All @@ -16,48 +16,49 @@ GEM
uri (>= 0.13.1)
ast (2.4.3)
base64 (0.3.0)
bigdecimal (4.0.1)
concurrent-ruby (1.3.6)
bigdecimal (4.1.2)
concurrent-ruby (1.3.8)
connection_pool (3.0.2)
drb (2.2.3)
i18n (1.14.8)
i18n (1.15.2)
concurrent-ruby (~> 1.0)
json (2.18.0)
language_server-protocol (3.17.0.5)
json (2.21.1)
language_server-protocol (3.17.0.6)
lint_roller (1.1.0)
logger (1.7.0)
minitest (6.0.1)
minitest (6.0.6)
drb (~> 2.0)
prism (~> 1.5)
parallel (1.27.0)
parser (3.3.10.1)
parallel (2.1.0)
parser (3.3.12.0)
ast (~> 2.4.1)
racc
prism (1.8.0)
prism (1.9.0)
racc (1.8.1)
rack (3.2.4)
rack (3.2.6)
rainbow (3.1.1)
regexp_parser (2.11.3)
rubocop (1.82.1)
regexp_parser (2.12.0)
rubocop (1.88.2)
json (~> 2.3)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.1.0)
parallel (~> 1.10)
parallel (>= 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.48.0, < 2.0)
rubocop-ast (>= 1.49.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.49.0)
rubocop-ast (1.50.0)
parser (>= 3.3.7.2)
prism (~> 1.7)
rubocop-capybara (2.22.1)
rubocop-capybara (3.0.0)
lint_roller (~> 1.1)
rubocop (~> 1.72, >= 1.72.1)
rubocop-discourse (3.14.0)
rubocop (~> 1.81)
rubocop-discourse (3.18.0)
activesupport (>= 6.1)
lint_roller (>= 1.1.0)
rubocop-capybara (>= 2.22.0)
rubocop-capybara (>= 2.23.0)
rubocop-discourse-base (>= 1.0.0)
rubocop-factory_bot (>= 2.27.0)
rubocop-rails (>= 2.30.3)
Expand All @@ -68,15 +69,16 @@ GEM
rubocop-factory_bot (2.28.0)
lint_roller (~> 1.1)
rubocop (~> 1.72, >= 1.72.1)
rubocop-rails (2.34.3)
rubocop-rails (2.36.0)
activesupport (>= 4.2.0)
lint_roller (~> 1.1)
rack (>= 1.1)
rubocop (>= 1.75.0, < 2.0)
rubocop-ast (>= 1.44.0, < 2.0)
rubocop-rspec (3.9.0)
rubocop-rspec (3.10.2)
lint_roller (~> 1.1)
rubocop (~> 1.81)
regexp_parser (>= 2.0)
rubocop (~> 1.86, >= 1.86.2)
rubocop-rspec_rails (2.32.0)
lint_roller (~> 1.1)
rubocop (~> 1.72, >= 1.72.1)
Expand All @@ -98,4 +100,4 @@ DEPENDENCIES
rubocop-discourse

BUNDLED WITH
2.6.9
4.0.11
5 changes: 1 addition & 4 deletions app/serializers/news/rss_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# frozen_string_literal: true

class News::RssSerializer < ::ApplicationSerializer
attributes :title,
:description,
:url,
:image_url
attributes :title, :description, :url, :image_url
end
4 changes: 1 addition & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# frozen_string_literal: true

Discourse::Application.routes.prepend do
get 'news' => 'list#news'
end
Discourse::Application.routes.prepend { get "news" => "list#news" }
2 changes: 1 addition & 1 deletion lib/news/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ class Engine < ::Rails::Engine
isolate_namespace News
config.autoload_paths << File.join(config.root, "lib")
end
end
end
2 changes: 1 addition & 1 deletion lib/news/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ def self.generate_body(html, featured_image_url)

doc.to_html
end
end
end
3 changes: 2 additions & 1 deletion lib/news/rss.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def description
def image_url
@image_url ||=
begin
if @attrs.enclosure && @attrs.enclosure.type.include?("image") && @attrs.enclosure.url
if @attrs.enclosure && @attrs.enclosure.type.include?("image") &&
@attrs.enclosure.url
@attrs.enclosure.url
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/news/rss_topic_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

module News
RssTopicList = Struct.new(:topics, :more_topics_url)
end
end
72 changes: 37 additions & 35 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
# 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
register_asset "stylesheets/common/discourse-news.scss"
register_asset "stylesheets/mobile/discourse-news.scss", :mobile

enabled_site_setting :discourse_news_enabled

Expand All @@ -21,71 +21,73 @@
end

add_to_class(:list_controller, :news) do
if SiteSetting.discourse_news_source == 'rss'
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)


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 do
render json: 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("

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')
).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 = 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
if news_item
News::Item.generate_body(first_post.cooked, image_url)
else
nil
@news_body ||=
begin
news_item ? News::Item.generate_body(first_post.cooked, image_url) : nil
end
end
end

module TopicNewsExtension
def reload(options = nil)
@news_body = nil
super(options)
end
end

reloadable_patch do
::Topic.prepend(TopicNewsExtension)
end

add_to_serializer(:topic_list_item, :news_body, include_condition: -> { object.news_item }) do
object.news_body
end
reloadable_patch { ::Topic.prepend(TopicNewsExtension) }

add_to_serializer(
:topic_list_item,
:news_body,
include_condition: -> { object.news_item }
) { object.news_body }
end
17 changes: 12 additions & 5 deletions spec/lib/news/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
RSpec.describe News::Item do
describe ".generate_body" do
it "removes the featured image from the body" do
html = '<p>Hello</p><img src="https://example.com/featured.jpg"><p>World</p>'
result = described_class.generate_body(html, "https://example.com/featured.jpg")
html =
'<p>Hello</p><img src="https://example.com/featured.jpg"><p>World</p>'
result =
described_class.generate_body(html, "https://example.com/featured.jpg")

expect(result).not_to include("featured.jpg")
expect(result).to include("Hello")
expect(result).to include("World")
end

it "removes lightbox wrappers" do
html = '<p>Text</p><div class="lightbox-wrapper"><img src="test.jpg"></div><p>More</p>'
html =
'<p>Text</p><div class="lightbox-wrapper"><img src="test.jpg"></div><p>More</p>'
result = described_class.generate_body(html, nil)

expect(result).not_to include("lightbox-wrapper")
Expand All @@ -28,11 +31,15 @@
paragraphs = doc.css("p")

expect(paragraphs.size).to eq(2)
expect(paragraphs.map(&:text)).to contain_exactly("Content", "More content")
expect(paragraphs.map(&:text)).to contain_exactly(
"Content",
"More content"
)
end

it "preserves valid HTML structure" do
html = '<div class="content"><p>Paragraph</p><ul><li>Item 1</li><li>Item 2</li></ul></div>'
html =
'<div class="content"><p>Paragraph</p><ul><li>Item 1</li><li>Item 2</li></ul></div>'
result = described_class.generate_body(html, nil)

expect(result).to include('<div class="content">')
Expand Down
14 changes: 9 additions & 5 deletions spec/lib/news/rss_spec.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# frozen_string_literal: true

RSpec.describe News::Rss do
let(:sample_rss) do
<<~RSS
let(:sample_rss) { <<~RSS }
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
Expand All @@ -25,11 +24,11 @@
</channel>
</rss>
RSS
end

describe ".detect_charset" do
it "extracts charset from Content-Type header" do
response = double(headers: { "Content-Type" => "text/xml; charset=ISO-8859-1" })
response =
double(headers: { "Content-Type" => "text/xml; charset=ISO-8859-1" })
charset = described_class.detect_charset(response)

expect(charset).to eq(Encoding::ISO_8859_1)
Expand All @@ -43,7 +42,12 @@
end

it "returns nil for invalid charset" do
response = double(headers: { "Content-Type" => "text/xml; charset=invalid-encoding" })
response =
double(
headers: {
"Content-Type" => "text/xml; charset=invalid-encoding"
}
)
charset = described_class.detect_charset(response)

expect(charset).to be_nil
Expand Down
2 changes: 1 addition & 1 deletion spec/system/news_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

RSpec.describe "News page", type: :system do
RSpec.describe "News page" do
fab!(:category)
fab!(:topic) { Fabricate(:topic, category: category) }
fab!(:post) { Fabricate(:post, topic: topic) }
Expand Down
Loading