A platform for community discussion. Free, open, simple.
DEV: Minor cleanup of PostStream model (#36537)
Jarek Radosz095f527c3cc0
diff --git a/frontend/discourse/app/models/post-stream.js b/frontend/discourse/app/models/post-stream.jsindex dd953a6050f..f86e234ebd7 100644--- a/frontend/discourse/app/models/post-stream.js+++ b/frontend/discourse/app/models/post-stream.js@@ -154,9 +154,8 @@ export default class PostStream extends RestModel {result.filter = this.filter;}- const userFilters = this.userFilters;- if (!isEmpty(userFilters)) {- result.username_filters = userFilters.join(",");+ if (!isEmpty(this.userFilters)) {+ result.username_filters = this.userFilters.join(",");}if (this.filterRepliesToPostNumber) {@@ -172,10 +171,10 @@ export default class PostStream extends RestModel {@dependentKeyCompatget hasNoFilters() {- const streamFilters = this.streamFilters;- return !(- streamFilters &&- (streamFilters.filter === "summary" || streamFilters.username_filters)+ return (+ !this.streamFilters ||+ (this.streamFilters.filter !== "summary" &&+ !this.streamFilters.username_filters));}@@ -223,7 +222,6 @@ export default class PostStream extends RestModel {}// Find the index of the last post loaded, if not found, bail- const stream = this.stream;const lastIndex = this.indexOf(lastLoadedPost);if (lastIndex === -1) {return [];@@ -233,22 +231,26 @@ export default class PostStream extends RestModel {}// find our window of posts- return stream.slice(lastIndex + 1, lastIndex + this.topic.chunk_size + 1);+ return this.stream.slice(+ lastIndex + 1,+ lastIndex + this.topic.chunk_size + 1+ );}cancelFilter() {+ this.streamFilters.mixedHiddenPosts = false;+this.setProperties({userFilters: [],filterRepliesToPostNumber: false,filterUpwardsPostID: false,- mixedHiddenPosts: false,filter: null,});}refreshAndJumpToSecondVisible() {return this.refresh({}).then(() => {- if (this.posts && this.posts.length > 1) {+ if (this.posts?.length > 1) {DiscourseURL.jumpToPost(this.posts[1].post_number);}});@@ -289,31 +291,26 @@ export default class PostStream extends RestModel {? element.getBoundingClientRect().top: null;- DiscourseURL.jumpToPost(postNumber, {- originalTopOffset,- });+ DiscourseURL.jumpToPost(postNumber, { originalTopOffset });- schedule("afterRender", () => {- highlightPost(postNumber);- });+ schedule("afterRender", () => highlightPost(postNumber));});}- filterUpwards(postID) {+ filterUpwards(postId) {this.cancelFilter();- this.filterUpwardsPostID = postID;+ this.filterUpwardsPostID = postId;this.appEvents.trigger("post-stream:filter-upwards", {topic_id: this.topic.id,- post_id: postID,+ post_id: postId,});+return this.refresh({ refreshInPlace: true }).then(() => {- if (this.posts && this.posts.length > 1) {+ if (this.posts?.length > 1) {const postNumber = this.posts[1].post_number;DiscourseURL.jumpToPost(postNumber, { skipIfOnScreen: true });- schedule("afterRender", () => {- highlightPost(postNumber);- });+ schedule("afterRender", () => highlightPost(postNumber));}});}@@ -323,7 +320,7 @@ export default class PostStream extends RestModel {is already loaded, it will simply scroll there and load nothing.**/refresh(opts) {- opts = opts || {};+ opts ||= {};if (opts.nearPost) {opts.nearPost = parseInt(opts.nearPost, 10);}@@ -333,8 +330,6 @@ export default class PostStream extends RestModel {delete opts.cancelFilter;}- const topic = this.topic;-// Do we already have the post in our list of posts? Jump there.if (opts.forceLoad) {this.loaded = false;@@ -356,7 +351,7 @@ export default class PostStream extends RestModel {opts = deepMerge(opts, this.streamFilters);// Request a topicView- return loadTopicView(topic, opts)+ return loadTopicView(this.topic, opts).then((json) => {this.updateFromJson(json.post_stream);this.setProperties({@@ -367,7 +362,7 @@ export default class PostStream extends RestModel {this._checkIfShouldShowRevisions();// Reset all error props- topic.setProperties({+ this.topic.setProperties({errorLoading: false,errorTitle: null,errorHtml: null,@@ -387,29 +382,27 @@ export default class PostStream extends RestModel {// Fill in a gap of posts before a particular postasync fillGapBefore(post, gap) {const postId = post.id;- const stream = this.stream;- const idx = stream.indexOf(postId);- const currentPosts = this.posts;+ const index = this.stream.indexOf(postId);- if (idx === -1) {+ if (index === -1) {return;}// Insert the gap at the appropriate place- let postIdx = currentPosts.indexOf(post);+ let postIndex = this.posts.indexOf(post);const headGap = gap.slice(0, this.topic.chunk_size);const tailGap = gap.slice(this.topic.chunk_size);- stream.splice.apply(stream, [idx, 0].concat(headGap));+ this.stream.splice.apply(this.stream, [index, 0].concat(headGap));- if (postIdx !== -1) {+ if (postIndex !== -1) {const posts = await this.findPostsByIds(headGap);posts.forEach((p) => {this._initUserModels(p);const stored = this.storePost(p);- if (!currentPosts.includes(stored)) {- const insertAtIndex = postIdx++;- currentPosts.splice(insertAtIndex, 0, stored);++ if (!this.posts.includes(stored)) {+ this.posts.splice(postIndex++, 0, stored);}});@@ -427,14 +420,13 @@ export default class PostStream extends RestModel {// Fill in a gap of posts after a particular postasync fillGapAfter(post, gap) {const postId = post.id;- const stream = this.stream;- const idx = stream.indexOf(postId);+ const index = this.stream.indexOf(postId);- if (idx === -1) {+ if (index === -1) {return;}- stream.push(...gap);+ this.stream.push(...gap);await this.appendMore();delete this.gaps.after[postId];@@ -444,7 +436,7 @@ export default class PostStream extends RestModel {gapExpanded() {// resets the reply count in posts-filtered-notice// because once a gap has been expanded that count is no longer exact- if (this.streamFilters && this.streamFilters.replies_to_post_number) {+ if (this.streamFilters?.replies_to_post_number) {this.streamFilters.mixedHiddenPosts = true;}}@@ -534,17 +526,16 @@ export default class PostStream extends RestModel {this.stagingPost = true;- const topic = this.topic;- topic.setProperties({- posts_count: (topic.posts_count || 0) + 1,+ this.topic.setProperties({+ posts_count: (this.topic.posts_count || 0) + 1,last_posted_at: new Date(),"details.last_poster": user,- highest_post_number: (topic.highest_post_number || 0) + 1,+ highest_post_number: (this.topic.highest_post_number || 0) + 1,});post.setProperties({- post_number: topic.highest_post_number,- topic,+ post_number: this.topic.highest_post_number,+ topic: this.topic,created_at: new Date(),id: -1,});@@ -631,13 +622,11 @@ export default class PostStream extends RestModel {return;}- const allPosts = this.posts;const postIds = posts.map((p) => p.id);- const identityMap = this._identityMap;removeValuesFromArray(this.stream, postIds);- removeValuesFromArray(allPosts, posts);- postIds.forEach((id) => delete identityMap[id]);+ removeValuesFromArray(this.posts, posts);+ postIds.forEach((id) => delete this._identityMap[id]);}removeAllPosts() {@@ -652,33 +641,29 @@ export default class PostStream extends RestModel {loadPostByPostNumber(postNumber) {const url = `/posts/by_number/${this.topic.id}/${postNumber}`;- const store = this.store;return ajax(url).then((post) => {- return this.storePost(store.createRecord("post", post));+ return this.storePost(this.store.createRecord("post", post));});}loadNearestPostToDate(date) {const url = `/posts/by-date/${this.topic.id}/${date}`;- const store = this.store;return ajax(url).then((post) => {- return this.storePost(store.createRecord("post", post));+ return this.storePost(this.store.createRecord("post", post));});}loadPost(postId) {- const url = "/posts/" + postId;- const store = this.store;const existing = this._identityMap[postId];- return ajax(url).then((p) => {+ return ajax(`/posts/${postId}`).then((p) => {if (existing) {p.cooked = existing.cooked;}- return this.storePost(store.createRecord("post", p));+ return this.storePost(this.store.createRecord("post", p));});}@@ -711,7 +696,6 @@ export default class PostStream extends RestModel {return;}- const loadedAllPosts = this.loadedAllPosts;this._loadingPostIds = this._loadingPostIds || [];// Find missing post IDs that aren't in the stream@@ -723,7 +707,7 @@ export default class PostStream extends RestModel {return;}- if (loadedAllPosts) {+ if (this.loadedAllPosts) {// Add missing posts to loading queue if not already loadingmissingIds.forEach((postId) => {if (!this._loadingPostIds.includes(postId)) {@@ -770,36 +754,31 @@ export default class PostStream extends RestModel {}// need to insert into stream- const url = `/posts/${postId}`;- const store = this.store;-- const p = await ajax(url);- const post = store.createRecord("post", p);- const stream = this.stream;- const posts = this.posts;+ const p = await ajax(`/posts/${postId}`);+ const post = this.store.createRecord("post", p);this.storePost(post);// we need to zip this into the streamlet index = 0;- stream.forEach((pid) => {+ this.stream.forEach((pid) => {if (pid < p.id) {index += 1;}});- stream.splice(index, 0, p.id);+ this.stream.splice(index, 0, p.id);index = 0;- posts.forEach((_post) => {+ this.posts.forEach((_post) => {if (_post.id < p.id) {index += 1;}});- if (index < posts.length) {- posts.splice(index, 0, post);+ if (index < this.posts.length) {+ this.posts.splice(index, 0, post);} else {- if (post.post_number < posts[posts.length - 1].post_number + 5) {+ if (post.post_number < this.posts.at(-1).post_number + 5) {await this.appendMore();}}@@ -809,17 +788,15 @@ export default class PostStream extends RestModel {const existing = this._identityMap[postId];if (existing && !existing.deleted_at) {- const url = "/posts/" + postId;- const store = this.store;-- return ajax(url)+ return ajax(`/posts/${postId}`).then((p) => {- this.storePost(store.createRecord("post", p));+ this.storePost(this.store.createRecord("post", p));}).catch(() => {this.removePosts([existing]);});}+return Promise.resolve();}@@ -881,6 +858,7 @@ export default class PostStream extends RestModel {triggerReadPost(postId, readersCount) {const resolved = Promise.resolve();+resolved.then(() => {const post = this.findLoadedPost(postId);if (post && readersCount > post.readers_count) {@@ -953,8 +931,7 @@ export default class PostStream extends RestModel {if (this.isMegaTopic) {return post.post_number;} else {- const index = this.stream.indexOf(postId);- return index + 1;+ return this.stream.indexOf(postId) + 1;}}@@ -1014,13 +991,11 @@ export default class PostStream extends RestModel {// Find a postId for a postNumber, respecting gapsfindPostIdForPostNumber(postNumber) {- const stream = this.stream;const beforeLookup = this.gaps?.before;- const streamLength = stream.length;let sum = 1;- for (let i = 0; i < streamLength; i++) {- const pid = stream[i];+ for (let i = 0; i < this.stream.length; i++) {+ const pid = this.stream[i];// See if there are posts before this postif (beforeLookup) {@@ -1038,28 +1013,27 @@ export default class PostStream extends RestModel {if (sum === postNumber) {return pid;}+sum++;}}updateFromJson(postStreamData) {this.posts.length = 0;-this.gaps = null;+if (postStreamData) {// Load posts if present- const store = this.store;postStreamData.posts.forEach((p) =>- this.appendPost(store.createRecord("post", p))+ this.appendPost(this.store.createRecord("post", p)));delete postStreamData.posts;// Update our attributes- const trackedGaps = {+ postStreamData.gaps = {before: new TrackedObject(postStreamData.gaps?.before || {}),after: new TrackedObject(postStreamData.gaps?.after || {}),};- postStreamData.gaps = trackedGaps;this.setProperties(postStreamData);}}@@ -1097,23 +1071,20 @@ export default class PostStream extends RestModel {}this._identityMap[post.id] = post;}+return post;}fetchNextWindow(postNumber, asc, callback) {- let includeSuggested = !this.topic.suggested_topics;-- const url = `/t/${this.topic.id}/posts.json`;let data = {post_number: postNumber,asc,- include_suggested: includeSuggested,+ include_suggested: !this.topic.suggested_topics,};data = deepMerge(data, this.streamFilters);- const store = this.store;- return ajax(url, { data })+ return ajax(`/t/${this.topic.id}/posts.json`, { data }).then((result) => {this._setSuggestedTopics(result);@@ -1121,7 +1092,7 @@ export default class PostStream extends RestModel {if (posts) {posts.forEach((p) => {- p = this.storePost(store.createRecord("post", p));+ p = this.storePost(this.store.createRecord("post", p));if (callback) {callback.call(this, p);@@ -1139,12 +1110,13 @@ export default class PostStream extends RestModel {}findPostsByIds(postIds, opts) {- const identityMap = this._identityMap;- const unloaded = postIds.filter((p) => !identityMap[p]);+ const unloaded = postIds.filter((p) => !this._identityMap[p]);// Load our unloaded posts by idreturn this.loadIntoIdentityMap(unloaded, opts).then(() => {- return postIds.map((p) => identityMap[p]).filter((item) => item != null);+ return postIds+ .map((p) => this._identityMap[p])+ .filter((item) => item != null);});}@@ -1153,18 +1125,17 @@ export default class PostStream extends RestModel {return Promise.resolve([]);}- let includeSuggested = !this.topic.suggested_topics;-- const url = "/t/" + this.topic.id + "/posts.json";- const data = { post_ids: postIds, include_suggested: includeSuggested };- const store = this.store;+ const data = {+ post_ids: postIds,+ include_suggested: !this.topic.suggested_topics,+ };let headers = {};- if (opts && opts.background) {+ if (opts?.background) {headers["Discourse-Background"] = "true";}- return ajax(url, {+ return ajax(`/t/${this.topic.id}/posts.json`, {data,headers,}).then((result) => {@@ -1177,28 +1148,29 @@ export default class PostStream extends RestModel {const posts = get(result, "post_stream.posts");if (posts) {- posts.forEach((p) => this.storePost(store.createRecord("post", p)));+ posts.forEach((p) =>+ this.storePost(this.store.createRecord("post", p))+ );}});}backfillExcerpts(streamPosition) {- this._excerpts = this._excerpts || [];- const stream = this.stream;-+ this._excerpts ||= [];this._excerpts.loadNext = streamPosition;if (this._excerpts.loading) {return this._excerpts.loading.then(() => {- if (!this._excerpts[stream[streamPosition]]) {- if (this._excerpts.loadNext === streamPosition) {- return this.backfillExcerpts(streamPosition);- }+ if (+ !this._excerpts[this.stream[streamPosition]] &&+ this._excerpts.loadNext === streamPosition+ ) {+ return this.backfillExcerpts(streamPosition);}});}- let postIds = stream.slice(+ let postIds = this.stream.slice(Math.max(streamPosition - 20, 0),streamPosition + 20);@@ -1213,7 +1185,7 @@ export default class PostStream extends RestModel {post_ids: postIds,};- this._excerpts.loading = ajax("/t/" + this.topic.id + "/excerpts.json", {+ this._excerpts.loading = ajax(`/t/${this.topic.id}/excerpts.json`, {data,}).then((excerpts) => {@@ -1233,10 +1205,8 @@ export default class PostStream extends RestModel {return new Promise((resolve) => resolve(""));}- const stream = this.stream;-return new Promise((resolve, reject) => {- let excerpt = this._excerpts && this._excerpts[stream[streamPosition]];+ const excerpt = this._excerpts?.[this.stream[streamPosition]];if (excerpt) {resolve(excerpt);@@ -1245,7 +1215,7 @@ export default class PostStream extends RestModel {this.backfillExcerpts(streamPosition).then(() => {- resolve(this._excerpts[stream[streamPosition]]);+ resolve(this._excerpts[this.stream[streamPosition]]);}).catch((e) => reject(e));});@@ -1261,21 +1231,20 @@ export default class PostStream extends RestModel {applyBehaviorTransformer("post-stream-error-loading",() => {- const topic = this.topic;this.loadingFilter = false;- topic.errorLoading = true;+ this.topic.errorLoading = true;if (!error.jqXHR) {throw error;}const json = error.jqXHR.responseJSON;- if (json && json.extras && json.extras.html) {- topic.errorTitle = json.extras.title;- topic.errorHtml = json.extras.html;+ if (json?.extras?.html) {+ this.topic.errorTitle = json.extras.title;+ this.topic.errorHtml = json.extras.html;} else {- topic.errorMessage = i18n("topic.server_error.description");- topic.noRetry = error.jqXHR.status === 403;+ this.topic.errorMessage = i18n("topic.server_error.description");+ this.topic.noRetry = error.jqXHR.status === 403;}},{@@ -1294,22 +1263,25 @@ export default class PostStream extends RestModel {}_checkIfShouldShowRevisions() {- if (_lastEditNotificationClick) {- const copy = _lastEditNotificationClick;- resetLastEditNotificationClick();- const postsNumbers = this.posts.map((post) => post.post_number);- if (- copy.topicId === this.topic.id &&- postsNumbers.includes(copy.postNumber)- ) {- schedule("afterRender", () => {- this.appEvents.trigger(- "post:show-revision",- copy.postNumber,- copy.revisionNumber- );- });- }+ if (!_lastEditNotificationClick) {+ return;+ }++ const copy = _lastEditNotificationClick;+ resetLastEditNotificationClick();+ const postsNumbers = this.posts.map((post) => post.post_number);++ if (+ copy.topicId === this.topic.id &&+ postsNumbers.includes(copy.postNumber)+ ) {+ schedule("afterRender", () => {+ this.appEvents.trigger(+ "post:show-revision",+ copy.postNumber,+ copy.revisionNumber+ );+ });}}