DEV: Refactor model/post-stream to support any filter. (#16081)

The current implementation ties the filter query params tightly to the
`summary` attribute on the post stream model making it hard to support
other filters.
This commit is contained in:
Alan Guo Xiang Tan 2022-03-03 08:50:47 +08:00 committed by GitHub
parent e3b4998efc
commit d52aa6a51b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 11 deletions

View File

@ -1,4 +1,4 @@
import { and, not, or } from "@ember/object/computed";
import { and, equal, not, or } from "@ember/object/computed";
import DiscourseURL from "discourse/lib/url";
import I18n from "I18n";
import PostsWithPlaceholders from "discourse/lib/posts-with-placeholders";
@ -37,7 +37,6 @@ export default RestModel.extend({
posts: null,
stream: null,
userFilters: null,
summary: null,
loaded: null,
loadingAbove: null,
loadingBelow: null,
@ -48,6 +47,7 @@ export default RestModel.extend({
timelineLookup: null,
filterRepliesToPostNumber: null,
filterUpwardsPostID: null,
filter: null,
init() {
this._identityMap = {};
@ -62,7 +62,6 @@ export default RestModel.extend({
postsWithPlaceholders,
stream: [],
userFilters: [],
summary: false,
filterRepliesToPostNumber:
parseInt(this.get("topic.replies_to_post_number"), 10) || false,
filterUpwardsPostID: false,
@ -78,6 +77,8 @@ export default RestModel.extend({
loading: or("loadingAbove", "loadingBelow", "loadingFilter", "stagingPost"),
notLoading: not("loading"),
summary: equal("filter", "summary"),
@discourseComputed(
"isMegaTopic",
"stream.length",
@ -137,7 +138,7 @@ export default RestModel.extend({
params for the stream.
**/
@discourseComputed(
"summary",
"filter",
"userFilters.[]",
"filterRepliesToPostNumber",
"filterUpwardsPostID"
@ -145,8 +146,8 @@ export default RestModel.extend({
streamFilters() {
const result = {};
if (this.summary) {
result.filter = "summary";
if (this.filter) {
result.filter = this.filter;
}
const userFilters = this.userFilters;
@ -235,10 +236,10 @@ export default RestModel.extend({
cancelFilter() {
this.setProperties({
userFilters: [],
summary: false,
filterRepliesToPostNumber: false,
filterUpwardsPostID: false,
mixedHiddenPosts: false,
filter: null,
});
},
@ -252,7 +253,7 @@ export default RestModel.extend({
showSummary() {
this.cancelFilter();
this.set("summary", true);
this.set("filter", "summary");
return this.refreshAndJumptoSecondVisible();
},

View File

@ -277,7 +277,7 @@ const TopicRoute = DiscourseRoute.extend({
setupParams(topic, params) {
const postStream = topic.get("postStream");
postStream.set("summary", get(params, "filter") === "summary");
postStream.set("filter", get(params, "filter"));
const usernames = get(params, "username_filters"),
userFilters = postStream.get("userFilters");

View File

@ -237,7 +237,7 @@ module("Unit | Model | post-stream", function () {
sinon.stub(postStream, "refresh").returns(Promise.resolve());
postStream.set("summary", true);
postStream.set("filter", "summary");
postStream.cancelFilter();
assert.ok(!postStream.get("summary"), "summary is cancelled");
@ -391,7 +391,7 @@ module("Unit | Model | post-stream", function () {
"there are no filters by default"
);
postStream.set("summary", true);
postStream.set("filter", "summary");
assert.deepEqual(
postStream.get("streamFilters"),
{ filter: "summary" },