mattermost_chat_service.rb 1.41 KB
Newer Older
1
class MattermostChatService < ChatService
2
3
4
5
6
7
8
9
  include TriggersHelper

  prop_accessor :token

  def can_test?
    false
  end

10
11
12
13
14
  def title
    'Mattermost'
  end

  def description
15
    'Mattermost is an open source, self-hosted Slack-alternative'
16
17
18
19
20
21
22
  end

  def to_param
    'mattermost'
  end

  def help
23
24
25
26
    "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."
27
28
29
30
  end

  def fields
    [
31
      { type: 'text', name: 'token', placeholder: '' }
32
33
34
35
    ]
  end

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

    user = find_chat_user(params)
39
40
    return authorize_chat_name(params) unless user

41
42
    Mattermost::CommandService.new(project, user, params.slice(:command, :text)).
      execute
43
44
45
46
  end

  private

47
48
49
50
51
52
53
  def find_chat_user(params)
    params = params.slice(:team_id, :user_id)
    ChatNames::FindUserService.
      new(chat_names, params).
      execute
  end

54
  def authorize_chat_name(params)
55
56
    params = params.slice(:team_id, :team_domain, :user_id, :user_name)
    url = ChatNames::AuthorizeUserService.new(self, params).execute
57
58
59
60
61
62
63

    {
      response_type: :ephemeral,
      message: "You are not authorized. Click this [link](#{url}) to authorize."
    }
  end
end