Skip to content

A platform for community discussion. Free, open, simple.

FIX: Only count the user's own words in Rewind Top Words

Régis Hanol4db8bb53bfc8
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/top_words.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/top_words.rb
index b8a2d70c8c3..1d031cbd088 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/top_words.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/top_words.rb
@@ -27,9 +27,11 @@ module DiscourseRewind
WORD_PATTERN = "^[^[:digit:][:punct:][:space:]]{2,}$"
WORD_COUNT = 5
CANDIDATE_COUNT = 100
+ QUOTES_REGEX = "\\[quote[^\\]]*\\](?:[^\\[]|\\[(?!/quote\\]))*\\[/quote\\]"
+ CODE_REGEX = "```(?:[^`]|`(?!``))*```|`[^`]*`"
LINKS_REGEX = "https?://[^\\s]+"
- RAW_TSVECTOR_SQL =
- "to_tsvector('#{SIMPLE_TS_CONFIG}', regexp_replace(raw, '#{LINKS_REGEX}', ' ', 'g'))"
+ STRIPPED_RAW_TSVECTOR_SQL =
+ "to_tsvector('#{SIMPLE_TS_CONFIG}', regexp_replace(raw, '#{QUOTES_REGEX}|#{CODE_REGEX}|#{LINKS_REGEX}', ' ', 'g'))"
FakeData = {
data: [
@@ -52,6 +54,15 @@ module DiscourseRewind
private
+ def own_search_data_sql
+ <<~SQL.squish
+ ts_filter(
+ post_search_data.search_data,
+ CASE WHEN topics.user_id = #{user.id} AND posts.post_number = 1 THEN '{a,d}' ELSE '{d}' END::"char"[]
+ )
+ SQL
+ end
+
def word_query
posts = self.class.publicly_visible_posts.where(user_id: user.id, created_at: date)
stem = "strip(to_tsvector('#{Search.ts_config}', #{Search.wrap_unaccent("word")}))"
@@ -62,7 +73,7 @@ module DiscourseRewind
word, ndoc, nentry
FROM
ts_stat($INNERSQL$
- #{posts.joins(:post_search_data).select(:search_data).to_sql}
+ #{posts.joins(:post_search_data).select(own_search_data_sql).to_sql}
$INNERSQL$)
WHERE
word ~ '#{WORD_PATTERN}'
@@ -79,7 +90,7 @@ module DiscourseRewind
word AS original_word
FROM
ts_stat($INNERSQL$
- #{posts.select(RAW_TSVECTOR_SQL).to_sql}
+ #{posts.select(STRIPPED_RAW_TSVECTOR_SQL).to_sql}
$INNERSQL$)
WHERE
word ~ '#{WORD_PATTERN}'
diff --git a/plugins/discourse-rewind/spec/actions/top_words_spec.rb b/plugins/discourse-rewind/spec/actions/top_words_spec.rb
index 493b10c1ce9..97f3282a4c3 100644
--- a/plugins/discourse-rewind/spec/actions/top_words_spec.rb
+++ b/plugins/discourse-rewind/spec/actions/top_words_spec.rb
@@ -42,6 +42,47 @@ RSpec.describe DiscourseRewind::Action::TopWords do
expect(call_report[:data].map { |word| word[:word] }).to include("releases")
end
+ it "counts the title of the user's own topics, but not of other people's" do
+ own_topic = Fabricate(:topic, user:, title: "Everything about kiwi fruit")
+ other_topic = Fabricate(:topic, title: "Everything about melon fruit")
+ [own_topic, other_topic].each do |topic|
+ post = Fabricate(:post, user:, topic:, raw: "kiwi melon " * 5, created_at: random_datetime)
+ index(post)
+ end
+ reply = Fabricate(:post, user:, topic: own_topic, raw: "grape", created_at: random_datetime)
+ index(reply)
+
+ expect(call_report[:data].first(2)).to eq(
+ [{ word: "kiwi", score: 13 }, { word: "melon", score: 12 }],
+ )
+ end
+
+ it "ignores words that are only quoted or in code blocks" do
+ raw = <<~RAW
+ [quote="someone, post:1, topic:2"]
+ quoted quoted quoted quoted
+ [/quote]
+ mine mine mine mine
+ [quote="someone, post:1, topic:2"]
+ quoted
+ [/quote]
+ ```
+ codeword codeword codeword codeword
+ ```
+ lemon lemon lemon lemon
+ ```
+ codeword
+ ```
+ RAW
+ post = Fabricate(:post, user:, raw:, created_at: random_datetime)
+ index(post)
+
+ words = call_report[:data].map { |word| word[:word] }
+
+ expect(words).to include("mine", "lemon")
+ expect(words).not_to include("quoted", "codeword")
+ end
+
it "ignores link and domain words" do
post = Fabricate(:post, user:, raw: "github github github", created_at: random_datetime)
index(post)