diff options
Diffstat (limited to 'create.rb')
-rw-r--r-- | create.rb | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/create.rb b/create.rb new file mode 100644 index 0000000..d986812 --- /dev/null +++ b/create.rb @@ -0,0 +1,97 @@ +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 << '<p>' + elsif line.match(/\A==\Z/) + attachment = lines[i + 1..-1].collect do |url| + url.chomp! + { + 'type' => 'Document', + 'mediaType' => media_type(url), + 'url' => url + } + end + break + else + 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 + } + end + mentions = line.split(/\s+/).grep(mention_regexp) + mentions.each do |mention| + actor = actor(mention) + tag << { + 'type' => 'Mention', + 'href' => actor, + 'name' => mention + } + end + content << line + end + end + + object = { + 'to' => to, + 'type' => 'Note', + 'attributedTo' => ACTOR, + 'content' => "#{content.join("\n")}" + } + object['inReplyTo'] = inReplyTo unless inReplyTo.empty? + object['attachment'] = attachment unless attachment.empty? + 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 + + 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 + end + 200 +end |