diff options
author | Patricio Cano <suprnova32@gmail.com> | 2016-07-30 07:18:32 +0300 |
---|---|---|
committer | Patricio Cano <suprnova32@gmail.com> | 2016-08-15 21:18:15 +0300 |
commit | 64ab2b3d9f10366249c03a6bcf5e8b1d20010d8f (patch) | |
tree | 80f4e4b496c55c8aacfc37ee361330b015d9fad4 /app/services | |
parent | 722fc84e3d4785fb3a9db5f1c7d2aabad22e8e01 (diff) |
Refactored spam related code even further
- Removed unnecessary column from `SpamLog`
- Moved creation of SpamLogs out of its own service and into SpamCheckService
- Simplified code in SpamCheckService.
- Moved move spam related code into Spammable concern
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/create_spam_log_service.rb | 13 | ||||
-rw-r--r-- | app/services/issues/create_service.rb | 34 | ||||
-rw-r--r-- | app/services/spam_check_service.rb | 35 | ||||
-rw-r--r-- | app/services/user_agent_detail_service.rb | 8 |
4 files changed, 39 insertions, 51 deletions
diff --git a/app/services/create_spam_log_service.rb b/app/services/create_spam_log_service.rb deleted file mode 100644 index 59a66fde47a..00000000000 --- a/app/services/create_spam_log_service.rb +++ /dev/null @@ -1,13 +0,0 @@ -class CreateSpamLogService < BaseService - def initialize(project, user, params) - super(project, user, params) - end - - def execute - spam_params = params.merge({ user_id: @current_user.id, - project_id: @project.id } ) - spam_log = SpamLog.new(spam_params) - spam_log.save - spam_log - end -end diff --git a/app/services/issues/create_service.rb b/app/services/issues/create_service.rb index d580834be83..9f8a642a75b 100644 --- a/app/services/issues/create_service.rb +++ b/app/services/issues/create_service.rb @@ -3,34 +3,34 @@ module Issues def execute filter_params label_params = params.delete(:label_ids) - request = params.delete(:request) - api = params.delete(:api) - issue = project.issues.new(params) - issue.author = params[:author] || current_user + @request = params.delete(:request) + @api = params.delete(:api) + @issue = project.issues.new(params) + @issue.author = params[:author] || current_user - issue.spam = spam_check_service.execute(request, api, issue) + spam_check_service.execute - if issue.save - issue.update_attributes(label_ids: label_params) - notification_service.new_issue(issue, current_user) - todo_service.new_issue(issue, current_user) - event_service.open_issue(issue, current_user) - user_agent_detail_service(issue, request).create - issue.create_cross_references!(current_user) - execute_hooks(issue, 'open') + if @issue.save + @issue.update_attributes(label_ids: label_params) + notification_service.new_issue(@issue, current_user) + todo_service.new_issue(@issue, current_user) + event_service.open_issue(@issue, current_user) + user_agent_detail_service.create + @issue.create_cross_references!(current_user) + execute_hooks(@issue, 'open') end - issue + @issue end private def spam_check_service - SpamCheckService.new(project, current_user, params) + SpamCheckService.new(@request, @api, @issue) end - def user_agent_detail_service(issue, request) - UserAgentDetailService.new(issue, request) + def user_agent_detail_service + UserAgentDetailService.new(@issue, @request) end end end diff --git a/app/services/spam_check_service.rb b/app/services/spam_check_service.rb index 7d6754546a8..71b9436a22e 100644 --- a/app/services/spam_check_service.rb +++ b/app/services/spam_check_service.rb @@ -1,32 +1,33 @@ -class SpamCheckService < BaseService - attr_accessor :request, :api, :subject +class SpamCheckService + attr_accessor :request, :api, :spammable - def execute(request, api, subject) - @request, @api, @subject = request, api, subject - return false unless request || subject.check_for_spam?(project) - return false unless subject.spam?(request.env, current_user) - - create_spam_log + def initialize(request, api, spammable) + @request, @api, @spammable = request, api, spammable + end - true + 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: current_user.id, - project_id: project.id, - title: params[:title], - description: params[:description], - source_ip: subject.client_ip(request.env), - user_agent: subject.user_agent(request.env), - noteable_type: subject.class.to_s, + 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 - CreateSpamLogService.new(project, current_user, spam_log_attrs).execute + SpamLog.create(spam_log_attrs) end end diff --git a/app/services/user_agent_detail_service.rb b/app/services/user_agent_detail_service.rb index dd995955be3..c07e2ca12a6 100644 --- a/app/services/user_agent_detail_service.rb +++ b/app/services/user_agent_detail_service.rb @@ -1,12 +1,12 @@ class UserAgentDetailService - attr_accessor :subject, :request + attr_accessor :spammable, :request - def initialize(subject, request) - @subject, @request = subject, request + def initialize(spammable, request) + @spammable, @request = spammable, request end def create return unless request - subject.create_user_agent_detail(user_agent: request.env['HTTP_USER_AGENT'], ip_address: request.env['action_dispatch.remote_ip'].to_s) + spammable.create_user_agent_detail(user_agent: request.env['HTTP_USER_AGENT'], ip_address: request.env['action_dispatch.remote_ip'].to_s) end end |