2017-06-23 21:19:08 +08:00
|
|
|
class Comment < ApplicationRecord
|
2016-02-12 23:52:43 +08:00
|
|
|
include SearchableModel
|
|
|
|
|
2016-09-21 21:35:23 +08:00
|
|
|
auto_strip_attributes :message, nullify: false
|
2016-10-05 23:45:20 +08:00
|
|
|
validates :message,
|
|
|
|
presence: true,
|
|
|
|
length: { maximum: Constants::TEXT_MAX_LENGTH }
|
2016-02-12 23:52:43 +08:00
|
|
|
validates :user, presence: true
|
|
|
|
|
2019-05-09 23:10:02 +08:00
|
|
|
belongs_to :user, inverse_of: :comments
|
|
|
|
belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User', optional: true
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2023-05-05 21:47:53 +08:00
|
|
|
scope :unseen_by, ->(user) { where('? = ANY (unseen_by)', user.id) }
|
|
|
|
|
2022-08-09 20:15:49 +08:00
|
|
|
def self.mark_as_seen_by(user, commentable)
|
2020-11-30 22:55:20 +08:00
|
|
|
# rubocop:disable Rails/SkipsModelValidations
|
|
|
|
all.where('? = ANY (unseen_by)', user.id).update_all("unseen_by = array_remove(unseen_by, #{user.id.to_i}::bigint)")
|
2022-08-30 17:48:09 +08:00
|
|
|
|
|
|
|
# Because we want the number of unseen comments to affect the cache of project
|
|
|
|
# and experiment lists, we need to set the updated_at of Project or Experiment.
|
|
|
|
commentable.touch if commentable.class.in? [Project, Experiment]
|
|
|
|
|
2020-11-30 22:55:20 +08:00
|
|
|
# rubocop:enable Rails/SkipsModelValidations
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|