From 7f3023df5be705436544b5724a6b83c2717aa0b8 Mon Sep 17 00:00:00 2001 From: pdp8 Date: Sat, 10 Aug 2024 00:29:02 +0200 Subject: improved outbox html, background sending --- client.rb | 5 ++--- helpers.rb | 50 ++++++++++++++++++-------------------------------- server.rb | 4 ++-- 3 files changed, 22 insertions(+), 37 deletions(-) diff --git a/client.rb b/client.rb index c47000d..4f1ee51 100644 --- a/client.rb +++ b/client.rb @@ -38,12 +38,11 @@ post '/undo' do # TODO: generalize for announce activity = JSON.load_file(activity_file) next unless activity['id'] == params['id'] - object_file = File.join(dir, activity['object']['id'].sub('https://', '')) - # object_file, object = find_object activity['object']['id'] create_activity 'Undo', params['id'], activity['to'] FileUtils.rm(activity_file) - FileUtils.rm(object_file) if activity_file.match 'create' + object_file = File.join(SOCIAL_DIR, activity['object']['id'].sub('https://', '')) + FileUtils.rm(object_file) outbox_html('create') elsif activity_file.match 'announce' outbox_html('announce') diff --git a/helpers.rb b/helpers.rb index 72666b1..ad0bd42 100644 --- a/helpers.rb +++ b/helpers.rb @@ -1,3 +1,4 @@ +require_relative '/home/ch/src/wgen/lib' require 'English' helpers do def save_inbox(item) @@ -61,7 +62,11 @@ helpers do end end save_outbox activity - send_activity activity, File.join(OUTBOX_DIR, rel_path) + pid = fork do + settings.running_server = nil # http://stackoverflow.com/a/38934866/1021515 + send_activity activity, File.join(OUTBOX_DIR, rel_path) + end + Process.detach pid end def send_activity(activity, activity_path) @@ -90,8 +95,7 @@ helpers do signature = Base64.strict_encode64(keypair.sign(OpenSSL::Digest.new('SHA256'), string)) signed_header = "keyId=\"#{ACTOR}#main-key\",algorithm=\"rsa-sha256\",headers=\"(request-target) host date digest content-type\",signature=\"#{signature}\"" - # Net::HTTP fails with OpenSSL error - curl( + curl( # Net::HTTP fails with OpenSSL error "-X POST -H 'Host: #{uri.host}' -H 'Date: #{httpdate}' -H 'Digest: #{digest}' -H 'Signature: #{signed_header}' --data-binary '@#{activity_path}'", inbox ) end @@ -220,63 +224,45 @@ helpers do end def outbox_html(activity) - html = File.read('/home/ch/src/publish/html/head.html') - html += '

@pdp8@social.pdp8.info

" + html = File.read(File.join(SNIPPETS, 'head.html')) + html += nav 'social' + html += "

@pdp8@social.pdp8.info

" html += if activity == 'create' "posts | boosts" elsif activity == 'announce' "posts | boosts" end - html += '

' + html += '' Dir[File.join(SOCIAL_DIR, 'outbox', activity, '*.json')].collect do |f| JSON.load_file(f) end.select { |a| a['to'].include?('https://www.w3.org/ns/activitystreams#Public') }.sort_by { |a| a['published'] }.reverse.collect { |a| a['object'] }.each do |object| object = fetch(object) if object.is_a? String if object mention = mention object['attributedTo'] - html += "
" + html += '
' + html += "

#{object['published']}

" if activity == 'announce' html += "#{mention} " end - html += "#{object['published']} - #{object['content']}" + html += object['content'] if object['attachment'] object['attachment'].each do |att| - w = 1024 - h = 768 case att['mediaType'] when /audio/ html += "

" when /image/ - if activity == 'create' - w, h = `/etc/profiles/per-user/ch/bin/identify -format "%w %h" #{att['url'].sub( - 'https://media.pdp8.info', '/srv/media' - )}`.chomp.split(' ') end - html += "