-
Notifications
You must be signed in to change notification settings - Fork 0
/
news.arc
2614 lines (2102 loc) · 84.2 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
; things to customize
(declare 'atstrings t)
(= this-site* "My Forum"
site-url* "http://news.yourdomain.com/"
parent-url* "http://www.yourdomain.com"
favicon-url* ""
site-desc* "What this site is about." ; for rss feed
site-color* (color 180 180 180)
border-color* (color 180 180 180)
prefer-url* t)
; these settings might improve performance when you need it
;(= static-max-age* 7200) ; browsers can cache static files for 7200 sec
;(declare 'direct-calls t) ; you promise not to redefine fns as tables
;(declare 'explicit-flush t) ; you take responsibility for flushing output
; (all existing news code already does)
; 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
votes nil ; for now just recent, elts each (time id by sitename dir)
karma 1
avg nil
weight .5
ignore nil
email nil
about nil
showdead 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* (+ srvdir* "news/")
storydir* (+ srvdir* "news/story/")
profdir* (+ srvdir* "news/profile/")
votedir* (+ srvdir* "news/vote/"))
(= votes* (table) profs* (table))
(= initload-users* nil)
(def nsv ((o port 8080))
(map ensure-dir (list srvdir* newsdir* storydir* votedir* profdir*))
(unless stories* (load-items))
(if (and initload-users* (empty profs*)) (load-users))
(asv 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 init-user (u)
(= (votes* u) (table)
(profs* u) (inst 'profile 'id u))
(save-votes u)
(save-prof u)
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) (temstore 'profile (profs* u) (+ profdir* 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))
(= 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 ()
(system (+ "rm " 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))
(= stemmable-sites* (table))
(def canonical-url (url)
(if (stemmable-sites* (sitename url))
(cut url 0 (pos #\? url))
url))
(def new-item-id ()
(evtil (++ maxid*) [~file-exists (+ storydir* _)]))
(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) (errsafe:read id) id)))
(def ok-id (id)
(and (exact id) (<= 1 id maxid*)))
(def live (i) (nor i!dead i!deleted))
(def save-item (i) (temstore 'item i (+ storydir* i!id)))
(def kill (i how)
(unless i!dead
(log-kill i how)
(wipe (comment-cache* i!id))
(set i!dead)
(save-item i)))
(= 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
(down ,g maxid* 0
(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* 1
nourl-factor* .4 lightweight-factor* .3 )
(def frontpage-rank (s (o scorefn realscore) (o gravity gravity*))
(* (/ (let base (- (scorefn s) 1)
(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) (- 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 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 gen-topstories ()
(= ranked-stories* (rank-stories 180 1000 (memo frontpage-rank))))
(def save-topstories ()
(writefile (map !id (firstn 180 ranked-stories*))
(+ newsdir* "topstories")))
(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* 0
(let i (item id)
(if (or (and stop (stop i)) (and n (<= n 0)))
(throw))
(when (test i)
(a i)
(if n (-- n))))))))
(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 topstories (user n (o threshold front-threshold*))
(retrieve n
[and (>= (realscore _) threshold) (cansee user _)]
ranked-stories*))
(= max-delay* 10)
(def cansee (user i)
(if i!deleted (admin user)
i!dead (or (author user i) (seesdead user))
(delayed i) (author user i)
t))
(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 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* "grayarrow.gif" down-url* "graydown.gif" logo-url* "arc.png")
(defopr favicon.ico req favicon-url*)
(def gen-css-url ()
(prn "<link rel=\"stylesheet\" type=\"text/css\" href=\"news.css\">"))
(mac npage (title . body)
`(tag html
(tag head
(gen-css-url)
(prn "<link rel=\"shortcut icon\" href=\"" favicon-url* "\">")
(prn "<meta name=\"viewport\" content=\"width=device-width\">")
(tag script (pr votejs*))
(tag title (pr ,title)))
(tag body
(center
(tag (table border 0 cellpadding 0 cellspacing 0 width "85%"
bgcolor sand)
,@body)))))
(= 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 (+ this-site* (if ,gt (+ bar* ,gt) ""))
(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 (vspace 10)
(color-stripe (main-color ,gu))
(br)
(center
(hook 'longfoot)
(admin-bar ,gu (- (msec) ,gt) ,whence)))))))
(def admin-bar (user elapsed whence)
(when (admin user)
(br2)
(w/bars
(pr (len items*) "/" maxid* " loaded")
(pr (round (/ (memory) 1000000)) " mb")
(pr elapsed " msec")
(link "settings" "newsadmin")
(hook 'admin-bar user whence))))
(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 (+ this-site* bar* ,label)
(pagetop nil nil ,label)
(trtd ,@body)))
(def msgpage (user msg (o title))
(minipage (or title "Message")
(spanclass admin
(center (if (len> msg 80)
(widtable 500 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
(defop news.css req
(pr "
body { font-family:Verdana; font-size:10pt; color:#828282; }
td { font-family:Verdana; font-size:10pt; color:#828282; }
.admin td { font-family:Verdana; font-size:8.5pt; color:#000000; }
.subtext td { font-family:Verdana; font-size: 7pt; color:#828282; }
input { font-family:Courier; font-size:10pt; color:#000000; }
input[type=\"submit\"] { font-family:Verdana; }
textarea { font-family:Courier; font-size:10pt; color:#000000; }
a:link { color:#000000; text-decoration:none; }
a:visited { color:#828282; text-decoration:none; }
.default { font-family:Verdana; font-size: 10pt; color:#828282; }
.admin { font-family:Verdana; font-size:8.5pt; color:#000000; }
.title { font-family:Verdana; font-size: 10pt; color:#828282; }
.adtitle { font-family:Verdana; font-size: 9pt; color:#828282; }
.subtext { font-family:Verdana; font-size: 7pt; color:#828282; }
.yclinks { font-family:Verdana; font-size: 8pt; color:#828282; }
.pagetop { font-family:Verdana; font-size: 10pt; color:#222222; }
.comhead { font-family:Verdana; font-size: 8pt; color:#828282; }
.comment { font-family:Verdana; font-size: 9pt; }
.dead { font-family:Verdana; font-size: 9pt; color:#dddddd; }
.comment a:link, .comment a:visited { text-decoration:underline;}
.dead a:link, .dead a:visited { color:#dddddd; }
.pagetop a:visited { color:#000000;}
.topsel a:link, .topsel a:visited { color:#ffffff; }
.subtext a:link, .subtext a:visited { color:#828282; }
.subtext a:hover { text-decoration:underline; }
.comhead a:link, .subtext a:visited { color:#828282; }
.comhead a:hover { text-decoration:underline; }
.default p { margin-top: 8px; margin-bottom: 0px; }
.pagebreak {page-break-before:always}
pre { overflow: auto; padding: 2px; max-width:600px; }
pre:hover {overflow:auto}
@@media (max-width: 517px) {
body { margin:0; }
body > center > table { width:100%; }
body > center > table > tbody > tr:nth-child(1) > td > table td .pagetop b { display:block }
body > center > table > tbody > tr:nth-child(1) > td > table td .pagetop img { display:none }
}"))
; 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) + (v[0] == 'up' ? 1 : -1);
score.innerHTML = newscore + (newscore == 1 ? ' point' : ' points');
// hide arrows
byId('up_' + item).style.visibility = 'hidden';
try { byId('down_' + item).style.visibility = 'hidden'; }
catch(err) {} // ignore
// 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 this-site* "news"))
(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 (prbold label))))))))
(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" "leaders" "*"))
(= welcome-url* "welcome")
(def toprow (user label)
(w/bars
(when (noob user)
(toplink "welcome" welcome-url* label))
(toplink "new" "newest" label)
(when user
(toplink "threads" (threads-url user) label))
(toplink "comments" "newcomments" label)
(toplink "leaders" "leaders" label)
(hook 'toprow user label)
(link "submit")
(unless (mem label toplabels*)
(fontcolor white (pr 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 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 (+ "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)))))
(= newsop-names* nil)
(mac newsop args
`(do (pushnew ',(car args) newsop-names*)
(opexpand defop ,@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"))))
; Users
(newsop user (id)
(if (only.profile id)
(user-page user id)
(pr "No such user.")))
(def user-page (user subject)
(let here (user-url subject)
(shortpage user nil nil (+ "Profile: " subject) here
(profile-form user subject)
(br2)
(when (some astory:item (uvar subject submitted))
(underlink "submissions" (submitted-url subject)))
(when (some acomment:item (uvar subject submitted))
(sp)
(underlink "comments" (threads-url subject)))
(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))
`((string user ,subject t nil)
(string name ,(p 'name) ,m ,m)
(string created ,(text-age:user-age subject) t nil)
(string password ,(resetpw-link) ,w nil)
(string saved ,(saved-link user subject) ,u 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)
(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))))
(def saved-link (user subject)
(when (or (admin user) (is user subject))
(let n (if (len> (votes subject) 500)
"many"
(len (voted-stories user subject)))
(if (is n 0)
""
(tostring (underlink n (saved-url subject)))))))
(def resetpw-link ()
(tostring (underlink "reset password" "resetpw")))
(newsop welcome ()
(pr "Welcome to " this-site* ", " user "!"))
; Main Operators
; remember to set caching to 0 when testing non-logged-in
(= caching* 1 perpage* 30 threads-perpage* 10 maxend* 210)
; Limiting that newscache can't take any arguments except the user.
; To allow other arguments, would have to turn the cache from a single
; stored value to a hash table whose keys were lists of arguments.
(mac newscache (name user time . body)
(w/uniq gc
`(let ,gc (cache (fn () (* caching* ,time))
(fn () (tostring (let ,user nil ,@body))))
(def ,name (,user)
(if ,user
(do ,@body)
(pr (,gc)))))))
(newsop news () (newspage user))
(newsop || () (newspage user))
;(newsop index.html () (newspage user))
(newscache newspage user 90
(listpage user (msec) (topstories user maxend*) nil nil "news"))
(def listpage (user t1 items label title (o url label) (o number t))
(hook 'listpage user)
(longpage user t1 nil label title url
(display-items user items label title url 0 perpage* number)))
(newsop newest () (newestpage user))
; Note: dead/deleted items will persist for the remaining life of the
; cached page. If this were a prob, could make deletion clear caches.
(newscache newestpage user 40
(listpage user (msec) (newstories user maxend*) "new" "New Links" "newest"))
(def newstories (user n)
(retrieve n [cansee user _] stories*))
(newsop best () (bestpage user))
(newscache bestpage user 1000
(listpage user (msec) (beststories user maxend*) "best" "Top Links"))
; As no of stories gets huge, could test visibility in fn sent to best.
(def beststories (user n)
(bestn n (compare > realscore) (visible user stories*)))
(newsop noobstories () (noobspage user stories*))
(newsop noobcomments () (noobspage user comments*))
(def noobspage (user source)
(listpage user (msec) (noobs user maxend* source) "noobs" "New Accounts"))
(def noobs (user n source)
(retrieve n [and (cansee user _) (bynoob _)] source))
(def bynoob (i)
(< (- (user-age i!by) (item-age i)) 2880))
(newsop bestcomments () (bestcpage user))
(newscache bestcpage user 1000
(listpage user (msec) (bestcomments user maxend*)
"best comments" "Best Comments" "bestcomments" nil))
(def bestcomments (user n)
(bestn n (compare > realscore) (visible user comments*)))
(newsop lists ()
(longpage user (msec) nil "lists" "Lists" "lists"
(sptab
(row (link "best") "Highest voted recent links.")
(row (link "active") "Most active current discussions.")
(row (link "bestcomments") "Highest voted recent comments.")
(row (link "noobstories") "Submissions from new accounts.")
(row (link "noobcomments") "Comments from new accounts.")
(when (admin user)
(map row:link
'(optimes topips flagged killed badguys badlogins goodlogins)))
(hook 'listspage user))))
(def saved-url (user) (+ "saved?id=" user))
(newsop saved (id)
(if (only.profile id)
(savedpage user id)
(pr "No such user.")))
(def savedpage (user subject)
(if (or (is user subject) (admin user))
(listpage user (msec)
(sort (compare < item-age) (voted-stories user subject))
"saved" "Saved Links" (saved-url subject))
(pr "Can't display that.")))
(def voted-stories (user subject)
(keep [and (astory _) (cansee user _)]
(map item (keys:votes subject))))
; Story Display
(def display-items (user items label title whence
(o start 0) (o end perpage*) (o number))
(zerotable
(let n start
(each i (cut items start end)
(display-item (and number (++ n)) i user whence t)
(spacerow (if (acomment i) 15 5))))
(when end
(let newend (+ end perpage*)
(when (and (<= newend maxend*) (< end (len items)))
(spacerow 10)
(tr (tag (td colspan (if number 2 1)))
(tag (td class 'title)
(morelink display-items
items label title end newend number))))))))
; This code is inevitably complex because the More fn needs to know
; its own fnid in order to supply a correct whence arg to stuff on
; the page it generates, like logout and delete links.
(def morelink (f items label title . args)
(tag (a href
(url-for
(afnid (fn (req)
(prrn)
(with (url (url-for it) ; it bound by afnid
user (get-user req))
(newslog req!ip user 'more label)
(longpage user (msec) nil label title url
(apply f user items label title url args))))))
rel 'nofollow)
(pr "More")))
(def display-story (i s user whence)
(when (or (cansee user s) (s 'kids))
(tr (display-item-number i)
(td (votelinks s user whence))
(titleline s s!url user whence))
(tr (tag (td colspan (if i 2 1)))
(tag (td class 'subtext)
(hook 'itemline s user)
(itemline s user)
(when (in s!type 'story 'poll) (commentlink s user))
(editlink s user)
(when (apoll s) (addoptlink s user))
(unless i (flaglink s user whence))
(killlink s user whence)
(blastlink s user whence)
(blastlink s user whence t)
(deletelink s user whence)))))
(def display-item-number (i)
(when i (tag (td align 'right valign 'top class 'title)
(pr i "."))))
(= follow-threshold* 5)
(def titleline (s url user whence)
(tag (td class 'title)
(if (cansee user s)
(do (deadmark s user)
(titlelink s url user)
(pdflink url)
(awhen (sitename url)
(spanclass comhead
(pr " (" )
(if (admin user)
(w/rlink (do (set-site-ban user
it
(case (car (banned-sites* it))
nil 'ignore
ignore 'kill
kill nil))