<commit_before>#!/usr/bin/env ruby
#
# This is a Sinatra application for storing/retrieving comments.
#
# It was originally designed to work on the http://tweaked.io/ site
# but there is nothing specific to that site here.
#
# The only dependencies are either the Redis client libraries, and a
# Redis server running on localhost, or the SQLite libraries and a writeable
# path somewhere.
#
#
#
#
# Storing Comments
# ----------------
#   POST "body" + "author" to /comments/ID
#
#
# Retrieving Comments
# -------------------
#   GET /comments/ID -> JSON array of hashes.
#
#
# IDs
# ---
#   Arbitrary IDs are supported, so long as they are unique for each page,
#   and don't contain slashes.  Ahem.
#
#
# Deployment
# ----------
#   Run this on a host such as comments.example.com, behind nginx, or similar.
#
#   NOTE: By default the server will bind to 127.0.0.1:9393
#
#   Specify the backend to use via the environmental variable STORAGE
#
#     STORAGE=redis ./server/comments.rb
#
#    or
#
#     DB=/tmp/comments.db STORAGE=sqlite ./server/comments.rb
#
#
# Steve
# --


require 'digest/md5'
require 'getoptlong'
require 'json'
require 'redcarpet'
require 'sinatra/base'
require 'time'


#
# Our modules
#
require_relative './sinatra_helpers.rb'
require_relative './backends.rb'
require_relative './plugins.rb'


#
#  The actual Sinatra-based API.
#
class CommentStore < Sinatra::Base

  #
  # Our date/time-formatting helpers.
  #
  include Helpers;

  #
  # Backend Storage
  #
  attr_reader :storage

  #
  # Listen on 127.0.0.1:9393
  #
  set :bind, "127.0.0.1"
  set :port, 9393


  #
  # Create the backend storage object.
  #
  def initialize
    super
    @storage = Backend.create(ENV["STORAGE"] || "redis")
  end


  #
  # Posting a hash of author + body, with a given ID will
  # append a simplified version of the comment to the storage-backend.
  #
  post '/comments/:id/?' do
    response.headers['Access-Control-Allow-Origin'] = '*'

    author = params[:author]
    body   = params[:body]
    id     = params[:id]

    #
    # Explicitly alert on which parameters were missing.
    #
    if ( body.nil? || body.empty? )
      halt 500, "Missing 'body'"
    end
    if ( author.nil? || author.empty? )
      halt 500, "Missing 'author'"
    end
    if ( id.nil? || id.empty? )
      halt 500, "Missing 'ID'"
    end

    #
    #  Test for spam, via our plugins
    #
    obj = { :author => author, :body => body,
      :ip => request.ip, :site => request.host,
      :time => Time.now }

    #
    #  TODO: params.each ...
    #
    obj[:email] = params[:email] if ( params[:email] )

    #
    # Look for spam.
    #
    SpamPlugin.repository.each do |plugin|
      if plugin.is_spam? obj

        #
        # Show this rejection to the console, unless running
        # under the test-suite.
        #
        if ( ENV["TESTSUITE"] != "true" )
          puts "comment marked as spam by the plugin #{plugin.class} - #{obj.to_json}"
        end

        halt 500, "The server marked this comment as likely to be SPAM."
      end
    end

    #
    #  Add to the set.
    #
    @storage.add( id, obj.to_json )

    #
    #  All done
    #
    status 204
  end


  #
  #  Get the comments associated with a given ID, sorted by the date
  # (oldest first).
  #
  get '/comments/:id/?:sort?' do
    response.headers['Access-Control-Allow-Origin'] = '*'

    id = params[:id]

    result = Array.new()<commit_msg>Updated in anticipation of threaded-comments.<commit_after>#!/usr/bin/env ruby
#
# This is a Sinatra application for storing/retrieving comments.
#
# It was originally designed to work on the https://tweaked.io/ site
# but there is nothing specific to that site here.
#
# The only dependencies are either the Redis client libraries, and a
# Redis server running on localhost, or the SQLite libraries and a writeable
# path somewhere.
#
#
#
#
# Storing Comments
# ----------------
#   POST "body" + "author" to /comments/ID
#
#
# Retrieving Comments
# -------------------
#   GET /comments/ID -> JSON array of hashes.
#
#
# IDs
# ---
#   Arbitrary IDs are supported, so long as they are unique for each page,
#   and don't contain slashes.  Ahem.
#
#
# Deployment
# ----------
#   Run this on a host such as comments.example.com, behind nginx, or similar.
#
#   NOTE: By default the server will bind to 127.0.0.1:9393
#
#   Specify the backend to use via the environmental variable STORAGE
#
#     STORAGE=redis ./server/comments.rb
#
#    or
#
#     DB=/tmp/comments.db STORAGE=sqlite ./server/comments.rb
#
#
# Steve
# --


require 'digest/md5'
require 'getoptlong'
require 'json'
require 'redcarpet'
require 'sinatra/base'
require 'time'
require 'uuidtools'


#
# Our modules
#
require_relative './sinatra_helpers.rb'
require_relative './backends.rb'
require_relative './plugins.rb'


#
#  The actual Sinatra-based API.
#
class CommentStore < Sinatra::Base

  #
  # Our date/time-formatting helpers.
  #
  include Helpers;

  #
  # Backend Storage
  #
  attr_reader :storage

  #
  # Listen on 127.0.0.1:9393
  #
  set :bind, "127.0.0.1"
  set :port, 9393


  #
  # Create the backend storage object.
  #
  def initialize
    super
    @storage = Backend.create(ENV["STORAGE"] || "redis")
  end


  #
  # Posting a hash of author + body, with a given ID will
  # append a simplified version of the comment to the storage-backend.
  #
  post '/comments/:id/?' do
    response.headers['Access-Control-Allow-Origin'] = '*'

    author = params[:author]
    body   = params[:body]
    id     = params[:id]

    #
    # Explicitly alert on which parameters were missing.
    #
    if ( body.nil? || body.empty? )
      halt 500, "Missing 'body'"
    end
    if ( author.nil? || author.empty? )
      halt 500, "Missing 'author'"
    end
    if ( id.nil? || id.empty? )
      halt 500, "Missing 'ID'"
    end

    #
    #  Test for spam, via our plugins
    #
    obj = { :author => author, :body => body,
      :ip => request.ip, :site => request.host,
      :time => Time.now }

    #
    #  TODO: params.each ...
    #
    #  Add the email and parent, if they were supplied.
    #
    obj[:email]  = params[:email] if ( params[:email] )
    obj[:parent] = params[:parent] if ( params[:parent] )

    #
    # Look for spam.
    #
    SpamPlugin.repository.each do |plugin|
      if plugin.is_spam? obj

        #
        # Show this rejection to the console, unless running
        # under the test-suite.
        #
        if ( ENV["TESTSUITE"] != "true" )
          puts "comment marked as spam by the plugin #{plugin.class} - #{obj.to_json}"
        end

        halt 500, "The server marked this comment as likely to be SPAM."
      end
    end

    #
    #  Ensure that each submission has a UUID
    #
    obj[:uuid] = UUIDTools::UUID.random_create

    #
    #  Add to the set.
    #
    @storage.add( id, obj.to_json )

    #
    #  All done
    #
    status 204
  end


  #
  #  Get the comments associated with a given ID, sorted by the date
  # (oldest first).
  #
  get '/comments/:id/?:sort?' do
    response.headers['Access-Control-Allow-Origin'] = '*'

    id = params[:id]

    result = Array.new()
{'fraction_non_alphanumeric': 0.13381858902575589, 'fraction_numerical': 0.012877939529675251, 'mean_word_length': 2.3450374531835205, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, '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': '2596758', 'n_tokens_mistral': 2477, 'n_tokens_neox': 2342, 'n_words': 729}