liquid_feedback_frontend
diff app/main/api/event.lua @ 1309:32cc544d5a5b
Cumulative patch for upcoming frontend version 4
author | bsw/jbe |
---|---|
date | Sun Jul 15 14:07:29 2018 +0200 (2018-07-15) |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/app/main/api/event.lua Sun Jul 15 14:07:29 2018 +0200 1.3 @@ -0,0 +1,132 @@ 1.4 +slot.set_layout(nil, "application/json") 1.5 + 1.6 +local r = json.object{ 1.7 + result = json.array{} 1.8 +} 1.9 + 1.10 +local selector = Event:new_selector() 1.11 + 1.12 +local member_id = param.get("member_id", atom.integer) 1.13 +local other_member_id = param.get("member_id", atom.integer) 1.14 +local scope = param.get("scope") 1.15 +local issue_id = param.get("issue_id", atom.integer) 1.16 +local state = param.get("state") 1.17 +local initiative_id = param.get("initiative_id", atom.integer) 1.18 + 1.19 +local include_members = param.get("include_members", atom.boolean) 1.20 +local include_other_members = param.get("include_other_members", atom.boolean) 1.21 +local include_profile = param.get("include_profile", atom.boolean) 1.22 +local include_issues = param.get("include_issues", atom.boolean) 1.23 +local include_initiatives = param.get("include_initiatives", atom.boolean) 1.24 +local include_drafts = param.get("include_drafts", atom.boolean) 1.25 +local include_suggestions = param.get("include_suggestions", atom.boolean) 1.26 + 1.27 +if member_id then 1.28 + selector:add_where{ "member_id = ?", member_id } 1.29 +end 1.30 + 1.31 +if other_member_id then 1.32 + selector:add_where{ "other_member_id = ?", other_member_id } 1.33 +end 1.34 + 1.35 +if scope then 1.36 + selector:add_where{ "scope = ?", scope } 1.37 +end 1.38 + 1.39 +if issue_id then 1.40 + selector:add_where{ "issue_id = ?", issue_id } 1.41 +end 1.42 + 1.43 +if scope then 1.44 + selector:add_where{ "scope = ?", scope } 1.45 +end 1.46 + 1.47 +if initiative_id then 1.48 + selector:add_where{ "initiative_id = ?", initiative_id } 1.49 +end 1.50 + 1.51 +selector:add_order_by("id DESC") 1.52 + 1.53 +local events = selector:exec() 1.54 + 1.55 +local member_ids = {} 1.56 +local issue_ids = {} 1.57 +local initiative_ids = {} 1.58 +local draft_ids = {} 1.59 +local suggestion_ids = {} 1.60 + 1.61 +for i, event in ipairs(events) do 1.62 + local e = json.object() 1.63 + e.id = event.id 1.64 + e.occurrence = format.timestamp(event.occurrence) 1.65 + e.event = event.event 1.66 + e.member_id = event.member_id 1.67 + e.other_member_id = event.other_member_id 1.68 + e.scope = event.scope 1.69 + e.issue_id = event.issue_id 1.70 + e.state = event.state 1.71 + e.initiative_id = event.initiative_id 1.72 + e.draft_id = event.draft_id 1.73 + e.suggestion_id = event.suggestion_id 1.74 + e.value = event.value 1.75 + if include_members and e.member_id then 1.76 + member_ids[e.member_id] = true 1.77 + end 1.78 + if include_other_members and e.other_member_id then 1.79 + member_ids[e.member_id] = true 1.80 + end 1.81 + if include_issues and e.issue_id then 1.82 + issue_ids[e.issue_id] = true 1.83 + end 1.84 + if include_initiatives and e.initiative_id then 1.85 + initiative_ids[e.initiative_id] = true 1.86 + end 1.87 + if include_drafts and e.draft_id then 1.88 + draft_ids[e.draft_id] = true 1.89 + end 1.90 + if include_suggestions and e.suggestion_id then 1.91 + suggestion_ids[e.suggestion_id] = true 1.92 + end 1.93 + r.result[#r.result+1] = e 1.94 +end 1.95 + 1.96 +function util.keys_to_array(tbl) 1.97 + local r = {} 1.98 + for k, v in pairs(tbl) do 1.99 + r[#r+1] = k 1.100 + end 1.101 + return r 1.102 +end 1.103 + 1.104 +function util.array_to_json_object(tbl, key) 1.105 + local r = json.object() 1.106 + for i, v in ipairs(tbl) do 1.107 + r[v[key]] = v 1.108 + end 1.109 + return r 1.110 +end 1.111 + 1.112 +if next(member_ids) then 1.113 + local members = Member:by_ids(util.keys_to_array(member_ids)) 1.114 + r.members = util.array_to_json_object( 1.115 + execute.chunk{ module = "api", chunk = "_member", params = { members = members, include_profile = include_profile } }, 1.116 + "id" 1.117 + ) 1.118 + if r.members == false then 1.119 + return 1.120 + end 1.121 +end 1.122 + 1.123 +if next(issue_ids) then 1.124 + local issues = Issue:by_ids(util.keys_to_array(issue_ids)) 1.125 + r.issues = util.array_to_json_object( 1.126 + execute.chunk{ module = "api", chunk = "_issue", params = { issues = issues } }, 1.127 + "id" 1.128 + ) 1.129 + if r.issues == false then 1.130 + return 1.131 + end 1.132 +end 1.133 + 1.134 + 1.135 +slot.put_into("data", json.export(r))