1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-03 07:40:05 +08:00
SpaceVim/bundle/vim-chat/autoload/chat/gitter.vim

223 lines
6.7 KiB
VimL
Raw Normal View History

2022-04-30 02:03:01 +08:00
"=============================================================================
" gitter.vim --- a gitter client
" Copyright (c) 2016-2019 Wang Shidong & Contributors
" Author: Wang Shidong < wsdjeg@outlook.com >
" URL: https://spacevim.org
" License: GPLv3
"=============================================================================
let s:JOB = SpaceVim#api#import('job')
let s:JSON = SpaceVim#api#import('data#json')
let s:LOG = SpaceVim#logger#derive('gitter')
let s:room = ''
2022-04-30 13:16:59 +08:00
let g:chat_gitter_token = get(g:, 'chat_gitter_token', '')
2022-04-30 13:45:27 +08:00
let s:room_jobs = {}
function! chat#gitter#enter_room(room) abort
if !has_key(s:room_jobs, a:room)
2022-04-30 18:05:52 +08:00
let roomid = s:room_to_roomid(a:room)
2022-04-30 13:45:27 +08:00
call s:fetch(roomid)
2022-04-30 18:05:52 +08:00
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)
2022-04-30 13:45:27 +08:00
let jobid = s:JOB.start(cmd, {
2022-04-30 02:03:01 +08:00
\ 'on_stdout' : function('s:gitter_stdout'),
\ 'on_stderr' : function('s:gitter_stderr'),
\ 'on_exit' : function('s:gitter_exit'),
\ })
2022-04-30 13:45:27 +08:00
let s:room_jobs[a:room] = jobid
2022-04-30 02:03:01 +08:00
endif
endfunction
2022-04-30 18:05:52 +08:00
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
2022-04-30 02:03:01 +08:00
2022-04-30 18:05:52 +08:00
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
2022-04-30 02:03:01 +08:00
endfunction
2022-04-30 18:05:52 +08:00
2022-04-30 02:03:01 +08:00
function! s:gitter_stdout(id, data, event) abort
for line in a:data
call s:LOG.debug(line)
endfor
2022-04-30 18:05:52 +08:00
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,
\ 'username' : msg.fromUser.username,
2022-04-30 18:05:52 +08:00
\ 'room' : room,
\ 'msg' : msg.text,
\ 'time': s:format_time(msg.sent),
\ })
if !chat#windows#is_opened()
\ && s:enable_notify(room)
2022-04-30 18:05:52 +08:00
call chat#notify#noti(msg.fromUser.displayName . ': ' . msg.text)
endif
return
endif
endfor
2022-04-30 02:03:01 +08:00
endfunction
function! s:enable_notify(room) abort
let room = filter(deepcopy(s:channels), 'has_key(v:val, "uri") && v:val.uri ==# a:room && has_key(v:val, "lurk")')
if !empty(room)
return room[0].lurk
else
return 0
endif
endfunction
2022-04-30 02:03:01 +08:00
function! s:format_time(t) abort
return matchstr(a:t, '\d\d\d\d-\d\d-\d\d') . ' ' . matchstr(a:t, '\d\d:\d\d')
endfunction
function! s:gitter_stderr(id, data, event) abort
for line in a:data
call s:LOG.debug(line)
endfor
endfunction
function! s:gitter_exit(id, data, event) abort
call s:LOG.debug(a:data)
endfunction
2022-04-30 18:05:52 +08:00
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
2022-04-30 02:03:01 +08:00
function! s:gitter_fetch_stdout(id, data, event) abort
for line in a:data
call s:LOG.debug(line)
endfor
2022-04-30 18:05:52 +08:00
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
2022-04-30 02:03:01 +08:00
endfunction
function! s:gitter_fetch_stderr(id, data, event) abort
for line in a:data
call s:LOG.debug(line)
endfor
endfunction
function! s:gitter_fetch_exit(id, data, event) abort
call s:LOG.debug(a:data)
2022-04-30 18:05:52 +08:00
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, ''))
let msgs = []
2022-04-30 18:05:52 +08:00
for msg in messages
call add(msgs, {
2022-04-30 18:05:52 +08:00
\ 'user' : msg.fromUser.displayName,
\ 'username' : msg.fromUser.username,
2022-04-30 18:05:52 +08:00
\ 'room' : room,
\ 'msg' : msg.text,
\ 'time': s:format_time(msg.sent),
\ })
endfor
call chat#windows#push(msgs)
2022-04-30 18:05:52 +08:00
break
endif
2022-04-30 02:03:01 +08:00
endfor
endfunction
2022-04-30 13:16:59 +08:00
let s:channels = []
2022-04-30 11:23:27 +08:00
function! chat#gitter#get_channels() abort
2022-04-30 13:16:59 +08:00
if empty(s:channels)
call s:get_all_channels()
return []
else
let rooms = filter(deepcopy(s:channels), 'has_key(v:val, "uri")')
return map(rooms, 'v:val.uri')
endif
endfunction
let s:list_all_channels_jobid = -1
let s:list_all_channels_result = []
function! s:get_all_channels() abort
if s:list_all_channels_jobid <= 0
let cmd = printf('curl -s -H "Accept: application/json" -H "Authorization: Bearer %s" "https://api.gitter.im/v1/rooms"', g:chat_gitter_token)
let s:list_all_channels_jobid = s:JOB.start(cmd, {
\ 'on_stdout' : function('s:get_all_channels_stdout'),
\ 'on_stderr' : function('s:get_all_channels_stderr'),
\ 'on_exit' : function('s:get_all_channels_exit'),
\ })
endif
endfunction
function! s:get_all_channels_stdout(id, data, event) abort
2022-04-30 18:05:52 +08:00
for line in a:data
call s:LOG.debug(line)
endfor
2022-04-30 13:16:59 +08:00
let s:list_all_channels_result = s:list_all_channels_result + a:data
endfunction
function! s:get_all_channels_stderr(id, data, event) abort
2022-04-30 18:05:52 +08:00
for line in a:data
call s:LOG.debug(line)
endfor
2022-04-30 13:16:59 +08:00
endfunction
function! s:get_all_channels_exit(id, data, event) abort
2022-04-30 18:05:52 +08:00
call s:LOG.debug(a:data)
2022-04-30 13:16:59 +08:00
let s:channels = s:JSON.json_decode(join(s:list_all_channels_result, ''))
2022-04-30 11:23:27 +08:00
endfunction
2022-04-30 18:05:52 +08:00
function! Test(str) abort
exe a:str
endfunction
function! chat#gitter#send(room, msg) abort
let roomid = s:room_to_roomid(a:room)
" the win 11 curl in system32/ directory do not support unicode, use
" neovim's curl
if has('nvim') && exists('v:progpath') && (has('win64') || has('win32'))
let curl = fnamemodify(v:progpath, ':h') . '\curl.exe'
else
let curl = 'curl'
endif
let cmd = [curl, '-X', 'POST', '-H', 'Content-Type: application/json', '-H', 'Accept: application/json',
\ '-H', 'Authorization: Bearer ' . g:chat_gitter_token,
\ printf('https://api.gitter.im/v1/rooms/%s/chatMessages', roomid),
\ '-d',
\ s:JSON.json_encode({'text' : a:msg})
\ ]
2022-04-30 18:05:52 +08:00
call s:JOB.start(cmd)
2022-04-30 02:03:01 +08:00
endfunction