<commit_before>require 'sinatra'
require 'sinatra/activerecord'
require 'json'
require_relative 'models/spaces'
Dir["models/*.rb"].each {|file| require_relative file }

class Spacechat < Sinatra::Base
  register Sinatra::ActiveRecordExtension

  def authorized?
    return true #for testing
    @auth ||=  Rack::Auth::Basic::Request.new(request.env)
    #TODO certainly want to change this for your specific environment
    @auth.provided? and @auth.basic? and @auth.credentials and @auth.credentials == ['admin', 'admin']
  end

  set(:auth) do |x|
    condition do
        throw :halt, [403, "Not Authorized"] if not authorized?
      end
  end

  get "/" do
    "Spacechat server ~~~>[o]>"
  end

  get "/user/:user_id/spaces" , :auth => true do
    id = params["user_id"]
    if User.exists?(id)
      User.find(id).spaces.to_json
    else
      throw :halt,  [404, "User does not exist"]
    end
  end

  get "/user/:user_id/space/:space_id" ,:auth => true do
    id, user_id = params[:space_id].to_i, params[:user_id].to_i
    if User.can_access_space?(user_id,space_id)
      Space.find(id).messages.to_json
    else
      throw :halt, [404, "User cannot access space"]
    end
  end

  post "/user/:user_id/space/:space_id" ,:auth => true do
    payload = JSON.parse(request.body.read)
    space_id, user_id = params[:space_id].to_i, params[:user_id].to_i
    unless User.can_access_space?(user_id, space_id)
      puts "whooop!"
      throw :halt, [403, "User cannot access space"]
    end
    message = Message.new(payload)
    message.space_id, message.user_id = space_id, user_id
    unless message.save
      throw :halt, [400, message.errors.messages.to_json]
    end
    message.to_json
  end

  post "/space/join" ,:auth => true do
    payload = JSON.parse(request.body.read)
    unless payload.has_key? "invite_code"<commit_msg>Get space messages fixes<commit_after>require 'sinatra'
require 'sinatra/activerecord'
require 'json'
require_relative 'models/spaces'
Dir["models/*.rb"].each {|file| require_relative file }

class Spacechat < Sinatra::Base
  register Sinatra::ActiveRecordExtension

  def authorized?
    return true #for testing
    @auth ||=  Rack::Auth::Basic::Request.new(request.env)
    #TODO certainly want to change this for your specific environment
    @auth.provided? and @auth.basic? and @auth.credentials and @auth.credentials == ['admin', 'admin']
  end

  set(:auth) do |x|
    condition do
        throw :halt, [403, "Not Authorized"] if not authorized?
      end
  end

  get "/" do
    "Spacechat server ~~~>[o]>"
  end

  get "/user/:user_id/spaces" , :auth => true do
    id = params["user_id"]
    if User.exists?(id)
      User.find(id).spaces.to_json
    else
      throw :halt,  [404, "User does not exist"]
    end
  end

  get "/user/:user_id/space/:space_id" ,:auth => true do
    space_id, user_id = params[:space_id].to_i, params[:user_id].to_i
    if User.can_access_space?(user_id,space_id)
      Space.find(space_id).messages.to_json
    else
      throw :halt, [404, "User cannot access space"]
    end
  end

  post "/user/:user_id/space/:space_id" ,:auth => true do
    payload = JSON.parse(request.body.read)
    space_id, user_id = params[:space_id].to_i, params[:user_id].to_i
    unless User.can_access_space?(user_id, space_id)
      puts "whooop!"
      throw :halt, [403, "User cannot access space"]
    end
    message = Message.new(payload)
    message.space_id, message.user_id = space_id, user_id
    unless message.save
      throw :halt, [400, message.errors.messages.to_json]
    end
    message.to_json
  end

  post "/space/join" ,:auth => true do
    payload = JSON.parse(request.body.read)
    unless payload.has_key? "invite_code"
{'fraction_non_alphanumeric': 0.13089146242930244, 'fraction_numerical': 0.008079719903043361, 'mean_word_length': 3.4004739336492893, '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': '6994060', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1257, 'n_words': 320}