summary refs log tree commit diff
diff options
context:
space:
mode:
authorpdp8 <pdp8@pdp8.info>2023-07-17 21:00:10 +0200
committerpdp8 <pdp8@pdp8.info>2023-07-17 21:00:10 +0200
commit8596528d9c0c07cc46279679f61729a3c06d902e (patch)
tree7fe1dd60cffddbe74488c360c119b87a5b125da1
parent4ca1c5555f18703d1dac673b1a77d1c919fb4787 (diff)
client (un)follow fixed
-rw-r--r--client.rb33
-rw-r--r--helpers.rb7
-rw-r--r--server.rb10
3 files changed, 21 insertions, 29 deletions
diff --git a/client.rb b/client.rb
index 24d8795..386175d 100644
--- a/client.rb
+++ b/client.rb
@@ -55,13 +55,6 @@ post '/' do
   redirect(params['anchor'] || '/inbox')
 end
 
-post '/share' do
-  protected!
-  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
   protected!
   collection = Kernel.const_get(params['dir'].upcase)
@@ -83,14 +76,21 @@ end
 post '/unfollow' do
   protected!
   actor, mention = parse_follow params['follow']
-  outbox 'Undo', JSON.parse(File.read(following_path)), [actor]
-  p actor
-  following_path = File.join(FOLLOWING_DIR, "#{mention}.json")
-  if File.exist?(following_path)
-    outbox 'Undo', JSON.parse(File.read(following_path)), [actor]
-    FileUtils.rm_f following_path
-    redirect(params['anchor'] || '/inbox')
+  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
+  protected!
+  selection(params).each { |f| FileUtils.mv f, f.sub(%r{/inbox/}, '/shared/') }
+  outbox 'Announce', params['id'], public
+  redirect(params['anchor'] || '/inbox')
 end
 
 post '/login' do
@@ -107,10 +107,7 @@ end
   get path, provides: 'html' do
     protected!
     @dir = path.sub('/', '')
-    # p(Kernel.const_get(@dir.upcase))
-    # p(File.read(Kernel.const_get(@dir.upcase)))
     collection = JSON.parse(File.read(Kernel.const_get(@dir.upcase)))['orderedItems'].uniq
-    # collection = ordered_collection(File.join(SOCIAL_DIR, path, 'note'))['orderedItems']
     @threads = []
     collection.each do |object|
       object['indent'] = 0
@@ -123,8 +120,6 @@ end
 
           object['indent'] = o['indent'] + 2
           o['replies'] << object
-          # else
-          # p o
         end
       end
     end
diff --git a/helpers.rb b/helpers.rb
index 84f6a89..3e24b82 100644
--- a/helpers.rb
+++ b/helpers.rb
@@ -83,13 +83,12 @@ helpers do
       json = f.read
       collection = JSON.parse(json)
       objects.each do |object|
+        id = object['id'] || object
         if delete
-          collection['orderedItems'].delete_if { |o| o['id'] == object['id'] }
-          modified = true
+          collection['orderedItems'].delete_if { |o| o['id'] == id or o == id }
         else
           ids = collection['orderedItems'].collect { |i| i['id'] }
-          collection['orderedItems'] << object unless ids.include?(object['id'])
-          modified = true
+          collection['orderedItems'] << object unless ids.include?(id) or collection['orderedItems'].include?(id)
         end
       end
       collection['totalItems'] = collection['orderedItems'].size
diff --git a/server.rb b/server.rb
index 1093566..49204a3 100644
--- a/server.rb
+++ b/server.rb
@@ -38,9 +38,9 @@ helpers do
   def create
     return unless @object
 
-    return if File.readlines(VISITED).collect { |l| l.chomp }.include? @object['id']
+    # return if File.readlines(VISITED).collect { |l| l.chomp }.include? @object['id']
 
-    File.open(VISITED, 'a+') { |f| f.puts @object['id'] }
+    # File.open(VISITED, 'a+') { |f| f.puts @object['id'] }
     update_collection INBOX, @object
     return unless @object['inReplyTo']
 
@@ -58,14 +58,12 @@ helpers do
   end
 
   def accept
-    return unless @object['type'] == 'Follow'
-
+    halt 501 unless @object['type'] == 'Follow'
     update_collection FOLLOWING, @object['object']
   end
 
   def undo
-    return unless @object['type'] == 'Follow'
-
+    halt 501 unless @object['type'] == 'Follow'
     update_collection FOLLOWERS, @object['actor'], true
   end