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
8 changes: 6 additions & 2 deletions app/controllers/discourse_rewind/rewinds_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ class RewindsController < ::ApplicationController

def show
DiscourseRewind::FetchReports.call(service_params) do
on_model_not_found(:year) { raise Discourse::NotFound }
on_model_not_found(:user) { raise Discourse::NotFound }
on_model_not_found(:year) do
raise Discourse::NotFound.new(nil, custom_message: "discourse_rewind.invalid_year")
end
on_model_not_found(:reports) do
raise Discourse::NotFound.new(nil, custom_message: "discourse_rewind.report_failed")
end
on_success { |reports:| render json: MultiJson.dump(reports), status: :ok }
end
end
Expand Down
14 changes: 12 additions & 2 deletions app/services/discourse_rewind/action/best_posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,19 @@ def call
.where(created_at: date)
.where(deleted_at: nil)
.where("post_number > 1")
.order("like_count DESC NULLS LAST")
.order("like_count DESC NULLS LAST, created_at ASC")
.limit(3)
.pluck(:post_number, :topic_id, :like_count, :reply_count, :raw, :cooked)
.select(:post_number, :topic_id, :like_count, :reply_count, :raw, :cooked)
.map do |post|
{
post_number: post.post_number,
topic_id: post.topic_id,
like_count: post.like_count,
reply_count: post.reply_count,
excerpt:
post.excerpt(200, { strip_links: true, remap_emoji: true, keep_images: true }),
}
end

{ data: best_posts, identifier: "best-posts" }
end
Expand Down
10 changes: 3 additions & 7 deletions app/services/discourse_rewind/fetch_reports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,11 @@ class FetchReports
]

model :year
model :user
model :date
model :reports

private

def fetch_user(guardian:)
User.find_by_username(guardian.user.username)
end

def fetch_year
current_date = Time.zone.now
current_month = current_date.month
Expand All @@ -76,12 +71,13 @@ def fetch_date(params:, year:)
Date.new(year).all_year
end

def fetch_reports(date:, user:, guardian:, year:)
def fetch_reports(date:, guardian:, year:)
key = "rewind:#{guardian.user.username}:#{year}"
reports = Discourse.redis.get(key)

if !reports
reports = REPORTS.map { |report| report.call(date:, user:, guardian:) }.compact
reports =
REPORTS.map { |report| report.call(date:, user: guardian.user, guardian:) }.compact
Discourse.redis.setex(key, CACHE_DURATION, MultiJson.dump(reports))
else
reports = MultiJson.load(reports, symbolize_keys: true)
Expand Down
11 changes: 6 additions & 5 deletions assets/javascripts/discourse/components/reports/best-posts.gjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Component from "@glimmer/component";
import { concat } from "@ember/helper";
import { get } from "@ember/object";
import { htmlSafe } from "@ember/template";
import icon from "discourse/helpers/d-icon";
import { i18n } from "discourse-i18n";
Expand All @@ -22,13 +21,15 @@ export default class BestPosts extends Component {
<div class={{concat "rewind-card" " rank-" (this.rank idx)}}>
<span class="best-posts -rank"></span>
<span class="best-posts -rank"></span>
<div class="best-posts__post">{{htmlSafe (get post "5")}}</div>
<div class="best-posts__post"><p>{{htmlSafe
post.excerpt
}}</p></div>
<div class="best-posts__metadata">
<span class="best-posts__likes">
{{icon "heart"}}{{htmlSafe (get post "2")}}</span>
{{icon "heart"}}{{post.like_count}}</span>
<span class="best-posts__replies">
{{icon "comment"}}{{htmlSafe (get post "3")}}</span>
<a href="/t/{{get post '1'}}/{{get post '0'}}">{{i18n
{{icon "comment"}}{{post.reply_count}}</span>
<a href="/t/{{post.topic_id}}/{{post.post_number}}">{{i18n
"discourse_rewind.reports.best_posts.view_post"
}}</a>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Component from "@glimmer/component";
import { concat } from "@ember/helper";
import { htmlSafe } from "@ember/template";
import replaceEmoji from "discourse/helpers/replace-emoji";
import { i18n } from "discourse-i18n";

export default class BestTopics extends Component {
Expand All @@ -25,8 +26,8 @@ export default class BestTopics extends Component {
<span class="best-topics -rank"></span>
<span class="best-topics -rank"></span>
<h2 class="best-topics__header">{{topic.title}}</h2>
<span class="best-topics__excerpt">{{htmlSafe
topic.excerpt
<span class="best-topics__excerpt">{{replaceEmoji
(htmlSafe topic.excerpt)
}}</span>
</a>
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const MostViewedCategories = <template>
<a class="folder-wrapper" href={{concat "/c/-/" data.category_id}}>
<span class="folder-tab"></span>
<div class="rewind-card">
<p class="most-viewed-categories__category">{{data.name}}</p>
<p class="most-viewed-categories__category">#{{data.name}}</p>
</div>
<span class="folder-bg"></span>
</a>
Expand Down
3 changes: 3 additions & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
en:
site_settings:
discourse_rewind_enabled: "Enable Discourse Rewind for a fun end-of-year summary for members' activity in the community."
discourse_rewind:
report_failed: "Failed to generate Discourse Rewind report."
invalid_year: "Rewind can only be generated in January or December."
16 changes: 16 additions & 0 deletions spec/requests/rewinds_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
get "/rewinds.json"

expect(response.status).to eq(404)
expect(response.parsed_body["errors"].first).to eq(I18n.t("discourse_rewind.invalid_year"))
end
end

Expand All @@ -26,6 +27,21 @@

expect(response.status).to eq(200)
end

context "when reports are not found or error" do
before do
DiscourseRewind::Action::TopWords.stubs(:call).raises(StandardError.new("Some error"))
end

it "returns 404 with message" do
get "/rewinds.json"

expect(response.status).to eq(404)
expect(response.parsed_body["errors"].first).to eq(
I18n.t("discourse_rewind.report_failed"),
)
end
end
end
end
end