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:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-01-30 18:09:15 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-01-30 18:09:15 +0300
commit536aa3a1f4b96abc4ca34489bf2cbe503afcded7 (patch)
tree88d08f7dfa29a32d6526773c4fe0fefd9f2bc7d1 /app/services/spam
parent50ae4065530c4eafbeb7c5ff2c462c48c02947ca (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/spam')
-rw-r--r--app/services/spam/akismet_service.rb75
1 files changed, 75 insertions, 0 deletions
diff --git a/app/services/spam/akismet_service.rb b/app/services/spam/akismet_service.rb
new file mode 100644
index 00000000000..7d16743b3ed
--- /dev/null
+++ b/app/services/spam/akismet_service.rb
@@ -0,0 +1,75 @@
+# frozen_string_literal: true
+
+module Spam
+ class AkismetService
+ attr_accessor :text, :options
+
+ def initialize(owner_name, owner_email, text, options = {})
+ @owner_name = owner_name
+ @owner_email = owner_email
+ @text = text
+ @options = options
+ end
+
+ def spam?
+ return false unless akismet_enabled?
+
+ params = {
+ type: 'comment',
+ text: text,
+ created_at: DateTime.now,
+ author: owner_name,
+ author_email: owner_email,
+ referrer: options[:referrer]
+ }
+
+ begin
+ is_spam, is_blatant = akismet_client.check(options[:ip_address], options[:user_agent], params)
+ is_spam || is_blatant
+ rescue => e
+ Rails.logger.error("Unable to connect to Akismet: #{e}, skipping check") # rubocop:disable Gitlab/RailsLogger
+ false
+ end
+ end
+
+ def submit_ham
+ submit(:ham)
+ end
+
+ def submit_spam
+ submit(:spam)
+ end
+
+ private
+
+ attr_accessor :owner_name, :owner_email
+
+ def akismet_client
+ @akismet_client ||= ::Akismet::Client.new(Gitlab::CurrentSettings.akismet_api_key,
+ Gitlab.config.gitlab.url)
+ end
+
+ def akismet_enabled?
+ Gitlab::CurrentSettings.akismet_enabled
+ end
+
+ def submit(type)
+ return false unless akismet_enabled?
+
+ params = {
+ type: 'comment',
+ text: text,
+ author: owner_name,
+ author_email: owner_email
+ }
+
+ begin
+ akismet_client.public_send(type, options[:ip_address], options[:user_agent], params) # rubocop:disable GitlabSecurity/PublicSend
+ true
+ rescue => e
+ Rails.logger.error("Unable to connect to Akismet: #{e}, skipping!") # rubocop:disable Gitlab/RailsLogger
+ false
+ end
+ end
+ end
+end