Commit 841960f8 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Fix flow

parent 34295036
......@@ -32,6 +32,7 @@ class Projects::MattermostsController < Projects::ApplicationController
def teams
@teams ||= @service.list_teams(current_user)
rescue => e
@teams = []
flash[:alert] = e.message
end
......
......@@ -25,18 +25,15 @@ class MattermostSlashCommandsService < ChatService
]
end
def configure!(current_user, params)
token = Mattermost::Session.new(current_user).with_session do |session|
Mattermost::Command.create(session, command(params))
end
def configure!(user, params)
token = Mattermost::Command.new(user).
create(command(params))
update!(active: true, token: token)
end
def list_teams(user)
Mattermost::Session.new(user).with_session do |session|
Mattermost::Team.all(session)
end
Mattermost::Team.new(user).all
end
def trigger(params)
......
module Mattermost
class Client
attr_reader :user
def initialize(user)
@user = user
end
private
def with_session(&blk)
Session.new(user).with_session(&blk)
end
def json_get(path, options = {})
with_session do |session|
json_response session.get(path, options)
end
end
def json_post(path, options = {})
with_session do |session|
json_response session.post(path, options)
end
end
def json_response(response)
json_response = JSON.parse(response.body)
if response.success?
json_response
elsif json_response['message']
raise json_response['message']
else
raise 'Undefined error'
end
end
end
end
module Mattermost
class Command
def self.create(session, params)
response = session.post("/api/v3/teams/#{params[:team_id]}/commands/create",
class Command < Client
def create(params)
response = json_post("/api/v3/teams/#{params[:team_id]}/commands/create",
body: params.to_json)
if response.success?
response.parsed_response['token']
elsif response.parsed_response.try(:has_key?, 'message')
raise response.parsed_response['message']
else
raise 'Failed to create a new command'
end
response['token']
end
end
end
module Mattermost
class Team
def self.all(session)
response = session.get('/api/v3/teams/all')
if response.success?
response.parsed_response
elsif response.parsed_response.try(:has_key?, 'message')
raise response.parsed_response['message']
else
raise 'Failed to list teams'
end
class Team < Client
def all
json_get('/api/v3/teams/all')
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment