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 ++++++++++++++------------------------------------------ pdp8.json | 39 ++++++++++++++++ public/pdp8 | 39 ---------------- views/index.erb | 21 +++++++++ views/item.erb | 39 ++++++++++++++++ 5 files changed, 133 insertions(+), 143 deletions(-) create mode 100644 pdp8.json delete mode 100644 public/pdp8 create mode 100644 views/index.erb create mode 100644 views/item.erb 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 diff --git a/pdp8.json b/pdp8.json new file mode 100644 index 0000000..fafc4e0 --- /dev/null +++ b/pdp8.json @@ -0,0 +1,39 @@ +{ + "@context": [ + "https://www.w3.org/ns/activitystreams" + ], + "id": "https://social.pdp8.info/pdp8", + "type": "Person", + "preferredUsername": "pdp8", + "name": "pdp8", + "inbox": "https://social.pdp8.info/inbox", + "outbox": "https://social.pdp8.info/outbox", + "following": "https://social.pdp8.info/following", + "followers": "https://social.pdp8.info/followers", + "icon": { + "type": "Image", + "url": "https://pdp8.info/pdp8.png" + }, + "attachment": [ + { + "type": "PropertyValue", + "name": "Web", + "value": "pdp8.info" + }, + { + "type": "PropertyValue", + "name": "Fediverse", + "value": "@pdp8@social.pdp8.info" + }, + { + "type": "PropertyValue", + "name": "Matrix", + "value": "@pdp8:matrix.pdp8.info" + } + ], + "publicKey": { + "id": "https://social.pdp8.info/pdp8#main-key", + "owner": "https://social.pdp8.info/pdp8", + "publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArDawzSl+XcJ+96sIrx+E\nsDoUQzSvoKazCgw7qOMaOGi7XxJ8riBvdRBlJ4zOEfQaxcaQgGn5JntOofqkeWvk\nIykOAzYfwY6HoUm7i1eZME2quO+CkMMq9SX9/DOqggOYtiVC9DX5FxXe5YHK7Q/n\nbo1iB6rgVS43wT0PnI6uduY4cUlvhRkX4Iht0N1GTrBlGKloRQ96KTzp+U9xF7bp\nKO87Y4yftv+d6L3ZZBfTRgWOtDXG8E4Vdvsq0aPQNBtazq0fwtBbk2G4mZtCMqyT\nvLZh8w+YPn1ICoQsKukU/q7eG29UJCz/QdZndkuv5iIm+H/c8gicGllw9rNQP2G0\nBQIDAQAB\n-----END PUBLIC KEY-----\n" + } +} diff --git a/public/pdp8 b/public/pdp8 deleted file mode 100644 index fafc4e0..0000000 --- a/public/pdp8 +++ /dev/null @@ -1,39 +0,0 @@ -{ - "@context": [ - "https://www.w3.org/ns/activitystreams" - ], - "id": "https://social.pdp8.info/pdp8", - "type": "Person", - "preferredUsername": "pdp8", - "name": "pdp8", - "inbox": "https://social.pdp8.info/inbox", - "outbox": "https://social.pdp8.info/outbox", - "following": "https://social.pdp8.info/following", - "followers": "https://social.pdp8.info/followers", - "icon": { - "type": "Image", - "url": "https://pdp8.info/pdp8.png" - }, - "attachment": [ - { - "type": "PropertyValue", - "name": "Web", - "value": "pdp8.info" - }, - { - "type": "PropertyValue", - "name": "Fediverse", - "value": "@pdp8@social.pdp8.info" - }, - { - "type": "PropertyValue", - "name": "Matrix", - "value": "@pdp8:matrix.pdp8.info" - } - ], - "publicKey": { - "id": "https://social.pdp8.info/pdp8#main-key", - "owner": "https://social.pdp8.info/pdp8", - "publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArDawzSl+XcJ+96sIrx+E\nsDoUQzSvoKazCgw7qOMaOGi7XxJ8riBvdRBlJ4zOEfQaxcaQgGn5JntOofqkeWvk\nIykOAzYfwY6HoUm7i1eZME2quO+CkMMq9SX9/DOqggOYtiVC9DX5FxXe5YHK7Q/n\nbo1iB6rgVS43wT0PnI6uduY4cUlvhRkX4Iht0N1GTrBlGKloRQ96KTzp+U9xF7bp\nKO87Y4yftv+d6L3ZZBfTRgWOtDXG8E4Vdvsq0aPQNBtazq0fwtBbk2G4mZtCMqyT\nvLZh8w+YPn1ICoQsKukU/q7eG29UJCz/QdZndkuv5iIm+H/c8gicGllw9rNQP2G0\nBQIDAQAB\n-----END PUBLIC KEY-----\n" - } -} 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 @@ + + + + + + +

<%= @dir %> +
+ +
+

+ <% @threads.each do |item| %> + <%= html item %> + <% end %> + <% if @dir == 'inbox' %> +
+ +
+ <% end %> + + 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 @@ +
+ <%= @item[:mention] %>  +
+ +
+   +
+ + #<%= @item[:nr] %>' /> + +
+ <% if @dir == 'inbox' %> +   +
+ + #<%= @item[:nr] %>' /> + +
+ <% end %> + <%= @item[:content] %> + <% if @item[:attachment] + @item[:attachment].each do |att| + case att['mediaType'] + when /audio/ %> +
+ <% when /image/ %> +
'>'> + <% when /video/ %> +
+ <% else %> + <%= att %>
+ '><%= att['url'] %> + <% end %> + <% end %> + <% end %> +
+<% @item[:replies].each do |reply| %> + <%= html reply %> +<% end %> -- cgit v1.2.3