summaryrefslogtreecommitdiff
path: root/create.rb
blob: 6a6dd4ef47358579826795e21c89f7bb5c4e67e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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!
        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, "<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>")
      end
      content << line
    end
  end
  content.shift while content[0] == '<p>'

  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?

  outbox 'Create', object, to

  200
end