summaryrefslogtreecommitdiff
path: root/client.rb
blob: efc2f6df4d8bae36f544a812069d3ea3afc35cd2 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# frozen_string_literal: true

# client-server
post '/' do # TODO
  protected!

  recipients = public
  recipients << params[:to]

  content = []
  attachment = []
  tag = []
  extensions = {
    image: %w[jpeg png],
    audio: %w[flac wav mp3 ogg],
    video: %w[mp4 webm]
  }
  params[:content].lines.each do |line|
    line.chomp!
    if line.match(/^http/)
      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
      }
    else
      tags = line.split(/\s+/).grep(/^#\w+$/)
      tags.each do |name|
        # href = File.join(TAGS_URL, name.sub('#', ''))
        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
  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'
    unless 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
    update_collection tag_path, activity['object']['id']
  end
  redirect(params['anchor'] || '/inbox')
end

post '/delete' do
  protected!
  collection = Kernel.const_get(params['dir'].upcase)
  if params['id']
    update_collection collection, params, true
  else
    update_collection collection, JSON.parse(File.read(collection))['orderedItems'], true
  end
  redirect(params['anchor'] || '/inbox')
end

post '/follow' do
  protected!
  actor, = parse_follow params['follow']
  outbox 'Follow', actor, [actor]
  redirect(params['anchor'] || '/inbox')
end

post '/unfollow' do
  protected!
  actor, mention = parse_follow params['follow']
  Dir[File.join(OUTBOX_DIR, 'follow', '*.json')].each do |f|
    activity = JSON.parse(File.read(f))
    if activity['object'] == actor
      outbox 'Undo', activity, [actor]
      update_collection FOLLOWING, actor, true
    end
  end
  redirect(params['anchor'] || '/inbox')
end

post '/share' do # TODO
  protected!
  # inbox = JSON.parse File.read(INBOX)
  # object = inbox['orderedItems'].find { |i| i['id'] == params['id'] }
  # update_collection SHARED, object
  # update_collection INBOX, object, true
  recipients = public
  recipients << object['attributedTo']
  outbox 'Announce', params['id'], recipients
  redirect(params['anchor'] || '/inbox')
end

post '/login' do
  session['client'] = (OpenSSL::Digest::SHA256.base64digest(params['secret']) == File.read('.digest').chomp)
  redirect '/inbox'
end

get '/' do
  protected!
  redirect '/inbox'
end

# ['/inbox', '/shared', '/outbox'].each do |path|
['/inbox', '/outbox'].each do |path|
  get path, provides: 'html' do
    protected!
    @dir = path.sub('/', '')
    @collection = Dir[File.join(@dir, 'create', '*.json')].collect { |f| JSON.parse(File.read(f))['object'] }
    @collection += Dir[File.join(@dir, 'announce', '*.json')].collect { |f| JSON.parse(File.read(f))['object'] }
    @threads = []
    @collection.collect! do |object|
      object = fetch(object) if object.is_a?(String) && object.match(/^http/)
      object
    end
    @collection.each do |object|
      add_parents object
    end
    @collection.each do |object|
      object['indent'] = 0
      object['replies'] = []
      @threads << object if object['inReplyTo'].nil? || @collection.select { |o| o['id'] == object['inReplyTo'] }.empty?
    end
    @collection.each do |object|
      @collection.select { |o| o['id'] == object['inReplyTo'] }.each do |o|
        object['indent'] = o['indent'] + 2
        o['replies'] << object
      end
    end
    @threads.sort_by! { |t| t['published'] }
    erb :collection
  end
end

helpers do
  def add_parents(object)
    return unless object['inReplyTo']

    object = fetch object['inReplyTo']
    return unless object

    @collection << object unless @collection.collect { |o| o['id'] }.include? object['id']
    add_parents object
  end

  def protected!
    halt 403 unless session['client']
  end

  def selection(params)
    selection = Dir[File.join(SOCIAL_DIR, params['dir'], '*', '*.json')]
    params['id'] ? selection.select { |f| JSON.parse(File.read(f))['id'] == params['id'] } : selection
  end

  def public
    recipients = ['https://www.w3.org/ns/activitystreams#Public']
    recipients += Dir[File.join(FOLLOWERS, '*.json')].collect { |f| JSON.parse(File.read(f))['actor'] }
    recipients.delete ACTOR
    recipients.uniq
  end

  def parse_follow(follow)
    case follow
    when /^#/
      actor = "https://relay.fedi.buzz/tag/#{follow.sub(/^#/, '')}"
      mention = follow
    when /^http/
      actor = follow
      mention = mention(actor)
    when /^@*\w+@\w+/
      mention = follow
      actor = actor(follow)
      return 502 unless actor
    end
    [actor, mention]
  end
end