2019-12-10 22:04:02 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Bookmark < ActiveRecord::Base
|
2023-03-07 18:39:12 -06:00
|
|
|
DEFAULT_BOOKMARKABLES = [
|
|
|
|
RegisteredBookmarkable.new(PostBookmarkable),
|
|
|
|
RegisteredBookmarkable.new(TopicBookmarkable),
|
|
|
|
]
|
2022-04-21 17:23:42 -05:00
|
|
|
|
2023-03-07 18:39:12 -06:00
|
|
|
def self.registered_bookmarkables
|
|
|
|
Set.new(DEFAULT_BOOKMARKABLES | DiscoursePluginRegistry.bookmarkables)
|
2022-04-21 17:23:42 -05:00
|
|
|
end
|
|
|
|
|
2023-03-07 18:39:12 -06:00
|
|
|
def self.registered_bookmarkable_from_type(type)
|
2023-03-20 04:19:28 -05:00
|
|
|
begin
|
|
|
|
resolved_type = Bookmark.polymorphic_class_for(type).name
|
|
|
|
Bookmark.registered_bookmarkables.find { |bm| bm.model.name == resolved_type }
|
|
|
|
|
|
|
|
# If the class cannot be found from the provided type using polymorphic_class_for,
|
|
|
|
# then the type is not valid and thus there will not be any registered bookmarkable.
|
|
|
|
rescue NameError
|
|
|
|
end
|
2022-04-21 17:23:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.valid_bookmarkable_types
|
2023-04-17 08:41:56 -05:00
|
|
|
Bookmark.registered_bookmarkables.map { |bm| bm.model.polymorphic_name }
|
2022-04-21 17:23:42 -05:00
|
|
|
end
|
|
|
|
|
2019-12-10 22:04:02 -06:00
|
|
|
belongs_to :user
|
2022-01-05 16:56:05 -06:00
|
|
|
belongs_to :bookmarkable, polymorphic: true
|
2021-09-14 19:16:54 -05:00
|
|
|
|
2021-09-15 18:56:54 -05:00
|
|
|
def self.auto_delete_preferences
|
2023-01-09 06:20:10 -06:00
|
|
|
@auto_delete_preferences ||=
|
|
|
|
Enum.new(never: 0, when_reminder_sent: 1, on_owner_reply: 2, clear_reminder: 3)
|
2021-09-15 18:56:54 -05:00
|
|
|
end
|
|
|
|
|
2022-04-21 17:23:42 -05:00
|
|
|
def self.select_type(bookmarks_relation, type)
|
|
|
|
bookmarks_relation.select { |bm| bm.bookmarkable_type == type }
|
|
|
|
end
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
validate :polymorphic_columns_present, on: %i[create update]
|
|
|
|
validate :valid_bookmarkable_type, on: %i[create update]
|
2022-03-29 21:43:11 -05:00
|
|
|
|
|
|
|
validate :unique_per_bookmarkable,
|
2023-01-09 06:20:10 -06:00
|
|
|
on: %i[create update],
|
|
|
|
if:
|
|
|
|
Proc.new { |b|
|
|
|
|
b.will_save_change_to_bookmarkable_id? || b.will_save_change_to_bookmarkable_type? ||
|
|
|
|
b.will_save_change_to_user_id?
|
|
|
|
}
|
2022-03-29 21:43:11 -05:00
|
|
|
|
2022-03-08 11:44:18 -06:00
|
|
|
validate :ensure_sane_reminder_at_time, if: :will_save_change_to_reminder_at?
|
2021-01-18 16:53:49 -06:00
|
|
|
validate :bookmark_limit_not_reached
|
2020-07-08 02:19:01 -05:00
|
|
|
validates :name, length: { maximum: 100 }
|
2020-03-11 19:16:00 -05:00
|
|
|
|
2022-05-08 18:37:23 -05:00
|
|
|
def registered_bookmarkable
|
2023-03-20 04:19:28 -05:00
|
|
|
Bookmark.registered_bookmarkable_from_type(self.bookmarkable_type)
|
2022-05-08 18:37:23 -05:00
|
|
|
end
|
|
|
|
|
2022-03-29 21:43:11 -05:00
|
|
|
def polymorphic_columns_present
|
|
|
|
return if self.bookmarkable_id.present? && self.bookmarkable_type.present?
|
|
|
|
|
|
|
|
self.errors.add(:base, I18n.t("bookmarks.errors.bookmarkable_id_type_required"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def unique_per_bookmarkable
|
2023-01-09 06:20:10 -06:00
|
|
|
if !Bookmark.exists?(
|
|
|
|
user_id: user_id,
|
|
|
|
bookmarkable_id: bookmarkable_id,
|
|
|
|
bookmarkable_type: bookmarkable_type,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
end
|
2022-03-29 21:43:11 -05:00
|
|
|
|
|
|
|
self.errors.add(:base, I18n.t("bookmarks.errors.already_bookmarked", type: bookmarkable_type))
|
|
|
|
end
|
|
|
|
|
2020-03-11 19:16:00 -05:00
|
|
|
def ensure_sane_reminder_at_time
|
|
|
|
return if reminder_at.blank?
|
|
|
|
if reminder_at < Time.zone.now
|
|
|
|
self.errors.add(:base, I18n.t("bookmarks.errors.cannot_set_past_reminder"))
|
|
|
|
end
|
|
|
|
if reminder_at > 10.years.from_now.utc
|
|
|
|
self.errors.add(:base, I18n.t("bookmarks.errors.cannot_set_reminder_in_distant_future"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-18 16:53:49 -06:00
|
|
|
def bookmark_limit_not_reached
|
2021-04-08 22:06:35 -05:00
|
|
|
return if user.bookmarks.count < SiteSetting.max_bookmarks_per_user
|
|
|
|
return if !new_record?
|
|
|
|
|
|
|
|
self.errors.add(
|
|
|
|
:base,
|
|
|
|
I18n.t(
|
|
|
|
"bookmarks.errors.too_many",
|
|
|
|
user_bookmarks_url: "#{Discourse.base_url}/my/activity/bookmarks",
|
2023-01-09 06:20:10 -06:00
|
|
|
limit: SiteSetting.max_bookmarks_per_user,
|
|
|
|
),
|
2021-04-08 22:06:35 -05:00
|
|
|
)
|
2021-01-18 16:53:49 -06:00
|
|
|
end
|
|
|
|
|
2022-04-21 17:23:42 -05:00
|
|
|
def valid_bookmarkable_type
|
|
|
|
return if Bookmark.valid_bookmarkable_types.include?(self.bookmarkable_type)
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
self.errors.add(
|
|
|
|
:base,
|
|
|
|
I18n.t("bookmarks.errors.invalid_bookmarkable", type: self.bookmarkable_type),
|
|
|
|
)
|
2022-04-21 17:23:42 -05:00
|
|
|
end
|
|
|
|
|
2020-07-29 02:02:36 -05:00
|
|
|
def auto_delete_when_reminder_sent?
|
2020-07-20 19:00:39 -05:00
|
|
|
self.auto_delete_preference == Bookmark.auto_delete_preferences[:when_reminder_sent]
|
|
|
|
end
|
|
|
|
|
2022-03-08 11:44:18 -06:00
|
|
|
def auto_clear_reminder_when_reminder_sent?
|
|
|
|
self.auto_delete_preference == Bookmark.auto_delete_preferences[:clear_reminder]
|
|
|
|
end
|
|
|
|
|
2021-09-14 19:16:54 -05:00
|
|
|
def reminder_at_ics(offset: 0)
|
|
|
|
(reminder_at + offset).strftime(I18n.t("datetime_formats.formats.calendar_ics"))
|
|
|
|
end
|
|
|
|
|
2020-07-28 18:43:32 -05:00
|
|
|
def clear_reminder!
|
2024-05-28 08:21:33 -05:00
|
|
|
update!(reminder_last_sent_at: Time.zone.now, reminder_set_at: nil, reminder_at: nil)
|
2020-07-28 18:43:32 -05:00
|
|
|
end
|
|
|
|
|
2023-07-16 19:14:17 -05:00
|
|
|
def reminder_at_in_zone(timezone)
|
|
|
|
self.reminder_at.in_time_zone(timezone)
|
|
|
|
end
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
scope :with_reminders, -> { where("reminder_at IS NOT NULL") }
|
2021-09-14 19:16:54 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
scope :pending_reminders,
|
2023-11-28 23:38:07 -06:00
|
|
|
->(before_time = Time.now.utc) do
|
2023-01-09 06:20:10 -06:00
|
|
|
with_reminders.where("reminder_at <= ?", before_time).where(reminder_last_sent_at: nil)
|
2023-11-28 23:38:07 -06:00
|
|
|
end
|
2020-03-11 19:16:00 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
scope :pending_reminders_for_user, ->(user) { pending_reminders.where(user: user) }
|
2020-03-11 19:16:00 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
scope :for_user_in_topic,
|
2023-11-28 23:38:07 -06:00
|
|
|
->(user_id, topic_id) do
|
2023-01-09 06:20:10 -06:00
|
|
|
joins(
|
|
|
|
"LEFT JOIN posts ON posts.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Post'",
|
|
|
|
).joins(
|
|
|
|
"LEFT JOIN topics ON (topics.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Topic') OR
|
|
|
|
(topics.id = posts.topic_id)",
|
|
|
|
).where(
|
|
|
|
"bookmarks.user_id = :user_id AND (topics.id = :topic_id OR posts.topic_id = :topic_id)
|
2022-05-22 19:07:15 -05:00
|
|
|
AND posts.deleted_at IS NULL AND topics.deleted_at IS NULL",
|
2023-01-09 06:20:10 -06:00
|
|
|
user_id: user_id,
|
|
|
|
topic_id: topic_id,
|
|
|
|
)
|
2023-11-28 23:38:07 -06:00
|
|
|
end
|
2021-09-14 19:16:54 -05:00
|
|
|
|
2020-04-21 22:44:19 -05:00
|
|
|
def self.count_per_day(opts = nil)
|
|
|
|
opts ||= {}
|
2023-01-09 06:20:10 -06:00
|
|
|
result =
|
|
|
|
where(
|
|
|
|
"bookmarks.created_at >= ?",
|
|
|
|
opts[:start_date] || (opts[:since_days_ago] || 30).days.ago,
|
|
|
|
)
|
2021-09-14 19:16:54 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
result = result.where("bookmarks.created_at <= ?", opts[:end_date]) if opts[:end_date]
|
2021-09-14 19:16:54 -05:00
|
|
|
|
|
|
|
if opts[:category_id]
|
2023-01-09 06:20:10 -06:00
|
|
|
result =
|
|
|
|
result
|
|
|
|
.joins(
|
|
|
|
"LEFT JOIN posts ON posts.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Post'",
|
|
|
|
)
|
|
|
|
.joins(
|
|
|
|
"LEFT JOIN topics ON (topics.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Topic') OR (topics.id = posts.topic_id)",
|
|
|
|
)
|
|
|
|
.where("topics.deleted_at IS NULL AND posts.deleted_at IS NULL")
|
|
|
|
.merge(Topic.in_category_and_subcategories(opts[:category_id]))
|
2021-09-14 19:16:54 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
result.group("date(bookmarks.created_at)").order("date(bookmarks.created_at)").count
|
2020-04-21 22:44:19 -05:00
|
|
|
end
|
2020-10-13 18:38:57 -05:00
|
|
|
|
|
|
|
##
|
2022-06-22 23:09:39 -05:00
|
|
|
# Deletes bookmarks that are attached to the bookmarkable records that were deleted
|
|
|
|
# more than X days ago. We don't delete bookmarks instantly when trashable bookmarkables
|
|
|
|
# are deleted so that there is a grace period to un-delete.
|
2020-10-13 18:38:57 -05:00
|
|
|
def self.cleanup!
|
2022-06-22 23:09:39 -05:00
|
|
|
Bookmark.registered_bookmarkables.each(&:cleanup_deleted)
|
2020-10-13 18:38:57 -05:00
|
|
|
end
|
2019-12-10 22:04:02 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: bookmarks
|
|
|
|
#
|
2020-07-22 05:32:31 -05:00
|
|
|
# id :bigint not null, primary key
|
|
|
|
# user_id :bigint not null
|
|
|
|
# name :string(100)
|
|
|
|
# reminder_at :datetime
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# reminder_last_sent_at :datetime
|
|
|
|
# reminder_set_at :datetime
|
|
|
|
# auto_delete_preference :integer default(0), not null
|
2021-03-21 18:50:22 -05:00
|
|
|
# pinned :boolean default(FALSE)
|
DEV: Re-add polymorphic bookmark columns (#16261)
This commit is a redo of2f1ddadff7dd47f824070c8a3f633f00a27aacde
which we reverted because it blew up an internal CI check. I looked
into it, and it happened because the old migration to add the bookmark
columns still existed, and those columns were dropped in a post migrate,
so the two migrations to add the columns were conflicting before
the post migrate was run.
------
This commit only includes the creation of the new columns and index,
and does not add any triggers, backfilling, or new data.
A backfill will be done in the final PR when we switch this over.
Intermediate PRs will look something like this:
Add an experimental site setting for using polymorphic bookmarks,
and make sure in the places where bookmarks are created or updated
we fill in the columns. This setting will be used in subsequent
PRs as well.
Listing and searching bookmarks based on polymorphic associations
Creating post and topic bookmarks using polymorphic associations,
and changing special for_topic logic to just rely on the Topic
bookmarkable_type
Querying bookmark reminders based on polymorphic associations
Make sure various other areas like importers, bookmark guardian,
and others all rely on the associations
Prepare plugins that rely on the Bookmark model to use polymorphic
associations
The final core PR will remove all the setting gates and switch over
to using the polymorphic associations, backfill the bookmarks
table columns, and ignore the old post_id and for_topic colummns.
Then it will just be a matter of dropping the old columns down the
line.
2022-03-22 21:43:08 -05:00
|
|
|
# bookmarkable_id :integer
|
|
|
|
# bookmarkable_type :string
|
2019-12-10 22:04:02 -06:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2022-11-08 15:48:05 -06:00
|
|
|
# idx_bookmarks_user_polymorphic_unique (user_id,bookmarkable_type,bookmarkable_id) UNIQUE
|
|
|
|
# index_bookmarks_on_reminder_at (reminder_at)
|
|
|
|
# index_bookmarks_on_reminder_set_at (reminder_set_at)
|
|
|
|
# index_bookmarks_on_user_id (user_id)
|
2019-12-10 22:04:02 -06:00
|
|
|
#
|