bsw/jbe@1309: slot.set_layout(nil, "application/json") bsw/jbe@1309: bsw/jbe@1309: local r = json.object{ bsw/jbe@1309: result = json.array{} bsw/jbe@1309: } bsw/jbe@1309: bsw/jbe@1309: local selector = Event:new_selector() bsw/jbe@1309: bsw/jbe@1309: local member_id = param.get("member_id", atom.integer) bsw/jbe@1309: local other_member_id = param.get("member_id", atom.integer) bsw/jbe@1309: local scope = param.get("scope") bsw/jbe@1309: local issue_id = param.get("issue_id", atom.integer) bsw/jbe@1309: local state = param.get("state") bsw/jbe@1309: local initiative_id = param.get("initiative_id", atom.integer) bsw/jbe@1309: bsw/jbe@1309: local include_members = param.get("include_members", atom.boolean) bsw/jbe@1309: local include_other_members = param.get("include_other_members", atom.boolean) bsw/jbe@1309: local include_profile = param.get("include_profile", atom.boolean) bsw/jbe@1309: local include_issues = param.get("include_issues", atom.boolean) bsw/jbe@1309: local include_initiatives = param.get("include_initiatives", atom.boolean) bsw/jbe@1309: local include_drafts = param.get("include_drafts", atom.boolean) bsw/jbe@1309: local include_suggestions = param.get("include_suggestions", atom.boolean) bsw/jbe@1309: bsw/jbe@1309: if member_id then bsw/jbe@1309: selector:add_where{ "member_id = ?", member_id } bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: if other_member_id then bsw/jbe@1309: selector:add_where{ "other_member_id = ?", other_member_id } bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: if scope then bsw/jbe@1309: selector:add_where{ "scope = ?", scope } bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: if issue_id then bsw/jbe@1309: selector:add_where{ "issue_id = ?", issue_id } bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: if scope then bsw/jbe@1309: selector:add_where{ "scope = ?", scope } bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: if initiative_id then bsw/jbe@1309: selector:add_where{ "initiative_id = ?", initiative_id } bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: selector:add_order_by("id DESC") bsw/jbe@1309: bsw/jbe@1309: local events = selector:exec() bsw/jbe@1309: bsw/jbe@1309: local member_ids = {} bsw/jbe@1309: local issue_ids = {} bsw/jbe@1309: local initiative_ids = {} bsw/jbe@1309: local draft_ids = {} bsw/jbe@1309: local suggestion_ids = {} bsw/jbe@1309: bsw/jbe@1309: for i, event in ipairs(events) do bsw/jbe@1309: local e = json.object() bsw/jbe@1309: e.id = event.id bsw/jbe@1309: e.occurrence = format.timestamp(event.occurrence) bsw/jbe@1309: e.event = event.event bsw/jbe@1309: e.member_id = event.member_id bsw/jbe@1309: e.other_member_id = event.other_member_id bsw/jbe@1309: e.scope = event.scope bsw/jbe@1309: e.issue_id = event.issue_id bsw/jbe@1309: e.state = event.state bsw/jbe@1309: e.initiative_id = event.initiative_id bsw/jbe@1309: e.draft_id = event.draft_id bsw/jbe@1309: e.suggestion_id = event.suggestion_id bsw/jbe@1309: e.value = event.value bsw/jbe@1309: if include_members and e.member_id then bsw/jbe@1309: member_ids[e.member_id] = true bsw/jbe@1309: end bsw/jbe@1309: if include_other_members and e.other_member_id then bsw/jbe@1309: member_ids[e.member_id] = true bsw/jbe@1309: end bsw/jbe@1309: if include_issues and e.issue_id then bsw/jbe@1309: issue_ids[e.issue_id] = true bsw/jbe@1309: end bsw/jbe@1309: if include_initiatives and e.initiative_id then bsw/jbe@1309: initiative_ids[e.initiative_id] = true bsw/jbe@1309: end bsw/jbe@1309: if include_drafts and e.draft_id then bsw/jbe@1309: draft_ids[e.draft_id] = true bsw/jbe@1309: end bsw/jbe@1309: if include_suggestions and e.suggestion_id then bsw/jbe@1309: suggestion_ids[e.suggestion_id] = true bsw/jbe@1309: end bsw/jbe@1309: r.result[#r.result+1] = e bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: function util.keys_to_array(tbl) bsw/jbe@1309: local r = {} bsw/jbe@1309: for k, v in pairs(tbl) do bsw/jbe@1309: r[#r+1] = k bsw/jbe@1309: end bsw/jbe@1309: return r bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: function util.array_to_json_object(tbl, key) bsw/jbe@1309: local r = json.object() bsw/jbe@1309: for i, v in ipairs(tbl) do bsw/jbe@1309: r[v[key]] = v bsw/jbe@1309: end bsw/jbe@1309: return r bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: if next(member_ids) then bsw/jbe@1309: local members = Member:by_ids(util.keys_to_array(member_ids)) bsw/jbe@1309: r.members = util.array_to_json_object( bsw/jbe@1309: execute.chunk{ module = "api", chunk = "_member", params = { members = members, include_profile = include_profile } }, bsw/jbe@1309: "id" bsw/jbe@1309: ) bsw/jbe@1309: if r.members == false then bsw/jbe@1309: return bsw/jbe@1309: end bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: if next(issue_ids) then bsw/jbe@1309: local issues = Issue:by_ids(util.keys_to_array(issue_ids)) bsw/jbe@1309: r.issues = util.array_to_json_object( bsw/jbe@1309: execute.chunk{ module = "api", chunk = "_issue", params = { issues = issues } }, bsw/jbe@1309: "id" bsw/jbe@1309: ) bsw/jbe@1309: if r.issues == false then bsw/jbe@1309: return bsw/jbe@1309: end bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: bsw/jbe@1309: slot.put_into("data", json.export(r))