liquid_feedback_frontend
changeset 1532:3c15fea3f1c0
Added FirstLife group mirroring
author | bsw |
---|---|
date | Sun Oct 04 16:31:47 2020 +0200 (2020-10-04) |
parents | 5c925bcc9036 |
children | d432f85e868e |
files | app/main/index/_head.lua lib/firstlife/groups.lua |
line diff
1.1 --- a/app/main/index/_head.lua Mon Sep 21 18:46:48 2020 +0200 1.2 +++ b/app/main/index/_head.lua Sun Oct 04 16:31:47 2020 +0200 1.3 @@ -135,6 +135,9 @@ 1.4 if unit.description and #(unit.description) > 0 then 1.5 ui.container{ attr = { class = "mdl-card__supporting-text mdl-card--border" }, content = unit.description } 1.6 end 1.7 + if config.render_external_reference_unit then 1.8 + config.render_external_reference_unit(unit) 1.9 + end 1.10 --ui.container{ attr = { class = "mdl-card__actions mdl-card--border" }, content = function() 1.11 --end } 1.12 end }
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/lib/firstlife/groups.lua Sun Oct 04 16:31:47 2020 +0200 2.3 @@ -0,0 +1,115 @@ 2.4 +local function firstlife_mirror_group_users(unit) 2.5 + local url = config.firstlife_groups.api_base_url .. "v6/fl/Things/" .. unit.attr.firstlife_id .. "/participants" 2.6 + 2.7 + local output, err, status = extos.pfilter(doc, "curl", "-X", "GET", "-H", "Content-Type: application/json", "-d", "@-", url) 2.8 + 2.9 + local data = json.import(output) 2.10 + 2.11 + local old_privileges_list = Privilege:new_selector() 2.12 + :add_where{ "unit_id = ?", unit.id } 2.13 + :exec() 2.14 + 2.15 + local old_privileges = {} 2.16 + for i, old_privilege in ipairs(old_privileges_list) do 2.17 + old_privileges[old_privilege.member_id] = old_privilege 2.18 + end 2.19 + 2.20 + local new_user_hash = {} 2.21 + 2.22 + for i, user in ipairs(data) do 2.23 + print(" Processing user ID " .. user.id) 2.24 + local user_id = tonumber(string.match(user.id, "^(.+)@")) 2.25 + new_user_hash[user_id] = user 2.26 + if old_privileges[user_id] then 2.27 + print(" Privilege entry exists") 2.28 + else 2.29 + print(" Creating new privilege") 2.30 + local privilege = Privilege:new() 2.31 + privilege.unit_id = unit.id 2.32 + privilege.member_id = user_id 2.33 + privilege.initiative_right = true 2.34 + privilege.voting_right = true 2.35 + privilege.weight = 1 2.36 + privilege:save() 2.37 + end 2.38 + end 2.39 + 2.40 + for i, old_privilege in ipairs(old_privileges_list) do 2.41 + if not new_user_hash[old_privilege.member_id] then 2.42 + print(" Destroying privilege for user ID " .. old_privilege.member_id) 2.43 + old_privilege:destroy() 2.44 + end 2.45 + end 2.46 + 2.47 +end 2.48 + 2.49 +function _G.firstlife_mirror_groups() 2.50 + 2.51 + 2.52 + local url = config.firstlife_groups.api_base_url .. "v6/fl/Things/search?types=CO3_ACA" 2.53 + 2.54 + local output, err, status = extos.pfilter(doc, "curl", "-X", "GET", "-H", "Content-Type: application/json", "-d", "@-", url) 2.55 + 2.56 + local data = json.import(output) 2.57 + 2.58 + if not data then return end 2.59 + if not data.things then return end 2.60 + if data.things.type ~= "FeatureCollection" then return end 2.61 + if not data.things.features then return end 2.62 + if json.type(data.things.features) ~= "array" then return end 2.63 + 2.64 + local units_new = {} 2.65 + 2.66 + for i, feature in ipairs(data.things.features) do 2.67 + print(feature.id, feature.properties.name) 2.68 + units_new[feature.id] = feature 2.69 + end 2.70 + 2.71 + local old_units_list = Unit:new_selector() 2.72 + :add_where("attr->'firstlife_id' NOTNULL") 2.73 + :exec() 2.74 + 2.75 + local old_units = {} 2.76 + 2.77 + for i, old_unit in ipairs(old_units_list) do 2.78 + old_units[old_unit.attr.firstlife_id] = old_unit 2.79 + end 2.80 + 2.81 + for id, unit_new in pairs(units_new) do 2.82 + local name_new = unit_new.properties.name 2.83 + local unit 2.84 + print("Processing unit ID " .. id .. " with name " .. name_new) 2.85 + if old_units[id] then 2.86 + unit = old_units[id] 2.87 + print(" Unit already exists") 2.88 + if old_units[id].name == name_new then 2.89 + print(" Name not changed") 2.90 + else 2.91 + print(" Name changed, updating") 2.92 + old_units[id].name = name_new 2.93 + old_units[id]:save() 2.94 + end 2.95 + else 2.96 + print(" Creating as new unit") 2.97 + local u = Unit:new() 2.98 + u.name = name_new 2.99 + u.attr = json.object() 2.100 + u.attr.firstlife_id = id 2.101 + u:save() 2.102 + local area = Area:new() 2.103 + area.unit_id = u.id 2.104 + area.name = config.firstlife_groups.area_name 2.105 + area:save() 2.106 + local allowed_policy = AllowedPolicy:new() 2.107 + allowed_policy.area_id = area.id 2.108 + allowed_policy.policy_id = config.firstlife_groups.policy_id 2.109 + allowed_policy.default_policy = true 2.110 + allowed_policy:save() 2.111 + unit = u 2.112 + end 2.113 + firstlife_mirror_group_users(unit) 2.114 + end 2.115 + 2.116 +end 2.117 + 2.118 +