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 --- create.rb | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'create.rb') 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 -- cgit v1.2.3