FIX: Dismiss unread respects tracked query param (#10714)

FIX: Dismiss unread respects tracked query param (#10714)

  • WIP: ‘dismiss…’ respectes tracked query param

  • Address review comments

  • Dismiss new respects query params

  • Remove comment

  • Better variable name

  • remove self

diff --git a/app/assets/javascripts/discourse/app/controllers/discovery-sortable.js b/app/assets/javascripts/discourse/app/controllers/discovery-sortable.js
index 0b84ba3..7365463 100644
--- a/app/assets/javascripts/discourse/app/controllers/discovery-sortable.js
+++ b/app/assets/javascripts/discourse/app/controllers/discovery-sortable.js
@@ -39,10 +39,12 @@ export function changeSort(sortBy) {
   }
 }
 
-export function resetParams() {
+export function resetParams(skipParams = []) {
   let { controller } = this;
   controllerOpts.queryParams.forEach((p) => {
-    controller.set(p, queryParams[p].default);
+    if (!skipParams.includes(p)) {
+      controller.set(p, queryParams[p].default);
+    }
   });
 }
 
diff --git a/app/assets/javascripts/discourse/app/controllers/discovery/topics.js b/app/assets/javascripts/discourse/app/controllers/discovery/topics.js
index c344dff..b3b96a1 100644
--- a/app/assets/javascripts/discourse/app/controllers/discovery/topics.js
+++ b/app/assets/javascripts/discourse/app/controllers/discovery/topics.js
@@ -57,9 +57,9 @@ const controllerOpts = {
       return false;
     },
 
-    refresh() {
+    refresh(options = { skipResettingParams: [] }) {
       const filter = this.get("model.filter");
-      this.send("resetParams");
+      this.send("resetParams", options.skipResettingParams);
 
       // Don't refresh if we're still loading
       if (this.get("discovery.loading")) {
@@ -72,23 +72,36 @@ const controllerOpts = {
       this.set("discovery.loading", true);
 
       this.topicTrackingState.resetTracking();
+
       this.store.findFiltered("topicList", { filter }).then((list) => {
         TopicList.hideUniformCategory(list, this.category);
 
-        this.setProperties({ model: list });
-        this.resetSelected();
-
-        if (this.topicTrackingState) {
-          this.topicTrackingState.sync(list, filter);
+        // If query params are present in the current route, we need still need to sync topic
+        // tracking with the topicList without any query params. Then we set the topic
+        // list to the list filtered with query params in the afterRefresh.
+        const params = this.router.currentRoute.queryParams;
+        if (Object.keys(params).length) {
+          this.store
+            .findFiltered("topicList", { filter, params })
+            .then((listWithParams) => {
+              this.afterRefresh(filter, list, listWithParams);
+            });
+        } else {
+          this.afterRefresh(filter, list);
         }
-
-        this.send("loadingComplete");
       });
     },
 
     resetNew() {
-      Topic.resetNew(this.category, !this.noSubcategories).then(() =>
-        this.send("refresh")
+      const tracked =
+        (this.router.currentRoute.queryParams["f"] ||
+          this.router.currentRoute.queryParams["filter"]) === "tracked";
+
+      Topic.resetNew(this.category, !this.noSubcategories, tracked).then(() =>
+        this.send(
+          "refresh",
+          tracked ? { skipResettingParams: ["filter", "f"] } : {}
+        )
       );
     },
 
@@ -97,6 +110,17 @@ const controllerOpts = {
     },
   },
 
+  afterRefresh(filter, list, listModel = list) {
+    this.setProperties({ model: listModel });
+    this.resetSelected();
+
+    if (this.topicTrackingState) {
+      this.topicTrackingState.sync(list, filter);
+    }
+
+    this.send("loadingComplete");
+  },
+
   isFilterPage: function (filter, filterType) {
     if (!filter) {
       return false;
diff --git a/app/assets/javascripts/discourse/app/mixins/bulk-topic-selection.js b/app/assets/javascripts/discourse/app/mixins/bulk-topic-selection.js
index e495649..279fe79 100644
--- a/app/assets/javascripts/discourse/app/mixins/bulk-topic-selection.js
+++ b/app/assets/javascripts/discourse/app/mixins/bulk-topic-selection.js
@@ -22,22 +22,21 @@ export default Mixin.create({
     },
 
     dismissRead(operationType, options) {
-      let operation;
-      if (operationType === "posts") {
-        operation = { type: "dismiss_posts" };
-      } else {
-        operation = {
-          type: "change_notification_level",
-          notification_level_id: NotificationLevels.REGULAR,
-        };
-      }
-
-      let promise;
-      if (this.selected.length > 0) {
-        promise = Topic.bulkOperation(this.selected, operation);
-      } else {
-        promise = Topic.bulkOperationByFilter("unread", operation, options);
-      }
+      const operation =
+        operationType === "posts"
+          ? { type: "dismiss_posts" }
+          : {
+              type: "change_notification_level",
+              notification_level_id: NotificationLevels.REGULAR,
+            };
+
+      const tracked =
+        (this.router.currentRoute.queryParams["f"] ||
+          this.router.currentRoute.queryParams["filter"]) === "tracked";
+
+      const promise = this.selected.length
+        ? Topic.bulkOperation(this.selected, operation, tracked)
+        : Topic.bulkOperationByFilter("unread", operation, options, tracked);
 
       promise.then((result) => {
         if (result && result.topic_ids) {
@@ -47,7 +46,10 @@ export default Mixin.create({
         }
 
         this.send("closeModal");
-        this.send("refresh");
+        this.send(
+          "refresh",
+          tracked ? { skipResettingParams: ["filter", "f"] } : {}
+        );
       });
     },
   },
diff --git a/app/assets/javascripts/discourse/app/models/topic.js b/app/assets/javascripts/discourse/app/models/topic.js
index 39b8b26..ed9f513 100644
--- a/app/assets/javascripts/discourse/app/models/topic.js
+++ b/app/assets/javascripts/discourse/app/models/topic.js
@@ -799,18 +799,21 @@ Topic.reopenClass({
     return promise;
   },
 
-  bulkOperation(topics, operation) {
+  bulkOperation(topics, operation, tracked) {
+    const data = {
+      topic_ids: topics.mapBy("id"),
+      operation,
+      tracked,
+    };
+
     return ajax("/topics/bulk", {
       type: "PUT",
-      data: {
-        topic_ids: topics.map((t) => t.get("id")),
-        operation,
-      },
+      data,
     });
   },
 
-  bulkOperationByFilter(filter, operation, options) {
-    let data = { filter, operation };
+  bulkOperationByFilter(filter, operation, options, tracked) {
+    const data = { filter, operation, tracked };
 
     if (options) {
       if (options.categoryId) {
@@ -830,10 +833,12 @@ Topic.reopenClass({
     });
   },
 
-  resetNew(category, include_subcategories) {
-    const data = category
-      ? { category_id: category.id, include_subcategories }
-      : {};
+  resetNew(category, include_subcategories, tracked = false) {
+    const data = { tracked };
+    if (category) {
+      data.category_id = category.id;
+      data.include_subcategories = include_subcategories;
+    }
     return ajax("/topics/reset-new", { type: "PUT", data });
   },
 
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index d6fadb3..9b45385 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -845,6 +845,7 @@ class TopicsController < ApplicationController
     elsif params[:filter] == 'unread'
       tq = TopicQuery.new(current_user)
       topics = TopicQuery.unread_filter(tq.joined_topic_user, current_user.id, staff: guardian.is_staff?).listable_topics
+      topics = TopicQuery.tracked_filter(topics, current_user.id) if params[:tracked].to_s == "true"
 
       if params[:category_id]
         if params[:include_subcategories]
@@ -892,8 +893,14 @@ class TopicsController < ApplicationController
         TopicTrackingState.publish_dismiss_new(current_user.id, category_id)
       end
     else
-      current_user.user_stat.update_column(:new_since, Time.zone.now)
-      TopicTrackingState.publish_dismiss_new(current_user.id)
+      if params[:tracked].to_s == "true"
+        topics = TopicQuery.tracked_filter(TopicQuery.new(current_user).new_results, current_user.id)

[... diff too long, it was truncated ...]

GitHub sha: b8015ab6

This commit appears in #10714 which was approved by eviltrout. It was merged by markvanlan.