mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-04-14 23:49:19 +08:00
feat(chat): improve gitter support
This commit is contained in:
parent
92b247e6c7
commit
feb355f905
@ -17,9 +17,9 @@ let g:chat_gitter_token = get(g:, 'chat_gitter_token', '')
|
||||
let s:room_jobs = {}
|
||||
function! chat#gitter#enter_room(room) abort
|
||||
if !has_key(s:room_jobs, a:room)
|
||||
let roomid = s:get_roomid(a:room)
|
||||
let roomid = s:room_to_roomid(a:room)
|
||||
call s:fetch(roomid)
|
||||
let cmd = printf('curl -s -H "Accept: application/json" -H "Authorization: Bearer %s" "https://stream.gitter.im/v1/rooms/%s/chatMessages"',token , roomid)
|
||||
let cmd = printf('curl -s -N -H "Accept: application/json" -H "Authorization: Bearer %s" "https://stream.gitter.im/v1/rooms/%s/chatMessages"',g:chat_gitter_token , roomid)
|
||||
let jobid = s:JOB.start(cmd, {
|
||||
\ 'on_stdout' : function('s:gitter_stdout'),
|
||||
\ 'on_stderr' : function('s:gitter_stderr'),
|
||||
@ -29,32 +29,49 @@ function! chat#gitter#enter_room(room) abort
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
function! s:fetch(roomid) abort
|
||||
let s:fetch_response = []
|
||||
call s:JOB.start(g:gitter_fetch_command, {
|
||||
\ 'on_stdout' : function('s:gitter_fetch_stdout'),
|
||||
\ 'on_stderr' : function('s:gitter_fetch_stderr'),
|
||||
\ 'on_exit' : function('s:gitter_fetch_exit'),
|
||||
\ })
|
||||
function! s:room_to_roomid(room) abort
|
||||
let room = filter(deepcopy(s:channels), 'has_key(v:val, "uri") && v:val.uri ==# a:room')
|
||||
if !empty(room)
|
||||
return room[0].id
|
||||
else
|
||||
return ''
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:roomid_to_room(roomid) abort
|
||||
let room = filter(deepcopy(s:channels), 'has_key(v:val, "id") && has_key(v:val, "uri") && v:val.id ==# a:roomid')
|
||||
if !empty(room)
|
||||
return room[0].uri
|
||||
else
|
||||
return ''
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
function! s:gitter_stdout(id, data, event) abort
|
||||
for line in a:data
|
||||
call s:LOG.debug(line)
|
||||
endfor
|
||||
let message = join(a:data, '')
|
||||
let msg = s:JSON.json_decode(message)
|
||||
if chat#windows#is_opened()
|
||||
call chat#windows#push({
|
||||
\ 'user' : msg.fromUser.displayName,
|
||||
\ 'room' : s:room,
|
||||
\ 'msg' : msg.text,
|
||||
\ 'time': s:format_time(msg.sent),
|
||||
\ })
|
||||
else
|
||||
call chat#notify#noti(msg.fromUser.displayName . ': ' . msg.text)
|
||||
endif
|
||||
for room in keys(s:room_jobs)
|
||||
if s:room_jobs[room] ==# a:id
|
||||
let message = join(a:data, '')
|
||||
if message =~# '^\s*$'
|
||||
" skip empty string or space
|
||||
return
|
||||
endif
|
||||
let msg = s:JSON.json_decode(message)
|
||||
call chat#windows#push({
|
||||
\ 'user' : msg.fromUser.displayName,
|
||||
\ 'room' : room,
|
||||
\ 'msg' : msg.text,
|
||||
\ 'time': s:format_time(msg.sent),
|
||||
\ })
|
||||
if !chat#windows#is_opened()
|
||||
call chat#notify#noti(msg.fromUser.displayName . ': ' . msg.text)
|
||||
endif
|
||||
return
|
||||
endif
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
function! s:format_time(t) abort
|
||||
@ -72,11 +89,33 @@ function! s:gitter_exit(id, data, event) abort
|
||||
call s:LOG.debug(a:data)
|
||||
endfunction
|
||||
|
||||
let s:fetch_response = {}
|
||||
function! s:fetch(roomid) abort
|
||||
let room = s:roomid_to_room(a:roomid)
|
||||
if !has_key(s:fetch_response, room)
|
||||
let cmd = printf( 'curl -s -H "Accept: application/json" -H "Authorization: Bearer %s" "https://api.gitter.im/v1/rooms/%s/chatMessages?limit=50"', g:chat_gitter_token, a:roomid)
|
||||
let jobid = s:JOB.start(cmd, {
|
||||
\ 'on_stdout' : function('s:gitter_fetch_stdout'),
|
||||
\ 'on_stderr' : function('s:gitter_fetch_stderr'),
|
||||
\ 'on_exit' : function('s:gitter_fetch_exit'),
|
||||
\ })
|
||||
let s:fetch_response[room] = {
|
||||
\ 'jobid' : jobid,
|
||||
\ 'response' : [],
|
||||
\ }
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! s:gitter_fetch_stdout(id, data, event) abort
|
||||
for line in a:data
|
||||
call s:LOG.debug(line)
|
||||
endfor
|
||||
let s:fetch_response = s:fetch_response + a:data
|
||||
for room in keys(s:fetch_response)
|
||||
if s:fetch_response[room].jobid ==# a:id
|
||||
let s:fetch_response[room].response += a:data
|
||||
break
|
||||
endif
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
function! s:gitter_fetch_stderr(id, data, event) abort
|
||||
@ -88,14 +127,19 @@ endfunction
|
||||
|
||||
function! s:gitter_fetch_exit(id, data, event) abort
|
||||
call s:LOG.debug(a:data)
|
||||
let messages = s:JSON.json_decode(join(s:fetch_response, ''))
|
||||
for msg in messages
|
||||
call chat#windows#push({
|
||||
\ 'user' : msg.fromUser.displayName,
|
||||
\ 'room' : s:room,
|
||||
\ 'msg' : msg.text,
|
||||
\ 'time': s:format_time(msg.sent),
|
||||
\ })
|
||||
for room in keys(s:fetch_response)
|
||||
if s:fetch_response[room].jobid ==# a:id
|
||||
let messages = s:JSON.json_decode(join(s:fetch_response[room].response, ''))
|
||||
for msg in messages
|
||||
call chat#windows#push({
|
||||
\ 'user' : msg.fromUser.displayName,
|
||||
\ 'room' : room,
|
||||
\ 'msg' : msg.text,
|
||||
\ 'time': s:format_time(msg.sent),
|
||||
\ })
|
||||
endfor
|
||||
break
|
||||
endif
|
||||
endfor
|
||||
endfunction
|
||||
|
||||
@ -125,15 +169,28 @@ function! s:get_all_channels() abort
|
||||
endfunction
|
||||
|
||||
function! s:get_all_channels_stdout(id, data, event) abort
|
||||
for line in a:data
|
||||
call s:LOG.debug(line)
|
||||
endfor
|
||||
let s:list_all_channels_result = s:list_all_channels_result + a:data
|
||||
endfunction
|
||||
function! s:get_all_channels_stderr(id, data, event) abort
|
||||
for line in a:data
|
||||
call s:LOG.debug(line)
|
||||
endfor
|
||||
|
||||
endfunction
|
||||
function! s:get_all_channels_exit(id, data, event) abort
|
||||
call s:LOG.debug(a:data)
|
||||
let s:channels = s:JSON.json_decode(join(s:list_all_channels_result, ''))
|
||||
endfunction
|
||||
|
||||
function! chat#gitter#send(msg) abort
|
||||
call s:JOB.start(printf(g:gitter_send_command, substitute(a:msg, '"', '\\\"', 'g')))
|
||||
function! Test(str) abort
|
||||
exe a:str
|
||||
endfunction
|
||||
|
||||
function! chat#gitter#send(room, msg) abort
|
||||
let roomid = s:room_to_roomid(a:room)
|
||||
let cmd = printf('curl -X POST -i -H "Content-Type: application/json" -H "Accept: application/json" -H "Authorization: Bearer %s" "https://api.gitter.im/v1/rooms/%s/chatMessages" -d "{\"text\":\"%s\"}"', g:chat_gitter_token, roomid, substitute(a:msg, '"', '\\\"', 'g'))
|
||||
call s:JOB.start(cmd)
|
||||
endfunction
|
||||
|
@ -227,7 +227,12 @@ function! s:enter() abort
|
||||
call s:update_msg_screen()
|
||||
return
|
||||
elseif s:c_begin . s:c_char . s:c_end =~# '/set_channel\s*'
|
||||
let s:current_channel = matchstr(s:c_begin . s:c_char . s:c_end, '/set_channel\s*\zs.*')
|
||||
if !empty(s:protocol)
|
||||
let s:current_channel = matchstr(s:c_begin . s:c_char . s:c_end, '/set_channel\s*\zs.*')
|
||||
if !empty(s:current_channel)
|
||||
call chat#{s:protocol}#enter_room(s:current_channel)
|
||||
endif
|
||||
endif
|
||||
let s:c_end = ''
|
||||
let s:c_char = ''
|
||||
let s:c_begin = ''
|
||||
@ -259,7 +264,7 @@ function! s:complete(base,num) abort
|
||||
let channels = chat#{s:protocol}#get_channels()
|
||||
catch
|
||||
endtry
|
||||
let rsl = filter(copy(channels), "v:val =~# matchstr(a:base, '\w*$') .'[^\ .]*'")
|
||||
let rsl = filter(copy(channels), "v:val =~# '^' . matchstr(a:base, '\\w*$')")
|
||||
if len(rsl) > 0
|
||||
return matchstr(a:base, '^/set_channel\s*') . rsl[a:num % len(rsl)]
|
||||
endif
|
||||
@ -353,8 +358,8 @@ function! s:next_channel() abort
|
||||
endfunction
|
||||
|
||||
function! s:send(msg) abort
|
||||
if !empty(s:protocol)
|
||||
call chat#{s:protocol}#send(a:msg)
|
||||
if !empty(s:protocol) && !empty(s:current_channel)
|
||||
call chat#{s:protocol}#send(s:current_channel, a:msg)
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user