summary refs log tree commit diff
diff options
context:
space:
mode:
authorpdp8 <pdp8@pdp8.info>2024-08-10 00:29:02 +0200
committerpdp8 <pdp8@pdp8.info>2024-08-10 00:29:02 +0200
commit7f3023df5be705436544b5724a6b83c2717aa0b8 (patch)
tree99687362f33cdf6c8f83f2cdd2d20875d8df0480
parent060492722d6137225a9e00add6c7f150d014927d (diff)
improved outbox html, background sending
-rw-r--r--client.rb5
-rw-r--r--helpers.rb50
-rw-r--r--server.rb4
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 += '<nav>'
-    html += "<a id='logo' href='/about.html'><img src='/pdp8.png' alt='pdp8'></a>"
-    %w[music pictures videos climbing code contact].each do |c|
-      html += "&nbsp;<a class='item' href='/#{c}.html'>#{c}</a>"
-    end
-    html += "&nbsp;<a class='item current' href='/social/create.html'>social</a>"
-    html += "&nbsp;<a class='item' href='/rss.xml'>rss</a>"
-    html += "&nbsp;<a id='menu' href='#' onclick='show_vertical_menu()'>&equiv;</a>"
-    html += "</nav><div class='post'><h1><a href='https://social.pdp8.info/pdp8'>@pdp8@social.pdp8.info</a></h1><h2>"
+    html = File.read(File.join(SNIPPETS, 'head.html'))
+    html += nav 'social'
+    html += "<h1><a href='https://social.pdp8.info/pdp8'>@pdp8@social.pdp8.info</a></h1><h2>"
     html += if activity == 'create'
               "posts&nbsp;|&nbsp;<a href='/social/announce.html'>boosts</a>"
             elsif activity == 'announce'
               "<a href='/social/create.html'>posts</a>&nbsp;|&nbsp;boosts"
             end
-    html += '</h2></div>'
+    html += '</h2>'
     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 += "<div class='post'>"
+        html += '<section>'
+        html += "<h3>#{object['published']}</h3>"
         if activity == 'announce'
           html += "<b><a href='#{object['attributedTo']}' target='_blank'>#{mention}</a></b>&nbsp;"
         end
-        html += "<em>#{object['published']}</em>
-            #{object['content']}"
+        html += object['content']
         if object['attachment']
           object['attachment'].each do |att|
-            w = 1024
-            h = 768
             case att['mediaType']
             when /audio/
               html += "<p><audio controls><source src='#{att['url']}' type='#{att['mediaType']}'></audio>"
             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 += "<p><a href='#{att['url']}'><img loading='lazy' width='#{w}' height='#{h}' "
+              html += "<p><a href='#{att['url']}'><img loading='lazy' "
               alt = att['name'] ? att['name'].gsub("'", '&apos;').gsub('"', '&quot;') : ''
               html += "alt='#{alt}' src='#{att['url']}'></a>"
             when /video/
-              if activity == 'create'
-                w, h = `ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=p=0 #{att['url'].sub(
-                  'https://media.pdp8.info', '/srv/media'
-                )}`.chomp.split(',')
-              end
-              html += "<p><video width='#{w}' height='#{h}' controls><source src='#{att['url']}' type='#{att['mediaType']}'></video>"
+              html += "<p><video controls><source src='#{att['url']}' type='#{att['mediaType']}'></video>"
             end
           end
         end
       end
-      html += '</div>'
+      html += '</section>'
     end
-    html += File.read('/home/ch/src/publish/html/tail.html')
+    html += File.read(File.join(SNIPPETS, 'tail.html'))
     %w[pdp8 pdp8-test].each do |d|
       outdir = "/srv/www/#{d}/social"
       out = File.join(outdir, activity + '.html')
diff --git a/server.rb b/server.rb
index d9983ef..ff1c73b 100644
--- a/server.rb
+++ b/server.rb
@@ -19,6 +19,7 @@ get '/' do
 end
 
 get '/outbox/:activity', provides: 'html' do
+  # outbox_html params['activity']
   redirect "https://pdp8.info/social/#{params['activity']}.html"
 end
 
@@ -137,8 +138,7 @@ helpers do
 
   def delete
     file = File.join(INBOX_DIR, @activity['object']['id'].sub('https://', ''))
-    # file, object = find_object(@activity['object']['id'])
-    FileUtils.rm(file) if file and File.exist? file # and @activity['actor'] == @activity['object']['attributedTo']
+    FileUtils.rm_f(file) if file
   end
 
   def move