-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathusers.cc
More file actions
581 lines (467 loc) · 25.5 KB
/
users.cc
File metadata and controls
581 lines (467 loc) · 25.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#include "inja.hpp"
#include "thingpool.hh"
#include "sws.hh"
#include "scanmon.hh"
#include "pugixml.hpp"
#include "search.hh"
#include <fmt/chrono.h>
using namespace std;
static auto prepRSS(auto& doc, const std::string& title, const std::string& desc)
{
doc.append_attribute("standalone") = "yes";
doc.append_attribute("version") = "1.0";
doc.append_attribute("encoding") = "utf-8";
pugi::xml_node rss = doc.append_child("rss");
rss.append_attribute("version")="2.0";
rss.append_attribute("xmlns:atom")="http://www.w3.org/2005/Atom";
pugi::xml_node channel = rss.append_child("channel");
channel.append_child("title").append_child(pugi::node_pcdata).set_value(title.c_str());
channel.append_child("description").append_child(pugi::node_pcdata).set_value(desc.c_str());
channel.append_child("link").append_child(pugi::node_pcdata).set_value("https://berthub.eu/tkconv/");
channel.append_child("generator").append_child(pugi::node_pcdata).set_value("OpenTK");
return channel;
}
static int g_defScannerIntS = 300;
void addTkUserManagement(SimpleWebSystem& sws, const std::string& mailserver,
const std::string& fromaddr,
const std::string& baseUrl)
{
sws.wrapPost({}, "/create-user-invite", [mailserver, baseUrl, fromaddr](auto& cr) {
string email = cr.req.get_file_value("email").content;
nlohmann::json j;
if(email.empty()) {
j["ok"]=0;
j["message"] = "User field empty";
}
else {
try {
auto rows = cr.lsqw.query("select * from users where email=?", {email});
if(!rows.empty()) {
string user=get<string>(rows[0]["user"]);
cr.sessions.cleanExpired(); // needs to run somewhere
string session = cr.sessions.createSessionForUser(user, "Passwordless login session", cr.getIP(), true, time(0)+ 14 * 86400); // authenticated session
string dest=baseUrl + "/mijn.html?session="+session;
sendEmail(mailserver, fromaddr, email, "[OpenTK] log-in link",
"Log in door op deze link te klikken: "+dest+"\nDeze link werkt maar *14* dagen!",
fmt::format("Log in door op deze link <a href='{}'>{}</a> te klikken. Let op, deze link werkt maar *14* dagen!",
dest, dest));
cout<<"Sent email pointing user "<<email<<" at "<<dest<<endl;
cr.stats.sessionJoinInvite++;
cr.log({{"action", "invite-for-rejoin"}, {"email", email}, {"for", user}, {"session", session}});
j["ok"] = 1;
return j;
}
}
catch(std::exception& e) {
cerr<<"Could fail if there is no users table yet, is ok: "<<e.what()<<endl;
}
string id = getLargeId();
time_t tstamp = time(0);
cr.lsqw.addValue({{"id", id}, {"timestamp", tstamp}, {"email", email}}, "userInvite");
cr.lsqw.query("delete from userInvite where email=? and timestamp < ?", {email, tstamp});
sendEmail(mailserver, fromaddr, email, "[OpenTK] Koppel je email-adres",
fmt::format("Welkom!\nKlik nu op {}/mijn.html?id={} om je email-adres te koppelen aan OpenTK.", baseUrl, id),
fmt::format("Welkom!\nKlik nu op <a href='{}/mijn.html?id={}'>{}/mijn.html?id={}</a> om je email-adres te koppelen aan OpenTK.",
baseUrl, id,
baseUrl, id),
fromaddr
);
cr.log({{"action", "initial-invite"}, {"email", email}, {"session", id}});
j["ok"] = 1;
}
return j;
});
sws.wrapPost({}, "/confirm-user-invite", [](auto& cr) {
string id = cr.req.get_file_value("id").content;
nlohmann::json j;
if(id.empty()) {
j["ok"]=0;
j["message"] = "id field empty";
return j;
}
auto rows = cr.lsqw.query("select * from userInvite where id=?", {id});
if(rows.empty()) {
j["ok"]=0;
j["message"] = "id field empty";
return j;
}
string email = get<string>(rows[0]["email"]);
string user = getLargeId();
cr.users.createUser(user, "", email, false);
cr.lsqw.query("delete from userInvite where email=?", {email});
string ip=cr.getIP(), agent= cr.req.get_header_value("User-Agent");
string sessionid = cr.sessions.createSessionForUser(user, agent, ip);
cr.res.set_header("Set-Cookie",
"tkconv_session="+sessionid+"; SameSite=Strict; Path=/; HttpOnly; " + cr.sws.d_extraCookieSpec +" Max-Age="+to_string(5*365*86400));
cout<<"Logged in user '"<<user<<"' with email '"<<email<<"'"<<endl;
cr.log({{"action", "confirm-invite"}, {"for", user}, {"session", sessionid}});
j["ok"] = 1;
j["email"] = email;
return j;
});
//'set-oo-verantwoordelijken-monitors'
sws.wrapPost({Capability::IsUser}, "/remove-oo-verantwoordelijke-monitor", [](auto& cr) {
string verantwoordelijke=cr.req.get_file_value("verantwoordelijke").content;
cr.lsqw.query("delete from scanners where userid=? and soort=? and verantwoordelijke=?",
{cr.user, "OODocumentVerantwoordelijke", verantwoordelijke});
auto mons = cr.lsqw.query("select verantwoordelijke from scanners where userid=? and soort=? order by verantwoordelijke",
{cr.user, "OODocumentVerantwoordelijke"});
nlohmann::json oomonitors = nlohmann::json::array();
for(const auto& m : mons)
oomonitors.push_back(eget(m, "verantwoordelijke"));
return nlohmann::json{{"ok", 1}, {"oomonitors", oomonitors}};
});
sws.wrapPost({Capability::IsUser}, "/add-oo-verantwoordelijke-monitor", [](auto& cr) {
string verantwoordelijke=cr.req.get_file_value("verantwoordelijke").content;
string id = getLargeId();
cr.lsqw.addValue({{"id", id}, {"userid", cr.user}, {"soort", "OODocumentVerantwoordelijke"}, {"verantwoordelijke", verantwoordelijke}, {"cutoff", getTodayDBFormat()},
{"lastRun", 0}, {"interval", g_defScannerIntS}}, "scanners");
auto mons = cr.lsqw.query("select verantwoordelijke from scanners where userid=? and soort=? order by verantwoordelijke",
{cr.user, "OODocumentVerantwoordelijke"});
nlohmann::json oomonitors = nlohmann::json::array();
for(const auto& m : mons)
oomonitors.push_back(eget(m, "verantwoordelijke"));
return nlohmann::json{{"ok", 1}, {"oomonitors", oomonitors}};
});
sws.wrapGet({Capability::IsUser}, "/get-oo-verantwoordelijke-monitors", [](auto& cr) {
nlohmann::json oomonitors = nlohmann::json::array();
try {
auto mons = cr.lsqw.query("select verantwoordelijke from scanners where userid=? and soort=? order by verantwoordelijke",
{cr.user, "OODocumentVerantwoordelijke"});
for(const auto& m : mons)
oomonitors.push_back(eget(m, "verantwoordelijke"));
}
catch(std::exception& e) {
cout<<"Retrieving OO scanners failed, possibly because no oomonitors have ever been created: "<<e.what()<<endl;
}
return nlohmann::json{{"ok", 1}, {"oomonitors", oomonitors}};
});
sws.wrapPost({Capability::IsUser}, "/add-person-monitor", [](auto& cr) {
string nummer = cr.req.get_file_value("nummer").content;
string id = getLargeId();
cr.lsqw.addValue({{"id", id}, {"userid", cr.user}, {"soort", "persoon"}, {"nummer", nummer}, {"cutoff", getTodayDBFormat()},
{"lastRun", 0}, {"interval", g_defScannerIntS}}, "scanners");
return nlohmann::json{{"ok", 1}, {"id", id}};
});
sws.wrapPost({Capability::IsUser}, "/add-commissie-monitor", [](auto& cr) {
string nummer = cr.req.get_file_value("nummer").content;
string id = getLargeId();
cr.lsqw.addValue({{"id", id}, {"userid", cr.user}, {"soort", "commissie"}, {"commissieId", nummer}, {"cutoff", getTodayDBFormat()},
{"lastRun", 0}, {"interval", g_defScannerIntS}}, "scanners");
return nlohmann::json{{"ok", 1}, {"id", id}};
});
sws.wrapPost({Capability::IsUser}, "/add-activiteit-monitor", [](auto& cr) {
string nummer = cr.req.get_file_value("nummer").content;
string id = getLargeId();
cr.lsqw.addValue({{"id", id}, {"userid", cr.user}, {"soort", "activiteit"}, {"nummer", nummer}, {"cutoff", getTodayDBFormat()},
{"lastRun", 0}, {"interval", g_defScannerIntS}}, "scanners");
return nlohmann::json{{"ok", 1}, {"id", id}};
});
sws.wrapPost({Capability::IsUser}, "/add-zaak-monitor", [](auto& cr) {
string nummer = cr.req.get_file_value("nummer").content;
string id = getLargeId();
cr.lsqw.addValue({{"id", id}, {"userid", cr.user}, {"soort", "zaak"}, {"nummer", nummer}, {"cutoff", getTodayDBFormat()},
{"lastRun", 0}, {"interval", g_defScannerIntS}}, "scanners");
return nlohmann::json{{"ok", 1}, {"id", id}};
});
sws.wrapPost({Capability::IsUser}, "/add-search-monitor", [](auto& cr) {
string query = cr.req.get_file_value("query").content;
query = convertToSQLiteFTS5(query); // this was unfortunate
string categorie = cr.req.get_file_value("categorie").content;
string id = getLargeId();
cr.lsqw.addValue({{"id", id}, {"userid", cr.user}, {"soort", "zoek"}, {"categorie", ""}, {"query", query}, {"cutoff", getTodayDBFormat()},
{"lastRun", 0}, {"interval", g_defScannerIntS}}, "scanners");
return nlohmann::json{{"ok", 1}, {"id", id}};
});
sws.wrapPost({Capability::IsUser}, "/add-ksd-monitor", [](auto& cr) {
string nummer = cr.req.get_file_value("nummer").content;
string toevoeging = cr.req.get_file_value("toevoeging").content;
string id = getLargeId();
cr.lsqw.addValue({{"id", id}, {"userid", cr.user}, {"soort", "ksd"}, {"nummer", nummer}, {"toevoeging", toevoeging}, {"cutoff", getTodayDBFormat()},
{"lastRun", 0}, {"interval", g_defScannerIntS}
}, "scanners");
return nlohmann::json{{"ok", 1}, {"id", id}};
});
sws.wrapPost({Capability::IsUser}, "/remove-monitor", [](auto& cr) {
string id = cr.req.get_file_value("id").content;
cr.lsqw.query("delete from scanners where id=? and userid=?", {id, cr.user});
nlohmann::json j;
j["ok"]=1;
return j;
});
sws.wrapPost({Capability::IsUser}, "/set-monitor-interval", [](auto& cr) {
string id = cr.req.get_file_value("id").content;
int interval = atoi(cr.req.get_file_value("interval").content.c_str());
cr.lsqw.query("update scanners set interval=? where id=? and userid=?", {interval, id, cr.user});
nlohmann::json j;
j["ok"]=1;
return j;
});
sws.wrapPost({Capability::IsUser}, "/set-my-monitors-interval", [](auto& cr) {
int interval = atoi(cr.req.get_file_value("interval").content.c_str());
cr.lsqw.query("update scanners set interval=? where userid=?", {interval, cr.user});
cr.log({{"action", "set-my-monitor-interval"}, {"for", cr.user}, {"to", interval}});
nlohmann::json j;
j["ok"]=1;
return j;
});
sws.wrapPost({Capability::IsUser}, "/add-toezeggingen-monitor", [](auto& cr) {
string fractie = cr.req.get_file_value("fractie").content;
string voortouw = cr.req.get_file_value("voortouw").content;
string id = getLargeId();
cr.lsqw.addValue({{"id", id}, {"userid", cr.user}, {"soort", "toezeggingen"}, {"fractie", fractie}, {"voortouwAfkorting", voortouw}, {"cutoff", getTodayDBFormat()},
{"lastRun", 0}, {"interval", g_defScannerIntS}
}, "scanners");
return nlohmann::json{{"ok", 1}, {"id", id}};
});
sws.wrapGet({Capability::IsUser}, "/have-monitor/:kind/:nummer", [](auto& cr) {
string kind = cr.req.path_params.at("kind");
string nummer = cr.req.path_params.at("nummer");
cout<<"have-monitor nummer: "<<nummer<<endl;
string selector;
std::vector<std::unordered_map<std::string,MiniSQLite::outvar_t>> rows;
if(kind == "persoon" || kind =="activiteit" || kind=="zaak")
selector = "nummer";
else if(kind == "commissie") {
selector = "commissieId";
}
else if(kind == "ksd") {
string toevoeging = cr.req.get_param_value("toevoeging");
rows = cr.lsqw.query("select id from scanners where soort=? and nummer =? and toevoeging =? and userid=?", {kind, nummer, toevoeging, cr.user});
}
else if(kind=="toezeggingen") {
if(nummer=="all")
rows = cr.lsqw.query("select id from scanners where soort=? and (fractie is null or fractie='') and (voortouwAfkorting is null or voortouwAfkorting ='') and userid=?", {kind, cr.user});
else if(nummer.find("fractie=") == 0)
rows = cr.lsqw.query("select id from scanners where soort=? and userid=? and fractie=?", {kind, cr.user, nummer.substr(8)});
else if(nummer.find("voortouw=") == 0)
rows = cr.lsqw.query("select id from scanners where soort=? and userid=? and voortouwAfkorting=?", {kind, cr.user, nummer.substr(9)});
}
if(!selector.empty()) {
rows=cr.lsqw.query("select id from scanners where soort=? and "+selector+" =? and userid=?", {kind, nummer, cr.user});
}
nlohmann::json j;
j["ok"]=1;
j["have"] = rows.size();
if(!rows.empty())
j["id"] = get<string>(rows[0]["id"]);
return j;
});
sws.wrapGet({Capability::IsUser}, "/my-monitors", [](auto& cr) {
auto rows=cr.lsqw.query("select *,(select count(1) from sentNotification where scannerId = scanners.id) as cnt from scanners where userid=? order by rowid desc", {cr.user});
struct ScannerCombo
{
unique_ptr<Scanner> ptr;
int64_t count;
};
vector<pair<string, ScannerCombo> >scanners;
std::lock_guard<std::mutex> l(cr.lsqw.sqwlock);
for(auto& ts: rows) {
if(auto iter = g_scanmakers.find(eget(ts,"soort")); iter != g_scanmakers.end()) {
ScannerCombo sc;
sc.ptr = iter->second(cr.lsqw.sqw, get<string>(ts["id"]));
sc.count = get<int64_t>(ts["cnt"]);
scanners.emplace_back(get<string>(ts["id"]), std::move(sc));
} // XXXX this is wrong we don't have an autolocking sqlwriter not to use it
}
nlohmann::json j;
j["ok"]=1;
nlohmann::json jmonitors=nlohmann::json::array();
for(auto& [id, sc] : scanners) {
auto& [ptr, count] = sc;
nlohmann::json jmon;
jmon["description"] = ptr->describe(cr.tp.getLease().get());
jmon["type"] = ptr->getType();
jmon["id"] = id;
jmon["cnt"] = count;
jmon["interval"] = ptr->d_interval;
jmonitors.push_back(jmon);
}
j["monitors"] = jmonitors;
return j;
});
/*
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Bert Hubert's writings</title>
<link>https://berthub.eu/articles/</link>
<description>Recent content on Bert Hubert's writings</description>
<generator>Hugo -- gohugo.io</generator>
<lastBuildDate>Fri, 13 Dec 2024 14:13:41 +0000</lastBuildDate><atom:link href="https://berthub.eu/articles/index.xml" rel="self" type="application/rss+xml" />
<item>
<title>Welkom bij OpenTK (deel 2): de monitors</title>
<link>https://berthub.eu/articles/posts/welkom-bij-opentk-deel-2/</link>
<pubDate>Fri, 13 Dec 2024 14:13:41 +0000</pubDate>
<guid>https://berthub.eu/articles/posts/welkom-bij-opentk-deel-2/</guid>
<description>Welkom!
Goed inzicht in ons parlement is belangrijk, soms omdat er dingen in het nieuws zijn. En soms juist omdat dingen (nog) niet in het nieuws zijn, maar er binnenkort wel besluiten over genomen gaan worden. De Tweede Kamer publiceert alles wat ze doen via een technische API, en dat is echt geweldig. Hierdoor kunnen we op Internet eigen viewers maken om deze data zo goed mogelijk zichtbaar te maken.</description>
</item>
*/
sws.wrapGet({}, "/index.xml", [](auto& cr) {
pugi::xml_document doc;
string dlim = fmt::format("{:%Y-%m-%d}", fmt::localtime(time(0) - 8*86400));
bool onlyRegeringsstukken=false;
auto rows = cr.tp.getLease()->queryT("select Document.datum datum, Document.nummer nummer, Document.onderwerp onderwerp, Document.titel titel, Document.soort soort, Document.bijgewerkt bijgewerkt, ZaakActor.naam naam, ZaakActor.afkorting afkorting from Document left join Link on link.van = document.id left join zaak on zaak.id = link.naar left join ZaakActor on ZaakActor.zaakId = zaak.id and relatie = 'Voortouwcommissie' where +bronDocument='' and Document.soort != 'Sprekerslijst' and datum > ? and (? or Document.soort in ('Brief regering', 'Antwoord schriftelijke vragen', 'Voorstel van wet', 'Memorie van toelichting', 'Antwoord schriftelijke vragen (nader)')) order by datum desc, bijgewerkt desc",
{dlim, !onlyRegeringsstukken});
time_t latest = time(0);
if(!rows.empty()) {
string maxbw;
for(const auto& r: rows) {
if(maxbw < eget(r, "bijgewerkt"))
maxbw = eget(r, "bijgewerkt");
}
latest = getTstamp(maxbw);
}
string date = fmt::format("{:%a, %d %b %Y %H:%M:%S %z}", fmt::localtime(latest));
pugi::xml_node channel = prepRSS(doc, "Hoofd OpenTK feed", "Meest recente kamerdocumenten");
channel.append_child("lastBuildDate").append_child(pugi::node_pcdata).set_value(date.c_str());
for(const auto& r : rows) {
pugi::xml_node item = channel.append_child("item");
string onderwerp = eget(r, "onderwerp");
item.append_child("title").append_child(pugi::node_pcdata).set_value(onderwerp.c_str());
onderwerp = eget(r, "naam")+" | " + eget(r, "titel") + " " + onderwerp;
item.append_child("description").append_child(pugi::node_pcdata).set_value(onderwerp.c_str());
item.append_child("link").append_child(pugi::node_pcdata).set_value(
fmt::format("https://berthub.eu/tkconv/document.html?nummer={}", eget(r,"nummer")).c_str());
item.append_child("guid").append_child(pugi::node_pcdata).set_value(("tkconv_"+eget(r, "nummer")).c_str());
// 2024-12-06T06:01:10.2530000
string pubDate = eget(r, "bijgewerkt");
time_t then = getTstamp(pubDate);
// <pubDate>Fri, 13 Dec 2024 14:13:41 +0000</pubDate>
date = fmt::format("{:%a, %d %b %Y %H:%M:%S %z}", fmt::localtime(then));
item.append_child("pubDate").append_child(pugi::node_pcdata).set_value(date.c_str());
}
ostringstream str;
doc.save(str);
return make_pair<string,string>(str.str(), "application/xml");
});
// https://berthub.eu/tkconv/search.html?q=bert+hubert&twomonths=false&soorten=alles
sws.wrapGet({}, "/search/index.xml", [](auto& cr) {
string q = convertToSQLiteFTS5(cr.req.get_param_value("q"));
string categorie;
SQLiteWriter own("tkindex-small.sqlite3", SQLWFlag::ReadOnly);
own.query("ATTACH database 'tk.sqlite3' as meta");
SearchHelper sh(own);
// for now we can't do the rest, only Document XXX
auto matches = sh.search(q, {"Document"});
cout<<"Have "<<matches.size()<<" matches\n";
pugi::xml_document doc;
pugi::xml_node channel = prepRSS(doc, "Zoek RSS naar " +q, "Documenten gematched door zoekstring "+q);
bool first = true;
for(auto& m : matches) {
auto docs = own.queryT("select Document.onderwerp, Document.titel titel, Document.nummer nummer, Document.bijgewerkt bijgewerkt, ZaakActor.naam naam, ZaakActor.afkorting afkorting from Document left join Link on link.van = document.id left join zaak on zaak.id = link.naar left join ZaakActor on ZaakActor.zaakId = zaak.id and relatie = 'Voortouwcommissie' where Document.nummer=?", {m.nummer});
if(docs.empty()) {
cout<<"No hits for "<< m.nummer<<endl;
continue;
}
auto& r = docs[0];
pugi::xml_node item = channel.append_child("item");
string onderwerp = eget(r, "onderwerp");
item.append_child("title").append_child(pugi::node_pcdata).set_value(onderwerp.c_str());
onderwerp = eget(r, "naam")+" | " + eget(r, "titel") + " " + onderwerp;
item.append_child("description").append_child(pugi::node_pcdata).set_value(onderwerp.c_str());
item.append_child("link").append_child(pugi::node_pcdata).set_value(
fmt::format("https://berthub.eu/tkconv/document.html?nummer={}", eget(r,"nummer")).c_str());
item.append_child("guid").append_child(pugi::node_pcdata).set_value(("tkconv_"+eget(r, "nummer")).c_str());
// 2024-12-06T06:01:10.2530000
string pubDate = eget(r, "bijgewerkt");
time_t then = getTstamp(pubDate);
// <pubDate>Fri, 13 Dec 2024 14:13:41 +0000</pubDate>
string date = fmt::format("{:%a, %d %b %Y %H:%M:%S %z}", fmt::localtime(then));
item.append_child("pubDate").append_child(pugi::node_pcdata).set_value(date.c_str());
if(first) {
channel.prepend_child("lastBuildDate").append_child(pugi::node_pcdata).set_value(date.c_str());
first=false;
}
}
if(first) {
string date = fmt::format("{:%a, %d %b %Y %H:%M:%S %z}", fmt::localtime(time(0)));
channel.append_child("pubDate").append_child(pugi::node_pcdata).set_value(date.c_str());
}
ostringstream str;
doc.save(str);
return make_pair<string,string>(str.str(), "application/xml");
});
sws.wrapGet({}, "/:timsi/index.xml", [](auto& cr) {
string timsi = cr.req.path_params.at("timsi");
cout<<"Called for timsi "<< timsi <<endl;
auto docids = cr.lsqw.query("select identifier,timestamp from sentNotification,users where sentNotification.userid = users.user and users.timsi=? order by timestamp desc", {timsi});
cout<<"Got "<<docids.size()<<" docids\n";
pugi::xml_document doc;
pugi::xml_node channel = prepRSS(doc, "Monitor RSS", "Documenten gematched door jouw monitors");
bool first = true;
for(const auto& di : docids) {
pugi::xml_node item = channel.append_child("item");
auto docs = cr.tp.getLease()->queryT("select Document.onderwerp, Document.titel titel, Document.nummer nummer, Document.bijgewerkt bijgewerkt, ZaakActor.naam naam, ZaakActor.afkorting afkorting from Document left join Link on link.van = document.id left join zaak on zaak.id = link.naar left join ZaakActor on ZaakActor.zaakId = zaak.id and relatie = 'Voortouwcommissie' where Document.nummer=?", {eget(di, "identifier")});
if(docs.empty())
continue;
auto& r = docs[0];
string onderwerp = eget(r, "onderwerp");
item.append_child("title").append_child(pugi::node_pcdata).set_value(onderwerp.c_str());
onderwerp = eget(r, "naam")+" | " + eget(r, "titel") + " " + onderwerp;
item.append_child("description").append_child(pugi::node_pcdata).set_value(onderwerp.c_str());
item.append_child("link").append_child(pugi::node_pcdata).set_value(
fmt::format("https://berthub.eu/tkconv/document.html?nummer={}", eget(r,"nummer")).c_str());
item.append_child("guid").append_child(pugi::node_pcdata).set_value(("tkconv_"+eget(r, "nummer")).c_str());
// 2024-12-06T06:01:10.2530000
string pubDate = eget(r, "bijgewerkt");
time_t then = getTstamp(pubDate);
// <pubDate>Fri, 13 Dec 2024 14:13:41 +0000</pubDate>
string date = fmt::format("{:%a, %d %b %Y %H:%M:%S %z}", fmt::localtime(then));
item.append_child("pubDate").append_child(pugi::node_pcdata).set_value(date.c_str());
if(first) {
channel.prepend_child("lastBuildDate").append_child(pugi::node_pcdata).set_value(date.c_str());
first=false;
}
}
if(first) {
string date = fmt::format("{:%a, %d %b %Y %H:%M:%S %z}", fmt::localtime(time(0)));
channel.append_child("pubDate").append_child(pugi::node_pcdata).set_value(date.c_str());
}
ostringstream str;
doc.save(str);
return make_pair<string,string>(str.str(), "application/xml");
});
sws.wrapGet({}, "/commissie/:commissieid/index.xml", [](auto& cr) {
string commissieid = cr.req.path_params.at("commissieid");
cout<<"Called for commissieid "<<commissieid<<endl;
string dlim = fmt::format("{:%Y-%m-%d}", fmt::localtime(time(0) - 100*86400));
auto docs = cr.tp.getLease()->queryT("select Document.*, ZaakActor.relatie from Zaak,ZaakActor,Document,Link where ZaakActor.zaakId = zaak.id and zaakactor.commissieId=? and Document.id = link.van and Zaak.id = link.naar and ZaakActor.relatie='Voortouwcommissie' and Document.datum > ? order by Document.datum desc", {commissieid, dlim});
cout<<"Got "<<docs.size()<<" docids\n";
auto comm = cr.tp.getLease()->queryT("select naam from commissie where id=?", {commissieid});
if(comm.empty())
throw runtime_error("Commissie " + commissieid + " bestaat niet");
string naam = eget(comm[0], "naam");
pugi::xml_document doc;
pugi::xml_node channel = prepRSS(doc, "OpenTK: "+ naam + " RSS", "Documenten voor " + naam);
bool first = true;
for(const auto& r : docs) {
pugi::xml_node item = channel.append_child("item");
string onderwerp = eget(r, "onderwerp");
item.append_child("title").append_child(pugi::node_pcdata).set_value(onderwerp.c_str());
onderwerp = eget(r, "naam")+" | " + eget(r, "titel") + " " + onderwerp;
item.append_child("description").append_child(pugi::node_pcdata).set_value(onderwerp.c_str());
item.append_child("link").append_child(pugi::node_pcdata).set_value(
fmt::format("https://berthub.eu/tkconv/document.html?nummer={}", eget(r,"nummer")).c_str());
item.append_child("guid").append_child(pugi::node_pcdata).set_value(("tkconv_"+eget(r, "nummer")).c_str());
// 2024-12-06T06:01:10.2530000
string pubDate = eget(r, "bijgewerkt");
time_t then = getTstamp(pubDate);
// <pubDate>Fri, 13 Dec 2024 14:13:41 +0000</pubDate>
string date = fmt::format("{:%a, %d %b %Y %H:%M:%S %z}", fmt::localtime(then));
item.append_child("pubDate").append_child(pugi::node_pcdata).set_value(date.c_str());
if(first) {
channel.prepend_child("lastBuildDate").append_child(pugi::node_pcdata).set_value(date.c_str());
first=false;
}
}
if(first) {
string date = fmt::format("{:%a, %d %b %Y %H:%M:%S %z}", fmt::localtime(time(0)));
channel.append_child("pubDate").append_child(pugi::node_pcdata).set_value(date.c_str());
}
ostringstream str;
doc.save(str);
return make_pair<string,string>(str.str(), "application/xml");
});
}