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

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/services/discourse_rewind/action/best_posts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ def call
.order("like_count DESC NULLS LAST")
.limit(3)
.pluck(:post_number, :topic_id, :like_count, :reply_count, :raw, :cooked)
.map do |post_number, topic_id, like_count, reply_count, raw, cooked|
{
post_number: post_number,
topic_id: topic_id,
like_count: like_count,
reply_count: reply_count,
raw: raw,
cooked: cooked,
}
end

{ data: best_posts, identifier: "best-posts" }
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/discourse_rewind/action/reactions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def enabled?
end

def sort_and_limit(reactions)
reactions.sort_by { |_, v| -v }.first(5).reverse.to_h
reactions.sort_by { |_, value| -value }.first(5).reverse.to_h
end
end
end
Expand Down
9 changes: 7 additions & 2 deletions app/services/discourse_rewind/action/top_words.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class TopWords < BaseReport
{ word: "you", score: 80 },
{ word: "overachieved", score: 70 },
{ word: "assume", score: 60 },
{ word: "week", score: 50 },
],
identifier: "top-words",
}
Expand Down Expand Up @@ -79,7 +78,13 @@ def call
LIMIT 100
SQL

word_score = words.map { { word: _1.original_word, score: _1.ndoc + _1.nentry } }
word_score =
words
.map do |word_data|
{ word: word_data.original_word, score: word_data.ndoc + word_data.nentry }
end
.sort_by! { |w| -w[:score] }
.take(5)

{ data: word_score, identifier: "top-words" }
end
Expand Down
9 changes: 7 additions & 2 deletions app/services/discourse_rewind/fetch_reports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module DiscourseRewind
#
# @example
# ::DiscourseRewind::Rewind::Fetch.call(
# guardian: guardian
# guardian: guardian,
# params: { year: 2023, username: 'codinghorror' }
# )
#
class FetchReports
Expand Down Expand Up @@ -55,7 +56,11 @@ def fetch_year
when 12
current_year
else
false
if Rails.env.development?
current_year
else
false
end
end
end

Expand Down
25 changes: 14 additions & 11 deletions assets/javascripts/discourse/components/reports/best-posts.gjs
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
import Component from "@glimmer/component";
import { concat } from "@ember/helper";
import { get } from "@ember/object";
import { htmlSafe } from "@ember/template";
import concatClass from "discourse/helpers/concat-class";
import icon from "discourse/helpers/d-icon";
import { i18n } from "discourse-i18n";

export default class BestPosts extends Component {
rank(idx) {
return idx + 1;
rankClass(idx) {
return `rank-${idx + 1}`;
}

<template>
{{#if @report.data.length}}
<div class="rewind-report-page -best-posts">
<h2 class="rewind-report-title">{{i18n
<h2 class="rewind-report-title">
{{i18n
"discourse_rewind.reports.best_posts.title"
count=@report.data.length
}}</h2>
}}
</h2>
<div class="rewind-report-container">
{{#each @report.data as |post idx|}}
<div class={{concat "rewind-card" " rank-" (this.rank idx)}}>
<div class={{concatClass "rewind-card" (this.rankClass 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">{{htmlSafe post.cooked}}</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
22 changes: 13 additions & 9 deletions assets/javascripts/discourse/components/reports/best-topics.gjs
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
import Component from "@glimmer/component";
import { concat } from "@ember/helper";
import { htmlSafe } from "@ember/template";
import concatClass from "discourse/helpers/concat-class";
import getURL from "discourse/lib/get-url";
import { i18n } from "discourse-i18n";

export default class BestTopics extends Component {
rank(idx) {
return idx + 1;
rankClass(idx) {
return `rank-${idx + 1}`;
}

<template>
{{#if @report.data.length}}
<div class="rewind-report-page -best-topics">
<h2 class="rewind-report-title">{{i18n
<h2 class="rewind-report-title">
{{i18n
"discourse_rewind.reports.best_topics.title"
count=@report.data.length
}}</h2>
}}
</h2>
<div class="rewind-report-container">
<div class="rewind-card">
{{#each @report.data as |topic idx|}}
<a
href={{concat "/t/-/" topic.topic_id}}
class={{concat "best-topics__topic" " rank-" (this.rank idx)}}
href={{getURL (concat "/t/-/" topic.topic_id)}}
class={{concatClass "best-topics__topic" (this.rankClass idx)}}
>
<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>
<span class="best-topics__excerpt">
{{htmlSafe topic.excerpt}}
</span>
</a>
{{/each}}
</div>
Expand Down
41 changes: 18 additions & 23 deletions assets/javascripts/discourse/components/reports/top-words.gjs
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
import Component from "@glimmer/component";
import { i18n } from "discourse-i18n";
import WordCard from "discourse/plugins/discourse-rewind/discourse/components/reports/top-words/word-card";

export default class WordCards extends Component {
get topWords() {
return this.args.report.data.sort((a, b) => b.score - a.score).slice(0, 5);
}

<template>
<div class="rewind-report-page -top-words">
<div class="rewind-report-container">
<h2 class="rewind-report-title">{{i18n
"discourse_rewind.reports.top_words.title"
}}</h2>
<div class="cards-container">
{{#each this.topWords as |entry index|}}
<WordCard
@word={{entry.word}}
@count={{entry.score}}
@index={{index}}
/>
{{/each}}
</div>
const WordCards = <template>
<div class="rewind-report-page -top-words">
<div class="rewind-report-container">
<h2 class="rewind-report-title">{{i18n
"discourse_rewind.reports.top_words.title"
}}</h2>
<div class="cards-container">
{{#each @report.data as |entry index|}}
<WordCard
@word={{entry.word}}
@count={{entry.score}}
@index={{index}}
/>
{{/each}}
</div>
</div>
</template>
}
</div>
</template>;

export default WordCards;
66 changes: 66 additions & 0 deletions spec/actions/best_posts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

RSpec.describe DiscourseRewind::Action::BestPosts do
fab!(:date) { Date.new(2021).all_year }
fab!(:user)
fab!(:post_1) { Fabricate(:post, created_at: random_datetime, user: user, post_number: 3) }
fab!(:post_2) { Fabricate(:post, created_at: random_datetime, user: user, post_number: 2) }
fab!(:post_3) { Fabricate(:post, created_at: random_datetime, user: user, post_number: 10) }
fab!(:post_4) { Fabricate(:post, created_at: random_datetime, user: user, post_number: 6) }
fab!(:post_5) { Fabricate(:post, created_at: random_datetime, user: user, post_number: 1) }

describe ".call" do
it "returns top 3 posts ordered by like count" do
post_4.update!(like_count: 15)
post_3.update!(like_count: 13)
post_1.update!(like_count: 11)
post_2.update!(like_count: 9)
post_5.update!(like_count: 7)

expect(call_report[:data]).to eq(
[
{
post_number: post_4.post_number,
topic_id: post_4.topic_id,
like_count: post_4.like_count,
reply_count: post_4.reply_count,
raw: post_4.raw,
cooked: post_4.cooked,
},
{
post_number: post_3.post_number,
topic_id: post_3.topic_id,
like_count: post_3.like_count,
reply_count: post_3.reply_count,
raw: post_3.raw,
cooked: post_3.cooked,
},
{
post_number: post_1.post_number,
topic_id: post_1.topic_id,
like_count: post_1.like_count,
reply_count: post_1.reply_count,
raw: post_1.raw,
cooked: post_1.cooked,
},
],
)
end

context "when a post is deleted" do
before { post_1.trash!(Discourse.system_user) }

it "is not included" do
expect(call_report[:data].map { |d| d[:post_number] }).not_to include(post_1.post_number)
end
end

context "when a post is made by another user" do
before { post_1.update!(user: Fabricate(:user)) }

it "is not included" do
expect(call_report[:data].map { |d| d[:post_number] }).not_to include(post_1.post_number)
end
end
end
end
61 changes: 61 additions & 0 deletions spec/actions/best_topics_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

RSpec.describe DiscourseRewind::Action::BestTopics do
fab!(:date) { Date.new(2021).all_year }
fab!(:user)
fab!(:topic_1) { Fabricate(:topic, user: user, created_at: random_datetime) }
fab!(:topic_2) { Fabricate(:topic, user: user, created_at: random_datetime) }
fab!(:topic_3) { Fabricate(:topic, user: user, created_at: random_datetime) }
fab!(:topic_4) { Fabricate(:topic, user: user, created_at: random_datetime) }
fab!(:topic_5) { Fabricate(:topic, user: user, created_at: random_datetime) }

before { TopTopic.refresh! }

describe ".call" do
it "returns top 3 topics ordered by yearly_score" do
TopTopic.find_by(topic_id: topic_1.id).update!(yearly_score: 15)
TopTopic.find_by(topic_id: topic_2.id).update!(yearly_score: 10)
TopTopic.find_by(topic_id: topic_3.id).update!(yearly_score: 6)
TopTopic.find_by(topic_id: topic_4.id).update!(yearly_score: 11)
TopTopic.find_by(topic_id: topic_5.id).update!(yearly_score: 13)
expect(call_report[:data]).to eq(
[
{
topic_id: topic_1.id,
title: topic_1.title,
excerpt: topic_1.excerpt,
yearly_score: 15,
},
{
topic_id: topic_5.id,
title: topic_5.title,
excerpt: topic_5.excerpt,
yearly_score: 13,
},
{
topic_id: topic_4.id,
title: topic_4.title,
excerpt: topic_4.excerpt,
yearly_score: 11,
},
],
)
end

context "when a topic is deleted" do
before { topic_1.trash!(Discourse.system_user) }

it "is not included" do
expect(call_report[:data].map { |d| d[:topic_id] }).not_to include(topic_1.id)
end
end

context "when a topic" do
before { topic_1.update!(user: Fabricate(:user)) }

it "is not included" do
expect(call_report[:data].map { |d| d[:topic_id] }).not_to include(topic_1.id)
end
end
end
end
Loading
Loading