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
...
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
...
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
...
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
...
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
...
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
...
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
...
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
...
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
...
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
...
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
...
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
....
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
....
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
....
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
....
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
....
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
|
delete from parsav_config where
key = $1::text
]];
};
actor_fetch_uid = {
params = {uint64}, sql = [[
select a.id, a.nym, a.handle, a.origin, a.bio,
a.avataruri, a.rank, a.quota, a.key, a.epithet,
extract(epoch from a.knownsince)::bigint,
coalesce(a.handle || '@' || s.domain,
'@' || a.handle) as xid,
a.invites
from parsav_actors as a
left join parsav_servers as s
on a.origin = s.id
where a.id = $1::bigint
]];
};
actor_fetch_xid = {
params = {pstring}, sql = [[
select a.id, a.nym, a.handle, a.origin, a.bio,
a.avataruri, a.rank, a.quota, a.key, a.epithet,
extract(epoch from a.knownsince)::bigint,
coalesce(a.handle || '@' || s.domain,
'@' || a.handle) as xid,
a.invites,
coalesce(s.domain,
(select value from parsav_config
where key='domain' limit 1)) as domain
from parsav_actors as a
left join parsav_servers as s
on a.origin = s.id
where $1::text = (a.handle || '@' || domain) or
$1::text = ('@' || a.handle || '@' || domain) or
(a.origin is null and
$1::text = a.handle or
$1::text = ('@' || a.handle))
]];
};
actor_purge_uid = {
params = {uint64}, cmd = true, sql = [[
with d as ( -- cheating
delete from parsav_sanctions where victim = $1::bigint
................................................................................
};
actor_create = {
params = {
rawstring, rawstring, uint64, lib.store.timepoint,
rawstring, rawstring, lib.mem.ptr(uint8),
rawstring, uint16, uint32, uint32
};
sql = [[
insert into parsav_actors (
nym,handle,
origin,knownsince,
bio,avataruri,key,
epithet,rank,quota,
invites
) values ($1::text, $2::text,
case when $3::bigint = 0 then null
else $3::bigint end,
to_timestamp($4::bigint),
$5::bigint, $6::bigint, $7::bytea,
$8::text, $9::smallint, $10::integer,
$11::integer
) returning id
]];
};
actor_auth_pw = {
params = {pstring,rawstring,pstring,lib.store.inet}, sql = [[
select a.aid, a.uid, a.name from parsav_auth as a
................................................................................
]];
};
actor_enum_local = {
params = {}, sql = [[
select id, nym, handle, origin, bio,
null::text, rank, quota, key, epithet,
extract(epoch from knownsince)::bigint,
'@' || handle,
invites
from parsav_actors where origin is null
order by nullif(rank,0) nulls last, handle
]];
};
actor_enum = {
params = {}, sql = [[
select a.id, a.nym, a.handle, a.origin, a.bio,
a.avataruri, a.rank, a.quota, a.key, a.epithet,
extract(epoch from a.knownsince)::bigint,
coalesce(a.handle || '@' || s.domain,
'@' || a.handle) as xid,
invites
from parsav_actors a
left join parsav_servers s on s.id = a.origin
order by nullif(a.rank,0) nulls last, a.handle, a.origin
]];
};
actor_stats = {
params = {uint64}, sql = ([[
with tweets as (
select from parsav_posts where author = $1::bigint
),
follows as (
select relatee as user from parsav_rels
where relator = $1::bigint and kind = <follow>
),
followers as (
select relator as user from parsav_rels
where relatee = $1::bigint and kind = <follow>
),
mutuals as (
select * from follows intersect select * from followers
)
values (
(select count(tweets.*)::bigint from tweets),
(select count(follows.*)::bigint from follows),
(select count(followers.*)::bigint from followers),
(select count(mutuals.*)::bigint from mutuals)
)
]]):gsub('<(%w+)>',function(r) return tostring(lib.store.relation.idvmap[r]) end)
};
actor_auth_how = {
params = {rawstring, lib.store.inet}, sql = [[
with mts as (select a.kind from parsav_auth as a
left join parsav_actors as u on u.id = a.uid
where (a.uid is null or u.handle = $1::text or (
................................................................................
(select count(*) from mts where kind like 'challenge-%') > 0,
(select count(*) from mts where kind = 'trust') > 0
]]; -- cheat
};
actor_session_fetch = {
params = {uint64, lib.store.inet, int64}, sql = [[
select a.id, a.nym, a.handle, a.origin, a.bio,
a.avataruri, a.rank, a.quota, a.key, a.epithet,
extract(epoch from a.knownsince)::bigint,
coalesce(a.handle || '@' || s.domain,
'@' || a.handle) as xid,
au.restrict,
array['post' ] <@ au.restrict,
array['edit' ] <@ au.restrict,
array['account' ] <@ au.restrict,
array['upload' ] <@ au.restrict,
array['moderate'] <@ au.restrict,
array['admin' ] <@ au.restrict
from parsav_auth au
left join parsav_actors a on au.uid = a.id
left join parsav_servers s on a.origin = s.id
where au.aid = $1::bigint and au.blacklist = false and
(au.netmask is null or au.netmask >> $2::inet) and
($3::bigint = 0 or --slightly abusing the epoch time fmt here, but
((a.authtime is null or a.authtime <= to_timestamp($3::bigint)) and
(au.valperiod is null or au.valperiod <= to_timestamp($3::bigint))))
]];
};
actor_powers_fetch = {
params = {uint64}, sql = [[
select key, allow from parsav_rights where actor = $1::bigint
]]
................................................................................
actor_power_delete = {
params = {uint64,lib.mem.ptr(int8)}, cmd = true, sql = [[
delete from parsav_rights where
actor = $1::bigint and
key = $2::text
]]
};
auth_sigtime_user_fetch = {
params = {uint64}, sql = [[
select extract(epoch from authtime)::bigint
from parsav_actors where id = $1::bigint
]];
};
auth_sigtime_user_alter = {
params = {uint64,int64}, cmd = true, sql = [[
update parsav_actors set
authtime = to_timestamp($2::bigint)
where id = $1::bigint
]];
};
auth_create_pw = {
params = {uint64, binblob, pstring}, cmd = true, sql = [[
insert into parsav_auth (uid, name, kind, cred, comment) values (
$1::bigint,
(select handle from parsav_actors where id = $1::bigint),
'pw-sha256', $2::bytea,
$3::text
)
]]
};
auth_purge_type = {
params = {rawstring, uint64, rawstring}, cmd = true, sql = [[
delete from parsav_auth where
................................................................................
rawstring, rawstring, rawstring;
}, cmd = true, sql = [[
update parsav_posts set
subject = $4::text,
acl = $5::text,
body = $6::text,
chgcount = $2::integer,
edited = to_timestamp($3::bigint)
where id = $1::bigint
]]
};
post_create = {
params = {
uint64, rawstring, rawstring, rawstring,
................................................................................
insert into parsav_posts (
author, subject, acl, body,
parent, posted, discovered,
circles, mentions, convoheaduri
) values (
$1::bigint, case when $2::text = '' then null else $2::text end,
$3::text, $4::text,
$5::bigint, to_timestamp($6::bigint), now(),
array[]::bigint[], array[]::bigint[], $7::text
) returning id
]]; -- TODO array handling
};
post_destroy_prepare = {
params = {uint64}, cmd = true, sql = [[
................................................................................
params = {uint64}, cmd = true, sql = [[
delete from parsav_posts where id = $1::bigint
]]
};
post_fetch = {
params = {uint64}, sql = [[
with counts as (
select a.kind, p.id as subject, count(*) as ct from parsav_acts as a
inner join parsav_posts as p on p.id = a.subject
group by a.kind, p.id
)
select a.origin is null,
p.id, p.author, p.subject, p.acl, p.body,
extract(epoch from p.posted )::bigint,
extract(epoch from p.discovered)::bigint,
extract(epoch from p.edited )::bigint,
p.parent, p.convoheaduri, p.chgcount,
coalesce(c.value, -1)::smallint, 0::bigint, 0::bigint,
coalesce((select ct from counts where kind = 'like' and counts.subject = p.id),0)::integer,
coalesce((select ct from counts where kind = 'rt' and counts.subject = p.id),0)::integer
from parsav_posts as p
inner join parsav_actors as a on p.author = a.id
left join parsav_actor_conf_ints as c on c.uid = a.id and c.key = 'ui-accent'
where p.id = $1::bigint
]];
};
post_enum_parent = {
params = {uint64}, sql = [[
with counts as (
select a.kind, p.id as subject, count(*) as ct from parsav_acts as a
inner join parsav_posts as p on p.id = a.subject
group by a.kind, p.id
)
select a.origin is null,
p.id, p.author, p.subject, p.acl, p.body,
extract(epoch from p.posted )::bigint,
extract(epoch from p.discovered)::bigint,
extract(epoch from p.edited )::bigint,
p.parent, p.convoheaduri, p.chgcount,
coalesce(c.value, -1)::smallint, 0::bigint, 0::bigint,
coalesce((select ct from counts where kind = 'like' and counts.subject = p.id),0)::integer,
coalesce((select ct from counts where kind = 'rt' and counts.subject = p.id),0)::integer
from parsav_posts as p
inner join parsav_actors as a on a.id = p.author
left join parsav_actor_conf_ints as c on c.uid = a.id and c.key = 'ui-accent'
where p.parent = $1::bigint
order by p.posted, p.discovered asc
]]
};
thread_latest_arrival_calc = {
params = {uint64}, sql = [[
with recursive posts(id) as (
select id from parsav_posts where parent = $1::bigint
union
................................................................................
maxes as (
select unnest(array[max(p.posted), max(p.discovered), max(p.edited)]) as m
from posts
inner join parsav_posts as p
on p.id = posts.id
)
select extract(epoch from max(m))::bigint from maxes
]];
};
post_react_simple = {
params = {uint64, uint64, pstring}, sql = [[
insert into parsav_acts (kind,actor,subject) values (
$3::text, $1::bigint, $2::bigint
) returning id
]];
};
post_react_cancel = {
params = {uint64, uint64, pstring}, cmd = true, sql = [[
delete from parsav_acts where
................................................................................
($2::bigint = 0 or subject = $2::bigint) and
($3::text is null or kind = $3::text )
]]
};
post_enum_author_uid = {
params = {uint64,uint64,uint64,uint64, uint64}, sql = [[
with ownposts as (
select *, 0::bigint as rtid from parsav_posts as p
where p.author = $5::bigint and
($1::bigint = 0 or p.posted <= to_timestamp($1::bigint)) and
($2::bigint = 0 or to_timestamp($2::bigint) < p.posted)
),
retweets as (
select p.*, a.id as rtid from parsav_acts as a
inner join parsav_posts as p on a.subject = p.id
where a.actor = $5::bigint and
a.kind = 'rt' and
($1::bigint = 0 or a.time <= to_timestamp($1::bigint)) and
($2::bigint = 0 or to_timestamp($2::bigint) < a.time)
),
allposts as (select *, 0::bigint as retweeter from ownposts
union select *, $5::bigint as retweeter from retweets),
counts as (
select a.kind, p.id as subject, count(*) as ct from parsav_acts as a
inner join parsav_posts as p on p.id = a.subject
group by a.kind, p.id
)
select a.origin is null,
p.id, p.author, p.subject, p.acl, p.body,
extract(epoch from p.posted )::bigint,
extract(epoch from p.discovered)::bigint,
extract(epoch from p.edited )::bigint,
p.parent, p.convoheaduri, p.chgcount,
coalesce(c.value,-1)::smallint,
p.retweeter, p.rtid,
coalesce((select ct from counts where kind = 'like' and counts.subject = p.id),0)::integer,
coalesce((select ct from counts where kind = 'rt' and counts.subject = p.id),0)::integer
from allposts as p
inner join parsav_actors as a on p.author = a.id
left join parsav_actor_conf_ints as c
on c.key = 'ui-accent' and
c.uid = a.id
order by (p.posted, p.discovered) desc
limit case when $3::bigint = 0 then null
else $3::bigint end
offset $4::bigint
]]
};
-- maybe there's some way to unify these two, idk, im tired
timeline_instance_fetch = {
params = {uint64, uint64, uint64, uint64}, sql = [[
with posts as (
select true,
p.id, p.author, p.subject, p.acl, p.body,
extract(epoch from p.posted )::bigint,
extract(epoch from p.discovered)::bigint,
extract(epoch from p.edited )::bigint,
p.parent, null::text, p.chgcount,
coalesce(c.value, -1)::smallint, 0::bigint, 0::bigint
from parsav_posts as p
inner join parsav_actors as a on p.author = a.id
left join parsav_actor_conf_ints as c on c.uid = a.id and c.key = 'ui-accent'
where
($1::bigint = 0 or p.posted <= to_timestamp($1::bigint)) and
($2::bigint = 0 or to_timestamp($2::bigint) < p.posted) and
(a.origin is null)
order by (p.posted, p.discovered) desc
limit case when $3::bigint = 0 then null
else $3::bigint end
offset $4::bigint
), counts as (
select a.kind, p.id as subject, count(*) as ct from parsav_acts as a
inner join parsav_posts as p on p.id = a.subject
group by a.kind, p.id
)
select *,
coalesce((select ct from counts as c where kind = 'like' and c.subject = posts.id),0)::integer,
coalesce((select ct from counts as c where kind = 'rt' and c.subject = posts.id),0)::integer
from posts
]]
};
artifact_instantiate = {
params = {binblob, binblob, pstring}, sql = [[
insert into parsav_artifacts (content,hash,mime) values (
$1::bytea, $2::bytea, $3::text
) on conflict do nothing returning id
................................................................................
buf[2] = tycode
buf[3] = sz
for j=0,sz do buf[4 + j] = i.v6[j] end -- 😬
return buf
end
end;
}
local con = symbol(&lib.pq.PGconn)
local prep = {}
local function sqlsquash(s) return s
:gsub('%%include (.-)%%',function(f)
return sqlsquash(lib.util.ingest('backend/schema/' .. f))
end) -- include dependencies
:gsub('%-%-.-\n','') -- remove disruptive line comments
:gsub('%-%-.-$','') -- remove unnecessary terminal comments
:gsub('%s+',' ') -- remove whitespace
:gsub('^%s*(.-)%s*$','%1') -- chomp
end
for k,q in pairs(queries) do
local qt = sqlsquash(q.sql)
local stmt = 'parsavpg_' .. k
terra q.prep([con])
var res = lib.pq.PQprepare([con], stmt, qt, [#q.params], nil)
defer lib.pq.PQclear(res)
................................................................................
lib.dbg(['searching for hashed password credentials in format SHA' .. tostring(hash)])
var [out]
[vdrs]
lib.dbg(['could not find password hash'])
end
end
local schema = sqlsquash(lib.util.ingest('backend/schema/pgsql.sql'))
local obliterator = sqlsquash(lib.util.ingest('backend/schema/pgsql-drop.sql'))
local privupdate = terra(
src: &lib.store.source,
ac: &lib.store.actor
): {}
var pdef: lib.store.powerset pdef:clear()
var map = array([privmap])
................................................................................
return con
end];
close = [terra(src: &lib.store.source) lib.pq.PQfinish([&lib.pq.PGconn](src.handle)) end];
tx_enter = txdo, tx_complete = txdone;
conprep = [terra(src: &lib.store.source, mode: lib.store.prepmode.t)
var [con] = [&lib.pq.PGconn](src.handle)
if mode == lib.store.prepmode.full then [prep]
elseif mode == lib.store.prepmode.conf or
mode == lib.store.prepmode.admin then
queries.conf_get.prep(con)
queries.conf_set.prep(con)
queries.conf_reset.prep(con)
if mode == lib.store.prepmode.admin then
end
else lib.bail('unsupported connection preparation mode') end
end];
dbsetup = [terra(src: &lib.store.source)
var res = lib.pq.PQexec([&lib.pq.PGconn](src.handle), schema)
if lib.pq.PQresultStatus(res) == lib.pq.PGRES_COMMAND_OK then
lib.report('successfully instantiated schema in database')
return true
else
lib.warn('backend pgsql - failed to initialize database: \n', lib.pq.PQresultErrorMessage(res))
return false
end
end];
obliterate_everything = [terra(src: &lib.store.source)
var res = lib.pq.PQexec([&lib.pq.PGconn](src.handle), obliterator)
if lib.pq.PQresultStatus(res) == lib.pq.PGRES_COMMAND_OK then
lib.report('successfully wiped out everything parsav-related in database')
return true
else
lib.warn('backend pgsql - failed to obliterate database: \n', lib.pq.PQresultErrorMessage(res))
return false
................................................................................
var a = row_to_actor(&r, 0)
a.ptr.source = src
var au = [lib.stat(lib.store.auth)] { ok = true }
au.val.aid = aid
au.val.uid = a.ptr.id
if not r:null(0,14) then -- restricted?
au.val.privs:clear()
(au.val.privs.post << r:bool(0,15))
(au.val.privs.edit << r:bool(0,16))
(au.val.privs.account << r:bool(0,17))
(au.val.privs.upload << r:bool(0,18))
(au.val.privs.moderate<< r:bool(0,19))
(au.val.privs.admin << r:bool(0,20))
else au.val.privs:fill() end
return au, a
end
::fail:: return [lib.stat (lib.store.auth) ] { ok = false },
[lib.mem.ptr(lib.store.actor)] { ptr = nil, ct = 0 }
................................................................................
post_retweet = [terra(
src: &lib.store.source,
uid: uint64,
post: uint64,
undo: bool
): {}
if not undo then
queries.post_react_simple.exec(src,uid,post,"rt")
else
queries.post_react_cancel.exec(src,uid,post,"rt")
end
end];
post_like = [terra(
src: &lib.store.source,
uid: uint64,
post: uint64,
undo: bool
): {}
if not undo then
queries.post_react_simple.exec(src,uid,post,"like")
else
queries.post_react_cancel.exec(src,uid,post,"like")
end
end];
post_liked_uid = [terra(
src: &lib.store.source,
uid: uint64,
................................................................................
-- check against default rights, insert records for wherever powers differ
lib.dbg('created new actor, establishing powers')
privupdate(src,ac)
lib.dbg('powers established')
return ac.id
end];
actor_purge_uid = [terra(
src: &lib.store.source,
uid: uint64
) queries.actor_purge_uid.exec(src,uid) end];
auth_enum_uid = [terra(
................................................................................
): {}
var hash: uint8[lib.crypt.algsz.sha256]
if lib.md.mbedtls_md(lib.md.mbedtls_md_info_from_type(lib.crypt.alg.sha256.id),
[&uint8](pw.ptr), pw.ct, &hash[0]) ~= 0 then
lib.bail('cannot hash password')
end
if reset then queries.auth_purge_type.exec(src, nil, uid, 'pw-%') end
queries.auth_create_pw.exec(src, uid, binblob {ptr = &hash[0], ct = [hash.type.N]}, comment)
end];
auth_purge_pw = [terra(src: &lib.store.source, uid: uint64, handle: rawstring): {}
queries.auth_purge_type.exec(src, handle, uid, 'pw-%')
end];
auth_purge_otp = [terra(src: &lib.store.source, uid: uint64, handle: rawstring): {}
|
|
<
<
<
<
<
<
|
<
<
|
|
|
<
<
<
<
|
<
<
<
<
<
<
<
<
|
<
|
>
<
|
|
|
|
|
|
<
<
<
<
<
|
<
>
|
|
|
|
|
<
<
<
<
<
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
<
>
|
|
|
|
|
|
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
>
>
|
<
>
|
|
|
|
|
|
|
<
<
<
<
<
<
<
|
<
<
>
|
<
>
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
|
<
<
<
<
<
<
>
<
<
<
<
<
>
>
|
<
<
>
>
|
|
|
<
<
<
<
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
<
<
<
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
>
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
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
..
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
...
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
...
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
...
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
...
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
...
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
...
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
...
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
...
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
...
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
...
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
....
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
....
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
....
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
....
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
....
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
|
delete from parsav_config where
key = $1::text
]];
};
actor_fetch_uid = {
params = {uint64}, sql = [[
select (pg_temp.parsavpg_translate_actor(a)).*
from parsav_actors as a
where a.id = $1::bigint
]];
};
actor_fetch_xid = {
params = {pstring}, sql = [[
with txd as (
select (pg_temp.parsavpg_translate_actor(a)).* from parsav_actors as a
)
select * from txd as a where $1::text = xid or
(a.origin is null and
$1::text = a.handle or
$1::text = (a.handle ||'@'||
(select value from parsav_config where key='domain')))
]];
};
actor_purge_uid = {
params = {uint64}, cmd = true, sql = [[
with d as ( -- cheating
delete from parsav_sanctions where victim = $1::bigint
................................................................................
};
actor_create = {
params = {
rawstring, rawstring, uint64, lib.store.timepoint,
rawstring, rawstring, lib.mem.ptr(uint8),
rawstring, uint16, uint32, uint32
}, sql = [[
insert into parsav_actors (
nym,handle,
origin,knownsince,
bio,avataruri,key,
epithet,rank,quota,
invites,authtime
) values ($1::text, $2::text,
case when $3::bigint = 0 then null
else $3::bigint end,
$4::bigint,
$5::bigint, $6::bigint, $7::bytea,
$8::text, $9::smallint, $10::integer,
$11::integer,$4::bigint
) returning id
]];
};
actor_auth_pw = {
params = {pstring,rawstring,pstring,lib.store.inet}, sql = [[
select a.aid, a.uid, a.name from parsav_auth as a
................................................................................
]];
};
actor_enum_local = {
params = {}, sql = [[
select id, nym, handle, origin, bio,
null::text, rank, quota, key, epithet,
knownsince::bigint,
'@' || handle,
invites
from parsav_actors where origin is null
order by nullif(rank,0) nulls last, handle
]];
};
actor_enum = {
params = {}, sql = [[
select (pg_temp.parsavpg_translate_actor(a)).*
from parsav_actors as a
order by nullif(a.rank,0) nulls last, a.handle, a.origin
]];
};
actor_stats = {
params = {uint64}, sql = [[
with tweets as (
select from parsav_posts where author = $1::bigint
),
follows as (
select relatee as user from parsav_rels
where relator = $1::bigint and kind = <rel:follow>
),
followers as (
select relator as user from parsav_rels
where relatee = $1::bigint and kind = <rel:follow>
),
mutuals as (
select * from follows intersect select * from followers
)
values (
(select count(tweets.*)::bigint from tweets),
(select count(follows.*)::bigint from follows),
(select count(followers.*)::bigint from followers),
(select count(mutuals.*)::bigint from mutuals)
)
]]
};
actor_auth_how = {
params = {rawstring, lib.store.inet}, sql = [[
with mts as (select a.kind from parsav_auth as a
left join parsav_actors as u on u.id = a.uid
where (a.uid is null or u.handle = $1::text or (
................................................................................
(select count(*) from mts where kind like 'challenge-%') > 0,
(select count(*) from mts where kind = 'trust') > 0
]]; -- cheat
};
actor_session_fetch = {
params = {uint64, lib.store.inet, int64}, sql = [[
select (pg_temp.parsavpg_translate_actor(a)).*,
au.restrict,
array['post' ] <@ au.restrict,
array['edit' ] <@ au.restrict,
array['account' ] <@ au.restrict,
array['upload' ] <@ au.restrict,
array['moderate'] <@ au.restrict,
array['admin' ] <@ au.restrict
from parsav_auth au
left join parsav_actors a on au.uid = a.id
where au.aid = $1::bigint and au.blacklist = false and
(au.netmask is null or au.netmask >> $2::inet) and
($3::bigint = 0 or --slightly abusing the epoch time fmt here, but
((a.authtime is null or a.authtime <= $3::bigint) and
(au.valperiod is null or au.valperiod <= $3::bigint)))
]];
};
actor_powers_fetch = {
params = {uint64}, sql = [[
select key, allow from parsav_rights where actor = $1::bigint
]]
................................................................................
actor_power_delete = {
params = {uint64,lib.mem.ptr(int8)}, cmd = true, sql = [[
delete from parsav_rights where
actor = $1::bigint and
key = $2::text
]]
};
actor_rel_create = {
params = {uint16,uint64, uint64}, cmd = true, sql = [[
insert into parsav_rels (kind,relator,relatee)
values($1::smallint, $2::bigint, $3::bigint)
on conflict do nothing
]];
};
actor_rel_destroy = {
params = {uint16,uint64, uint64}, cmd = true, sql = [[
delete from parsav_rels where
kind = $1::smallint and
relator = $2::bigint and
relatee = $3::bigint
]];
};
actor_rel_enum = {
params = {uint64, uint64}, sql = [[
select kind from parsav_rels where
relator = $1::bigint and
relatee = $2::bigint
]];
};
actor_notice_enum = {
params = {uint64}, sql = [[
select (notice).* from pg_temp.parsavpg_notices
where rcpt = $1::bigint
]];
};
auth_sigtime_user_fetch = {
params = {uint64}, sql = [[
select authtime::bigint
from parsav_actors where id = $1::bigint
]];
};
auth_sigtime_user_alter = {
params = {uint64,int64}, cmd = true, sql = [[
update parsav_actors set
authtime = $2::bigint
where id = $1::bigint
]];
};
auth_create_pw = {
params = {uint64, binblob, int64, pstring}, cmd = true, sql = [[
insert into parsav_auth (uid, name, kind, cred, valperiod, comment) values (
$1::bigint,
(select handle from parsav_actors where id = $1::bigint),
'pw-sha256', $2::bytea,
$3::bigint, $4::text
)
]]
};
auth_purge_type = {
params = {rawstring, uint64, rawstring}, cmd = true, sql = [[
delete from parsav_auth where
................................................................................
rawstring, rawstring, rawstring;
}, cmd = true, sql = [[
update parsav_posts set
subject = $4::text,
acl = $5::text,
body = $6::text,
chgcount = $2::integer,
edited = $3::bigint
where id = $1::bigint
]]
};
post_create = {
params = {
uint64, rawstring, rawstring, rawstring,
................................................................................
insert into parsav_posts (
author, subject, acl, body,
parent, posted, discovered,
circles, mentions, convoheaduri
) values (
$1::bigint, case when $2::text = '' then null else $2::text end,
$3::text, $4::text,
$5::bigint, $6::bigint, $6::bigint,
array[]::bigint[], array[]::bigint[], $7::text
) returning id
]]; -- TODO array handling
};
post_destroy_prepare = {
params = {uint64}, cmd = true, sql = [[
................................................................................
params = {uint64}, cmd = true, sql = [[
delete from parsav_posts where id = $1::bigint
]]
};
post_fetch = {
params = {uint64}, sql = [[
select (p.post).*
from pg_temp.parsavpg_known_content as p
where (p.post).id = $1::bigint and (p.post).rtdby = 0
]]
};
post_enum_parent = {
params = {uint64}, sql = [[
select (p.post).*
from pg_temp.parsavpg_known_content as p
where (p.post).parent = $1::bigint and (p.post).rtdby = 0
order by (p.post).posted, (p.post).discovered asc
]];
};
thread_latest_arrival_calc = {
params = {uint64}, sql = [[
with recursive posts(id) as (
select id from parsav_posts where parent = $1::bigint
union
................................................................................
maxes as (
select unnest(array[max(p.posted), max(p.discovered), max(p.edited)]) as m
from posts
inner join parsav_posts as p
on p.id = posts.id
)
select max(m)::bigint from maxes
]];
};
post_react_simple = {
params = {uint64, uint64, pstring, int64}, sql = [[
insert into parsav_acts (kind,actor,subject,time) values (
$3::text, $1::bigint, $2::bigint, $4::bigint
) returning id
]];
};
post_react_cancel = {
params = {uint64, uint64, pstring}, cmd = true, sql = [[
delete from parsav_acts where
................................................................................
($2::bigint = 0 or subject = $2::bigint) and
($3::text is null or kind = $3::text )
]]
};
post_enum_author_uid = {
params = {uint64,uint64,uint64,uint64, uint64}, sql = [[
select (c.post).*
from pg_temp.parsavpg_known_content as c
where c.promoter = $5::bigint and
($1::bigint = 0 or c.tltime <= $1::bigint) and
($2::bigint = 0 or $2::bigint < c.tltime)
order by c.tltime desc
limit case when $3::bigint = 0 then null
else $3::bigint end
offset $4::bigint
]];
};
-- maybe there's some way to unify these two, idk, im tired
timeline_instance_fetch = {
params = {uint64, uint64, uint64, uint64}, sql = [[
select (c.post).*
from pg_temp.parsavpg_known_content as c
where (c.post).localpost = true and
($1::bigint = 0 or c.tltime <= $1::bigint) and
($2::bigint = 0 or $2::bigint < c.tltime)
order by c.tltime desc
limit case when $3::bigint = 0 then null
else $3::bigint end
offset $4::bigint
]];
};
timeline_actor_fetch = {
params = {uint64, uint64, uint64, uint64, uint64}, sql = [[
with followed as (
select relatee from parsav_rels where
kind = <rel:follow> and
relator = $1::bigint
), avoided as (
select relatee as avoidee from parsav_rels where
kind = <rel:avoid> or kind = <rel:mute> and
relator = $1::bigint
union select relator as avoidee from parsav_rels where
kind = <rel:exclude> and
relatee = $1::bigint
)
select (c.post).*
from pg_temp.parsavpg_known_content as c
where ($2::bigint = 0 or c.tltime <= $2::bigint) and
($3::bigint = 0 or $3::bigint < c.tltime) and
(c.promoter in (table followed) or
c.promoter = $1::bigint) and
not ((c.post).author in (table avoided))
order by c.tltime desc
limit case when $4::bigint = 0 then null
else $4::bigint end
offset $5::bigint
]];
};
artifact_instantiate = {
params = {binblob, binblob, pstring}, sql = [[
insert into parsav_artifacts (content,hash,mime) values (
$1::bytea, $2::bytea, $3::text
) on conflict do nothing returning id
................................................................................
buf[2] = tycode
buf[3] = sz
for j=0,sz do buf[4 + j] = i.v6[j] end -- 😬
return buf
end
end;
}
local sqlvars = {}
for i, n in ipairs(lib.store.noticetype.members) do
sqlvars['notice:' .. n] = lib.store.noticetype[n]
end
for i, n in ipairs(lib.store.relation.members) do
sqlvars['rel:' .. n] = lib.store.relation.idvmap[n]
end
local con = symbol(&lib.pq.PGconn)
local function sqlsquash(s) return s
:gsub('%%include (.-)%%',function(f)
return sqlsquash(lib.util.ingest('backend/schema/' .. f))
end) -- include dependencies
:gsub('%-%-.-\n','') -- remove disruptive line comments
:gsub('%-%-.-$','') -- remove unnecessary terminal comments
:gsub('<(%g-)>',function(r) return tostring(sqlvars[r]) end)
:gsub('%s+',' ') -- remove whitespace
:gsub('^%s*(.-)%s*$','%1') -- chomp
end
-- to simplify queries and reduce development headaches in general, we
-- offload as much logic as possible into views. to avoid versioning
-- difficulties, these views are not part of the schema, but are rather
-- uploaded to the database at the start of a parsav connection, visible
-- only to the connecting parsav instance, stored in memory, and dropped
-- as soon as the connection session ends.
local tempviews = sqlsquash(lib.util.ingest 'backend/schema/pgsql-views.sql')
local prep = { quote
var res = lib.pq.PQexec([con], tempviews)
if lib.pq.PQresultStatus(res) == lib.pq.PGRES_COMMAND_OK then
lib.dbg('uploading pgsql session views')
else
lib.bail('backend pgsql - failed to upload session views: \n', lib.pq.PQresultErrorMessage(res))
end
end }
for k,q in pairs(queries) do
local qt = sqlsquash(q.sql)
local stmt = 'parsavpg_' .. k
terra q.prep([con])
var res = lib.pq.PQprepare([con], stmt, qt, [#q.params], nil)
defer lib.pq.PQclear(res)
................................................................................
lib.dbg(['searching for hashed password credentials in format SHA' .. tostring(hash)])
var [out]
[vdrs]
lib.dbg(['could not find password hash'])
end
end
local schema = sqlsquash(lib.util.ingest 'backend/schema/pgsql.sql')
local obliterator = sqlsquash(lib.util.ingest 'backend/schema/pgsql-drop.sql')
local privupdate = terra(
src: &lib.store.source,
ac: &lib.store.actor
): {}
var pdef: lib.store.powerset pdef:clear()
var map = array([privmap])
................................................................................
return con
end];
close = [terra(src: &lib.store.source) lib.pq.PQfinish([&lib.pq.PGconn](src.handle)) end];
tx_enter = txdo, tx_complete = txdone;
conprep = [terra(src: &lib.store.source, mode: lib.store.prepmode.t): {}
var [con] = [&lib.pq.PGconn](src.handle)
if mode == lib.store.prepmode.full then [prep]
elseif mode == lib.store.prepmode.conf or
mode == lib.store.prepmode.admin then
queries.conf_get.prep(con)
queries.conf_set.prep(con)
queries.conf_reset.prep(con)
if mode == lib.store.prepmode.admin then
end
else lib.bail('unsupported connection preparation mode') end
end];
dbsetup = [terra(src: &lib.store.source): bool
var res = lib.pq.PQexec([&lib.pq.PGconn](src.handle), schema)
if lib.pq.PQresultStatus(res) == lib.pq.PGRES_COMMAND_OK then
lib.report('successfully instantiated schema in database')
return true
else
lib.warn('backend pgsql - failed to initialize database: \n', lib.pq.PQresultErrorMessage(res))
return false
end
end];
obliterate_everything = [terra(src: &lib.store.source): bool
var res = lib.pq.PQexec([&lib.pq.PGconn](src.handle), obliterator)
if lib.pq.PQresultStatus(res) == lib.pq.PGRES_COMMAND_OK then
lib.report('successfully wiped out everything parsav-related in database')
return true
else
lib.warn('backend pgsql - failed to obliterate database: \n', lib.pq.PQresultErrorMessage(res))
return false
................................................................................
var a = row_to_actor(&r, 0)
a.ptr.source = src
var au = [lib.stat(lib.store.auth)] { ok = true }
au.val.aid = aid
au.val.uid = a.ptr.id
if not r:null(0,13) then -- restricted?
au.val.privs:clear()
(au.val.privs.post << r:bool(0,14))
(au.val.privs.edit << r:bool(0,15))
(au.val.privs.account << r:bool(0,16))
(au.val.privs.upload << r:bool(0,17))
(au.val.privs.moderate<< r:bool(0,18))
(au.val.privs.admin << r:bool(0,19))
else au.val.privs:fill() end
return au, a
end
::fail:: return [lib.stat (lib.store.auth) ] { ok = false },
[lib.mem.ptr(lib.store.actor)] { ptr = nil, ct = 0 }
................................................................................
post_retweet = [terra(
src: &lib.store.source,
uid: uint64,
post: uint64,
undo: bool
): {}
var time = lib.osclock.time(nil)
if not undo then
queries.post_react_simple.exec(src,uid,post,"rt",time)
else
queries.post_react_cancel.exec(src,uid,post,"rt")
end
end];
post_like = [terra(
src: &lib.store.source,
uid: uint64,
post: uint64,
undo: bool
): {}
var time = lib.osclock.time(nil)
if not undo then
queries.post_react_simple.exec(src,uid,post,"like",time)
else
queries.post_react_cancel.exec(src,uid,post,"like")
end
end];
post_liked_uid = [terra(
src: &lib.store.source,
uid: uint64,
................................................................................
-- check against default rights, insert records for wherever powers differ
lib.dbg('created new actor, establishing powers')
privupdate(src,ac)
lib.dbg('powers established')
return ac.id
end];
actor_rel_create = [terra(
src: &lib.store.source,
kind: uint16,
relator: uint64,
relatee: uint64
): {} queries.actor_rel_create.exec(src,kind,relator,relatee) end];
actor_rel_destroy = [terra(
src: &lib.store.source,
kind: uint16,
relator: uint64,
relatee: uint64
): {} queries.actor_rel_destroy.exec(src,kind,relator,relatee) end];
actor_rel_calc = [terra(
src: &lib.store.source,
relator: uint64,
relatee: uint64
): lib.store.relationship
var r = lib.store.relationship {
agent = relator, patient = relatee
} r.rel:clear()
r.recip:clear()
var res = queries.actor_rel_enum.exec(src,relator,relatee)
var recip = queries.actor_rel_enum.exec(src,relatee,relator)
if res.sz > 0 then defer res:free()
for i = 0, res.sz do
var bit = res:int(uint16, i, 0)-1
if bit < [#lib.store.relation.members] then r.rel:setbit(bit, true)
else lib.warn('unknown relationship type in database') end
end
end
if recip.sz > 0 then defer recip:free()
for i = 0, recip.sz do
var bit = recip:int(uint16, i, 0)-1
if bit < [#lib.store.relation.members] then r.recip:setbit(bit, true)
else lib.warn('unknown relationship type in database') end
end
end
return r
end];
actor_purge_uid = [terra(
src: &lib.store.source,
uid: uint64
) queries.actor_purge_uid.exec(src,uid) end];
auth_enum_uid = [terra(
................................................................................
): {}
var hash: uint8[lib.crypt.algsz.sha256]
if lib.md.mbedtls_md(lib.md.mbedtls_md_info_from_type(lib.crypt.alg.sha256.id),
[&uint8](pw.ptr), pw.ct, &hash[0]) ~= 0 then
lib.bail('cannot hash password')
end
if reset then queries.auth_purge_type.exec(src, nil, uid, 'pw-%') end
queries.auth_create_pw.exec(src, uid, binblob {ptr = &hash[0], ct = [hash.type.N]}, lib.osclock.time(nil), comment)
end];
auth_purge_pw = [terra(src: &lib.store.source, uid: uint64, handle: rawstring): {}
queries.auth_purge_type.exec(src, handle, uid, 'pw-%')
end];
auth_purge_otp = [terra(src: &lib.store.source, uid: uint64, handle: rawstring): {}
|