diff options
author | pdp8 <pdp8@pdp8.info> | 2023-09-11 21:09:26 +0200 |
---|---|---|
committer | pdp8 <pdp8@pdp8.info> | 2023-09-11 21:09:26 +0200 |
commit | d635057cb576c5570c5ceba5945cc5339b0f41ab (patch) | |
tree | 3baf2432690f221f67dc318a3fd5aa6b271c9961 /create.rb | |
parent | da017e7cd9394cb759ee74440c5fd25860063905 (diff) |
new create format, outbox refactoring
Diffstat (limited to 'create.rb')
-rw-r--r-- | create.rb | 117 |
1 files changed, 56 insertions, 61 deletions
@@ -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 << '<p>' - 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 << '<p>' + else # create links # single quotes in html invalidate digest, reason unknown - line.split(/\s+/).grep(url_regexp).each { |u| line.gsub!(u, "<a href=\"#{u}\">#{u}</a>") } - line.split(/\s+/).grep(URI::MailTo::EMAIL_REGEXP).each { |m| line.gsub!(m, "<a href=\"mailto:#{m}\">#{m}</a>") } - 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, "<a href=\"#{tag_url}\">#{name}</a>") - 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, "<a href=\"#{actor}\">#{mention}</a>") + content << line.split(/\s+/).collect do |word| + case word + when URL_REGEXP + "<a href=\"#{word}\">#{word}</a>" + when URI::MailTo::EMAIL_REGEXP + "<a href=\"mailto:#{word}\">#{word}</a>" + when HASHTAG_REGEXP + tag_url = File.join('https://social.pdp8.info', 'tags', word.sub('#', '')) + tag << { + 'type' => 'Hashtag', + 'href' => tag_url, + 'name' => word + } + "<a href=\"#{tag_url}\">#{word}</a>" + when MENTION_REGEXP + actor = actor(word) + tag << { + 'type' => 'Mention', + 'href' => actor, + 'name' => word + } + "<a href=\"#{actor}\">#{word}</a>" + else + word + end end - content << '<br>' + line end end - content.shift while content[0] == '<p>' 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 |