From 66c4e63adb91a64a2f0c33374a2a6f6df0bad680 Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Sat, 3 Apr 2021 23:01:31 -0400 Subject: [PATCH 1/3] starting refactor, simple functionality works atm --- modpol/api.lua | 4 +-- modpol/modpol.lua | 6 ++-- modpol/orgs/new_orgs.lua | 74 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 modpol/orgs/new_orgs.lua diff --git a/modpol/api.lua b/modpol/api.lua index c44cf88..b5092de 100644 --- a/modpol/api.lua +++ b/modpol/api.lua @@ -6,10 +6,10 @@ local localdir = modpol.topdir dofile (localdir .. "/users/users.lua") --orgs -dofile (localdir .. "/orgs/orgs.lua") +dofile (localdir .. "/orgs/new_orgs.lua") --interactions dofile (localdir .. "/interactions/interactions.lua") -- messaging functions -dofile (localdir .. "/processes/processes.lua") +dofile (localdir .. "/processes/processes.lua") \ No newline at end of file diff --git a/modpol/modpol.lua b/modpol/modpol.lua index fbdae77..e7c7e70 100644 --- a/modpol/modpol.lua +++ b/modpol/modpol.lua @@ -77,9 +77,9 @@ dofile (topdir .. "/api.lua") -- Final checks -- create instance if not present -if not modpol.get_org_id_by_name('instance') then - modpol.add_org("instance", modpol.list_users()) -end +-- if not modpol.get_org_id_by_name('instance') then +-- modpol.add_org("instance", modpol.list_users()) +-- end modpol.ocutil.log ("modpol loaded") diff --git a/modpol/orgs/new_orgs.lua b/modpol/orgs/new_orgs.lua new file mode 100644 index 0000000..9016173 --- /dev/null +++ b/modpol/orgs/new_orgs.lua @@ -0,0 +1,74 @@ +modpol.orgs = +{ + count = 1, + list = {} +} + +-- sets modpol.orgs as it's own callback +modpol.orgs.__index = modpol.orgs + +function modpol.orgs:init_instance() + if modpol.orgs.list[1] then + print('Error: instance has already been initialized') + return false + end + + local instance = { + id = 1, + name = "instance", + policies = {}, + members = {}, + ledger = {}, + parent = nil, + children = {}, + properties = {}, + old_names = {} + } + setmetatable(instance, modpol.orgs) + + -- adding instance to org list + modpol.orgs.list[1] = instance + return instance +end + +function modpol.orgs:add_org(name) + if self.id == nil then + print('Error: add_org can only be run by a parent org') + return false + end + + if modpol.ocutil.str_empty(name) then + print('Error: name is required') + return false + end + + -- creating the child sub org + modpol.orgs.count = modpol.orgs.count + 1 + local child_org = { + id = modpol.orgs.count, + name = name, + policies = {}, + members = {}, + ledger = {}, + parent = self.id, + children = {}, + properties = {}, + old_names = {} + } + 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.list[child_org.id] = child_org + return child_org +end + +function modpol.orgs:add_member(user) + table.insert(self.members, user) +end + +function modpol.orgs:list_member() + print(self.members[1]) +end \ No newline at end of file From 0a16d85be28b02778fa73a058dceb7975996a3aa Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Mon, 5 Apr 2021 22:39:20 -0400 Subject: [PATCH 2/3] added remove_org, get_org, and record functions to orgs --- modpol/orgs/new_orgs.lua | 98 +++++++++++++++++++++++++++++++++++----- 1 file changed, 86 insertions(+), 12 deletions(-) diff --git a/modpol/orgs/new_orgs.lua b/modpol/orgs/new_orgs.lua index 9016173..4d466da 100644 --- a/modpol/orgs/new_orgs.lua +++ b/modpol/orgs/new_orgs.lua @@ -4,13 +4,26 @@ modpol.orgs = list = {} } --- sets modpol.orgs as it's own callback +-- sets modpol.orgs as it's own fallback modpol.orgs.__index = modpol.orgs +-- ================================================== +-- returns org when given its id + +function modpol.orgs.get_org(id) + return modpol.orgs.list[id] +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.list[1] then - print('Error: instance has already been initialized') - return false + error_msg = 'Error: instance has already been initialized' + modpol.ocutil.log(error_msg) + return false, error_msg end local instance = { @@ -31,17 +44,57 @@ function modpol.orgs:init_instance() return instance end -function modpol.orgs:add_org(name) - if self.id == nil then - print('Error: add_org can only be run by a parent org') +-- ======================================================= +-- 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 + print('Error: msg must be a non empty string') return false end - if modpol.ocutil.str_empty(name) then - print('Error: name is required') + if type(entry_type) == 'string' and not(modpol.ocutil.str_empty(entry_type)) then + entry.entry_type = entry_type + else + print('Error: entry_type must be a non empty string') 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 +-- ex: instance:add_org('town hall') + +function modpol.orgs:add_org(name) + if self.id == nil then + error_msg = 'Error: add_org can only be called by another org' + modpol.ocutil.log(error_msg) + return false, error_msg + end + + if modpol.ocutil.str_empty(name) then + error_msg = 'Error: org name is required' + modpol.ocutil.log(error_msg) + return false, error_msg + end + -- creating the child sub org modpol.orgs.count = modpol.orgs.count + 1 local child_org = { @@ -49,11 +102,8 @@ function modpol.orgs:add_org(name) name = name, policies = {}, members = {}, - ledger = {}, parent = self.id, children = {}, - properties = {}, - old_names = {} } setmetatable(child_org, modpol.orgs) @@ -62,13 +112,37 @@ function modpol.orgs:add_org(name) -- adding child to org list modpol.orgs.list[child_org.id] = child_org + + return child_org end +-- ======================================== +-- recursively deletes an org and its suborgs +-- note: "reason" param was removed, can be added back + +function modpol.orgs:delete() + if self.id == 1 then + return false, 'Error: cannot delete instance' + end + + if #self.children > 0 then + for i, child_id in pairs(self.children) do + local child = modpol.orgs.get_org(child_id) + print(child_id, child) + child:delete() + end + end + + modpol.orgs.list[self.id] = nil + print('Removed ' .. self.name .. ': ' .. self.id) + +end + function modpol.orgs:add_member(user) table.insert(self.members, user) end function modpol.orgs:list_member() print(self.members[1]) -end \ No newline at end of file +end From 4557e9c7d833af2ae014d2bba4b36f6487add537 Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Sat, 10 Apr 2021 21:09:29 -0400 Subject: [PATCH 3/3] completed refactor --- modpol/orgs/new_orgs.lua | 99 +++++++++++++++++++++++++++++++++++----- 1 file changed, 88 insertions(+), 11 deletions(-) diff --git a/modpol/orgs/new_orgs.lua b/modpol/orgs/new_orgs.lua index 4d466da..586f656 100644 --- a/modpol/orgs/new_orgs.lua +++ b/modpol/orgs/new_orgs.lua @@ -9,16 +9,40 @@ modpol.orgs.__index = modpol.orgs -- ================================================== -- returns org when given its id - function modpol.orgs.get_org(id) return modpol.orgs.list[id] end +-- =============================================== +-- returns a string list of all orgs +function modpol.orgs.list_orgs() + local str + for k, v in ipairs(modpol.orgs.list) do + if type(v) == 'table' then + if str then + str = str .. '\n' .. v.name + else + str = v.name + end + end + end + return str +end + +-- =========================================== +-- deletes all orgs except for the instance +function modpol.orgs.reset() + for k, v in ipairs(modpol.orgs.list) do + if k > 1 then + modpol.orgs.list[k] = nil + end + end +end + -- =================================================== -- initializes the instance (root org) -- can only be run once, as only one instance can exist - -function modpol.orgs:init_instance() +function modpol.orgs.init_instance() local error_msg if modpol.orgs.list[1] then error_msg = 'Error: instance has already been initialized' @@ -44,9 +68,11 @@ function modpol.orgs:init_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 = '', @@ -81,7 +107,6 @@ end -- ================================================== -- adds a new sub org to the org it is called on -- ex: instance:add_org('town hall') - function modpol.orgs:add_org(name) if self.id == nil then error_msg = 'Error: add_org can only be called by another org' @@ -119,8 +144,8 @@ end -- ======================================== -- recursively deletes an org and its suborgs +-- leaves entry in modpol.orgs.list as a string "removed" -- note: "reason" param was removed, can be added back - function modpol.orgs:delete() if self.id == 1 then return false, 'Error: cannot delete instance' @@ -134,15 +159,67 @@ function modpol.orgs:delete() end end - modpol.orgs.list[self.id] = nil + modpol.orgs.list[self.id] = 'removed' print('Removed ' .. self.name .. ': ' .. self.id) end -function modpol.orgs:add_member(user) - table.insert(self.members, user) + +-- =========================================== +-- 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 -function modpol.orgs:list_member() - print(self.members[1]) +-- =========================================== +-- adds a user to an org +function modpol.orgs:add_member(user) + -- trys to fill in empty spots first + 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 +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 + user_index = self:get_member_index(user) + if user_index then + self.members[user_index] = '' + end +end + +-- =========================================== +-- boolean check whether user is an org +function modpol.orgs:has_member(user) + user_index = self:get_member_index(user) + if user_index then + return true + else + return false + end +end + +-- ================================== +-- returns a list of users in an org +function modpol.orgs:list_member() + local str + for k, v in ipairs(self.members) do + if str then + str = str .. '\n' .. v + else + str = v + end + end + return str end