mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
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:
parent
2b40049abb
commit
7290a74aa6
@ -19,6 +19,11 @@ export default Component.extend({
|
|||||||
bookmark
|
bookmark
|
||||||
.destroy()
|
.destroy()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
this.appEvents.trigger(
|
||||||
|
"bookmarks:changed",
|
||||||
|
null,
|
||||||
|
bookmark.attachedTo()
|
||||||
|
);
|
||||||
this._removeBookmarkFromList(bookmark);
|
this._removeBookmarkFromList(bookmark);
|
||||||
resolve(true);
|
resolve(true);
|
||||||
})
|
})
|
||||||
@ -52,7 +57,12 @@ export default Component.extend({
|
|||||||
@action
|
@action
|
||||||
editBookmark(bookmark) {
|
editBookmark(bookmark) {
|
||||||
openBookmarkModal(bookmark, {
|
openBookmarkModal(bookmark, {
|
||||||
onAfterSave: () => {
|
onAfterSave: (savedData) => {
|
||||||
|
this.appEvents.trigger(
|
||||||
|
"bookmarks:changed",
|
||||||
|
savedData,
|
||||||
|
bookmark.attachedTo()
|
||||||
|
);
|
||||||
this.reload();
|
this.reload();
|
||||||
},
|
},
|
||||||
onAfterDelete: () => {
|
onAfterDelete: () => {
|
||||||
|
@ -201,6 +201,7 @@ export default Component.extend({
|
|||||||
post_id: this.model.postId,
|
post_id: this.model.postId,
|
||||||
id: this.model.id || response.id,
|
id: this.model.id || response.id,
|
||||||
name: this.model.name,
|
name: this.model.name,
|
||||||
|
topic_id: this.model.topicId,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -24,6 +24,7 @@ export function openBookmarkModal(
|
|||||||
let modalController = showModal("bookmark", {
|
let modalController = showModal("bookmark", {
|
||||||
model: {
|
model: {
|
||||||
postId: bookmark.post_id,
|
postId: bookmark.post_id,
|
||||||
|
topicId: bookmark.topic_id,
|
||||||
id: bookmark.id,
|
id: bookmark.id,
|
||||||
reminderAt: bookmark.reminder_at,
|
reminderAt: bookmark.reminder_at,
|
||||||
autoDeletePreference: bookmark.auto_delete_preference,
|
autoDeletePreference: bookmark.auto_delete_preference,
|
||||||
|
@ -755,7 +755,11 @@ export default Controller.extend(bufferedProperty("model"), {
|
|||||||
(bookmark) => bookmark.post_id === post.id && !bookmark.for_topic
|
(bookmark) => bookmark.post_id === post.id && !bookmark.for_topic
|
||||||
);
|
);
|
||||||
return this._modifyPostBookmark(
|
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
|
post
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
@ -1231,6 +1235,13 @@ export default Controller.extend(bufferedProperty("model"), {
|
|||||||
this.model.set("bookmarking", false);
|
this.model.set("bookmarking", false);
|
||||||
this.model.set("bookmarked", true);
|
this.model.set("bookmarked", true);
|
||||||
this.model.incrementProperty("bookmarksWereChanged");
|
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");
|
this.appEvents.trigger("topic:bookmark-toggled");
|
||||||
},
|
},
|
||||||
onAfterDelete: (topicBookmarked, bookmarkId) => {
|
onAfterDelete: (topicBookmarked, bookmarkId) => {
|
||||||
@ -1300,6 +1311,7 @@ export default Controller.extend(bufferedProperty("model"), {
|
|||||||
const firstPost = await this.model.firstPost();
|
const firstPost = await this.model.firstPost();
|
||||||
return this._modifyTopicBookmark({
|
return this._modifyTopicBookmark({
|
||||||
post_id: firstPost.id,
|
post_id: firstPost.id,
|
||||||
|
topic_id: this.model.id,
|
||||||
for_topic: true,
|
for_topic: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -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() {
|
togglePin() {
|
||||||
if (this.newBookmark) {
|
if (this.newBookmark) {
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
|
@ -314,6 +314,11 @@ const Post = RestModel.extend({
|
|||||||
bookmark_id: data.id,
|
bookmark_id: data.id,
|
||||||
});
|
});
|
||||||
this.topic.incrementProperty("bookmarksWereChanged");
|
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("page:bookmark-post-toggled", this);
|
||||||
this.appEvents.trigger("post-stream:refresh", { id: this.id });
|
this.appEvents.trigger("post-stream:refresh", { id: this.id });
|
||||||
},
|
},
|
||||||
@ -321,8 +326,6 @@ const Post = RestModel.extend({
|
|||||||
deleteBookmark(bookmarked) {
|
deleteBookmark(bookmarked) {
|
||||||
this.set("topic.bookmarked", bookmarked);
|
this.set("topic.bookmarked", bookmarked);
|
||||||
this.clearBookmark();
|
this.clearBookmark();
|
||||||
this.topic.incrementProperty("bookmarksWereChanged");
|
|
||||||
this.appEvents.trigger("page:bookmark-post-toggled", this);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
clearBookmark() {
|
clearBookmark() {
|
||||||
@ -334,6 +337,12 @@ const Post = RestModel.extend({
|
|||||||
bookmark_auto_delete_preference: null,
|
bookmark_auto_delete_preference: null,
|
||||||
});
|
});
|
||||||
this.topic.incrementProperty("bookmarksWereChanged");
|
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) {
|
updateActionsSummary(json) {
|
||||||
|
@ -386,7 +386,13 @@ const Topic = RestModel.extend({
|
|||||||
"bookmarks",
|
"bookmarks",
|
||||||
this.bookmarks.filter((bookmark) => {
|
this.bookmarks.filter((bookmark) => {
|
||||||
if (bookmark.id === id && bookmark.for_topic) {
|
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("topic:bookmark-toggled");
|
||||||
|
this.appEvents.trigger(
|
||||||
|
"bookmarks:changed",
|
||||||
|
null,
|
||||||
|
bookmark.attachedTo()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return bookmark.id !== id;
|
return bookmark.id !== id;
|
||||||
|
@ -388,7 +388,7 @@ class TopicView
|
|||||||
|
|
||||||
def bookmarks
|
def bookmarks
|
||||||
@bookmarks ||= @topic.bookmarks.where(user: @user).joins(:topic).select(
|
@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
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user