Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatricio Cano <suprnova32@gmail.com>2016-08-06 01:10:08 +0300
committerPatricio Cano <suprnova32@gmail.com>2016-08-15 21:18:15 +0300
commit43e756d4eafd79f4d2f366b646ebb94af78b5a4c (patch)
tree07949d3368affcda301fd266e1e5bf0649474b23 /app/services
parent7179165af7553720089a0b7e7024374c371e2f90 (diff)
Refactored AkismetHelper into AkismetService and cleaned up `Spammable`
- Refactored SpamCheckService into SpamService
Diffstat (limited to 'app/services')
-rw-r--r--app/services/akismet_service.rb78
-rw-r--r--app/services/issues/create_service.rb6
-rw-r--r--app/services/spam_check_service.rb33
-rw-r--r--app/services/spam_service.rb64
-rw-r--r--app/services/system_note_service.rb4
5 files changed, 147 insertions, 38 deletions
diff --git a/app/services/akismet_service.rb b/app/services/akismet_service.rb
new file mode 100644
index 00000000000..c09663bce85
--- /dev/null
+++ b/app/services/akismet_service.rb
@@ -0,0 +1,78 @@
+class AkismetService
+ attr_accessor :spammable
+
+ def initialize(spammable)
+ @spammable = spammable
+ end
+
+ def client_ip(env)
+ env['action_dispatch.remote_ip'].to_s
+ end
+
+ def user_agent(env)
+ env['HTTP_USER_AGENT']
+ end
+
+ def is_spam?(environment)
+ ip_address = client_ip(environment)
+ user_agent = user_agent(environment)
+
+ params = {
+ type: 'comment',
+ text: spammable.spammable_text,
+ created_at: DateTime.now,
+ author: spammable.owner.name,
+ author_email: spammable.owner.email,
+ referrer: environment['HTTP_REFERER'],
+ }
+
+ begin
+ is_spam, is_blatant = akismet_client.check(ip_address, user_agent, params)
+ is_spam || is_blatant
+ rescue => e
+ Rails.logger.error("Unable to connect to Akismet: #{e}, skipping check")
+ false
+ end
+ end
+
+ def ham!
+ params = {
+ type: 'comment',
+ text: spammable.text,
+ author: spammable.user.name,
+ author_email: spammable.user.email
+ }
+
+ begin
+ akismet_client.submit_ham(spammable.source_ip, spammable.user_agent, params)
+ true
+ rescue => e
+ Rails.logger.error("Unable to connect to Akismet: #{e}, skipping!")
+ false
+ end
+ end
+
+ def spam!
+ params = {
+ type: 'comment',
+ text: spammable.spammable_text,
+ author: spammable.owner.name,
+ author_email: spammable.owner.email
+ }
+
+ begin
+ akismet_client.submit_spam(spammable.user_agent_detail.ip_address, spammable.user_agent_detail.user_agent, params)
+ true
+ rescue => e
+ Rails.logger.error("Unable to connect to Akismet: #{e}, skipping!")
+ false
+ end
+ end
+
+ private
+
+ def akismet_client
+ @akismet_client ||= ::Akismet::Client.new(current_application_settings.akismet_api_key,
+ Gitlab.config.gitlab.url)
+ end
+end
diff --git a/app/services/issues/create_service.rb b/app/services/issues/create_service.rb
index 9f8a642a75b..67125d5c0e4 100644
--- a/app/services/issues/create_service.rb
+++ b/app/services/issues/create_service.rb
@@ -8,7 +8,7 @@ module Issues
@issue = project.issues.new(params)
@issue.author = params[:author] || current_user
- spam_check_service.execute
+ @issue.spam = spam_service.check(@api, @request)
if @issue.save
@issue.update_attributes(label_ids: label_params)
@@ -25,8 +25,8 @@ module Issues
private
- def spam_check_service
- SpamCheckService.new(@request, @api, @issue)
+ def spam_service
+ SpamService.new(@issue)
end
def user_agent_detail_service
diff --git a/app/services/spam_check_service.rb b/app/services/spam_check_service.rb
deleted file mode 100644
index 71b9436a22e..00000000000
--- a/app/services/spam_check_service.rb
+++ /dev/null
@@ -1,33 +0,0 @@
-class SpamCheckService
- attr_accessor :request, :api, :spammable
-
- def initialize(request, api, spammable)
- @request, @api, @spammable = request, api, spammable
- end
-
- def execute
- if request && spammable.check_for_spam?
- if spammable.spam_detected?(request.env)
- create_spam_log
- end
- end
- end
-
- private
-
- def spam_log_attrs
- {
- user_id: spammable.owner_id,
- title: spammable.spam_title,
- description: spammable.spam_description,
- source_ip: spammable.client_ip(request.env),
- user_agent: spammable.user_agent(request.env),
- noteable_type: spammable.class.to_s,
- via_api: api
- }
- end
-
- def create_spam_log
- SpamLog.create(spam_log_attrs)
- end
-end
diff --git a/app/services/spam_service.rb b/app/services/spam_service.rb
new file mode 100644
index 00000000000..ad60de368aa
--- /dev/null
+++ b/app/services/spam_service.rb
@@ -0,0 +1,64 @@
+class SpamService
+ attr_accessor :spammable
+
+ def initialize(spammable)
+ @spammable = spammable
+ end
+
+ def check(api, request)
+ return false unless request && spammable.check_for_spam?
+ return false unless akismet.is_spam?(request.env)
+
+ create_spam_log(api, request)
+ true
+ end
+
+ def mark_as_spam!(current_user)
+ return false unless akismet_enabled? && spammable.can_be_submitted?
+ if akismet.spam!
+ spammable.user_agent_detail.update_attribute(:submitted, true)
+
+ if spammable.is_a?(Issuable)
+ SystemNoteService.submit_spam(spammable, spammable.project, current_user)
+ end
+ true
+ else
+ false
+ end
+ end
+
+ def mark_as_ham!
+ return false unless spammable.is_a?(SpamLog)
+
+ if akismet.ham!
+ spammable.update_attribute(:submitted_as_ham, true)
+ true
+ else
+ false
+ end
+ end
+
+ private
+
+ def akismet
+ @akismet ||= AkismetService.new(spammable)
+ end
+
+ def akismet_enabled?
+ current_application_settings.akismet_enabled
+ end
+
+ def create_spam_log(api, request)
+ SpamLog.create(
+ {
+ user_id: spammable.owner_id,
+ title: spammable.spam_title,
+ description: spammable.spam_description,
+ source_ip: akismet.client_ip(request.env),
+ user_agent: akismet.user_agent(request.env),
+ noteable_type: spammable.class.to_s,
+ via_api: api
+ }
+ )
+ end
+end
diff --git a/app/services/system_note_service.rb b/app/services/system_note_service.rb
index 56d3329f5bd..35c9ce909e6 100644
--- a/app/services/system_note_service.rb
+++ b/app/services/system_note_service.rb
@@ -395,7 +395,7 @@ module SystemNoteService
create_note(noteable: noteable, project: project, author: author, note: body)
end
- # Called when the status of a Issuable is submitted as spam
+ # Called when Issuable is submitted as spam
#
# noteable - Noteable object
# project - Project owning noteable
@@ -407,7 +407,7 @@ module SystemNoteService
#
# Returns the created Note object
def submit_spam(noteable, project, author)
- body = "Submitted #{noteable.class.to_s.downcase} as spam"
+ body = "Submitted this #{noteable.class.to_s.downcase} as spam"
create_note(noteable: noteable, project: project, author: author, note: body)
end