FIX: Ensure first post is loaded before trying to bookmark topic (#9382)

Load first post if not loaded when trying to bookmark topic

* if the first post was not loaded we could not bookmark it because
  now we call the toggleBookmarkReminder function on an actual post
  to open the modal window
* add a firstPost function to the topic model to get the first post
  from the stream if it is loaded and if not do a GET request to load
  it
* use the firstPost promise when bookmarking topic
This commit is contained in:
Martin Brennan 2020-04-09 09:42:03 +10:00 committed by GitHub
parent 138d4aebde
commit 452d0c868f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 76 deletions

View File

@ -453,22 +453,9 @@ export default Controller.extend(bufferedProperty("model"), {
}, },
editFirstPost() { editFirstPost() {
const postStream = this.get("model.postStream"); this.model
let firstPost = postStream.get("posts.firstObject"); .firstPost()
.then(firstPost => this.send("editPost", firstPost));
if (firstPost.get("post_number") !== 1) {
const postId = postStream.findPostIdForPostNumber(1);
// try loading from identity map first
firstPost = postStream.findLoadedPost(postId);
if (firstPost === undefined) {
return this.get("model.postStream")
.loadPost(postId)
.then(post => {
this.send("editPost", post);
});
}
}
this.send("editPost", firstPost);
}, },
// Post related methods // Post related methods

View File

@ -1,4 +1,4 @@
import EmberObject, { get } from "@ember/object"; import EmberObject from "@ember/object";
import { not, notEmpty, equal, and, or } from "@ember/object/computed"; import { not, notEmpty, equal, and, or } from "@ember/object/computed";
import { ajax } from "discourse/lib/ajax"; import { ajax } from "discourse/lib/ajax";
import { flushMap } from "discourse/models/store"; import { flushMap } from "discourse/models/store";
@ -407,18 +407,34 @@ const Topic = RestModel.extend({
} }
}, },
firstPost() {
const postStream = this.postStream;
let firstPost = postStream.get("posts.firstObject");
if (firstPost.post_number === 1) {
return Promise.resolve(firstPost);
}
const postId = postStream.findPostIdForPostNumber(1);
// try loading from identity map first
firstPost = postStream.findLoadedPost(postId);
if (firstPost) {
return Promise.resolve(firstPost);
}
return this.postStream.loadPost(postId);
},
toggleBookmark() { toggleBookmark() {
if (this.bookmarking) { if (this.bookmarking) {
return Promise.resolve(); return Promise.resolve();
} }
this.set("bookmarking", true); this.set("bookmarking", true);
const stream = this.postStream;
const posts = get(stream, "posts");
const firstPost =
posts && posts[0] && posts[0].get("post_number") === 1 && posts[0];
const bookmark = !this.bookmarked; const bookmark = !this.bookmarked;
let posts = this.postStream.posts;
return this.firstPost().then(firstPost => {
const toggleBookmarkOnServer = () => { const toggleBookmarkOnServer = () => {
if (bookmark) { if (bookmark) {
if (this.siteSettings.enable_bookmarks_with_reminders) { if (this.siteSettings.enable_bookmarks_with_reminders) {
@ -442,13 +458,13 @@ const Topic = RestModel.extend({
if (posts) { if (posts) {
const updated = []; const updated = [];
posts.forEach(post => { posts.forEach(post => {
if (post.get("bookmarked")) { if (post.bookmarked) {
post.set("bookmarked", false); post.set("bookmarked", false);
updated.push(post.id); updated.push(post.id);
} }
if ( if (
this.siteSettings.enable_bookmarks_with_reminders && this.siteSettings.enable_bookmarks_with_reminders &&
post.get("bookmarked_with_reminder") post.bookmarked_with_reminder
) { ) {
post.set("bookmarked_with_reminder", false); post.set("bookmarked_with_reminder", false);
updated.push(post.id); updated.push(post.id);
@ -466,7 +482,7 @@ const Topic = RestModel.extend({
if (!bookmark && posts) { if (!bookmark && posts) {
posts.forEach( posts.forEach(
post => post =>
(post.get("bookmarked") || post.get("bookmarked_with_reminder")) && (post.bookmarked || post.bookmarked_with_reminder) &&
unbookmarkedPosts.push(post) unbookmarkedPosts.push(post)
); );
} }
@ -484,6 +500,7 @@ const Topic = RestModel.extend({
toggleBookmarkOnServer().then(resolve); toggleBookmarkOnServer().then(resolve);
} }
}); });
});
}, },
createGroupInvite(group) { createGroupInvite(group) {