FIX: Make sure reminder_type is parsed on bookmark update (#9503)

Otherwise we are trying to update the reminder type with a string which often evaluates to 0 (At Desktop) which causes reminders to come through early.
This commit is contained in:
Martin Brennan 2020-04-22 10:44:04 +10:00 committed by GitHub
parent d1bee13fda
commit e18aeb799e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 9 deletions

View File

@ -9,7 +9,7 @@ class BookmarkManager
def create(post_id:, name: nil, reminder_type: nil, reminder_at: nil)
post = Post.unscoped.includes(:topic).find(post_id)
reminder_type = Bookmark.reminder_types[reminder_type.to_sym] if reminder_type.present?
reminder_type = parse_reminder_type(reminder_type)
raise Discourse::InvalidAccess.new if !Guardian.new(@user).can_see_post?(post)
@ -74,16 +74,20 @@ class BookmarkManager
raise Discourse::NotFound if bookmark.blank?
raise Discourse::InvalidAccess.new if !Guardian.new(@user).can_edit?(bookmark)
if bookmark.errors.any?
return add_errors_from(bookmark)
end
reminder_type = parse_reminder_type(reminder_type)
bookmark.update(
success = bookmark.update(
name: name,
reminder_at: reminder_at,
reminder_type: reminder_type,
reminder_set_at: Time.zone.now
)
if bookmark.errors.any?
return add_errors_from(bookmark)
end
success
end
private
@ -100,4 +104,9 @@ class BookmarkManager
def update_topic_user_bookmarked(topic:, bookmarked:)
TopicUser.change(@user.id, topic, bookmarked: bookmarked)
end
def parse_reminder_type(reminder_type)
return if reminder_type.blank?
reminder_type.is_a?(Integer) ? reminder_type : Bookmark.reminder_types[reminder_type.to_sym]
end
end

View File

@ -181,10 +181,19 @@ RSpec.describe BookmarkManager do
it "saves the time and new reminder type sucessfully" do
update_bookmark
bookmark.reload
expect(bookmark.name).to eq(new_name)
expect(bookmark.reminder_at).to eq_time(new_reminder_at)
expect(bookmark.reminder_type).to eq(new_reminder_type)
bookmark.reload
expect(bookmark.name).to eq(new_name)
expect(bookmark.reminder_at).to eq_time(new_reminder_at)
expect(bookmark.reminder_type).to eq(new_reminder_type)
end
context "if the new reminder type is a string" do
let(:new_reminder_type) { "custom" }
it "is parsed" do
update_bookmark
bookmark.reload
expect(bookmark.reminder_type).to eq(Bookmark.reminder_types[:custom])
end
end
context "if the bookmark is belonging to some other user" do