summaryrefslogtreecommitdiff
path: root/send.rb
blob: 91d3e9bfe4f7eebb98af8e2fa61fd3d039b58133 (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
post '/' do # TODO
  protected!

  recipients = public
  recipients << params[:to]

  # content = []
  tag = []
  params[:content].lines.each do |line|
    # line.chomp!
    tags = line.split(/\s+/).grep(/^#\w+$/)
    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(/^@\w+@\S+$/)
    mentions.each do |mention|
      actor = actor(mention)
      tag << {
        'type' => 'Mention',
        'href' => actor,
        'name' => mention
      }
    end
    # content << line
  end

  attachment = []
  extensions = {
    image: %w[jpeg png],
    audio: %w[flac wav mp3 ogg],
    video: %w[mp4 webm]
  }
  params[:media].each do |_media|
    ext = File.extname(line).sub('.', '')
    media_type = extensions.select { |_k, v| v.include? ext }.keys[0].to_s + '/' + ext
    attachment << {
      'type' => 'Document',
      'mediaType' => media_type,
      'url' => line
    }
  end

  object = {
    'type' => 'Note',
    'attributedTo' => ACTOR,
    'inReplyTo' => params[:inReplyTo],
    'content' => "<p>\n#{content.join("\n<br>")}\n</p>",
    'attachment' => attachment,
    'tag' => tag,
    'to' => recipients
  }

  activity = outbox 'Create', object, recipients, true
  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
    # update_collection tag_path, activity['object']['id']
  end
  redirect(params['anchor'] || '/inbox')
end