# client-server ['/inbox/object', '/outbox/object'].each do |path| get path do protected! Dir[File.join(path.sub('/', ''), '*', '*.json')].collect { |f| JSON.load_file(f) }.to_json end end post '/delete' do protected! params['id'].each do |id| file, object = find_object id halt 404 unless file and File.exist?(file) FileUtils.rm(file) end 200 end post '/follow' do protected! params['id'] = actor params['mention'] if params['mention'] create_activity 'Follow', params['id'], [params['id']] 200 end post '/unfollow' do protected! params['id'] = actor params['mention'] if params['mention'] following = Dir[File.join(OUTBOX[:dir], 'follow', '*.json')].collect { |f| JSON.load_file(f) } activity = following.find { |a| a['object'] == params['id'] } activity ||= { "@context": 'https://www.w3.org/ns/activitystreams', "type": 'Follow', "actor": 'https://social.pdp8.info/pdp8', "object": params['id'] } create_activity 'Undo', activity, [params['id']] update_collection FOLLOWING, params['id'], 'delete' 200 end post '/share' do # TODO protected! src = find_file params['id'] object = JSON.load_file(src) recipients = ['https://www.w3.org/ns/activitystreams#Public'] recipients += JSON.load_file(FOLLOWERS)['orderedItems'] recipients << object['attributedTo'] create_activity 'Announce', object, recipients dest = src.sub('inbox/', 'outbox/') FileUtils.mkdir_p File.dirname(dest) FileUtils.mv src, dest 200 end post '/undo' do # TODO: generalize for announce protected! Dir[File.join('outbox', '*', '*.json')].each do |activity_file| activity = JSON.load_file(activity_file) next unless activity['id'] == params['id'] object_file = find_file activity['object']['id'] create_activity 'Undo', params['id'], activity['to'] FileUtils.rm(activity_file) FileUtils.rm(object_file) end 200 end post '/login' do session['client'] = (OpenSSL::Digest::SHA256.base64digest(params['secret']) == File.read('.digest').chomp) 200 end helpers do def protected! halt 403 unless session['client'] end end