summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar √Čtienne Deparis 2020-03-28 14:56:25 +0100
committerGravatar √Čtienne Deparis 2020-03-28 15:12:22 +0100
commitbb226fc5b5f1f621bfd976ea48caae8fa27e83da (patch)
treeef16a0cb4e6b55fa0cb1501c61ffffb926c3f08c
parent99b87beb10464bde73631a977606edde2a523a79 (diff)
downloadstatus-bb226fc5b5f1f621bfd976ea48caae8fa27e83da.tar.gz
status-bb226fc5b5f1f621bfd976ea48caae8fa27e83da.tar.xz
status-bb226fc5b5f1f621bfd976ea48caae8fa27e83da.zip
feat: Improve mattermost helper by using API calls
-rw-r--r--lib/mattermost_helper.rb87
-rw-r--r--status_app.rb3
2 files changed, 81 insertions, 9 deletions
diff --git a/lib/mattermost_helper.rb b/lib/mattermost_helper.rb
index 42a150a..105c869 100644
--- a/lib/mattermost_helper.rb
+++ b/lib/mattermost_helper.rb
@@ -13,21 +13,91 @@ module MattermostHelper # rubocop:disable Metrics/ModuleLength
line.join(' ')
end
- def mattermost_hook_message
+ def mattermost_answer_message
state = format_mattermost_status(@current_status, 'present' => 'back')
['@' + @current_status['handle'], 'is', state].join(' ')
end
+ def mattermost_build_curl_cmd(endpoint, meth = 'GET', data = nil)
+ api_token = @config.dig('mattermost', 'api_token')
+ api_url = @config.dig('mattermost', 'api_url')
+ return nil unless api_token && api_url
+ curl_cmd = ['curl', '-s', '-X', meth, '-H',
+ "Authorization: Bearer #{api_token}"]
+ if ['POST', 'PUT'].include?(meth)
+ curl_cmd += ['-H', 'Content-Type: application/json']
+ end
+ curl_cmd += ['-d', JSON.dump(data)] if data
+ curl_cmd << api_url + endpoint
+ curl_cmd
+ end
+
+ def mattermost_run_curl(curl_cmd)
+ warn curl_cmd.join(' ') if ENV['RACK_ENV'] == 'development'
+ data = nil
+ IO.popen(curl_cmd, unsetenv_others: true, err: '/dev/null') do |p|
+ data = p.read
+ end
+ warn data if ENV['RACK_ENV'] == 'development'
+ data
+ end
+
+ def mattermost_fetch_user_id(req)
+ curl_cmd = mattermost_build_curl_cmd(req)
+ return nil unless curl_cmd
+ raw = mattermost_run_curl(curl_cmd)
+ begin
+ data = JSON.parse(raw)
+ rescue JSON::ParserError
+ warn "Wrong data received: #{data.inspect}"
+ end
+ { 'mattermost_uid' => data.dig('id'),
+ 'handle' => data.dig('username') }
+ end
+
+ def mattermost_user_id
+ return @details['mattermost_uid'] if @details['mattermost_uid']
+ if key_value_unless_blank(@current_status, 'handle')
+ req = '/users/username/' + @current_status['handle']
+ else
+ req = '/users/email/' + @current_status['email']
+ end
+ data = mattermost_fetch_user_id(req)
+ return nil unless data
+ update_status(data, @current_status['email'])
+ data['mattermost_uid']
+ end
+
def send_mattermost_notification
- hook_url = @config.dig('mattermost', 'hook_url')
- return unless hook_url && @current_status['handle']
+ return unless @current_status['handle']
if ENV['RACK_ENV'] == 'development'
- warn mattermost_hook_message
+ channel = @config.dig('mattermost', 'test_channel')
+ else
+ channel = @config.dig('mattermost', 'public_channel')
+ end
+ return unless channel
+ uid = mattermost_user_id
+ return unless uid
+ msg = { 'channel_id' => channel,
+ 'message' => mattermost_answer_message }
+ curl_cmd = mattermost_build_curl_cmd('/posts', 'POST', msg)
+ return unless curl_cmd
+ mattermost_run_curl(curl_cmd)
+ end
+
+ def change_mattermost_status(new_status)
+ return unless @current_status['handle']
+ uid = mattermost_user_id
+ return unless uid
+ if new_status == 'present'
+ mat_state = 'online'
else
- system('curl', '-s', '-X', 'POST', '--data-urlencode',
- "payload={\"text\": \"#{mattermost_hook_message}\"}",
- hook_url)
+ mat_state = 'dnd'
end
+ data = { 'user_id' => uid, 'status' => mat_state }
+ curl_cmd = mattermost_build_curl_cmd("/users/#{uid}/status", 'PUT', data)
+ return unless curl_cmd
+ mattermost_run_curl(curl_cmd)
end
def find_status_for_mattermost(username)
@@ -72,8 +142,9 @@ module MattermostHelper # rubocop:disable Metrics/ModuleLength
eta = '' if status == 'present'
update_status({ 'status' => status, 'eta' => eta },
@current_status['email'])
+ change_mattermost_status(status)
{ 'response_type' => 'in_channel',
- 'text' => mattermost_hook_message }
+ 'text' => mattermost_answer_message }
end
def mattermost_command_help(username)
diff --git a/status_app.rb b/status_app.rb
index a273c4b..b9a077a 100644
--- a/status_app.rb
+++ b/status_app.rb
@@ -67,6 +67,7 @@ class StatusApp < Sinatra::Base
details['eta'] = '' if details['status'] == 'present'
update_status(details, @current_status['email'])
send_mattermost_notification
+ change_mattermost_status(@current_status['status'])
redirect to("/status?#{@auth_chain}")
end
@@ -107,7 +108,7 @@ class StatusApp < Sinatra::Base
post '/mattermost' do
content_type 'application/json'
- check_token = @config.dig('mattermost', 'token')
+ check_token = @config.dig('mattermost', 'slash_token')
if !check_token || params[:token] != check_token
return JSON.dump({ 'response_type' => 'ephemeral',
'text' => 'Not authorized' })