From 4586c63bd86fe3dad403086b15d5b74b6d67fc92 Mon Sep 17 00:00:00 2001 From: pdp8 Date: Thu, 1 Feb 2024 12:23:31 +0100 Subject: general outbox routes --- create.rb | 47 ++++++++++++++++++++++++++--------------------- helpers.rb | 15 +++++---------- public/style.css | 3 +-- server.rb | 24 ++++++------------------ views/outbox.erb | 6 +++--- 5 files changed, 41 insertions(+), 54 deletions(-) diff --git a/create.rb b/create.rb index 40bfbb8..371f8f8 100644 --- a/create.rb +++ b/create.rb @@ -1,5 +1,4 @@ TO_REGEXP = /^to:\s+/i -CC_REGEXP = /^cc:\s+/i REPLY_REGEXP = /^inreplyto:\s+/i ATTACH_REGEXP = /^attach:\s+/i URL_REGEXP = %r{\Ahttps?://\S+\Z} @@ -23,11 +22,25 @@ post '/create' do line.sub(TO_REGEXP, '').split(/\s+/).each do |word| case word when 'public' - to += ['https://www.w3.org/ns/activitystreams#Public', FOLLOWERS_URL] + to << 'https://www.w3.org/ns/activitystreams#Public' + to << FOLLOWERS_URL when MENTION_REGEXP - to << actor(word) + href = actor(word) + m = { + 'type' => 'Mention', + 'href' => href, + 'name' => word + } + to << href unless to.include? href + tag << m unless tag.include? m when URL_REGEXP - to << word + m = { + 'type' => 'Mention', + 'href' => word, + 'name' => mention(word) + } + to << word unless to.include? word + tag << m unless tag.include? m end end when REPLY_REGEXP @@ -40,8 +53,8 @@ post '/create' do 'url' => url, 'name' => description } - when "\n" - "

\n" + when '' + content << '

' else # create links content << line.split(/\s+/).collect do |word| case word @@ -55,36 +68,28 @@ post '/create' do "#{word}" when MENTION_REGEXP actor = actor(word) - tag << { + m = { 'type' => 'Mention', 'href' => actor, 'name' => word } - to << actor + tag << m unless tag.include? m + to << actor unless to.include? actor "#{word}" + when URL_REGEXP + "#{word}" else word end end.join(' ') - # content << line end end - # https://docs.joinmastodon.org/spec/activitypub/#Mention - if to.size == 1 # mastodon DM - m = { - 'type' => 'Mention', - 'href' => to[0], - 'name' => mention(to[0]) - } - tag << m unless tag.include? m - end - object = { 'to' => to, 'type' => 'Note', 'attributedTo' => ACTOR, - 'content' => "#{content.join(' ')}" + 'content' => "#{content.join('')}" } object['inReplyTo'] = inReplyTo unless inReplyTo.empty? object['attachment'] = attachment unless attachment.empty? @@ -92,7 +97,7 @@ post '/create' do # p to jj object - # create_activity 'Create', object, to + create_activity 'Create', object, to 200 end diff --git a/helpers.rb b/helpers.rb index 09b0d42..292c71d 100644 --- a/helpers.rb +++ b/helpers.rb @@ -162,7 +162,7 @@ helpers do a = fetch(actor) return nil unless a - mention = "#{a['preferredUsername']}@#{URI(actor).host}" + mention = "@#{a['preferredUsername']}@#{URI(actor).host}" cache mention, actor, a mention else @@ -171,11 +171,12 @@ helpers do end def actor(mention) - mention = mention.sub(/^@/, '').chomp + mention = mention.chomp actors = people.select { |p| p[0] == mention } if actors.empty? - _, server = mention.split('@') - a = fetch("https://#{server}/.well-known/webfinger?resource=acct:#{mention}", 'application/jrd+json') + server = mention.split('@').last + a = fetch("https://#{server}/.well-known/webfinger?resource=acct:#{mention.sub(/^@/, '')}", + 'application/jrd+json') return nil unless a actor = a['links'].select do |l| @@ -208,12 +209,6 @@ helpers do "#{type[0]}/#{ext}" end - # def find_file(id) - # Dir[File.join('*', 'object', '*', '*.json')].find do |f| - # JSON.load_file(f)['id'] == id - # end - # end - def find_object(id) Dir[File.join('*', '**', '*.json')].each do |file| object = JSON.load_file(file) diff --git a/public/style.css b/public/style.css index bef69c9..01d0412 100644 --- a/public/style.css +++ b/public/style.css @@ -11,13 +11,12 @@ div.pdp8 { padding: 1em; border: 2px solid #333; border-radius: 1em; - } img, video { max-width: 100%; - max-height: 80vh; + height: auto; display: block; } diff --git a/server.rb b/server.rb index 94729cc..61cbe3c 100644 --- a/server.rb +++ b/server.rb @@ -18,15 +18,9 @@ get '/' do redirect 'https://social.pdp8.info/outbox' end -get '/outbox/announce', provides: 'html' do - @objects = announce_outbox.collect { |a| a['object'] } - @type = 'announce' - erb :outbox -end - -get '/outbox/create', provides: 'html' do - @objects = create_outbox.collect { |a| a['object'] } - @type = 'create' +get '/outbox/:activity', provides: 'html' do + @activity = params['activity'] + @objects = outbox(@activity).collect { |a| a['object'] } erb :outbox end @@ -167,20 +161,14 @@ helpers do File.open(File.join(INBOX[:dir], 'visited'), 'a+') { |f| f.puts @object['id'] } end - def create_outbox - Dir[File.join('outbox', 'create', '*.json')].collect do |f| - JSON.load_file(f) - end.select { |a| a['to'].include?('https://www.w3.org/ns/activitystreams#Public') }.sort_by { |a| a['published'] }.reverse - end - - def announce_outbox - Dir[File.join('outbox', 'announce', '*.json')].collect do |f| + def outbox(activity) + Dir[File.join('outbox', activity, '*.json')].collect do |f| JSON.load_file(f) end.select { |a| a['to'].include?('https://www.w3.org/ns/activitystreams#Public') }.sort_by { |a| a['published'] }.reverse end def public_outbox - create_outbox + announce_outbox + outbox('create') + outbox('announce') end # https://github.com/mastodon/mastodon/blob/main/app/controllers/concerns/signature_verification.rb diff --git a/views/outbox.erb b/views/outbox.erb index 23adb19..d5e3db5 100644 --- a/views/outbox.erb +++ b/views/outbox.erb @@ -10,9 +10,9 @@

pdp8@social.pdp8.info

music, pictures and videos: https://pdp8.info

- <% if @type == 'create' %> + <% if @activity == 'create' %>

posts | boosts

- <% elsif @type == 'announce' %> + <% elsif @activity == 'announce' %>

posts | boosts

<% end %> <% @objects.each do |object| @@ -29,7 +29,7 @@ when /audio/ %>
<% when /image/ %> -
'>' alt='<%= att['name'].gsub("'",''').gsub('"','"') if att['name'] %>'> +
'><%= att[' src='<%= att['url'] %>'> <% when /video/ %>
<% end %> -- cgit v1.2.3