From 711bf7f86daddd0209244f9640d8a3f27d958e3a Mon Sep 17 00:00:00 2001 From: pdp8 Date: Fri, 21 Jul 2023 00:52:49 +0200 Subject: inbox and outbox unified --- client.rb | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) (limited to 'client.rb') diff --git a/client.rb b/client.rb index 386bce5..efc2f6d 100644 --- a/client.rb +++ b/client.rb @@ -3,7 +3,6 @@ # client-server post '/' do # TODO protected! - Time.now.strftime('%Y-%m-%dT%H:%M:%S.%N') recipients = public recipients << params[:to] @@ -116,10 +115,10 @@ 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 + # inbox = JSON.parse File.read(INBOX) + # object = inbox['orderedItems'].find { |i| i['id'] == params['id'] } + # update_collection SHARED, object + # update_collection INBOX, object, true recipients = public recipients << object['attributedTo'] outbox 'Announce', params['id'], recipients @@ -136,28 +135,48 @@ get '/' do redirect '/inbox' end -['/inbox', '/shared', '/outbox'].each do |path| +# ['/inbox', '/shared', '/outbox'].each do |path| +['/inbox', '/outbox'].each do |path| get path, provides: 'html' do protected! @dir = path.sub('/', '') - collection = JSON.parse(File.read(Kernel.const_get(@dir.upcase)))['orderedItems'].uniq + @collection = Dir[File.join(@dir, 'create', '*.json')].collect { |f| JSON.parse(File.read(f))['object'] } + @collection += Dir[File.join(@dir, 'announce', '*.json')].collect { |f| JSON.parse(File.read(f))['object'] } @threads = [] - collection.each do |object| + @collection.collect! do |object| + object = fetch(object) if object.is_a?(String) && object.match(/^http/) + object + end + @collection.each do |object| + add_parents object + end + @collection.each do |object| object['indent'] = 0 object['replies'] = [] - @threads << object if object['inReplyTo'].nil? || collection.select { |o| o['id'] == object['inReplyTo'] }.empty? + @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| + @collection.each do |object| + @collection.select { |o| o['id'] == object['inReplyTo'] }.each do |o| object['indent'] = o['indent'] + 2 o['replies'] << object end end + @threads.sort_by! { |t| t['published'] } erb :collection end end helpers do + def add_parents(object) + return unless object['inReplyTo'] + + object = fetch object['inReplyTo'] + return unless object + + @collection << object unless @collection.collect { |o| o['id'] }.include? object['id'] + add_parents object + end + def protected! halt 403 unless session['client'] end -- cgit v1.2.3