From 4ca1c5555f18703d1dac673b1a77d1c919fb4787 Mon Sep 17 00:00:00 2001 From: pdp8 Date: Mon, 17 Jul 2023 19:09:02 +0200 Subject: write to json instead of direcories --- server.rb | 121 ++++++++++++++++++++------------------------------------------ 1 file changed, 39 insertions(+), 82 deletions(-) (limited to 'server.rb') diff --git a/server.rb b/server.rb index 3263d9d..1093566 100644 --- a/server.rb +++ b/server.rb @@ -14,43 +14,61 @@ post '/inbox' do type = @activity['type'].downcase.to_sym p type halt 501 unless respond_to?(type) - # jj @activity @object = @activity['object'] @object = fetch(@object) if @object.is_a?(String) && @object.match(/^http/) halt 400 unless @object - # verify! unless type == :accept # pixelfed sends unsigned accept activities + verify! unless type == :accept # pixelfed sends unsigned accept activities??? send(type) + halt 200 end # public get '/.well-known/webfinger' do - if request['resource'] == "acct:#{MENTION}" - send_file('./public/webfinger', - type: 'application/jrd+json') - else - halt 404 - end -end - -get '/outbox' do - ordered_collection(OUTBOX_DIR).to_json + halt 404 unless request['resource'] == "acct:#{MENTION}" + send_file(WEBFINGER, type: 'application/jrd+json') end -['/following', '/followers'].each do |path| +['/pdp8', '/following', '/followers', '/outbox', '/shared'].each do |path| get path do - ordered_collection(File.join(PUBLIC_DIR, path)).to_json + send_file(File.join(PUBLIC_DIR, path) + '.json', type: CONTENT_TYPE) end end -get '/pdp8' do - send_file('./public/pdp8') -end +helpers do + def create + return unless @object -get '/tags/:tag' do |tag| - ordered_collection(File.join(TAGS, tag)).to_json -end + return if File.readlines(VISITED).collect { |l| l.chomp }.include? @object['id'] + + File.open(VISITED, 'a+') { |f| f.puts @object['id'] } + update_collection INBOX, @object + return unless @object['inReplyTo'] + + @object = fetch @object['inReplyTo'] + create if @object + end + + def announce + create + end + + def follow + update_collection FOLLOWERS, @activity['actor'] + outbox 'Accept', @activity, [@activity['actor']] + end + + def accept + return unless @object['type'] == 'Follow' + + update_collection FOLLOWING, @object['object'] + end + + def undo + return unless @object['type'] == 'Follow' + + update_collection FOLLOWERS, @object['actor'], true + end -helpers do # https://github.com/mastodon/mastodon/blob/main/app/controllers/concerns/signature_verification.rb def verify! # digest @@ -86,65 +104,4 @@ helpers do halt 403 unless key.verify(OpenSSL::Digest.new('SHA256'), signature, comparison) end - - def create - return unless @object - return if object_exists? - - File.open(object_file, 'w+') { |f| f.puts @object.to_json } - return unless @object['inReplyTo'] - - @object = fetch @object['inReplyTo'] - create if @object - end - - def announce - create - end - - def follow - File.open(File.join(FOLLOWERS, "#{mention(@activity['actor'])}.json"), 'w+') { |f| f.puts @body } - outbox 'Accept', @activity, [@activity['actor']] - end - - def accept - return unless @object['type'] == 'Follow' - - File.open(File.join(FOLLOWING_DIR, "#{mention(@object['object'])}.json"), 'w+') { |f| f.puts @object.to_json } - end - - def undo - return unless @object['type'] == 'Follow' - - Dir[File.join(FOLLOWERS, '*.json')].each do |follower| - FileUtils.rm follower if JSON.parse(File.read(follower))['actor'] == @object['actor'] - end - end - - def inbox - Dir[File.join(INBOX_DIR, 'note', '*.json')].collect do |file| - JSON.parse(File.read(file)) - end.sort_by { |o| o['published'] } - end - - def object_exists? - !inbox.select { |o| o['id'] == @object['id'] }.empty? - end - - def object_file - dir = File.join 'inbox', @object['type'].downcase - FileUtils.mkdir_p dir - File.join dir, "#{Time.now.strftime('%Y-%m-%dT%H:%M:%S.%N')}.json" - end - - def ordered_collection(dir) - posts = Dir[File.join(dir, '*.json')].collect { |f| JSON.parse(File.read(f)) }.sort_by { |o| o['published'] } - { - '@context' => 'https://www.w3.org/ns/activitystreams', - 'summary' => "#{USER} #{dir}", - 'type' => 'OrderedCollection', - 'totalItems' => posts.size, - 'orderedItems' => posts - } - end end -- cgit v1.2.3