summaryrefslogtreecommitdiff
path: root/app/models/project_services/mattermost_command_service.rb
blob: 262dd3076f789fcf35db0d1613d7fa873f1c88d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class MattermostCommandService < ChatService
  include TriggersHelper

  prop_accessor :token

  def can_test?
    false
  end

  def title
    'Mattermost Command'
  end

  def description
    'Mattermost is an open source, self-hosted Slack-alternative'
  end

  def to_param
    'mattermost_command'
  end

  def help
    "This service allows you to use slash commands with your Mattermost installation.<br/>
    To setup this Service you need to create a new <b>Slash commands</b> in your Mattermost integration panel.<br/>
    <br/>
    Create integration with URL #{service_trigger_url(self)} and enter the token below."
  end

  def fields
    [
      { type: 'text', name: 'token', placeholder: '' }
    ]
  end

  def trigger(params)
    return nil unless valid_token?(params[:token])

    user = find_chat_user(params)
    unless user
      url = authorize_chat_name_url(params)
      return Mattermost::Presenter.authorize_user(url)
    end

    Mattermost::CommandService.new(project, user, params).execute
  end

  private

  def find_chat_user(params)
    ChatNames::FindUserService.new(chat_names, params).execute
  end

  def authorize_chat_name_url(params)
    ChatNames::RequestService.new(self, params).execute
  end
end