diff options
author | pdp8 <pdp8@pdp8.info> | 2023-07-24 02:46:05 +0200 |
---|---|---|
committer | pdp8 <pdp8@pdp8.info> | 2023-07-24 02:46:05 +0200 |
commit | a509c55faca368709044133199f71fb862b1e605 (patch) | |
tree | 9c53d06a4f26e6ccf8635278b8eb56c4a62403f3 /client.rb | |
parent | 3c38f81b8a145778d4329c6be4c91baa00ca0d48 (diff) |
html output removed
Diffstat (limited to 'client.rb')
-rw-r--r-- | client.rb | 160 |
1 files changed, 38 insertions, 122 deletions
@@ -1,162 +1,77 @@ # frozen_string_literal: true # client-server -post '/' do # TODO - protected! - - recipients = public - recipients << params[:to] - - content = [] - attachment = [] - tag = [] - extensions = { - image: %w[jpeg png], - audio: %w[flac wav mp3 ogg], - video: %w[mp4 webm] - } - params[:content].lines.each do |line| - line.chomp! - if line.match(/^http/) - ext = File.extname(line).sub('.', '') - media_type = extensions.select { |_k, v| v.include? ext }.keys[0].to_s + '/' + ext - attachment << { - 'type' => 'Document', - 'mediaType' => media_type, - 'url' => line - } - else - tags = line.split(/\s+/).grep(/^#\w+$/) - tags.each do |name| - # href = File.join(TAGS_URL, name.sub('#', '')) - tag_url = File.join(TAGS_URL, name.sub('#', '')) - tag << { - 'type' => 'Hashtag', - 'href' => tag_url, - 'name' => name - } - end - mentions = line.split(/\s+/).grep(/^@\w+@\S+$/) - mentions.each do |mention| - actor = actor(mention) - tag << { - 'type' => 'Mention', - 'href' => actor, - 'name' => mention - } +['/inbox', '/outbox'].each do |path| + get path do + protected! + box = path.sub('/', '') + 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 - content << line + object['replies'] = [] + threads << object if object['inReplyTo'].nil? || collection.select do |o| + o['id'] == object['inReplyTo'] + end.empty? end - end - - object = { - 'type' => 'Note', - 'attributedTo' => ACTOR, - 'inReplyTo' => params[:inReplyTo], - 'content' => "<p>\n#{content.join("\n<br>")}\n</p>", - 'attachment' => attachment, - 'tag' => tag, - 'to' => recipients - } - - activity = outbox 'Create', object, recipients, true - activity['object']['tag'].each do |tag| - next unless tag['type'] == 'Hashtag' - - tag_path = File.join(TAGS_DIR, tag['name'].sub('#', '')) + '.json' - unless File.exist? tag_path - File.open(tag_path, 'w+') do |f| - tag_collection = { - '@context' => 'https://www.w3.org/ns/activitystreams', - 'id' => tag['href'], - 'type' => 'OrderedCollection', - 'totalItems' => 0, - 'orderedItems' => [] - } - f.puts tag_collection.to_json + collection.each do |object| + collection.select { |o| o['id'] == object['inReplyTo'] }.each do |o| + o['replies'] << object end end - update_collection tag_path, activity['object']['id'] + threads.sort_by { |o| o['published'] }.to_json end - redirect(params['anchor'] || '/inbox') end post '/delete' do protected! - collection = Kernel.const_get(params['dir'].upcase) - if params['id'] - update_collection collection, params, true - else - update_collection collection, JSON.parse(File.read(collection))['orderedItems'], true + params['id'].each do |id| + file = find_file id + FileUtils.rm(file) if File.exist? file end - redirect(params['anchor'] || '/inbox') end post '/follow' do protected! actor, = parse_follow params['follow'] outbox 'Follow', actor, [actor] - redirect(params['anchor'] || '/inbox') end post '/unfollow' do protected! actor, mention = parse_follow params['follow'] - Dir[File.join(OUTBOX_DIR, 'follow', '*.json')].each do |f| + Dir[File.join(OUTBOX[:dir], 'follow', '*.json')].each do |f| activity = JSON.parse(File.read(f)) if activity['object'] == actor outbox 'Undo', activity, [actor] update_collection FOLLOWING, actor, true end end - redirect(params['anchor'] || '/inbox') end post '/share' do # TODO protected! - # inbox = JSON.parse File.read(INBOX) - # object = inbox['orderedItems'].find { |i| i['id'] == params['id'] } - # update_collection SHARED, object - # update_collection INBOX, object, true + src = find_file INBOX, params['id'] + object = JSON.parse(File.read(src)) recipients = public recipients << object['attributedTo'] - outbox 'Announce', params['id'], recipients - redirect(params['anchor'] || '/inbox') + # outbox 'Announce', object, recipients + dest = src.sub('/inbox/', '/outbox/') + FileUtils.mkdir_p File.dirname(dest) + FileUtils.mv src, dest end post '/login' do session['client'] = (OpenSSL::Digest::SHA256.base64digest(params['secret']) == File.read('.digest').chomp) - redirect '/inbox' -end - -get '/' do - protected! - redirect '/inbox' -end - -['/inbox', '/outbox'].each do |path| - get path, provides: 'html' do - protected! - @box = path.sub('/', '') - collection = Dir[File.join(@box, 'object', '*', '*.json')].collect { |f| JSON.parse(File.read(f)) } - @threads = [] - collection.collect! do |object| - object = fetch(object) if object.is_a?(String) && object.match(/^http/) - object - end - collection.each do |object| - object['replies'] = [] - @threads << object if object['inReplyTo'].nil? || collection.select { |o| o['id'] == object['inReplyTo'] }.empty? - end - collection.each do |object| - collection.select { |o| o['id'] == object['inReplyTo'] }.each do |o| - o['replies'] << object - end - end - @threads.sort_by! { |t| t['published'] } - erb :collection - end end helpers do @@ -164,14 +79,15 @@ helpers do halt 403 unless session['client'] end - def selection(params) - selection = Dir[File.join(SOCIAL_DIR, params['dir'], '*', '*.json')] - params['id'] ? selection.select { |f| JSON.parse(File.read(f))['id'] == params['id'] } : selection + def find_file(id) + Dir[File.join('*', 'object', '*', '*.json')].select do |f| + JSON.parse(File.read(f))['id'] == id + end[0] end def public recipients = ['https://www.w3.org/ns/activitystreams#Public'] - recipients += Dir[File.join(FOLLOWERS, '*.json')].collect { |f| JSON.parse(File.read(f))['actor'] } + recipients += JSON.parse(File.read(FOLLOWERS))['orderedItems'] recipients.delete ACTOR recipients.uniq end |