summaryrefslogtreecommitdiff
path: root/app/services
diff options
context:
space:
mode:
authorPatricio Cano <suprnova32@gmail.com>2016-07-20 20:15:17 -0500
committerPatricio Cano <suprnova32@gmail.com>2016-07-26 15:18:07 -0500
commit8f04cf0eadbcde7fc5d1c970741e30ca8b97967d (patch)
tree4eac04baa5093bc4fd97691e2d6219d09810b55f /app/services
parentf7807c5b68b59f6a5b984ee64a6c82a3bd993d92 (diff)
downloadgitlab-ce-8f04cf0eadbcde7fc5d1c970741e30ca8b97967d.tar.gz
Refactor `SpamCheckService` to make it cleaner and clearer.
Diffstat (limited to 'app/services')
-rw-r--r--app/services/issues/create_service.rb5
-rw-r--r--app/services/issues/spam_check_service.rb25
-rw-r--r--app/services/spam_check_service.rb28
3 files changed, 31 insertions, 27 deletions
diff --git a/app/services/issues/create_service.rb b/app/services/issues/create_service.rb
index 496ea5a86a2..1085a1b93b8 100644
--- a/app/services/issues/create_service.rb
+++ b/app/services/issues/create_service.rb
@@ -6,8 +6,9 @@ module Issues
issue = project.issues.new(params.except(:label_ids, :request))
issue.author = params[:author] || current_user
- if Issues::SpamCheckService.new(project, current_user, params).spam_detected?
- return nil
+ if SpamCheckService.new(project, current_user, params).spam_detected?
+ issue.errors.add(:base, 'Your issue has been recognized as spam and has been discarded.')
+ return issue
end
if issue.save
diff --git a/app/services/issues/spam_check_service.rb b/app/services/issues/spam_check_service.rb
deleted file mode 100644
index b8d4e37faf5..00000000000
--- a/app/services/issues/spam_check_service.rb
+++ /dev/null
@@ -1,25 +0,0 @@
-module Issues
- class SpamCheckService < BaseService
- include Gitlab::AkismetHelper
-
- def spam_detected?
- text = [params[:title], params[:description]].reject(&:blank?).join("\n")
- request = params[:request]
-
- if request
- if check_for_spam?(project) && is_spam?(request.env, current_user, text)
- attrs = {
- user_id: current_user.id,
- project_id: project.id,
- title: params[:title],
- description: params[:description]
- }
- create_spam_log(project, current_user, attrs, request.env, api: false)
- return true
- end
- end
-
- false
- end
- end
-end
diff --git a/app/services/spam_check_service.rb b/app/services/spam_check_service.rb
new file mode 100644
index 00000000000..6768047aa63
--- /dev/null
+++ b/app/services/spam_check_service.rb
@@ -0,0 +1,28 @@
+class SpamCheckService
+ include Gitlab::AkismetHelper
+
+ attr_accessor :subject, :current_user, :params
+
+ def initialize(subject, user, params = {})
+ @subject, @current_user, @params = subject, user, params.dup
+ end
+
+ def spam_detected?
+ request = params[:request]
+ return false unless request || check_for_spam?(subject)
+
+ text = [params[:title], params[:description]].reject(&:blank?).join("\n")
+
+ return false unless is_spam?(request.env, current_user, text)
+
+ attrs = {
+ user_id: current_user.id,
+ project_id: subject.id,
+ title: params[:title],
+ description: params[:description]
+ }
+ create_spam_log(subject, current_user, attrs, request.env, api: false)
+
+ true
+ end
+end