-
Notifications
You must be signed in to change notification settings - Fork 4
/
query.sp
512 lines (497 loc) · 12.5 KB
/
query.sp
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
query "timeline" {
sql = <<EOQ
with toots as (
select
case when display_name = '' then username else display_name end as person,
case
when reblog -> 'url' is null then
content
else
reblog_content
end as toot,
to_char(created_at, 'YYYY-MM-DD HH24:MI') as created_at,
case
when reblog -> 'url' is not null then '▲'
else ' '
end as boosted,
case
when in_reply_to_account_id is not null then ' → ' || ( select acct from mastodon_account where id = in_reply_to_account_id )
else ''
end as in_reply_to,
case
when reblog -> 'url' is not null then instance_qualified_reblog_url
else instance_qualified_url
end as url,
case
when reblog is not null then reblog ->> 'reblogs_count'
else ''
end as reblog_count,
case
when reblog is not null then reblog ->> 'favourites_count'
else ''
end as fave_count,
reblog
from
mastodon_toot
where
timeline = $1
and url !~ '${local.timeline_exclude}'
limit $2
),
boosted as (
select
created_at,
$3 as boost,
boosted,
in_reply_to,
person,
toot,
reblog_count,
fave_count,
url,
reblog
from
toots
order by
created_at desc
)
select
created_at,
person ||
case
when in_reply_to is null then ''
else in_reply_to
end as person,
case
when reblog is not null then boosted || ' ' || toot || ' ★ ' || fave_count || ' ▲ ' || reblog_count
else boosted || ' ' || toot
end as toot,
url
from
boosted
where
boost = boosted
or boost = 'include'
or boost = 'n/a'
EOQ
param "timeline" {}
param "limit" {}
param "boost" {}
}
query "search_status" {
sql = <<EOQ
with toots as (
select
account_url as account,
case when display_name = '' then username else display_name end as person,
case
when reblog -> 'url' is null then
content
else
reblog_content
end as toot,
to_char(created_at, 'MM-DD HH24:MI') as created_at,
case
when reblog -> 'url' is not null then '▲'
else ''
end as boosted,
case
when in_reply_to_account_id is not null then ' → ' || ( select acct from mastodon_account where id = in_reply_to_account_id )
else ''
end as in_reply_to,
case
when reblog -> 'url' is not null then reblog ->> 'url'
else url
end as url
from
mastodon_toot
where
timeline = 'search_status'
and query = $1
limit ${local.limit}
)
select
account,
person ||
case
when in_reply_to is null then ''
else in_reply_to
end as person,
boosted || ' ' || substring(toot from 1 for 200) as toot,
url
from
toots
order by
created_at desc
EOQ
param "search_term" {}
}
query "favorite" {
sql = <<EOQ
with toots as (
select
case when display_name = '' then username else display_name end as person,
case
when reblog -> 'url' is null then
content
else
reblog_content
end as toot,
to_char(created_at, 'YYYY-MM-DD HH24:MI') as created_at,
case
when reblog -> 'url' is not null then '▲'
else ''
end as boosted,
case
when in_reply_to_account_id is not null then ' → ' || ( select acct from mastodon_account where id = in_reply_to_account_id )
else ''
end as in_reply_to,
instance_qualified_url
from
mastodon_favorite
limit $1
)
select
created_at,
person ||
case
when in_reply_to is null then ''
else in_reply_to
end as person,
boosted || ' ' || substring(toot from 1 for 200) as toot,
instance_qualified_url
from
toots
order by
created_at desc
limit $1
EOQ
param "limit" {}
}
/*
The duplicate code in the above three queries could be DRYed out using a Postgres function parameterized by table name. But there's
not currently a standard way to deploy a mod that defines and uses functions. If we could alternatively parameterize queries by
table name in HCL that would be very nice.
*/
query "search_hashtag" {
sql = <<EOQ
with data as (
select
name,
url || '.rss' as feed_link
from
mastodon_search_hashtag
where
query = $1
and name = query
limit 1
)
select
to_char(r.published, 'YYYY-MM-DD') as published,
d.name as tag,
(
select string_agg(trim(JsonString::text, '"'), ', ')
from jsonb_array_elements(r.categories) JsonString
) as categories,
r.guid as link,
( select content as toot from mastodon_toot where timeline = 'search_status' and query = r.guid ) as content
from
data d
join
rss_item r
on
r.feed_link = d.feed_link
order by
r.published desc
limit 10
EOQ
}
query "search_people" {
sql = <<EOQ
with data as (
select
id,
instance_qualified_account_url,
case when display_name = '' then username else display_name end as person,
to_char(created_at, 'YYYY-MM-DD') as created_at,
followers_count,
following_count,
statuses_count as toots,
note
from
mastodon_search_account
where
query = $1
order by
person
)
select
d.instance_qualified_account_url,
d.person,
case when r.following then '✔️' else '' end as i_follow,
case when r.followed_by then '✔️' else '' end as follows_me,
d.created_at,
d.followers_count as followers,
d.following_count as following,
d.toots,
d.note
from
data d
join
mastodon_relationship r
on
d.id = r.id
EOQ
param "search_term" {}
}
query "followers" {
sql = <<EOQ
with data as (
select
l.title as list,
a.*
from
mastodon_list l
join
mastodon_list_account a
on
l.id = a.list_id
),
combined as (
select
d.list,
f.instance_qualified_account_url,
case when f.display_name = '' then f.username else f.display_name end as person,
to_char(f.created_at, 'YYYY-MM-DD') as since,
f.followers_count as followers,
f.following_count as following,
f.statuses_count as toots,
f.note
from
mastodon_followers f
left join
data d
on
f.id = d.id
)
select
*
from
combined
order by
person
EOQ
}
/*
Joining with `mastodon_relationship` is possible, and useful -- I want to see at a glance
if a person I follow has followed me back! -- but not yet practical. The API's `accounts/relationships`
endpoint takes an array of ids, but the `mastodon_relationship` table for now only takes one id at a time because
you can't make an URL like `accounts/relationships?id[]=1&id[]=2...&id[]=500`. The one-at-a-time approach
is not only slow, but worse, quickly exhausts the 300-API-calls-per-5-minutes limit if you are following
hundreds of people.
TBD: Work out a way to query `mastodon_relationship` with batches of (10? 100?) ids.
Meanwhile, see query.search_people, this approach is practical there if the query yields a small result set.
*/
query "following" {
sql = <<EOQ
with data as (
select
l.title as list,
a.*
from
mastodon_list l
join
mastodon_list_account a
on
l.id = a.list_id
),
combined as (
select
d.list,
f.instance_qualified_account_url,
case when f.display_name = '' then f.username else f.display_name end as person,
to_char(f.created_at, 'YYYY-MM-DD') as since,
f.followers_count as followers,
f.following_count as following,
f.statuses_count as toots,
f.note
from
mastodon_following f
left join
data d
on
f.id = d.id
)
select
*
from
combined
order by
person
EOQ
}
query "notification" {
sql = <<EOQ
with notifications as (
select
category,
instance_qualified_account_url,
account_id,
display_name as person,
to_char(created_at, 'MM-DD HH24:MI') as created_at,
instance_qualified_status_url,
status_content
from
mastodon_notification
limit $1
)
select
n.created_at,
n.category,
n.person,
n.instance_qualified_account_url,
case when r.following then '✔️' else '' end as following,
case when r.followed_by then '✔️' else '' end as followed_by,
substring(n.status_content from 1 for 200) as toot,
case
when n.instance_qualified_status_url != '' then n.instance_qualified_status_url
else n.instance_qualified_account_url
end as url
from
notifications n
join
mastodon_relationship r
on
r.id = n.account_id
order by
n.created_at desc
EOQ
}
query "list" {
sql = <<EOQ
with list_ids as (
select
id,
title as list
from
mastodon_list
),
data as (
select
l.list,
to_char(t.created_at, 'YYYY-MM-DD') as day,
case when t.display_name = '' then t.username else t.display_name end as person,
t.instance_qualified_url as url,
substring(t.content from 1 for 200) as toot
from
mastodon_toot t
join
list_ids l
on
l.id = t.list_id
where
timeline = 'list'
and l.list = $1
and t.reblog -> 'url' is null -- only original posts
and t.in_reply_to_account_id is null -- only original posts
limit 20
)
select distinct on (person, day) -- only one per person per day
day,
person,
toot,
url
from
data
order by
day desc, person
EOQ
param "title" {}
}
query "list_account" {
sql = <<EOQ
select
l.title as list,
array_to_string( array_agg( lower(a.username) order by lower(a.username)), ', ') as people
from
mastodon_list l
join
mastodon_list_account a
on
l.id = a.list_id
group by
l.title
EOQ
}
query "list_account_follows" {
sql = <<EOQ
with list_account as (
select
a.id,
l.title as list
from
mastodon_list l
join mastodon_list_account a on l.id = a.list_id
),
list_account_follows as (
select
list
from
mastodon_following
left join list_account using (id)
)
select 'follows listed' as label, count(*) from list_account_follows where list is not null
union
select 'follows unlisted' as label, count(*) from list_account_follows where list is null
EOQ
}
query "my_toots" {
sql = <<EOQ
with data as (
select
account_url as account,
to_char(created_at, 'YYYY-MM-DD HH24:MI') as created_at,
case
when reblog -> 'url' is not null then '▲'
else ''
end as boosted,
case
when reblog is null then
substring(content from 1 for 300) || ' ★ ' || (status->>'favourites_count') || ' ▲ ' || (status->>'reblogs_count')
else
substring(reblog_content from 1 for 300) || ' ★ ' || (reblog->>'favourites_count') || ' ▲ ' || (reblog->>'reblogs_count')
end as toot,
case
when in_reply_to_account_id is not null then ' → ' || ( select acct from mastodon_account where id = in_reply_to_account_id )
else ''
end as in_reply_to,
case
when reblog -> 'url' is not null then instance_qualified_reblog_url
else instance_qualified_url
end as instance_qualified_url
from
mastodon_toot
where
timeline = 'me'
limit $1
)
select
created_at,
instance_qualified_url,
boosted || ' ' || toot as toot
from
data
order by
created_at desc
EOQ
param "limit" {}
}
query "connection" {
sql = <<EOQ
select
_ctx ->> 'connection_name' as connection,
name as server
from
mastodon_server
EOQ
}