Created
February 10, 2024 15:20
-
-
Save Hakkadaikon/d779b9c2aba45a0409a59d5de03a66e1 to your computer and use it in GitHub Desktop.
gorilla.vim #30 live coding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"------------------------------------------------------------------------------ | |
" client.vim | |
" | |
" description: | |
" This is a sample client for the nostr relay. | |
" dependencies: | |
" ui.vim : https://github.com/skanehira/ui.vim | |
" nostr-vim : https://github.com/Hakkadaikon/nostr-vim | |
"------------------------------------------------------------------------------ | |
function! s:create_buffer() | |
silent noautocmd split __Nostr_TL__ | |
setlocal buftype=nofile bufhidden=wipe noswapfile | |
setlocal wrap nonumber signcolumn=no filetype=markdown | |
wincmd p | |
let s:winid = bufwinid("__Nostr_TL__") | |
let s:note_map = {} | |
let s:is_duplicate_event_map = {} | |
endfunction | |
function! s:find_noteid_from_tags(tags) | |
let l:noteid = "" | |
for l:tag in a:tags | |
if l:tag[0] == "e" | |
let l:noteid = l:tag[1] | |
break | |
endif | |
endfor | |
return l:noteid | |
endfunction | |
function! s:update_reaction_from_notemap(noteid, reaction) | |
if !has_key(s:note_map, a:noteid) | |
return | |
endif | |
let l:found = 0 | |
for l:reaction in s:note_map[a:noteid]["reactions"] | |
if l:reaction["action"] == a:reaction | |
let l:found = 1 | |
let l:reaction["count"] += 1 | |
endif | |
endfor | |
if l:found == 0 | |
call add(s:note_map[a:noteid]["reactions"], { | |
\ "action": a:reaction, | |
\ "count": 1 | |
\}) | |
endif | |
endfunction | |
function! s:insert_note_to_notemap(event, profile) | |
let l:time = strftime("%Y-%m-%d %H:%M:%S", a:event["created_at"]) | |
let s:note_map[a:event["id"]] = { | |
\ "text": a:event["content"], | |
\ "user": { | |
\ "name": a:profile["name"], | |
\ "screen_name": a:profile["display_name"], | |
\ "url": a:profile["website"], | |
\ }, | |
\ "metadata": { | |
\ "created_at_str": l:time, | |
\ }, | |
\"reactions": [] | |
\} | |
endfunction | |
function! s:update_buffer() | |
let l:note_array = [] | |
for l:note in values(s:note_map) | |
call add(l:note_array, l:note) | |
endfor | |
call win_execute(s:winid, 'setlocal modifiable', 1) | |
call win_execute(s:winid, 'normal! G', 1) | |
call win_gotoid(s:winid) | |
call ui#chat#draw(s:winid, l:note_array) | |
call win_execute(s:winid, 'setlocal nomodifiable nomodified', 1) | |
endfunction | |
" s:callback(event, profile) | |
" - event : The event received from the nostr relay. | |
" - profile: The profile of the user who posted the event. | |
function! s:callback(event, profile) | |
if has_key(s:is_duplicate_event_map, a:event["id"]) | |
return | |
endif | |
let s:is_duplicate_event_map[a:event["id"]] = 1 | |
if a:profile == {} | |
return | |
endif | |
let l:kind = a:event["kind"] | |
if l:kind == 1 | |
call s:insert_note_to_notemap(a:event, a:profile) | |
elseif l:kind == 7 | |
let l:noteid = s:find_noteid_from_tags(a:event["tags"]) | |
call s:update_reaction_from_notemap(l:noteid, a:event["content"]) | |
else | |
return | |
endif | |
call s:update_buffer() | |
endfunction | |
call s:create_buffer() | |
call nostr#open(function('s:callback')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment