diff options
-rw-r--r-- | activitypub.rb | 138 | ||||
-rw-r--r-- | pdp8.json (renamed from public/pdp8) | 0 | ||||
-rw-r--r-- | views/index.erb | 21 | ||||
-rw-r--r-- | views/item.erb | 39 |
4 files changed, 94 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 diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 0000000..269c495 --- /dev/null +++ b/views/index.erb @@ -0,0 +1,21 @@ +<!DOCTYPE html> +<html lang='en'> + <head> + <link rel='stylesheet' type='text/css' href='/style.css'> + </head> + <body> + <h1><%= @dir %> + <form action='<%= @alt_dir %>' method='get'> + <button><%= @alt_name %></button> + </form> + </h1> + <% @threads.each do |item| %> + <%= html item %> + <% end %> + <% if @dir == 'inbox' %> + <form action='delete_all' method='post'> + <button>Delete all</button> + </form> + <% end %> + </body> +</html> diff --git a/views/item.erb b/views/item.erb new file mode 100644 index 0000000..aafc4cc --- /dev/null +++ b/views/item.erb @@ -0,0 +1,39 @@ +<div style='margin-left:<%= @item[: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> + + <form action='/delete' method='post'> + <input type='hidden' name='file' id='file' value='<%= @item[:file] %>' /> + <input type='hidden' name='redirect' id='redirect' value='/<%= @dir.sub('inbox','') %>#<%= @item[:nr] %>' /> + <button>Delete</button> + </form> + <% if @dir == 'inbox' %> + + <form action='/archive' method='post'> + <input type='hidden' name='file' id='file' value='<%= @item[:file] %>' /> + <input type='hidden' name='redirect' id='redirect' value='/<%= @dir.sub('inbox','') %>#<%= @item[:nr] %>' /> + <button>Archive</button> + </form> + <% end %> + <%= @item[:content] %> + <% if @item[:attachment] + @item[:attachment].each do |att| + case att['mediaType'] + when /audio/ %> + <br><audio controls=''><source src='<%= att['url'] %>' type='<%= att['mediaType'] %>'></audio> + <% when /image/ %> + <br><a href='<%= att['url'] %>'><img src='<%= att['url'] %>'></a> + <% when /video/ %> + <br><video controls=''><source src='<%= att['url'] %>' type='<%= att['mediaType'] %>'></video> + <% else %> + <%= att %><br> + <a href='<%= att['url'] %>'><%= att['url'] %></a> + <% end %> + <% end %> + <% end %> +</div> +<% @item[:replies].each do |reply| %> + <%= html reply %> +<% end %> |