diff options
author | pdp8 <pdp8@pdp8.info> | 2023-06-21 10:32:41 +0200 |
---|---|---|
committer | pdp8 <pdp8@pdp8.info> | 2023-06-21 10:32:41 +0200 |
commit | b837b19b1950c7bc14a38aa5ea917e91b6f081dd (patch) | |
tree | 3805eebb26ce716c241e00091f5788fb750bf716 /activitypub.rb | |
parent | c719909e458d9896ec777b25fe8620df3eb9ff7a (diff) |
erb templates separated
Diffstat (limited to 'activitypub.rb')
-rw-r--r-- | activitypub.rb | 138 |
1 files changed, 34 insertions, 104 deletions
diff --git a/activitypub.rb b/activitypub.rb index f51ea3b..e250fe1 100644 --- a/activitypub.rb +++ b/activitypub.rb @@ -5,7 +5,7 @@ # boost # thread expansion # include own posts in threads -# remaining activities +# implement remaining activities # test with pleroma etc # client @@ -136,13 +136,13 @@ end post "/archive" do protected! FileUtils.mv params['file'], "archive/" - redirect to("/##{params['anchor']}") + redirect to(params['redirect']) end post "/delete" do protected! FileUtils.rm params['file'] - redirect to("/archive##{params['anchor']}") + redirect to(params['redirect']) end post "/delete_all" do @@ -200,7 +200,7 @@ get "/pdp8", :provides => 'html' do end get "/pdp8" do - send_file "pdp8.json" + send_file "pdp8.json", :type => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' end ["/outbox","/following","/followers"].each do |path| @@ -210,14 +210,21 @@ end end # private -get "/archive", :provides => 'html' do - protected! - dir_html "archive" -end - -get "/", :provides => 'html' do - protected! - dir_html "inbox" +["/", "/archive"].each do |path| + get path, :provides => 'html' do + protected! + if path == '/' + @dir = 'inbox' + @alt_dir = '/archive' + @alt_name = 'archive' + else + @dir = path.sub('/','') + @alt_dir = '/' + @alt_name = 'inbox' + end + threads + erb :index + end end helpers do @@ -255,15 +262,14 @@ helpers do halt 400 unless key.verify(OpenSSL::Digest.new('SHA256'), signature, comparison) rescue => e - p request.env["HTTP_SIGNATURE"] - p e + p request.env["HTTP_SIGNATURE"], e halt 400 end end - def dir_html dir + def threads nr = 0 - items = Dir[File.join(dir, '*.json')].sort.collect do |file| + items = Dir[File.join(@dir, '*.json')].sort.collect do |file| item = JSON.parse(File.read(file)) mention = mention(item['attributedTo']) following_path = File.join('public', 'following', mention + '.json') @@ -278,101 +284,27 @@ helpers do :follow => follow, :content => item['content'], :attachment => item['attachment'], + :indent => 2, :replies => [] } end.compact items.last[:nr] = items.last[:nr] - 2 - threads = [] + @threads = [] items.each do |i| if i[:parent].nil? or items.select{|it| it[:id] == i[:parent] }.empty? - threads << i + @threads << i else - items.select{|it| it[:id] == i[:parent] }.each{|it| it[:replies] << i} + items.select{|it| it[:id] == i[:parent] }.each do |it| + i[:indent] = it[:indent] + 2 + it[:replies] << i + end end end - html="<!DOCTYPE html> - <html lang='en'> - <head> - <link rel='stylesheet' type='text/css' href='/style.css'> - </head> - <body> - <h1>#{dir}" - - if dir == "inbox" - html << "<form action='archive' method='get'> - <button>archive</button> - </form>" - elsif dir == "archive" - html << "<form action='/' method='get'> - <button>inbox</button> - </form>" - end - html << " </h1> " - threads.each do |item| - html << item_html(item,dir) - end - html << " - <form action='delete_all' method='post'> - <button>Delete all</button> - </form> - </body> - </html>" if dir == "inbox" - html end - def item_html item, dir, indent=2 - html = " - <div style='margin-left:#{indent}em' id='#{item[:nr]}'> - <b><a href='#{ item[:actor_url] }', target='_blank'>#{ item[:mention] }</a></b> - <form action='#{ File.join item[:follow], item[:mention] }' method='post'> - <button>#{ item[:follow].capitalize }</button> - </form> - - " - case dir - when "inbox" - html << " - <form action='/archive' method='post'> - <input type='hidden' name='file' id='file' value='#{item[:file]}' /> - <input type='hidden' name='anchor' id='anchor' value='#{item[:nr]}' /> - <button>Archive</button> - </form> - " - when "archive" - html << " - <form action='/delete' method='post'> - <input type='hidden' name='file' id='file' value='#{item[:file]}' /> - <input type='hidden' name='anchor' id='anchor' value='#{item[:nr]}' /> - <button>Delete</button> - </form> - " - end - - html << " - #{ item[:content].gsub('<br />','') }" - if item[:attachment] - item[:attachment].each do |att| - html << "<br>" - case att['mediaType'] - when /audio/ - html << "<audio controls=''><source src='#{ att['url'] }' type='#{ att['mediaType'] }'></audio>" - when /image/ - html << "<a href='#{ att['url'] }'><img src='#{ att['url'] }'></a>" - when /video/ - html << "<video controls=''><source src='#{ att['url'] }' type='#{ att['mediaType'] }'></video>" - else - html << "#{ att }<br> - <a href='#{ att['url'] }'>#{ att['url'] }</a>" - end - end - end - html << " - </div>" - item[:replies].each do |r| - html << item_html(r,dir,indent+4) - end - - html + def html item + @item = item + erb :item end def delete object @@ -420,11 +352,9 @@ helpers do end def fetch url, accept = 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' + p url response = `/run/current-system/sw/bin/curl --fail-with-body -sSL -H 'Accept: #{accept}' #{url}` - unless $?.success? - p url - halt 400 - end + halt 400 unless $?.success? JSON.parse(response) end |