DEV: Add new bookmarks:changed app event (#14674)

This new app event will fire whenever a bookmark is created,
edited, or deleted for a post or topic, and replaces these old
app events which had inconsistent APIs:

* page:bookmark-post-toggled
* topic:bookmark-toggled

When the event is triggered, the arguments are in this order:

1. bookmark - The bookmark record created or changed. Will be null
              if the bookmark was deleted.
2. target   - Object with target (post or topic) and targetId (post ID
              or topic ID)
This commit is contained in:
Martin Brennan 2021-10-22 09:38:02 +10:00 committed by GitHub
parent 2b40049abb
commit 7290a74aa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 51 additions and 5 deletions

View File

@ -19,6 +19,11 @@ export default Component.extend({
bookmark
.destroy()
.then(() => {
this.appEvents.trigger(
"bookmarks:changed",
null,
bookmark.attachedTo()
);
this._removeBookmarkFromList(bookmark);
resolve(true);
})
@ -52,7 +57,12 @@ export default Component.extend({
@action
editBookmark(bookmark) {
openBookmarkModal(bookmark, {
onAfterSave: () => {
onAfterSave: (savedData) => {
this.appEvents.trigger(
"bookmarks:changed",
savedData,
bookmark.attachedTo()
);
this.reload();
},
onAfterDelete: () => {

View File

@ -201,6 +201,7 @@ export default Component.extend({
post_id: this.model.postId,
id: this.model.id || response.id,
name: this.model.name,
topic_id: this.model.topicId,
});
},

View File

@ -24,6 +24,7 @@ export function openBookmarkModal(
let modalController = showModal("bookmark", {
model: {
postId: bookmark.post_id,
topicId: bookmark.topic_id,
id: bookmark.id,
reminderAt: bookmark.reminder_at,
autoDeletePreference: bookmark.auto_delete_preference,

View File

@ -755,7 +755,11 @@ export default Controller.extend(bufferedProperty("model"), {
(bookmark) => bookmark.post_id === post.id && !bookmark.for_topic
);
return this._modifyPostBookmark(
bookmarkForPost || { post_id: post.id, for_topic: false },
bookmarkForPost || {
post_id: post.id,
topic_id: post.topic_id,
for_topic: false,
},
post
);
} else {
@ -1231,6 +1235,13 @@ export default Controller.extend(bufferedProperty("model"), {
this.model.set("bookmarking", false);
this.model.set("bookmarked", true);
this.model.incrementProperty("bookmarksWereChanged");
this.appEvents.trigger(
"bookmarks:changed",
savedData,
bookmark.attachedTo()
);
// TODO (martin) (2022-02-01) Remove these old bookmark events, replaced by bookmarks:changed.
this.appEvents.trigger("topic:bookmark-toggled");
},
onAfterDelete: (topicBookmarked, bookmarkId) => {
@ -1300,6 +1311,7 @@ export default Controller.extend(bufferedProperty("model"), {
const firstPost = await this.model.firstPost();
return this._modifyTopicBookmark({
post_id: firstPost.id,
topic_id: this.model.id,
for_topic: true,
});
}

View File

@ -36,6 +36,13 @@ const Bookmark = RestModel.extend({
});
},
attachedTo() {
if (this.for_topic) {
return { target: "topic", targetId: this.topic_id };
}
return { target: "post", targetId: this.post_id };
},
togglePin() {
if (this.newBookmark) {
return Promise.resolve();

View File

@ -314,6 +314,11 @@ const Post = RestModel.extend({
bookmark_id: data.id,
});
this.topic.incrementProperty("bookmarksWereChanged");
this.appEvents.trigger("bookmarks:changed", data, {
target: "post",
targetId: this.id,
});
// TODO (martin) (2022-02-01) Remove these old bookmark events, replaced by bookmarks:changed.
this.appEvents.trigger("page:bookmark-post-toggled", this);
this.appEvents.trigger("post-stream:refresh", { id: this.id });
},
@ -321,8 +326,6 @@ const Post = RestModel.extend({
deleteBookmark(bookmarked) {
this.set("topic.bookmarked", bookmarked);
this.clearBookmark();
this.topic.incrementProperty("bookmarksWereChanged");
this.appEvents.trigger("page:bookmark-post-toggled", this);
},
clearBookmark() {
@ -334,6 +337,12 @@ const Post = RestModel.extend({
bookmark_auto_delete_preference: null,
});
this.topic.incrementProperty("bookmarksWereChanged");
this.appEvents.trigger("bookmarks:changed", null, {
target: "post",
targetId: this.id,
});
// TODO (martin) (2022-02-01) Remove these old bookmark events, replaced by bookmarks:changed.
this.appEvents.trigger("page:bookmark-post-toggled", this);
},
updateActionsSummary(json) {

View File

@ -386,7 +386,13 @@ const Topic = RestModel.extend({
"bookmarks",
this.bookmarks.filter((bookmark) => {
if (bookmark.id === id && bookmark.for_topic) {
// TODO (martin) (2022-02-01) Remove these old bookmark events, replaced by bookmarks:changed.
this.appEvents.trigger("topic:bookmark-toggled");
this.appEvents.trigger(
"bookmarks:changed",
null,
bookmark.attachedTo()
);
}
return bookmark.id !== id;

View File

@ -388,7 +388,7 @@ class TopicView
def bookmarks
@bookmarks ||= @topic.bookmarks.where(user: @user).joins(:topic).select(
:id, :post_id, :for_topic, :reminder_at, :name, :auto_delete_preference
:id, :post_id, "topics.id AS topic_id", :for_topic, :reminder_at, :name, :auto_delete_preference
)
end