summaryrefslogtreecommitdiff
path: root/client.rb
diff options
context:
space:
mode:
authorpdp8 <pdp8@pdp8.info>2023-07-02 00:37:33 +0200
committerpdp8 <pdp8@pdp8.info>2023-07-02 00:37:33 +0200
commit7f38d569d8dd2491d1b9b8bc0ff1ae016b02f34f (patch)
tree5ddd0eba0dce147e8b799351a9f3e20945a08118 /client.rb
parent086709cae3da7a01a011fe906004c8685fdd2ed0 (diff)
activity sending/storage unified (send_signed -> outbox)
Diffstat (limited to 'client.rb')
-rw-r--r--client.rb105
1 files changed, 41 insertions, 64 deletions
diff --git a/client.rb b/client.rb
index 3016d02..8f3557a 100644
--- a/client.rb
+++ b/client.rb
@@ -3,14 +3,10 @@
# client-server
post '/' do
protected!
- date = Time.now.strftime('%Y-%m-%dT%H:%M:%S')
- outbox_path = File.join('public/outbox', "#{date}.json")
- notes_path = File.join('public/notes', "#{date}.json")
+ date = Time.now.strftime('%Y-%m-%dT%H:%M:%S.%N')
- recipients = ['https://www.w3.org/ns/activitystreams#Public', params[:to]]
- recipients += Dir[File.join('public/followers', '*.json')].collect { |f| JSON.parse(File.read(f))['actor'] }
- recipients.delete ACTOR
- recipients.uniq!
+ recipients = public
+ recipients << params[:to]
content = []
attachment = []
@@ -45,99 +41,68 @@ post '/' do
end
end
- create = {
- '@context' => 'https://www.w3.org/ns/activitystreams',
- 'id' => File.join(SOCIAL_URL, outbox_path),
- 'type' => 'Create',
- 'actor' => ACTOR,
- 'object' => {
- 'id' => File.join(SOCIAL_URL, notes_path),
- 'type' => 'Note',
- 'attributedTo' => ACTOR,
- 'inReplyTo' => params[:inReplyTo],
- 'published' => date,
- 'content' => "<p>\n#{content.join("\n<br>")}\n</p>",
- 'attachment' => attachment,
- 'tag' => tag,
- 'to' => recipients
- },
- 'published' => date,
+ object = {
+ 'type' => 'Note',
+ 'attributedTo' => ACTOR,
+ 'inReplyTo' => params[:inReplyTo],
+ 'content' => "<p>\n#{content.join("\n<br>")}\n</p>",
+ 'attachment' => attachment,
+ 'tag' => tag,
'to' => recipients
}
- File.open(outbox_path, 'w+') { |f| f.puts create.to_json }
- File.open(notes_path, 'w+') { |f| f.puts create['object'].to_json }
- tag.each do |t|
- dir = File.join('public', 'tags', t['name'].sub('#', ''))
- FileUtils.mkdir_p dir
- FileUtils.ln_s File.join('/srv/social/', notes_path), dir
- end
-
- # recipients.delete "https://www.w3.org/ns/activitystreams#Public"
- # recipients.each { |r| send_signed create, r }
- # send_signed create # , r }
- outbox create
- redirect params['redirect']
+ outbox 'Create', object, recipients, true
+ redirect(params['anchor'] || '/inbox')
end
-post '/archive' do
+post '/share' do
protected!
- FileUtils.mv params['file'], 'archive/'
- redirect to(params['redirect'])
+ selection(params).each { |f| FileUtils.mv f, f.sub(%r{/inbox/}, '/shared/') }
+ outbox 'Announce', params['id'], public
+ redirect(params['anchor'] || '/inbox')
end
-post '/delete' do # delete not supported by html forms
+post '/delete' do
protected!
- FileUtils.rm_f(params['file'] || Dir['inbox/*.json'])
- redirect(params['redirect'] || '/')
+ selection(params).each { |f| FileUtils.rm f }
+ redirect(params['anchor'] || '/inbox')
end
post '/follow' do
protected!
actor, mention = parse_follow params['follow']
- follow = { '@context' => 'https://www.w3.org/ns/activitystreams',
- 'id' => File.join(SOCIAL_URL, 'following', "#{mention}.json"),
- 'type' => 'Follow',
- 'actor' => ACTOR,
- 'object' => actor,
- 'to' => [actor] }
- send_signed follow # , actor
- redirect '/'
+ outbox 'Follow', actor, [actor]
+ redirect(params['anchor'] || '/inbox')
end
post '/unfollow' do
protected!
actor, mention = parse_follow params['follow']
- following_path = File.join('public', 'following', "#{mention}.json")
+ following_path = File.join(FOLLOWING_DIR, "#{mention}.json")
if File.exist?(following_path)
- undo = { '@context' => 'https://www.w3.org/ns/activitystreams',
- 'id' => File.join("#{SOCIAL_URL}#undo", SecureRandom.uuid),
- 'type' => 'Undo',
- 'actor' => ACTOR,
- 'object' => JSON.parse(File.read(following_path)),
- 'to' => [actor] }
- send_signed undo # , actor
- FileUtils.rm following_path
- redirect '/'
+ outbox 'Undo', JSON.parse(File.read(following_path)), [actor]
+ FileUtils.rm_f following_path
+ redirect(params['anchor'] || '/inbox')
end
end
post '/login' do
session['client'] = (OpenSSL::Digest::SHA256.base64digest(params['secret']) == File.read('.digest').chomp)
- redirect '/'
+ redirect '/inbox'
end
get '/' do
+ protected!
redirect '/inbox'
end
-['/inbox', '/archive', '/outbox'].each do |path|
+['/inbox', '/shared', '/outbox'].each do |path|
get path, provides: 'html' do
protected!
@dir = path.sub('/', '')
collection = ordered_collection(File.join(SOCIAL_DIR, path, 'note'))['orderedItems']
@threads = []
- collection.each do |object|
+ collection.each_with_index do |object, _idx|
object['indent'] = 0
object['replies'] = []
if object['inReplyTo'].nil? || collection.select { |o| o['id'] == object['inReplyTo'] }.empty?
@@ -158,6 +123,18 @@ helpers do
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 /^#/