liquid_feedback_frontend
view lib/firstlife/groups.lua @ 1574:be96623e575a
Allow multiple ids for member endpoint
| author | bsw | 
|---|---|
| date | Mon Nov 23 14:04:42 2020 +0100 (2020-11-23) | 
| parents | d432f85e868e | 
| children | 
 line source
     1 local function firstlife_mirror_group_users(unit)
     2   local url = config.firstlife_groups.api_base_url .. "v6/fl/Things/" .. unit.attr.firstlife_id .. "/participants"
     4   local output, err, status = extos.pfilter(doc, "curl", "-X", "GET", "-H", "Content-Type: application/json", "-d", "@-", url)
     6   local data = json.import(output)
     8   local old_privileges_list = Privilege:new_selector()
     9     :add_where{ "unit_id = ?", unit.id }
    10     :exec()
    12   local old_privileges = {}
    13   for i, old_privilege in ipairs(old_privileges_list) do
    14     old_privileges[old_privilege.member_id] = old_privilege
    15   end
    17   local new_user_hash = {}
    19   for i, user in ipairs(data) do
    20 --    print("  Processing user ID " .. user.id)
    21     local user_id = tonumber(string.match(user.id, "^(.+)@"))
    22     new_user_hash[user_id] = user
    23     if old_privileges[user_id] then
    24 --      print("    Privilege entry exists")
    25     else
    26 --      print("    Creating new privilege")
    27       local privilege = Privilege:new()
    28       privilege.unit_id = unit.id
    29       privilege.member_id = user_id
    30       privilege.initiative_right = true
    31       privilege.voting_right = true
    32       privilege.weight = 1
    33       privilege:save()
    34     end
    35   end
    37   for i, old_privilege in ipairs(old_privileges_list) do
    38     if not new_user_hash[old_privilege.member_id] then
    39 --      print("  Destroying privilege for user ID " .. old_privilege.member_id)
    40       old_privilege:destroy()
    41     end
    42   end
    44 end
    46 function _G.firstlife_mirror_groups()
    49   local url = config.firstlife_groups.api_base_url .. "v6/fl/Things/search?types=CO3_ACA"
    51   local output, err, status = extos.pfilter(doc, "curl", "-X", "GET", "-H", "Content-Type: application/json", "-d", "@-", url)
    53   local data = json.import(output)
    55   if not data then return end
    56   if not data.things then return end
    57   if data.things.type ~= "FeatureCollection" then return end
    58   if not data.things.features then return end
    59   if json.type(data.things.features) ~= "array" then return end
    61   local units_new = {}
    63   for i, feature in ipairs(data.things.features) do
    64 --    print(feature.id, feature.properties.name)
    65     units_new[feature.id] = feature
    66   end
    68   local old_units_list = Unit:new_selector()
    69     :add_where("attr->'firstlife_id' NOTNULL")
    70     :exec()
    72   local old_units = {}
    74   for i, old_unit in ipairs(old_units_list) do
    75     old_units[old_unit.attr.firstlife_id] = old_unit
    76   end
    78   for id, unit_new in pairs(units_new) do
    79     local name_new = unit_new.properties.name
    80     local unit
    81 --    print("Processing unit ID " .. id .. " with name " .. name_new)
    82     if old_units[id] then
    83       unit = old_units[id]
    84 --      print("  Unit already exists")
    85       if old_units[id].name == name_new then
    86 --        print("  Name not changed")
    87       else
    88 --        print("  Name changed, updating")
    89         old_units[id].name = name_new
    90         old_units[id]:save()
    91       end
    92     else          
    93 --      print("  Creating as new unit")
    94       local u = Unit:new()
    95       u.name = name_new
    96       u.attr = json.object()
    97       u.attr.firstlife_id = id
    98       u:save()
    99       local area = Area:new()
   100       area.unit_id = u.id
   101       area.name = config.firstlife_groups.area_name
   102       area:save()
   103       local allowed_policy = AllowedPolicy:new()
   104       allowed_policy.area_id = area.id
   105       allowed_policy.policy_id = config.firstlife_groups.policy_id
   106       allowed_policy.default_policy = true
   107       allowed_policy:save()
   108       unit = u
   109     end
   110     firstlife_mirror_group_users(unit)
   111   end
   113 end
