liquid_feedback_frontend
view app/main/api/event.lua @ 1618:2f9e1e882de6
Add/remove interest via fetch
| author | bsw | 
|---|---|
| date | Mon Feb 01 23:43:54 2021 +0100 (2021-02-01) | 
| parents | 32cc544d5a5b | 
| children | 
 line source
     1 slot.set_layout(nil, "application/json")
     3 local r = json.object{
     4   result = json.array{}
     5 }
     7 local selector = Event:new_selector()
     9 local member_id = param.get("member_id", atom.integer)
    10 local other_member_id = param.get("member_id", atom.integer)
    11 local scope = param.get("scope")
    12 local issue_id = param.get("issue_id", atom.integer)
    13 local state = param.get("state")
    14 local initiative_id = param.get("initiative_id", atom.integer)
    16 local include_members = param.get("include_members", atom.boolean)
    17 local include_other_members = param.get("include_other_members", atom.boolean)
    18 local include_profile = param.get("include_profile", atom.boolean)
    19 local include_issues = param.get("include_issues", atom.boolean)
    20 local include_initiatives = param.get("include_initiatives", atom.boolean)
    21 local include_drafts = param.get("include_drafts", atom.boolean)
    22 local include_suggestions = param.get("include_suggestions", atom.boolean)
    24 if member_id then
    25   selector:add_where{ "member_id = ?", member_id }
    26 end
    28 if other_member_id then
    29   selector:add_where{ "other_member_id = ?", other_member_id }
    30 end
    32 if scope then
    33   selector:add_where{ "scope = ?", scope }
    34 end
    36 if issue_id then
    37   selector:add_where{ "issue_id = ?", issue_id }
    38 end
    40 if scope then
    41   selector:add_where{ "scope = ?", scope }
    42 end
    44 if initiative_id then
    45   selector:add_where{ "initiative_id = ?", initiative_id }
    46 end
    48 selector:add_order_by("id DESC")
    50 local events = selector:exec()
    52 local member_ids = {}
    53 local issue_ids = {}
    54 local initiative_ids = {}
    55 local draft_ids = {}
    56 local suggestion_ids = {}
    58 for i, event in ipairs(events) do
    59   local e = json.object()
    60   e.id = event.id
    61   e.occurrence = format.timestamp(event.occurrence)
    62   e.event = event.event
    63   e.member_id = event.member_id
    64   e.other_member_id = event.other_member_id
    65   e.scope = event.scope
    66   e.issue_id = event.issue_id
    67   e.state = event.state
    68   e.initiative_id = event.initiative_id
    69   e.draft_id = event.draft_id
    70   e.suggestion_id = event.suggestion_id
    71   e.value = event.value
    72   if include_members and e.member_id then
    73     member_ids[e.member_id] = true
    74   end
    75   if include_other_members and e.other_member_id then
    76     member_ids[e.member_id] = true
    77   end
    78   if include_issues and e.issue_id then
    79     issue_ids[e.issue_id] = true
    80   end
    81   if include_initiatives and e.initiative_id then
    82     initiative_ids[e.initiative_id] = true
    83   end
    84   if include_drafts and e.draft_id then
    85     draft_ids[e.draft_id] = true
    86   end
    87   if include_suggestions and e.suggestion_id then
    88     suggestion_ids[e.suggestion_id] = true
    89   end
    90   r.result[#r.result+1] = e
    91 end
    93 function util.keys_to_array(tbl)
    94   local r = {}
    95   for k, v in pairs(tbl) do
    96     r[#r+1] = k
    97   end
    98   return r
    99 end
   101 function util.array_to_json_object(tbl, key)
   102   local r = json.object()
   103   for i, v in ipairs(tbl) do
   104     r[v[key]] = v
   105   end
   106   return r
   107 end
   109 if next(member_ids) then
   110   local members = Member:by_ids(util.keys_to_array(member_ids))
   111   r.members = util.array_to_json_object(
   112     execute.chunk{ module = "api", chunk = "_member", params = { members = members, include_profile = include_profile } },
   113     "id"
   114   )
   115   if r.members == false then
   116     return
   117   end
   118 end
   120 if next(issue_ids) then
   121   local issues = Issue:by_ids(util.keys_to_array(issue_ids))
   122   r.issues = util.array_to_json_object(
   123     execute.chunk{ module = "api", chunk = "_issue", params = { issues = issues } },
   124     "id"
   125   )
   126   if r.issues == false then
   127     return
   128   end
   129 end
   132 slot.put_into("data", json.export(r))
