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
|
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
|