From d635057cb576c5570c5ceba5945cc5339b0f41ab Mon Sep 17 00:00:00 2001 From: pdp8 Date: Mon, 11 Sep 2023 21:09:26 +0200 Subject: new create format, outbox refactoring --- create.rb | 117 ++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 56 insertions(+), 61 deletions(-) (limited to 'create.rb') diff --git a/create.rb b/create.rb index 12d9a27..9241d2b 100644 --- a/create.rb +++ b/create.rb @@ -1,4 +1,11 @@ -post '/create' do # TODO +TO_REGEXP = /^to:\s+/i +REPLY_REGEXP = /^inreplyto:\s+/i +ATTACH_REGEXP = /^attach:\s+/i +URL_REGEXP = %r{\Ahttps?://\S+\Z} +MENTION_REGEXP = /\A@\w+@\S+\Z/ +HASHTAG_REGEXP = /\A#\w+\Z/ + +post '/create' do protected! request.body.rewind # in case someone already read it @@ -8,72 +15,61 @@ post '/create' do # TODO tag = [] attachment = [] - url_regexp = %r{\Ahttps?://\S+\Z} - mention_regexp = /\A@\w+@\S+\Z/ - hashtag_regexp = /\A#\w+\Z/ - - lines = request.body.read.each_line.to_a - lines.each.with_index do |line, i| + request.body.read.each_line do |line| line.chomp! - if i == 0 - to = line.split(/\s+/).collect do |word| + case line + when TO_REGEXP + line.sub(TO_REGEXP, '').split(/\s+/).each do |word| case word when 'public' - ['https://www.w3.org/ns/activitystreams#Public', FOLLOWERS_URL] - when mention_regexp - actor word - when url_regexp - word + to += ['https://www.w3.org/ns/activitystreams#Public', FOLLOWERS_URL] + when MENTION_REGEXP + to << actor(word) + when URL_REGEXP + to << word end - end.flatten - elsif i == 1 and line.match url_regexp - inReplyTo = line - elsif line == '' - content << '

' - elsif line.match(/\A==\Z/) - attachment = lines[i + 1..-1].collect do |url| - url.chomp! - url, name = url.split(/\s+/, 2) - doc = { - 'type' => 'Document', - 'mediaType' => media_type(url), - 'url' => url - } - doc['name'] = name if name - doc end - break - else - # create links + when REPLY_REGEXP + inReplyTo = line.sub(REPLY_REGEXP, '') + when ATTACH_REGEXP + url = line.sub(ATTACH_REGEXP, '') + attachment << { + 'type' => 'Document', + 'mediaType' => media_type(url), + 'url' => url + } + when '' + content << '

' + 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('#', '')) - tag << { - 'type' => 'Hashtag', - 'href' => tag_url, - 'name' => name - } - # single quotes in html invalidate digest, reason unknown - line.gsub!(name, "#{name}") - end - mentions = line.split(/\s+/).grep(mention_regexp) - mentions.each do |mention| - actor = actor(mention) - tag << { - 'type' => 'Mention', - 'href' => actor, - 'name' => mention - } - # single quotes in html invalidate digest, reason unknown - line.gsub!(mention, "#{mention}") + content << line.split(/\s+/).collect do |word| + case word + when URL_REGEXP + "#{word}" + when URI::MailTo::EMAIL_REGEXP + "#{word}" + when HASHTAG_REGEXP + tag_url = File.join('https://social.pdp8.info', 'tags', word.sub('#', '')) + tag << { + 'type' => 'Hashtag', + 'href' => tag_url, + 'name' => word + } + "#{word}" + when MENTION_REGEXP + actor = actor(word) + tag << { + 'type' => 'Mention', + 'href' => actor, + 'name' => word + } + "#{word}" + else + word + end end - content << '
' + line end end - content.shift while content[0] == '

' object = { 'to' => to, @@ -84,10 +80,9 @@ post '/create' do # TODO object['inReplyTo'] = inReplyTo unless inReplyTo.empty? object['attachment'] = attachment unless attachment.empty? object['tag'] = tag unless tag.empty? - - p 'outbox' jj object - # outbox 'Create', object, to + + # create_activity 'Create', object, to 200 end -- cgit v1.2.3