Class: Feedback

Inherits:
ApplicationRecord
  • Object
show all
Includes:
AASM, ContentBase, PublifyGuid, StringLengthLimit
Defined in:
app/models/feedback.rb

Direct Known Subclasses

Comment, Trackback

Constant Summary

Constants included from StringLengthLimit

StringLengthLimit::STRING_LIMIT

Instance Attribute Summary

Attributes included from ContentBase

#just_changed_published_status

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ContentBase

#default_text_filter, #excerpt_text, #generate_html, #html, #html_map, #html_preprocess, included, #really_send_notifications, #send_notification_to_user, #text_filter

Methods included from PublifyGuid

#create_guid

Class Method Details

.allowed_tagsObject



75
76
77
# File 'app/models/feedback.rb', line 75

def self.allowed_tags
  @allowed_tags ||= Rails::Html::SafeListSanitizer.allowed_tags - ["img"]
end

.paginated(page, per_page) ⇒ Object

FIXME: Inline this method



71
72
73
# File 'app/models/feedback.rb', line 71

def self.paginated(page, per_page)
  page(page).per(per_page)
end

Instance Method Details

#akismet_is_spam?(_options = {}) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
164
165
166
167
168
169
# File 'app/models/feedback.rb', line 159

def akismet_is_spam?(_options = {})
  return false if akismet.nil?

  begin
    Timeout.timeout(60) do
      akismet.comment_check(ip, user_agent, akismet_options)
    end
  rescue Timeout::Error
    nil
  end
end

#akismet_optionsObject



123
124
125
126
127
128
129
# File 'app/models/feedback.rb', line 123

def akismet_options
  { type: self.class.to_s.downcase,
    author: originator,
    author_email: email,
    author_url: url,
    text: body }
end

#change_state!Object



171
172
173
174
175
176
177
178
179
180
181
# File 'app/models/feedback.rb', line 171

def change_state!
  result = ""
  if spam? || presumed_spam?
    mark_as_ham!
    result = "ham"
  else
    mark_as_spam!
    result = "spam"
  end
  result
end

#classifyObject



135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/feedback.rb', line 135

def classify
  return :ham if user_id
  return :spam if blog.default_moderate_comments
  return :ham unless blog.sp_global

  # Yeah, three state logic is evil...
  case sp_is_spam? || akismet_is_spam?
  when nil then :spam
  when true then :spam
  when false then :ham
  end
end

#classify_contentObject



83
84
85
86
87
88
89
90
# File 'app/models/feedback.rb', line 83

def classify_content
  return unless unclassified?

  case classify
  when :ham then presume_ham
  else presume_spam
  end
end

#confirm_classificationObject



188
189
190
191
192
193
194
# File 'app/models/feedback.rb', line 188

def confirm_classification
  if presumed_spam?
    mark_as_spam
  elsif presumed_ham?
    mark_as_ham
  end
end

#confirm_classification!Object



183
184
185
186
# File 'app/models/feedback.rb', line 183

def confirm_classification!
  confirm_classification
  save!
end

#correct_urlObject



101
102
103
104
105
# File 'app/models/feedback.rb', line 101

def correct_url
  return if url.blank?

  self.url = "http://#{url}" unless %r{^https?://}.match?(url)
end

#feedback_allowedObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/feedback.rb', line 107

def feedback_allowed
  return unless article

  unless blog_allows_feedback?
    errors.add(:base, "#{plural_model_name} are disabled")
    return
  end

  unless article_allows_feedback?
    errors.add(:article, "Article is not open for #{plural_model_name.downcase}")
    return
  end

  errors.add(:article, "#{plural_model_name} are closed") if article_closed_for_feedback?
end

#html_postprocess(_field, html) ⇒ Object



96
97
98
99
# File 'app/models/feedback.rb', line 96

def html_postprocess(_field, html)
  helper = ContentTextHelpers.new
  helper.sanitize(helper.auto_link(html), tags: self.class.allowed_tags)
end

#parentObject



79
80
81
# File 'app/models/feedback.rb', line 79

def parent
  article
end


92
93
94
# File 'app/models/feedback.rb', line 92

def permalink_url(_anchor = :ignored, only_path = false)
  article.permalink_url("#{self.class.to_s.downcase}-#{id}", only_path)
end

#published?Boolean

Returns:

  • (Boolean)


224
225
226
# File 'app/models/feedback.rb', line 224

def published?
  ham? || presumed_ham?
end

#report_as_hamObject



208
209
210
211
212
213
214
215
216
217
218
# File 'app/models/feedback.rb', line 208

def report_as_ham
  return if akismet.nil?

  begin
    Timeout.timeout(5) do
      akismet.ham(ip, user_agent, akismet_options)
    end
  rescue Timeout::Error
    nil
  end
end

#report_as_spamObject



196
197
198
199
200
201
202
203
204
205
206
# File 'app/models/feedback.rb', line 196

def report_as_spam
  return if akismet.nil?

  begin
    Timeout.timeout(5) do
      akismet.submit_spam(ip, user_agent, akismet_options)
    end
  rescue Timeout::Error
    nil
  end
end

#send_notificationsObject



220
221
222
# File 'app/models/feedback.rb', line 220

def send_notifications
  nil
end

#sp_is_spam?(_options = {}) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
151
152
153
154
155
156
157
# File 'app/models/feedback.rb', line 148

def sp_is_spam?(_options = {})
  sp = SpamProtection.new(blog)
  Timeout.timeout(30) do
    spam_fields.any? do |field|
      sp.is_spam?(send(field))
    end
  end
rescue Timeout::Error
  nil
end

#spam_fieldsObject



131
132
133
# File 'app/models/feedback.rb', line 131

def spam_fields
  [:title, :body, :ip, :url]
end

#spammy?Boolean

Returns:

  • (Boolean)


232
233
234
# File 'app/models/feedback.rb', line 232

def spammy?
  spam? || presumed_spam?
end

#status_confirmed?Boolean

Returns:

  • (Boolean)


228
229
230
# File 'app/models/feedback.rb', line 228

def status_confirmed?
  ham? || spam?
end