mirror of
https://github.com/discourse/discourse.git
synced 2025-05-30 15:28:37 +08:00
FEATURE: Promote polymorphic bookmarks to default and migrate (#16729)
This commit migrates all bookmarks to be polymorphic (using the bookmarkable_id and bookmarkable_type) columns. It also deletes all the old code guarded behind the use_polymorphic_bookmarks setting and changes that setting to true for all sites and by default for the sake of plugins. No data is deleted in the migrations, the old post_id and for_topic columns for bookmarks will be dropped later on.
This commit is contained in:
@ -9,175 +9,24 @@ RSpec.describe BookmarkManager do
|
||||
|
||||
subject { described_class.new(user) }
|
||||
|
||||
describe ".create" do
|
||||
it "creates the bookmark for the user" do
|
||||
subject.create(post_id: post.id, name: name)
|
||||
bookmark = Bookmark.find_by(user: user)
|
||||
|
||||
expect(bookmark.post_id).to eq(post.id)
|
||||
expect(bookmark.topic_id).to eq(post.topic_id)
|
||||
end
|
||||
|
||||
it "allows creating a bookmark for the topic and for the first post" do
|
||||
subject.create(post_id: post.id, name: name, for_topic: true)
|
||||
bookmark = Bookmark.find_by(user: user, post_id: post.id, for_topic: true)
|
||||
|
||||
expect(bookmark.post_id).to eq(post.id)
|
||||
expect(bookmark.topic_id).to eq(post.topic_id)
|
||||
expect(bookmark.for_topic).to eq(true)
|
||||
|
||||
subject.create(post_id: post.id, name: name)
|
||||
bookmark = Bookmark.find_by(user: user, post_id: post.id, for_topic: false)
|
||||
|
||||
expect(bookmark.post_id).to eq(post.id)
|
||||
expect(bookmark.topic_id).to eq(post.topic_id)
|
||||
expect(bookmark.for_topic).to eq(false)
|
||||
end
|
||||
|
||||
# TODO (martin) [POLYBOOK] Not relevant once polymorphic bookmarks are implemented.
|
||||
# Topic bookmarks will be distinct, not attached to a post.
|
||||
it "errors when creating a for_topic bookmark for a post that is not the first one" do
|
||||
subject.create(post_id: Fabricate(:post, topic: post.topic).id, name: name, for_topic: true)
|
||||
expect(subject.errors.full_messages).to include(I18n.t("bookmarks.errors.for_topic_must_use_first_post"))
|
||||
end
|
||||
|
||||
it "when topic is deleted it raises invalid access" do
|
||||
post.topic.trash!
|
||||
expect { subject.create(post_id: post.id, name: name) }.to raise_error(Discourse::InvalidAccess)
|
||||
end
|
||||
|
||||
it "when post is deleted it raises invalid access" do
|
||||
post.trash!
|
||||
expect { subject.create(post_id: post.id, name: name) }.to raise_error(Discourse::InvalidAccess)
|
||||
end
|
||||
|
||||
it "updates the topic user bookmarked column to true if any post is bookmarked" do
|
||||
subject.create(post_id: post.id, name: name, reminder_at: reminder_at)
|
||||
tu = TopicUser.find_by(user: user)
|
||||
expect(tu.bookmarked).to eq(true)
|
||||
tu.update(bookmarked: false)
|
||||
subject.create(post_id: Fabricate(:post, topic: post.topic).id)
|
||||
tu.reload
|
||||
expect(tu.bookmarked).to eq(true)
|
||||
end
|
||||
|
||||
context "when a reminder time is provided" do
|
||||
it "saves the values correctly" do
|
||||
subject.create(post_id: post.id, name: name, reminder_at: reminder_at)
|
||||
bookmark = Bookmark.find_by(user: user)
|
||||
|
||||
expect(bookmark.reminder_at).to eq_time(reminder_at)
|
||||
expect(bookmark.reminder_set_at).not_to eq(nil)
|
||||
end
|
||||
end
|
||||
|
||||
context "when options are provided" do
|
||||
let(:options) { { auto_delete_preference: Bookmark.auto_delete_preferences[:when_reminder_sent] } }
|
||||
|
||||
it "saves any additional options successfully" do
|
||||
subject.create(post_id: post.id, name: name, options: options)
|
||||
bookmark = Bookmark.find_by(user: user)
|
||||
|
||||
expect(bookmark.auto_delete_preference).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
context "when the bookmark already exists for the user & post" do
|
||||
before do
|
||||
Bookmark.create(post: post, user: user)
|
||||
end
|
||||
|
||||
it "adds an error to the manager" do
|
||||
subject.create(post_id: post.id)
|
||||
expect(subject.errors.full_messages).to include(I18n.t("bookmarks.errors.already_bookmarked_post"))
|
||||
end
|
||||
end
|
||||
|
||||
context "when the bookmark name is too long" do
|
||||
it "adds an error to the manager" do
|
||||
subject.create(post_id: post.id, name: "test" * 100)
|
||||
expect(subject.errors.full_messages).to include("Name is too long (maximum is 100 characters)")
|
||||
end
|
||||
end
|
||||
|
||||
context "when the reminder time is in the past" do
|
||||
let(:reminder_at) { 10.days.ago }
|
||||
|
||||
it "adds an error to the manager" do
|
||||
subject.create(post_id: post.id, name: name, reminder_at: reminder_at)
|
||||
expect(subject.errors.full_messages).to include(I18n.t("bookmarks.errors.cannot_set_past_reminder"))
|
||||
end
|
||||
end
|
||||
|
||||
context "when the reminder time is far-flung (> 10 years from now)" do
|
||||
let(:reminder_at) { 11.years.from_now }
|
||||
|
||||
it "adds an error to the manager" do
|
||||
subject.create(post_id: post.id, name: name, reminder_at: reminder_at)
|
||||
expect(subject.errors.full_messages).to include(I18n.t("bookmarks.errors.cannot_set_reminder_in_distant_future"))
|
||||
end
|
||||
end
|
||||
|
||||
context "when the post is inaccessible for the user" do
|
||||
before do
|
||||
post.trash!
|
||||
end
|
||||
it "raises an invalid access error" do
|
||||
expect { subject.create(post_id: post.id, name: name) }.to raise_error(Discourse::InvalidAccess)
|
||||
end
|
||||
end
|
||||
|
||||
context "when the topic is inaccessible for the user" do
|
||||
before do
|
||||
post.topic.update(category: Fabricate(:private_category, group: Fabricate(:group)))
|
||||
end
|
||||
it "raises an invalid access error" do
|
||||
expect { subject.create(post_id: post.id, name: name) }.to raise_error(Discourse::InvalidAccess)
|
||||
end
|
||||
end
|
||||
|
||||
it "saves user's preference" do
|
||||
subject.create(post_id: post.id, options: { auto_delete_preference: Bookmark.auto_delete_preferences[:when_reminder_sent] })
|
||||
expect(user.user_option.bookmark_auto_delete_preference).to eq(Bookmark.auto_delete_preferences[:when_reminder_sent])
|
||||
|
||||
bookmark = Bookmark.find_by(user: user)
|
||||
subject.update(bookmark_id: bookmark, name: "test", reminder_at: 1.day.from_now, options: { auto_delete_preference: Bookmark.auto_delete_preferences[:on_owner_reply] })
|
||||
expect(user.user_option.bookmark_auto_delete_preference).to eq(Bookmark.auto_delete_preferences[:on_owner_reply])
|
||||
end
|
||||
end
|
||||
|
||||
describe ".destroy" do
|
||||
let!(:bookmark) { Fabricate(:bookmark, user: user, post: post) }
|
||||
let!(:bookmark) { Fabricate(:bookmark, user: user, bookmarkable: post) }
|
||||
it "deletes the existing bookmark" do
|
||||
subject.destroy(bookmark.id)
|
||||
expect(Bookmark.exists?(id: bookmark.id)).to eq(false)
|
||||
end
|
||||
|
||||
# TODO (martin) [POLYBOOK] Not relevant once polymorphic bookmarks are implemented.
|
||||
context "if the bookmark is the last one bookmarked in the topic" do
|
||||
it "marks the topic user bookmarked column as false" do
|
||||
TopicUser.create(user: user, topic: bookmark.post.topic, bookmarked: true)
|
||||
TopicUser.create(user: user, topic: post.topic, bookmarked: true)
|
||||
subject.destroy(bookmark.id)
|
||||
tu = TopicUser.find_by(user: user)
|
||||
expect(tu.bookmarked).to eq(false)
|
||||
end
|
||||
end
|
||||
|
||||
context "if the bookmark is the last one bookmarked in the topic (polymorphic)" do
|
||||
before do
|
||||
SiteSetting.use_polymorphic_bookmarks = true
|
||||
end
|
||||
it "marks the topic user bookmarked column as false" do
|
||||
poly_bookmark = Fabricate(:bookmark, user: user, bookmarkable: post)
|
||||
TopicUser.create(user: user, topic: post.topic, bookmarked: true)
|
||||
subject.destroy(poly_bookmark.id)
|
||||
tu = TopicUser.find_by(user: user)
|
||||
expect(tu.bookmarked).to eq(false)
|
||||
end
|
||||
end
|
||||
|
||||
context "if the bookmark is belonging to some other user" do
|
||||
let!(:bookmark) { Fabricate(:bookmark, user: Fabricate(:admin), post: post) }
|
||||
let!(:bookmark) { Fabricate(:bookmark, user: Fabricate(:admin), bookmarkable: post) }
|
||||
it "raises an invalid access error" do
|
||||
expect { subject.destroy(bookmark.id) }.to raise_error(Discourse::InvalidAccess)
|
||||
end
|
||||
@ -191,7 +40,7 @@ RSpec.describe BookmarkManager do
|
||||
end
|
||||
|
||||
describe ".update" do
|
||||
let!(:bookmark) { Fabricate(:bookmark_next_business_day_reminder, user: user, post: post, name: "Old name") }
|
||||
let!(:bookmark) { Fabricate(:bookmark_next_business_day_reminder, user: user, bookmarkable: post, name: "Old name") }
|
||||
let(:new_name) { "Some new name" }
|
||||
let(:new_reminder_at) { 10.days.from_now }
|
||||
let(:options) { {} }
|
||||
@ -230,7 +79,7 @@ RSpec.describe BookmarkManager do
|
||||
end
|
||||
|
||||
context "if the bookmark is belonging to some other user" do
|
||||
let!(:bookmark) { Fabricate(:bookmark, user: Fabricate(:admin), post: post) }
|
||||
let!(:bookmark) { Fabricate(:bookmark, user: Fabricate(:admin), bookmarkable: post) }
|
||||
it "raises an invalid access error" do
|
||||
expect { update_bookmark }.to raise_error(Discourse::InvalidAccess)
|
||||
end
|
||||
@ -247,35 +96,6 @@ RSpec.describe BookmarkManager do
|
||||
end
|
||||
|
||||
describe ".destroy_for_topic" do
|
||||
let!(:topic) { Fabricate(:topic) }
|
||||
let!(:bookmark1) { Fabricate(:bookmark, post: Fabricate(:post, topic: topic), user: user) }
|
||||
let!(:bookmark2) { Fabricate(:bookmark, post: Fabricate(:post, topic: topic), user: user) }
|
||||
|
||||
it "destroys all bookmarks for the topic for the specified user" do
|
||||
subject.destroy_for_topic(topic)
|
||||
expect(Bookmark.for_user_in_topic(user.id, topic.id).length).to eq(0)
|
||||
end
|
||||
|
||||
it "does not destroy any other user's topic bookmarks" do
|
||||
user2 = Fabricate(:user)
|
||||
Fabricate(:bookmark, post: Fabricate(:post, topic: topic), user: user2)
|
||||
subject.destroy_for_topic(topic)
|
||||
expect(Bookmark.for_user_in_topic(user2.id, topic.id).length).to eq(1)
|
||||
end
|
||||
|
||||
it "updates the topic user bookmarked column to false" do
|
||||
TopicUser.create(user: user, topic: topic, bookmarked: true)
|
||||
subject.destroy_for_topic(topic)
|
||||
tu = TopicUser.find_by(user: user)
|
||||
expect(tu.bookmarked).to eq(false)
|
||||
end
|
||||
end
|
||||
|
||||
describe ".destroy_for_topic (polymorphic)" do
|
||||
before do
|
||||
SiteSetting.use_polymorphic_bookmarks = true
|
||||
end
|
||||
|
||||
let!(:topic) { Fabricate(:topic) }
|
||||
let!(:bookmark1) { Fabricate(:bookmark, bookmarkable: Fabricate(:post, topic: topic), user: user) }
|
||||
let!(:bookmark2) { Fabricate(:bookmark, bookmarkable: Fabricate(:post, topic: topic), user: user) }
|
||||
@ -312,7 +132,7 @@ RSpec.describe BookmarkManager do
|
||||
it "creates a notification for the reminder" do
|
||||
described_class.send_reminder_notification(bookmark.id)
|
||||
notif = notifications_for_user.last
|
||||
expect(notif.post_number).to eq(bookmark.post.post_number)
|
||||
expect(notif.post_number).to eq(bookmark.bookmarkable.post_number)
|
||||
end
|
||||
|
||||
context "when the bookmark does no longer exist" do
|
||||
@ -327,7 +147,7 @@ RSpec.describe BookmarkManager do
|
||||
|
||||
context "if the post has been deleted" do
|
||||
before do
|
||||
bookmark.post.trash!
|
||||
bookmark.bookmarkable.trash!
|
||||
end
|
||||
it "does not error and does not create a notification" do
|
||||
described_class.send_reminder_notification(bookmark.id)
|
||||
@ -373,11 +193,7 @@ RSpec.describe BookmarkManager do
|
||||
end
|
||||
end
|
||||
|
||||
describe "#create_for (use_polymorphic_bookmarks)" do
|
||||
before do
|
||||
SiteSetting.use_polymorphic_bookmarks = true
|
||||
end
|
||||
|
||||
describe "#create_for" do
|
||||
it "allows creating a bookmark for the topic and for the first post" do
|
||||
subject.create_for(bookmarkable_id: post.topic_id, bookmarkable_type: "Topic", name: name)
|
||||
bookmark = Bookmark.find_by(user: user, bookmarkable: post.topic)
|
||||
|
Reference in New Issue
Block a user