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

Fix flow

parent 34295036
...@@ -32,6 +32,7 @@ class Projects::MattermostsController < Projects::ApplicationController ...@@ -32,6 +32,7 @@ class Projects::MattermostsController < Projects::ApplicationController
def teams def teams
@teams ||= @service.list_teams(current_user) @teams ||= @service.list_teams(current_user)
rescue => e rescue => e
@teams = []
flash[:alert] = e.message flash[:alert] = e.message
end end
......
...@@ -25,18 +25,15 @@ class MattermostSlashCommandsService < ChatService ...@@ -25,18 +25,15 @@ class MattermostSlashCommandsService < ChatService
] ]
end end
def configure!(current_user, params) def configure!(user, params)
token = Mattermost::Session.new(current_user).with_session do |session| token = Mattermost::Command.new(user).
Mattermost::Command.create(session, command(params)) create(command(params))
end
update!(active: true, token: token) update!(active: true, token: token)
end end
def list_teams(user) def list_teams(user)
Mattermost::Session.new(user).with_session do |session| Mattermost::Team.new(user).all
Mattermost::Team.all(session)
end
end end
def trigger(params) 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 module Mattermost
class Command class Command < Client
def self.create(session, params) def create(params)
response = session.post("/api/v3/teams/#{params[:team_id]}/commands/create", response = json_post("/api/v3/teams/#{params[:team_id]}/commands/create",
body: params.to_json) body: params.to_json)
if response.success? response['token']
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
end end
end end
end end
module Mattermost module Mattermost
class Team class Team < Client
def self.all(session) def all
response = session.get('/api/v3/teams/all') json_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
end end
end 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