A simple Ruby server using Sinatra that serves Bluesky custom feeds

add option of context and reason fields for posts

+44 -5
+44 -5
lib/blue_factory/server.rb
··· 64 64 65 65 posts = response[:posts] 66 66 raise InvalidResponseError, ":posts key is missing" unless response.has_key?(:posts) 67 - raise InvalidResponseError, ":posts should be an array of strings" unless posts.is_a?(Array) 68 - raise InvalidResponseError, ":posts should be an array of strings" unless posts.all? { |x| x.is_a?(String) } 67 + raise InvalidResponseError, ":posts should be an array" unless posts.is_a?(Array) 68 + # raise InvalidResponseError, ":posts should be an array of strings" unless posts.all? { |x| x.is_a?(String) } 69 + # 70 + # if bad_uri = posts.detect { |x| x !~ AT_URI_REGEXP } 71 + # raise InvalidResponseError, "Invalid post URI: #{bad_uri}" 72 + # end 73 + end 74 + 75 + def process_post_object(object) 76 + if object.is_a?(String) 77 + return { post: object } 78 + end 79 + 80 + post = {} 81 + 82 + if object[:post] 83 + post[:post] = object[:post] 84 + else 85 + raise InvalidResponseError, "post hash is missing a :post key" 86 + end 87 + 88 + if object[:reason] 89 + post[:reason] = process_post_reason(object[:reason]) 90 + end 91 + 92 + if object[:context] 93 + post[:feedContext] = object[:context] 94 + end 95 + 96 + post 97 + end 69 98 70 - if bad_uri = posts.detect { |x| x !~ AT_URI_REGEXP } 71 - raise InvalidResponseError, "Invalid post URI: #{bad_uri}" 99 + def process_post_reason(reason) 100 + if reason[:repost] 101 + { 102 + "$type" => "app.bsky.feed.defs#skeletonReasonRepost", 103 + "repost" => reason[:repost] 104 + } 105 + elsif reason[:pin] 106 + { 107 + "$type" => "app.bsky.feed.defs#skeletonReasonPin" 108 + } 109 + else 110 + raise InvalidResponseError, "invalid post reason: #{reason.inspect}" 72 111 end 73 112 end 74 113 end ··· 94 133 validate_response(response) if config.validate_responses 95 134 96 135 output = {} 97 - output[:feed] = response[:posts].map { |s| { post: s }} 136 + output[:feed] = response[:posts].map { |x| process_post_object(x) } 98 137 output[:cursor] = response[:cursor] if response[:cursor] 99 138 output[:reqId] = response[:req_id] if response[:req_id] 100 139