bsw@1532: local function firstlife_mirror_group_users(unit) bsw@1532: local url = config.firstlife_groups.api_base_url .. "v6/fl/Things/" .. unit.attr.firstlife_id .. "/participants" bsw@1532: bsw@1532: local output, err, status = extos.pfilter(doc, "curl", "-X", "GET", "-H", "Content-Type: application/json", "-d", "@-", url) bsw@1532: bsw@1532: local data = json.import(output) bsw@1532: bsw@1532: local old_privileges_list = Privilege:new_selector() bsw@1532: :add_where{ "unit_id = ?", unit.id } bsw@1532: :exec() bsw@1532: bsw@1532: local old_privileges = {} bsw@1532: for i, old_privilege in ipairs(old_privileges_list) do bsw@1532: old_privileges[old_privilege.member_id] = old_privilege bsw@1532: end bsw@1532: bsw@1532: local new_user_hash = {} bsw@1532: bsw@1532: for i, user in ipairs(data) do bsw@1533: -- print(" Processing user ID " .. user.id) bsw@1532: local user_id = tonumber(string.match(user.id, "^(.+)@")) bsw@1532: new_user_hash[user_id] = user bsw@1532: if old_privileges[user_id] then bsw@1533: -- print(" Privilege entry exists") bsw@1532: else bsw@1533: -- print(" Creating new privilege") bsw@1532: local privilege = Privilege:new() bsw@1532: privilege.unit_id = unit.id bsw@1532: privilege.member_id = user_id bsw@1532: privilege.initiative_right = true bsw@1532: privilege.voting_right = true bsw@1532: privilege.weight = 1 bsw@1532: privilege:save() bsw@1532: end bsw@1532: end bsw@1532: bsw@1532: for i, old_privilege in ipairs(old_privileges_list) do bsw@1532: if not new_user_hash[old_privilege.member_id] then bsw@1533: -- print(" Destroying privilege for user ID " .. old_privilege.member_id) bsw@1532: old_privilege:destroy() bsw@1532: end bsw@1532: end bsw@1532: bsw@1532: end bsw@1532: bsw@1532: function _G.firstlife_mirror_groups() bsw@1532: bsw@1532: bsw@1532: local url = config.firstlife_groups.api_base_url .. "v6/fl/Things/search?types=CO3_ACA" bsw@1532: bsw@1532: local output, err, status = extos.pfilter(doc, "curl", "-X", "GET", "-H", "Content-Type: application/json", "-d", "@-", url) bsw@1532: bsw@1532: local data = json.import(output) bsw@1532: bsw@1532: if not data then return end bsw@1532: if not data.things then return end bsw@1532: if data.things.type ~= "FeatureCollection" then return end bsw@1532: if not data.things.features then return end bsw@1532: if json.type(data.things.features) ~= "array" then return end bsw@1532: bsw@1532: local units_new = {} bsw@1532: bsw@1532: for i, feature in ipairs(data.things.features) do bsw@1533: -- print(feature.id, feature.properties.name) bsw@1532: units_new[feature.id] = feature bsw@1532: end bsw@1532: bsw@1532: local old_units_list = Unit:new_selector() bsw@1532: :add_where("attr->'firstlife_id' NOTNULL") bsw@1532: :exec() bsw@1532: bsw@1532: local old_units = {} bsw@1532: bsw@1532: for i, old_unit in ipairs(old_units_list) do bsw@1532: old_units[old_unit.attr.firstlife_id] = old_unit bsw@1532: end bsw@1532: bsw@1532: for id, unit_new in pairs(units_new) do bsw@1532: local name_new = unit_new.properties.name bsw@1532: local unit bsw@1533: -- print("Processing unit ID " .. id .. " with name " .. name_new) bsw@1532: if old_units[id] then bsw@1532: unit = old_units[id] bsw@1533: -- print(" Unit already exists") bsw@1532: if old_units[id].name == name_new then bsw@1533: -- print(" Name not changed") bsw@1532: else bsw@1533: -- print(" Name changed, updating") bsw@1532: old_units[id].name = name_new bsw@1532: old_units[id]:save() bsw@1532: end bsw@1532: else bsw@1533: -- print(" Creating as new unit") bsw@1532: local u = Unit:new() bsw@1532: u.name = name_new bsw@1532: u.attr = json.object() bsw@1532: u.attr.firstlife_id = id bsw@1532: u:save() bsw@1532: local area = Area:new() bsw@1532: area.unit_id = u.id bsw@1532: area.name = config.firstlife_groups.area_name bsw@1532: area:save() bsw@1532: local allowed_policy = AllowedPolicy:new() bsw@1532: allowed_policy.area_id = area.id bsw@1532: allowed_policy.policy_id = config.firstlife_groups.policy_id bsw@1532: allowed_policy.default_policy = true bsw@1532: allowed_policy:save() bsw@1532: unit = u bsw@1532: end bsw@1532: firstlife_mirror_group_users(unit) bsw@1532: end bsw@1532: bsw@1532: end bsw@1532: