2019-12-10 23:04:02 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Bookmark < ActiveRecord::Base
|
2022-01-06 21:16:43 -05:00
|
|
|
# these columns were here for a very short amount of time,
|
|
|
|
# hence the very short ignore time
|
|
|
|
self.ignored_columns = [
|
2022-01-10 02:50:53 -05:00
|
|
|
"bookmarkable_id", # TODO 2022-04-01 remove
|
|
|
|
"bookmarkable_type", # TODO 2022-04-01 remove
|
|
|
|
"topic_id", # TODO 2022-04-01: remove
|
|
|
|
"reminder_type" # TODO 2021-04-01: remove
|
2022-01-06 21:16:43 -05:00
|
|
|
]
|
|
|
|
|
2019-12-10 23:04:02 -05:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :post
|
2021-09-14 20:16:54 -04:00
|
|
|
has_one :topic, through: :post
|
2022-01-05 17:56:05 -05:00
|
|
|
belongs_to :bookmarkable, polymorphic: true
|
2021-09-14 20:16:54 -04:00
|
|
|
|
|
|
|
delegate :topic_id, to: :post
|
2019-12-10 23:04:02 -05:00
|
|
|
|
2021-09-15 19:56:54 -04:00
|
|
|
def self.auto_delete_preferences
|
|
|
|
@auto_delete_preferences ||= Enum.new(
|
|
|
|
never: 0,
|
|
|
|
when_reminder_sent: 1,
|
|
|
|
on_owner_reply: 2
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-08 20:50:26 -04:00
|
|
|
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? }
|
|
|
|
|
2021-09-20 18:45:47 -04:00
|
|
|
validate :for_topic_must_use_first_post,
|
|
|
|
on: [:create, :update],
|
|
|
|
if: Proc.new { |b| b.will_save_change_to_post_id? || b.will_save_change_to_for_topic? }
|
|
|
|
|
2020-03-11 20:16:00 -04:00
|
|
|
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
|
|
|
|
|
|
|
def unique_per_post_for_user
|
2021-09-20 18:45:47 -04:00
|
|
|
exists = if is_for_first_post?
|
2021-09-14 21:29:22 -04:00
|
|
|
Bookmark.exists?(user_id: user_id, post_id: post_id, for_topic: for_topic)
|
|
|
|
else
|
|
|
|
Bookmark.exists?(user_id: user_id, post_id: post_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
if exists
|
2021-09-08 20:50:26 -04:00
|
|
|
self.errors.add(:base, I18n.t("bookmarks.errors.already_bookmarked_post"))
|
|
|
|
end
|
2020-03-11 20:16:00 -04:00
|
|
|
end
|
|
|
|
|
2021-09-20 18:45:47 -04:00
|
|
|
def for_topic_must_use_first_post
|
|
|
|
if !is_for_first_post? && self.for_topic
|
|
|
|
self.errors.add(:base, I18n.t("bookmarks.errors.for_topic_must_use_first_post"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-11 20:16:00 -04: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 17:53:49 -05:00
|
|
|
def bookmark_limit_not_reached
|
2021-04-08 23:06:35 -04: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",
|
|
|
|
limit: SiteSetting.max_bookmarks_per_user
|
|
|
|
)
|
|
|
|
)
|
2021-01-18 17:53:49 -05:00
|
|
|
end
|
|
|
|
|
2021-09-20 18:45:47 -04:00
|
|
|
def is_for_first_post?
|
|
|
|
@is_for_first_post ||= new_record? ? Post.exists?(id: post_id, post_number: 1) : post.post_number == 1
|
|
|
|
end
|
|
|
|
|
2020-03-12 20:44:39 -04:00
|
|
|
def no_reminder?
|
2021-09-15 19:56:54 -04:00
|
|
|
self.reminder_at.blank?
|
2020-03-12 20:44:39 -04:00
|
|
|
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
|
|
|
|
|
2021-09-14 20:16:54 -04:00
|
|
|
def reminder_at_ics(offset: 0)
|
|
|
|
(reminder_at + offset).strftime(I18n.t("datetime_formats.formats.calendar_ics"))
|
|
|
|
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_last_sent_at: Time.zone.now,
|
|
|
|
reminder_set_at: nil
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-14 20:16:54 -04:00
|
|
|
scope :with_reminders, -> do
|
|
|
|
where("reminder_at IS NOT NULL")
|
|
|
|
end
|
|
|
|
|
2020-03-11 20:16:00 -04:00
|
|
|
scope :pending_reminders, ->(before_time = Time.now.utc) do
|
2021-09-14 20:16:54 -04:00
|
|
|
with_reminders.where("reminder_at <= :before_time", before_time: before_time)
|
2020-03-11 20:16:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
scope :pending_reminders_for_user, ->(user) do
|
|
|
|
pending_reminders.where(user: user)
|
|
|
|
end
|
|
|
|
|
2021-09-14 20:16:54 -04:00
|
|
|
scope :for_user_in_topic, ->(user_id, topic_id) {
|
|
|
|
joins(:post).where(user_id: user_id, posts: { topic_id: topic_id })
|
|
|
|
}
|
|
|
|
|
2021-09-14 21:29:22 -04:00
|
|
|
def self.find_for_topic_by_user(topic_id, user_id)
|
|
|
|
for_user_in_topic(user_id, topic_id).where(for_topic: true).first
|
|
|
|
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)
|
2021-09-14 20:16:54 -04:00
|
|
|
|
|
|
|
if opts[:end_date]
|
|
|
|
result = result.where('bookmarks.created_at <= ?', opts[:end_date])
|
|
|
|
end
|
|
|
|
|
|
|
|
if opts[:category_id]
|
|
|
|
result = result.joins(:topic).merge(Topic.in_category_and_subcategories(opts[:category_id]))
|
|
|
|
end
|
|
|
|
|
2020-04-21 23:44:19 -04:00
|
|
|
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
|
2021-06-15 18:30:40 -04:00
|
|
|
topics_deleted = DB.query(<<~SQL, grace_time: grace_time)
|
2020-10-13 19:38:57 -04:00
|
|
|
DELETE FROM bookmarks b
|
|
|
|
USING topics t, posts p
|
2021-09-14 20:16:54 -04:00
|
|
|
WHERE (t.id = p.topic_id AND b.post_id = p.id)
|
2020-10-13 19:38:57 -04:00
|
|
|
AND (t.deleted_at < :grace_time OR p.deleted_at < :grace_time)
|
2021-09-14 20:16:54 -04:00
|
|
|
RETURNING t.id AS topic_id
|
2020-10-13 19:38:57 -04:00
|
|
|
SQL
|
2021-06-15 18:30:40 -04:00
|
|
|
|
|
|
|
topics_deleted_ids = topics_deleted.map(&:topic_id).uniq
|
|
|
|
topics_deleted_ids.each do |topic_id|
|
|
|
|
Jobs.enqueue(:sync_topic_user_bookmarked, topic_id: topic_id)
|
|
|
|
end
|
2020-10-13 19:38:57 -04:00
|
|
|
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
|
|
|
|
# post_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 19:50:22 -04:00
|
|
|
# pinned :boolean default(FALSE)
|
2021-09-14 21:29:22 -04:00
|
|
|
# for_topic :boolean default(FALSE), not null
|
2019-12-10 23:04:02 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2021-09-14 21:29:22 -04:00
|
|
|
# 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_user_id (user_id)
|
|
|
|
# index_bookmarks_on_user_id_and_post_id_and_for_topic (user_id,post_id,for_topic) UNIQUE
|
2019-12-10 23:04:02 -05:00
|
|
|
#
|