diff options
author | Z.J. van de Weg <git@zjvandeweg.nl> | 2016-11-15 09:55:56 +0100 |
---|---|---|
committer | Z.J. van de Weg <git@zjvandeweg.nl> | 2016-11-17 21:34:23 +0100 |
commit | 106b1e39c0d00f25e34dda0eba962c8cf1d43f1b (patch) | |
tree | e7033c7543c697bec6612a294925c2e3456ecbd7 /lib/mattermost | |
parent | 53271b486d296fae2e290d6948a05aeb47dbea89 (diff) | |
download | gitlab-ce-106b1e39c0d00f25e34dda0eba962c8cf1d43f1b.tar.gz |
First steps on refactoring Mattermost Slash commands
Now, each subcommand has its own service, plus I've introduced
presenters to be able to delegate the generation of the views.
Diffstat (limited to 'lib/mattermost')
-rw-r--r-- | lib/mattermost/presenter.rb | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/lib/mattermost/presenter.rb b/lib/mattermost/presenter.rb new file mode 100644 index 00000000000..d8e3b3805f9 --- /dev/null +++ b/lib/mattermost/presenter.rb @@ -0,0 +1,84 @@ +module Mattermost + class Presenter + class << self + COMMAND_PREFIX = '/gitlab'.freeze + + def authorize_chat_name(params) + url = ChatNames::RequestService.new(service, params).execute + + { + response_type: :ephemeral, + message: "You are not authorized. Click this [link](#{url}) to authorize." + } + end + + # TODO figure out how I know which are available or not + def help_message(commands) + messages = ["Available commands:"] + + commands.each do |sub_command, attrs| + messages << "\t#{COMMAND_PREFIX} #{attrs[:help_message]}" + end + + { + response_type: :ephemeral, + text: messages.join("\n") + } + end + + def not_found + { + response_type: :ephemeral, + text: "404 not found! GitLab couldn't find what your were looking for! :boom:", + } + end + end + + attr_reader :result + + def initialize(result) + @result = result + end + + def present + if result.respond_to?(:count) + if result.count > 1 + return respond_collection(result) + elsif result.count == 0 + return not_found + else + result = result.first + end + end + + single_resource + end + + private + + def single_resource + message = title(resource) + message << "\n\n#{resource.description}" if resource.description + + { + response_type: :in_channel, + text: message + } + end + + def multiple_resources(resources) + message = "Multiple results were found:\n" + message << resource.map { |resource| " #{title(resource)}" }.join("\n") + + { + response_type: :ephemeral, + text: message + } + end + + def title(resource) + url = url_for([resource.project.namespace.becomes(Namespace), resource.project, resource]) + "### [#{resource.to_reference} #{resource.title}](#{url})" + end + end +end |