From 3a8be7b1606885b3b94bc22a6d775a6527e1c07d Mon Sep 17 00:00:00 2001 From: pdp8 Date: Sun, 30 Jul 2023 15:07:28 +0200 Subject: note parsing, tag links --- .gitignore | 2 ++ activitypub.rb | 38 -------------------------------------- application.rb | 38 ++++++++++++++++++++++++++++++++++++++ client.rb | 6 ------ create.rb | 32 +++++++++++++++++++------------- helpers.rb | 6 ++++++ server.rb | 15 +++++++++++++-- 7 files changed, 78 insertions(+), 59 deletions(-) delete mode 100644 activitypub.rb create mode 100644 application.rb diff --git a/.gitignore b/.gitignore index 6911852..da487b4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +generate-digest.rb +watch *.pem .usr .pwd diff --git a/activitypub.rb b/activitypub.rb deleted file mode 100644 index 8a62fdd..0000000 --- a/activitypub.rb +++ /dev/null @@ -1,38 +0,0 @@ -# frozen_string_literal: true - -require 'uri' -require 'base64' -require 'digest/sha2' -require 'sinatra' - -SOCIAL_DIR = '/srv/social/' -PUBLIC_DIR = File.join(SOCIAL_DIR, 'public') -PRIVATE_DIR = File.join(SOCIAL_DIR, 'private') - -FOLLOWERS = File.join(PUBLIC_DIR, 'followers.json') -FOLLOWING = File.join(PUBLIC_DIR, 'following.json') - -USER = 'pdp8' -SOCIAL_DOMAIN = 'social.pdp8.info' -MENTION = "#{USER}@#{SOCIAL_DOMAIN}" -WEBFINGER = File.join(PUBLIC_DIR, MENTION + '.json') - -SOCIAL_URL = "https://#{SOCIAL_DOMAIN}" -ACTOR = File.join(SOCIAL_URL, USER) -INBOX = { dir: File.join(SOCIAL_DIR, 'inbox') } -OUTBOX = { dir: File.join(SOCIAL_DIR, 'outbox'), url: File.join(SOCIAL_URL, 'outbox') } -TAGS = { dir: File.join(PUBLIC_DIR, 'tags'), url: File.join(SOCIAL_URL, 'tags') } -FOLLOWERS_URL = 'https://social.pdp8.info/followers' - -CONTENT_TYPE = 'application/activity+json' -# CONTENT_TYPE = 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' - -enable :sessions -set :session_secret, File.read('.secret').chomp -set :default_content_type, CONTENT_TYPE -set :port, 9292 - -require_relative 'helpers' -require_relative 'server' -require_relative 'client' -require_relative 'create' diff --git a/application.rb b/application.rb new file mode 100644 index 0000000..8a62fdd --- /dev/null +++ b/application.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'uri' +require 'base64' +require 'digest/sha2' +require 'sinatra' + +SOCIAL_DIR = '/srv/social/' +PUBLIC_DIR = File.join(SOCIAL_DIR, 'public') +PRIVATE_DIR = File.join(SOCIAL_DIR, 'private') + +FOLLOWERS = File.join(PUBLIC_DIR, 'followers.json') +FOLLOWING = File.join(PUBLIC_DIR, 'following.json') + +USER = 'pdp8' +SOCIAL_DOMAIN = 'social.pdp8.info' +MENTION = "#{USER}@#{SOCIAL_DOMAIN}" +WEBFINGER = File.join(PUBLIC_DIR, MENTION + '.json') + +SOCIAL_URL = "https://#{SOCIAL_DOMAIN}" +ACTOR = File.join(SOCIAL_URL, USER) +INBOX = { dir: File.join(SOCIAL_DIR, 'inbox') } +OUTBOX = { dir: File.join(SOCIAL_DIR, 'outbox'), url: File.join(SOCIAL_URL, 'outbox') } +TAGS = { dir: File.join(PUBLIC_DIR, 'tags'), url: File.join(SOCIAL_URL, 'tags') } +FOLLOWERS_URL = 'https://social.pdp8.info/followers' + +CONTENT_TYPE = 'application/activity+json' +# CONTENT_TYPE = 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' + +enable :sessions +set :session_secret, File.read('.secret').chomp +set :default_content_type, CONTENT_TYPE +set :port, 9292 + +require_relative 'helpers' +require_relative 'server' +require_relative 'client' +require_relative 'create' diff --git a/client.rb b/client.rb index c2995f5..bc153f5 100644 --- a/client.rb +++ b/client.rb @@ -82,12 +82,6 @@ helpers do halt 403 unless session['client'] end - def find_file(id) - Dir[File.join('*', 'object', '*', '*.json')].find do |f| - JSON.parse(File.read(f))['id'] == id - end - end - def public recipients = ['https://www.w3.org/ns/activitystreams#Public'] recipients += JSON.parse(File.read(FOLLOWERS))['orderedItems'] diff --git a/create.rb b/create.rb index 301417f..b418c5b 100644 --- a/create.rb +++ b/create.rb @@ -9,7 +9,7 @@ post '/create' do # TODO attachment = [] url_regexp = %r{\Ahttps?://\S+\Z} - mention_regexp = /\A@?\w+@\S+\Z/ + mention_regexp = /\A@\w+@\S+\Z/ hashtag_regexp = /\A#\w+\Z/ lines = request.body.read.each_line.to_a @@ -40,10 +40,11 @@ post '/create' do # TODO } end break - elsif line.match(url_regexp) - # single quotes in html invalidate digest, reason unknown - content << line.gsub(Regexp.last_match(0), "#{Regexp.last_match(0)}") else + # create links + # single quotes in html invalidate digest, reason unknown + line.split(/\s+/).grep(url_regexp).each { |u| line.gsub!(u, "#{u}") } + line.split(/\s+/).grep(URI::MailTo::EMAIL_REGEXP).each { |m| line.gsub!(m, "#{m}") } tags = line.split(/\s+/).grep(hashtag_regexp) tags.each do |name| tag_url = File.join(TAGS[:url], name.sub('#', '')) @@ -81,21 +82,26 @@ post '/create' do # TODO object['tag'] = tag unless tag.empty? activity = outbox 'Create', object, to + if activity['object']['tag'] activity['object']['tag'].each do |tag| next unless tag['type'] == 'Hashtag' tag_path = File.join(TAGS[:dir], tag['name'].sub('#', '')) + '.json' - next if File.exist? tag_path - + tag_collection = if File.exist? tag_path + JSON.parse(File.read(tag_path)) + else + { + '@context' => 'https://www.w3.org/ns/activitystreams', + 'id' => tag['href'], + 'type' => 'OrderedCollection', + 'totalItems' => 0, + 'orderedItems' => [] + } + end + tag_collection['orderedItems'] << activity['object']['id'] + tag_collection['totalItems'] = tag_collection['orderedItems'].size 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 end end diff --git a/helpers.rb b/helpers.rb index 4940b34..bb43303 100644 --- a/helpers.rb +++ b/helpers.rb @@ -135,4 +135,10 @@ helpers do type = extensions.find { |_k, v| v.include? ext } "#{type[0]}/#{ext}" end + + def find_file(id) + Dir[File.join('*', 'object', '*', '*.json')].find do |f| + JSON.parse(File.read(f))['id'] == id + end + end end diff --git a/server.rb b/server.rb index fd14356..4eae86b 100644 --- a/server.rb +++ b/server.rb @@ -54,6 +54,10 @@ end end end +get '/tags/:tag' do |tag| + send_file(File.join(PUBLIC_DIR, 'tags', tag) + '.json', type: CONTENT_TYPE) +end + helpers do def create @object ||= @activity['object'] @@ -79,8 +83,15 @@ helpers do end def undo - halt 501 unless @activity['object']['type'] == 'Follow' - update_collection FOLLOWERS, @activity['object']['actor'], true + case @activity['object']['type'] + when 'Follow' + update_collection FOLLOWERS, @activity['object']['actor'], true + when 'Create', 'Announce' + file = find_file @activity['object']['id'] + FileUtils.rm(file) if File.exist? file + else + halt 501 + end end def update -- cgit v1.2.3