Skip to content

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

FEATURE: Add bulk pin/unpin topics (#39025)

Alan Guo Xiang Tan5aa8ff0f0c56
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index e93bf4d182f..39465fe8e91 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -534,23 +534,26 @@ class TopicsController < ApplicationController
end
def feature_stats
- params.require(:category_id)
- category_id = params[:category_id].to_i
+ category_id = params[:category_id]&.to_i
visible_topics = Topic.listable_topics.visible.secured(guardian)
- render json: {
- pinned_in_category_count:
- visible_topics
- .where(category_id: category_id)
- .where(pinned_globally: false)
- .where.not(pinned_at: nil)
- .count,
- pinned_globally_count:
- visible_topics.where(pinned_globally: true).where.not(pinned_at: nil).count,
- banner_count:
- Topic.listable_topics.secured(guardian).where(archetype: Archetype.banner).count,
- }
+ result = {
+ pinned_globally_count:
+ visible_topics.where(pinned_globally: true).where.not(pinned_at: nil).count,
+ banner_count:
+ Topic.listable_topics.secured(guardian).where(archetype: Archetype.banner).count,
+ }
+
+ if category_id
+ result[:pinned_in_category_count] = visible_topics
+ .where(category_id: category_id)
+ .where(pinned_globally: false)
+ .where.not(pinned_at: nil)
+ .count
+ end
+
+ render json: result
end
def status
@@ -1123,6 +1126,8 @@ class TopicsController < ApplicationController
:notification_level_id,
:message,
:silent,
+ :pinned_globally,
+ :pinned_until,
*DiscoursePluginRegistry.permitted_bulk_action_parameters,
tag_ids: [],
tags: [],
@@ -1130,8 +1135,10 @@ class TopicsController < ApplicationController
.to_h
.symbolize_keys
- if operation.has_key? :silent
- operation[:silent] = ActiveModel::Type::Boolean.new.cast(operation[:silent])
+ %i[silent pinned_globally].each do |key|
+ operation[key] = ActiveModel::Type::Boolean.new.cast(operation[key]) if operation.has_key?(
+ key,
+ )
end
raise ActionController::ParameterMissing.new(:operation_type) if operation[:type].blank?
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 41287d64a41..112d3c184dc 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -3709,6 +3709,8 @@ en:
clear_all: "Clear All"
unlist_topics: "Unlist topics"
relist_topics: "Relist topics"
+ pin_topics: "Feature these topics"
+ unpin_topics: "Un-pin topics"
reset_bump_dates: "Reset bump dates"
defer: "Mark unread"
delete: "Delete topics"
@@ -3773,6 +3775,9 @@ en:
confirm_remove_tags:
one: "All tags will be removed from this topic. Are you sure?"
other: "All tags will be removed from %{count} topics. Are you sure?"
+ confirm_unpin_topics:
+ one: "Un-pin %{count} topic"
+ other: "Un-pin %{count} topics"
confirm_update_topics:
one: "Update %{count} topic"
other: "Update %{count} topics"
@@ -3878,6 +3883,11 @@ en:
update_notifications:
name: "Update Notifications"
description: "Update your personal notification level for the selected topics to:"
+ pin_topics:
+ name: "Pin"
+ unpin_topics:
+ name: "Un-pin"
+ description: "Un-pin all the selected topics that are pinned to a category or pinned globally"
topic:
filter_to:
@@ -4216,6 +4226,13 @@ en:
no_banner_exists: "There is no banner topic."
banner_exists: "There <strong class='badge badge-notification unread'>is</strong> currently a banner topic."
+ bulk_feature_topic:
+ pin: "Make these topics appear at the top of the topic's category until"
+ pin_note: "Users can unpin topics individually for themselves."
+ pin_globally: "Make these topics appear at the top of all topic lists until"
+ pin_button: "Pin topics"
+ pin_globally_button: "Pin topics globally"
+
inviting: "Inviting…"
automatically_add_to_groups: "This invite also includes access to these groups:"
diff --git a/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs b/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs
index 9a271b82606..26ecf1cd4d9 100644
--- a/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs
+++ b/frontend/discourse/app/components/bulk-select-topics-dropdown.gjs
@@ -92,6 +92,20 @@ export default class BulkSelectTopicsDropdown extends Component {
icon: "topic.closed",
name: i18n("topic_bulk_actions.close_topics.name"),
},
+ {
+ id: "pin-topics",
+ icon: "thumbtack",
+ name: i18n("topic_bulk_actions.pin_topics.name"),
+ visible: ({ topics }) => !topics.some((t) => t.isPrivateMessage),
+ },
+ {
+ id: "unpin-topics",
+ icon: "thumbtack",
+ name: i18n("topic_bulk_actions.unpin_topics.name"),
+ visible: ({ topics }) =>
+ topics.some((t) => t.pinned || t.unpinned) &&
+ !topics.some((t) => t.isPrivateMessage),
+ },
{
id: "archive-topics",
icon: "folder",
@@ -218,6 +232,7 @@ export default class BulkSelectTopicsDropdown extends Component {
allowSilent,
initialAction,
initialActionLabel,
+ showFooter: opts.showFooter !== false,
},
});
}
@@ -322,6 +337,17 @@ export default class BulkSelectTopicsDropdown extends Component {
confirmButtonTranslationKey: "topics.bulk.confirm_update_topics",
});
break;
+ case "pin-topics":
+ this.showBulkTopicActionsModal("pin", "pin_topics", {
+ showFooter: false,
+ });
+ break;
+ case "unpin-topics":
+ this.showBulkTopicActionsModal("unpin", "unpin_topics", {
+ description: i18n("topic_bulk_actions.unpin_topics.description"),
+ confirmButtonTranslationKey: "topics.bulk.confirm_unpin_topics",
+ });
+ break;
case "defer":
this.deferTopics();
break;
diff --git a/frontend/discourse/app/components/modal/bulk-topic-actions.gjs b/frontend/discourse/app/components/modal/bulk-topic-actions.gjs
index 2b1bc40cbd9..8cb35dadb75 100644
--- a/frontend/discourse/app/components/modal/bulk-topic-actions.gjs
+++ b/frontend/discourse/app/components/modal/bulk-topic-actions.gjs
@@ -10,6 +10,7 @@ import { Promise } from "rsvp";
import ConditionalLoadingSection from "discourse/components/conditional-loading-section";
import DButton from "discourse/components/d-button";
import DModal from "discourse/components/d-modal";
+import BulkPinOptions from "discourse/components/modal/feature-topic/bulk-pin-options";
import RadioButton from "discourse/components/radio-button";
import { categoryBadgeHTML } from "discourse/helpers/category-link";
import { topicLevels } from "discourse/lib/notification-levels";
@@ -157,7 +158,7 @@ export default class BulkTopicActions extends Component {
}
@action
- performAction() {
+ performAction(opts = {}) {
this.loading = true;
switch (this.model.action) {
case "close":
@@ -200,6 +201,12 @@ export default class BulkTopicActions extends Component {
t.set("unlisted", false)
);
break;
+ case "unpin":
+ this.forEachPerformed({ type: "unpin" }, (t) => t.set("pinned", false));
+ break;
+ case "pin":
+ this.performAndRefresh({ type: "pin", ...opts });
+ break;
case "append-tags":
this.performAndRefresh({
type: "append_tags",
@@ -344,6 +351,10 @@ export default class BulkTopicActions extends Component {
return this.model.action === "close";
}
+ get isPinAction() {
+ return this.model.action === "pin";
+ }
+
@action
updateCloseNote(event) {
event.preventDefault();
@@ -516,6 +527,13 @@ export default class BulkTopicActions extends Component {
}}
{{/if}}
+ {{#if this.isPinAction}}
+ <BulkPinOptions
+ @onPin={{this.performAction}}
+ @category={{this.soleCategory}}
+ />
+ {{/if}}
+
{{#if this.isCloseAction}}
<div class="bulk-close-note-section">
<label>
@@ -543,7 +561,7 @@ export default class BulkTopicActions extends Component {
class="btn-primary"
id="bulk-topics-close"
/>
- {{else}}
+ {{else if @model.showFooter}}
{{#if @model.allowSilent}}
<div class="topic-bulk-actions-options">
<label
diff --git a/frontend/discourse/app/components/modal/feature-topic.gjs b/frontend/discourse/app/components/modal/feature-topic.gjs
index ee86711807d..b8f55b11d1c 100644
--- a/frontend/discourse/app/components/modal/feature-topic.gjs
+++ b/frontend/discourse/app/components/modal/feature-topic.gjs
@@ -1,30 +1,26 @@
import Component from "@glimmer/component";
import { tracked } from "@glimmer/tracking";
import { fn } from "@ember/helper";
-import EmberObject, { action } from "@ember/object";
+import { action } from "@ember/object";
import { service } from "@ember/service";
import { trustHTML } from "@ember/template";
import ConditionalLoadingSpinner from "discourse/components/conditional-loading-spinner";
import DButton from "discourse/components/d-button";
import DModal from "discourse/components/d-modal";
-import FutureDateInput from "discourse/components/future-date-input";
-import PopupInputTip from "discourse/components/popup-input-tip";
+import PinOptionSection, {
+ MAX_GLOBALLY_PINNED_TOPICS,
+} from "discourse/components/modal/feature-topic/pin-option-section";
import { categoryLinkHTML } from "discourse/helpers/category-link";
-import icon from "discourse/helpers/d-icon";
import { ajax } from "discourse/lib/ajax";
-import { FORMAT } from "discourse/select-kit/components/future-date-input-selector";
import { i18n } from "discourse-i18n";
export default class FeatureTopic extends Component {
@service currentUser;
- @service dialog;
@tracked loading = true;
@tracked pinnedInCategoryCount = 0;
@tracked pinnedGloballyCount = 0;
@tracked bannerCount = 0;
- @tracked pinInCategoryTipShownAt = false;
- @tracked pinGloballyTipShownAt = false;
constructor() {
super(...arguments);
@@ -73,46 +69,21 @@ export default class FeatureTopic extends Component {
});
}
- get pinDisabled() {
- return !this._isDateValid(this.parsedPinnedInCategoryUntil);
- }
-
- get pinGloballyDisabled() {
- return !this._isDateValid(this.parsedPinnedGloballyUntil);
- }
-
- get parsedPinnedInCategoryUntil() {
- return this._parseDate(this.args.model.topic.pinnedInCategoryUntil);
- }
-
- get parsedPinnedGloballyUntil() {
- return this._parseDate(this.args.model.topic.pinnedGloballyUntil);
- }
-
- get pinInCategoryValidation() {
- if (this.pinDisabled) {
- return EmberObject.create({
- failed: true,
- reason: i18n("topic.feature_topic.pin_validation"),
+ get pinGloballyConfirmMessage() {
+ if (this.pinnedGloballyCount >= MAX_GLOBALLY_PINNED_TOPICS) {
+ return i18n("topic.feature_topic.confirm_pin_globally", {
+ count: this.pinnedGloballyCount,
});
}
}
- get pinGloballyValidation() {
- if (this.pinGloballyDisabled) {
- return EmberObject.create({
- failed: true,
- reason: i18n("topic.feature_topic.pin_validation"),
+ get pinGloballyStatsMessage() {
+ if (this.pinnedGloballyCount) {
+ return i18n("topic.feature_topic.already_pinned_globally", {
+ count: this.pinnedGloballyCount,
});
}
- }
-
- _parseDate(date) {
- return moment(date, FORMAT);
- }
-
- _isDateValid(parsedDate) {
- return parsedDate.isValid() && parsedDate > moment();
+ return i18n("topic.feature_topic.not_pinned_globally");
}
@action
@@ -133,40 +104,16 @@ export default class FeatureTopic extends Component {
}
}
- async _confirmBeforePinningGlobally() {
- if (this.pinnedGloballyCount < 4) {
- this.args.model.pinGlobally();
- this.args.closeModal();
- } else {
- this.dialog.yesNoConfirm({
- message: i18n("topic.feature_topic.confirm_pin_globally", {
- count: this.pinnedGloballyCount,
- }),
- didConfirm: () => {
- this.args.model.pinGlobally();
- this.args.closeModal();
- },
- });
- }
- }
-
@action
pin() {
- if (this.pinDisabled) {
- this.pinInCategoryTipShownAt = Date.now();
- } else {
- this.args.model.togglePinned();
- this.args.closeModal();
- }
+ this.args.model.togglePinned();
+ this.args.closeModal();
}
@action
pinGlobally() {
- if (this.pinGloballyDisabled) {
- this.pinGloballyTipShownAt = Date.now();
- } else {
- this._confirmBeforePinningGlobally();
- }
+ this.args.model.pinGlobally();
+ this.args.closeModal();
}
@action
@@ -239,141 +186,29 @@ export default class FeatureTopic extends Component {
</div>
</div>
{{else}}
- <div class="feature-section">
- <div class="desc">
- <p>
- <ConditionalLoadingSpinner
- @size="small"
- @condition={{this.loading}}
- >
- {{trustHTML this.alreadyPinnedMessage}}
- </ConditionalLoadingSpinner>
- </p>
- <p>
- {{i18n "topic.feature_topic.pin_note"}}
- </p>
- {{#if this.site.mobileView}}
- <p>
- {{trustHTML this.pinMessage}}
- </p>
- <p class="with-validation">
- <FutureDateInput
- class="pin-until"
- @clearable={{true}}
- @input={{@model.topic.pinnedInCategoryUntil}}
- @onChangeInput={{fn
- (mut @model.topic.pinnedInCategoryUntil)
- }}
- />
- <PopupInputTip
- @validation={{this.pinInCategoryValidation}}
- @shownAt={{this.pinInCategoryTipShownAt}}
- />
- </p>
- {{else}}
- <p class="with-validation">
- {{trustHTML this.pinMessage}}
- <span>
- {{icon "far-clock"}}
- <FutureDateInput
- class="pin-until"
- @clearable={{true}}
- @input={{@model.topic.pinnedInCategoryUntil}}
- @onChangeInput={{fn
- (mut @model.topic.pinnedInCategoryUntil)
- }}
- />
- <PopupInputTip
- @validation={{this.pinInCategoryValidation}}
- @shownAt={{this.pinInCategoryTipShownAt}}
- />
- </span>
- </p>
- {{/if}}
- <p>
- <DButton
- @action={{this.pin}}
- @icon="thumbtack"
- @label="topic.feature.pin"
- class="btn-primary"
- />
- </p>
- </div>
- </div>
+ <PinOptionSection
+ @statsMessage={{this.alreadyPinnedMessage}}
+ @loading={{this.loading}}
+ @noteMessage={{i18n "topic.feature_topic.pin_note"}}
+ @pinMessage={{this.pinMessage}}
+ @buttonLabel="topic.feature.pin"
+ @onPin={{this.pin}}
+ @dateValue={{@model.topic.pinnedInCategoryUntil}}
+ @onDateChange={{fn (mut @model.topic.pinnedInCategoryUntil)}}
+ />
{{#if this.canPinGlobally}}
<hr />
- <div class="feature-section">
- <div class="desc">
- <p>
- <ConditionalLoadingSpinner
- @size="small"
- @condition={{this.loading}}
- >
- {{#if this.pinnedGloballyCount}}
- {{trustHTML
- (i18n
- "topic.feature_topic.already_pinned_globally"
- count=this.pinnedGloballyCount
- )
- }}
- {{else}}
- {{trustHTML
- (i18n "topic.feature_topic.not_pinned_globally")
- }}
- {{/if}}
- </ConditionalLoadingSpinner>
- </p>
- <p>
- {{i18n "topic.feature_topic.global_pin_note"}}
- </p>
- {{#if this.site.mobileView}}
- <p>
- {{i18n "topic.feature_topic.pin_globally"}}
- </p>
- <p class="with-validation">
- <FutureDateInput
- class="pin-until"
- @clearable={{true}}
- @input={{@model.topic.pinnedGloballyUntil}}
- @onChangeInput={{fn
- (mut @model.topic.pinnedGloballyUntil)
- }}
- />
- <PopupInputTip
- @validation={{this.pinGloballyValidation}}
- @shownAt={{this.pinGloballyTipShownAt}}
- />
- </p>
- {{else}}
- <p class="with-validation">
- {{i18n "topic.feature_topic.pin_globally"}}
- <span>
- {{icon "far-clock"}}
- <FutureDateInput
- class="pin-until"
- @clearable={{true}}
- @input={{@model.topic.pinnedGloballyUntil}}
- @onChangeInput={{fn
- (mut @model.topic.pinnedGloballyUntil)
- }}
- />
- <PopupInputTip
- @validation={{this.pinGloballyValidation}}
- @shownAt={{this.pinGloballyTipShownAt}}
- />
- </span>
- </p>
- {{/if}}
- <p>
- <DButton
- @action={{this.pinGlobally}}
- @icon="thumbtack"
- @label="topic.feature.pin_globally"
- class="btn-primary"
- />
- </p>
- </div>
- </div>
+ <PinOptionSection
+ @statsMessage={{this.pinGloballyStatsMessage}}
+ @loading={{this.loading}}
+ @noteMessage={{i18n "topic.feature_topic.global_pin_note"}}
+ @pinMessage={{i18n "topic.feature_topic.pin_globally"}}
+ @buttonLabel="topic.feature.pin_globally"
+ @onPin={{this.pinGlobally}}
+ @dateValue={{@model.topic.pinnedGloballyUntil}}
+ @onDateChange={{fn (mut @model.topic.pinnedGloballyUntil)}}
+ @confirmMessage={{this.pinGloballyConfirmMessage}}
+ />
{{/if}}
{{/if}}
<hr />
diff --git a/frontend/discourse/app/components/modal/feature-topic/bulk-pin-options.gjs b/frontend/discourse/app/components/modal/feature-topic/bulk-pin-options.gjs
new file mode 100644
index 00000000000..4209abe583b
--- /dev/null
+++ b/frontend/discourse/app/components/modal/feature-topic/bulk-pin-options.gjs
@@ -0,0 +1,137 @@
+import Component from "@glimmer/component";
+import { tracked } from "@glimmer/tracking";
+import { action } from "@ember/object";
+import { service } from "@ember/service";
+import PinOptionSection, {
+ MAX_GLOBALLY_PINNED_TOPICS,
+} from "discourse/components/modal/feature-topic/pin-option-section";
+import { categoryLinkHTML } from "discourse/helpers/category-link";
+import { ajax } from "discourse/lib/ajax";
+import { i18n } from "discourse-i18n";
+
+export default class BulkPinOptions extends Component {
+ @service currentUser;
+
+ @tracked loading = true;
+ @tracked pinnedInCategoryCount = 0;
+ @tracked pinnedGloballyCount = 0;
+ @tracked pinnedInCategoryUntil = null;
+ @tracked pinnedGloballyUntil = null;
+
+ constructor() {
+ super(...arguments);
+ this.loadFeatureStats();
+ }
+
+ get categoryLink() {
+ if (this.args.category) {
+ return categoryLinkHTML(this.args.category, { allowUncategorized: true });
+ }
+ }
+
+ get pinnedInCategoryStatsMessage() {
+ if (!this.args.category) {
+ return;
+ }
+
+ const key =
+ this.pinnedInCategoryCount === 0
+ ? "topic.feature_topic.not_pinned"
+ : "topic.feature_topic.already_pinned";
+
+ return i18n(key, {
+ categoryLink: this.categoryLink,
+ count: this.pinnedInCategoryCount,
+ });
+ }
+
+ get pinGloballyConfirmMessage() {
+ if (this.pinnedGloballyCount >= MAX_GLOBALLY_PINNED_TOPICS) {
+ return i18n("topic.feature_topic.confirm_pin_globally", {
+ count: this.pinnedGloballyCount,
+ });
+ }
+ }
+
+ get pinnedGloballyStatsMessage() {
+ if (this.pinnedGloballyCount) {
+ return i18n("topic.feature_topic.already_pinned_globally", {
+ count: this.pinnedGloballyCount,
+ });
+ }
+ return i18n("topic.feature_topic.not_pinned_globally");
+ }
+
+ async loadFeatureStats() {
+ try {
+ const data = {};
+
+ if (this.args.category) {
+ data.category_id = this.args.category.id;
+ }
+
+ const result = await ajax("/topics/feature_stats.json", { data });
+
+ if (result) {
+ this.pinnedInCategoryCount = result.pinned_in_category_count;
+ this.pinnedGloballyCount = result.pinned_globally_count;
+ }
+ } finally {
+ this.loading = false;
+ }
+ }
+
+ @action
+ pinInCategory() {
+ this.args.onPin({
+ pinned_globally: false,
+ pinned_until: this.pinnedInCategoryUntil,
+ });
+ }
+
+ @action
+ pinGlobally() {
+ this.args.onPin({
+ pinned_globally: true,
+ pinned_until: this.pinnedGloballyUntil,
+ });
+ }
+
+ @action
+ updatePinnedInCategoryUntil(date) {
+ this.pinnedInCategoryUntil = date;
+ }
+
+ @action
+ updatePinnedGloballyUntil(date) {
+ this.pinnedGloballyUntil = date;
+ }
+
+ <template>
+ <PinOptionSection
+ @statsMessage={{this.pinnedInCategoryStatsMessage}}
+ @loading={{this.loading}}
+ @noteMessage={{i18n "topic.bulk_feature_topic.pin_note"}}
+ @pinMessage={{i18n "topic.bulk_feature_topic.pin"}}
+ @buttonLabel="topic.bulk_feature_topic.pin_button"
+ @onPin={{this.pinInCategory}}
+ @dateValue={{this.pinnedInCategoryUntil}}
+ @onDateChange={{this.updatePinnedInCategoryUntil}}
+ />
+
+ {{#if this.currentUser.canManageTopic}}
+ <hr />
+ <PinOptionSection
+ @statsMessage={{this.pinnedGloballyStatsMessage}}
+ @loading={{this.loading}}
+ @noteMessage={{i18n "topic.bulk_feature_topic.pin_note"}}
+ @pinMessage={{i18n "topic.bulk_feature_topic.pin_globally"}}
+ @buttonLabel="topic.bulk_feature_topic.pin_globally_button"
+ @onPin={{this.pinGlobally}}
+ @dateValue={{this.pinnedGloballyUntil}}
+ @onDateChange={{this.updatePinnedGloballyUntil}}
+ @confirmMessage={{this.pinGloballyConfirmMessage}}
+ />
+ {{/if}}
+ </template>
+}
diff --git a/frontend/discourse/app/components/modal/feature-topic/pin-option-section.gjs b/frontend/discourse/app/components/modal/feature-topic/pin-option-section.gjs
new file mode 100644
index 00000000000..d4dddcb6e11
--- /dev/null
+++ b/frontend/discourse/app/components/modal/feature-topic/pin-option-section.gjs
@@ -0,0 +1,111 @@
+import Component from "@glimmer/component";
+import { tracked } from "@glimmer/tracking";
+import { action } from "@ember/object";
+import { service } from "@ember/service";
+import { trustHTML } from "@ember/template";
+import ConditionalLoadingSpinner from "discourse/components/conditional-loading-spinner";
+import DButton from "discourse/components/d-button";
+import FutureDateInput from "discourse/components/future-date-input";
+import PopupInputTip from "discourse/components/popup-input-tip";
+import icon from "discourse/helpers/d-icon";
+import { FORMAT } from "discourse/select-kit/components/future-date-input-selector";
+import { i18n } from "discourse-i18n";
+
+export const MAX_GLOBALLY_PINNED_TOPICS = 4;
+
+export default class PinOptionSection extends Component {
+ @service dialog;
+ @service site;
+
+ @tracked tipShownAt = false;
+
+ get validation() {
+ if (!this._isDateValid()) {
+ return {
+ failed: true,
+ reason: i18n("topic.feature_topic.pin_validation"),
+ };
+ }
+ }
+
+ _isDateValid() {
+ const parsed = moment(this.args.dateValue, FORMAT);
+ return parsed.isValid() && parsed > moment();
+ }
+
+ @action
+ pin() {
+ if (!this._isDateValid()) {
+ this.tipShownAt = Date.now();
+ return;
+ }
+
+ if (this.args.confirmMessage) {
+ this.dialog.yesNoConfirm({
+ message: this.args.confirmMessage,
+ didConfirm: () => this.args.onPin(),
+ });
+ } else {
+ this.args.onPin();
+ }
+ }
+
+ <template>
+ <div class="feature-section">
+ <div class="desc">
+ {{#if @statsMessage}}
+ <p>
+ <ConditionalLoadingSpinner @size="small" @condition={{@loading}}>
+ {{trustHTML @statsMessage}}
+ </ConditionalLoadingSpinner>
+ </p>
+ {{/if}}
+
+ <p>{{@noteMessage}}</p>
+
+ {{#if this.site.mobileView}}
+ <p>{{trustHTML @pinMessage}}</p>
+
+ <p class="with-validation">
+ <FutureDateInput
+ class="pin-until"
+ @clearable={{true}}
+ @input={{@dateValue}}
+ @onChangeInput={{@onDateChange}}
+ />
+ <PopupInputTip
+ @validation={{this.validation}}
+ @shownAt={{this.tipShownAt}}
+ />
+ </p>
+ {{else}}
+ <p class="with-validation">
+ {{trustHTML @pinMessage}}
+ <span>
+ {{icon "far-clock"}}
+ <FutureDateInput
+ class="pin-until"
+ @clearable={{true}}
+ @input={{@dateValue}}
+ @onChangeInput={{@onDateChange}}
+ />
+ <PopupInputTip
+ @validation={{this.validation}}
+ @shownAt={{this.tipShownAt}}
+ />
+ </span>
+ </p>
+ {{/if}}
+
+ <p>
+ <DButton
+ @action={{this.pin}}
+ @icon="thumbtack"
+ @label={{@buttonLabel}}
+ class="btn-primary"
+ />
+ </p>
+ </div>
+ </div>
+ </template>
+}
diff --git a/lib/topics_bulk_action.rb b/lib/topics_bulk_action.rb
index 3264283495e..e49c2e1362c 100644
--- a/lib/topics_bulk_action.rb
+++ b/lib/topics_bulk_action.rb
@@ -30,6 +30,8 @@ class TopicsBulkAction
relist
dismiss_topics
reset_bump_dates
+ pin
+ unpin
]
end
@@ -232,6 +234,26 @@ class TopicsBulkAction
end
end
+ def pin
+ status = @operation[:pinned_globally] ? "pinned_globally" : "pinned"
+ topics.each do |t|
+ if guardian.can_moderate?(t)
+ t.update_status(status, true, @user, until: @operation[:pinned_until])
+ @changed_ids << t.id
+ end
+ end
+ end
+
+ def unpin
+ topics.each do |t|
+ if guardian.can_moderate?(t) && t.pinned_at
+ status = t.pinned_globally ? "pinned_globally" : "pinned"
+ t.update_status(status, false, @user)
+ @changed_ids << t.id
+ end
+ end
+ end
+
def archive
topics.each do |t|
if guardian.can_moderate?(t)
diff --git a/spec/lib/topics_bulk_action_spec.rb b/spec/lib/topics_bulk_action_spec.rb
index 43ea3e418e5..fa65bdf9f43 100644
--- a/spec/lib/topics_bulk_action_spec.rb
+++ b/spec/lib/topics_bulk_action_spec.rb
@@ -503,6 +503,97 @@ RSpec.describe TopicsBulkAction do
end
end
+ describe "#pin" do
+ fab!(:moderator)
+ fab!(:topic_2, :topic)
+
+ it "pins topics in category when user can moderate" do
+ topic_ids =
+ TopicsBulkAction.new(
+ moderator,
+ [topic.id, topic_2.id],
+ type: "pin",
+ pinned_globally: false,
+ ).perform!
+
+ expect(topic_ids).to contain_exactly(topic.id, topic_2.id)
+ expect(topic.reload.pinned_at).to be_present
+ expect(topic.pinned_globally).to eq(false)
+ expect(topic_2.reload.pinned_at).to be_present
+ expect(topic_2.pinned_globally).to eq(false)
+ end
+
+ it "pins topics globally with pinned_until when provided" do
+ pinned_until = 3.days.from_now.beginning_of_minute.to_s
+
+ topic_ids =
+ TopicsBulkAction.new(
+ moderator,
+ [topic.id, topic_2.id],
+ type: "pin",
+ pinned_globally: true,
+ pinned_until: pinned_until,
+ ).perform!
+
+ expect(topic_ids).to contain_exactly(topic.id, topic_2.id)
+ expect(topic.reload.pinned_globally).to eq(true)
+ expect(topic.pinned_until).to be_within_one_second_of(Time.parse(pinned_until))
+ expect(topic_2.reload.pinned_globally).to eq(true)
+ expect(topic_2.pinned_until).to be_within_one_second_of(Time.parse(pinned_until))
+ end
+
+ it "does not pin when user cannot moderate" do
+ topic_ids =
+ TopicsBulkAction.new(
+ user,
+ [topic.id, topic_2.id],
+ type: "pin",
+ pinned_globally: false,
+ ).perform!
+
+ expect(topic_ids).to eq([])
+ expect(topic.reload.pinned_at).to be_nil
+ expect(topic_2.reload.pinned_at).to be_nil
+ end
+ end
+
+ describe "#unpin" do
+ fab!(:moderator)
+ fab!(:topic_2, :topic)
+
+ it "unpins multiple pinned topics and clears pinned_until when user can moderate" do
+ topic.update_pinned(true, false, 3.days.from_now.to_s)
+ topic_2.update_pinned(true, true, 5.days.from_now.to_s)
+
+ topic_ids = TopicsBulkAction.new(moderator, [topic.id, topic_2.id], type: "unpin").perform!
+
+ expect(topic_ids).to contain_exactly(topic.id, topic_2.id)
+ expect(topic.reload.pinned_at).to be_nil
+ expect(topic.pinned_until).to be_nil
+ expect(topic_2.reload.pinned_at).to be_nil
+ expect(topic_2.pinned_until).to be_nil
+ end
+
+ it "skips topics that are not pinned" do
+ topic.update_pinned(true, false)
+
+ topic_ids = TopicsBulkAction.new(moderator, [topic.id, topic_2.id], type: "unpin").perform!
+
+ expect(topic_ids).to eq([topic.id])
+ end
+
+ it "does not unpin when user cannot moderate" do
+ topic.update_pinned(true, false)
+ topic_2.update_pinned(true, false)
+
+ topic_ids = TopicsBulkAction.new(user, [topic.id, topic_2.id], type: "unpin").perform!
+
+ expect(topic_ids).to eq([])
+ expect(topic.reload.pinned_at).to be_present
+ expect(topic_2.reload.pinned_at).to be_present
+ end
+ end
+
describe "change_tags" do
fab!(:first_post) { Fabricate(:post, topic:) }
fab!(:tag1, :tag)
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 661d387766b..6d984a6c769 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -4460,6 +4460,8 @@ RSpec.describe TopicsController do
end
describe "when logged in" do
+ fab!(:topic_2, :topic)
+
before { sign_in(user) }
let!(:operation) { { type: "change_category", category_id: "1", silent: true } }
let!(:topic_ids) { [1, 2, 3] }
@@ -4801,6 +4803,43 @@ RSpec.describe TopicsController do
expect(response.parsed_body["errors"]).to eq(nil)
end
+ it "can pin multiple topics with pinned_until" do
+ sign_in(moderator)
+ pinned_until = 3.days.from_now.beginning_of_minute.iso8601
+
+ put "/topics/bulk.json",
+ params: {
+ topic_ids: [topic.id, topic_2.id],
+ operation: {
+ type: "pin",
+ pinned_globally: false,
+ pinned_until: pinned_until,
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["topic_ids"]).to contain_exactly(topic.id, topic_2.id)
+ expect(topic.reload.pinned_until).to be_within_one_second_of(Time.parse(pinned_until))
+ expect(topic_2.reload.pinned_until).to be_within_one_second_of(Time.parse(pinned_until))
+ end
+
+ it "can unpin multiple topics" do
+ sign_in(moderator)
+ topic.update_pinned(true, true)
+ topic_2.update_pinned(true, false)
+
+ put "/topics/bulk.json",
+ params: {
+ topic_ids: [topic.id, topic_2.id],
+ operation: {
+ type: "unpin",
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["topic_ids"]).to contain_exactly(topic.id, topic_2.id)
+ end
+
it "respects the tracked parameter" do
# untracked topic
CategoryUser.set_notification_level_for_category(
@@ -5490,20 +5529,37 @@ RSpec.describe TopicsController do
end
describe "#feature_stats" do
- it "works" do
- get "/topics/feature_stats.json", params: { category_id: 1 }
+ fab!(:category_for_stats, :category)
+ fab!(:pinned_in_category_topic) do
+ Fabricate(:topic, category: category_for_stats, pinned_at: 1.hour.ago, pinned_globally: false)
+ end
+ fab!(:globally_pinned_topic) { Fabricate(:topic, pinned_at: 1.hour.ago, pinned_globally: true) }
+ fab!(:banner_topic) { Fabricate(:topic, archetype: Archetype.banner) }
+
+ it "returns category and global pin counts when category_id is provided" do
+ get "/topics/feature_stats.json", params: { category_id: category_for_stats.id }
expect(response.status).to eq(200)
json = response.parsed_body
- expect(json["pinned_in_category_count"]).to eq(0)
- expect(json["pinned_globally_count"]).to eq(0)
- expect(json["banner_count"]).to eq(0)
+ expect(json["pinned_in_category_count"]).to eq(1)
+ expect(json["pinned_globally_count"]).to eq(1)
+ expect(json["banner_count"]).to eq(1)
+ end
+
+ it "returns only global pin and banner counts when category_id is omitted" do
+ get "/topics/feature_stats.json"
+
+ expect(response.status).to eq(200)
+ json = response.parsed_body
+ expect(json).not_to have_key("pinned_in_category_count")
+ expect(json["pinned_globally_count"]).to eq(1)
+ expect(json["banner_count"]).to eq(1)
end
it "allows unlisted banner topic" do
- Fabricate(:topic, category_id: 1, archetype: Archetype.banner, visible: false)
+ banner_topic.update!(visible: false)
- get "/topics/feature_stats.json", params: { category_id: 1 }
+ get "/topics/feature_stats.json", params: { category_id: category_for_stats.id }
json = response.parsed_body
expect(json["banner_count"]).to eq(1)
end
@@ -5523,8 +5579,8 @@ RSpec.describe TopicsController do
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["pinned_in_category_count"]).to eq(0)
- expect(json["pinned_globally_count"]).to eq(0)
- expect(json["banner_count"]).to eq(0)
+ expect(json["pinned_globally_count"]).to eq(1)
+ expect(json["banner_count"]).to eq(1)
end
end
diff --git a/spec/system/bulk_pin_unpin_topics_spec.rb b/spec/system/bulk_pin_unpin_topics_spec.rb
new file mode 100644
index 00000000000..366dbacc0cf
--- /dev/null
+++ b/spec/system/bulk_pin_unpin_topics_spec.rb
@@ -0,0 +1,89 @@
+# frozen_string_literal: true
+
+describe "Bulk pin and unpin topics" do
+ fab!(:admin)
+ fab!(:category)
+ fab!(:topic_1) { Fabricate(:topic, category: category) }
+ fab!(:topic_2) { Fabricate(:topic, category: category) }
+ fab!(:post_1) { Fabricate(:post, topic: topic_1) }
+ fab!(:post_2) { Fabricate(:post, topic: topic_2) }
+
+ let(:topic_list_header) { PageObjects::Components::TopicListHeader.new }
+ let(:topic_list) { PageObjects::Components::TopicList.new }
+ let(:topic_bulk_actions_modal) { PageObjects::Modals::TopicBulkActions.new }
+ let(:toasts) { PageObjects::Components::Toasts.new }
+
+ before { sign_in(admin) }
+
+ it "allows a user to pin and unpin multiple topics in bulk" do
+ visit("/latest")
+
+ expect(topic_list).to have_no_pinned_status(topic_1)
+ expect(topic_list).to have_no_pinned_status(topic_2)
+
+ topic_list_header.click_bulk_select_button
+ topic_list.click_topic_checkbox(topic_1)
+ topic_list.click_topic_checkbox(topic_2)
+ topic_list_header.click_bulk_select_topics_dropdown
+ topic_list_header.click_bulk_button("pin-topics")
+
+ topic_bulk_actions_modal.pin_in_category_date_selector.expand
+ topic_bulk_actions_modal.pin_in_category_date_selector.select_row_by_value("next_month")
+ topic_bulk_actions_modal.click_pin_in_category
+
+ expect(toasts).to have_success(I18n.t("js.topics.bulk.completed"))
+
+ visit("/latest")
+
+ expect(topic_list).to have_pinned_status(topic_1)
+ expect(topic_list).to have_pinned_status(topic_2)
+
+ topic_list_header.click_bulk_select_button
+ topic_list.click_topic_checkbox(topic_1)
+ topic_list.click_topic_checkbox(topic_2)
+ topic_list_header.click_bulk_select_topics_dropdown
+ topic_list_header.click_bulk_button("unpin-topics")
+
+ topic_bulk_actions_modal.click_bulk_topics_confirm
+
+ expect(toasts).to have_success(I18n.t("js.topics.bulk.completed"))
+
+ visit("/latest")
+
+ expect(topic_list).to have_no_pinned_status(topic_1)
+ expect(topic_list).to have_no_pinned_status(topic_2)
+ end
+
+ it "displays pin counts and confirms before pinning globally when many are already pinned" do
+ topic_1.update_pinned(true, false)
+
+ 4.times do
+ t = Fabricate(:topic)
+ Fabricate(:post, topic: t)
+ t.update_pinned(true, true)
+ end
+
+ dialog = PageObjects::Components::Dialog.new
+
+ visit("/c/#{category.id}")
+
+ topic_list_header.click_bulk_select_button
+ topic_list.click_topic_checkbox(topic_2)
+ topic_list_header.click_bulk_select_topics_dropdown
+ topic_list_header.click_bulk_button("pin-topics")
+
+ expect(topic_bulk_actions_modal).to have_pin_stats_text(
+ "Topics currently pinned in #{category.name} : 1",
+ )
+ expect(topic_bulk_actions_modal).to have_pin_stats_text("Topics currently pinned globally: 4")
+
+ topic_bulk_actions_modal.pin_globally_date_selector.expand
+ topic_bulk_actions_modal.pin_globally_date_selector.select_row_by_value("next_month")
+ topic_bulk_actions_modal.click_pin_globally
+
+ expect(dialog).to have_content(I18n.t("js.topic.feature_topic.confirm_pin_globally", count: 4))
+ dialog.click_yes
+
+ expect(toasts).to have_success(I18n.t("js.topics.bulk.completed"))
+ end
+end
diff --git a/spec/system/page_objects/components/topic_list.rb b/spec/system/page_objects/components/topic_list.rb
index bafaa8b9caa..e8563a8ca84 100644
--- a/spec/system/page_objects/components/topic_list.rb
+++ b/spec/system/page_objects/components/topic_list.rb
@@ -132,6 +132,14 @@ module PageObjects
find(".show-more.has-topics").click
end
+ def has_pinned_status?(topic)
+ page.has_css?("#{topic_list_item_class(topic)} .topic-statuses .topic-status.--pinned")
+ end
+
+ def has_no_pinned_status?(topic)
+ page.has_no_css?("#{topic_list_item_class(topic)} .topic-statuses .topic-status.--pinned")
+ end
+
private
def topic_list_item_closed(topic)
diff --git a/spec/system/page_objects/modals/topic_bulk_actions.rb b/spec/system/page_objects/modals/topic_bulk_actions.rb
index 5cc7132251c..8c64198aac8 100644
--- a/spec/system/page_objects/modals/topic_bulk_actions.rb
+++ b/spec/system/page_objects/modals/topic_bulk_actions.rb
@@ -38,6 +38,30 @@ module PageObjects
end
end
+ def pin_in_category_date_selector
+ PageObjects::Components::SelectKit.new(
+ "#{MODAL_SELECTOR} .feature-section:first-of-type .future-date-input-selector",
+ )
+ end
+
+ def click_pin_in_category
+ find("#{MODAL_SELECTOR} .feature-section:first-of-type .btn-primary").click
+ end
+
+ def pin_globally_date_selector
+ PageObjects::Components::SelectKit.new(
+ "#{MODAL_SELECTOR} .feature-section:last-of-type .future-date-input-selector",
+ )
+ end
+
+ def click_pin_globally
+ find("#{MODAL_SELECTOR} .feature-section:last-of-type .btn-primary").click
+ end
+
+ def has_pin_stats_text?(text)
+ page.has_css?("#{MODAL_SELECTOR} .feature-section", text: text, normalize_ws: true)
+ end
+
def has_no_category_badge?(category)
within(MODAL_SELECTOR) do
has_no_css?(PageObjects::Components::CategoryBadge.new.category_selector(category))