Renamed modpol/modpol directory to modpol_core for clarity and consistency
This commit is contained in:
318
modpol_core/orgs/base.lua
Normal file
318
modpol_core/orgs/base.lua
Normal file
@ -0,0 +1,318 @@
|
||||
--- Orgs: Base
|
||||
-- Basic functions for orgs
|
||||
|
||||
modpol.orgs = modpol.orgs or
|
||||
{
|
||||
count = 1,
|
||||
array = {}
|
||||
}
|
||||
|
||||
-- sets modpol.orgs as its own fallback
|
||||
modpol.orgs.__index = modpol.orgs
|
||||
|
||||
function temp_org()
|
||||
return {
|
||||
id = nil,
|
||||
name = nil,
|
||||
modules = modpol.modules,
|
||||
processes = {},
|
||||
pending = {},
|
||||
members = {},
|
||||
parent = nil,
|
||||
children = {}
|
||||
}
|
||||
end
|
||||
|
||||
-- ==================================================
|
||||
-- returns org when given its id or name
|
||||
function modpol.orgs.get_org(arg)
|
||||
if type(arg) == 'string' then
|
||||
for id, org in ipairs(modpol.orgs.array) do
|
||||
if org.name == arg then
|
||||
return org
|
||||
end
|
||||
end
|
||||
elseif type(arg) == 'number' then
|
||||
return modpol.orgs.array[arg]
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- ===============================================
|
||||
-- returns a table list of all org names
|
||||
function modpol.orgs.list_all()
|
||||
local org_table
|
||||
for k, v in ipairs(modpol.orgs.array) do
|
||||
if type(v) == 'table' then
|
||||
if org_table then
|
||||
table.insert(org_table, v.name)
|
||||
else
|
||||
org_table = {v.name}
|
||||
end
|
||||
end
|
||||
end
|
||||
return org_table
|
||||
end
|
||||
|
||||
-- Function: modpol.orgs.user_orgs(user)
|
||||
-- input: user (string)
|
||||
-- output: table of strings of org names
|
||||
function modpol.orgs.user_orgs(user)
|
||||
local all_orgs = modpol.orgs.list_all()
|
||||
local user_orgs = {}
|
||||
for i,v in ipairs(all_orgs) do
|
||||
local this_table = modpol.orgs.get_org(v)
|
||||
if this_table:has_member(user) then
|
||||
table.insert(user_orgs,v)
|
||||
end
|
||||
end
|
||||
return user_orgs
|
||||
end
|
||||
|
||||
-- ===========================================
|
||||
-- deletes all orgs except for the instance
|
||||
function modpol.orgs.reset()
|
||||
for id, org in ipairs(modpol.orgs.array) do
|
||||
if id > 1 then
|
||||
modpol.orgs.array[id] = "removed"
|
||||
end
|
||||
end
|
||||
|
||||
modpol.orgs.array[1] = nil
|
||||
modpol.instance = modpol.orgs.init_instance()
|
||||
|
||||
|
||||
modpol.ocutil.log('Reset all orgs')
|
||||
modpol.orgs:record('Resetting all orgs', 'org_reset')
|
||||
end
|
||||
|
||||
-- ===================================================
|
||||
-- initializes the instance (root org)
|
||||
-- can only be run once, as only one instance can exist
|
||||
function modpol.orgs.init_instance()
|
||||
local error_msg
|
||||
if modpol.orgs.array[1] then
|
||||
modpol.ocutil.log('Error in orgs.init_instance -> instance has already been initialized')
|
||||
return false
|
||||
end
|
||||
|
||||
local instance = temp_org()
|
||||
instance.id = 1
|
||||
instance.name = "root"
|
||||
|
||||
setmetatable(instance, modpol.orgs)
|
||||
|
||||
-- adding instance to org list
|
||||
modpol.orgs.array[1] = instance
|
||||
|
||||
modpol.ocutil.log('Initialized the instance root org')
|
||||
modpol.orgs:record('Initialized the instance root org', 'create_instance')
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
|
||||
-- FUNCTIONS BEYOND HERE OPERATE ON ORG OBJECTS
|
||||
|
||||
-- =======================================================
|
||||
-- records a log message to the modpol ledger
|
||||
function modpol.orgs:record(msg, entry_type)
|
||||
local entry = {
|
||||
timestamp = '',
|
||||
entry_type = nil,
|
||||
action_msg = '',
|
||||
org_name = '',
|
||||
org_id = nil,
|
||||
}
|
||||
|
||||
if type(msg) == 'string' and not(modpol.ocutil.str_empty(msg)) then
|
||||
entry.action_msg = msg
|
||||
else
|
||||
modpol.ocutil.log('Error in ' .. self.name .. ':record -> msg must be a non empty string')
|
||||
return false
|
||||
end
|
||||
|
||||
if type(entry_type) == 'string' and not(modpol.ocutil.str_empty(entry_type)) then
|
||||
entry.entry_type = entry_type
|
||||
else
|
||||
modpol.ocutil.log('Error in ' .. self.name .. ':record -> entry_type must be a non empty string')
|
||||
modpol.ocutil.log(msg, entry_type)
|
||||
return false
|
||||
end
|
||||
|
||||
entry.timestamp = os.time()
|
||||
entry.org_id = self.id
|
||||
entry.org_name = self.name
|
||||
|
||||
table.insert(modpol.ledger, entry)
|
||||
modpol.store_data()
|
||||
end
|
||||
|
||||
-- ==================================================
|
||||
-- adds a new sub org to the org it is called on
|
||||
-- input: name (string), user (string)
|
||||
-- ex: instance:add_org('town hall')
|
||||
function modpol.orgs:add_org(name, user)
|
||||
if self.id == nil then
|
||||
modpol.ocutil.log('Error in ' .. self.name .. ':add_org -> add_org can only be called by another org')
|
||||
return false
|
||||
end
|
||||
|
||||
if modpol.ocutil.str_empty(name) then
|
||||
modpol.ocutil.log('Error in ' .. self.name .. ':add_org -> org name is required')
|
||||
return false
|
||||
end
|
||||
|
||||
if modpol.orgs.get_org(name) then
|
||||
modpol.ocutil.log('Error in ' .. self.name .. ':add_org -> org name is already being used')
|
||||
return false
|
||||
end
|
||||
|
||||
-- creating the child sub org
|
||||
modpol.orgs.count = modpol.orgs.count + 1
|
||||
local child_org = temp_org()
|
||||
child_org.id = modpol.orgs.count
|
||||
child_org.name = name
|
||||
child_org.parent = self.id
|
||||
child_org.processes = {}
|
||||
child_org.modules = self.modules
|
||||
|
||||
setmetatable(child_org, modpol.orgs)
|
||||
|
||||
-- adding child id to list of children
|
||||
table.insert(self.children, child_org.id)
|
||||
|
||||
-- adding child to org list
|
||||
modpol.orgs.array[child_org.id] = child_org
|
||||
|
||||
-- adding creator of org as the first member
|
||||
child_org:add_member(user)
|
||||
|
||||
self:record('created sub org ' .. name, 'add_org')
|
||||
modpol.ocutil.log('Created ' .. name .. ' (suborg of ' .. self.name .. ')')
|
||||
|
||||
return child_org
|
||||
end
|
||||
|
||||
-- ========================================
|
||||
-- recursively deletes an org and its suborgs
|
||||
-- leaves entry in modpol.orgs.array as a string "removed"
|
||||
-- note: "reason" param was removed, can be added back
|
||||
function modpol.orgs:delete()
|
||||
if self.id == 1 then
|
||||
modpol.ocutil.log('Error in ' .. self.name .. ':delete -> cannot delete instance')
|
||||
return false
|
||||
end
|
||||
|
||||
if #self.children > 0 then
|
||||
for i, child_id in pairs(self.children) do
|
||||
local child = modpol.orgs.get_org(child_id)
|
||||
modpol.ocutil.log(child_id, child)
|
||||
child:delete()
|
||||
end
|
||||
end
|
||||
|
||||
modpol.orgs.array[self.id] = 'removed'
|
||||
modpol.ocutil.log('Deleted org ' .. self.name .. ': ' .. self.id)
|
||||
|
||||
self:record('Deleted ' .. self.name .. ' and all child orgs', 'del_org')
|
||||
|
||||
end
|
||||
|
||||
|
||||
-- ===========================================
|
||||
-- internal function to get the index of a member name
|
||||
function modpol.orgs:get_member_index(member)
|
||||
for k, v in ipairs(self.members) do
|
||||
if v == member then
|
||||
return k
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- ===========================================
|
||||
-- adds a user to an org
|
||||
function modpol.orgs:add_member(user)
|
||||
for id, name in ipairs(self.members) do
|
||||
if user == name then
|
||||
modpol.ocutil.log('Error in ' .. self.name .. ':add_member -> user already in org')
|
||||
return false
|
||||
end
|
||||
end
|
||||
-- trys to fill in empty spots first
|
||||
local empty_index = self:get_member_index('')
|
||||
if empty_index then
|
||||
self.members[empty_index] = user
|
||||
else
|
||||
-- adds to end if no empty spots
|
||||
table.insert(self.members, user)
|
||||
end
|
||||
|
||||
modpol.ocutil.log('Added member ' .. user .. ' to ' .. self.name)
|
||||
self:record('Added member ' .. user, 'add_member')
|
||||
|
||||
end
|
||||
|
||||
-- =======================================
|
||||
-- removes a user from an org
|
||||
function modpol.orgs:remove_member(user)
|
||||
-- sets the array index to an empty string so that consecutive list is preserved
|
||||
-- empty spots will get filled in by new members
|
||||
local user_index = self:get_member_index(user)
|
||||
if user_index then
|
||||
self.members[user_index] = ''
|
||||
else
|
||||
modpol.ocutil.log('Error in ' .. self.name .. ':remove_member -> user not in org')
|
||||
end
|
||||
modpol.ocutil.log('Removed member ' .. user .. ' from ' .. self.name)
|
||||
self:record('Removed member ' .. user, 'del_member')
|
||||
end
|
||||
|
||||
-- ===========================================
|
||||
-- boolean check whether user is an org
|
||||
function modpol.orgs:has_member(user)
|
||||
local user_index = self:get_member_index(user)
|
||||
if user_index then
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
-- ==================================
|
||||
-- Function: modpol.orgs:list_members()
|
||||
-- output: a table of the names (string) of members
|
||||
function modpol.orgs:list_members()
|
||||
local members = {}
|
||||
for k, v in ipairs(self.members) do
|
||||
table.insert(members, v)
|
||||
end
|
||||
return members
|
||||
end
|
||||
|
||||
-- ==============================
|
||||
-- because member list uses lazy deletion, using #org.members will not show an accurate number
|
||||
function modpol.orgs:get_member_count()
|
||||
local count = 0
|
||||
for k, v in ipairs(self.members) do
|
||||
-- the empty string represents a deleted member in the members list
|
||||
if v ~= '' then
|
||||
count = count + 1
|
||||
end
|
||||
end
|
||||
return count
|
||||
end
|
||||
-- ====================================
|
||||
-- adds a new policy to the policy table
|
||||
-- must define the policy type, process associated with it, and whether the request must be made by an org member
|
||||
function modpol.orgs:set_policy(policy_type, process_type, must_be_member)
|
||||
local new_policy = {
|
||||
process_type = process_type,
|
||||
must_be_member = must_be_member
|
||||
}
|
||||
self.policies[policy_type] = new_policy
|
||||
modpol.ocutil.log('Added policy for ' .. policy_type .. ' in ' .. self.name)
|
||||
self:record('Added policy for ' .. policy_type, 'set_policy')
|
||||
end
|
||||
|
||||
|
98
modpol_core/orgs/consent.lua
Normal file
98
modpol_core/orgs/consent.lua
Normal file
@ -0,0 +1,98 @@
|
||||
-- TODO: NEEDS TO BE REWRITTEN AS A LIBRARY NOT A MODULE
|
||||
|
||||
modpol.orgs.consent = {}
|
||||
|
||||
-- sets consent to its own callback
|
||||
modpol.orgs.consent.__index = modpol.orgs.consent
|
||||
|
||||
function temp_consent_process()
|
||||
return {
|
||||
type = "consent",
|
||||
id = nil,
|
||||
org_id = nil,
|
||||
request_id = nil,
|
||||
total_votes = 0,
|
||||
majority_to_pass = 0.51,
|
||||
votes_needed = nil,
|
||||
votes_yes = {},
|
||||
votes_no = {}
|
||||
}
|
||||
end
|
||||
|
||||
-- ===============================================
|
||||
-- function to create a new consent process to resolve a pending process
|
||||
function modpol.orgs.consent:new_process(id, request_id, org_id)
|
||||
local process = temp_consent_process()
|
||||
process.id = id
|
||||
process.request_id = request_id
|
||||
process.org_id = org_id
|
||||
|
||||
setmetatable(process, modpol.orgs.consent)
|
||||
modpol.ocutil.log('Created new process #' .. id .. ' for request id #' .. request_id)
|
||||
|
||||
local p_org = modpol.orgs.get_org(org_id)
|
||||
|
||||
for i, member in ipairs(p_org.members) do
|
||||
p_org:add_pending_action(id, member)
|
||||
end
|
||||
|
||||
process.votes_needed = math.ceil(process.majority_to_pass * p_org:get_member_count())
|
||||
|
||||
return process
|
||||
end
|
||||
|
||||
-- ============================
|
||||
-- interact function for the consent module
|
||||
-- input: user (string)
|
||||
function modpol.orgs.consent:interact(user)
|
||||
-- TODO this needs more context on the vote at hand
|
||||
modpol.interactions.binary_poll_user(
|
||||
user, "Do you consent?",
|
||||
function(vote)
|
||||
if vote == 'Yes' then
|
||||
self:approve(user, true)
|
||||
elseif vote == 'No' then
|
||||
self:approve(user, false)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
-- ======================================================
|
||||
-- function for users to vote on a pending request
|
||||
function modpol.orgs.consent:approve(user, decision)
|
||||
if not modpol.orgs.get_org(self.org_id):has_member(user) then
|
||||
modpol.ocutil.log('Error in consent:approve -> user not a member of the org')
|
||||
return
|
||||
end
|
||||
|
||||
if decision then
|
||||
table.insert(self.votes_yes, user)
|
||||
modpol.ocutil.log('User ' .. user .. ' voted yes on request #' .. self.request_id)
|
||||
else
|
||||
table.insert(self.votes_no, user)
|
||||
modpol.ocutil.log('User ' .. user .. ' voted no on request #' .. self.request_id)
|
||||
end
|
||||
|
||||
self.total_votes = self.total_votes + 1
|
||||
|
||||
local p_org = modpol.orgs.get_org(self.org_id)
|
||||
p_org:remove_pending_action(self.id, user)
|
||||
|
||||
self:update_status()
|
||||
end
|
||||
|
||||
-- ===================================================
|
||||
-- determines whether process has finished and resolves request if it has (unfinished)
|
||||
function modpol.orgs.consent:update_status()
|
||||
local process_org = modpol.orgs.get_org(self.org_id)
|
||||
|
||||
if #self.votes_yes >= self.votes_needed then
|
||||
modpol.ocutil.log('Request #' .. self.request_id .. ' passes')
|
||||
process_org:resolve_request(self.request_id, true)
|
||||
elseif #self.votes_no >= self.votes_needed then
|
||||
modpol.ocutil.log('Request #' .. self.request_id .. ' fails to pass')
|
||||
process_org:resolve_request(self.request_id, false)
|
||||
else
|
||||
modpol.ocutil.log('Waiting for more votes...')
|
||||
end
|
||||
end
|
0
modpol_core/orgs/defer.lua
Normal file
0
modpol_core/orgs/defer.lua
Normal file
48
modpol_core/orgs/example.lua
Normal file
48
modpol_core/orgs/example.lua
Normal file
@ -0,0 +1,48 @@
|
||||
--[[
|
||||
modpol calls this function when someone starts a new request for this module
|
||||
--]]
|
||||
|
||||
function module:initiate(org) {
|
||||
|
||||
form = {
|
||||
{
|
||||
"name": "to_remove",
|
||||
"display": "Which user should be removed?",
|
||||
"type": "drop-down",
|
||||
"values": org:list_members()
|
||||
},
|
||||
{
|
||||
"name": "reason",
|
||||
"type": "text-box",
|
||||
"prompt": "Reason for removing member:"
|
||||
}
|
||||
}
|
||||
|
||||
return form
|
||||
}
|
||||
|
||||
--[[
|
||||
modpol prompts the user with this form, and after receiving the data asynchronously
|
||||
the returned form would look like:
|
||||
|
||||
{
|
||||
"to_remove": luke,
|
||||
"reason": stealing food
|
||||
}
|
||||
|
||||
based on provided "name" fields and the input given by the user
|
||||
now module:request is called
|
||||
--]]
|
||||
|
||||
function module:request(form) {
|
||||
self.data = form
|
||||
}
|
||||
|
||||
--[[
|
||||
after the module request function runs, modpol will initiate the consent process
|
||||
if consent is approved, the implement function is called
|
||||
--]]
|
||||
|
||||
function module:implement() {
|
||||
org:remove_member(self.data.to_remove)
|
||||
}
|
102
modpol_core/orgs/process.lua
Normal file
102
modpol_core/orgs/process.lua
Normal file
@ -0,0 +1,102 @@
|
||||
--- Process functions for orgs
|
||||
|
||||
function modpol.orgs:call_module(module_slug, initiator, config, result)
|
||||
if not modpol.modules[module_slug] then
|
||||
modpol.ocutil.log('Error in ' .. self.name .. ':call_module -> module "' .. module_slug .. '" not found')
|
||||
return
|
||||
end
|
||||
|
||||
local empty_index = nil
|
||||
-- linear search for empty process slots (lazy deletion)
|
||||
for k, v in ipairs(self.processes) do
|
||||
if v == 'deleted' then
|
||||
empty_index = k
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
local index
|
||||
-- attempts to fill empty spots in list, otherwise appends to end
|
||||
if empty_index then
|
||||
index = empty_index
|
||||
else
|
||||
index = #self.processes + 1
|
||||
end
|
||||
|
||||
local module = modpol.modules[module_slug]
|
||||
|
||||
-- sets default values for undeclared config variables
|
||||
if #module.config > 0 then
|
||||
for k, v in pairs(module.config) do
|
||||
if config[k] == nil then
|
||||
config[k] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- setting default params
|
||||
local new_process = {
|
||||
metatable = {__index = module},
|
||||
initiator = initiator,
|
||||
org = self,
|
||||
id = index,
|
||||
config = config,
|
||||
data = module.data,
|
||||
slug = module_slug
|
||||
}
|
||||
|
||||
setmetatable(new_process, new_process.metatable)
|
||||
|
||||
self.processes[index] = new_process
|
||||
self.processes[index]:initiate(result)
|
||||
|
||||
return index
|
||||
end
|
||||
|
||||
function modpol.orgs:delete_process(id)
|
||||
self.processes[id] = 'deleted'
|
||||
end
|
||||
|
||||
function modpol.orgs:add_pending_action(process_id, user, callback)
|
||||
self.pending[user] = self.pending[user] or {}
|
||||
self.pending[user][process_id] = callback
|
||||
end
|
||||
|
||||
function modpol.orgs:remove_pending_action(process_id, user)
|
||||
if self.pending[user] then
|
||||
self.pending[user][process_id] = nil
|
||||
end
|
||||
end
|
||||
|
||||
function modpol.orgs:wipe_pending_actions(process_id)
|
||||
for user in pairs(self.pending) do
|
||||
self.pending[user][process_id] = nil
|
||||
end
|
||||
end
|
||||
|
||||
function modpol.orgs:has_pending_actions(user)
|
||||
-- next() will return the next pair in a table
|
||||
-- if next() returns nil, the table is empty
|
||||
if not self.pending[user] then
|
||||
return false
|
||||
else
|
||||
if not next(self.pending[user]) then
|
||||
return false
|
||||
else
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function modpol.orgs:interact(process_id, user)
|
||||
local process = self.processes[process_id]
|
||||
modpol.interactions.message(user,"hi!")
|
||||
if self.pending[user] then
|
||||
modpol.interactions.message(user,"id: "..process_id)
|
||||
local callback = self.pending[user][process_id]
|
||||
if callback then
|
||||
modpol.interactions.message(user,"la!")
|
||||
process[callback](process, user)
|
||||
end
|
||||
end
|
||||
end
|
153
modpol_core/orgs/process_old.lua
Normal file
153
modpol_core/orgs/process_old.lua
Normal file
@ -0,0 +1,153 @@
|
||||
old_request_format = {
|
||||
user=user, -- requesting user
|
||||
type="add_member", -- action
|
||||
params={user} -- action params
|
||||
}
|
||||
|
||||
old_process_format = {
|
||||
type = "consent", -- delete
|
||||
id = nil,
|
||||
org_id = nil,
|
||||
request_id = nil, -- delete
|
||||
|
||||
-- consent config
|
||||
majority_to_pass = 0.51, -- voting threshold
|
||||
votes_needed = nil,
|
||||
|
||||
-- consent data
|
||||
total_votes = 0,
|
||||
votes_yes = {},
|
||||
votes_no = {}
|
||||
}
|
||||
|
||||
new_process_format = {
|
||||
initiator = "user",
|
||||
status = "request",
|
||||
org_id = 12314,
|
||||
module = "create_child_org", -- policy table lookup
|
||||
process_id = 8347,
|
||||
timestamp = 1632850133, -- look into supporting other formats, overrides (turn based, etc.)
|
||||
|
||||
data = {
|
||||
child_org_name = "oligarchy"
|
||||
},
|
||||
|
||||
consent = {
|
||||
-- voter eligibilty frozen by action table invites
|
||||
start_time = 384179234,
|
||||
member_count = 14,
|
||||
votes_yes = {},
|
||||
votes_no = {}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
-- initialize values
|
||||
function init_consent(policy) {
|
||||
self.start_time = os.time()
|
||||
self.member_count = modpol.orgs.get_org(self.org_id):get_member_count()
|
||||
|
||||
if policy.duration then
|
||||
-- mintest.after(time, func, ...args)
|
||||
-- should override modpol callback function
|
||||
register_callback(self.start_time + policy.duration)
|
||||
end
|
||||
|
||||
if (duration and (consent_ratio or yes_threshold or no_threshold)) or (yes_threshold) or (consent_ratio) then
|
||||
-- well formed policy
|
||||
else
|
||||
-- invalid
|
||||
end
|
||||
|
||||
}
|
||||
|
||||
-- update vote count
|
||||
function update_consent(user, approve) {
|
||||
if approve then
|
||||
table.insert(yes_votes, user)
|
||||
else
|
||||
table.insert(no_votes, user)
|
||||
|
||||
if not duration then
|
||||
eval_consent()
|
||||
end
|
||||
|
||||
}
|
||||
|
||||
-- evaluate state of vote
|
||||
function eval_consent() {
|
||||
consent_ratio = #yes_votes / (#yes_votes + #no_votes)
|
||||
quorum = (#yes_votes + #no_votes) / member_count
|
||||
|
||||
if policy.duration then
|
||||
|
||||
if policy.consent_ratio then
|
||||
if policy.quorum then
|
||||
if quorum < policy.quorum then
|
||||
fail()
|
||||
end
|
||||
end
|
||||
if consent_ratio >= policy.consent_ratio then
|
||||
pass()
|
||||
else
|
||||
fail()
|
||||
end
|
||||
|
||||
elseif policy.yes_threshold then
|
||||
if #yes_votes >= policy.yes_threshold then
|
||||
pass()
|
||||
else
|
||||
fail()
|
||||
end
|
||||
|
||||
elseif policy.no_threshold then
|
||||
if #no_votes <= policy.no_threshold then
|
||||
fail()
|
||||
else
|
||||
pass()
|
||||
end
|
||||
end
|
||||
|
||||
elseif policy.yes_threshold then
|
||||
if policy.no_threshold then
|
||||
if #no_votes >= policy.no_threshold then
|
||||
fail()
|
||||
end
|
||||
if #yes_votes >= policy.yes_threshold then
|
||||
pass()
|
||||
end
|
||||
|
||||
elseif policy.consent_ratio and policy.quorum then
|
||||
if quorum >= policy.quorum then
|
||||
if consent_ratio >= policy.consent_ratio then
|
||||
pass()
|
||||
else
|
||||
fail()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
}
|
||||
|
||||
|
||||
policy_table_format = {
|
||||
"create_child_org": {
|
||||
defer_to = nil,
|
||||
|
||||
-- duration
|
||||
duration = nil, -- evaluates end conditions when reached
|
||||
|
||||
-- thesholds
|
||||
no_threshold = nil, -- fails if reached
|
||||
yes_threshold = nil, -- succeeds if reached
|
||||
|
||||
--ratios
|
||||
consent_ratio = nil, -- % of voters
|
||||
quorum = nil, -- % of members that vote
|
||||
}
|
||||
"create_child_org": {
|
||||
consent_threshold = 0.51,
|
||||
max_duration = 89324, -- seconds until vote closes if threshold not reached, or nil for no limit
|
||||
defer = nil, -- org id to defer to, or nil
|
||||
}
|
||||
}
|
278
modpol_core/orgs/requests.lua
Normal file
278
modpol_core/orgs/requests.lua
Normal file
@ -0,0 +1,278 @@
|
||||
-- THIS IS NOW DEPRECATED, TO MERGE INTO PROCESS
|
||||
|
||||
|
||||
-- REQUESTS
|
||||
-- Provides request functionality to org
|
||||
|
||||
-- TODO
|
||||
-- include consent functionality, available to modules
|
||||
-- load available modules in modpol.lua
|
||||
-- make policies a set of options for consent functions
|
||||
-- create simple modules for core functions: change_policy, add_member, start_org, join_org, close_org
|
||||
|
||||
-- CONSENT
|
||||
-- Provides consent functionality for modules
|
||||
|
||||
-- define default params
|
||||
modpol.default_policy = {
|
||||
target_org = nil,
|
||||
time_limit = nil,
|
||||
vote_threshold = 1 -- a ratio of the total number of members
|
||||
}
|
||||
|
||||
|
||||
-- PROCESSES
|
||||
-- functions that enable requests to create processes
|
||||
|
||||
-- ================================
|
||||
-- creates a new process linked to a request id
|
||||
function modpol.orgs:create_process(process_type, request_id)
|
||||
if not modpol.modules[process_type] then
|
||||
modpol.ocutil.log('Process type "' .. process_type .. '" does not exist')
|
||||
return
|
||||
end
|
||||
|
||||
local empty_index = nil
|
||||
-- linear search for empty process slots (lazy deletion)
|
||||
for k, v in ipairs(self.processes) do
|
||||
if v == 'deleted' then
|
||||
empty_index = k
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
local index
|
||||
-- attempts to fill empty spots in list, otherwise appends to end
|
||||
if empty_index then
|
||||
index = empty_index
|
||||
else
|
||||
index = #self.processes + 1
|
||||
end
|
||||
|
||||
-- retrieving requested module
|
||||
local module = modpol.modules[process_type]
|
||||
local new_process = module:new_process(index, request_id, self.id)
|
||||
|
||||
self.processes[index] = new_process
|
||||
|
||||
modpol.ocutil.log('Created process #' .. index .. ' - ' .. process_type .. ' for ' .. self.name)
|
||||
self:record('Created process #' .. index .. ' - ' .. process_type, 'add_process')
|
||||
|
||||
return index
|
||||
end
|
||||
|
||||
-- ===========================
|
||||
-- adds a new pending action to the org's table
|
||||
function modpol.orgs:add_pending_action(process_id, user)
|
||||
-- adds tables if they don't exist already
|
||||
self.pending[user] = self.pending[user] or {}
|
||||
-- flagging actual action
|
||||
self.pending[user][process_id] = true
|
||||
|
||||
local message =
|
||||
modpol.interactions.message(user, "New pending action in " .. self.name)
|
||||
modpol.ocutil.log("Added pending action to " .. user .. " in process #" .. process_id .. ' from ' .. self.name)
|
||||
self:record('Added pending action to ' .. user .. ' in process #' .. process_id, "add_pending_action")
|
||||
end
|
||||
|
||||
-- ========================
|
||||
-- removes a pending action from the org's table
|
||||
function modpol.orgs:remove_pending_action(process_id, user)
|
||||
|
||||
if self.pending[user] then
|
||||
self.pending[user][process_id] = nil
|
||||
modpol.ocutil.log("Removed pending action from " .. user .. " in process #" .. process_id .. ' from ' .. self.name)
|
||||
self:record('Removed pending action from ' .. user .. ' in process #' .. process_id, "del_pending_action")
|
||||
else
|
||||
modpol.ocutil.log("Could not remove pending action from " .. user .. " in process #" .. process_id)
|
||||
end
|
||||
end
|
||||
|
||||
-- =====================
|
||||
-- removes all pending actions for a given process id from all users
|
||||
function modpol.orgs:wipe_pending_actions(process_id)
|
||||
for user in pairs(self.pending) do
|
||||
self.pending[user][process_id] = nil
|
||||
end
|
||||
modpol.ocutil.log("Removed all pending actions for process #" .. process_id)
|
||||
self:record('Removed all pending actions for process #' .. process_id, "del_pending_action")
|
||||
end
|
||||
|
||||
-- ======================
|
||||
-- returns a boolean to indicate whether a user has any active pending actions
|
||||
function modpol.orgs:has_pending_actions(user)
|
||||
-- next() will return the next pair in a table
|
||||
-- if next() returns nil, the table is empty
|
||||
if not self.pending[user] then
|
||||
return false
|
||||
else
|
||||
if not next(self.pending[user]) then
|
||||
return false
|
||||
else
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- ===========================
|
||||
-- compares to requests to see if they are identical
|
||||
function modpol.orgs.comp_req(req1, req2)
|
||||
-- compares request type
|
||||
if req1.type ~= req2.type then
|
||||
return false
|
||||
else
|
||||
-- comparing parameters
|
||||
-- we can assume the number of params is the same as this is checked in the make_request func
|
||||
for k, v in ipairs(req1.params) do
|
||||
if v ~= req2.params[k] then
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- ===============================
|
||||
-- returns string of all active requests
|
||||
function modpol.orgs:list_request()
|
||||
local str
|
||||
for id, req in ipairs(self.requests) do
|
||||
if req ~= "deleted" then
|
||||
if str then
|
||||
str = str .. '\n' .. req.type .. ' (' .. req.user .. ') '
|
||||
else
|
||||
str = req.type .. ' (' .. req.user .. ') '
|
||||
end
|
||||
end
|
||||
end
|
||||
return str
|
||||
end
|
||||
|
||||
-- ===============================
|
||||
-- if the request was approved, the associated function is called, otherwise it is deleted
|
||||
-- TODO Rather than hard-coding functions below, this should be given an arbitrary result function based on the request
|
||||
function modpol.orgs:resolve_request(request_id, approve)
|
||||
|
||||
-- wipe actions
|
||||
self:wipe_pending_actions(request_id)
|
||||
|
||||
if approve then
|
||||
local request = self.requests[request_id]
|
||||
local p = request.params
|
||||
|
||||
-- there's probably a way to clean this up, the issue is the varying number of commands
|
||||
-- ex: self['add_member'](self, 'member_name')
|
||||
-- not sure if this is safe, more testing to do
|
||||
|
||||
-- self[request.type](self, p[1], p[2], p[3])
|
||||
|
||||
if request.type == "add_org" then
|
||||
self:add_org(request.params[1], request.user)
|
||||
elseif request.type == "delete" then
|
||||
self:delete()
|
||||
elseif request.type == "add_member" then
|
||||
self:add_member(request.params[1])
|
||||
elseif request.type == "remove_member" then
|
||||
self:remove_member(request.params[1])
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
self.processes[request_id] = "deleted"
|
||||
modpol.ocutil.log('Deleted process #' .. request_id)
|
||||
|
||||
self.requests[request_id] = "deleted"
|
||||
modpol.ocutil.log("Resolved request #" .. request_id .. ' in ' .. self.name)
|
||||
|
||||
self:record("Resolved request #" .. request_id, "resolve_request")
|
||||
end
|
||||
|
||||
-- ================================
|
||||
-- tries to make a request to the org
|
||||
function modpol.orgs:make_request(request)
|
||||
|
||||
-- checking to see if identical request already exists
|
||||
for k, v in ipairs(self.requests) do
|
||||
if self.comp_req(request, v) == true then
|
||||
modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request has already been made')
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
-- checking to see if user is able to make request
|
||||
local requested_policy = self.policies[request.type]
|
||||
|
||||
-- if not the instance org (instance's don't have parents)
|
||||
if self.id ~= 1 then
|
||||
local parent_policy = modpol.orgs.get_org(self.parent).policies[request.type]
|
||||
|
||||
-- tries to use org's policy table, defers to parent otherwise
|
||||
if not requested_policy then
|
||||
modpol.ocutil.log(request.type .. ' policy not found, deferring to parent org')
|
||||
requested_policy = parent_policy
|
||||
|
||||
if not parent_policy then
|
||||
modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> parent policy undefined')
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
-- fails if instance policy undefined
|
||||
else
|
||||
if not requested_policy then
|
||||
modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> policy undefined')
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
-- make sure user is allowed to make request
|
||||
if requested_policy.must_be_member and not self:has_member(request.user) then
|
||||
modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> user must be org member to make this request')
|
||||
return false
|
||||
end
|
||||
|
||||
local empty_index = nil
|
||||
-- linear search for empty process slots (lazy deletion)
|
||||
for k, v in ipairs(self.requests) do
|
||||
if v == 'deleted' then
|
||||
empty_index = k
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
-- attempts to fill empty spots in list, otherwise appends to end
|
||||
local request_id = nil
|
||||
if empty_index then
|
||||
self.requests[empty_index] = request
|
||||
request_id = empty_index
|
||||
else
|
||||
table.insert(self.requests, request)
|
||||
request_id = #self.requests
|
||||
end
|
||||
|
||||
modpol.interactions.message(request.user, "Request made by " .. request.user .. " to " .. request.type .. " in " .. self.name)
|
||||
modpol.ocutil.log("Request made by " .. request.user .. " to " .. request.type .. " in " .. self.name)
|
||||
self:record("Request made by " .. request.user .. " to " .. request.type, "make_request")
|
||||
|
||||
-- launching process tied to this request
|
||||
local process_id = self:create_process(requested_policy.process_type, request_id)
|
||||
|
||||
-- returns process id of processes launched by this request
|
||||
return process_id
|
||||
end
|
||||
|
||||
-- wrapper for process:interact function, ensures that user actually has a pending action for that process
|
||||
function modpol.orgs:interact(process_id, user)
|
||||
process = self.processes[process_id]
|
||||
if self.pending[user] then
|
||||
if self.pending[user][process_id] == true then
|
||||
process:interact(user)
|
||||
else
|
||||
modpol.ocutil.log("Cannot interact with process, user does not have a valid pending action")
|
||||
end
|
||||
else
|
||||
modpol.ocutil.log("Cannot interact with process, user does not have any pending actions")
|
||||
end
|
||||
|
||||
end
|
||||
|
Reference in New Issue
Block a user