summary refs log tree commit diff
diff options
context:
space:
mode:
authorpdp8 <pdp8@pdp8.info>2023-07-25 00:24:02 +0200
committerpdp8 <pdp8@pdp8.info>2023-07-25 00:24:02 +0200
commit3daf30b0a3837d3d8becb0baceed580e92403ce6 (patch)
tree8e0ac2d67a0ead3e0bd324088485618927858bb2
parent0f039c1b813653869033e961810942f479a28685 (diff)
send notes to outbox
-rw-r--r--client.rb6
-rw-r--r--helpers.rb2
-rw-r--r--send.rb33
-rw-r--r--server.rb5
4 files changed, 25 insertions, 21 deletions
diff --git a/client.rb b/client.rb
index b0b6385..5ef721b 100644
--- a/client.rb
+++ b/client.rb
@@ -38,6 +38,7 @@ post '/delete' do
     file = find_file id
     FileUtils.rm(file) if File.exist? file
   end
+  200
 end
 
 post '/follow' do
@@ -58,14 +59,15 @@ end
 
 post '/share' do # TODO
   protected!
-  src = find_file INBOX, params['id']
+  src = find_file params['id']
   object = JSON.parse(File.read(src))
   recipients = public
   recipients << object['attributedTo']
   outbox 'Announce', object, recipients
-  dest = src.sub('/inbox/', '/outbox/')
+  dest = src.sub('inbox/', 'outbox/')
   FileUtils.mkdir_p File.dirname(dest)
   FileUtils.mv src, dest
+  200
 end
 
 post '/login' do
diff --git a/helpers.rb b/helpers.rb
index 6812a83..5344606 100644
--- a/helpers.rb
+++ b/helpers.rb
@@ -6,7 +6,7 @@ helpers do
   # add date and id, save
   def save_activity(activity, box)
     date = Time.now.utc.iso8601
-    activity['published'] = date if box == OUTBOX
+    activity['published'] ||= date # if box == OUTBOX
     basename = "#{activity['published']}_#{mention(activity['actor'])}.json"
     activity_rel_path = File.join(activity['type'].downcase, basename)
     activity_path = File.join(box[:dir], activity_rel_path)
diff --git a/send.rb b/send.rb
index 91d3e9b..6dd4f3b 100644
--- a/send.rb
+++ b/send.rb
@@ -1,13 +1,11 @@
-post '/' do # TODO
+post '/outbox' do # TODO
   protected!
 
-  recipients = public
+  recipients = params[:public] == 'true' ? public : []
   recipients << params[:to]
 
-  # content = []
   tag = []
   params[:content].lines.each do |line|
-    # line.chomp!
     tags = line.split(/\s+/).grep(/^#\w+$/)
     tags.each do |name|
       tag_url = File.join(TAGS[:url], name.sub('#', ''))
@@ -27,7 +25,6 @@ post '/' do # TODO
         'name' => mention
       }
     end
-    # content << line
   end
 
   attachment = []
@@ -36,31 +33,33 @@ post '/' do # TODO
     audio: %w[flac wav mp3 ogg],
     video: %w[mp4 webm]
   }
-  params[:media].each do |_media|
-    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
-    }
+  if params[:media]
+    params[:media].each do |_media|
+      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
+      }
+    end
   end
 
   object = {
     'type' => 'Note',
     'attributedTo' => ACTOR,
     'inReplyTo' => params[:inReplyTo],
-    'content' => "<p>\n#{content.join("\n<br>")}\n</p>",
+    'content' => "<p>\n#{params[:content]}\n</p>",
     'attachment' => attachment,
     'tag' => tag,
     'to' => recipients
   }
 
-  activity = outbox 'Create', object, recipients, true
+  activity = outbox 'Create', object, recipients
   activity['object']['tag'].each do |tag|
     next unless tag['type'] == 'Hashtag'
 
-    tag_path = File.join(TAGS_DIR, tag['name'].sub('#', '')) + '.json'
+    tag_path = File.join(TAGS[:dir], tag['name'].sub('#', '')) + '.json'
     next if File.exist? tag_path
 
     File.open(tag_path, 'w+') do |f|
@@ -73,7 +72,5 @@ post '/' do # TODO
       }
       f.puts tag_collection.to_json
     end
-    # update_collection tag_path, activity['object']['id']
   end
-  redirect(params['anchor'] || '/inbox')
 end
diff --git a/server.rb b/server.rb
index 666b59e..e9f4f2e 100644
--- a/server.rb
+++ b/server.rb
@@ -83,6 +83,11 @@ helpers do
     update_collection FOLLOWERS, @activity['object']['actor'], true
   end
 
+  def update
+    FileUtils.rm(find_file(@activity['object']['id']))
+    create
+  end
+
   # https://github.com/mastodon/mastodon/blob/main/app/controllers/concerns/signature_verification.rb
   def verify!
     # digest