From b837b19b1950c7bc14a38aa5ea917e91b6f081dd Mon Sep 17 00:00:00 2001 From: pdp8 Date: Wed, 21 Jun 2023 10:32:41 +0200 Subject: erb templates separated --- activitypub.rb | 138 ++++++++++++++------------------------------------------- 1 file changed, 34 insertions(+), 104 deletions(-) (limited to 'activitypub.rb') 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=" - - - - - -

#{dir}" - - if dir == "inbox" - html << "
- -
" - elsif dir == "archive" - html << "
- -
" - end - html << "

" - threads.each do |item| - html << item_html(item,dir) - end - html << " -
- -
- - " if dir == "inbox" - html end - def item_html item, dir, indent=2 - html = " -
- #{ item[:mention] }  -
- -
-   - " - case dir - when "inbox" - html << " -
- - - -
- " - when "archive" - html << " -
- - - -
- " - end - - html << " - #{ item[:content].gsub('
','') }" - if item[:attachment] - item[:attachment].each do |att| - html << "
" - case att['mediaType'] - when /audio/ - html << "" - when /image/ - html << "" - when /video/ - html << "" - else - html << "#{ att }
- #{ att['url'] }" - end - end - end - html << " -
" - 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 -- cgit v1.2.3