2019-12-10 23:04:02 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Bookmark < ActiveRecord::Base
|
2021-01-18 17:53:49 -05:00
|
|
|
BOOKMARK_LIMIT = 2000
|
|
|
|
|
2020-07-21 20:17:21 -04:00
|
|
|
self.ignored_columns = [
|
|
|
|
"delete_when_reminder_sent" # TODO(2021-07-22): remove
|
|
|
|
]
|
|
|
|
|
2019-12-10 23:04:02 -05:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :post
|
|
|
|
belongs_to :topic
|
|
|
|
|
2020-07-22 23:12:28 -04:00
|
|
|
def self.reminder_types
|
|
|
|
@reminder_types ||= Enum.new(
|
|
|
|
later_today: 1,
|
|
|
|
next_business_day: 2,
|
|
|
|
tomorrow: 3,
|
|
|
|
next_week: 4,
|
|
|
|
next_month: 5,
|
|
|
|
custom: 6,
|
|
|
|
start_of_next_business_week: 7,
|
|
|
|
later_this_week: 8
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.auto_delete_preferences
|
|
|
|
@auto_delete_preferences ||= Enum.new(
|
|
|
|
never: 0,
|
|
|
|
when_reminder_sent: 1,
|
|
|
|
on_owner_reply: 2
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-12-10 23:04:02 -05:00
|
|
|
validates :reminder_at, presence: {
|
2020-05-06 01:22:43 -04:00
|
|
|
message: I18n.t("bookmarks.errors.time_must_be_provided"),
|
|
|
|
if: -> { reminder_type.present? }
|
2019-12-10 23:04:02 -05:00
|
|
|
}
|
|
|
|
|
2020-03-11 20:16:00 -04:00
|
|
|
validate :unique_per_post_for_user
|
|
|
|
validate :ensure_sane_reminder_at_time
|
2021-01-18 17:53:49 -05:00
|
|
|
validate :bookmark_limit_not_reached
|
2020-07-08 03:19:01 -04:00
|
|
|
validates :name, length: { maximum: 100 }
|
2020-03-11 20:16:00 -04:00
|
|
|
|
2020-03-12 02:00:45 -04:00
|
|
|
# we don't care whether the post or topic is deleted,
|
|
|
|
# they hold important information about the bookmark
|
|
|
|
def post
|
|
|
|
Post.unscoped { super }
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic
|
|
|
|
Topic.unscoped { super }
|
|
|
|
end
|
|
|
|
|
2020-03-11 20:16:00 -04:00
|
|
|
def unique_per_post_for_user
|
|
|
|
existing_bookmark = Bookmark.find_by(user_id: user_id, post_id: post_id)
|
|
|
|
return if existing_bookmark.blank? || existing_bookmark.id == id
|
|
|
|
self.errors.add(:base, I18n.t("bookmarks.errors.already_bookmarked_post"))
|
|
|
|
end
|
|
|
|
|
|
|
|
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 17:53:49 -05:00
|
|
|
def bookmark_limit_not_reached
|
|
|
|
return if user.bookmarks.count < BOOKMARK_LIMIT
|
|
|
|
self.errors.add(:base, I18n.t("bookmarks.errors.too_many", user_bookmarks_url: "#{Discourse.base_url}/my/activity/bookmarks"))
|
|
|
|
end
|
|
|
|
|
2020-03-12 20:44:39 -04:00
|
|
|
def no_reminder?
|
|
|
|
self.reminder_at.blank? && self.reminder_type.blank?
|
|
|
|
end
|
|
|
|
|
2020-07-29 03:02:36 -04:00
|
|
|
def auto_delete_when_reminder_sent?
|
2020-07-20 20:00:39 -04:00
|
|
|
self.auto_delete_preference == Bookmark.auto_delete_preferences[:when_reminder_sent]
|
|
|
|
end
|
|
|
|
|
2020-07-29 03:02:36 -04:00
|
|
|
def auto_delete_on_owner_reply?
|
2020-07-20 20:00:39 -04:00
|
|
|
self.auto_delete_preference == Bookmark.auto_delete_preferences[:on_owner_reply]
|
|
|
|
end
|
|
|
|
|
2020-07-28 19:43:32 -04:00
|
|
|
def clear_reminder!
|
2021-03-15 01:10:40 -04:00
|
|
|
update!(
|
2020-07-28 19:43:32 -04:00
|
|
|
reminder_at: nil,
|
|
|
|
reminder_type: nil,
|
|
|
|
reminder_last_sent_at: Time.zone.now,
|
|
|
|
reminder_set_at: nil
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-03-11 20:16:00 -04:00
|
|
|
scope :pending_reminders, ->(before_time = Time.now.utc) do
|
|
|
|
where("reminder_at IS NOT NULL AND reminder_at <= :before_time", before_time: before_time)
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :pending_reminders_for_user, ->(user) do
|
|
|
|
pending_reminders.where(user: user)
|
|
|
|
end
|
|
|
|
|
2020-04-21 23:44:19 -04:00
|
|
|
def self.count_per_day(opts = nil)
|
|
|
|
opts ||= {}
|
|
|
|
result = where('bookmarks.created_at >= ?', opts[:start_date] || (opts[:since_days_ago] || 30).days.ago)
|
|
|
|
result = result.where('bookmarks.created_at <= ?', opts[:end_date]) if opts[:end_date]
|
|
|
|
result = result.joins(:topic).merge(Topic.in_category_and_subcategories(opts[:category_id])) if opts[:category_id]
|
|
|
|
result.group('date(bookmarks.created_at)')
|
|
|
|
.order('date(bookmarks.created_at)')
|
|
|
|
.count
|
|
|
|
end
|
2020-10-13 19:38:57 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Deletes bookmarks that are attached to posts/topics that were deleted
|
|
|
|
# more than X days ago. We don't delete bookmarks instantly when a post/topic
|
|
|
|
# is deleted so that there is a grace period to un-delete.
|
|
|
|
def self.cleanup!
|
|
|
|
grace_time = 3.days.ago
|
|
|
|
DB.exec(<<~SQL, grace_time: grace_time)
|
|
|
|
DELETE FROM bookmarks b
|
|
|
|
USING topics t, posts p
|
|
|
|
WHERE (b.topic_id = t.id AND b.post_id = p.id)
|
|
|
|
AND (t.deleted_at < :grace_time OR p.deleted_at < :grace_time)
|
|
|
|
SQL
|
|
|
|
end
|
2019-12-10 23:04:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: bookmarks
|
|
|
|
#
|
2020-07-22 06:32:31 -04:00
|
|
|
# id :bigint not null, primary key
|
|
|
|
# user_id :bigint not null
|
|
|
|
# topic_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
|
|
|
|
# reminder_last_sent_at :datetime
|
|
|
|
# reminder_set_at :datetime
|
|
|
|
# auto_delete_preference :integer default(0), not null
|
2019-12-10 23:04:02 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_bookmarks_on_post_id (post_id)
|
|
|
|
# index_bookmarks_on_reminder_at (reminder_at)
|
2020-03-11 20:16:00 -04:00
|
|
|
# index_bookmarks_on_reminder_set_at (reminder_set_at)
|
2019-12-10 23:04:02 -05:00
|
|
|
# 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 (user_id,post_id) UNIQUE
|
|
|
|
#
|