1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-02 22:20:06 +08:00

fix(gitter): improve fetch logic

This commit is contained in:
wsdjeg 2022-05-07 17:17:04 +08:00
parent 1fb0b1d76e
commit 67cfdddf69

View File

@ -19,12 +19,14 @@ let g:chat_gitter_token = get(g:, 'chat_gitter_token', '')
let s:room_jobs = {} let s:room_jobs = {}
function! chat#gitter#enter_room(room) abort function! chat#gitter#enter_room(room) abort
if !has_key(s:room_jobs, a:room) let roomid = s:room_to_roomid(a:room)
let roomid = s:room_to_roomid(a:room) if empty(roomid)
if empty(roomid) return 0
return 0 endif
endif if !has_key(s:fetch_response, a:room)
call s:fetch(roomid) call s:fetch(roomid)
endif
if !has_key(s:room_jobs, a:room)
let cmd = printf('curl -s --show-error --fail -N -H "Accept: application/json" -H "Authorization: Bearer %s" "https://stream.gitter.im/v1/rooms/%s/chatMessages"',g:chat_gitter_token , roomid) let cmd = printf('curl -s --show-error --fail -N -H "Accept: application/json" -H "Authorization: Bearer %s" "https://stream.gitter.im/v1/rooms/%s/chatMessages"',g:chat_gitter_token , roomid)
let s:room_jobs[a:room] = s:JOB.start(cmd, { let s:room_jobs[a:room] = s:JOB.start(cmd, {
\ 'on_stdout' : function('s:gitter_stream_stdout'), \ 'on_stdout' : function('s:gitter_stream_stdout'),
@ -138,7 +140,8 @@ function! s:fetch(roomid) abort
if !has_key(s:fetch_response, room) if !has_key(s:fetch_response, room)
let cmd = printf( 'curl -s --show-error --fail -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 cmd = printf( 'curl -s --show-error --fail -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 s:fetch_response[room] = { let s:fetch_response[room] = {
\ 'response' : [], \ 'stdout' : [],
\ 'stderr' : [],
\ 'jobid' : s:JOB.start(cmd, \ 'jobid' : s:JOB.start(cmd,
\ { \ {
\ 'on_stdout' : function('s:gitter_fetch_stdout'), \ 'on_stdout' : function('s:gitter_fetch_stdout'),
@ -163,7 +166,7 @@ function! s:gitter_fetch_stdout(id, data, event) abort
call s:LOG.debug('s:fetch_response keys :' . string(keys(s:fetch_response))) call s:LOG.debug('s:fetch_response keys :' . string(keys(s:fetch_response)))
for room in keys(s:fetch_response) for room in keys(s:fetch_response)
if s:fetch_response[room].jobid ==# a:id if s:fetch_response[room].jobid ==# a:id
let s:fetch_response[room].response += a:data let s:fetch_response[room].stdout += a:data
break break
endif endif
endfor endfor
@ -173,26 +176,32 @@ function! s:gitter_fetch_stderr(id, data, event) abort
for line in a:data for line in a:data
call s:LOG.debug('fetch_stderr :' . line) call s:LOG.debug('fetch_stderr :' . line)
endfor endfor
for room in keys(s:fetch_response)
if s:fetch_response[room].jobid ==# a:id
let s:fetch_response[room].stderr += a:data
break
endif
endfor
endfunction endfunction
function! s:gitter_fetch_exit(id, data, event) abort function! s:gitter_fetch_exit(id, data, event) abort
call s:LOG.debug('fetch job exit code' . a:data) call s:LOG.debug('fetch job exit code :' . a:data)
for room in keys(s:fetch_response) for room in keys(s:fetch_response)
if s:fetch_response[room].jobid ==# a:id if s:fetch_response[room].jobid ==# a:id
let messages = s:JSON.json_decode(join(s:fetch_response[room].response, '')) if !empty(s:fetch_response[room].stdout)
let msgs = [] let messages = s:JSON.json_decode(join(s:fetch_response[room].stdout, ''))
for msg in messages let msgs = []
call add(msgs, { for msg in messages
\ 'user' : msg.fromUser.displayName, call add(msgs, {
\ 'username' : msg.fromUser.username, \ 'user' : msg.fromUser.displayName,
\ 'room' : room, \ 'username' : msg.fromUser.username,
\ 'msg' : msg.text, \ 'room' : room,
\ 'replyCounts' : get(msg, 'threadMessageCount', 0), \ 'msg' : msg.text,
\ 'time': s:format_time(msg.sent), \ 'replyCounts' : get(msg, 'threadMessageCount', 0),
\ }) \ 'time': s:format_time(msg.sent),
endfor \ })
call chat#windows#push(msgs) endfor
if a:data ==# 0 call chat#windows#push(msgs)
call chat#windows#push({ call chat#windows#push({
\ 'user' : '--->', \ 'user' : '--->',
\ 'username' : '--->', \ 'username' : '--->',
@ -200,8 +209,18 @@ function! s:gitter_fetch_exit(id, data, event) abort
\ 'msg' : 'fetch channel message done!', \ 'msg' : 'fetch channel message done!',
\ 'time': strftime("%Y-%m-%d %H:%M"), \ 'time': strftime("%Y-%m-%d %H:%M"),
\ }) \ })
return
else
call chat#windows#push({
\ 'user' : '--->',
\ 'username' : '--->',
\ 'room' : room,
\ 'msg' : 'failed to fetch message.',
\ 'time': strftime("%Y-%m-%d %H:%M"),
\ })
unlet s:fetch_response[room]
endif endif
break return
endif endif
endfor endfor
endfunction endfunction