From 8cb9bc7e4d516a27b4fe27ba0d0a9c40497e003d Mon Sep 17 00:00:00 2001 From: pdp8 Date: Mon, 14 Aug 2023 17:42:57 +0200 Subject: undo/delete activities for outbox posts, cleanup --- client.rb | 79 +++++++++++++++++++-------------------------------------------- 1 file changed, 24 insertions(+), 55 deletions(-) (limited to 'client.rb') diff --git a/client.rb b/client.rb index b44f9ba..13ba3a6 100644 --- a/client.rb +++ b/client.rb @@ -1,34 +1,8 @@ -# frozen_string_literal: true - # client-server - -['/inbox/threads', '/outbox/threads'].each do |path| +['/inbox/object', '/outbox/object'].each do |path| get path do protected! - box = path.sub('/', '').sub('/threads', '') - collection = Dir[File.join(box, 'object', '*', '*.json')].collect { |f| JSON.parse(File.read(f)) } - threads = [] - collection.collect! do |object| - object.is_a?(String) && object.match(/^http/) ? fetch(object) : object - end - collection.compact! - collection.each do |object| - object['mention'] = mention object['attributedTo'] - if object['type'] == 'Audio' - audio_url = object['url'].select { |url| url['mediaType'].match('audio') }[0] - object['attachment'] = [{ 'url' => audio_url['href'], 'mediaType' => audio_url['mediaType'] }] - end - object['replies'] = [] - threads << object if object['inReplyTo'].nil? || collection.select do |o| - o['id'] == object['inReplyTo'] - end.empty? - end - collection.each do |object| - collection.select { |o| o['id'] == object['inReplyTo'] }.each do |o| - o['replies'] << object - end - end - threads.sort_by { |o| o['published'] }.to_json + Dir[File.join(path.sub('/', ''), '*', '*.json')].collect { |f| JSON.load_file(f) }.to_json end end @@ -36,6 +10,24 @@ post '/delete' do protected! params['id'].each do |id| file = find_file id + if file.match(%r{outbox/}) # find/delete activity + + Dir[File.join 'outbox', 'announce', '*'].each do |announce_file| + announce = JSON.load_file(announce_file) + next unless announce['object']['id'] == id + + outbox 'Undo', announce, announce['to'] + FileUtils.rm(announce_file) + end + Dir[File.join 'outbox', 'create', '*'].each do |create_file| + create = JSON.load_file(create_file) + next unless create['object']['id'] == id + + object = JSON.load_file(file) + outbox 'Delete', object, object['to'] + FileUtils.rm(create_file) + end + end FileUtils.rm(file) if File.exist? file end 200 @@ -51,9 +43,8 @@ end post '/unfollow' do protected! params['id'] = actor params['mention'] if params['mention'] - following = Dir[File.join(OUTBOX[:dir], 'follow', '*.json')].collect { |f| JSON.parse(File.read(f)) } + following = Dir[File.join(OUTBOX[:dir], 'follow', '*.json')].collect { |f| JSON.load_file(f) } activity = following.find { |a| a['object'] == params['id'] } - # activity ||= save_activity({ 'type' => 'Follow', 'actor' => ACTOR, 'object' => params['id'] }, OUTBOX) # recreate activity for old/deleted follows outbox 'Undo', activity, [params['id']] update_collection FOLLOWING, params['id'], true 200 @@ -62,8 +53,9 @@ end post '/share' do # TODO protected! src = find_file params['id'] - object = JSON.parse(File.read(src)) - recipients = public + object = JSON.load_file(src) + recipients = ['https://www.w3.org/ns/activitystreams#Public'] + recipients += JSON.load_file(FOLLOWERS)['orderedItems'] recipients << object['attributedTo'] outbox 'Announce', object, recipients dest = src.sub('inbox/', 'outbox/') @@ -81,27 +73,4 @@ helpers do def protected! halt 403 unless session['client'] end - - def public - recipients = ['https://www.w3.org/ns/activitystreams#Public'] - recipients += JSON.parse(File.read(FOLLOWERS))['orderedItems'] - recipients.delete ACTOR - recipients.uniq - end - - def parse_follow(follow) - case follow - when /^#/ - actor = "https://relay.fedi.buzz/tag/#{follow.sub(/^#/, '')}" - mention = follow - when /^http/ - actor = follow - mention = mention(actor) - when /^@*\w+@\w+/ - mention = follow - actor = actor(follow) - return 502 unless actor - end - [actor, mention] - end end -- cgit v1.2.3