Class: Flag

Inherits:
ApplicationRecord show all
Includes:
CommunityRelated, Routing, Timestamped
Defined in:
/var/apps/qpixel/app/models/flag.rb

Overview

Represents a flag. Flags are attached to both a user and a post, and have a single status.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Routing

#default_url_options

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<Flag>

Gets flags appropriately scoped for a given user & post

Parameters:

  • user (User, nil)

    user to check

  • post (Post)

    post to check

Returns:

  • (ActiveRecord::Relation<Flag>)


31
32
33
34
35
36
37
38
39
# File '/var/apps/qpixel/app/models/flag.rb', line 31

def self.accessible_to(user, post)
  if user&.at_least_moderator?
    post.flags
  elsif user&.can_handle_flags?
    post.flags.not_confidential
  else
    post.flags.none
  end
end

Instance Method Details

#confidential?Boolean

Checks if the flag is confidential as per its type

Returns:

  • (Boolean)

    check result



43
44
45
# File '/var/apps/qpixel/app/models/flag.rb', line 43

def confidential?
  post_flag_type&.confidential || false
end

#maximum_reason_lengthObject



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

def maximum_reason_length
  max_len = SiteSetting['MaxFlagReasonLength'] || 1000
  if reason.length > [max_len, 1000].min
    errors.add(:reason, "can't be more than #{max_len} characters")
  end
end

#resolve(status:, message:, handled_by:, handled_at: nil) ⇒ Boolean

Resolve a flag and run associated ability and notification actions.

Parameters:

  • status (String, ActionController::Parameters)

    new status of the flag

  • message (String, ActionController::Parameters)

    custom response to the flag

  • handled_by (User)

    user who handled the flag

  • handled_at (DateTime, nil) (defaults to: nil)

    time the flag was handled (defaults to current time)

Returns:

  • (Boolean)

    result



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

def resolve(status:, message:, handled_by:, handled_at: nil)
  resolve_status = false

  transaction do
    resolve_status = update(status: status,
                            message: message,
                            handled_by: handled_by,
                            handled_at: handled_at || DateTime.now)

    resolve_status &&= AbilityQueue.add(user, "Flag Handled ##{id}")

    unless resolve_status
      raise ActiveRecord::Rollback
    end

    unless message.blank?
      # TODO: create_notification actually behaves like a bang method
      user.create_notification('A moderator has written a response to your flag. Check your flag history page.',
                               flag_history_url(user, { host: community.host }))
    end
  end

  resolve_status
end