DEV: Ignore reminder_type for bookmarks (#14349)

DEV: Ignore reminder_type for bookmarks (#14349)

We don’t actually use the reminder_type for bookmarks anywhere; we are just storing it. It has no bearing on the UI. It used to be relevant with the at_desktop bookmark reminders (see fa572d3a7a6a3e107fdbc5f00e04d2bb990ab77d)

This commit marks the column as readonly, ignores it, and removes the index, and it will be dropped in a later PR. Some plugins are relying on reminder_type partially so some stubs have been left in place to avoid errors.

diff --git a/app/assets/javascripts/discourse/app/lib/transform-post.js b/app/assets/javascripts/discourse/app/lib/transform-post.js
index b9695b5..8f61645 100644
--- a/app/assets/javascripts/discourse/app/lib/transform-post.js
+++ b/app/assets/javascripts/discourse/app/lib/transform-post.js
@@ -39,7 +39,6 @@ export function transformBasicPost(post) {
     bookmarked: post.bookmarked,
     bookmarkReminderAt: post.bookmark_reminder_at,
     bookmarkName: post.bookmark_name,
-    bookmarkReminderType: post.bookmark_reminder_type,
     yours: post.yours,
     shareUrl: post.get("shareUrl"),
     staff: post.staff,
diff --git a/app/controllers/bookmarks_controller.rb b/app/controllers/bookmarks_controller.rb
index c0645e2..f8b68be 100644
--- a/app/controllers/bookmarks_controller.rb
+++ b/app/controllers/bookmarks_controller.rb
@@ -14,7 +14,6 @@ class BookmarksController < ApplicationController
     bookmark = bookmark_manager.create(
       post_id: params[:post_id],
       name: params[:name],
-      reminder_type: params[:reminder_type],
       reminder_at: params[:reminder_at],
       options: {
         auto_delete_preference: params[:auto_delete_preference] || 0
@@ -41,7 +40,6 @@ class BookmarksController < ApplicationController
     bookmark_manager.update(
       bookmark_id: params[:id],
       name: params[:name],
-      reminder_type: params[:reminder_type],
       reminder_at: params[:reminder_at],
       options: {
         auto_delete_preference: params[:auto_delete_preference] || 0
diff --git a/app/jobs/regular/export_user_archive.rb b/app/jobs/regular/export_user_archive.rb
index 7a8e4b1..4ff3f1f 100644
--- a/app/jobs/regular/export_user_archive.rb
+++ b/app/jobs/regular/export_user_archive.rb
@@ -31,7 +31,7 @@ module Jobs
       auth_tokens: ['id', 'auth_token_hash', 'prev_auth_token_hash', 'auth_token_seen', 'client_ip', 'user_agent', 'seen_at', 'rotated_at', 'created_at', 'updated_at'],
       auth_token_logs: ['id', 'action', 'user_auth_token_id', 'client_ip', 'auth_token_hash', 'created_at', 'path', 'user_agent'],
       badges: ['badge_id', 'badge_name', 'granted_at', 'post_id', 'seq', 'granted_manually', 'notification_id', 'featured_rank'],
-      bookmarks: ['post_id', 'topic_id', 'post_number', 'link', 'name', 'created_at', 'updated_at', 'reminder_type', 'reminder_at', 'reminder_last_sent_at', 'reminder_set_at', 'auto_delete_preference'],
+      bookmarks: ['post_id', 'topic_id', 'post_number', 'link', 'name', 'created_at', 'updated_at', 'reminder_at', 'reminder_last_sent_at', 'reminder_set_at', 'auto_delete_preference'],
       category_preferences: ['category_id', 'category_names', 'notification_level', 'dismiss_new_timestamp'],
       flags: ['id', 'post_id', 'flag_type', 'created_at', 'updated_at', 'deleted_at', 'deleted_by', 'related_post_id', 'targets_topic', 'was_take_action'],
       likes: ['id', 'post_id', 'topic_id', 'post_number', 'created_at', 'updated_at', 'deleted_at', 'deleted_by'],
@@ -248,7 +248,6 @@ module Jobs
           bkmk.name,
           bkmk.created_at,
           bkmk.updated_at,
-          Bookmark.reminder_types[bkmk.reminder_type],
           bkmk.reminder_at,
           bkmk.reminder_last_sent_at,
           bkmk.reminder_set_at,
diff --git a/app/models/bookmark.rb b/app/models/bookmark.rb
index 69219cf..14f9eba 100644
--- a/app/models/bookmark.rb
+++ b/app/models/bookmark.rb
@@ -2,7 +2,8 @@
 
 class Bookmark < ActiveRecord::Base
   self.ignored_columns = [
-    "topic_id" # TODO (martin) (2021-12-01): remove
+    "topic_id", # TODO (martin) (2021-12-01): remove
+    "reminder_type" # TODO (martin) (2021-12-01): remove
   ]
 
   belongs_to :user
@@ -11,6 +12,15 @@ class Bookmark < ActiveRecord::Base
 
   delegate :topic_id, to: :post
 
+  def self.auto_delete_preferences
+    @auto_delete_preferences ||= Enum.new(
+      never: 0,
+      when_reminder_sent: 1,
+      on_owner_reply: 2
+    )
+  end
+
+  # TODO (martin) (2021-12-01) Remove this once plugins are not using it.
   def self.reminder_types
     @reminder_types ||= Enum.new(
       later_today: 1,
@@ -24,19 +34,6 @@ class Bookmark < ActiveRecord::Base
     )
   end
 
-  def self.auto_delete_preferences
-    @auto_delete_preferences ||= Enum.new(
-      never: 0,
-      when_reminder_sent: 1,
-      on_owner_reply: 2
-    )
-  end
-
-  validates :reminder_at, presence: {
-    message: I18n.t("bookmarks.errors.time_must_be_provided"),
-    if: -> { reminder_type.present? }
-  }
-
   validate :unique_per_post_for_user,
     on: [:create, :update],
     if: Proc.new { |b| b.will_save_change_to_post_id? || b.will_save_change_to_user_id? }
@@ -83,7 +80,7 @@ class Bookmark < ActiveRecord::Base
   end
 
   def no_reminder?
-    self.reminder_at.blank? && self.reminder_type.blank?
+    self.reminder_at.blank?
   end
 
   def auto_delete_when_reminder_sent?
@@ -101,7 +98,6 @@ class Bookmark < ActiveRecord::Base
   def clear_reminder!
     update!(
       reminder_at: nil,
-      reminder_type: nil,
       reminder_last_sent_at: Time.zone.now,
       reminder_set_at: nil
     )
@@ -173,7 +169,6 @@ end
 #  user_id                :bigint           not null
 #  post_id                :bigint           not null
 #  name                   :string(100)
-#  reminder_type          :integer
 #  reminder_at            :datetime
 #  created_at             :datetime         not null
 #  updated_at             :datetime         not null
@@ -188,7 +183,6 @@ end
 #  index_bookmarks_on_post_id                            (post_id)
 #  index_bookmarks_on_reminder_at                        (reminder_at)
 #  index_bookmarks_on_reminder_set_at                    (reminder_set_at)
-#  index_bookmarks_on_reminder_type                      (reminder_type)
 #  index_bookmarks_on_topic_id                           (topic_id)
 #  index_bookmarks_on_user_id                            (user_id)
 #  index_bookmarks_on_user_id_and_post_id_and_for_topic  (user_id,post_id,for_topic) UNIQUE
diff --git a/app/serializers/post_serializer.rb b/app/serializers/post_serializer.rb
index d585090..8de921e 100644
--- a/app/serializers/post_serializer.rb
+++ b/app/serializers/post_serializer.rb
@@ -53,7 +53,6 @@ class PostSerializer < BasicPostSerializer
              :bookmarked,
              :bookmark_reminder_at,
              :bookmark_id,
-             :bookmark_reminder_type,
              :bookmark_name,
              :bookmark_auto_delete_preference,
              :raw,
@@ -346,10 +345,6 @@ class PostSerializer < BasicPostSerializer
     bookmarked
   end
 
-  def include_bookmark_reminder_type?
-    bookmarked
-  end
-
   def include_bookmark_name?
     bookmarked
   end
@@ -374,11 +369,6 @@ class PostSerializer < BasicPostSerializer
     post_bookmark&.reminder_at
   end
 
-  def bookmark_reminder_type
-    return if post_bookmark.blank?
-    Bookmark.reminder_types[post_bookmark.reminder_type].to_s
-  end
-
   def bookmark_name
     post_bookmark&.name
   end
diff --git a/db/migrate/20210913032326_add_for_topic_to_bookmarks.rb b/db/migrate/20210913032326_add_for_topic_to_bookmarks.rb
index 2660cf6..84524f5 100644
--- a/db/migrate/20210913032326_add_for_topic_to_bookmarks.rb
+++ b/db/migrate/20210913032326_add_for_topic_to_bookmarks.rb
@@ -4,6 +4,8 @@ class AddForTopicToBookmarks < ActiveRecord::Migration[6.1]
   def change
     add_column :bookmarks, :for_topic, :boolean, default: false, null: false
     add_index :bookmarks, [:user_id, :post_id, :for_topic], unique: true
-    remove_index :bookmarks, [:user_id, :post_id]
+    if index_exists?(:bookmarks, [:user_id, :post_id])
+      remove_index :bookmarks, [:user_id, :post_id]
+    end
   end
 end
diff --git a/db/migrate/20210915215952_mark_reminder_type_as_readonly_for_bookmarks.rb b/db/migrate/20210915215952_mark_reminder_type_as_readonly_for_bookmarks.rb
new file mode 100644
index 0000000..83f171d
--- /dev/null
+++ b/db/migrate/20210915215952_mark_reminder_type_as_readonly_for_bookmarks.rb
@@ -0,0 +1,11 @@

[... diff too long, it was truncated ...]

GitHub sha: 41e19adb0d7685303009650efec3ebec0319c577

This commit appears in #14349 which was approved by lis2. It was merged by martin.