Read about our upcoming Code of Conduct on this issue

slash_commands_service.rb 1.4 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
# Base class for Chat services
# This class is not meant to be used directly, but only to inherrit from.
5
class SlashCommandsService < Service
6
7
8
9
  default_value_for :category, 'chat'

  prop_accessor :token

10
  has_many :chat_names, foreign_key: :service_id, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
11
12
13
14

  def valid_token?(token)
    self.respond_to?(:token) &&
      self.token.present? &&
Heinrich Lee Yu's avatar
Heinrich Lee Yu committed
15
      ActiveSupport::SecurityUtils.secure_compare(token, self.token)
16
17
  end

18
19
  def self.supported_events
    %w()
20
21
22
23
24
25
26
27
  end

  def can_test?
    false
  end

  def fields
    [
28
      { type: 'text', name: 'token', placeholder: 'XXxxXXxxXXxxXXxxXXxxXXxx' }
29
30
31
32
    ]
  end

  def trigger(params)
Z.J. van de Weg's avatar
Z.J. van de Weg committed
33
    return unless valid_token?(params[:token])
34

35
    chat_user = find_chat_user(params)
36

37
38
    if chat_user&.user
      Gitlab::SlashCommands::Command.new(project, chat_user, params).execute
39
    else
40
      url = authorize_chat_name_url(params)
41
      Gitlab::SlashCommands::Presenters::Access.new(url).authorize
42
43
44
45
46
    end
  end

  private

47
  # rubocop: disable CodeReuse/ServiceClass
48
49
50
  def find_chat_user(params)
    ChatNames::FindUserService.new(self, params).execute
  end
51
  # rubocop: enable CodeReuse/ServiceClass
52

53
  # rubocop: disable CodeReuse/ServiceClass
54
55
56
  def authorize_chat_name_url(params)
    ChatNames::AuthorizeUserService.new(self, params).execute
  end
57
  # rubocop: enable CodeReuse/ServiceClass
58
end