summaryrefslogtreecommitdiff
path: root/client.rb
blob: 61d1ac3e01d029be69d449a88ed2d6b966206b9d (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
# client-server
['/inbox/object', '/outbox/object'].each do |path|
  get path do
    protected!
    Dir[File.join(path.sub('/', ''), '*', '*.json')].collect { |f| JSON.load_file(f) }.to_json
  end
end

post '/delete' do
  protected!
  params['id'].each do |id|
    file = find_file id
    halt 404 unless file
    # if file.match(%r{outbox/}) # find/delete activity

    %w[inbox outbox].each do |box|
      Dir[File.join box, 'announce', '*.json'].each do |announce_file|
        announce = JSON.load_file(announce_file)
        next unless announce['object']['id'] == id

        outbox 'Undo', announce, announce['to']
        FileUtils.rm(announce_file)
      end
      Dir[File.join box, 'create', '*.json'].each do |create_file|
        create = JSON.load_file(create_file)
        next unless create['object']['id'] == id

        object = JSON.load_file(file)
        outbox 'Delete', object, object['to']
        FileUtils.rm(create_file)
      end
    end
    FileUtils.rm(file) if File.exist? file
  end
  200
end

post '/undo' do # TODO: generalize for announce
  protected!
  Dir[File.join('outbox', '*', '*.json')].each do |f|
    activity = JSON.load_file(f)
    next unless activity['id'] == params['id']

    object_file = find_file activity['object']['id']
    outbox 'Undo', params['id'], activity['to']
    FileUtils.rm(object_file)
    FileUtils.rm(f)
  end
  200
end

post '/follow' do
  protected!
  params['id'] = actor params['mention'] if params['mention']
  outbox 'Follow', params['id'], [params['id']]
  200
end

post '/unfollow' do
  protected!
  params['id'] = actor params['mention'] if params['mention']
  following = Dir[File.join(OUTBOX[:dir], 'follow', '*.json')].collect { |f| JSON.load_file(f) }
  activity = following.find { |a| a['object'] == params['id'] }
  activity ||= {
    "@context": 'https://www.w3.org/ns/activitystreams',
    "type": 'Follow',
    "actor": 'https://social.pdp8.info/pdp8',
    "object": params['id']
  }
  outbox 'Undo', activity, [params['id']]
  update_collection FOLLOWING, params['id'], true
  200
end

post '/share' do # TODO
  protected!
  src = find_file params['id']
  object = JSON.load_file(src)
  recipients = ['https://www.w3.org/ns/activitystreams#Public']
  recipients += JSON.load_file(FOLLOWERS)['orderedItems']
  recipients << object['attributedTo']
  outbox 'Announce', object, recipients
  dest = src.sub('inbox/', 'outbox/')
  FileUtils.mkdir_p File.dirname(dest)
  FileUtils.mv src, dest
  200
end

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

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