From c54273b94ab597144bcb433512ce4876ac270a3b Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Thu, 25 Feb 2021 15:36:43 +0530 Subject: [PATCH 1/5] implemented a relative time based filtering for similar topics --- .../wizard-realtime-validations.js.es6 | 16 ++++++++++++++++ .../components/wizard-realtime-validations.hbs | 12 +++++++----- .../components/similar-topics-validator.js.es6 | 3 ++- assets/stylesheets/common/wizard-admin.scss | 10 ++++++++++ config/locales/client.en.yml | 7 ++++++- .../realtime_validations/similar_topics.rb | 12 +++++++++--- .../realtime_validations/similar_topics_spec.rb | 6 +++--- 7 files changed, 53 insertions(+), 13 deletions(-) diff --git a/assets/javascripts/discourse/components/wizard-realtime-validations.js.es6 b/assets/javascripts/discourse/components/wizard-realtime-validations.js.es6 index 5bafaac3..c4b35d5c 100644 --- a/assets/javascripts/discourse/components/wizard-realtime-validations.js.es6 +++ b/assets/javascripts/discourse/components/wizard-realtime-validations.js.es6 @@ -2,9 +2,25 @@ import Component from "@ember/component"; import EmberObject from "@ember/object"; import { cloneJSON } from "discourse-common/lib/object"; import Category from "discourse/models/category"; +import discourseComputed from "discourse-common/utils/decorators"; +import I18n from "I18n"; export default Component.extend({ classNames: ["realtime-validations"], + @discourseComputed + timeUnits() { + return [ + "days", + "weeks", + "months", + "years" + ].map((unit) => { + return { + id: unit, + name: I18n.t(`admin.wizard.field.validations.time_units.${unit}`) + } + }); + }, init() { this._super(...arguments); diff --git a/assets/javascripts/discourse/templates/components/wizard-realtime-validations.hbs b/assets/javascripts/discourse/templates/components/wizard-realtime-validations.hbs index fe053bc3..d4d068ac 100644 --- a/assets/javascripts/discourse/templates/components/wizard-realtime-validations.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-realtime-validations.hbs @@ -22,13 +22,15 @@
- +
- {{date-picker-past - value=(readonly props.date_after) - containerId="date-container" - onSelect=(action (mut props.date_after))}} + {{input type="number" class="time-n-value" value=props.n_value}} + {{combo-box + value=(readonly props.time_unit) + content=timeUnits + class="time-unit-selector" + onChange=(action (mut props.time_unit))}}
diff --git a/assets/javascripts/wizard/components/similar-topics-validator.js.es6 b/assets/javascripts/wizard/components/similar-topics-validator.js.es6 index 2625cdf2..d8bc952d 100644 --- a/assets/javascripts/wizard/components/similar-topics-validator.js.es6 +++ b/assets/javascripts/wizard/components/similar-topics-validator.js.es6 @@ -124,7 +124,8 @@ export default WizardFieldValidator.extend({ this.backendValidate({ title: this.get("field.value"), categories: this.get("validation.categories"), - date_after: this.get("validation.date_after"), + time_unit: this.get("validation.time_unit"), + n_value: this.get("validation.n_value") }).then((result) => { const similarTopics = A( deepMerge(result["topics"], result["similar_topics"]) diff --git a/assets/stylesheets/common/wizard-admin.scss b/assets/stylesheets/common/wizard-admin.scss index a06c19e2..389de55c 100644 --- a/assets/stylesheets/common/wizard-admin.scss +++ b/assets/stylesheets/common/wizard-admin.scss @@ -680,3 +680,13 @@ .wizard.category-selector { width: 200px !important; } + +.time-n-value { + width: 70px; + vertical-align: middle; +} + +.select-kit.time-unit-selector { + width: 80px; + vertical-align: middle; +} diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 8b3d4e66..9e008b94 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -184,7 +184,12 @@ en: above: "Above" below: "Below" categories: "Categories" - date_after: "Date After" + time_after: "Time After" + time_units: + days: "Days" + weeks: "Weeks" + months: "Months" + years: "Years" type: text: "Text" diff --git a/lib/custom_wizard/realtime_validations/similar_topics.rb b/lib/custom_wizard/realtime_validations/similar_topics.rb index 29425f5b..9ec8f60d 100644 --- a/lib/custom_wizard/realtime_validations/similar_topics.rb +++ b/lib/custom_wizard/realtime_validations/similar_topics.rb @@ -21,8 +21,9 @@ class CustomWizard::RealtimeValidation::SimilarTopics title = params[:title] raw = params[:raw] categories = params[:categories] - date_after = params[:date_after] - + n_value = params[:n_value] + time_unit = params[:time_unit] + result = CustomWizard::RealtimeValidation::Result.new(:similar_topic) if title.length < SiteSetting.min_title_similar_length || !Topic.count_exceeds_minimum? @@ -31,7 +32,12 @@ class CustomWizard::RealtimeValidation::SimilarTopics topics = Topic.similar_to(title, raw, user).to_a topics.select! { |t| categories.include?(t.category.id.to_s) } if categories.present? - topics.select! { |t| t.created_at > DateTime.parse(date_after) } if date_after.present? + + if n_value.present? and time_unit.present? + if n_value.to_i > 0 + topics.select! { |t| t.created_at >= n_value.to_i.send(time_unit).ago } + end + end topics.map! { |t| SimilarTopic.new(t) } result.items = topics diff --git a/spec/components/custom_wizard/realtime_validations/similar_topics_spec.rb b/spec/components/custom_wizard/realtime_validations/similar_topics_spec.rb index e76b7fdb..1116f019 100644 --- a/spec/components/custom_wizard/realtime_validations/similar_topics_spec.rb +++ b/spec/components/custom_wizard/realtime_validations/similar_topics_spec.rb @@ -30,12 +30,12 @@ describe ::CustomWizard::RealtimeValidation::SimilarTopics do expect(result.items.length).to eq(1) end - it "filters topics based on created date" do - topic.update!(created_at: 1.day.ago) + it "filters topics based on Time After setting" do + topic.update!(created_at: 23.hours.ago) cat_topic.update!(created_at: 2.days.ago) validation = ::CustomWizard::RealtimeValidation::SimilarTopics.new(user) - result = validation.perform({ title: "matching similar", date_after: 1.day.ago.to_date.to_s }) + result = validation.perform({ title: "matching similar", n_value: 1, time_unit: "days" }) expect(result.items.length).to eq(1) end end From bc2cc858045126b2dd5642328bbb2dbc495c35ed Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Thu, 25 Feb 2021 15:59:42 +0530 Subject: [PATCH 2/5] rename n_value to time_n_value --- .../templates/components/wizard-realtime-validations.hbs | 2 +- .../wizard/components/similar-topics-validator.js.es6 | 2 +- lib/custom_wizard/realtime_validations/similar_topics.rb | 8 ++++---- .../realtime_validations/similar_topics_spec.rb | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/assets/javascripts/discourse/templates/components/wizard-realtime-validations.hbs b/assets/javascripts/discourse/templates/components/wizard-realtime-validations.hbs index d4d068ac..4544f28a 100644 --- a/assets/javascripts/discourse/templates/components/wizard-realtime-validations.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-realtime-validations.hbs @@ -25,7 +25,7 @@
- {{input type="number" class="time-n-value" value=props.n_value}} + {{input type="number" class="time-n-value" value=props.time_n_value}} {{combo-box value=(readonly props.time_unit) content=timeUnits diff --git a/assets/javascripts/wizard/components/similar-topics-validator.js.es6 b/assets/javascripts/wizard/components/similar-topics-validator.js.es6 index d8bc952d..3b63fafb 100644 --- a/assets/javascripts/wizard/components/similar-topics-validator.js.es6 +++ b/assets/javascripts/wizard/components/similar-topics-validator.js.es6 @@ -125,7 +125,7 @@ export default WizardFieldValidator.extend({ title: this.get("field.value"), categories: this.get("validation.categories"), time_unit: this.get("validation.time_unit"), - n_value: this.get("validation.n_value") + time_n_value: this.get("validation.time_n_value") }).then((result) => { const similarTopics = A( deepMerge(result["topics"], result["similar_topics"]) diff --git a/lib/custom_wizard/realtime_validations/similar_topics.rb b/lib/custom_wizard/realtime_validations/similar_topics.rb index 9ec8f60d..117b2681 100644 --- a/lib/custom_wizard/realtime_validations/similar_topics.rb +++ b/lib/custom_wizard/realtime_validations/similar_topics.rb @@ -21,7 +21,7 @@ class CustomWizard::RealtimeValidation::SimilarTopics title = params[:title] raw = params[:raw] categories = params[:categories] - n_value = params[:n_value] + time_n_value = params[:time_n_value] time_unit = params[:time_unit] result = CustomWizard::RealtimeValidation::Result.new(:similar_topic) @@ -33,9 +33,9 @@ class CustomWizard::RealtimeValidation::SimilarTopics topics = Topic.similar_to(title, raw, user).to_a topics.select! { |t| categories.include?(t.category.id.to_s) } if categories.present? - if n_value.present? and time_unit.present? - if n_value.to_i > 0 - topics.select! { |t| t.created_at >= n_value.to_i.send(time_unit).ago } + if time_n_value.present? and time_unit.present? + if time_n_value.to_i > 0 + topics.select! { |t| t.created_at >= time_n_value.to_i.send(time_unit).ago } end end topics.map! { |t| SimilarTopic.new(t) } diff --git a/spec/components/custom_wizard/realtime_validations/similar_topics_spec.rb b/spec/components/custom_wizard/realtime_validations/similar_topics_spec.rb index 1116f019..94b17b63 100644 --- a/spec/components/custom_wizard/realtime_validations/similar_topics_spec.rb +++ b/spec/components/custom_wizard/realtime_validations/similar_topics_spec.rb @@ -35,7 +35,7 @@ describe ::CustomWizard::RealtimeValidation::SimilarTopics do cat_topic.update!(created_at: 2.days.ago) validation = ::CustomWizard::RealtimeValidation::SimilarTopics.new(user) - result = validation.perform({ title: "matching similar", n_value: 1, time_unit: "days" }) + result = validation.perform({ title: "matching similar", time_n_value: 1, time_unit: "days" }) expect(result.items.length).to eq(1) end end From 6754d744f085e026d38e9a0613dd4b1470e76596 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Thu, 25 Feb 2021 17:10:02 +0530 Subject: [PATCH 3/5] renamed setting's copy to Max Topic Age --- .../templates/components/wizard-realtime-validations.hbs | 2 +- config/locales/client.en.yml | 2 +- .../custom_wizard/realtime_validations/similar_topics_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/assets/javascripts/discourse/templates/components/wizard-realtime-validations.hbs b/assets/javascripts/discourse/templates/components/wizard-realtime-validations.hbs index 4544f28a..39734761 100644 --- a/assets/javascripts/discourse/templates/components/wizard-realtime-validations.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-realtime-validations.hbs @@ -22,7 +22,7 @@
- +
{{input type="number" class="time-n-value" value=props.time_n_value}} diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 9e008b94..47b2aa5b 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -184,7 +184,7 @@ en: above: "Above" below: "Below" categories: "Categories" - time_after: "Time After" + max_topic_age: "Max Topic Age" time_units: days: "Days" weeks: "Weeks" diff --git a/spec/components/custom_wizard/realtime_validations/similar_topics_spec.rb b/spec/components/custom_wizard/realtime_validations/similar_topics_spec.rb index 94b17b63..7d871d45 100644 --- a/spec/components/custom_wizard/realtime_validations/similar_topics_spec.rb +++ b/spec/components/custom_wizard/realtime_validations/similar_topics_spec.rb @@ -30,7 +30,7 @@ describe ::CustomWizard::RealtimeValidation::SimilarTopics do expect(result.items.length).to eq(1) end - it "filters topics based on Time After setting" do + it "filters topics based on Max Topic Age setting" do topic.update!(created_at: 23.hours.ago) cat_topic.update!(created_at: 2.days.ago) From 33e457a5c9e6ee3153a3ea69bf2fd2f4b84861a5 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Fri, 26 Feb 2021 09:46:23 +0530 Subject: [PATCH 4/5] simplified the conditional --- lib/custom_wizard/realtime_validations/similar_topics.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/custom_wizard/realtime_validations/similar_topics.rb b/lib/custom_wizard/realtime_validations/similar_topics.rb index 117b2681..189054cc 100644 --- a/lib/custom_wizard/realtime_validations/similar_topics.rb +++ b/lib/custom_wizard/realtime_validations/similar_topics.rb @@ -33,10 +33,8 @@ class CustomWizard::RealtimeValidation::SimilarTopics topics = Topic.similar_to(title, raw, user).to_a topics.select! { |t| categories.include?(t.category.id.to_s) } if categories.present? - if time_n_value.present? and time_unit.present? - if time_n_value.to_i > 0 - topics.select! { |t| t.created_at >= time_n_value.to_i.send(time_unit).ago } - end + if time_n_value.to_i > 0 && time_unit.present? + topics.select! { |t| t.created_at >= time_n_value.to_i.send(time_unit).ago } end topics.map! { |t| SimilarTopic.new(t) } From cced7a8d2512947fec7e05286283d1b7c545d358 Mon Sep 17 00:00:00 2001 From: Faizaan Gagan Date: Fri, 26 Feb 2021 09:53:39 +0530 Subject: [PATCH 5/5] minor formatting --- lib/custom_wizard/realtime_validations/similar_topics.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/custom_wizard/realtime_validations/similar_topics.rb b/lib/custom_wizard/realtime_validations/similar_topics.rb index 189054cc..3323a2ee 100644 --- a/lib/custom_wizard/realtime_validations/similar_topics.rb +++ b/lib/custom_wizard/realtime_validations/similar_topics.rb @@ -36,6 +36,7 @@ class CustomWizard::RealtimeValidation::SimilarTopics if time_n_value.to_i > 0 && time_unit.present? topics.select! { |t| t.created_at >= time_n_value.to_i.send(time_unit).ago } end + topics.map! { |t| SimilarTopic.new(t) } result.items = topics