diff --git a/modpol/DOCS/GOVERNANCE.md b/GOVERNANCE.md similarity index 90% rename from modpol/DOCS/GOVERNANCE.md rename to GOVERNANCE.md index 33b26c7..5283261 100644 --- a/modpol/DOCS/GOVERNANCE.md +++ b/GOVERNANCE.md @@ -6,6 +6,7 @@ One administrator, @ntnsndr, holds ultimate decision-making power over the proje * **Do-ocracy** Those who step forward to do a given task can decide how it should be done, in ongoing consultation with each other and the administrator. * **Membership** Participation is open to anyone who wants to contribute. The administrator can remove misbehaving participants at will for the sake of the common good. * **Code of Conduct** Participants are expected to abide by the Contributor Covenant (contributor-covenant.org). +* **Ownership** This is a project of the Media Enterprise Design Lab at the University of Colorado Boulder and is owned by the university. --- diff --git a/LICENSE b/LICENSE index 147ff18..4653428 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2021 Media Enterprise Design Lab, University of Colorado Boulder +Copyright (c) 2021 University of Colorado Boulder. A project of the Media Enterprise Design Lab (colorado.edu/lab/medlab/). Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 954d1a6..2e9cc95 100644 --- a/README.md +++ b/README.md @@ -1,54 +1,81 @@ -# Modular Politics Prototype for Minetest +# Modular Politics for Minetest -This is a mod for [Minetest](https://minetest.net) that enables diverse governance mechanisms. It seeks to implement the [Modular Politics](https://metagov.org/modpol) proposal. In the future, it -will be possible to use this framework to simulate governance in a -number of platform contexts. +Modular Politics is an extension that enables diverse governance processes on multi-user platforms. It offers a library that enables users to create or adapt their own modules that add specific governance functionalities. -This mod produces an API that can serve as a dependency for other mods that add specific governance functionalities. +This implementation is a mod for [Minetest](https://minetest.net), a free/open-source voxel game. It is designed to be easily adapted to other multi-user platforms that also employ Lua as an extension language. -For background information and project roadmap, see [the wiki](https://gitlab.com/medlabboulder/modpol/-/wikis/home). +## Design philosophy + +Modular Politics seeks to implement a theoretical framework, also called "[modular politics](https://metagov.org/modpol)," which proposes these design goals: + +* *Modularity*: Platform operators and community members should have the ability to construct systems by creating, importing, and arranging composable parts together as a coherent whole. +* *Expressiveness*: The governance layer should be able to implement as wide a range of processes as possible. +* *Portability*: Governance tools developed for one platform should be portable to another platform for reuse and adaptation. +* *Interoperability*: Governance systems operating on different platforms and protocols should have the ability to interact with each other, sharing data and influencing each other's processes. + +Additionally, Modular Politics seeks to counteract the tendency for "[implicit feudalism](https://ntnsndr.in/ImplicitFeudalism)," according to which rigid, top-down power structures are the norm in online spaces. To this end, some design patterns include: + +* *Groups, not roles*: While most platforms assign powers through particular permissions given to individuals, in Modular Politics, power lies in groups (which Modular Politics calls "orgs"). +* *Consent, not oligarchy*: Rather than assuming that decisions will be made by a few power-holders, the software assumes that consent by all affected users is the norm. +* *Inheritance, not blank slates*: When a new group is formed, it inherits the patterns of what preceded it, rather than imagining that it is starting from scratch. + +It is certainly possible to use Modular Politics to replicate practices of implicit feudalism, such as all-powerful admins, but doing so requires extra work to overcome these defaults. ## Installation in Minetest -To use this in Minetest, simply install it in your mods/ or worldmods/ folder. Minetest will load init.lua. +To use this in Minetest, simply install it in your `mods/` or `worldmods/` folder. Minetest will load `init.lua`. -In the game, open the Modular Politics interface with the command `/modpol`. +In the game, open the Modular Politics dashboard with the command `/mp`. + +For testing purposes, players with the `privs` privilege (generally admins) can use the `/mp` command, which resets all the orgs and opens a dashboard. ## Standalone Version on the Command Line Modular Politics can also be used independently of Minetest as a command-line tool. Currently command-line use of modpol requires a Unix-style system, but it is intended to become more fully platform independent. -The command-line version is in the `modpol` subdirectory. To interact with the interpreter on Unix systems in CLI mode, install lua or luajit and execute the following command in this directory: +The command-line version is in the `modpol` subdirectory. To run the program on Unix systems in CLI mode, install lua or luajit and execute the following in this directory: ``` -$ cd modpol/ -$ lua [or luajit] -> dofile("modpol.lua") +$ lua[jit] login.lua ``` -For a list of global functions and tables, use `modpol.menu()`. +You can also interact with the interpreter by starting it this way: + +``` +$ lua[jit] +> dofile("login.lua") +``` + +In the interpreter, for a list of global functions and tables, use `modpol.menu()`. ## Storage -By default, a data directory named "data" will be created in this directory. "/data" will contain a log file and serialized program data files. +By default, a data directory named "data" will be created in this directory. `/data` will contain a log file and serialized program data files. -Another storage method may be chosen in modpol.lua. A StorageRef-based method for Minetest 5.* is included: storage-mod_storage.lua. +Another storage method may be chosen in `modpol.lua`. A StorageRef-based method for Minetest 5.* is included: `storage-mod_storage.lua`. ## Credits -Initiated by [Nathan Schneider](https://nathanschneider.info) of the [Media Enterprise Design Lab](https://colorado.edu/lab/medlab) at the University of Colorado Boulder, as part of the [Metagovernance Project](https://metagov.org). Based on the paper "[Modular Politics: Toward a Governance Layer for Online Communities](https://metagov.org/modpol)." +This project is led by [Nathan Schneider](https://nathanschneider.info) of the [Media Enterprise Design Lab](https://colorado.edu/lab/medlab) at the University of Colorado Boulder, as part of the [Metagovernance Project](https://metagov.org). -Other contributors include: +Contributors include: -* [Luke Miller](https://gitlab.com/lukvmil) (main control flow, object orientation, module spec) -* [MisterE](https://gitlab.com/gbrrudmin) (project refactoring, core feature development) +* [Luke Miller](https://gitlab.com/lukvmil) (co-leadership, main control flow, object orientation, module spec) +* [MisterE](https://gitlab.com/gbrrudmin) (early project refactoring, core feature development) * Robert Kiraly [[OldCoder](https://github.com/oldcoder/)] (ocutils.lua, storage-local.lua, project refactoring) +* Skylar Hew (documentation) -We'd love to welcome more contributors, particularly from the Minetest community! Please join the conversation in the [Issues](https://gitlab.com/medlabboulder/modpol/-/issues) or the [Minetest.net forum](https://forum.minetest.net/viewtopic.php?f=47&t=26037). +We are grateful for initial support for this project from a residency with [The Bentway Conservancy](https://www.thebentway.ca/). Read about us in _[The Field Guide to Digital and/as Public Space](https://www.thebentway.ca/stories/field-guide/)_. + + +## Contributing + +We'd love to welcome more contributors. Please join the conversation in the [Issues](https://gitlab.com/medlabboulder/modpol/-/issues), our [Matrix.org channel](https://matrix.to/#/#minetest-modpol:matrix.org), and the [Minetest.net forum](https://forum.minetest.net/viewtopic.php?f=47&t=26037). + +Learn more about the project and how to develop your own modules in [the wiki](https://gitlab.com/medlabboulder/modpol/-/wikis/home). -We are grateful for support for this project from a residency with [The Bentway Conservancy](https://www.thebentway.ca/). ## Licenses diff --git a/description.txt b/description.txt index 4a6c332..7b5f707 100644 --- a/description.txt +++ b/description.txt @@ -1 +1 @@ -Provides an API for diverse governance mechanisms +Provides a basis for diverse, user-configurable modules for in-game governance. diff --git a/init.lua b/init.lua index 08bbb39..66cc4a6 100644 --- a/init.lua +++ b/init.lua @@ -14,7 +14,7 @@ modpol = {} -- get modpol_minetest's version of directory modpol.get_script_dir = function() - return minetest.get_modpath("modpol").."/modpol" + return minetest.get_modpath("modpol").."/modpol_core" end -- TKTK: Implement minetest settingtypes for this... for now, the default is to use minetest mod storage. @@ -34,7 +34,7 @@ modpol.storage_file_path = minetest.get_modpath("modpol").."/modpol_minetest/sto -- =================================================================== -- Load modpol system -dofile(minetest.get_modpath("modpol") .. "/modpol/modpol.lua") +dofile(minetest.get_modpath("modpol") .. "/modpol_core/modpol.lua") -- =================================================================== diff --git a/login.lua b/login.lua new file mode 100644 index 0000000..a29a58c --- /dev/null +++ b/login.lua @@ -0,0 +1,7 @@ +dofile("modpol_core/modpol.lua") + +print("Log in as which user?") +local username = io.read() + +print() +modpol.interactions.dashboard(username) diff --git a/modpol/DOCS/Predefinitions.txt b/modpol/DOCS/Predefinitions.txt deleted file mode 100644 index 8498435..0000000 --- a/modpol/DOCS/Predefinitions.txt +++ /dev/null @@ -1,35 +0,0 @@ -When calling modpol.lua from another file, one may wish to use -different functions than those included by default. - -So far, 2 definitions need to be made BEFORE calling modpol.lua, the rest can be overwritten -after calling modpol.lua . - -setup: ----------------- -Define the global modpol table: -modpol = {} - -the following 2 items *may* be defined, if they are not, then defaults will be used: -======================== --------------------- -modpol.get_script_dir() --------------------- -type: function - -defaults to: a function that can only work in linux - -usage: must return the file path of the modpol directory. If modpol is in a -larger directory, such as a mod filesystem, make sure that the path returned is the "base" -modpol folder, i.e. the folder with all the original modpol files. -======================== --------------------- -modpol.storage_file_path --------------------- -type: string - -defaults to: topdir .. "/storage/storage-local.lua", -where topdir is the directory defined by modpol.storage_file_path - -usage: if you wish to use another method of storage than that provided by default, -then you can save a filepath to the storage script here. - diff --git a/modpol/DOCS/ledgers b/modpol/DOCS/ledgers deleted file mode 100644 index dedf78a..0000000 --- a/modpol/DOCS/ledgers +++ /dev/null @@ -1,50 +0,0 @@ -Ledger -************************** - -a legder is a table of records that holds the history of an org, and of modpol itself. - -its structure is a table of legder entries. - -Every org has one, starting with the entry recording its creation. - -Ledger Entry -************************** -a legder entry is a table with the following properties: - -timestamp = nil, -entrytype = nil, -action_msg = '', - -the timestamp will hold the output of os.time at the moment the entry was created. - -the entrytype is optional; it can hold an identifier of the kind of entry it is (e.g. 'org_init', 'new_user', etc etc.) - -action_msg is the record output itself. - - - -Entrytypes -************************** - -These are the entrytypes that modpol uses. Each additional module should document whatever entrytypes it adds. - -'org_init' -type used when an org is created - -'org_purge' -type used when all orgs are purged. -- will only be found in the main modpol ledger. - -'org_remove' -type used when removing an individual org - -'add_member' -type used when adding a member to an org - -'remove_member' -type used when removing a member from an org - - - -Old orgs -************************** -when an org is removed, its ledger is stored in modpol.old_legders = {} Note that multiple ledgers may exist here with the same org name. \ No newline at end of file diff --git a/modpol/DOCS/orgs b/modpol/DOCS/orgs deleted file mode 100644 index a360bd7..0000000 --- a/modpol/DOCS/orgs +++ /dev/null @@ -1,137 +0,0 @@ -== Dev note === -*note, see doc ledgers for info about org ledgers. -they are *not* just a message in a table anymore... they have a timestamp and more. -=============== - - -Orgs are stored in modpol.orgs by the key 'org_id', a unique integer - -Old ledgers of deleted orgs are stored in modpol.old_ledgers, -if preserve records is enabled - -An org is a table with the following properties: - -{ - name = '', -- a unique name... - this is a reference to the key used to store the org table. Changing this does not change the org name. - policies = {}, -- a table to store the org's policies - members = {}, -- a table that contains the member names - or the org, in no particular order (TKTK: maybe change it so that members have a name:properties table?) - ledger = {}, -- a table of ledger entries. See the ledger docs - parent = nil, -- the name of the org that spawned this org. - removing a parent org removes its children - children = {}, -- a table of strings of org names of child orgs - properties = { -- good for modules to store arbitrary info about the org here. - - }, - -} - - -API functions for orgs -====================== - -a local variable setting 'preserve_records' determines whether -to store old ledgers when an org is deleted. It is set to true. -It would be possible to extend this to a settings file later. - ---==oo888888888888888888888888888888oo==-- -Function: modpol.record(org_id, msg, type) --- Params: strings msg, type, org_id (number) --- Outputs: --- "msg" specifies an event and/or status message. --- "org" specifies an "org" name. --- "type" -- optional type of legder entry. Could be e.g. 'election_result', 'add_member', etc - - --- This function adds the message to a global ledger and, if "org" --- specifies a valid "org", to an "org"-specific ledger. Both the mem- --- ory-resident and on-disk copies of the data structures used are up- --- dated. --- the type input is used to be able to search ledgers for specific events - ---==oo888888888888888888888888888888oo==-- - -Function: modpol.add_org(org_id, members, parent, properties) --- Params: org)id (number), table members, string parent, table properties --- Parent must be an existing org, defaults to 'instance', properties --- are arbitrary properties to initialize the org with - --- Output: --- This function creates an "org". It returns a boolean success indicator and --- either an error message starting with "Error:" or a success message. --- --- --- The string parameter specifies the "org" name. --- --- The members table should be an integer-indexed array of member --- names. - --- parent should be nil or a valid existing org name. If nil, defaults to --- 'instance' - --- properties defaults to an empty table. Use it to initialize the org --- with arbitrary properties - ---==oo888888888888888888888888888888oo==-- - --- Function: modpol.remove_org(org_id, reason) --- Params: org_id (number), opt string reason --- Output: --- --- This function removes an "org". It returns a boolean --- success indicator and either an error message --- starting with "Error:" or a success message. It also recursively --- removes all child orgs, with 'remove parent org' as reason. If --- preserve_records is enabled, it logs the removal in the org ledger, --- and stores the ledger in modpol.old_legders --- --- The string parameter specifies the "org" name. --- --- The reason is an optional string to additionally include in the --- log as reason for removal - ---==oo888888888888888888888888888888oo==-- - -Function: modpol.list_orgs() --- Params: None --- Output: --- This function returns a text-format list of "orgs". The list shows --- one "org" per line in the following format: --- org_name (member, member, ...) --- If there are no "orgs", the output is an empty string. - ---==oo888888888888888888888888888888oo==-- - - -Function: modpol.reset_orgs() --- Params: None --- Removes all orgs and recreates blank org "instance" with all --- current users --- returns confirmation message --- if preserve_records is enabled, stores all org ledgers --- in modpol.old_ledgers - ---==oo888888888888888888888888888888oo==-- - -Function: modpol.add_member(org_id, member) --- Params: org_id (number), member (string) --- Output: --- Adds the specified member to the specified org --- Returns a boolean success indicator and --- either a confirmation or error message - ---==oo888888888888888888888888888888oo==-- - -Function: modpol.is_member --- Params: org (number), member (string) --- Output: boolean, or nil and error message if not applicable. (org nonexistent) - - ---==oo888888888888888888888888888888oo==-- - -Function: modpol.remove_member --- Params: org (number), member (string) --- Output: --- Removes the specified member from the specified org --- Returns confirmation or error message \ No newline at end of file diff --git a/modpol/api.lua b/modpol/api.lua deleted file mode 100644 index 9565302..0000000 --- a/modpol/api.lua +++ /dev/null @@ -1,16 +0,0 @@ ---call all files in this directory - -local localdir = modpol.topdir - ---Users -dofile (localdir .. "/users/users.lua") - ---orgs -dofile (localdir .. "/orgs/base.lua") -dofile (localdir .. "/orgs/requests.lua") - ---interactions -dofile (localdir .. "/interactions/interactions.lua") - ---modules -dofile (localdir .. "/modules/consent.lua") diff --git a/modpol/interactions/login.lua b/modpol/interactions/login.lua deleted file mode 100644 index efb0063..0000000 --- a/modpol/interactions/login.lua +++ /dev/null @@ -1,7 +0,0 @@ -dofile("../modpol.lua") - -print("Log in as which user?") -local username = io.read() - -print() -modpol.interactions.dashboard(username) \ No newline at end of file diff --git a/modpol/modules/consent.lua b/modpol/modules/consent.lua deleted file mode 100644 index 1cbcbf8..0000000 --- a/modpol/modules/consent.lua +++ /dev/null @@ -1,98 +0,0 @@ -modpol.modules = modpol.modules or {} - -modpol.modules.consent = {} - --- sets consent to its own callback -modpol.modules.consent.__index = modpol.modules.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.modules.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.modules.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.modules.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.modules.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.modules.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 diff --git a/modpol/modules/defer_to.lua b/modpol/modules/defer_to.lua deleted file mode 100644 index e69de29..0000000 diff --git a/modpol/orgs/requests.lua b/modpol/orgs/requests.lua deleted file mode 100644 index aacc085..0000000 --- a/modpol/orgs/requests.lua +++ /dev/null @@ -1,272 +0,0 @@ -modpol.orgs.request_params = { - add_org = 1, - delete = 0, - add_member = 1, - remove_member = 1 -} - --- ================================ --- 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) - -- makes sure the request has the valid number of parameters - local num_params = modpol.orgs.request_params[request.type] - - if num_params == nil then - modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request type is invalid') - return false - end - - -- request.params should match the num of params for that type - if #request.params ~= num_params then - modpol.ocutil.log('Error in ' .. self.name .. ':make_request -> request has invalid number of parameters') - return false - end - - -- 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 - diff --git a/modpol/tests/nested_functions.lua b/modpol/tests/nested_functions.lua deleted file mode 100644 index 914d68f..0000000 --- a/modpol/tests/nested_functions.lua +++ /dev/null @@ -1,31 +0,0 @@ -dofile("../modpol.lua") - -print('\nRemoving existing orgs') -modpol.orgs.reset() - -print('\nCreating an org called "test_org"') -test_org = modpol.instance:add_org('test_org', 'luke') - -print('\nAdding user "nathan" to test_org') -test_org:add_member('nathan') - -print("\nOkay, let's start with requests. Setting up an org...") -modpol.instance:add_org('test_org', 'luke') -test_org:add_member('nathan') - -print('\nMaking consent the add_member policy') -test_org:set_policy("add_member", "consent", false); - -print('\nMaking a request to add Josh') -add_josh = { - user = "josh", - type = "add_member", - params = {"josh"} -} -request_id = test_org:make_request(add_josh) - - -print('\nHave the two members vote on it') -modpol.interactions.org_dashboard("nathan","test_org") -modpol.interactions.org_dashboard("luke","test_org") - diff --git a/modpol/users/users.lua b/modpol/users/users.lua deleted file mode 100644 index 8c94eec..0000000 --- a/modpol/users/users.lua +++ /dev/null @@ -1,29 +0,0 @@ --- =================================================================== --- /users.lua --- User-related functions for Modular Politics --- Called by modpol.lua - --- =================================================================== --- Function: modpol.list_users --- Params: org --- Outputs: Table of user names --- --- This may be overwritten by the platform-specific interface - -modpol.list_users = function(org) - local users = {} - if (org == nil) then -- no specified org; all players - if modpol.instance - and modpol.instance.members then - -- if instance exists and has membership - users = modpol.instance.members - else - users = {} - end - else -- if an org is specified - if (modpol.orgs[org] ~= nil) then -- org exists - users = modpol.orgs[org]["members"] - end - end - return users -end diff --git a/modpol_core/api.lua b/modpol_core/api.lua new file mode 100644 index 0000000..b50bf18 --- /dev/null +++ b/modpol_core/api.lua @@ -0,0 +1,23 @@ +--call all files in this directory + +local localdir = modpol.topdir + +--orgs +dofile (localdir .. "/orgs/base.lua") +dofile (localdir .. "/orgs/process.lua") + +--interactions +dofile (localdir .. "/interactions/interactions.lua") + +--modules +--TODO make this automatic and directory-based +dofile (localdir .. "/modules/add_child_org_consent.lua") +dofile (localdir .. "/modules/change_modules.lua") +dofile (localdir .. "/modules/consent.lua") +dofile (localdir .. "/modules/join_org_consent.lua") +dofile (localdir .. "/modules/leave_org.lua") +dofile (localdir .. "/modules/remove_child_consent.lua") +dofile (localdir .. "/modules/remove_member_consent.lua") +dofile (localdir .. "/modules/remove_org_consent.lua") +dofile (localdir .. "/modules/remove_org.lua") +dofile (localdir .. "/modules/rename_org_consent.lua") diff --git a/modpol/interactions/interactions.lua b/modpol_core/interactions/interactions.lua similarity index 72% rename from modpol/interactions/interactions.lua rename to modpol_core/interactions/interactions.lua index d51f47b..664841b 100644 --- a/modpol/interactions/interactions.lua +++ b/modpol_core/interactions/interactions.lua @@ -20,7 +20,7 @@ function modpol.interactions.dashboard(user) modpol.instance:add_member(user) end - local all_users = modpol.list_users() + local all_users = modpol.instance:list_members() print('All orgs: (user orgs indicated by *)') for id, org in ipairs(modpol.orgs.array) do @@ -34,25 +34,27 @@ function modpol.interactions.dashboard(user) print('All users: ' .. table.concat(all_users, ', ')) print() - print('Access which org?') - + print('Access which org id?') local sel = io.read() print() - local sel_org = modpol.orgs.array[tonumber(sel)].name - if not sel_org then return end - - modpol.interactions.org_dashboard(user, sel_org) + if modpol.orgs.array[tonumber(sel)] then + local sel_org = modpol.orgs.array[tonumber(sel)].name + modpol.interactions.org_dashboard(user, sel_org) + else + print("Org id not found.") + end end -- Function: modpol.interactions.org_dashboard --- Params: user (string), org_name (string) +-- Params: user (string), org_string (string or id) -- Output: Displays a menu of org-specific commands to the user -function modpol.interactions.org_dashboard(user, org_name) - local org = modpol.orgs.get_org(org_name) +function modpol.interactions.org_dashboard(user, org_string) + local org = modpol.orgs.get_org(org_string) if not org then return nil end + -- identify parent local parent = "" if org.id == 1 then parent = "none" @@ -60,66 +62,67 @@ function modpol.interactions.org_dashboard(user, org_name) parent = modpol.orgs.get_org(org.parent).name end + -- identify children local children = {} for k,v in ipairs(org.children) do local this_child = modpol.orgs.get_org(v) table.insert(children, this_child.name) end - local process_msg = #org.processes .. " total" + -- list available modules + local org_modules = {} + for k,v in pairs(org.modules) do + if not v.hide then + table.insert(org_modules, v.slug) + end + end + -- list pending + local process_msg = #org.processes .. " total processes" if org.pending[user] then process_msg = process_msg .. " (" .. #org.pending[user] .. " pending)" else process_msg = process_msg .. " (0 pending)" end - -- set up output - print("Org: " .. org_name) + print("Org: " .. org.name) print("Parent: " .. parent) print("Members: " .. table.concat(org.members, ", ")) print("Children: " .. table.concat(children, ", ")) - print("Processes: " .. process_msg) + print("Modules: " .. table.concat(org_modules, ", ")) + print("Pending: " .. process_msg) print() - print("Commands: (L)eave, (J)oin, (P)rocesses, (A)dd child, (D)elete org") + print("Commands: (M)odules, (P)ending") local sel = io.read() print() - if sel == 'l' or sel == 'L' then - org:remove_member(user) - - elseif sel == 'j' or sel == 'J' then - org:make_request({user=user, type="add_member", params={user}}) - + if sel == 'm' or sel == 'M' then + print("Type module name: ") + local module_sel = io.read() + print() + local module_result = false + for k,v in ipairs(org_modules) do + if v == module_sel then + module_result = true + end + end + if module_result then + org:call_module(module_sel, user) + else + print("Error: Module not found.") + end + elseif sel == 'a' or sel == 'A' then - print("What should the new org be named?") - local new_org_name = io.read() - org:make_request({user=user, type="add_org", params={new_org_name}}) - - elseif sel == 'd' or sel == 'D' then - org:make_request({user=user, type="delete", params={}}) - - elseif sel == 'p' or sel == 'P' then local processes = {} - print("All processes: (* indicates pending action)") + print("All processes: (* indicates pending)") for k,v in ipairs(org.processes) do - local this_request = org.requests[v.request_id] - if type(this_request) == "table" then - local active = '' - if org.pending[user] then - if org.pending[user][v.id] then - active = '*' - end + local active = '' + if org.pending[user] then + if org.pending[user][v.id] then + active = '*' end - local req_str = "[" .. v.id .. "] " .. - active .. this_request.type - if this_request.params[1] then - req_str = req_str .. ": " .. - table.concat(this_request.params, ", ") - end - print(req_str) end end print() @@ -129,14 +132,13 @@ function modpol.interactions.org_dashboard(user, org_name) if not process then return end if org:has_pending_actions(user) then if org.pending[user][process.id] then - process:interact(user) + org:interact(process.id, user) end end + else + print("Command not found") + modpol.interactions.org_dashboard(user, org.name) end - - - - end -- Function: modpol.interactions.policy_dashboard @@ -228,8 +230,15 @@ end -- ==================== -- Function: modpol.interactions.message_org --- input: initiator (string), org_id (number), message (string) +-- input: initiator (string), org (number or string), message (string) -- output: broadcasts message to all org members +function modpol.interactions.message_org(initiator, org, message) + local this_org = modpol.orgs.get_org(org) + local users = this_org:list_members() + for k,v in ipairs(users) do + modpol.interactions.message(v, message) + end +end -- Function: modpol.interactions.binary_poll_org -- input: initator (user string), org_id (number) diff --git a/modpol/modpol.lua b/modpol_core/modpol.lua similarity index 92% rename from modpol/modpol.lua rename to modpol_core/modpol.lua index 844c34a..6533ade 100644 --- a/modpol/modpol.lua +++ b/modpol_core/modpol.lua @@ -44,6 +44,9 @@ print (topdir) -- OldCoder utilities dofile (topdir .. "/util/ocutil/ocutil.lua") +-- Misc. utilities +dofile (topdir .. "/util/misc.lua") + -- =================================================================== -- Persistent storage -- must implement modpol.load_storage() and modpol.store_data() @@ -63,12 +66,19 @@ dofile (modpol.storage_file_path) -- If available, load persistent storage into active tables modpol.load_storage() + -- =================================================================== --- ModPol core features +-- Modpol modules + +modpol.modules = modpol.modules or {} + +-- TKTK need to specify modules to include + +-- =================================================================== +-- Modpol core features dofile (topdir .. "/api.lua") - -- =================================================================== -- Final checks @@ -90,7 +100,6 @@ end -- create instance if not present modpol.instance = modpol.orgs.array[1] or modpol.orgs.init_instance() - modpol.ocutil.log ("modpol loaded") -- =================================================================== diff --git a/modpol_core/modules/add_child_org_consent.lua b/modpol_core/modules/add_child_org_consent.lua new file mode 100644 index 0000000..e323e19 --- /dev/null +++ b/modpol_core/modules/add_child_org_consent.lua @@ -0,0 +1,75 @@ +--- @module add_child_org_consent +-- Adds a child org +-- Depends on `consent` + +local add_child_org_consent = { + name = "Add child org (consent)", + slug = "add_child_org_consent", + desc = "Create a child org in the current one with member consent" +} +add_child_org_consent.data = { + child_name = "", + result = nil +} +add_child_org_consent.config = { +} + +-- @function initiate +function add_child_org_consent:initiate(result) + modpol.interactions.text_query( + self.initiator,"Child org name: ", + function(input) + if input == "" then + modpol.interactions.message( + self.initiator, + "No name entered for child org") + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + self.org:delete_process(self.id) + if result then result() end + return + elseif modpol.orgs.get_org(input) then + modpol.interactions.message( + self.initiator, + "Org name already in use") + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + self.org:delete_process(self.id) + if result then result() end + return + end + self.data.child_name = input + modpol.interactions.message( + self.initiator, + "Proposed child org: " .. input) + -- initiate consent process + self.org:call_module( + "consent", + self.initiator, + { + prompt = "Create child org " .. + self.data.child_name .. "?", + votes_required = #self.org.members + }, + function() + self:create_child_org() + end + ) + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + end + ) +end + +function add_child_org_consent:create_child_org() + self.org:add_org(self.data.child_name, self.initiator) + modpol.interactions.message_org( + self.initiator, + self.org.name, + "Child org created: "..self.data.child_name) + if self.data.result then self.data.result() end + self.org:delete_process(self.id) +end + +--- (Required) Add to module table +modpol.modules.add_child_org_consent = add_child_org_consent diff --git a/modpol_core/modules/change_modules.lua b/modpol_core/modules/change_modules.lua new file mode 100644 index 0000000..d5c97da --- /dev/null +++ b/modpol_core/modules/change_modules.lua @@ -0,0 +1,171 @@ +--- change_modules +-- @module change_modules +-- Depends on consent + +local change_modules = { + name = "Change modules (consent)", + slug = "change_modules", + desc = "Add or remove modules from the org with member consent", + hide = false; +} + +change_modules.data = { + result = nil +} + +change_modules.config = { +} + +function change_modules:initiate(result) + self.data.result = result + -- Step 1: add or remove? + modpol.interactions.dropdown_query( + self.initiator, "Module change options:", + {"Add module","Remove module"}, + function(input) + if input == "Add module" then + self:add_module() + elseif input == "Remove module" then + self:remove_module() + end + end + ) +end + +function change_modules:add_module() + -- prepare module options + local available_modules = modpol.copy_table(modpol.modules) + for k,org_mod in pairs(self.org.modules) do + if available_modules[org_mod.slug] then + available_modules[org_mod.slug] = nil + end end + -- present module options + local modules_list = {} + for k,v in pairs(available_modules) do + table.insert(modules_list,v.name) + end + if #modules_list == 0 then + modpol.interactions.message( + self.initiator, "Org has all modules") + modpol.interactions.org_dashboard( + self.initiator, self.org.id) + if self.data.result then self.data.result() end + self.org:delete_process(self.id) + return + end + table.sort(modules_list) + -- now ask which to add + modpol.interactions.dropdown_query( + self.initiator, "Choose a module to add:", + modules_list, + function(mod_choice) + -- confirm choice + modpol.interactions.binary_poll_user( + self.initiator, + "Confirm: propose to add module \"" .. + mod_choice .. "\"?", + function(input) + if input == "Yes" then + self:propose_change("add",mod_choice) + modpol.interactions.org_dashboard( + self.initiator, self.org.id) + else + self:add_module() + end + end + ) + end + ) +end + +function change_modules:remove_module() + -- prepare module options + local available_modules = {} + for k,org_mod in pairs(self.org.modules) do + if not org_mod.hide then + available_modules[org_mod.slug] = modpol.copy_table(org_mod) + end end + local modules_list = {} + local modules_count = 0 + for k,v in pairs(available_modules) do + table.insert(modules_list,v.name) + modules_count = modules_count + 1 + end + -- abort if no modules to remove + if modules_count == 0 then + modpol.interactions.message( + self.initiator, "Org has no modules") + modpol.interactions.org_dashboard( + self.initiator, self.org.id) + if self.data.result then self.data.result() end + self.org:delete_process(self.id) + return + end + table.sort(modules_list) + -- now ask which to remove + modpol.interactions.dropdown_query( + self.initiator, "Choose a module to remove:", + modules_list, + function(mod_choice) + -- confirm choice + modpol.interactions.binary_poll_user( + self.initiator, + "Confirm: propose to remove module \"" .. mod_choice .. "\"?", + function(input) + if input == "Yes" then + self:propose_change("remove",mod_choice) + modpol.interactions.org_dashboard( + self.initiator, self.org.id) + else + self:remove_module() + end + end + ) + end + ) +end + +--- propose_change +-- @field type "add" or "remove" +function change_modules:propose_change(type, mod_text) + self.org:call_module( + "consent",self.initiator, + { + prompt = "Do you consent to "..type.. + " this module in org "..self.org.name.. + ":\n"..mod_text, + votes_required = #self.org.members + }, + function() + if type == "add" then + for k,v in pairs(modpol.modules) do + if v.name == mod_text then + table.insert(self.org.modules,v) + end + end + modpol.interactions.message_org( + self.initiator,self.org.id, + "Consent reached:\nAdding \"" + ..mod_text.."\" to org "..self.org.name) + elseif type == "remove" then + modpol.msg("Removing!") + local i = 0 + for k,v in pairs(self.org.modules) do + i = i + 1 + if v.name == mod_text then + modpol.msg("got it!") + self.org.modules[k] = nil + end + end + modpol.interactions.message_org( + self.initiator,self.org.id, + "Consent reached:\nRemoving \"" + ..mod_text.."\" from org "..self.org.name) + end + end) + if self.data.result then self.data.result() end + self.org:delete_process(self.id) +end + +--- (Required) Add to module table +modpol.modules.change_modules = change_modules diff --git a/modpol_core/modules/consent.lua b/modpol_core/modules/consent.lua new file mode 100644 index 0000000..78deca1 --- /dev/null +++ b/modpol_core/modules/consent.lua @@ -0,0 +1,56 @@ +--- @module consent +-- A utility module for checking consent + +local consent = { + name = "Consent process utility", + slug = "consent", + desc = "A module other modules use for consent decisions", + hide = true +} + +consent.data = { + votes = 0 +} + +consent.config = { + prompt = "Do you consent?", + votes_required = 1 +} + +function consent:initiate(result) + self.data.result = result + -- if org is empty, consent is given automatically + if self.org:get_member_count() == 0 then + if self.data.result then + self.data.result() end + self.org:wipe_pending_actions(self.id) + else + -- otherwise, create poll + for id, member in pairs(self.org.members) do + self.org:add_pending_action(self.id, member, "callback") + end + end +end + +function consent:callback(member) + modpol.interactions.binary_poll_user( + member, + self.config.prompt, + function (resp) + self.org:remove_pending_action(self.id,member) + if resp == "Yes" then + self.data.votes = self.data.votes + 1 + end + if self.data.votes >= self.config.votes_required then + if self.data.result then + self.data.result() end + self.org:wipe_pending_actions(self.id) + self.org:delete_process(self.id) + end + modpol.interactions.org_dashboard( + member, self.org.name) + end + ) +end + +modpol.modules.consent = consent diff --git a/modpol_core/modules/join_org.lua b/modpol_core/modules/join_org.lua new file mode 100644 index 0000000..23d277b --- /dev/null +++ b/modpol_core/modules/join_org.lua @@ -0,0 +1,39 @@ + +join_org = {} + +join_org.setup = { + name = "Join Org", + slug = "join_org", + desc = "If consent process is passed, initiator joins this org." +} + +function join_org.initiate(result) + modpol.interactions.binary_poll_user( + initiator, + "Would you like to join " .. org.name, + function (resp) + if resp == "Yes" then + self.org:add_member(self.initiator) + end + end + ) + + for i, member in ipairs(self.org.members) do + self.org:add_pending_action( + member, + function () + modpol.interactions.binary_poll_user( + member, + "Let " .. initiator .. " join " .. org.name .. "?", + function (resp) + + end + ) + end + ) + end + + if result then result() end +end + +modpol.modules.join_org = join_org diff --git a/modpol_core/modules/join_org_consent.lua b/modpol_core/modules/join_org_consent.lua new file mode 100644 index 0000000..9bfa437 --- /dev/null +++ b/modpol_core/modules/join_org_consent.lua @@ -0,0 +1,51 @@ +--- Join org (consent) +-- A simple module that calls a consent process on an org to add a member. +-- Depends on the Consent module. + +local join_org_consent = { + name = "Join this org (consent)", + slug = "join_org_consent", + desc = "Adds member with consent of all members" +} + +join_org_consent.data = { + result = nil +} + +join_org_consent.config = { +} + +function join_org_consent:initiate(result) + if self.org:has_member(self.initiator) then + modpol.interactions.message( + self.initiator, + "You are already a member of this org") + if result then result() end + self.org:delete_process(self.id) + else + self.data.result = result + self.org:call_module( + "consent", + self.initiator, + { + prompt = "Allow " .. self.initiator .. " to join?", + votes_required = #self.org.members + }, + function () + self:complete() + end + ) + end +end + +function join_org_consent:complete() + self.org:add_member(self.initiator) + modpol.interactions.message_org( + self.initiator,self.org.name, + "Consent reached: " .. self.initiator .. + " joined org " .. self.org.name) + if self.data.result then self.data.result() end + self.org:delete_process(self.id) +end + +modpol.modules.join_org_consent = join_org_consent diff --git a/modpol_core/modules/leave_org.lua b/modpol_core/modules/leave_org.lua new file mode 100644 index 0000000..d8e685d --- /dev/null +++ b/modpol_core/modules/leave_org.lua @@ -0,0 +1,39 @@ +--- leave_org +-- @module leave_org + +local leave_org = { + name = "Leave org", + slug = "leave_org", + desc = "Remove yourself from the current org" +} + +leave_org.data = { +} + +leave_org.config = { +} + +--- (Required): initiate function +-- Modules have access to the following instance variables: +-- @param result (optional) Callback if this module is embedded in other modules +-- @function initiate +function leave_org:initiate(result) + if self.org == modpol.instance then + modpol.interactions.message( + self.initiator, + "You cannot leave the root org") + else + self.org:remove_member(self.initiator) + modpol.interactions.message_org( + self.initiator,self.org.id, + self.initiator .. " has left org " .. self.org.name) + modpol.interactions.message( + self.initiator, + "You have left org " .. self.org.name) + end + if result then result() end + self.org:delete_process(self.id) +end + +--- (Required) Add to module table +modpol.modules.leave_org = leave_org diff --git a/modpol_core/modules/remove_child_consent.lua b/modpol_core/modules/remove_child_consent.lua new file mode 100644 index 0000000..ba3255e --- /dev/null +++ b/modpol_core/modules/remove_child_consent.lua @@ -0,0 +1,71 @@ +--- Remove child (consent) +-- A simple module that calls a consent process on an org to remove its child +-- Depends on the Consent module. + +local remove_child_consent = { + name = "Remove child (consent)", + slug = "remove_child_consent", + desc = "Removes a child org if all members of this org consent." +} + +remove_child_consent.data = { + result = nil, + child_to_remove = nil +} + +remove_child_consent.config = { +} + +function remove_child_consent:initiate(result) + local children = {} + for i,v in ipairs(self.org.children) do + local child = modpol.orgs.get_org(v) + if child then table.insert(children, child.name) end + end + -- Abort if no child orgs + if #children == 0 then + modpol.interactions.message( + self.initiator, + "Org has no children") + if result then result() end + self.org:delete_process(self.id) + else + self.data.result = result + modpol.interactions.dropdown_query( + self.initiator, + "Choose a child of org ".. + self.org.name.." to remove:", + children, + function(input) + self.data.child_to_remove = modpol.orgs.get_org(input) + self.org:call_module( + "consent", + self.initiator, + { + prompt = "Remove child org "..input.."?", + votes_required = #self.org.members + }, + function() + self:complete() + end) + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + end) + end +end + +function remove_child_consent:complete() + modpol.interactions.message_org( + self.initiator, self.data.child_to_remove.id, + "Removing org " .. self.data.child_to_remove.name .. + " by parent org consent") + modpol.interactions.message_org( + self.initiator, self.org.id, + "Consent reached: removing org " .. + self.data.child_to_remove.name) + self.data.child_to_remove:delete() + if self.data.result then self.data.result() end + self.org:delete_process(self.id) +end + +modpol.modules.remove_child_consent = remove_child_consent diff --git a/modpol_core/modules/remove_member_consent.lua b/modpol_core/modules/remove_member_consent.lua new file mode 100644 index 0000000..fc118e1 --- /dev/null +++ b/modpol_core/modules/remove_member_consent.lua @@ -0,0 +1,64 @@ +--- remove_member_consent +-- @module remove_member_consent + +local remove_member_consent = { + name = "Remove a member (consent)", + slug = "remove_member_consent", + desc = "Removes org member with consent of other members" +} + +remove_member_consent.data = { + member_to_remove = "", + result = nil +} + +remove_member_consent.config = { +} + +function remove_member_consent:initiate(result) + -- Abort if in root org + if self.org == modpol.instance then + modpol.interactions.message( + self.initiator, + "Members cannot be removed from the root org") + if result then result() end + self.org:delete_process(self.id) + else -- proceed if not root + self.data.result = result + modpol.interactions.dropdown_query( + self.initiator, + "Which member of org "..self.org.name.. + " do you want to remove?", + self.org.members, + function(input) + self.data.member_to_remove = input + self.org:call_module( + "consent", + self.initiator, + { + prompt = "Remove "..input.. + " from org "..self.org.name.."?", + votes_required = #self.org.members - 1 + }, + function() + self:complete() + end) + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + end) + end +end + +function remove_member_consent:complete() + modpol.interactions.message_org( + self.initiator, self.org.id, + "Consent reached: removing ".. + self.data.member_to_remove.. + " from org "..self.org.name) + self.org:remove_member(self.data.member_to_remove) + self.org:delete_process(self.id) + if self.data.result then self.data.result() end +end + +--- (Required) Add to module table +modpol.modules.remove_member_consent = remove_member_consent diff --git a/modpol_core/modules/remove_org.lua b/modpol_core/modules/remove_org.lua new file mode 100644 index 0000000..b2118d1 --- /dev/null +++ b/modpol_core/modules/remove_org.lua @@ -0,0 +1,33 @@ +--- @module Remove Org +-- A simple module that removes an org. + + +--- Main module table +remove_org = { + name = "Remove this org", + slug = "remove_org", + desc = "Eliminates the org and all child orgs." +} + +remove_org.config = {} +remove_org.data = {} + +--- Initiate function +-- @function initiate +function remove_org:initiate(result) + if self.org == modpol.instance then + modpol.interactions.message( + self.initiator, + "Cannot remove the root org") + else + modpol.interactions.message_org( + self.initiator,self.org.id, + "Removing org: "..self.org.name) + self.org:delete() + modpol.interactions.dashboard(self.initiator) + -- call result function + end + if result then result() end +end + +modpol.modules.remove_org = remove_org diff --git a/modpol_core/modules/remove_org_consent.lua b/modpol_core/modules/remove_org_consent.lua new file mode 100644 index 0000000..f9e8513 --- /dev/null +++ b/modpol_core/modules/remove_org_consent.lua @@ -0,0 +1,52 @@ +--- Remove org (consent) +-- A simple module that calls a consent process on an org to remove it. +-- Depends on the Consent module. + +local remove_org_consent = { + name = "Remove this org (consent)", + slug = "remove_org_consent", + desc = "Removes an org if all members consent." +} + +remove_org_consent.data = { + result = nil +} + +remove_org_consent.config = { +} + +function remove_org_consent:initiate(result) + if self.org == modpol.instance then + modpol.interactions.message( + self.initiator, + "Cannot remove root org") + if result then result() end + self.org:delete_process(self.id) + else + self.data.result = result + self.org:call_module( + "consent", + self.initiator, + { + prompt = "Remove org " .. self.org.name .. "?", + votes_required = #self.org.members + }, + function () + self:complete() + end + ) + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + end +end + +function remove_org_consent:complete() + modpol.interactions.message_org( + self.initiator, self.org.id, + "Consent reached: removing org " .. self.org.name) + if self.data.result then self.data.result() end + self.org:delete_process(self.id) + self.org:delete() +end + +modpol.modules.remove_org_consent = remove_org_consent diff --git a/modpol_core/modules/rename_org_consent.lua b/modpol_core/modules/rename_org_consent.lua new file mode 100644 index 0000000..1b9aa47 --- /dev/null +++ b/modpol_core/modules/rename_org_consent.lua @@ -0,0 +1,77 @@ +--- Rename org (consent) +-- A simple module that calls a consent process on an org to rename it. +-- Depends on the Consent module. + +local rename_org_consent = { + name = "Rename this org (consent)", + slug = "rename_org_consent", + desc = "Renames an org if all members consent." +} + +rename_org_consent.data = { + result = nil, + new_name = nil +} + +rename_org_consent.config = { +} + +function rename_org_consent:initiate(result) + modpol.interactions.text_query( + self.initiator,"New org name: ", + function(input) + if input == "" then + modpol.interactions.message( + self.initiator, + "No name entered for child org") + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + self.org:delete_process(self.id) + if result then result() end + return + elseif modpol.orgs.get_org(input) then + modpol.interactions.message( + self.initiator, + "Org name already in use") + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + self.org:delete_process(self.id) + if result then result() end + return + end + self.data.new_name = input + modpol.interactions.message( + self.initiator, + "Proposed to change name of org " .. + self.org.name .. " to " .. input) + -- initiate consent process + self.org:call_module( + "consent", + self.initiator, + { + prompt = "Change name of org " .. + self.org.name .. " to " .. input .. "?", + votes_required = #self.org.members + }, + function() + self:complete() + end + ) + modpol.interactions.org_dashboard( + self.initiator, self.org.name) + end + ) +end + +function rename_org_consent:complete() + modpol.interactions.message_org( + self.initiator, + self.org.name, + "Changing name of org " .. self.org.name .. + " to " .. self.data.new_name) + self.org.name = self.data.new_name + if self.data.result then self.data.result() end + self.org:delete_process(self.id) +end + +modpol.modules.rename_org_consent = rename_org_consent diff --git a/modpol_core/modules/template.lua b/modpol_core/modules/template.lua new file mode 100644 index 0000000..ae63712 --- /dev/null +++ b/modpol_core/modules/template.lua @@ -0,0 +1,54 @@ +--- module_template +-- @module module_template + +--- (Required): data table containing name and description of the module +-- @field name "Human-readable name (parens OK, no brackets)" +-- @field slug "Same as module class name" +-- @field desc "Description of the module" +-- @field hide "Whether this is a hidden utility module" +local module_template = { + name = "Module Human-Readable Name", + slug = "template", + desc = "Description of the module", + hide = false; +} + +--- (Required) Data for module +-- Variables that module uses during the course of a process +-- Can be blank +module_template.data = { +} + +--- (Required): config for module +-- Defines the input parameters to the module initiate function. +-- Can be blank +-- When calling a module from within another module, +-- variables not defined in config will be ignored. +-- Default values set in config can be overridden +-- @field field_1 ex: votes_required, default = 5 +-- @field field_2 ex: voting_type, default = "majority" +module_template.config = { + field_1 = 5 + field_2 = "majority" +} + +--- (Required): initiate function +-- Modules have access to the following instance variables: +--
self.org
(the org the module was called in),self.initiator
(the user that callced the module),self.id
(the process id of the module instance)