liquid_feedback_frontend

diff lib/firstlife/groups.lua @ 1532:3c15fea3f1c0

Added FirstLife group mirroring
author bsw
date Sun Oct 04 16:31:47 2020 +0200 (2020-10-04)
parents
children d432f85e868e
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lib/firstlife/groups.lua	Sun Oct 04 16:31:47 2020 +0200
     1.3 @@ -0,0 +1,115 @@
     1.4 +local function firstlife_mirror_group_users(unit)
     1.5 +  local url = config.firstlife_groups.api_base_url .. "v6/fl/Things/" .. unit.attr.firstlife_id .. "/participants"
     1.6 +  
     1.7 +  local output, err, status = extos.pfilter(doc, "curl", "-X", "GET", "-H", "Content-Type: application/json", "-d", "@-", url)
     1.8 +  
     1.9 +  local data = json.import(output)
    1.10 +
    1.11 +  local old_privileges_list = Privilege:new_selector()
    1.12 +    :add_where{ "unit_id = ?", unit.id }
    1.13 +    :exec()
    1.14 +
    1.15 +  local old_privileges = {}
    1.16 +  for i, old_privilege in ipairs(old_privileges_list) do
    1.17 +    old_privileges[old_privilege.member_id] = old_privilege
    1.18 +  end
    1.19 +
    1.20 +  local new_user_hash = {}
    1.21 +
    1.22 +  for i, user in ipairs(data) do
    1.23 +    print("  Processing user ID " .. user.id)
    1.24 +    local user_id = tonumber(string.match(user.id, "^(.+)@"))
    1.25 +    new_user_hash[user_id] = user
    1.26 +    if old_privileges[user_id] then
    1.27 +      print("    Privilege entry exists")
    1.28 +    else
    1.29 +      print("    Creating new privilege")
    1.30 +      local privilege = Privilege:new()
    1.31 +      privilege.unit_id = unit.id
    1.32 +      privilege.member_id = user_id
    1.33 +      privilege.initiative_right = true
    1.34 +      privilege.voting_right = true
    1.35 +      privilege.weight = 1
    1.36 +      privilege:save()
    1.37 +    end
    1.38 +  end
    1.39 +
    1.40 +  for i, old_privilege in ipairs(old_privileges_list) do
    1.41 +    if not new_user_hash[old_privilege.member_id] then
    1.42 +      print("  Destroying privilege for user ID " .. old_privilege.member_id)
    1.43 +      old_privilege:destroy()
    1.44 +    end
    1.45 +  end
    1.46 +
    1.47 +end
    1.48 +
    1.49 +function _G.firstlife_mirror_groups()
    1.50 +
    1.51 +
    1.52 +  local url = config.firstlife_groups.api_base_url .. "v6/fl/Things/search?types=CO3_ACA"
    1.53 +  
    1.54 +  local output, err, status = extos.pfilter(doc, "curl", "-X", "GET", "-H", "Content-Type: application/json", "-d", "@-", url)
    1.55 +  
    1.56 +  local data = json.import(output)
    1.57 +
    1.58 +  if not data then return end
    1.59 +  if not data.things then return end
    1.60 +  if data.things.type ~= "FeatureCollection" then return end
    1.61 +  if not data.things.features then return end
    1.62 +  if json.type(data.things.features) ~= "array" then return end
    1.63 +
    1.64 +  local units_new = {}
    1.65 +
    1.66 +  for i, feature in ipairs(data.things.features) do
    1.67 +    print(feature.id, feature.properties.name)
    1.68 +    units_new[feature.id] = feature
    1.69 +  end
    1.70 +
    1.71 +  local old_units_list = Unit:new_selector()
    1.72 +    :add_where("attr->'firstlife_id' NOTNULL")
    1.73 +    :exec()
    1.74 +
    1.75 +  local old_units = {}
    1.76 +
    1.77 +  for i, old_unit in ipairs(old_units_list) do
    1.78 +    old_units[old_unit.attr.firstlife_id] = old_unit
    1.79 +  end
    1.80 +
    1.81 +  for id, unit_new in pairs(units_new) do
    1.82 +    local name_new = unit_new.properties.name
    1.83 +    local unit
    1.84 +    print("Processing unit ID " .. id .. " with name " .. name_new)
    1.85 +    if old_units[id] then
    1.86 +      unit = old_units[id]
    1.87 +      print("  Unit already exists")
    1.88 +      if old_units[id].name == name_new then
    1.89 +        print("  Name not changed")
    1.90 +      else
    1.91 +        print("  Name changed, updating")
    1.92 +        old_units[id].name = name_new
    1.93 +        old_units[id]:save()
    1.94 +      end
    1.95 +    else          
    1.96 +      print("  Creating as new unit")
    1.97 +      local u = Unit:new()
    1.98 +      u.name = name_new
    1.99 +      u.attr = json.object()
   1.100 +      u.attr.firstlife_id = id
   1.101 +      u:save()
   1.102 +      local area = Area:new()
   1.103 +      area.unit_id = u.id
   1.104 +      area.name = config.firstlife_groups.area_name
   1.105 +      area:save()
   1.106 +      local allowed_policy = AllowedPolicy:new()
   1.107 +      allowed_policy.area_id = area.id
   1.108 +      allowed_policy.policy_id = config.firstlife_groups.policy_id
   1.109 +      allowed_policy.default_policy = true
   1.110 +      allowed_policy:save()
   1.111 +      unit = u
   1.112 +    end
   1.113 +    firstlife_mirror_group_users(unit)
   1.114 +  end
   1.115 +
   1.116 +end
   1.117 +
   1.118 +

Impressum / About Us