liquid_feedback_frontend
view lib/ontomap/ontomap.lua @ 1538:25ea15b4bd5e
Reworked cookie session control, exceptions for API
author | bsw |
---|---|
date | Tue Oct 20 17:48:49 2020 +0200 (2020-10-20) |
parents | 802245716e0a |
children | 0fce83763138 |
line source
1 function _G.ontomap_get_instances(event)
2 if true then return {} end
3 local url = config.ontomap.base_url .. "instances/Issue?geometries=true&descriptions=true"
4 print("OnToMap>>")
6 local output, err, status = extos.pfilter(doc, "curl", "-X", "GET", "-H", "Content-Type: application/json", "--cert", config.ontomap.client_cert_file, "-d", "@-", url)
7 print(output)
9 local data = json.import(output)
11 if not data then
12 return {}
13 end
15 if data.type ~= "FeatureCollection" or not data.features then
16 return {}
17 end
19 local instances = {}
20 for i, feature in ipairs(data.features) do
21 if feature.geometry then
22 table.insert(instances, {
23 application = feature.applicationName,
24 title = feature.hasName,
25 description = slot.use_temporary(function()
26 ui.link{ external = feature.properties.external_url, text = feature.properties.hasName or "" }
27 ui.container{ content = feature.hasDescription }
28 end),
29 lon = feature.geometry.coordinates[1],
30 lat = feature.geometry.coordinates[2],
31 label = "IMC" .. feature.properties.hasID,
32 type = "Improve My City"
33 })
34 print(feature.applicationName, feature.properties.hasName, feature.properties.hasDescription)
35 end
36 end
37 return instances
39 end
42 local function new_log_event(event, actor, activity_type)
43 local e = json.object{
44 activity_type = activity_type,
45 activity_objects = json.array(),
46 references = json.array(),
47 visibility_details = json.array(),
48 details = json.object()
49 }
50 e.actor = actor
51 e.timestamp = math.floor(event.occurrence_epoch * 1000)
52 return e
53 end
55 local function new_activity_object(attr)
56 return json.object{
57 type = "Feature",
58 geometry = attr.geometry or json.null,
59 properties = json.object{
60 hasType = attr.type,
61 external_url = attr.url
62 }
63 }
64 end
66 local function new_reference_object(url, application, type)
67 return json.object{
68 application = application or config.ontomap.application_ident,
69 external_url = url,
70 type = type
71 }
72 end
74 local function log_to_ontomap(log_events)
75 if json.type(log_events) == "object" then
76 log_events = json.array{ log_events }
77 end
78 for i, log_event in ipairs(log_events) do
79 if #(log_event.activity_objects) == 0 then
80 log_event.activity_objects = nil
81 end
82 if #(log_event.references) == 0 then
83 log_event.references = nil
84 end
85 if #(log_event.visibility_details) == 0 then
86 log_event.visibility_details = nil
87 end
88 if not (next(log_event.details)) then -- TODO
89 log_event.details = nil
90 end
91 end
93 local doc = json.export(json.object{
94 event_list = log_events
95 }, " ")
97 local url = config.ontomap.base_url .. "logger/events"
98 print("OnToMap<<")
99 print(doc)
101 local output, err, status = extos.pfilter(doc, "curl", "-X", "POST", "-H", "Content-Type: application/json", "--cert", config.ontomap.client_cert_file, "-d", "@-", url)
103 print("---------")
104 print(output)
105 print("---------")
107 if err then
108 -- TODO log error
109 end
110 end
112 local function url_for(relation, id)
113 return config.absolute_base_url .. relation .. "/show/" .. id .. ".html"
114 end
117 local function unit_updated(event, event_type)
118 local log_event = new_log_event(event, 0, event_type == "created" and "object_created" or "object_updated")
119 table.insert(log_event.activity_objects, new_activity_object{
120 type = "unit",
121 url = url_for("unit", event.unit_id)
122 })
123 log_event.details.active = event.unit.active
124 log_event.details.name = event.unit.name
125 log_event.details.description = event.unit.description
126 log_event.details.external_reference = event.unit.external_reference
127 log_event.details.region = event.unit.region
128 log_to_ontomap(log_event)
129 end
131 local function area_updated(event, event_type)
132 local log_event = new_log_event(event, 0, event_type == "created" and "object_created" or "object_updated")
133 table.insert(log_event.activity_objects, new_activity_object{
134 type = "area",
135 url = url_for("area", event.area_id)
136 })
137 table.insert(log_event.references, new_reference_object(
138 url_for("unit", event.area.unit_id)
139 ))
140 log_event.details.active = event.area.active
141 log_event.details.name = event.area.name
142 log_event.details.description = event.area.description
143 log_event.details.external_reference = event.area.external_reference
144 log_event.details.region = event.area.region
145 log_to_ontomap(log_event)
146 end
148 local function policy_updated(event, event_type)
149 local log_event = new_log_event(event, 0, event_type == "created" and "object_created" or "object_updated")
150 table.insert(log_event.activity_objects, new_activity_object{
151 type = "policy",
152 url = url_for("policy", event.policy_id)
153 })
154 log_event.details.active = event.policy.active
155 log_event.details.name = event.policy.name
156 log_event.details.description = event.policy.description
157 log_to_ontomap(log_event)
158 end
160 local mapper = {
162 unit_created = function(event)
163 unit_updated(event, "created")
164 end,
166 unit_updated = function(event)
167 unit_updated(event, "updated")
168 end,
170 area_created = function(event)
171 area_updated(event, "created")
172 end,
174 area_updated = function(event)
175 area_updated(event, "updated")
176 end,
178 policy_created = function(event)
179 policy_updated(event, "created")
180 end,
182 policy_updated = function(event)
183 policy_updated(event, "updated")
184 end,
186 issue_state_changed = function(event)
187 local log_event = new_log_event(event, 0, "issue_status_updated")
188 table.insert(log_event.references, new_reference_object(
189 url_for("issue", event.issue_id)
190 ))
191 log_event.details.new_issue_state = event.state
192 log_to_ontomap(log_event)
193 end,
195 initiative_created_in_new_issue = function(event)
196 local log_events = json.array()
198 local log_event = new_log_event(event, 0, "object_created")
199 table.insert(log_event.activity_objects, new_activity_object{
200 type = "issue",
201 url = url_for("issue", event.issue_id)
202 })
203 table.insert(log_event.references, new_reference_object(
204 url_for("policy", event.issue.policy_id)
205 ))
206 log_event.details.new_issue_state = event.state
207 table.insert(log_events, log_event)
209 local log_event = new_log_event(event, event.member_id, "object_created")
211 local location = event.initiative.location
212 if location and location.marker_link then
213 local marker_link = location.marker_link
214 location.marker_link = nil
215 table.insert(log_event.references, new_reference_object(
216 marker_link, config.firstlife.application_ident, "BELONGS_TO"
217 ))
218 end
220 local activity_object = new_activity_object{
221 type = "initiative",
222 url = url_for("initiative", event.initiative_id),
223 geometry = location
224 }
225 activity_object.properties.name = event.initiative.name
226 table.insert(log_event.activity_objects, activity_object)
227 table.insert(log_event.references, new_reference_object(
228 url_for("issue", event.issue_id)
229 ))
230 table.insert(log_events, log_event)
232 log_to_ontomap(log_events)
233 end,
235 initiative_created_in_existing_issue = function(event)
236 local log_event = new_log_event(event, event.member_id, "object_created")
237 local location = event.initiative.location
238 if location and location.marker_link then
239 local marker_link = location.marker_link
240 location.marker_link = nil
241 table.insert(log_event.references, new_reference_object(
242 marker_link, config.firstlife.application_ident, "BELONGS_TO"
243 ))
244 end
245 local activity_object = new_activity_object{
246 type = "initiative",
247 url = url_for("initiative", event.initiative_id),
248 geometry = location
249 }
250 activity_object.properties.name = event.initiative.name
251 table.insert(log_event.activity_objects, activity_object)
252 table.insert(log_event.references, new_reference_object(
253 url_for("issue", event.issue_id)
254 ))
255 log_to_ontomap(log_event)
256 end,
258 initiative_revoked = function(event)
259 -- TODO -> which activity?
260 end,
262 new_draft_created = function(event)
263 local log_event = new_log_event(event, event.member_id, "object_updated")
264 table.insert(log_event.activity_objects, new_activity_object{
265 type = "initiative",
266 url = url_for("initiative", event.issue_id)
267 })
268 table.insert(log_event.references, new_reference_object(
269 url_for("issue", event.issue_id)
270 ))
271 log_event.details.name = event.initiative.name
272 log_event.details.location = event.initiative.current_draft.location
273 log_to_ontomap(log_event)
274 end,
276 interest = function(event)
277 local activity_type = event.boolean_value and "interest_added" or "interest_removed"
278 local log_event = new_log_event(event, event.member_id, activity_type)
279 table.insert(log_event.references, new_reference_object(
280 url_for("issue", event.issue_id)
281 ))
282 log_to_ontomap(log_event)
283 end,
285 initiator = function(event)
286 local activity_type = event.boolean_value and "initiator_added" or "initiator_removed"
287 local log_event = new_log_event(event, event.member_id, activity_type)
288 table.insert(log_event.references, new_reference_object(
289 url_for("initiative", event.initiative_id)
290 ))
291 log_to_ontomap(log_event)
292 end,
294 support = function(event)
295 local activity_type = event.boolean_value and "support_added" or "support_removed"
296 local log_event = new_log_event(event, event.member_id, activity_type)
297 table.insert(log_event.references, new_reference_object(
298 url_for("initiative", event.initiative_id)
299 ))
300 log_event.details.draft_id = event.draft_id
301 log_to_ontomap(log_event)
302 end,
304 support_updated = function(event)
305 local log_event = new_log_event(event, event.member_id, "support_updated")
306 table.insert(log_event.references, new_reference_object(
307 url_for("initiative", event.initiative_id)
308 ))
309 log_event.details.draft_id = event.draft_id
310 log_to_ontomap(log_event)
311 end,
313 suggestion_created = function(event)
314 local log_event = new_log_event(event, event.member_id, "object_created")
315 table.insert(log_event.activity_objects, new_activity_object{
316 type = "suggestion",
317 url = url_for("suggestion", event.suggestion_id)
318 })
319 table.insert(log_event.references, new_reference_object(
320 url_for("initiative", event.initiative_id)
321 ))
322 log_to_ontomap(log_event)
323 end,
325 suggestion_removed = function(event)
326 local log_event = new_log_event(event, 0, "object_removed")
327 table.insert(log_event.activity_objects, new_activity_object{
328 type = "suggestion",
329 url = url_for("suggestion", event.suggestion_id)
330 })
331 table.insert(log_event.references, new_reference_object(
332 url_for("initiative", event.initiative_id)
333 ))
334 log_to_ontomap(log_event)
335 end,
337 suggestion_rated = function(event)
338 local log_event = new_log_event(event, event.member_id, "suggestion_rated")
339 table.insert(log_event.references, new_reference_object(
340 url_for("suggestion", event.suggestion_id)
341 ))
342 log_event.details.degree = event.numeric_value
343 log_event.details.fulfilled = event.boolean_value or json.null
344 log_to_ontomap(log_event)
345 end,
347 delegation = function(event)
348 -- TODO
349 end,
351 member_activated = function(event)
352 local log_event = new_log_event(event, event.member_id, "account_registered")
353 log_to_ontomap(log_event)
354 end,
356 member_removed = function(event)
357 -- TODO -> which activity to log?
358 end,
360 member_active = function(event)
361 -- TODO -> which activity to log?
362 end,
364 member_name_updated = function(event)
365 local log_event = new_log_event(event, event.member_id, "screen_name_changed")
366 log_event.details.screen_name = event.text_value
367 log_to_ontomap(log_event)
368 end,
370 member_profile_updated = function(event)
371 local log_event = new_log_event(event, event.member_id, "profile_updated")
372 log_to_ontomap(log_event)
373 end,
375 member_image_updated = function(event)
376 local log_event = new_log_event(event, event.member_id, "avatar_changed")
377 log_to_ontomap(log_event)
378 end,
380 contact = function(event)
381 local activity_type = event.boolean_value and "contact_published" or "contact_unpublished"
382 local log_event = new_log_event(event, event.member_id, activity_type)
383 log_event.details.other_member_id = event.other_member_id
384 log_to_ontomap(log_event)
385 end
387 }
389 function _G.ontomap_log_event(event)
391 if mapper[event.event] then
392 local e = Event:new_selector()
393 :add_where{ "id = ?", event.id }
394 :add_field("extract(epoch from occurrence)", "occurrence_epoch")
395 :optional_object_mode()
396 :exec()
397 if e then
398 mapper[event.event](e)
399 end
400 end
403 end