diff options
author | pdp8 <pdp8@pdp8.info> | 2024-05-03 11:57:42 +0200 |
---|---|---|
committer | pdp8 <pdp8@pdp8.info> | 2024-05-03 11:57:42 +0200 |
commit | ffade03e9d3f77e41f44882c39eb6b12b3b05e63 (patch) | |
tree | 7cab80e5760996ca74e883d4794992d75a74e8b3 | |
parent | 97d98fd5481359b96215f8a50d032c8fe96e9add (diff) |
storage paths from id
-rw-r--r-- | application.rb | 7 | ||||
-rw-r--r-- | client.rb | 5 | ||||
-rw-r--r-- | helpers.rb | 30 | ||||
-rw-r--r-- | server.rb | 50 |
4 files changed, 45 insertions, 47 deletions
diff --git a/application.rb b/application.rb index 39e47c7..5b47935 100644 --- a/application.rb +++ b/application.rb @@ -19,11 +19,12 @@ WEBFINGER = File.join(PUBLIC_DIR, MENTION + '.json') SOCIAL_URL = "https://#{SOCIAL_DOMAIN}" ACTOR = File.join(SOCIAL_URL, USER) -INBOX = { dir: File.join(SOCIAL_DIR, 'inbox') } -OUTBOX = { dir: File.join(SOCIAL_DIR, 'outbox'), url: File.join(SOCIAL_URL, 'outbox') } +INBOX_DIR = File.join(SOCIAL_DIR, 'inbox') +OUTBOX_DIR = File.join(SOCIAL_DIR, 'outbox') +OUTBOX_URL = File.join(SOCIAL_URL, 'outbox') TAGS = { dir: File.join(PUBLIC_DIR, 'tags'), url: File.join(SOCIAL_URL, 'tags') } FOLLOWERS_URL = 'https://social.pdp8.info/followers' -VISITED = File.join(INBOX[:dir], 'visited') +VISITED = File.join(INBOX_DIR, 'visited') ACTIVITIES = %i[create announce follow accept undo delete like update move] CONTENT_TYPE = 'application/activity+json' diff --git a/client.rb b/client.rb index 891a1d8..c47000d 100644 --- a/client.rb +++ b/client.rb @@ -10,7 +10,7 @@ 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) } + 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', @@ -38,7 +38,8 @@ post '/undo' do # TODO: generalize for announce activity = JSON.load_file(activity_file) next unless activity['id'] == params['id'] - object_file, object = find_object activity['object']['id'] + object_file = File.join(dir, activity['object']['id'].sub('https://', '')) + # object_file, object = find_object activity['object']['id'] create_activity 'Undo', params['id'], activity['to'] FileUtils.rm(activity_file) FileUtils.rm(object_file) diff --git a/helpers.rb b/helpers.rb index 7126160..c2b0136 100644 --- a/helpers.rb +++ b/helpers.rb @@ -1,6 +1,9 @@ require 'English' helpers do - def save_item(item, path) + def save_item(item, dir) + return unless @object['id'] + + path = File.join(dir, @object['id'].sub('https://', '')) FileUtils.mkdir_p File.dirname(path) File.open(path, 'w+') { |f| f.puts item.to_json } end @@ -10,7 +13,7 @@ helpers do rel_path = File.join(type.downcase, "#{date}.json") activity = { '@context' => 'https://www.w3.org/ns/activitystreams', - 'id' => File.join(OUTBOX[:url], rel_path), + 'id' => File.join(OUTBOX_URL, rel_path), 'type' => type, 'actor' => ACTOR, 'published' => date, @@ -22,9 +25,9 @@ helpers do object_rel_path = File.join('object', object['type'].downcase, "#{date}.json") object = activity['object'] object['@context'] = 'https://www.w3.org/ns/activitystreams' - object['id'] = File.join(OUTBOX[:url], object_rel_path) + object['id'] = File.join(OUTBOX_URL, object_rel_path) object['published'] = date - save_item activity['object'], File.join(OUTBOX[:dir], object_rel_path) + save_item activity['object'], OUTBOX_DIR if object['tag'] object['tag'].each do |tag| next unless tag['type'] == 'Hashtag' @@ -49,9 +52,8 @@ helpers do end end end - activity_path = File.join(OUTBOX[:dir], rel_path) - save_item activity, activity_path - send_activity activity, activity_path + save_item activity, OUTBOX_DIR + send_activity activity, File.join(OUTBOX_DIR, rel_path) end def send_activity(activity, activity_path) @@ -209,12 +211,14 @@ helpers do "#{type[0]}/#{ext}" end - def find_object(id) - Dir[File.join('*', '**', '*.json')].each do |file| - object = JSON.load_file(file) - return [file, object] if object['id'] == id - end - end + # def find_object(id) + # Dir[File.join('*', '**', '*.json')].each do |file| + # object = JSON.load_file(file) + # return [file, object] if object['id'] == id + # rescue JSON::ParserError + # puts "Invalid JSON in #{file}" + # end + # end def outbox_html(activity) html = File.read('/home/ch/src/publish/html/head.html') diff --git a/server.rb b/server.rb index 9197ce5..b202624 100644 --- a/server.rb +++ b/server.rb @@ -62,7 +62,18 @@ helpers do def create @count ||= 0 @object ||= @activity['object'] - save_inbox_object + @object = @object['object'] if @object['type'] and ACTIVITIES.include? @object['type'].downcase.to_sym # lemmy + return unless @object and @object['type'] != 'Person' + + @object = fetch(@object) if @object.is_a? String and @object.match(/^http/) + return unless @object + + if @object['id'] and File.readlines(VISITED, chomp: true).include? @object['id'] && !(@activity['type'] = 'Update') + return + end + + save_item @object, INBOX_DIR + File.open(VISITED, 'a+') { |f| f.puts @object['id'] } return unless @object and @object['inReplyTo'] and @count < 5 return if File.readlines(VISITED, chomp: true).include?(@object['inReplyTo']) @@ -88,13 +99,13 @@ helpers do end def follow - save_item @activity, File.join(INBOX[:dir], @activity['type'].downcase, activity_name) + # save_item @activity, File.join(INBOX[:dir], @activity['type'].downcase, activity_name) update_collection FOLLOWERS, @activity['actor'] create_activity 'Accept', @activity, [@activity['actor']] end def accept - save_item @activity, File.join(INBOX[:dir], @activity['type'].downcase, activity_name) + # save_item @activity, File.join(INBOX[:dir], @activity['type'].downcase, activity_name) if @activity['object']['type'] == 'Follow' update_collection FOLLOWING, @activity['object']['object'] else @@ -109,8 +120,9 @@ helpers do when 'Follow' update_collection FOLLOWERS, @activity['object']['actor'], 'delete' when 'Create', 'Announce' - file, object = find_object @activity['object']['object'] - FileUtils.rm(file) if file and File.exist? file and @activity['actor'] == object['attributedTo'] + file = File.join(dir, @activity['object']['id'].sub('https://', '')) + # file, object = find_object @activity['object']['object'] + FileUtils.rm(file) if file and File.exist? file # and @activity['actor'] == object['attributedTo'] else p "Error: Cannot undo @activity['object']['type']" jj @activity @@ -124,8 +136,9 @@ helpers do end def delete - file, object = find_object(@activity['object']['id']) - FileUtils.rm(file) if file and File.exist? file and @activity['actor'] == object['attributedTo'] + file = File.join(dir, @activity['object']['id'].sub('https://', '')) + # file, object = find_object(@activity['object']['id']) + FileUtils.rm(file) if file and File.exist? file # and @activity['actor'] == @activity['object']['attributedTo'] end def move @@ -134,7 +147,7 @@ helpers do def handle_activity type = @activity['type'].downcase.to_sym - save_item @activity, File.join(INBOX[:dir], @activity['type'].downcase, activity_name) + # save_item @activity, File.join(INBOX[:dir], @activity['type'].downcase, activity_name) if ACTIVITIES.include? type send(type) else @@ -149,27 +162,6 @@ helpers do @activity['published'] ? "#{@activity['published']}_#{mention(@activity['actor'])}.json" : "#{Time.now.utc.iso8601}_#{mention(@activity['actor'])}.json" end - def save_inbox_object - @object = @object['object'] if @object['type'] and ACTIVITIES.include? @object['type'].downcase.to_sym # lemmy - return unless @object and @object['type'] != 'Person' - - @object = fetch(@object) if @object.is_a? String and @object.match(/^http/) - return unless @object - - # if @activity['type'] != 'Update' && (@object['id'] and File.readlines(VISITED, chomp: true).include? @object['id']) - # return - # end - if @object['id'] and File.readlines(VISITED, chomp: true).include? @object['id'] && !(@activity['type'] = 'Update') - return - # - # FileUtils.rm_f find_object(@object['id'])[0] - # - end - - save_item @object, File.join(INBOX[:dir], 'object', @object['type'].downcase, activity_name) - File.open(VISITED, 'a+') { |f| f.puts @object['id'] } - end - def outbox(activity) Dir[File.join('outbox', activity, '*.json')].collect do |f| JSON.load_file(f) |