From 9781dda58c3d2926f928544a8155d493fc2f1ca2 Mon Sep 17 00:00:00 2001 From: pdp8 Date: Sun, 13 Aug 2023 10:31:26 +0200 Subject: activity/object saving, send activities from file --- helpers.rb | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'helpers.rb') diff --git a/helpers.rb b/helpers.rb index bb43303..21ff206 100644 --- a/helpers.rb +++ b/helpers.rb @@ -21,13 +21,18 @@ helpers do # save activity FileUtils.mkdir_p File.dirname(activity_path) File.open(activity_path, 'w+') { |f| f.puts activity.to_json } - activity + 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' + unless object['attributedTo'] + jj object + return + end + object['@context'] = 'https://www.w3.org/ns/activitystreams' basename = "#{object['published']}_#{mention(object['attributedTo'])}.json" object_rel_path = File.join 'object', object['type'].downcase, basename @@ -35,6 +40,29 @@ helpers do 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.parse(File.read(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 + end object end @@ -61,7 +89,13 @@ helpers do end def fetch(url, accept = 'application/activity+json') - uri = URI(url) + begin + uri = URI(url) + rescue StandardError => e + p url + p e + return nil + end httpdate = Time.now.utc.httpdate keypair = OpenSSL::PKey::RSA.new(File.read('private.pem')) string = "(request-target): get #{uri.request_uri}\nhost: #{uri.host}\ndate: #{httpdate}" @@ -74,11 +108,11 @@ helpers do end def curl(ext, url) - p url response = `/run/current-system/sw/bin/curl -H 'Content-Type: #{CONTENT_TYPE}' -H 'Accept: #{CONTENT_TYPE}' --fail-with-body -sSL #{ext} #{url}` if $CHILD_STATUS.success? response else + p url p response nil end -- cgit v1.2.3