From eea88e8022d3fd3c46401e1a297ebd7318c3c7b1 Mon Sep 17 00:00:00 2001 From: pdp8 Date: Thu, 14 Sep 2023 14:02:15 +0200 Subject: send/digest fixed --- clean-inbox | 2 +- create.rb | 15 +++--------- helpers.rb | 72 +++----------------------------------------------------- public/style.css | 46 ++++-------------------------------- server.rb | 32 ++++++++++++++++++------- watch | 2 +- 6 files changed, 37 insertions(+), 132 deletions(-) diff --git a/clean-inbox b/clean-inbox index 9429962..0c2e560 100755 --- a/clean-inbox +++ b/clean-inbox @@ -1,3 +1,3 @@ #!/usr/bin/env nu -[create announce undo delete like update move add] | each { |a| glob $'/srv/social/inbox/($a)/*.json' | each {|f| rm $f } } +[create announce undo delete like update move add remove] | each { |a| glob $'/srv/social/inbox/($a)/*.json' | each {|f| rm $f } } diff --git a/create.rb b/create.rb index 9241d2b..8e221c8 100644 --- a/create.rb +++ b/create.rb @@ -38,16 +38,10 @@ post '/create' do 'mediaType' => media_type(url), 'url' => url } - when '' - content << '

' else # create links # single quotes in html invalidate digest, reason unknown - content << line.split(/\s+/).collect do |word| + line.split(/\s+/).each do |word| case word - when URL_REGEXP - "#{word}" - when URI::MailTo::EMAIL_REGEXP - "#{word}" when HASHTAG_REGEXP tag_url = File.join('https://social.pdp8.info', 'tags', word.sub('#', '')) tag << { @@ -55,19 +49,16 @@ post '/create' do 'href' => tag_url, 'name' => word } - "#{word}" - when MENTION_REGEXP + when MENTION_REGEXP actor = actor(word) tag << { 'type' => 'Mention', 'href' => actor, 'name' => word } - "#{word}" - else - word end end + content << line end end diff --git a/helpers.rb b/helpers.rb index 26bcd9d..adb0b16 100644 --- a/helpers.rb +++ b/helpers.rb @@ -55,7 +55,7 @@ helpers do end def send_activity(activity, activity_path) - to = [to] if to = activity['to'].is_a?(String) ? [activity['to']] : activity['to'] + to = activity['to'].is_a?(String) ? [activity['to']] : activity['to'] inboxes = [] to.uniq.each do |url| next if [ACTOR, 'https://www.w3.org/ns/activitystreams#Public'].include? url @@ -70,7 +70,7 @@ helpers do end sha256 = OpenSSL::Digest.new('SHA256') - digest = "SHA-256=#{sha256.base64digest(activity)}" + digest = "SHA-256=#{sha256.base64digest(File.read(activity_path))}" keypair = OpenSSL::PKey::RSA.new(File.read('private.pem')) inboxes.compact.uniq.each do |inbox| @@ -98,72 +98,6 @@ helpers do end end - # # add date and id, save - # def save_activity(activity, box) - # date = Time.now.utc.iso8601 - # activity['published'] ||= date # if box == OUTBOX - # basename = "#{activity['published']}_#{mention(activity['actor'])}.json" - # activity_rel_path = File.join(activity['type'].downcase, basename) - # activity_path = File.join(box[:dir], activity_rel_path) - # if box == OUTBOX - # # return unless activity['to'].include? 'https://www.w3.org/ns/activitystreams#Public' # save only public messages - # - # activity['id'] = File.join(box[:url], activity_rel_path) - # activity['object']['published'] = date unless activity['object'].is_a? String - # # save object - # save_object activity['object'], box if %w[Create Announce Update].include? activity['type'] - # end - # # save activity - # FileUtils.mkdir_p File.dirname(activity_path) - # File.open(activity_path, 'w+') { |f| f.puts activity.to_json } - # activity_path - # end - # - # def save_object(object, box) - # object = fetch(object) if object.is_a? String and object.match(/^http/) - # return unless object and object['type'] != 'Person' - # return if box == INBOX and object['id'] and File.readlines(VISITED, chomp: true).include? object['id'] - # - # object['@context'] = 'https://www.w3.org/ns/activitystreams' - # if object['attributedTo'] - # basename = "#{object['published']}_#{mention(object['attributedTo'])}.json" - # else - # basename = "#{object['published']}.json" - # jj object - # end - # object_rel_path = File.join 'object', object['type'].downcase, basename - # object['id'] ||= File.join box[:url], object_rel_path # if box == OUTBOX - # object_path = File.join box[:dir], object_rel_path - # FileUtils.mkdir_p File.dirname(object_path) - # File.open(object_path, 'w+') { |f| f.puts object.to_json } - # if box == OUTBOX and object['tag'] - # object['tag'].each do |tag| - # next unless tag['type'] == 'Hashtag' - # - # tag_path = File.join(TAGS[:dir], tag['name'].sub('#', '')) + '.json' - # tag_collection = if File.exist? tag_path - # JSON.load_file(tag_path) - # else - # { - # '@context' => 'https://www.w3.org/ns/activitystreams', - # 'id' => tag['href'], - # 'type' => 'OrderedCollection', - # 'totalItems' => 0, - # 'orderedItems' => [] - # } - # end - # tag_collection['orderedItems'] << object['id'] - # tag_collection['totalItems'] = tag_collection['orderedItems'].size - # File.open(tag_path, 'w+') do |f| - # f.puts tag_collection.to_json - # end - # end - # elsif box == INBOX - # File.open(File.join(INBOX[:dir], 'visited'), 'a+') { |f| f.puts object['id'] } - # end - # object - # end - def update_collection(path, objects, action = 'add') objects = [objects] unless objects.is_a? Array File.open(path, 'r+') do |f| @@ -209,7 +143,7 @@ helpers do if $CHILD_STATUS.success? response else - p url, response + p 'curl error:', url, response nil end end diff --git a/public/style.css b/public/style.css index affde4c..bef69c9 100644 --- a/public/style.css +++ b/public/style.css @@ -1,15 +1,17 @@ body { + margin: 1em; + padding: 1em; font-family: sans-serif; - font-size: 3vmin; color: white; background-color: black; } -div { +div.pdp8 { margin: 1em; padding: 1em; - border: 1px solid #333; + border: 2px solid #333; border-radius: 1em; + } img, @@ -17,22 +19,6 @@ video { max-width: 100%; max-height: 80vh; display: block; - margin-left: auto; - margin-right: auto; -} - -form { - display: inline; -} - -button { - font-size: 1.5em; - background-color: #BBB; - border-radius: 0.5em; -} - -h1 button { - font-size: 0.5em; } a { @@ -43,26 +29,4 @@ textarea { width: 50vw; height: 50vh; display: block; -} - -@media (orientation: landscape) { - button { - font-size: 1em; - } -} - -@media (min-width: 1500px) { - body { - font-size: 1.25em; - } -} - -@media (orientation: portrait) and (min-width: 1000px) { - body { - font-size: 1.25em; - } - - button { - font-size: 1em; - } } \ No newline at end of file diff --git a/server.rb b/server.rb index 2bcb102..b0f3bd9 100644 --- a/server.rb +++ b/server.rb @@ -17,13 +17,16 @@ end # public get '/' do - redirect 'https://pdp8.info' + redirect 'https://social.pdp8.info/outbox' +end + +get '/outbox', provides: 'html' do + @activities = public_outbox + erb :outbox end get '/outbox' do - files = Dir[File.join('outbox', 'create', '*.json')] + Dir[File.join('outbox', 'announce', '*.json')] - activities = files.collect { |f| JSON.load_file(f) } - ids = activities.sort_by { |a| a['published'] }.collect { |a| a['id'] } + ids = public_outbox.collect { |a| a['id'] } { '@context' => 'https://www.w3.org/ns/activitystreams', 'id' => 'https://social.pdp8.info/outbox', 'type' => 'OrderedCollection', @@ -32,7 +35,7 @@ get '/outbox' do end get '/pdp8', provides: 'html' do - redirect 'https://pdp8.info' + redirect 'https://social.pdp8.info/outbox' end get '/pdp8' do @@ -99,7 +102,7 @@ helpers do update_collection FOLLOWERS, @activity['object']['actor'], 'delete' when 'Create', 'Announce' file = find_file @activity['object']['object'] - FileUtils.rm(file) if file + FileUtils.rm(file) if file and File.exist? file else p "Cannot undo @activity['object']['type']" jj @activity @@ -109,13 +112,13 @@ helpers do def update file = find_file(@activity['object']['id']) - FileUtils.rm(file) if file + FileUtils.rm(file) if file and File.exist? file create end def delete file = find_file(@activity['object']['id']) - FileUtils.rm(file) if file + FileUtils.rm(file) if file and File.exist? file end def move @@ -151,6 +154,19 @@ helpers do File.open(File.join(INBOX[:dir], 'visited'), 'a+') { |f| f.puts @object['id'] } end + def public_outbox + # files = Dir[File.join('outbox', 'create', '*.json')] + Dir[File.join('outbox', 'announce', '*.json')] + create = Dir[File.join('outbox', 'create', '*.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 + announce = Dir[File.join('outbox', 'announce', '*.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 + # activities = files.collect { |f| JSON.load_file(f) } + # activities.select { |a| a['to'].include?('https://www.w3.org/ns/activitystreams#Public') }.sort_by { |a| a['published'] } + create + announce + end + # https://github.com/mastodon/mastodon/blob/main/app/controllers/concerns/signature_verification.rb def verify! # digest diff --git a/watch b/watch index 4c020a9..31ac8fd 100755 --- a/watch +++ b/watch @@ -1,6 +1,6 @@ #!/bin/sh while inotifywait -qq -r ./ -e create,delete,modify; do - update + ./update #rsync -a --exclude='.git/' --exclude='watch' --exclude='generate-digest.rb' --exclude='.gitignore' --exclude='TODO' --filter=":- .gitignore" ./ /srv/social/ sudo systemctl restart social.service done -- cgit v1.2.3