summaryrefslogtreecommitdiff
path: root/server.rb
diff options
context:
space:
mode:
Diffstat (limited to 'server.rb')
-rw-r--r--server.rb71
1 files changed, 51 insertions, 20 deletions
diff --git a/server.rb b/server.rb
index 7319efa..68c51e1 100644
--- a/server.rb
+++ b/server.rb
@@ -2,24 +2,16 @@
post '/inbox' do
request.body.rewind # in case someone already read it
@body = request.body.read
- halt 400 if @body.empty?
begin
@activity = JSON.parse @body
- rescue StandardError
+ rescue StandardError => e
p @body
halt 400
end
# deleted actors return 403 => verification error
halt 200 if @activity['type'] == 'Delete' and @activity['actor'] == @activity['object']
# verify! # pixelfed sends unsigned activities???
- type = @activity['type'].downcase.to_sym
- save_activity(@activity, INBOX) # unless %i[create announce].include? type
- if %i[create announce follow accept undo delete like update move].include? type
- send(type)
- else
- p "Unknown activity #{type}:"
- jj @activity
- end
+ handle_activity
200
end
@@ -66,12 +58,7 @@ helpers do
def create
@count ||= 0
@object ||= @activity['object']
-
- @object = if @object['type'] == 'Like' # lemmy likes
- save_object @object['object'], INBOX
- else
- save_object @object, INBOX
- end
+ save_inbox_object
return unless @object and @object['inReplyTo'] and @count < 5
# recursive thread download
@@ -81,11 +68,14 @@ helpers do
end
def announce
+ @object ||= @activity['object']
+ @object = fetch(@object) if @object.is_a? String and @object.match(/^http/)
+ @object['announce'] = @activity['actor'] if @object
create
end
def like
- create
+ announce
end
def follow
@@ -94,8 +84,13 @@ helpers do
end
def accept
- halt 501 unless @activity['object']['type'] == 'Follow'
- update_collection FOLLOWING, @activity['object']['object']
+ if @activity['object']['type'] == 'Follow'
+ update_collection FOLLOWING, @activity['object']['object']
+ else
+ p "Cannot accept @activity['object']['type']"
+ jj @activity
+ halt 501
+ end
end
def undo
@@ -106,12 +101,15 @@ helpers do
file = find_file @activity['object']['object']
FileUtils.rm(file) if file
else
+ p "Cannot undo @activity['object']['type']"
+ jj @activity
halt 501
end
end
def update
- FileUtils.rm(find_file(@activity['object']['id']))
+ file = find_file(@activity['object']['id'])
+ FileUtils.rm(file) if file
create
end
@@ -124,6 +122,39 @@ helpers do
outbox 'Follow', @activity['target'], [@activity['target']] if @activity['actor'] == @activity['object']
end
+ def handle_activity
+ type = @activity['type'].downcase.to_sym
+ save_item @activity, File.join(INBOX[:dir], @activity['type'].downcase, activity_name)
+ if ACTIVITIES.include? type
+ send(type)
+ else
+ p "Unknown activity #{type}:"
+ jj @activity
+ end
+ end
+
+ def activity_name
+ @activity['published'] ? "#{@activity['published']}_#{mention(@activity['actor'])}.json" : "_#{Time.now.utc.iso8601}_#{mention(@activity['actor'])}.json"
+ end
+
+ def save_inbox_object
+ @object = fetch(@object) if @object.is_a? String and @object.match(/^http/)
+ if @object['type'] and ACTIVITIES.include? @object['type'].downcase.to_sym
+ @activity = @object
+ handle_activity
+ return
+ end
+ # @object = @object['object'] if @object['type'] == 'Like' # lemmy likes
+ return unless @object and @object['type'] != 'Person'
+
+ if @activity['type'] != 'Update' && (@object['id'] and File.readlines(VISITED, chomp: true).include? @object['id'])
+ return
+ end
+
+ save_item @object, File.join(INBOX[:dir], 'object', @object['type'].downcase, activity_name)
+ File.open(File.join(INBOX[:dir], 'visited'), 'a+') { |f| f.puts @object['id'] }
+ end
+
# https://github.com/mastodon/mastodon/blob/main/app/controllers/concerns/signature_verification.rb
def verify!
# digest