# client-server 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 '/announce' do # TODO protected! to = ['https://www.w3.org/ns/activitystreams#Public', FOLLOWERS_URL] to << params['attributedTo'] create_activity 'Announce', params['id'], to outbox_html('announce') 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'] create_activity 'Undo', params['id'], activity['to'] FileUtils.rm(activity_file) if activity_file.match 'create' object_file = File.join(SOCIAL_DIR, activity['object']['id'].sub('https://', '')) FileUtils.rm(object_file) outbox_html('create') elsif activity_file.match 'announce' outbox_html('announce') end 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