diff options
-rw-r--r-- | client.rb | 42 | ||||
-rw-r--r-- | create.rb | 2 | ||||
-rw-r--r-- | helpers.rb | 35 | ||||
-rw-r--r-- | server.rb | 7 |
4 files changed, 59 insertions, 27 deletions
@@ -10,29 +10,45 @@ post '/delete' do protected! params['id'].each do |id| file = find_file id + halt 404 unless file # 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 + %w[inbox outbox].each do |box| + Dir[File.join box, 'announce', '*.json'].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 + outbox 'Undo', announce, announce['to'] + FileUtils.rm(announce_file) + end + Dir[File.join box, 'create', '*.json'].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) + object = JSON.load_file(file) + outbox 'Delete', object, object['to'] + FileUtils.rm(create_file) + end end - # end FileUtils.rm(file) if File.exist? file end 200 end +post '/undo' do # TODO: generalize for announce + protected! + Dir[File.join('outbox', '*', '*.json')].each do |f| + activity = JSON.load_file(f) + next unless activity['id'] == params['id'] + + object_file = find_file activity['object']['id'] + outbox 'Undo', params['id'], activity['to'] + FileUtils.rm(object_file) + FileUtils.rm(f) + end + 200 +end + post '/follow' do protected! params['id'] = actor params['mention'] if params['mention'] @@ -85,6 +85,8 @@ post '/create' do # TODO object['attachment'] = attachment unless attachment.empty? object['tag'] = tag unless tag.empty? + p 'outbox' + jj object outbox 'Create', object, to 200 @@ -1,7 +1,4 @@ -# frozen_string_literal: true - require 'English' - helpers do # add date and id, save def save_activity(activity, box) @@ -15,9 +12,9 @@ helpers do 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 object - save_object activity['object'], box if %w[Create Announce Update].include? activity['type'] # save activity FileUtils.mkdir_p File.dirname(activity_path) File.open(activity_path, 'w+') { |f| f.puts activity.to_json } @@ -26,15 +23,18 @@ helpers do 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 unless object and object['type'] != 'Person' + # File.open(File.join(INBOX[:dir]), 'visited', 'w+').open { |f| f.puts object['id'] } + return if box == INBOX and object['id'] and File.readlines(File.join(INBOX[:dir], 'visited'), + chomp: true).include? object['id'] - unless object['attributedTo'] + 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 - 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 object['id'] ||= File.join box[:url], object_rel_path # if box == OUTBOX object_path = File.join box[:dir], object_rel_path @@ -62,6 +62,8 @@ helpers do 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 @@ -159,7 +161,7 @@ helpers do def media_type(url) # TODO: extend extensions extensions = { - image: %w[jpeg jpg png tiff], + image: %w[jpeg jpg png tiff webp], audio: %w[flac wav mp3 ogg aiff], video: %w[mp4 webm] } @@ -173,4 +175,13 @@ helpers do JSON.load_file(f)['id'] == id end end + + def find_id(id, return_filename = true) + Dir[File.join('**', '*.json')].find do |f| + content = JSON.load_file(f) + if content['id'] == id + return_filename ? f : content + end + end + end end @@ -64,16 +64,19 @@ end helpers do def create + @count ||= 0 @object ||= @activity['object'] + @object = if @object['type'] == 'Like' # lemmy likes save_object @object['object'], INBOX else save_object @object, INBOX end - return unless @object and @object['inReplyTo'] + return unless @object and @object['inReplyTo'] and @count < 5 # recursive thread download @object = @object['inReplyTo'] + @count += 1 create end @@ -100,7 +103,7 @@ helpers do when 'Follow' update_collection FOLLOWERS, @activity['object']['actor'], true when 'Create', 'Announce' - file = find_file @activity['object']['id'] + file = find_file @activity['object']['object'] FileUtils.rm(file) if file else halt 501 |