liquid_feedback_frontend
changeset 1850:accb808d5b4a
Added token based voting
author | bsw |
---|---|
date | Tue Mar 22 10:19:22 2022 +0100 (2022-03-22) |
parents | c3dba3a85be3 |
children | a01d5c0604de |
files | app/main/_prefork/10_init.lua env/ui/supporter_count.lua lib/token/sync.lua |
line diff
1.1 --- a/app/main/_prefork/10_init.lua Thu Feb 03 16:10:39 2022 +0100 1.2 +++ b/app/main/_prefork/10_init.lua Tue Mar 22 10:19:22 2022 +0100 1.3 @@ -283,5 +283,21 @@ 1.4 } 1.5 end 1.6 1.7 +if config.token then 1.8 + assert(loadcached(encode.file_path(WEBMCP_BASE_PATH, "lib", "token", "sync.lua")))() 1.9 + listen{ 1.10 + { 1.11 + proto = "interval", 1.12 + name = "sync_token", 1.13 + delay = 5, 1.14 + handler = function() 1.15 + sync_token() 1.16 + end 1.17 + }, 1.18 + min_fork = 1, 1.19 + max_fork = 1 1.20 + } 1.21 +end 1.22 + 1.23 execute.inner() 1.24
2.1 --- a/env/ui/supporter_count.lua Thu Feb 03 16:10:39 2022 +0100 2.2 +++ b/env/ui/supporter_count.lua Tue Mar 22 10:19:22 2022 +0100 2.3 @@ -5,18 +5,34 @@ 2.4 content = _"[calculating]" 2.5 } 2.6 elseif initiative.issue.closed == nil then 2.7 - ui.tag { 2.8 - attr = { class = "satisfiedSupporterCount" }, 2.9 - content = _("#{count} supporter", { count = initiative.satisfied_supporter_count }) 2.10 - } 2.11 - if initiative.potential_supporter_count and 2.12 - initiative.potential_supporter_count > 0 2.13 - then 2.14 - slot.put ( " " ) 2.15 + if config.token and initiative.issue.area.unit_id == config.token.unit_id then 2.16 ui.tag { 2.17 - attr = { class = "potentialSupporterCount" }, 2.18 - content = _("(+ #{count} potential)", { count = initiative.potential_supporter_count }) 2.19 + attr = { class = "satisfiedSupporterCount" }, 2.20 + content = _("#{count} #{token_name}", { count = initiative.satisfied_supporter_count / 100, token_name = config.token.token_name }) 2.21 } 2.22 + if initiative.potential_supporter_count and 2.23 + initiative.potential_supporter_count > 0 2.24 + then 2.25 + slot.put ( " " ) 2.26 + ui.tag { 2.27 + attr = { class = "potentialSupporterCount" }, 2.28 + content = _("(+ #{count} potential)", { count = initiative.potential_supporter_count / 100 }) 2.29 + } 2.30 + end 2.31 + else 2.32 + ui.tag { 2.33 + attr = { class = "satisfiedSupporterCount" }, 2.34 + content = _("#{count} supporter", { count = initiative.satisfied_supporter_count }) 2.35 + } 2.36 + if initiative.potential_supporter_count and 2.37 + initiative.potential_supporter_count > 0 2.38 + then 2.39 + slot.put ( " " ) 2.40 + ui.tag { 2.41 + attr = { class = "potentialSupporterCount" }, 2.42 + content = _("(+ #{count} potential)", { count = initiative.potential_supporter_count }) 2.43 + } 2.44 + end 2.45 end 2.46 end 2.47 end
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/lib/token/sync.lua Tue Mar 22 10:19:22 2022 +0100 3.3 @@ -0,0 +1,52 @@ 3.4 +function _G.sync_token() 3.5 + local request = [[ 3.6 + { 3.7 + "query": "query balanceNotificationMany($tokenSymbol: String) { 3.8 + balanceNotificationMany(filter: { token_symbol: $tokenSymbol }) { 3.9 + _id 3.10 + account_pk 3.11 + name 3.12 + token_symbol 3.13 + amount 3.14 + contractAddress 3.15 + owner 3.16 + } 3.17 + }", 3.18 + "variables": { 3.19 + "tokenSymbol": "]] .. config.token.token_name .. [[" 3.20 + } 3.21 + } 3.22 + ]] 3.23 + 3.24 + local output, err, status = extos.pfilter(request, "curl", "--insecure", "-X", "POST", "-H", "Content-Type: application/json", "-H", "X-REQUEST-TYPE: GraphQL", "-d", "@-", config.token.graphql_url) 3.25 + 3.26 + local data = json.import(output) 3.27 + 3.28 + local privileges = Privilege:new_selector() 3.29 + :add_where{ "unit_id = ?", config.token.unit_id } 3.30 + :exec() 3.31 + 3.32 + for i, privilege in ipairs(privileges) do 3.33 + privilege:destroy() 3.34 + end 3.35 + 3.36 + for i, entry in ipairs(data.data.balanceNotificationMany) do 3.37 + print(entry.account_pk, entry.amount) 3.38 + local member = Member:new_selector() 3.39 + :join("member_profile", nil, "member_profile.member_id = member.id") 3.40 + :add_where{ "member_profile.profile->>'" .. config.token.key_profile_field .."' = ?", entry.account_pk } 3.41 + :optional_object_mode() 3.42 + :exec() 3.43 + print(member.name) 3.44 + if member then 3.45 + local privilege = Privilege:new() 3.46 + privilege.unit_id = config.token.unit_id 3.47 + privilege.member_id = member.id 3.48 + privilege.initiative_right = true 3.49 + privilege.voting_right = true 3.50 + privilege.weight = entry.amount 3.51 + privilege:save() 3.52 + end 3.53 + end 3.54 + 3.55 +end 3.56 \ No newline at end of file