liquid_feedback_frontend

view lib/ontomap/ontomap.lua @ 1859:02c34183b6df

Fixed wrong filename in INSTALL file
author bsw
date Tue Nov 28 18:54:51 2023 +0100 (6 months ago)
parents 0fce83763138
children
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 elseif event.initiative.external_reference then
219 table.insert(log_event.references, new_reference_object(
220 event.initiative.external_reference, config.firstlife.application_ident, "BELONGS_TO"
221 ))
222 end
224 local activity_object = new_activity_object{
225 type = "initiative",
226 url = url_for("initiative", event.initiative_id),
227 geometry = location
228 }
229 activity_object.properties.name = event.initiative.name
230 table.insert(log_event.activity_objects, activity_object)
231 table.insert(log_event.references, new_reference_object(
232 url_for("issue", event.issue_id)
233 ))
234 table.insert(log_events, log_event)
236 log_to_ontomap(log_events)
237 end,
239 initiative_created_in_existing_issue = function(event)
240 local log_event = new_log_event(event, event.member_id, "object_created")
241 local location = event.initiative.location
242 if location and location.marker_link then
243 local marker_link = location.marker_link
244 location.marker_link = nil
245 table.insert(log_event.references, new_reference_object(
246 marker_link, config.firstlife.application_ident, "BELONGS_TO"
247 ))
248 elseif event.initiative.external_reference then
249 table.insert(log_event.references, new_reference_object(
250 event.initiative.external_reference, config.firstlife.application_ident, "BELONGS_TO"
251 ))
252 end
253 local activity_object = new_activity_object{
254 type = "initiative",
255 url = url_for("initiative", event.initiative_id),
256 geometry = location
257 }
258 activity_object.properties.name = event.initiative.name
259 table.insert(log_event.activity_objects, activity_object)
260 table.insert(log_event.references, new_reference_object(
261 url_for("issue", event.issue_id)
262 ))
263 log_to_ontomap(log_event)
264 end,
266 initiative_revoked = function(event)
267 -- TODO -> which activity?
268 end,
270 new_draft_created = function(event)
271 local log_event = new_log_event(event, event.member_id, "object_updated")
272 table.insert(log_event.activity_objects, new_activity_object{
273 type = "initiative",
274 url = url_for("initiative", event.issue_id)
275 })
276 table.insert(log_event.references, new_reference_object(
277 url_for("issue", event.issue_id)
278 ))
279 log_event.details.name = event.initiative.name
280 log_event.details.location = event.initiative.current_draft.location
281 log_to_ontomap(log_event)
282 end,
284 interest = function(event)
285 local activity_type = event.boolean_value and "interest_added" or "interest_removed"
286 local log_event = new_log_event(event, event.member_id, activity_type)
287 table.insert(log_event.references, new_reference_object(
288 url_for("issue", event.issue_id)
289 ))
290 log_to_ontomap(log_event)
291 end,
293 initiator = function(event)
294 local activity_type = event.boolean_value and "initiator_added" or "initiator_removed"
295 local log_event = new_log_event(event, event.member_id, activity_type)
296 table.insert(log_event.references, new_reference_object(
297 url_for("initiative", event.initiative_id)
298 ))
299 log_to_ontomap(log_event)
300 end,
302 support = function(event)
303 local activity_type = event.boolean_value and "support_added" or "support_removed"
304 local log_event = new_log_event(event, event.member_id, activity_type)
305 table.insert(log_event.references, new_reference_object(
306 url_for("initiative", event.initiative_id)
307 ))
308 log_event.details.draft_id = event.draft_id
309 log_to_ontomap(log_event)
310 end,
312 support_updated = function(event)
313 local log_event = new_log_event(event, event.member_id, "support_updated")
314 table.insert(log_event.references, new_reference_object(
315 url_for("initiative", event.initiative_id)
316 ))
317 log_event.details.draft_id = event.draft_id
318 log_to_ontomap(log_event)
319 end,
321 suggestion_created = function(event)
322 local log_event = new_log_event(event, event.member_id, "object_created")
323 table.insert(log_event.activity_objects, new_activity_object{
324 type = "suggestion",
325 url = url_for("suggestion", event.suggestion_id)
326 })
327 table.insert(log_event.references, new_reference_object(
328 url_for("initiative", event.initiative_id)
329 ))
330 log_to_ontomap(log_event)
331 end,
333 suggestion_removed = function(event)
334 local log_event = new_log_event(event, 0, "object_removed")
335 table.insert(log_event.activity_objects, new_activity_object{
336 type = "suggestion",
337 url = url_for("suggestion", event.suggestion_id)
338 })
339 table.insert(log_event.references, new_reference_object(
340 url_for("initiative", event.initiative_id)
341 ))
342 log_to_ontomap(log_event)
343 end,
345 suggestion_rated = function(event)
346 local log_event = new_log_event(event, event.member_id, "suggestion_rated")
347 table.insert(log_event.references, new_reference_object(
348 url_for("suggestion", event.suggestion_id)
349 ))
350 log_event.details.degree = event.numeric_value
351 log_event.details.fulfilled = event.boolean_value or json.null
352 log_to_ontomap(log_event)
353 end,
355 delegation = function(event)
356 -- TODO
357 end,
359 member_activated = function(event)
360 local log_event = new_log_event(event, event.member_id, "account_registered")
361 log_to_ontomap(log_event)
362 end,
364 member_removed = function(event)
365 -- TODO -> which activity to log?
366 end,
368 member_active = function(event)
369 -- TODO -> which activity to log?
370 end,
372 member_name_updated = function(event)
373 local log_event = new_log_event(event, event.member_id, "screen_name_changed")
374 log_event.details.screen_name = event.text_value
375 log_to_ontomap(log_event)
376 end,
378 member_profile_updated = function(event)
379 local log_event = new_log_event(event, event.member_id, "profile_updated")
380 log_to_ontomap(log_event)
381 end,
383 member_image_updated = function(event)
384 local log_event = new_log_event(event, event.member_id, "avatar_changed")
385 log_to_ontomap(log_event)
386 end,
388 contact = function(event)
389 local activity_type = event.boolean_value and "contact_published" or "contact_unpublished"
390 local log_event = new_log_event(event, event.member_id, activity_type)
391 log_event.details.other_member_id = event.other_member_id
392 log_to_ontomap(log_event)
393 end
395 }
397 function _G.ontomap_log_event(event)
399 if mapper[event.event] then
400 local e = Event:new_selector()
401 :add_where{ "id = ?", event.id }
402 :add_field("extract(epoch from occurrence)", "occurrence_epoch")
403 :optional_object_mode()
404 :exec()
405 if e then
406 mapper[event.event](e)
407 end
408 end
411 end

Impressum / About Us