<commit_before>module(..., package.seeall)

local config = require "config"
local luasql = require "luasql.postgres"
local json = require "cjson"
local uuid = require "uuid"

local env = assert(luasql.postgres())
local conn = assert(env:connect(config.db.name, config.db.user, config.db.pass, config.db.host, config.db.port))

local function error_()
	return 400, {["Content-Type"] = "application/json"}, coroutine.wrap(function()
		coroutine.yield([[{"success": false}]])
	end)
end

function run(wsapi_env)
	local req = wsapi_env.input:read()
	print(req)
	local success, table = pcall(function() return json.decode(req) end)
	if not success then
		return error_()
	else
		local command = table.command
		local account = table.account
		local item = table.item
		if command == "enqueue" then
			local query = [[ INSERT INTO items (itemid, content, sorted, userid) VALUES ('%s', '%s', FALSE,
			(SELECT userid FROM users WHERE username = '%s')); ]]
			assert(conn:execute(query:format(uuid.new(), conn:escape(item), conn:escape(account))))
			conn:commit()
		else
			return error_()
		end

		return 200, {["Content-Type"] = "application/json"}, coroutine.wrap(function()
			coroutine.yield([[{"success": true}]])
		end)
	end
end<commit_msg>Support dequeueing; use tweed<commit_after>local config = require "config"
local luasql = require "luasql.postgres"
local uuid = require "uuid"
local tweed = require "tweed"
local inspect = require "inspect"
local cjson = require "cjson"

local env = assert(luasql.postgres())

local site = tweed.make_site {
	[tweed.string 'username'] = {
		[tweed.POST] = function(context)
			-- enqueue item
			local item = context.request.body
			local conn = assert(env:connect(config.db.name, config.db.user, config.db.pass, config.db.host, config.db.port))
			local userid = conn:execute(("SELECT userid FROM users WHERE username = '%s'"):format(conn:escape(context.params.username))):fetch()
			if not userid then
				context.response:err(404, "no such user")
				return
			end

			print (item)
			local query = [[ INSERT INTO items (itemid, content, sorted, userid) VALUES ('%s', '%s', FALSE, '%s'); ]]
			query = query:format(uuid.new(), conn:escape(item), conn:escape(userid))
			assert(conn:execute(query))
			conn:commit()
		end,
		[tweed.GET] = function(context)
			local itemid = context.request.body
			local conn = assert(env:connect(config.db.name, config.db.user, config.db.pass, config.db.host, config.db.port))
			local userid = conn:execute(("SELECT userid FROM users WHERE username = '%s'"):format(conn:escape(context.params.username))):fetch()
			if not userid then
				context.response:err(404, "no such user")
				return
			end

			local query = [[ SELECT itemid, content, sort_index FROM items WHERE userid = '%s'; ]]
			local cursor = conn:execute(query:format(userid))
			local results = {}
			for i = 1, cursor:numrows() do
				local id, content, idx = cursor:fetch()
				results[i] = {id=id, content=content, idx=idx or -1}
			end

			context.response:json(cjson.encode(results))
		end,
		[tweed.DELETE] = function(context)
			local id = context.request.body
			local conn = assert(env:connect(config.db.name, config.db.user, config.db.pass, config.db.host, config.db.port))
			local userid = conn:execute(("SELECT userid FROM users WHERE username = '%s'"):format(conn:escape(context.params.username))):fetch()
			if not userid then
				context.response:err(404, "no such user")
				return
			end

			local query = [[ DELETE FROM items WHERE itemid = '%s'; ]]
			local cursor = conn:execute(query:format(id))

			context.response:json(cjson.encode({success=true}))
		end,
	}
}

site.error_handlers[400] = function(site, ...)
	local res = context.response
	res:json(cjson.encode({success=false, err={...}}))
end

return site
{'fraction_non_alphanumeric': 0.15148305084745764, 'fraction_numerical': 0.005296610169491525, 'mean_word_length': 4.497816593886463, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '1947374', 'n_tokens_mistral': 1378, 'n_tokens_neox': 1232, 'n_words': 321}