Module: UserRateLimits

Extended by:
ActiveSupport::Concern
Included in:
User
Defined in:
/var/apps/qpixel/app/models/concerns/user_rate_limits.rb

Instance Method Summary collapse

Instance Method Details

#comment_rate_limited?(post) ⇒ Boolean

Has the user reached comment limit for a given post?

Parameters:

  • post (Post)

    post to check

Returns:

  • (Boolean)

    check result



63
64
65
# File '/var/apps/qpixel/app/models/concerns/user_rate_limits.rb', line 63

def comment_rate_limited?(post)
  recent_comments_count(post) >= max_comments_per_day(post)
end

#max_comments_per_day(post) ⇒ Integer

Gets the max number of comments the user can make on a given post

Parameters:

  • post (Post)

    post to get the limit for

Returns:

  • (Integer)


19
20
21
# File '/var/apps/qpixel/app/models/concerns/user_rate_limits.rb', line 19

def max_comments_per_day(post)
  owns_post_or_parent?(post) ? max_comments_per_day_on_own_posts : max_comments_per_day_on_posts_of_others
end

#max_comments_per_day_on_own_postsInteger

Gets the max number of comments the user can make per day on their own posts or answers to them

Returns:

  • (Integer)


12
13
14
# File '/var/apps/qpixel/app/models/concerns/user_rate_limits.rb', line 12

def max_comments_per_day_on_own_posts
  SiteSetting[new? ? 'RL_NewUserCommentsOwnPosts' : 'RL_CommentsOwnPosts'] || 0
end

#max_comments_per_day_on_posts_of_othersInteger

Gets the max number of comments the user can make per day on posts made by other users

Returns:

  • (Integer)


6
7
8
# File '/var/apps/qpixel/app/models/concerns/user_rate_limits.rb', line 6

def max_comments_per_day_on_posts_of_others
  SiteSetting[new? ? 'RL_NewUserComments' : 'RL_Comments'] || 0
end

#max_votes_per_dayInteger

Gets the max number of votes the user can make per day

Returns:

  • (Integer)


25
26
27
# File '/var/apps/qpixel/app/models/concerns/user_rate_limits.rb', line 25

def max_votes_per_day
  SiteSetting[new? ? 'RL_NewUserVotes' : 'RL_Votes'] || 0
end

#recent_comments_count(post) ⇒ Integer

Number of comments by the user based on whether they own a given post

Parameters:

  • post (Post)

    post to use for the check

Returns:

  • (Integer)


32
33
34
# File '/var/apps/qpixel/app/models/concerns/user_rate_limits.rb', line 32

def recent_comments_count(post)
  owns_post_or_parent?(post) ? recent_comments_on_own_posts_count : recent_comments_on_posts_of_others_count
end

#recent_comments_on_own_posts_countInteger

Number of comments by the user on own posts or answers to them in the last 24 hours

Returns:

  • (Integer)


38
39
40
41
42
43
# File '/var/apps/qpixel/app/models/concerns/user_rate_limits.rb', line 38

def recent_comments_on_own_posts_count
  Comment.recent.by(self)
         .where(post: Post.parent_by(self))
         .or(Comment.recent.by(self).where(post: Post.by(self)))
         .count
end

#recent_comments_on_posts_of_others_countInteger

Number of comments by the user on posts made by other users in the last 24 hours

Returns:

  • (Integer)


47
48
49
50
51
52
# File '/var/apps/qpixel/app/models/concerns/user_rate_limits.rb', line 47

def recent_comments_on_posts_of_others_count
  Comment.recent.by(self)
         .where.not(post: Post.parent_by(self))
         .where.not(post: Post.by(self))
         .count
end

#recent_votes_countInteger

Number of votes by the user on posts of others in the last 24 hours

Returns:

  • (Integer)

    number of recent votes



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

def recent_votes_count
  Vote.recent.by(self).where.not(post: Post.parent_by(self)).count
end