forked from tensorfork/tlarc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
news.arc
4084 lines (3375 loc) · 135 KB
/
news.arc
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
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
742
743
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; News. 2 Sep 06.
; to run news: (nsv), then go to http://localhost:8080
; put usernames of admins, separated by whitespace, in arc/admins
; bug: somehow (+ votedir* nil) is getting evaluated.
(declare 'atstrings t)
(load "firebase.arc")
(load "algolia.arc")
(= site-name* "Tensorfork Labs"
site-abbrev* "TL"
site-email* "tl@@tensorfork.com"
site-twitter* "theshawwn"
site-discord* "shawwn#3694"
discord-url* "https://discord.gg/x52Xz3y"
site-url* "https://www.tensorfork.com"
parent-url* "https://www.tensorfork.com"
welcome-url* "/welcome.html"
favicon-url* ""
site-desc* "Tensorfork Labs" ; for rss feed
site-color* (color 154 186 170)
border-color* (color 154 186 170)
prefer-url* t)
; Structures
; Could add (html) types like choice, yesno to profile fields. But not
; as part of deftem, which is defstruct. Need another mac on top of
; deftem. Should not need the type specs in user-fields.
(deftem profile
id nil
name nil
created (seconds)
auth 0
member nil
submitted nil
favorites nil
votes nil ; for now just recent, elts each (time id by sitename dir)
karma 1
avg nil
weight .5
ignore nil
email nil
verified nil ; if (is (uvar u email) (uvar u verified)), then email is valid
about nil
showdead nil
notify nil
noprocrast nil
firstview nil
lastview nil
maxvisit 20
minaway 180
topcolor nil
keys nil
delay 0)
(deftem item
id nil
type nil
by nil
ip nil
time (seconds)
url nil
title nil
text nil
votes nil ; elts each (time ip user type score)
score 0
sockvotes 0
flags nil
dead nil
deleted nil
parts nil
parent nil
kids nil
keys nil)
; Load and Save
(= newsdir* "arc/news/"
storydir* "arc/news/story/"
profdir* "arc/news/profile/"
votedir* "arc/news/vote/")
(or= votes* (table) profs* (table))
(= initload-users* t)
(def load-news ((o reload))
(when reload
(load "news.arc")
;(= caching* 0)
)
(map ensure-dir (list arcdir* newsdir* storydir* votedir* profdir*))
(unless stories* (load-items))
(if (and initload-users* (empty profs*)) (load-users)))
(or= srv-port* (readenv "PORT" 8080))
(def nsv ((o port srv-port*))
(load-news)
(asv port))
(def run-news ((o port srv-port*))
(prn "srv-port* " (= srv-port* port))
(prn "srv-noisy* " (= srv-noisy* (readenv "NOISY" nil)))
(prn "caching* " (= caching* (readenv "CACHING" 1)))
(prn "explicit-flush " (declare 'explicit-flush (readenv "FLUSH" t)))
(flushout)
(nsv port))
(def load-users ()
(pr "load users: ")
(noisy-each 100 id (dir profdir*)
(load-user id)))
; For some reason vote files occasionally get written out in a
; broken way. The nature of the errors (random missing or extra
; chars) suggests the bug is lower-level than anything in Arc.
; Which unfortunately means all lists written to disk are probably
; vulnerable to it, since that's all save-table does.
(def load-user (u)
(= (votes* u) (load-table (+ votedir* u))
(profs* u) (temload 'profile (+ profdir* u)))
u)
; Have to check goodname because some user ids come from http requests.
; So this is like safe-item. Don't need a sep fn there though.
(def profile (u)
(or (profs* u)
(aand (goodname u)
(file-exists (+ profdir* u))
(= (profs* u) (temload 'profile it)))))
(def votes (u)
(or (votes* u)
(aand (file-exists (+ votedir* u))
(= (votes* u) (load-table it)))))
(def make-user (name (o email (user->email* name)) (o ip (get-ip)))
(inst 'profile
'id name
; For the moment, trust that users aren't giving bogus emails
; at signup. No need to force them to verify just yet.
'email email
'verified email
; Punish users for giving out their email by spamming them
; with cat pictures and NP-hard problems.
; https://www.explainxkcd.com/wiki/index.php/287:_NP-Complete
; Actually, I've been reluctant to notify by default. But it's
; been a surprisingly popular feature, so this might be ok.
'notify t))
(def init-user (u (o email (user->email* u)))
(= (votes* u) (table)
(profs* u) (make-user u email))
(save-votes u)
(save-prof u)
(newslog (get-ip) u 'noob email)
u)
; Need this because can create users on the server (for other apps)
; without setting up places to store their state as news users.
; See the admin op in app.arc. So all calls to login-page from the
; news app need to call this in the after-login fn.
(def ensure-news-user (u)
(if (profile u) u (init-user u)))
(def save-votes (u) (save-table (votes* u) (+ votedir* u)))
(def save-prof (u) (save-table (profs* u) (+ profdir* u)) (hook 'save-prof (profs* u)))
(mac uvar (u k) `((profile ,u) ',k))
(mac karma (u) `(uvar ,u karma))
(mac ignored (u) `(uvar ,u ignore))
; Note that users will now only consider currently loaded users.
(def users ((o f idfn))
(keep f (keys profs*)))
(def check-key (u k)
(and u (mem k (uvar u keys))))
(def author (u i) (is u i!by))
(or= stories* nil comments* nil
items* (table) url->story* (table)
maxid* 0 initload* 15000)
; The dir expression yields stories in order of file creation time
; (because arc infile truncates), so could just rev the list instead of
; sorting, but sort anyway.
; Note that stories* etc only include the initloaded (i.e. recent)
; ones, plus those created since this server process started.
; Could be smarter about preloading by keeping track of popular pages.
(def load-items ()
(map rmrf (glob (+ storydir* "*.tmp")))
(pr "load items: ")
(with (items (table)
ids (sort > (map int (dir storydir*))))
(if ids (= maxid* (car ids)))
(noisy-each 100 id (firstn initload* ids)
(let i (load-item id)
(push i (items i!type))))
(= stories* (rev (merge (compare < !id) items!story items!poll))
comments* (rev items!comment))
(hook 'initload items))
(ensure-topstories))
(def ensure-topstories ()
(aif (errsafe (readfile1 (+ newsdir* "topstories")))
(= ranked-stories* (map item it))
(do (prn "ranking stories.")
(flushout)
(gen-topstories))))
(def astory (i) (is i!type 'story))
(def acomment (i) (is i!type 'comment))
(def apoll (i) (is i!type 'poll))
(def load-item (id)
(let i (temload 'item (+ storydir* id))
(= (items* id) i)
(awhen (and (astory&live i) (check i!url ~blank))
(register-url i it))
i))
; Note that duplicates are only prevented of items that have at some
; point been loaded.
(def register-url (i url)
(= (url->story* (canonical-url url)) i!id))
; redefined later
(or= stemmable-sites* (table))
(def canonical-url (url)
(if (stemmable-sites* (sitename url))
(cut url 0 (pos #\? url))
url))
(def new-item-id ()
(do1 (evtil (++ maxid*) [~file-exists (+ storydir* _)])
(hook 'maxid maxid*)))
(def item (id)
(or (items* id) (errsafe:load-item id)))
(def kids (i) (map item i!kids))
; For use on external item references (from urls). Checks id is int
; because people try e.g. item?id=363/blank.php
(def safe-item (id)
(ok-id&item (if (isa id 'string) (saferead id) id)))
(def ok-id (id)
(and (exact id) (<= 1 id maxid*)))
(def arg->item (req key)
(safe-item:saferead (arg req key)))
(def live (i) (nor i!dead i!deleted (flagged i)))
(def save-item (i) (save-table i (+ storydir* i!id)) (hook 'save-item i))
(def kill (i how)
(when (nor i!dead (mem how i!keys))
(log-kill i how)
(wipe (comment-cache* i!id))
(set i!dead))
(when (in how 'flagged 'dupe)
(pushnew how i!keys))
(save-item i))
(or= kill-log* nil)
(def log-kill (i how)
(push (list i!id how) kill-log*))
(mac each-loaded-item (var . body)
(w/uniq g
`(let ,g nil
(loop (= ,g maxid*) (> ,g 0) (-- ,g)
(whenlet ,var (items* ,g)
,@body)))))
(def loaded-items (test)
(accum a (each-loaded-item i (test&a i))))
(def newslog args (apply srvlog 'news args))
; Ranking
; Votes divided by the age in hours to the gravityth power.
; Would be interesting to scale gravity in a slider.
(= gravity* 1.8 timebase* 120 front-threshold* -10
nourl-factor* 1.0 lightweight-factor* .3 )
(def frontpage-rank (s (o scorefn realscore) (o gravity gravity*))
(* (/ (let base (- (scorefn s) 0)
(if (> base 0) (expt base .8) base))
(expt (/ (+ (item-age s) timebase*) 60) gravity))
(if (no (in s!type 'story 'poll)) .5
(blank s!url) nourl-factor*
(lightweight s) (min lightweight-factor*
(contro-factor s))
(contro-factor s))))
(def contro-factor (s)
(aif (check (visible-family nil s) [> _ 20])
(min 1 (expt (/ (realscore s) it) 2))
1))
(def realscore (i)
(if (mem 'bury i!keys) -1000 (- i!score i!sockvotes)))
(disktable lightweights* (+ newsdir* "lightweights"))
(def lightweight (s)
(or s!dead
(mem 'rally s!keys) ; title is a rallying cry
(mem 'image s!keys) ; post is mainly image(s)
(lightweights* (sitename s!url))
(lightweight-url s!url)))
(defmemo lightweight-url (url)
(in (downcase (last (tokens url #\.))) "png" "jpg" "jpeg"))
(def apath (x)
(is ((str x) 0) #\/))
(def item-paths (i) (keep apath i!keys))
(def item-age (i) (minutes-since i!time))
(def user-age (u) (minutes-since (uvar u created)))
; Only looks at the 1000 most recent stories, which might one day be a
; problem if there is massive spam.
(def ranked-stories ((o n 500))
(map !id (firstn n ranked-stories*)))
(def gen-topstories ()
(= ranked-stories* (rank-stories 180 1000 (memo frontpage-rank)))
(hook 'save-topstories))
(def save-topstories ()
(let ids (ranked-stories)
(writefile ids (+ newsdir* "topstories"))
(hook 'save-topstories ids)))
(defhook save-topstories ((o ids (ranked-stories)))
(firebase-set "v0/topstories" ids))
(def rank-stories (n consider scorefn)
(bestn n (compare > scorefn) (latest-items metastory nil consider)))
; With virtual lists the above call to latest-items could be simply:
; (map item (retrieve consider metastory:item (gen maxid* [- _ 1])))
(def latest-items (test (o stop) (o n))
(accum a
(catch
(down id maxid* 1
(let i (item id)
(if (or (and stop (stop i)) (and n (<= n 0)))
(throw))
(when (test i)
(a i)
(if n (-- n))))))))
; redefined later
(def metastory (i) (and i (in i!type 'story 'poll)))
(def adjust-rank (s (o scorefn frontpage-rank))
(insortnew (compare > (memo scorefn)) s ranked-stories*)
(save-topstories))
; If something rose high then stopped getting votes, its score would
; decline but it would stay near the top. Newly inserted stories would
; thus get stuck in front of it. I avoid this by regularly adjusting
; the rank of a random top story.
(defbg rerank-random 30 (rerank-random))
(def rerank-random ()
(when ranked-stories*
(adjust-rank (ranked-stories* (rand (min 50 (len ranked-stories*)))))))
(def rerank-stories ()
(atomic (= ranked-stories*
(sort (compare > (memo frontpage-rank))
(latest-items metastory)))))
(def subs (i)
(if (mem '/l/private i!keys)
i!keys
(cons '/l/all i!keys)))
(defmemo match-subs (x)
(let x (or x "all")
(apply orf
(each x (tokens (str x) #\|)
(let fns (each x (tokens x #\&)
(if (begins x "!")
(let x (sym (string "/l/" (cut x 1)))
(out [~mem x (subs _)]))
(let x (sym (string "/l/" x))
(out [mem x (subs _)]))))
(unless (empty fns)
(out (apply andf fns))))))))
(def topstories (user n (o sub) (o threshold front-threshold*))
(let sub (match-subs sub)
(retrieve n
[and (>= (realscore _) threshold)
(cansee user _)
(sub _)]
ranked-stories*)))
(= max-delay* 10)
(def private (i)
(mem '/l/private superparent.i!keys))
(def cansee (user i)
(if i!deleted (admin user)
i!dead (or (author user i) (seesdead user))
(delayed i) (author user i)
(private i) (or (author user i) (admin user))
t))
(def isfrom (url i)
(let u (sitename i!url)
(and (~empty u) (is url u))))
(let mature (table)
(def delayed (i)
(and (no (mature i!id))
(acomment i)
(or (< (item-age i) (min max-delay* (uvar i!by delay)))
(do (set (mature i!id))
nil)))))
(def seesdead (user)
(or (and user (uvar user showdead) (no (ignored user)))
(editor user)))
(def visible (user is)
(keep [cansee user _] is))
(def fromsite (url (o is stories*))
(keep [isfrom url _] is))
(def cansee-descendant (user c)
(or (cansee user c)
(some [cansee-descendant user (item _)]
c!kids)))
(def editor (u)
(and u (or (admin u) (> (uvar u auth) 0))))
(def member (u)
(and u (or (admin u) (uvar u member))))
; Page Layout
(= up-url* (static-src "grayarrow.gif")
down-url* (static-src "graydown.gif")
logo-url* (static-src "ln.png"))
; redefined later
(def rss-url ((o label))
(if (is label "comments") "/rsscomments" "/rss"))
(def gen-head (title label)
(tag head
(gentag meta name 'viewport content "width=device-width, initial-scale=1.0")
(gentag meta name 'description content site-desc*)
(gentag meta name 'theme-color content "#@(hexrep sand)")
(gentag meta name 'msapplication-TileColor content "#@(hexrep orangered)")
(gentag link rel 'manifest href (static-src "site.webmanifest"))
(gentag link rel 'stylesheet type 'text/css href (static-src "news.css"))
(gentag link rel 'mask-icon href (static-src "safari-pinned-tab.svg") color teal)
(gentag link rel "shortcut icon" href "")
(gentag link rel 'apple-touch-icon sizes '180x180 href (static-src "apple-touch-icon.png"))
(gentag link rel 'icon type 'image/png sizes '32x32 href (static-src "favicon-32x32.png"))
(gentag link rel 'icon type 'image/png sizes '16x16 href (static-src "favicon-16x16.png"))
(gentag link rel 'alternate type 'application/rss+xml
title 'RSS href (rss-url label))
(tag title (pr:eschtml title))
(tag (script) (pr votejs*))
(when (in label "place" "/l/place")
(tag (script src (static-src "place.js")))
(tag (style) (pr "body { background-color: #@(hexrep sand); }")))))
(mac npage (title label . body)
`(tag html
(gen-head ,title ,label)
(tag body
(center
(tag (table id 'hnmain
border 0 cellpadding 0 cellspacing 0 width "85%"
bgcolor sand)
,@body)))))
(or= pagefns* nil)
(mac fulltop (user lid label title whence . body)
(w/uniq (gu gi gl gt gw)
`(with (,gu ,user ,gi ,lid ,gl ,label ,gt ,title ,gw ,whence)
(npage (+ (if ,gt (+ ,gt bar*) "") site-name*) ,gl
(if (check-procrast ,gu)
(do (pagetop 'full ,gi ,gl ,gt ,gu ,gw)
(hook 'page ,gu ,gl)
,@body)
(row (procrast-msg ,gu ,gw)))))))
(mac longpage (user t1 lid label title whence . body)
(w/uniq (gu gt gi)
`(with (,gu ,user ,gt ,t1 ,gi ,lid)
(fulltop ,gu ,gi ,label ,title ,whence
(trtd ,@body)
(trtd (longfoot ,gu (- (now) ,gt) ,whence))))))
(def longfoot (user elapsed whence)
(when (in whence "/l/teapots" "/l/teapot" "/l/418")
(vspace 10)
(center (tag (img src "/teapot.jpg"))))
(when (in whence "/l/chess")
(vspace 10)
(center
(chess-board user)))
(when (in whence "/l/place")
(vspace 10)
(center
(place-board user)))
(when (in whence "/l/templeos")
(terry))
(vspace 10)
(color-stripe (main-color user))
(br)
(center
(hook 'longfoot)
(w/bars
(link "Welcome" welcome-url*)
(link "Guidelines" "/guidelines.html")
(link "Bookmarklet" "/bookmarklet.html")
;(link "Feature Requests" "/item?id=230")
(link "Source" "https://github.com/tensorfork/tlarc")
(link "Contact" "mailto:@site-email*")
(link "Twitter" "https://twitter.com/@site-twitter*")
(link "Lists" "/lists"))
(br2)
(w/bars
(link "RSS (stories)" "/rss")
(link "RSS (comments)" "/rsscomments"))
(search-bar user elapsed whence)
(admin-bar user elapsed whence)))
(def search-bar (user elapsed whence)
(br2)
;(tag (form method "get" action "//search.laarc.io/")
; (inputs (q Search 17 nil 'plain)))
)
(defcache memusage 5
;(repeat 3 (seval!collect-garbage 'major))
(memory))
(def admin-bar (user elapsed whence)
(br2)
(when (admin user)
(w/bars
(pr whence)
(pr (len items*) "/" maxid* " loaded")
(pr (num (round (/ (memusage) 1000))) " kb")
(pr (len fns*) " fns")
(pr (num (* elapsed 1000)) " msec")
(link "settings" "/newsadmin")
(link "pages" "/pages")
(hook 'admin-bar user whence))
(br2))
(when (in whence "/l/dev" "/l/programming" "/l/react" "/l/reactnative")
(prn "<iframe src=\"https://open.spotify.com/embed/user/johanbrook/playlist/2mtlhuFVOFMn6Ho3JmrLc2\" width=\"300\" height=\"380\" frameborder=\"0\" allowtransparency=\"true\" allow=\"encrypted-media\"></iframe>")))
(def color-stripe (c)
(tag (table width "100%" cellspacing 0 cellpadding 1)
(tr (tdcolor c))))
(mac shortpage (user lid label title whence . body)
`(fulltop ,user ,lid ,label ,title ,whence
(trtd ,@body)))
(mac minipage (label . body)
`(npage (+ ,label bar* site-name*) ,label
(pagetop nil nil ,label)
(trtd ,@body)))
(def msgpage (user msg (o title) (o editurl) (o alert))
(minipage (or title "Message")
(awhen alert (pr it) (br2))
(spanclass admin
(center
(awhen editurl
(when (admin user)
(underlink "edit" it)
(br2)))
(if (len> msg 80)
(widtable 320 msg)
(pr msg))))
(br2)))
;(= (max-age* 'news.css) 86400) ; cache css in browser for 1 day
; turn off server caching via (= caching* 0) or won't see changes
; only need pre padding because of a bug in Mac Firefox
; Without setting the bottom margin of p tags to 0, 1- and n-para comments
; have different space at the bottom. This solution suggested by Devin.
; Really am using p tags wrong (as separators rather than wrappers) and the
; correct thing to do would be to wrap each para in <p></p>. Then whatever
; I set the bottom spacing to, it would be the same no matter how many paras
; in a comment. In this case by setting the bottom spacing of p to 0, I'm
; making it the same as no p, which is what the first para has.
; supplied by pb
;.vote { padding-left:2px; vertical-align:top; }
;.comment { margin-top:1ex; margin-bottom:1ex; color:black; }
;.vote IMG { border:0; margin: 3px 2px 3px 2px; }
;.reply { font-size:smaller; text-decoration:underline !important; }
(= votejs* "
function byId(id) {
return document.getElementById(id);
}
function vote(node) {
var v = node.id.split(/_/); // {'up', '123'}
var item = v[1];
// adjust score
var score = byId('score_' + item);
var newscore = parseInt(score.innerHTML || '1') + (v[0] == 'up' ? 1 : -1);
score.innerHTML = newscore + (newscore == 1 ? ' point' : ' points');
// hide arrows
byId('up_' + item).style.visibility = 'hidden';
byId('down_' + item).style.visibility = 'hidden';
// ping server
var ping = new Image();
ping.src = node.href;
return false; // cancel browser nav
}")
; Page top
(= sand (color 246 246 239) textgray (gray 130))
(def main-color (user)
(aif (and user (uvar user topcolor))
(hex>color it)
site-color*))
(def pagetop (switch lid label (o title) (o user) (o whence))
; (tr (tdcolor black (vspace 5)))
(tr (tdcolor (main-color user)
(tag (table border 0 cellpadding 0 cellspacing 0 width "100%"
style "padding:2px")
(tr (gen-logo)
(when (is switch 'full)
(tag (td style "line-height:12pt; height:10px;")
(spanclass pagetop
(tag b (link site-name* "/l/all"))
(hspace 10)
(toprow user label))))
(if (is switch 'full)
(tag (td style "text-align:right;padding-right:4px;")
(spanclass pagetop (topright user whence)))
(tag (td style "line-height:12pt; height:10px;")
(spanclass pagetop (tag b (link label "/l/all")))))))))
(map [_ user] pagefns*)
(spacerow 10))
(def gen-logo ()
(tag (td style "width:18px;padding-right:4px")
(tag (a href parent-url*)
(tag (img src logo-url* width 18 height 18
style "border:1px #@(hexrep border-color*) solid;")))))
(= toplabels* '(nil "welcome" "new" "threads" "comments" "discord"
"/l/show" "show" "/l/ask" "ask" "/l/place" "place" "*"))
; redefined later
(def toprow (user label)
(w/bars
(toplink "new" "/newest" label)
(when user
(toplink "threads" (threads-url user) label))
(toplink "comments" "/newcomments" label)
(toplink "discord" discord-url* label)
(hook 'toprow user label)
(link "tags" "/l")
(toplink "ask" "/l/ask" (if (is label "/l/ask") "ask" label))
(toplink "show" "/l/show" (if (is label "/l/show") "show" label))
(toplink "place" "/l/place" (if (is label "/l/place") "place" label))
(link "submit" "/submit")
(unless (mem label toplabels*)
(fontcolor white (pr:eschtml label)))))
(def toplink (name dest label)
(tag-if (is name label) (span class 'topsel)
(link name dest)))
(def topright (user whence (o showkarma t))
(when user
(userlink user user nil)
(when showkarma (pr " (@(karma user))"))
(pr " | "))
(if user
(rlinkf 'logout (req)
(when-umatch/r user req
(logout-user user)
whence))
(onlink "login"
(login-page 'both nil
(list (fn (u ip)
(ensure-news-user u)
(newslog ip u 'top-login))
whence)))))
(def noob (user)
(and user (< (days-since (uvar user created)) 1)))
; News-Specific Defop Variants
(mac defopt (name parm test msg . body)
`(defop ,name ,parm
(if (,test (get-user ,parm))
(do ,@body)
(login-page 'both (+ "Please log in" ,msg ".")
(list (fn (u ip) (ensure-news-user u))
(string ',name (reassemble-args ,parm)))))))
(mac defopg (name parm . body)
`(defopt ,name ,parm idfn "" ,@body))
(mac defope (name parm . body)
`(defopt ,name ,parm editor " as an editor" ,@body))
(mac defopa (name parm . body)
`(defopt ,name ,parm admin " as an administrator" ,@body))
(mac opexpand (definer name parms . body)
(w/uniq gr
`(,definer ,name ,gr
(with (user (get-user ,gr) ip (,gr 'ip))
(with ,(and parms (mappend [list _ (list 'arg gr (string _))]
parms))
(newslog ip user ',name ,@parms)
,@body)))))
(or= newsop-names* nil)
(mac newsop args
`(do (pushnew ',(car args) newsop-names*)
(opexpand defop ,@args)))
(mac newsopr args
`(do (pushnew ',(car args) newsop-names*)
(opexpand defopr ,@args)))
(mac adop (name parms . body)
(w/uniq g
`(opexpand defopa ,name ,parms
(let ,g (string ',name)
(shortpage user nil ,g ,g ,g
,@body)))))
(mac edop (name parms . body)
(w/uniq g
`(opexpand defope ,name ,parms
(let ,g (string ',name)
(shortpage user nil ,g ,g ,g
,@body)))))
; News Admin
(defopa newsadmin req
(let user (get-user req)
(newslog req!ip user 'newsadmin)
(newsadmin-page user)))
; Note that caching* is reset to val in source when restart server.
(def nad-fields ()
`((num caching ,caching* t t)
(bigtoks comment-kill ,comment-kill* t t)
(bigtoks comment-ignore ,comment-ignore* t t)
(bigtoks lightweights ,(sort < (keys lightweights*)) t t)))
; Need a util like vars-form for a collection of variables.
; Or could generalize vars-form to think of places (in the setf sense).
(def newsadmin-page (user)
(shortpage user nil nil "newsadmin" "newsadmin"
(vars-form user
(nad-fields)
(fn (name val)
(case name
caching (= caching* val)
comment-kill (todisk comment-kill* val)
comment-ignore (todisk comment-ignore* val)
lightweights (todisk lightweights* (memtable val))
))
(fn () (newsadmin-page user)))
(br2)
(aform (fn (req)
(with (user (get-user req) subject (arg req "id"))
(if (profile subject)
(do (killallby subject)
(submitted-page user subject))
(admin&newsadmin-page user))))
(single-input "" 'id 20 "kill all by"))
(br2)
(aform (fn (req)
(let user (get-user req)
(set-ip-ban user (arg req "ip") t)
(admin&newsadmin-page user)))
(single-input "" 'ip 20 "ban ip"))))
(defmemo suggested-title (url)
(or (fetch-title url) "unknown"))
(newsop suggest-title (url)
(pr:suggested-title url))
; Users
(newsop user (id)
(if (only.profile id)
(user-page user id)
(pr "No such user.")))
(= (static-header* 'user.json) "application/json")
(newsop user.json (id)
(aif (only.profile id)
(write-json (user>json it))
(pr "null")))
(= (static-header* 'auth.json) "application/json")
(newsop auth.json ()
(aif (get-auth)
(write-json (obj auth it))
(pr "null")))
(= (static-header* 'apple-app-site-association) "application/json")
(defop apple-app-site-association req
(write-json (obj webcredentials
(obj apps (list ;"B9452FEMTF.com.emilykolar.LaarcIOS"
))))
)
(def user>json (u)
(obj id u!id
created u!created
karma u!karma
about u!about
submitted u!submitted))
(defhook save-prof (u)
(firebase-set "v0/user/@u!id" (user>json u)))
(def user-page (user subject)
(let here (user-url subject)
(shortpage user nil nil (+ "Profile: " subject) here
(profile-form user subject)
(br2)
(hook 'user user subject))))
(def profile-form (user subject)
(let prof (profile subject)
(vars-form user
(user-fields user subject)
(fn (name val)
(when (and (is name 'ignore) val (no prof!ignore))
(log-ignore user subject 'profile))
(= (prof name) val))
(fn () (save-prof subject)
(user-page user subject)))))
(= topcolor-threshold* 250)
(def user-fields (user subject)
(withs (e (editor user)
a (admin user)
w (is user subject)
k (and w (> (karma user) topcolor-threshold*))
u (or a w)
m (or a (and (member user) w))
p (profile subject)
s subject)
(w/accum
`(string user ,subject t nil)
`(string name ,(p 'name) ,m ,m)
`(string created ,(text-age:user-age subject) t nil)
`(int auth ,(p 'auth) ,e ,a)
`(yesno member ,(p 'member) ,a ,a)
`(posint karma ,(p 'karma) t ,a)
`(num avg ,(p 'avg) ,a nil)
`(yesno ignore ,(p 'ignore) ,e ,e)
`(num weight ,(p 'weight) ,a ,a)
`(mdtext2 about ,(p 'about) t ,u)
`(string email ,(p 'email) ,u ,u)
`(string verified ,(p 'verified) ,a ,a)
(unless (blank p!email)
(if (isnt p!email p!verified)
`(string verify ,(verify-link s) ,u nil)
`(yesno notify ,(p 'notify) ,u ,u
"Be notified of replies by email?")))
`(yesno showdead ,(p 'showdead) ,u ,u)
`(yesno noprocrast ,(p 'noprocrast) ,u ,u)
`(string firstview ,(p 'firstview) ,a nil)
`(string lastview ,(p 'lastview) ,a nil)
`(posint maxvisit ,(p 'maxvisit) ,u ,u)
`(posint minaway ,(p 'minaway) ,u ,u)
`(sexpr keys ,(p 'keys) ,a ,a)
`(hexcol topcolor ,(or (p 'topcolor) (hexrep site-color*)) ,k ,k)
`(int delay ,(p 'delay) ,u ,u)
`(string password ,(resetpw-link s) ,u nil "")
`(string submissions ,(submissions-link s) t nil "")
`(string comments ,(comments-link s) t nil "")
`(string upvoted ,(+ (upvoted-link s) " (private)") ,u nil "")
`(string favorites ,(+ (favorites-link s) (if u " (shared)" "")) t nil "")
)))
(def verify-link (u (o label "verify email"))
(tostring (underlink label "/verify?u=@u")))
(def resetpw-link (u (o label "reset password"))
(tostring (underlink label "/resetpw?u=@u")))
(def submissions-link (u (o label "submissions"))
(tostring (underlink label (submitted-url u))))
(def comments-link (u (o label "comments"))
(tostring (underlink label (threads-url u))))
(def upvoted-link (u (o label "upvoted"))
(tostring (underlink label (upvoted-url u))))
(def favorites-link (u (o label "favorites"))
(tostring (underlink label (favorites-url u))))
(newsop welcome ()
(pr "Welcome to " site-name* ", " user "!"))
; Verify email
(defopg verify req
(with (user (get-user req)
subject (arg req "u"))
(verify-page user subject)))