A platform for community discussion. Free, open, simple.
FEATURE: Highlight expired bookmark reminders (#15317)
Bianca Nenciu6d422a8033fb
diff --git a/app/assets/javascripts/discourse/app/components/bookmark-actions-dropdown.js b/app/assets/javascripts/discourse/app/components/bookmark-actions-dropdown.jsindex e039f4d5aea..f20f9ddc1e4 100644--- a/app/assets/javascripts/discourse/app/components/bookmark-actions-dropdown.js+++ b/app/assets/javascripts/discourse/app/components/bookmark-actions-dropdown.js@@ -5,6 +5,7 @@ import I18n from "I18n";const ACTION_REMOVE = "remove";const ACTION_EDIT = "edit";+const ACTION_CLEAR_REMINDER = "clear_reminder";const ACTION_PIN = "pin";export default DropdownSelectBoxComponent.extend({@@ -18,32 +19,45 @@ export default DropdownSelectBoxComponent.extend({@discourseComputed("bookmark")content(bookmark) {- return [- {- id: ACTION_REMOVE,- icon: "trash-alt",- name: I18n.t("post.bookmarks.actions.delete_bookmark.name"),- description: I18n.t(- "post.bookmarks.actions.delete_bookmark.description"- ),- },- {- id: ACTION_EDIT,- icon: "pencil-alt",- name: I18n.t("post.bookmarks.actions.edit_bookmark.name"),- description: I18n.t("post.bookmarks.actions.edit_bookmark.description"),- },- {- id: ACTION_PIN,- icon: "thumbtack",- name: I18n.t(- `post.bookmarks.actions.${bookmark.pinAction()}_bookmark.name`- ),+ const actions = [];++ actions.push({+ id: ACTION_REMOVE,+ icon: "trash-alt",+ name: I18n.t("post.bookmarks.actions.delete_bookmark.name"),+ description: I18n.t("post.bookmarks.actions.delete_bookmark.description"),+ });++ actions.push({+ id: ACTION_EDIT,+ icon: "pencil-alt",+ name: I18n.t("post.bookmarks.actions.edit_bookmark.name"),+ description: I18n.t("post.bookmarks.actions.edit_bookmark.description"),+ });++ if (bookmark.reminder_at) {+ actions.push({+ id: ACTION_CLEAR_REMINDER,+ icon: "history",+ name: I18n.t("post.bookmarks.actions.clear_bookmark_reminder.name"),description: I18n.t(- `post.bookmarks.actions.${bookmark.pinAction()}_bookmark.description`+ "post.bookmarks.actions.clear_bookmark_reminder.description"),- },- ];+ });+ }++ actions.push({+ id: ACTION_PIN,+ icon: "thumbtack",+ name: I18n.t(+ `post.bookmarks.actions.${bookmark.pinAction()}_bookmark.name`+ ),+ description: I18n.t(+ `post.bookmarks.actions.${bookmark.pinAction()}_bookmark.description`+ ),+ });++ return actions;},@action@@ -52,6 +66,8 @@ export default DropdownSelectBoxComponent.extend({this.removeBookmark(this.bookmark);} else if (selectedAction === ACTION_EDIT) {this.editBookmark(this.bookmark);+ } else if (selectedAction === ACTION_CLEAR_REMINDER) {+ this.clearBookmarkReminder(this.bookmark);} else if (selectedAction === ACTION_PIN) {this.togglePinBookmark(this.bookmark);}diff --git a/app/assets/javascripts/discourse/app/components/bookmark-list.js b/app/assets/javascripts/discourse/app/components/bookmark-list.jsindex cafebfb4ec5..066a9019cde 100644--- a/app/assets/javascripts/discourse/app/components/bookmark-list.js+++ b/app/assets/javascripts/discourse/app/components/bookmark-list.js@@ -4,6 +4,7 @@ import { schedule } from "@ember/runloop";import bootbox from "bootbox";import discourseDebounce from "discourse-common/lib/debounce";import { openBookmarkModal } from "discourse/controllers/bookmark";+import { ajax } from "discourse/lib/ajax";import {openLinkInNewTab,shouldOpenInNewTab,@@ -107,6 +108,16 @@ export default Component.extend(Scrolling, {});},+ @action+ clearBookmarkReminder(bookmark) {+ return ajax(`/bookmarks/${bookmark.id}`, {+ type: "PUT",+ data: { reminder_at: null },+ }).then(() => {+ bookmark.set("reminder_at", null);+ });+ },+@actiontogglePinBookmark(bookmark) {bookmark.togglePin().then(this.reload);diff --git a/app/assets/javascripts/discourse/app/controllers/topic.js b/app/assets/javascripts/discourse/app/controllers/topic.jsindex 3354a0cfaaa..1398450d050 100644--- a/app/assets/javascripts/discourse/app/controllers/topic.js+++ b/app/assets/javascripts/discourse/app/controllers/topic.js@@ -767,6 +767,8 @@ export default Controller.extend(bufferedProperty("model"), {post_id: post.id,topic_id: post.topic_id,for_topic: false,+ auto_delete_preference: this.currentUser+ .bookmark_auto_delete_preference,}),post);@@ -1320,6 +1322,8 @@ export default Controller.extend(bufferedProperty("model"), {post_id: firstPost.id,topic_id: this.model.id,for_topic: true,+ auto_delete_preference: this.currentUser+ .bookmark_auto_delete_preference,}));}diff --git a/app/assets/javascripts/discourse/app/models/bookmark.js b/app/assets/javascripts/discourse/app/models/bookmark.jsindex bbe656d63b2..8852409a773 100644--- a/app/assets/javascripts/discourse/app/models/bookmark.js+++ b/app/assets/javascripts/discourse/app/models/bookmark.js@@ -14,6 +14,7 @@ import { none } from "@ember/object/computed";export const AUTO_DELETE_PREFERENCES = {NEVER: 0,+ CLEAR_REMINDER: 3,WHEN_REMINDER_SENT: 1,ON_OWNER_REPLY: 2,};@@ -129,6 +130,11 @@ const Bookmark = RestModel.extend({).capitalize();},+ @discourseComputed("reminder_at")+ reminderAtExpired(bookmarkReminderAt) {+ return moment(bookmarkReminderAt) < moment();+ },+@discourseComputed()topicForList() {// for topic level bookmarks we want to jump to the last unread post URL,diff --git a/app/assets/javascripts/discourse/app/models/user.js b/app/assets/javascripts/discourse/app/models/user.jsindex 1e773db6489..22108d871cc 100644--- a/app/assets/javascripts/discourse/app/models/user.js+++ b/app/assets/javascripts/discourse/app/models/user.js@@ -98,6 +98,7 @@ let userOptionFields = ["timezone","skip_new_user_tips","default_calendar",+ "bookmark_auto_delete_preference",];export function addSaveableUserOptionField(fieldName) {diff --git a/app/assets/javascripts/discourse/app/templates/components/bookmark-list.hbs b/app/assets/javascripts/discourse/app/templates/components/bookmark-list.hbsindex a5e6d62b177..819b95296ae 100644--- a/app/assets/javascripts/discourse/app/templates/components/bookmark-list.hbs+++ b/app/assets/javascripts/discourse/app/templates/components/bookmark-list.hbs@@ -17,7 +17,7 @@<span class="link-top-line"><div class="bookmark-metadata">{{#if bookmark.reminder_at}}- <span class="bookmark-metadata-item">+ <span class="bookmark-metadata-item bookmark-reminder {{if bookmark.reminderAtExpired "bookmark-expired-reminder"}}">{{d-icon "far-clock"}}{{bookmark.formattedReminder}}</span>{{/if}}@@ -63,6 +63,7 @@bookmark=bookmarkremoveBookmark=(action "removeBookmark")editBookmark=(action "editBookmark")+ clearBookmarkReminder=(action "clearBookmarkReminder")togglePinBookmark=(action "togglePinBookmark")}}</td>diff --git a/app/assets/javascripts/discourse/tests/acceptance/user-bookmarks-test.js b/app/assets/javascripts/discourse/tests/acceptance/user-bookmarks-test.jsindex faa7418de2a..d7eedfdd23a 100644--- a/app/assets/javascripts/discourse/tests/acceptance/user-bookmarks-test.js+++ b/app/assets/javascripts/discourse/tests/acceptance/user-bookmarks-test.js@@ -34,6 +34,10 @@ acceptance("User's bookmarks - reminder", function (needs) {json.user_bookmark_list.bookmarks[0].reminder_at = "2028-01-01T08:00";return helper.response(json);});++ server.put("/bookmarks/:id", () => {+ return helper.response({});+ });});test("removing a bookmark with a reminder shows a confirmation", async function (assert) {@@ -48,6 +52,18 @@ acceptance("User's bookmarks - reminder", function (needs) {await click(".bootbox.modal a.btn-primary");assert.notOk(exists(".bootbox.modal"));});++ test("bookmarks with reminders have a clear reminder option", async function (assert) {+ await visit("/u/eviltrout/activity/bookmarks");++ assert.ok(exists(".bookmark-reminder"));++ const dropdown = selectKit(".bookmark-actions-dropdown");+ await dropdown.expand();+ await dropdown.selectRowByValue("clear_reminder");++ assert.not(exists(".bookmark-reminder"));+ });});acceptance("User's bookmarks - no bookmarks", function (needs) {diff --git a/app/assets/stylesheets/common/components/bookmark-list.scss b/app/assets/stylesheets/common/components/bookmark-list.scssindex d3bbca3a1a0..7a1dc0d9c3e 100644--- a/app/assets/stylesheets/common/components/bookmark-list.scss+++ b/app/assets/stylesheets/common/components/bookmark-list.scss@@ -38,6 +38,10 @@ $mobile-breakpoint: 700px;span {word-break: break-word;}++ &.bookmark-expired-reminder {+ color: var(--danger);+ }}.d-icon {diff --git a/app/jobs/scheduled/bookmark_reminder_notifications.rb b/app/jobs/scheduled/bookmark_reminder_notifications.rbindex 0795e607bca..bf84ff16124 100644--- a/app/jobs/scheduled/bookmark_reminder_notifications.rb+++ b/app/jobs/scheduled/bookmark_reminder_notifications.rb@@ -18,7 +18,7 @@ module Jobsenddef execute(args = nil)- bookmarks = Bookmark.pending_reminders.includes(:user).order('reminder_at ASC')+ bookmarks = Bookmark.pending_reminders.includes(:user).where(reminder_last_sent_at: nil).order('reminder_at ASC')bookmarks.limit(BookmarkReminderNotifications.max_reminder_notifications_per_run).each do |bookmark|BookmarkReminderNotificationHandler.send_notification(bookmark)enddiff --git a/app/models/bookmark.rb b/app/models/bookmark.rbindex 9798acafc53..b181d9fd339 100644--- a/app/models/bookmark.rb+++ b/app/models/bookmark.rb@@ -21,7 +21,8 @@ class Bookmark < ActiveRecord::Base@auto_delete_preferences ||= Enum.new(never: 0,when_reminder_sent: 1,- on_owner_reply: 2+ on_owner_reply: 2,+ clear_reminder: 3,)end@@ -33,7 +34,7 @@ class Bookmark < ActiveRecord::Baseon: [:create, :update],if: Proc.new { |b| b.will_save_change_to_post_id? || b.will_save_change_to_for_topic? }- validate :ensure_sane_reminder_at_time+ validate :ensure_sane_reminder_at_time, if: :will_save_change_to_reminder_at?validate :bookmark_limit_not_reachedvalidates :name, length: { maximum: 100 }@@ -83,10 +84,6 @@ class Bookmark < ActiveRecord::Base@is_for_first_post ||= new_record? ? Post.exists?(id: post_id, post_number: 1) : post.post_number == 1end- def no_reminder?- self.reminder_at.blank?- end-def auto_delete_when_reminder_sent?self.auto_delete_preference == Bookmark.auto_delete_preferences[:when_reminder_sent]end@@ -95,15 +92,18 @@ class Bookmark < ActiveRecord::Baseself.auto_delete_preference == Bookmark.auto_delete_preferences[:on_owner_reply]end+ def auto_clear_reminder_when_reminder_sent?+ self.auto_delete_preference == Bookmark.auto_delete_preferences[:clear_reminder]+ end+def reminder_at_ics(offset: 0)(reminder_at + offset).strftime(I18n.t("datetime_formats.formats.calendar_ics"))enddef clear_reminder!update!(- reminder_at: nil,reminder_last_sent_at: Time.zone.now,- reminder_set_at: nil+ reminder_set_at: nil,)enddiff --git a/app/models/user_option.rb b/app/models/user_option.rbindex 4c01632a11a..2118b101973 100644--- a/app/models/user_option.rb+++ b/app/models/user_option.rb@@ -260,6 +260,7 @@ end# color_scheme_id :integer# default_calendar :integer default("none_selected"), not null# oldest_search_log_date :datetime+# bookmark_auto_delete_preference :integer default(3), not null## Indexes#diff --git a/app/serializers/current_user_serializer.rb b/app/serializers/current_user_serializer.rbindex b521e98752a..a5988d2ada1 100644--- a/app/serializers/current_user_serializer.rb+++ b/app/serializers/current_user_serializer.rb@@ -68,6 +68,7 @@ class CurrentUserSerializer < BasicUserSerializer:can_review,:draft_count,:default_calendar,+ :bookmark_auto_delete_preference,:pending_posts_countdelegate :user_stat, to: :object, private: true@@ -142,6 +143,10 @@ class CurrentUserSerializer < BasicUserSerializerobject.user_option.default_calendarend+ def bookmark_auto_delete_preference+ object.user_option.bookmark_auto_delete_preference+ end+def can_send_private_email_messagesscope.can_send_private_messages_to_email?enddiff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex 4fa86486cd9..2ae922540b7 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -330,10 +330,11 @@ en:list_permission_denied: "You do not have permission to view this user's bookmarks."no_user_bookmarks: "You have no bookmarked posts; bookmarks allow you to quickly refer to specific posts."auto_delete_preference:- label: "Automatically delete"- never: "Never"- when_reminder_sent: "Once the reminder is sent"- on_owner_reply: "After I reply to this topic"+ label: "After you are notified"+ never: "Keep bookmark"+ when_reminder_sent: "Delete bookmark"+ on_owner_reply: "Delete bookmark, once I reply"+ clear_reminder: "Keep bookmark and clear reminder"search_placeholder: "Search bookmarks by name, topic title, or post content"search: "Search"reminders:@@ -3268,6 +3269,9 @@ en:edit_bookmark:name: "Edit bookmark"description: "Edit the bookmark name or change the reminder date and time"+ clear_bookmark_reminder:+ name: "Clear reminder"+ description: "Clear the reminder date and time"pin_bookmark:name: "Pin bookmark"description: "Pin the bookmark. This will make it appear at the top of your bookmarks list."diff --git a/db/migrate/20220209210449_add_bookmark_auto_delete_preference_to_user_option.rb b/db/migrate/20220209210449_add_bookmark_auto_delete_preference_to_user_option.rbnew file mode 100644index 00000000000..856578713cf--- /dev/null+++ b/db/migrate/20220209210449_add_bookmark_auto_delete_preference_to_user_option.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++class AddBookmarkAutoDeletePreferenceToUserOption < ActiveRecord::Migration[6.1]+ def change+ add_column :user_options, :bookmark_auto_delete_preference, :integer, default: 3, null: false+ end+enddiff --git a/lib/bookmark_manager.rb b/lib/bookmark_manager.rbindex 3d9323ecff2..e4d5db5885d 100644--- a/lib/bookmark_manager.rb+++ b/lib/bookmark_manager.rb@@ -65,6 +65,7 @@ class BookmarkManagerendupdate_topic_user_bookmarked(post.topic)+ update_user_option(bookmark)bookmarkend@@ -101,11 +102,15 @@ class BookmarkManagerdef update(bookmark_id:, name:, reminder_at:, options: {})bookmark = find_bookmark_and_check_access(bookmark_id)+ if bookmark.reminder_at != reminder_at+ bookmark.reminder_at = reminder_at+ bookmark.reminder_last_sent_at = nil+ end+success = bookmark.update({name: name,- reminder_at: reminder_at,- reminder_set_at: Time.zone.now+ reminder_set_at: Time.zone.now,}.merge(options))@@ -113,6 +118,8 @@ class BookmarkManagerreturn add_errors_from(bookmark)end+ update_user_option(bookmark)+successend@@ -146,4 +153,8 @@ class BookmarkManagerTopicUser.change(@user.id, topic, bookmarked: bookmarks_remaining_in_topic)bookmarks_remaining_in_topicend++ def update_user_option(bookmark)+ @user.user_option.update!(bookmark_auto_delete_preference: bookmark.auto_delete_preference)+ endenddiff --git a/lib/bookmark_reminder_notification_handler.rb b/lib/bookmark_reminder_notification_handler.rbindex 28810ce7758..c0aa82d79be 100644--- a/lib/bookmark_reminder_notification_handler.rb+++ b/lib/bookmark_reminder_notification_handler.rb@@ -26,6 +26,10 @@ class BookmarkReminderNotificationHandler"Clearing bookmark reminder for bookmark_id #{bookmark.id}. reminder at: #{bookmark.reminder_at}")+ if bookmark.auto_clear_reminder_when_reminder_sent?+ bookmark.reminder_at = nil+ end+bookmark.clear_reminder!enddiff --git a/spec/jobs/bookmark_reminder_notifications_spec.rb b/spec/jobs/bookmark_reminder_notifications_spec.rbindex 1e4ff66565b..6d64c9f0093 100644--- a/spec/jobs/bookmark_reminder_notifications_spec.rb+++ b/spec/jobs/bookmark_reminder_notifications_spec.rb@@ -23,16 +23,13 @@ RSpec.describe Jobs::BookmarkReminderNotifications doDiscourse.redis.flushdbend- it "sends every reminder and marks the reminder_at to nil for all bookmarks, as well as last sent date" do+ it "sends every reminder and sets the reminder_last_sent_at" dosubject.executebookmark1.reloadbookmark2.reloadbookmark3.reload- expect(bookmark1.reminder_at).to eq(nil)expect(bookmark1.reminder_last_sent_at).not_to eq(nil)- expect(bookmark2.reminder_at).to eq(nil)expect(bookmark2.reminder_last_sent_at).not_to eq(nil)- expect(bookmark3.reminder_at).to eq(nil)expect(bookmark3.reminder_last_sent_at).not_to eq(nil)end@@ -60,9 +57,9 @@ RSpec.describe Jobs::BookmarkReminderNotifications dobeginJobs::BookmarkReminderNotifications.max_reminder_notifications_per_run = 2subject.execute- expect(bookmark1.reload.reminder_at).to eq(nil)- expect(bookmark2.reload.reminder_at).to eq(nil)- expect(bookmark3.reload.reminder_at).not_to eq(nil)+ expect(bookmark1.reload.reminder_last_sent_at).not_to eq(nil)+ expect(bookmark2.reload.reminder_last_sent_at).not_to eq(nil)+ expect(bookmark3.reload.reminder_last_sent_at).to eq(nil)endendenddiff --git a/spec/lib/bookmark_manager_spec.rb b/spec/lib/bookmark_manager_spec.rbindex a6de9fb2ec8..e5980162122 100644--- a/spec/lib/bookmark_manager_spec.rb+++ b/spec/lib/bookmark_manager_spec.rb@@ -133,6 +133,15 @@ RSpec.describe BookmarkManager doexpect { subject.create(post_id: post.id, name: name) }.to raise_error(Discourse::InvalidAccess)endend++ 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])+ endenddescribe ".destroy" do@@ -191,7 +200,14 @@ RSpec.describe BookmarkManager doupdate_bookmarkbookmark.reloadexpect(bookmark.name).to eq(new_name)- expect(bookmark.reminder_at).to eq_time(new_reminder_at)+ expect(bookmark.reminder_last_sent_at).to eq(nil)+ end++ it "does not reminder_last_sent_at if reminder did not change" do+ bookmark.update(reminder_last_sent_at: 1.day.ago)+ subject.update(bookmark_id: bookmark.id, name: new_name, reminder_at: bookmark.reminder_at)+ bookmark.reload+ expect(bookmark.reminder_last_sent_at).not_to eq(nil)endcontext "when options are provided" do@@ -248,11 +264,10 @@ RSpec.describe BookmarkManager dodescribe ".send_reminder_notification" dolet(:bookmark) { Fabricate(:bookmark, user: user) }- it "clears the reminder_at and sets the reminder_last_sent_at" do+ it "sets the reminder_last_sent_at" doexpect(bookmark.reminder_last_sent_at).to eq(nil)described_class.send_reminder_notification(bookmark.id)bookmark.reload- expect(bookmark.reminder_at).to eq(nil)expect(bookmark.reminder_last_sent_at).not_to eq(nil)end@@ -276,10 +291,9 @@ RSpec.describe BookmarkManager dobefore dobookmark.post.trash!end- it "does not error, and does not create a notification, and clears the reminder" do+ it "does not error and does not create a notification" dodescribed_class.send_reminder_notification(bookmark.id)bookmark.reload- expect(bookmark.reminder_at).to eq(nil)expect(notifications_for_user.any?).to eq(false)endenddiff --git a/spec/lib/bookmark_reminder_notification_handler_spec.rb b/spec/lib/bookmark_reminder_notification_handler_spec.rbindex 9e7dc0e06ca..aa26c4d6bd3 100644--- a/spec/lib/bookmark_reminder_notification_handler_spec.rb+++ b/spec/lib/bookmark_reminder_notification_handler_spec.rb@@ -26,20 +26,15 @@ RSpec.describe BookmarkReminderNotificationHandler doexpect(data["bookmark_name"]).to eq(bookmark.name)end- it "clears the reminder" do- subject.send_notification(bookmark)- expect(bookmark.reload.no_reminder?).to eq(true)- end-context "when the topic is deleted" dobefore dobookmark.topic.trash!bookmark.reloadend- it "does not send a notification and clears the reminder" do+ it "does not send a notification and updates last notification attempt time" doexpect { subject.send_notification(bookmark) }.not_to change { Notification.count }- expect(bookmark.reload.no_reminder?).to eq(true)+ expect(bookmark.reload.reminder_last_sent_at).not_to be_blankendend@@ -49,9 +44,9 @@ RSpec.describe BookmarkReminderNotificationHandler dobookmark.reloadend- it "does not send a notification and clears the reminder" do+ it "does not send a notification and updates last notification attempt time" doexpect { subject.send_notification(bookmark) }.not_to change { Notification.count }- expect(bookmark.reload.no_reminder?).to eq(true)+ expect(bookmark.reload.reminder_last_sent_at).not_to be_blankendend@@ -83,12 +78,24 @@ RSpec.describe BookmarkReminderNotificationHandler doendend+ context "when the auto_delete_preference is clear_reminder" do+ before do+ TopicUser.create!(topic: bookmark.topic, user: user, bookmarked: true)+ bookmark.update(auto_delete_preference: Bookmark.auto_delete_preferences[:clear_reminder])+ end++ it "resets reminder_at after the reminder gets sent" do+ subject.send_notification(bookmark)+ expect(Bookmark.find_by(id: bookmark.id).reminder_at).to eq(nil)+ end+ end+context "when the post has been deleted" do- it "clears the reminder and does not send a notification" do+ it "does not send a notification" dobookmark.post.trash!bookmark.reload- subject.send_notification(bookmark)- expect(bookmark.reload.no_reminder?).to eq(true)+ expect { subject.send_notification(bookmark) }.not_to change { Notification.count }+ expect(bookmark.reload.reminder_last_sent_at).not_to be_blankendendend