Class: CommentThread

Inherits:
ApplicationRecord show all
Includes:
Lockable, PostRelated, SoftDeletable
Defined in:
/var/apps/qpixel/app/models/comment_thread.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lockable

#lock_active?, #locked?

Methods inherited from ApplicationRecord

#attributes_print, fuzzy_search, match_search, #match_search, sanitize_for_search, sanitize_name, sanitize_sql_in, useful_err_msg, with_lax_group_rules

Class Method Details

.accessible_to(user, post) ⇒ ActiveRecord::Relation<CommentThread>

Gets threads appropriately scoped for a given user & post

Parameters:

  • user (User, nil)

    user to check

  • post (Post)

    post to check

Returns:



24
25
26
27
28
29
30
# File '/var/apps/qpixel/app/models/comment_thread.rb', line 24

def self.accessible_to(user, post)
  if user&.at_least_moderator? || user&.post_privilege?('flag_curate', post)
    CommentThread
  else
    CommentThread.undeleted
  end
end

Instance Method Details

#add_follower(user) ⇒ Boolean

Registers a given user as a follower of the thread

Parameters:

  • user (User)

    user to register as a follower

Returns:

  • (Boolean)

    status of the operation



93
94
95
96
97
98
99
100
# File '/var/apps/qpixel/app/models/comment_thread.rb', line 93

def add_follower(user)
  if ThreadFollower.where(comment_thread: self, user: user).any?
    bump_last_activity(persist_changes: true)
    return true
  end

  ThreadFollower.create(comment_thread: self, user: user)
end

#bump_last_activity(persist_changes: false) ⇒ Object

Directly bumps the thread’s last activity date & time

Parameters:

  • persist_changes (Boolean) (defaults to: false)

    if set to true, will persist the changes



104
105
106
107
108
109
110
# File '/var/apps/qpixel/app/models/comment_thread.rb', line 104

def bump_last_activity(persist_changes: false)
  self.last_activity_at = DateTime.now

  if persist_changes
    save
  end
end

#can_access?(user) ⇒ Boolean

Does a given user have access to the thread?

Parameters:

  • user (User)

    user to check access for

Returns:

  • (Boolean)

    check result



48
49
50
51
# File '/var/apps/qpixel/app/models/comment_thread.rb', line 48

def can_access?(user)
  (!deleted? || user&.privilege?('flag_curate') || user&.post_privilege?('flag_curate', post)) &&
    post.can_access?(user)
end

#followed_by?(user) ⇒ Boolean

Is a given user a follower of the thread?

Parameters:

  • user (User)

    user to check

Returns:

  • (Boolean)

    check result



41
42
43
# File '/var/apps/qpixel/app/models/comment_thread.rb', line 41

def followed_by?(user)
  ThreadFollower.where(comment_thread: self, user: user).any?
end

#last_activityDateTime

Gets last activity date and time on the thread

Returns:

  • (DateTime)

    last activity date and time



55
56
57
58
# File '/var/apps/qpixel/app/models/comment_thread.rb', line 55

def last_activity
  last_comment_activity = comments.map(&:last_activity).compact.max
  [created_at, updated_at, last_activity_at, last_comment_activity].compact.max
end

#maximum_title_lengthObject



83
84
85
86
87
88
# File '/var/apps/qpixel/app/models/comment_thread.rb', line 83

def maximum_title_length
  max_len = SiteSetting['MaxThreadTitleLength'] || 255
  if title.length > [max_len, 255].min
    errors.add(:title, "can't be more than #{max_len} characters")
  end
end

#pingableArray<Integer>

Gets a list of user IDs who should be pingable in the thread.

Returns:

  • (Array<Integer>)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File '/var/apps/qpixel/app/models/comment_thread.rb', line 62

def pingable
  # post author +
  # answer authors +
  # last 500 history event users +
  # last 500 comment authors +
  # all thread followers
  query = <<~END_SQL
    SELECT posts.user_id FROM posts WHERE posts.id = #{post.id}
    UNION DISTINCT
    SELECT DISTINCT posts.user_id FROM posts WHERE posts.parent_id = #{post.id}
    UNION DISTINCT
    SELECT DISTINCT ph.user_id FROM post_histories ph WHERE ph.post_id = #{post.id}
    UNION DISTINCT
    SELECT DISTINCT comments.user_id FROM comments WHERE comments.post_id = #{post.id}
    UNION DISTINCT
    SELECT DISTINCT tf.user_id FROM thread_followers tf WHERE tf.comment_thread_id = #{id || '-1'}
  END_SQL

  ActiveRecord::Base.connection.execute(query).to_a.flatten
end

#read_only?Boolean

Is the thread read-only (can’t be edited)?

Returns:

  • (Boolean)

    check result



34
35
36
# File '/var/apps/qpixel/app/models/comment_thread.rb', line 34

def read_only?
  locked? || archived? || deleted?
end

#remove_follower(user) ⇒ Boolean

Removes a given user from the thread’s followers

Parameters:

  • user (User)

    user to remove from followers

Returns:

  • (Boolean)

    status of the operation



115
116
117
118
119
120
121
122
# File '/var/apps/qpixel/app/models/comment_thread.rb', line 115

def remove_follower(user)
  if ThreadFollower.where(comment_thread: self, user: user).none?
    bump_last_activity(persist_changes: true)
    return true
  end

  ThreadFollower.where(comment_thread: self, user: user).destroy_all.any?
end