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() {
const postStream = this.get("model.postStream");
let firstPost = postStream.get("posts.firstObject");
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);
this.model
.firstPost()
.then(firstPost => this.send("editPost", firstPost));
},
// 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 { ajax } from "discourse/lib/ajax";
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() {
if (this.bookmarking) {
return Promise.resolve();
}
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;
let posts = this.postStream.posts;
return this.firstPost().then(firstPost => {
const toggleBookmarkOnServer = () => {
if (bookmark) {
if (this.siteSettings.enable_bookmarks_with_reminders) {
@ -442,13 +458,13 @@ const Topic = RestModel.extend({
if (posts) {
const updated = [];
posts.forEach(post => {
if (post.get("bookmarked")) {
if (post.bookmarked) {
post.set("bookmarked", false);
updated.push(post.id);
}
if (
this.siteSettings.enable_bookmarks_with_reminders &&
post.get("bookmarked_with_reminder")
post.bookmarked_with_reminder
) {
post.set("bookmarked_with_reminder", false);
updated.push(post.id);
@ -466,7 +482,7 @@ const Topic = RestModel.extend({
if (!bookmark && posts) {
posts.forEach(
post =>
(post.get("bookmarked") || post.get("bookmarked_with_reminder")) &&
(post.bookmarked || post.bookmarked_with_reminder) &&
unbookmarkedPosts.push(post)
);
}
@ -484,6 +500,7 @@ const Topic = RestModel.extend({
toggleBookmarkOnServer().then(resolve);
}
});
});
},
createGroupInvite(group) {