<commit_before>module TentD
  class API

    class LookupPost < Middleware
      def action(env)
        params = env['params']
        request_proxy_manager = env['request_proxy_manager']

        proxy_condition = if (params[:entity] == env['current_user'].entity) || (env['REQUEST_METHOD'] != 'GET')
          :never
        else
          request_proxy_manager.proxy_condition
        end

        post = unless proxy_condition == :always
          if params['version'] && params['version'] != 'latest'
            Model::Post.first(:public_id => params[:post], :entity => params[:entity], :version => params['version'])
          else
            Model::Post.where(:public_id => params[:post], :entity => params[:entity]).order(Sequel.desc(:version_received_at)).first
          end
        end

        if !post && proxy_condition != :never
          # proxy request
          proxy_client = request_proxy_manager.proxy_client(params[:entity], :skip_response_serialization => true)

          begin
            res = proxy_client.post.get(params[:entity], params[:post])

            body = res.body.respond_to?(:each) ? res.body : [res.body]
            return [res.status, res.headers, body]
          rescue Faraday::Error::TimeoutError
            halt!(504, "Failed to proxy request: #{res.env[:method].to_s.upcase} #{res.env[:url].to_s}")
          rescue Faraday::Error::ConnectionFailed
            halt!(502, "Failed to proxy request: #{res.env[:method].to_s.upcase} #{res.env[:url].to_s}")
          end
        else
          env['response.post'] = post
        end<commit_msg>Fix edge case for lookup post proxy<commit_after>module TentD
  class API

    class LookupPost < Middleware
      def action(env)
        params = env['params']
        request_proxy_manager = env['request_proxy_manager']

        proxy_condition = if (params[:entity] == env['current_user'].entity) || (env['REQUEST_METHOD'] != 'GET')
          :never
        else
          request_proxy_manager.proxy_condition
        end

        post = unless proxy_condition == :always
          if params['version'] && params['version'] != 'latest'
            Model::Post.first(:public_id => params[:post], :entity => params[:entity], :version => params['version'])
          else
            Model::Post.where(:public_id => params[:post], :entity => params[:entity]).order(Sequel.desc(:version_received_at)).first
          end
        end

        if !post && proxy_condition != :never
          # proxy request
          proxy_client = request_proxy_manager.proxy_client(params[:entity], :skip_response_serialization => true)

          begin
            res = proxy_client.post.get(params[:entity], params[:post])

            body = res.body.respond_to?(:each) ? res.body : [res.body]
            return [res.status, res.headers, body]
          rescue Faraday::Error::TimeoutError
            res ||= Faraday::Response.new({})
            halt!(504, "Failed to proxy request: #{res.env[:method].to_s.upcase} #{res.env[:url].to_s}")
          rescue Faraday::Error::ConnectionFailed
            res ||= Faraday::Response.new({})
            halt!(502, "Failed to proxy request: #{res.env[:method].to_s.upcase} #{res.env[:url].to_s}")
          end
        else
          env['response.post'] = post
        end
{'fraction_non_alphanumeric': 0.14173228346456693, 'fraction_numerical': 0.0036341611144760752, 'mean_word_length': 2.5708108108108108, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2258279', 'n_tokens_mistral': 931, 'n_tokens_neox': 910, 'n_words': 187}