-
Notifications
You must be signed in to change notification settings - Fork 10
/
internal_services.jl
696 lines (621 loc) · 29.5 KB
/
internal_services.jl
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
module InternalServices
import HTTP, EzXML, JSON, URIs
using DataStructures: CircularBuffer
import Glob
import ..Utils
import ..Nostr
import ..Bech32
import ..DB
import ..Media
PORT = Ref(14000)
PRINT_EXCEPTIONS = Ref(true)
server = Ref{Any}(nothing)
router = Ref{Any}(nothing)
exceptions = CircularBuffer(200)
est = Ref{Any}(nothing)
short_urls = Ref{Any}(nothing)
verified_users = Ref{Any}(nothing)
inappropriate_names = Ref{Any}(nothing)
memberships = Ref{Any}(nothing)
membership_tiers = Ref{Any}(nothing)
membership_products = Ref{Any}(nothing)
function catch_exception(body::Function, handler::Symbol, args...)
try
body()
catch ex
push!(exceptions, (handler, time(), ex, args...))
PRINT_EXCEPTIONS[] && Utils.print_exceptions()
HTTP.Response(500, "error")
end
end
function start(cache_storage::DB.CacheStorage; setup_handlers=true)
@assert isnothing(server[])
est[] = cache_storage
short_urls[] = est[].params.MembershipDBDict(Int, String, "short_urls"; connsel=est[].pqconnstr,
init_queries=["create table if not exists short_urls (
idx int8 not null,
url text not null,
path text not null,
ext text not null
)",
"create index if not exists short_urls_idx on short_urls (idx asc)",
"create index if not exists short_urls_url on short_urls (url asc)",
"create index if not exists short_urls_path on short_urls (path asc)",
])
verified_users[] = est[].params.MembershipDBDict(String, Int, "verified_users"; connsel=est[].pqconnstr,
init_queries=["create table if not exists verified_users (name varchar(200) not null, pubkey bytea not null)",
"create index if not exists verified_users_pubkey on verified_users (pubkey asc)",
"create index if not exists verified_users_name on verified_users (name asc)",
])
inappropriate_names[] = est[].params.MembershipDBDict(String, Int, "inappropriate_names"; connsel=est[].pqconnstr,
init_queries=["create table if not exists inappropriate_names (name varchar(200) primary key)",
"create index if not exists inappropriate_names_name on inappropriate_names (name asc)",
])
memberships[] = est[].params.MembershipDBDict(String, Int, "memberships"; connsel=est[].pqconnstr,
init_queries=["create table if not exists memberships (
pubkey bytea,
tier varchar(300),
valid_until timestamp,
name varchar(500),
used_storage int8
)",
"create index if not exists memberships_pubkey on memberships (pubkey asc)",
])
membership_tiers[] = est[].params.MembershipDBDict(String, Int, "membership_tiers"; connsel=est[].pqconnstr,
init_queries=["create table if not exists membership_tiers (
tier varchar(300) not null,
max_storage int8
)",
"create index if not exists membership_tiers_tier on membership_tiers (tier asc)",
])
membership_products[] = est[].params.MembershipDBDict(String, Int, "membership_products"; connsel=est[].pqconnstr,
init_queries=["create table if not exists membership_products (
product_id varchar(100) not null,
tier varchar(300),
months int,
amount_usd decimal,
max_storage int8
)",
"create index if not exists membership_products_product_id on membership_products (product_id asc)",
])
router[] = HTTP.Router()
server[] = HTTP.serve!(router[], "0.0.0.0", PORT[])
if setup_handlers
HTTP.register!(router[], "POST", "/collect_metadata", function (req::HTTP.Request)
pubkeys = JSON.parse(String(req.body))
res = try
collect_metadata(pubkeys)
catch ex
["ERROR", string(ex)]
end
return HTTP.Response(200, JSON.json(res))
end)
HTTP.register!(router[], "/.well-known/nostr.json", nostr_json_handler)
for path in ["/**", "/"]
HTTP.register!(router[], path, preview_handler)
end
HTTP.register!(router[], "/media-upload", media_upload_handler)
HTTP.register!(router[], "/media-cache", media_cache_handler)
HTTP.register!(router[], "/link-preview", link_preview_handler)
HTTP.register!(router[], "/url-shortening", url_shortening_handler)
HTTP.register!(router[], "/url-lookup/*", url_lookup_handler)
HTTP.register!(router[], "/spam", spam_handler)
HTTP.register!(router[], "/api", api_handler)
HTTP.register!(router[], "/api/suggestions", suggestions_handler)
end
nothing
end
function stop()
@assert !isnothing(server[])
close(server[])
server[] = nothing
close(short_urls[])
short_urls[] = nothing
close(verified_users[])
verified_users[] = nothing
end
function collect_metadata(pubkeys)
cache_storage = est[]
pubkeys = [pk isa Nostr.PubKeyId ? pk : Nostr.PubKeyId(pk) for pk in pubkeys]
res = Dict()
for pk in pubkeys
haskey(res, pk) && continue
pk in cache_storage.meta_data || continue
eid = cache_storage.meta_data[pk]
eid in cache_storage.events || continue
res[pk] = cache_storage.events[eid]
end
Dict([(Nostr.hex(pk), e) for (pk, e) in res])
end
re_url = r"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)"
# re_url = r"[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)"
function get_image_links(content)
urls = String[]
for m in eachmatch(re_url, content)
url = m.match
_, ext = splitext(lowercase(url))
if ext in [".png", ".gif", ".jpg", ".jpeg", ".webp"]
push!(urls, url)
end
end
urls
end
function content_refs_resolved(e::Nostr.Event)
cache_storage = est[]
c = replace(e.content, DB.re_hashref => function (r)
i = parse(Int, r[3:end-1]) + 1
if 1 <= i <= length(e.tags)
tag = e.tags[i]
if length(tag.fields) >= 2
if tag.fields[1] == "p"
try
c = JSON.parse(cache_storage.events[cache_storage.meta_data[Nostr.PubKeyId(tag.fields[2])]].content)
return "@"*mdtitle(c)
catch _ end
end
end
end
""
end)
replace(c, DB.re_mention => function (r)
# prefix = "nostr:npub"
# startswith(r, prefix) || return r
if !isnothing(local pk = Bech32.nip19_decode_wo_tlv(r[7:end]))
try
c = JSON.parse(cache_storage.events[cache_storage.meta_data[pk]].content)
return "@"*mdtitle(c)
catch _ end
end
r
end)
end
function mdtitle(c::Dict)
for k in ["displayName", "display_name", "name", "username"]
if haskey(c, k) && !isnothing(c[k]) && !isempty(c[k])
return strip(c[k])
end
end
""
end
function mdpubkey(cache_storage, pk)
title = description = image = ""
url = nothing
if pk in cache_storage.meta_data
c = JSON.parse(cache_storage.events[cache_storage.meta_data[pk]].content)
if c isa Dict
addr = try strip(c["nip05"]) catch _ "" end
if !isnothing(addr) && endswith(addr, "@primal.net")
url = "https://primal.net/$(split(addr, '@')[1])"
end
try title = mdtitle(c) catch _ end
description = try replace(c["about"], re_url=>"") catch _ "" end
image = try c["picture"] catch _ "" end
end
end
if !isempty(local r = DB.exec(cache_storage.media, DB.@sql("select media_url, width, height from media where url = ?1 order by (width*height) limit 1"), (image,)))
image = r[1][1]
end
return (; title, description, image, (isnothing(url) ? (;) : (; url))...)
end
function get_meta_elements(host::AbstractString, path::AbstractString)
cache_storage = est[]
title = description = image = url = ""
twitter_card = "summary"
mdpubkey_(pk) = (; url="https://$host$path", twitter_card, mdpubkey(cache_storage, pk)...)
if !isnothing(local m = match(r"^/(profile|p)/(.*)", path))
pk = string(m[2])
pk = startswith(pk, "npub") ? Bech32.nip19_decode_wo_tlv(pk) : Nostr.PubKeyId(pk)
return mdpubkey_(pk)
elseif !isnothing(local m = match(r"^/(thread|e)/(.*)", path))
eid = string(m[2])
try eid = Bech32.nip19_decode(eid) catch _ end
isnothing(eid) && (eid = try Nostr.EventId(eid) catch _ end)
if eid isa Nostr.EventId
if eid in cache_storage.events
e = cache_storage.events[eid]
url = "https://$(host)/e/$(m[2])"
description = replace(content_refs_resolved(e), re_url => "")
if e.pubkey in cache_storage.meta_data
c = JSON.parse(cache_storage.events[cache_storage.meta_data[e.pubkey]].content)
title = mdtitle(c)
image = get(c, "picture", "")
end
media_urls = DB.exec(cache_storage.event_media, "select url from event_media where event_id = ?1 limit 1", (eid,))
if !isempty(media_urls)
twitter_card = "summary_large_image"
image = media_urls[1][1]
thumbnails = DB.exec(cache_storage.dyn[:video_thumbnails], DB.@sql("select thumbnail_url from video_thumbnails where video_url = ?1 limit 1"), (image,))
if !isempty(thumbnails)
image = (thumbnails[1][1],)
else
if !isempty(local r = DB.exec(cache_storage.media, DB.@sql("select media_url, width, height from media where url = ?1 order by (width*height) limit 1"), (image,)))
image = r[1][1]
end
end
end
end
elseif eid isa Vector
if startswith(string(m[2]), "naddr")
d = Dict(eid)
for (eid,) in DB.exec(cache_storage.dyn[:parametrized_replaceable_events],
DB.@sql("select event_id from parametrized_replaceable_events where pubkey = ?1 and kind = ?2 and identifier = ?3 limit 1"),
(d[Bech32.Author], d[Bech32.Kind], d[Bech32.Special]))
eid = Nostr.EventId(eid)
if eid in cache_storage.events
e = cache_storage.events[eid]
url = "https://$(host)/e/$(m[2])"
for t in e.tags
if length(t.fields) >= 2
t.fields[1] == "title" && (title = t.fields[2])
t.fields[1] == "summary" && (description = t.fields[2])
t.fields[1] == "image" && (image = t.fields[2])
end
end
twitter_card = "summary_large_image"
end
end
end
end
return (; title, description, image, url, twitter_card)
elseif !isnothing(local m = match(r"^/downloads?", path)) && 1==1
return (;
title="Download Primal apps and source code",
description="",
image="https://primal.net/public/primal-link-preview.jpg",
url="https://primal.net/downloads",
twitter_card = "summary_large_image",
twitter_image = "https://primal.net/images/twitter-hero.jpg")
elseif !isnothing(local m = match(r"^/(\?.|$)", path)) && 1==1
return (;
title="Primal",
description="The Social Bitcoin Wallet",
image=("https://primal.net/images/primal-link-preview.jpg",),
url="https://primal.net/",
twitter_card = "summary_large_image",
twitter_image = "https://primal.net/images/twitter-hero.jpg")
elseif !isnothing(local m = match(r"^/landing$", path)) && 1==1
return (;
title="Primal",
description="The Social Bitcoin Wallet",
image="https://primal.net/images/primal-link-preview.jpg",
url="https://primal.net/",
twitter_card = "summary_large_image",
twitter_image = "https://primal.net/images/twitter-hero.jpg")
elseif !isnothing(local m = match(r"^/(.*)", path))
name = string(m[1])
if !isempty(local pks = nostr_json_query_by_name(name))
return mdpubkey_(pks[1])
end
end
return nothing
end
index_html_reading = Utils.Throttle(; period=5.0, t=0)
index_html = Ref("")
index_doc = Ref{Any}(nothing)
index_elems = Dict{Symbol, EzXML.Node}()
# index_defaults = Dict{Symbol, String}()
index_lock = ReentrantLock()
# nostr_json_reading = Utils.Throttle(; period=5.0, t=0)
# nostr_json = Ref(Dict{String, String}())
APP_ROOT = Ref("www")
# NOSTR_JSON_FILE = Ref("nostr.json")
# function get_nostr_json()
# nostr_json_reading() do
# # nostr_json[] = JSON.parse(read("$(APP_ROOT[])/.well-known/nostr.json", String))["names"]
# nostr_json[] = JSON.parse(read(NOSTR_JSON_FILE[], String))["names"]
# # nostr_json[] = Dict([name=>Nostr.hex(Nostr.PubKeyId(pk)) for (name, pk) in DB.rows(verified_users[])])
# end
# nostr_json[]
# end
nostr_json_query_lock = ReentrantLock()
function nostr_json_query_by_name(name::String)
lock(nostr_json_query_lock) do
[Nostr.PubKeyId(pk) for (pk,) in DB.exec(verified_users[], DB.@sql("select pubkey from verified_users where lower(name) = lower(?1)"), (name,))]
end
end
function nostr_json_query_by_pubkey(pubkey::Nostr.PubKeyId; default_name=false)
lock(nostr_json_query_lock) do
q = if default_name
DB.@sql("select name from verified_users where pubkey = ?1 and default_name = true")
else
DB.@sql("select name from verified_users where pubkey = ?1")
end
[name for (name,) in DB.exec(verified_users[], q, (pubkey,))]
end
end
function preview_handler(req::HTTP.Request)
lock(index_lock) do
dochtml = index_html[]
try
host = Dict(req.headers)["Host"]
index_html_reading() do
index_html[] = read("$(APP_ROOT[])/index.html", String)
index_doc[] = doc = EzXML.parsehtml(index_html[])
for e in EzXML.findall("/html/head/meta", doc)
if haskey(e, "property") && startswith(e["property"], "og:")
# index_defaults[Symbol(e["property"][4:end])] = e["content"]
EzXML.unlink!(e)
end
end
head = EzXML.findall("/html/head", doc)[1]
for k in [:title,
:description,
:image,
:url,
]
el = EzXML.ElementNode("meta")
el["property"] = "og:$(k)"
EzXML.link!(head, el)
index_elems[k] = el
end
el = EzXML.ElementNode("meta")
el["name"] = "og:type"
el["content"] = "website"
EzXML.link!(head, el)
el = EzXML.ElementNode("meta")
el["name"] = "twitter:card"
EzXML.link!(head, el)
index_elems[:twitter_card] = el
el = EzXML.ElementNode("meta")
el["name"] = "twitter:image"
EzXML.link!(head, el)
index_elems[:twitter_image] = el
for e in EzXML.findall("/html/head/script", doc) # WARN needed to run client app in browser
EzXML.link!(e, EzXML.TextNode(" "))
end
end
if !isnothing(local mels = get_meta_elements(host, req.target))
for (k, v) in pairs(mels)
if k == :image && !isempty(v)
if v isa Tuple
v = v[1]
else
v = "https://primal.net/media-cache?u=$(URIs.escapeuri(v))"
end
end
v = strip(v)
if !isempty(v)
index_elems[k]["content"] = v
else
delete!(index_elems[k], "content")
end
end
eltitle = EzXML.findall("/html/head/title", index_doc[])[1]
for el in EzXML.nodes(eltitle)
EzXML.unlink!(el)
end
maxlen = 50
s = mels.description
s = length(s) > maxlen ? (first(s, maxlen) * "...") : s
s = strip(mels.title * ": " * s)
EzXML.link!(eltitle, EzXML.TextNode(s))
dochtml = string(index_doc[])
end
catch ex
push!(exceptions, (:preview_handler, time(), ex, req))
PRINT_EXCEPTIONS[] && Utils.print_exceptions()
end
HTTP.Response(200, HTTP.Headers(["Content-Type"=>"text/html",
"Cache-Control"=>"no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0",
]), dochtml)
end
end
function nostr_json_handler(req::HTTP.Request)
catch_exception(:nostr_json_handler, req) do
host = Dict(req.headers)["Host"]
path = req.target
uri = HTTP.URI("https://$(host)$(path)")
nj = Dict()
if !isnothing(local m = match(r"^name=(.*)", uri.query))
name = string(m[1])
if !isempty(local pks = nostr_json_query_by_name(name))
nj = Dict([name=>pks[1]])
end
end
HTTP.Response(200, HTTP.Headers(["Content-Type"=>"application/json"]), JSON.json((; names=nj), 2))
end
end
function media_upload_handler(req::HTTP.Request)
catch_exception(:media_upload_handler, req) do
host = Dict(req.headers)["Host"]
path, query = split(req.target, '?')
args = NamedTuple([Symbol(k)=>v for (k, v) in [split(s, '=') for s in split(query, '&')]])
url = string(URIs.unescapeuri(args.u))
HTTP.Response(302, HTTP.Headers(["Location"=>url, "Cache-Control"=>"no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0"]))
end
end
function media_cache_handler(req::HTTP.Request)
catch_exception(:media_cache_handler, req) do
host = Dict(req.headers)["Host"]
path, query = split(req.target, '?')
args = NamedTuple([Symbol(k)=>v for (k, v) in [split(s, '=') for s in split(query, '&')]])
variant = if !haskey(args, :s)
(:original, true)
else (let s=args.s
if s=="o"; :original
elseif s=="s"; :small
elseif s=="m"; :medium
elseif s=="l"; :large
else error("invalid size")
end
end,
if !haskey(args, :a); true
elseif args.a == "1"; true
elseif args.a == "0"; false
else error("invalid animated")
end)
end
send_content = !(haskey(args, :c) && args.c == "0")
# send_content = 1==1
url = string(URIs.unescapeuri(args.u))
variants = variant == (:original, true) ? [variant] : Media.all_variants
tr = @elapsed (r = Media.media_variants(est[], url, variants; sync=true))
# println("$tr $query")
if isnothing(r)
HTTP.Response(302, HTTP.Headers(["Location"=>url, "Cache-Control"=>"no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0"]))
else
resurl = r[variant]
if !send_content
HTTP.Response(302, HTTP.Headers(["Location"=>resurl, "Cache-Control"=>"no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0"]))
else
@assert startswith(resurl, Media.MEDIA_URL_ROOT[])
fp = Media.MEDIA_PATH[] * resurl[length(Media.MEDIA_URL_ROOT[])+1:end]
fpb, ext = splitext(fp)
ct = "application/octet-stream"
for (mt, ext2) in Media.mimetype_ext
if ext2 == ext
ct = mt
break
end
end
ps = splitpath(fpb)
for p in Glob.glob(ps[end]*"*", join(ps[1:end-1], '/')[2:end])
rm(p)
end
r = Media.media_variants(est[], url, variants; sync=true)
if isnothing(r)
HTTP.Response(302, HTTP.Headers(["Location"=>url, "Cache-Control"=>"no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0"]))
else
resurl = r[variant]
if !send_content
HTTP.Response(302, HTTP.Headers(["Location"=>resurl, "Cache-Control"=>"no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0"]))
else
fp = Media.MEDIA_PATH[] * resurl[length(Media.MEDIA_URL_ROOT[])+1:end]
if !isfile(fp)
HTTP.Response(302, HTTP.Headers(["Location"=>resurl, "Cache-Control"=>"no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0"]))
else
d = read(fp)
# HTTP.Response(200, HTTP.Headers(["Content-Type"=>ct, "Content-Length"=>"$(length(d))", "Cache-Control"=>"no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0"]), d)
HTTP.Response(200, HTTP.Headers(["Content-Type"=>ct, "Content-Length"=>"$(length(d))", "Cache-Control"=>"max-age=20"]), d)
end
end
end
end
end
end
end
function link_preview_handler(req::HTTP.Request)
catch_exception(:link_preview_handler, req) do
host = Dict(req.headers)["Host"]
path, query = split(req.target, '?')
args = NamedTuple([Symbol(k)=>v for (k, v) in [split(s, '=') for s in split(query, '&')]])
url = string(URIs.unescapeuri(args.u))
r = Media.fetch_resource_metadata(url)
HTTP.Response(200, api_headers, JSON.json(r))
end
end
short_urls_lock = ReentrantLock()
short_urls_chars = ['A':'Z'..., 'a':'z'...]
URL_SHORTENING_ROOT_URL = Ref("https://m.primal.net")
function url_shortening(url)
r = DB.exec(short_urls[], DB.@sql("select path, ext from short_urls where url = ?1 limit 1"), (url,))
if isempty(r)
maxidx = DB.exec(short_urls[], DB.@sql("select max(idx) from short_urls"))[1][1]
ismissing(maxidx) && (maxidx = 0)
idx = maxidx + 1
i = 1000000+idx
cs = []
while i > 0
push!(cs, short_urls_chars[i % length(short_urls_chars) + 1])
i = i ÷ length(short_urls_chars)
end
u = URIs.parse_uri(url)
ext = lowercase(splitext(u.path)[end])
spath = join(reverse(cs))
DB.exec(short_urls[], DB.@sql("insert into short_urls values (?1, ?2, ?3, ?4)"), (idx, url, spath, ext))
else
spath, ext = r[1]
end
"$(URL_SHORTENING_ROOT_URL[])/$spath$ext"
end
function url_shortening_handler(req::HTTP.Request)
catch_exception(:url_shortening_handler, req) do
lock(short_urls_lock) do
host = Dict(req.headers)["Host"]
path, query = split(req.target, '?')
args = NamedTuple([Symbol(k)=>v for (k, v) in [split(s, '=') for s in split(query, '&')]])
url = string(URIs.unescapeuri(args.u))
res = url_shortening(url)
HTTP.Response(200, HTTP.Headers(["Content-Type"=>"application/text"]), res)
end
end
end
function url_lookup_handler(req::HTTP.Request)
catch_exception(:url_lookup_handler, req) do
host = Dict(req.headers)["Host"]
spath = splitext(string(split(req.target, '/')[end]))[1]
r = DB.exec(short_urls[], DB.@sql("select url from short_urls where path = ?1 limit 1"), (spath,))
if isnothing(r)
HTTP.Response(404, "unknown url")
else
url = r[1][1]
rurl = "https://primal.b-cdn.net/media-upload?u=$(URIs.escapeuri(url))"
HTTP.Response(302, HTTP.Headers(["Location"=>rurl]))
end
end
end
function spam_handler(req::HTTP.Request)
catch_exception(:spam_handler, req) do
host = Dict(req.headers)["Host"]
res = Main.Filterlist.get_dict()
HTTP.Response(200, HTTP.Headers(["Content-Type"=>"application/json"]), JSON.json(res, 2))
end
end
api_headers = HTTP.Headers(["Content-Type"=>"application/json",
"Access-Control-Allow-Origin"=>"*",
"Access-Control-Allow-Methods"=>"*",
"Access-Control-Allow-Headers"=>"*"
])
function api_handler(req::HTTP.Request)
catch_exception(:api_handler, req) do
req.method == "OPTIONS" && return HTTP.Response(200, api_headers, "ok")
body = String(req.body)
# @show req.method req.target body
host = Dict(req.headers)["Host"]
filt = JSON.parse(body)
funcall = Symbol(filt[1])
@assert funcall in Main.App.exposed_functions
kwargs = Pair{Symbol, Any}[Symbol(k)=>v for (k, v) in get(filt, 2, Dict())]
res = Ref{Any}(nothing)
try
Main.CacheServerHandlers.metrics_logged(funcall, kwargs; subid="http") do
wait(Threads.@spawn Base.invokelatest(Main.CacheServerHandlers.app_funcall, funcall, kwargs,
function(r)
res[] = Main.App.content_moderation_filtering_2(est[], r, funcall, kwargs)
end))
end
catch ex
PRINT_EXCEPTIONS[] && Utils.print_exceptions()
ex isa TaskFailedException && (ex = ex.task.result)
res[] = (; error=(ex isa ErrorException ? ex.msg : "error"))
end
HTTP.Response(200, api_headers, JSON.json(res[]))
end
end
function suggestions_handler(req::HTTP.Request)
catch_exception(:suggestions_handler, req) do
req.method == "OPTIONS" && return HTTP.Response(200, api_headers, "ok")
body = String(req.body)
# @show req.method req.target body
host = Dict(req.headers)["Host"]
d = JSON.parse(read(Main.App.SUGGESTED_USERS_FILE[], String))
mds = Dict{Nostr.PubKeyId, Nostr.Event}()
for (_, ms) in d
for (pubkey, name) in ms
pubkey = Nostr.PubKeyId(pubkey)
if !haskey(mds, pubkey) && pubkey in est[].meta_data
eid = est[].meta_data[pubkey]
if eid in est[].events
mds[pubkey] = est[].events[eid]
end
end
end
end
res = (;
metadata=Dict([Nostr.hex(pk)=>md for (pk, md) in mds]),
suggestions=[(; group, members=[(; name, pubkey) for (pubkey, name) in ms])
for (group, ms) in d])
HTTP.Response(200, api_headers, JSON.json(res))
end
end
end