summaryrefslogtreecommitdiff
path: root/create.rb
blob: 32ec4809ce4061de5425359fc6a4e6602756d59a (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
92
93
94
95
96
97
98
99
100
101
102
103
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

  to = []
  inReplyTo = ''
  content = []
  tag = []
  attachment = []

  request.body.read.each_line do |line|
    line.chomp!
    case line
    when TO_REGEXP
      line.sub(TO_REGEXP, '').split(/\s+/).each do |word|
        case word
        when 'public'
          to << 'https://www.w3.org/ns/activitystreams#Public'
          to << FOLLOWERS_URL
        when MENTION_REGEXP
          href = actor(word)
          m = {
            'type' => 'Mention',
            'href' => href,
            'name' => word
          }
          to << href unless to.include? href
          tag << m unless tag.include? m
        when URL_REGEXP
          m = {
            'type' => 'Mention',
            'href' => word,
            'name' => mention(word)
          }
          to << word unless to.include? word
          tag << m unless tag.include? m
        end
      end
    when REPLY_REGEXP
      inReplyTo = line.sub(REPLY_REGEXP, '')
    when ATTACH_REGEXP
      url, description = line.sub(ATTACH_REGEXP, '').split(/\s+/, 2)
      attachment << {
        'type' => 'Document',
        'mediaType' => media_type(url), # TODO: query with curl HEAD
        'url' => url,
        'name' => description
      }
    when ''
      content << '<p>'
    else # create links
      content << line.split(/\s+/).collect do |word|
        case word
        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)
          m = {
            'type' => 'Mention',
            'href' => actor,
            'name' => word
          }
          tag << m unless tag.include? m
          to << actor unless to.include? actor
          "<a href=\"#{actor}\">#{word}</a>"
        when URL_REGEXP
          "<a href=\"#{word}\">#{word}</a>"
        else
          word
        end
      end.join(' ')
    end
  end

  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?
  jj object

  create_activity 'Create', object, to
  outbox_html('create')

  200
end