FEATURE: Promote bookmarks with reminders to core functionality (#9369)

The main thrust of this PR is to take all the conditional checks based on the `enable_bookmarks_with_reminders` away and only keep the code from the `true` path, making bookmarks with reminders the core bookmarks feature. There is also a migration to create `Bookmark` records out of `PostAction` bookmarks for a site.

### Summary

* Remove logic based on whether enable_bookmarks_with_reminders is true. This site setting is now obsolete, the old bookmark functionality is being removed. Retain the setting and set the value to `true` in a migration.
* Use the code from the rake task to create a database migration that creates bookmarks from post actions.
* Change the bookmark report to read from the new table.
* Get rid of old endpoints for bookmarks
* Link to the new bookmarks list from the user summary page
This commit is contained in:
Martin Brennan
2020-04-22 13:44:19 +10:00
committed by GitHub
parent 5a98869c5d
commit 628ba9d1e2
41 changed files with 254 additions and 469 deletions

View File

@ -124,11 +124,11 @@ RSpec.describe Admin::UsersController do
end
describe '#suspend' do
fab!(:post) { Fabricate(:post) }
fab!(:created_post) { Fabricate(:post) }
let(:suspend_params) do
{ suspend_until: 5.hours.from_now,
reason: "because of this post",
post_id: post.id }
post_id: created_post.id }
end
it "works properly" do
@ -156,13 +156,13 @@ RSpec.describe Admin::UsersController do
expect(response.status).to eq(200)
log = UserHistory.where(target_user_id: user.id).order('id desc').first
expect(log.post_id).to eq(post.id)
expect(log.post_id).to eq(created_post.id)
end
it "can delete an associated post" do
put "/admin/users/#{user.id}/suspend.json", params: suspend_params.merge(post_action: 'delete')
post.reload
expect(post.deleted_at).to be_present
created_post.reload
expect(created_post.deleted_at).to be_present
expect(response.status).to eq(200)
end
@ -200,17 +200,17 @@ RSpec.describe Admin::UsersController do
reply = PostCreator.create(
Fabricate(:user),
raw: 'this is the reply text',
reply_to_post_number: post.post_number,
topic_id: post.topic_id
reply_to_post_number: created_post.post_number,
topic_id: created_post.topic_id
)
nested_reply = PostCreator.create(
Fabricate(:user),
raw: 'this is the reply text2',
reply_to_post_number: reply.post_number,
topic_id: post.topic_id
topic_id: created_post.topic_id
)
put "/admin/users/#{user.id}/suspend.json", params: suspend_params.merge(post_action: 'delete_replies')
expect(post.reload.deleted_at).to be_present
expect(created_post.reload.deleted_at).to be_present
expect(reply.reload.deleted_at).to be_present
expect(nested_reply.reload.deleted_at).to be_present
expect(response.status).to eq(200)
@ -223,9 +223,9 @@ RSpec.describe Admin::UsersController do
)
expect(response.status).to eq(200)
post.reload
expect(post.deleted_at).to be_blank
expect(post.raw).to eq("this is the edited content")
created_post.reload
expect(created_post.deleted_at).to be_blank
expect(created_post.raw).to eq("this is the edited content")
expect(response.status).to eq(200)
end
end
@ -252,9 +252,8 @@ RSpec.describe Admin::UsersController do
it "also prevents use of any api keys" do
api_key = Fabricate(:api_key, user: user)
put "/posts/#{Fabricate(:post).id}/bookmark.json", params: {
bookmarked: "true"
post "/bookmarks.json", params: {
post_id: Fabricate(:post).id
}, headers: { HTTP_API_KEY: api_key.key }
expect(response.status).to eq(200)
@ -264,10 +263,9 @@ RSpec.describe Admin::UsersController do
user.reload
expect(user).to be_suspended
put "/posts/#{Fabricate(:post).id}/bookmark.json", params: {
bookmarked: "true",
api_key: api_key.key
}
post "/bookmarks.json", params: {
post_id: Fabricate(:post).id
}, headers: { HTTP_API_KEY: api_key.key }
expect(response.status).to eq(403)
end
end

View File

@ -482,131 +482,6 @@ describe PostsController do
end
end
describe '#bookmark' do
include_examples 'action requires login', :put, "/posts/2/bookmark.json"
let!(:post) { post_by_user }
describe 'when logged in' do
before do
sign_in(user)
end
fab!(:private_message) { Fabricate(:private_message_post) }
it "raises an error if the user doesn't have permission to see the post" do
put "/posts/#{private_message.id}/bookmark.json", params: { bookmarked: "true" }
expect(response).to be_forbidden
end
it 'creates a bookmark' do
put "/posts/#{post.id}/bookmark.json", params: { bookmarked: "true" }
expect(response.status).to eq(200)
post_action = PostAction.find_by(user: user, post: post)
expect(post_action.post_action_type_id).to eq(PostActionType.types[:bookmark])
end
context "removing a bookmark" do
let(:post_action) { PostActionCreator.create(user, post, :bookmark).post_action }
it "returns the right response when post is not bookmarked" do
put "/posts/#{post_by_user.id}/bookmark.json"
expect(response.status).to eq(404)
end
it "should be able to remove a bookmark" do
post_action
put "/posts/#{post.id}/bookmark.json"
expect(PostAction.find_by(id: post_action.id)).to eq(nil)
end
describe "when user doesn't have permission to see bookmarked post" do
it "should still be able to remove a bookmark" do
post_action
post = post_action.post
topic = post.topic
topic.convert_to_private_message(admin)
topic.remove_allowed_user(admin, user.username)
expect(Guardian.new(user).can_see_post?(post.reload)).to eq(false)
put "/posts/#{post.id}/bookmark.json"
expect(PostAction.find_by(id: post_action.id)).to eq(nil)
end
end
describe "when post has been deleted" do
it "should still be able to remove a bookmark" do
post = post_action.post
post.trash!
put "/posts/#{post.id}/bookmark.json"
expect(PostAction.find_by(id: post_action.id)).to eq(nil)
end
end
end
end
context "api" do
let(:api_key) { Fabricate(:api_key, user: user) }
let(:master_key) { Fabricate(:api_key, user: nil) }
# choosing an arbitrarily easy to mock trusted activity
it 'allows users with api key to bookmark posts' do
put "/posts/#{post.id}/bookmark.json",
params: { bookmarked: "true" },
headers: { HTTP_API_KEY: api_key.key }
expect(response.status).to eq(200)
expect(PostAction.where(
post: post,
user: user,
post_action_type_id: PostActionType.types[:bookmark]
).count).to eq(1)
end
it 'raises an error with a user key that does not match an optionally specified username' do
put "/posts/#{post.id}/bookmark.json",
params: { bookmarked: "true" },
headers: { HTTP_API_KEY: api_key.key, HTTP_API_USERNAME: 'made_up' }
expect(response.status).to eq(403)
end
it 'allows users with a master api key to bookmark posts' do
put "/posts/#{post.id}/bookmark.json",
params: { bookmarked: "true" },
headers: { HTTP_API_KEY: master_key.key, HTTP_API_USERNAME: user.username }
expect(response.status).to eq(200)
expect(PostAction.where(
post: post,
user: user,
post_action_type_id: PostActionType.types[:bookmark]
).count).to eq(1)
end
it 'disallows phonies to bookmark posts' do
put "/posts/#{post.id}/bookmark.json",
params: { bookmarked: "true" },
headers: { HTTP_API_KEY: SecureRandom.hex(32), HTTP_API_USERNAME: user.username }
expect(response.status).to eq(403)
end
it 'disallows blank api' do
put "/posts/#{post.id}/bookmark.json",
params: { bookmarked: "true" },
headers: { HTTP_API_KEY: "", HTTP_API_USERNAME: user.username }
expect(response.status).to eq(403)
end
end
end
describe '#wiki' do
include_examples "action requires login", :put, "/posts/2/wiki.json"

View File

@ -2346,19 +2346,15 @@ RSpec.describe TopicsController do
describe '#remove_bookmarks' do
it "should remove bookmarks properly from non first post" do
bookmark = PostActionType.types[:bookmark]
sign_in(user)
post = create_post
post2 = create_post(topic_id: post.topic_id)
PostActionCreator.new(user, post2, bookmark).perform
put "/t/#{post.topic_id}/bookmark.json"
expect(PostAction.where(user_id: user.id, post_action_type: bookmark).count).to eq(2)
Fabricate(:bookmark, user: user, post: post)
Fabricate(:bookmark, user: user, post: post2)
put "/t/#{post.topic_id}/remove_bookmarks.json"
expect(PostAction.where(user_id: user.id, post_action_type: bookmark).count).to eq(0)
expect(Bookmark.where(user: user).count).to eq(0)
end
it "should disallow bookmarks on posts you have no access to" do
@ -2369,10 +2365,7 @@ RSpec.describe TopicsController do
expect(response).to be_forbidden
end
context "when SiteSetting.enable_bookmarks_with_reminders is true" do
before do
SiteSetting.enable_bookmarks_with_reminders = true
end
context "bookmarks with reminders" do
it "deletes all the bookmarks for the user in the topic" do
sign_in(user)
post = create_post
@ -2388,26 +2381,23 @@ RSpec.describe TopicsController do
sign_in(user)
end
it "should create a new post action for the bookmark on the first post of the topic" do
it "should create a new bookmark on the first post of the topic" do
post = create_post
post2 = create_post(topic_id: post.topic_id)
put "/t/#{post.topic_id}/bookmark.json"
expect(PostAction.find_by(user_id: user.id, post_action_type: PostActionType.types[:bookmark]).post_id).to eq(post.id)
expect(Bookmark.find_by(user_id: user.id).post_id).to eq(post.id)
end
it "errors if the topic is already bookmarked for the user" do
post = create_post
PostActionCreator.new(user, post, PostActionType.types[:bookmark]).perform
Bookmark.create(post: post, user: user, topic: post.topic)
put "/t/#{post.topic_id}/bookmark.json"
expect(response).to be_forbidden
expect(response.status).to eq(400)
end
context "when SiteSetting.enable_bookmarks_with_reminders is true" do
before do
SiteSetting.enable_bookmarks_with_reminders = true
end
context "bookmarks with reminders" do
it "should create a new bookmark on the first post of the topic" do
post = create_post
post2 = create_post(topic_id: post.topic_id)