post '/create' do # TODO protected! request.body.rewind # in case someone already read it to = [] inReplyTo = '' content = [] 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| line.chomp! if i == 0 to = line.split(/\s+/).collect do |word| case word when 'public' ['https://www.w3.org/ns/activitystreams#Public', FOLLOWERS_URL] when mention_regexp actor word when url_regexp 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 # 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}") end content << '
' + line end end content.shift while content[0] == '

' object = { 'to' => to, 'type' => 'Note', 'attributedTo' => ACTOR, 'content' => "#{content.join(' ')}" } 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 200 end