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