forked from HCF3263827/ruqqus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
4736 lines (3319 loc) · 105 KB
/
schema.sql
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
--
-- PostgreSQL database dump
--
-- Dumped from database version 12.5
-- Dumped by pg_dump version 12.3
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: fuzzystrmatch; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS fuzzystrmatch WITH SCHEMA public;
--
-- Name: EXTENSION fuzzystrmatch; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION fuzzystrmatch IS 'determine similarities and distance between strings';
--
-- Name: pg_stat_statements; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS pg_stat_statements WITH SCHEMA public;
--
-- Name: EXTENSION pg_stat_statements; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION pg_stat_statements IS 'track execution statistics of all SQL statements executed';
--
-- Name: pg_trgm; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA public;
--
-- Name: EXTENSION pg_trgm; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION pg_trgm IS 'text similarity measurement and index searching based on trigrams';
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: boards; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.boards (
id integer NOT NULL,
name character varying(64),
is_banned boolean,
created_utc integer,
description character varying(1500),
description_html character varying(5000),
over_18 boolean,
creator_id integer,
has_banner boolean NOT NULL,
has_profile boolean NOT NULL,
ban_reason character varying(256),
color character varying(8),
downvotes_disabled boolean,
restricted_posting boolean,
hide_banner_data boolean,
profile_nonce integer NOT NULL,
banner_nonce integer NOT NULL,
is_private boolean,
color_nonce integer,
is_nsfl boolean,
rank_trending double precision,
stored_subscriber_count integer,
avg_score double precision,
all_opt_out boolean,
is_siegable boolean DEFAULT true,
last_yank_utc integer DEFAULT 0,
is_locked_category boolean DEFAULT false,
subcat_id integer,
secondary_color character(6) DEFAULT 'ffffff'::bpchar,
public_chat boolean DEFAULT false,
motd character varying(1000) DEFAULT ''::character varying,
disallowbots boolean DEFAULT false,
css_nonce integer DEFAULT 0,
css character varying(65536) DEFAULT ''::character varying
);
--
-- Name: age(public.boards); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.age(public.boards) RETURNS integer
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT CAST( EXTRACT( EPOCH FROM CURRENT_TIMESTAMP) AS int) - $1.created_utc
$_$;
--
-- Name: comments; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.comments (
id integer NOT NULL,
author_id integer,
created_utc integer NOT NULL,
parent_submission integer,
is_banned boolean,
parent_fullname character varying(255),
distinguish_level integer,
edited_utc integer,
deleted_utc integer NOT NULL,
is_approved integer NOT NULL,
approved_utc integer,
creation_ip character varying(64) NOT NULL,
score_disputed double precision,
score_hot double precision,
score_top integer,
level integer,
parent_comment_id integer,
title_id integer,
over_18 boolean,
is_op boolean,
is_offensive boolean,
is_nsfl boolean,
original_board_id integer,
upvotes integer,
downvotes integer,
is_bot boolean DEFAULT false,
gm_distinguish integer DEFAULT 0 NOT NULL,
is_pinned boolean DEFAULT false,
app_id integer,
creation_region character(2) DEFAULT NULL::bpchar,
purged_utc integer DEFAULT 0
);
--
-- Name: age(public.comments); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.age(public.comments) RETURNS integer
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT CAST( EXTRACT( EPOCH FROM CURRENT_TIMESTAMP) AS int) - $1.created_utc
$_$;
--
-- Name: submissions; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.submissions (
id integer NOT NULL,
author_id integer,
created_utc integer NOT NULL,
is_banned boolean,
over_18 boolean,
distinguish_level integer,
created_str character varying(255),
stickied boolean,
board_id integer,
deleted_utc integer NOT NULL,
domain_ref integer,
is_approved integer NOT NULL,
approved_utc integer,
original_board_id integer,
edited_utc integer,
creation_ip character varying(64) NOT NULL,
mod_approved integer,
is_image boolean,
has_thumb boolean,
accepted_utc integer,
post_public boolean,
score_hot double precision,
score_top integer,
score_activity double precision,
score_disputed double precision,
is_offensive boolean,
is_pinned boolean,
is_nsfl boolean,
repost_id integer,
score_best double precision,
upvotes integer,
downvotes integer,
gm_distinguish integer DEFAULT 0 NOT NULL,
app_id integer,
creation_region character(2) DEFAULT NULL::bpchar,
purged_utc integer DEFAULT 0,
is_bot boolean DEFAULT false
);
--
-- Name: age(public.submissions); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.age(public.submissions) RETURNS integer
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT CAST( EXTRACT( EPOCH FROM CURRENT_TIMESTAMP) AS int) - $1.created_utc
$_$;
--
-- Name: users; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.users (
id integer NOT NULL,
username character varying(255) NOT NULL,
email character varying(255),
passhash character varying(255) NOT NULL,
created_utc integer NOT NULL,
admin_level integer,
over_18 boolean,
creation_ip character varying(255),
hide_offensive boolean,
is_activated boolean,
bio character varying(300),
bio_html character varying(1000),
real_id character varying,
referred_by integer,
is_banned integer,
ban_reason character varying(128),
login_nonce integer,
title_id integer,
has_banner boolean NOT NULL,
has_profile boolean NOT NULL,
reserved character varying(256),
is_nsfw boolean NOT NULL,
tos_agreed_utc integer,
profile_nonce integer NOT NULL,
banner_nonce integer NOT NULL,
last_siege_utc integer,
mfa_secret character varying(32),
has_earned_darkmode boolean,
is_private boolean,
read_announcement_utc integer,
feed_nonce integer,
show_nsfl boolean,
karma integer,
comment_karma integer,
unban_utc integer,
is_deleted boolean,
delete_reason character varying(1000),
is_enrolled boolean,
filter_nsfw boolean,
is_nofollow boolean DEFAULT false,
coin_balance integer DEFAULT 0,
premium_expires_utc integer DEFAULT 0,
negative_balance_cents integer DEFAULT 0,
custom_filter_list character varying(1000) DEFAULT ''::character varying,
discord_id character varying(64),
last_yank_utc integer DEFAULT 0,
stored_karma integer DEFAULT 0,
stored_subscriber_count integer DEFAULT 0,
creation_region character(2) DEFAULT NULL::bpchar,
ban_evade integer DEFAULT 0,
profile_upload_ip character varying(255),
banner_upload_ip character varying(255),
profile_upload_region character(2),
banner_upload_region character(2),
name_last_changed_utc integer,
banner_set_utc integer DEFAULT 0,
profile_set_utc integer DEFAULT 0,
original_username character varying(255),
name_changed_utc integer DEFAULT 0,
hide_bot boolean DEFAULT false,
auto_join_chat boolean DEFAULT true,
last_mfa character(6),
defaulttime character varying(8) DEFAULT 'all'::character varying,
defaultsorting character varying(8) DEFAULT 'hot'::character varying,
stored_follower_count integer DEFAULT 0,
color character(6) DEFAULT '805ad5'::bpchar,
secondary_color character(6) DEFAULT 'ffff00'::bpchar,
signature character varying(280),
signature_html character varying(512)
);
--
-- Name: age(public.users); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.age(public.users) RETURNS integer
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT CAST( EXTRACT( EPOCH FROM CURRENT_TIMESTAMP) AS int) - $1.created_utc
$_$;
--
-- Name: avg_score_computed(public.boards); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.avg_score_computed(public.boards) RETURNS numeric
LANGUAGE sql IMMUTABLE STRICT
AS $_$
select coalesce (
(select avg(score_top) from submissions
where original_board_id=$1.id
and score_top>0)
,
1
)
$_$;
--
-- Name: board_id(public.comments); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.board_id(public.comments) RETURNS integer
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT submissions.board_id
FROM submissions
WHERE submissions.id=$1.parent_submission
$_$;
--
-- Name: reports; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.reports (
id integer NOT NULL,
post_id integer,
user_id integer,
created_utc integer
);
--
-- Name: board_id(public.reports); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.board_id(public.reports) RETURNS integer
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT submissions.board_id
FROM submissions
WHERE submissions.id=$1.post_id
$_$;
--
-- Name: comment_count(public.submissions); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.comment_count(public.submissions) RETURNS bigint
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT COUNT(*)
FROM comments
WHERE is_banned=false
AND deleted_utc=0
AND parent_submission = $1.id
$_$;
--
-- Name: comment_energy(public.users); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.comment_energy(public.users) RETURNS bigint
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT COALESCE(
(
SELECT SUM(comments.score_top)
FROM comments
WHERE comments.author_id=$1.id
AND comments.is_banned=false
and comments.parent_submission is not null
),
0
)
$_$;
--
-- Name: notifications; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.notifications (
id integer NOT NULL,
user_id integer,
comment_id integer,
read boolean NOT NULL,
submission_id integer
);
--
-- Name: created_utc(public.notifications); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.created_utc(public.notifications) RETURNS integer
LANGUAGE sql IMMUTABLE STRICT
AS $_$
select created_utc from comments
where comments.id=$1.comment_id
$_$;
--
-- Name: downs(public.comments); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.downs(public.comments) RETURNS bigint
LANGUAGE sql
AS $_$
select (
(
SELECT count(*)
from (
select * from commentvotes
where comment_id=$1.id
and vote_type=-1
and user_id not in
(
select user_id
from bans
where board_id=$1.original_board_id
and is_active=true
)
) as v1
join (select * from users where users.is_banned=0 or users.unban_utc>0
) as u0
on u0.id=v1.user_id
)-(
SELECT count(distinct v1.id)
from (
select * from commentvotes
where comment_id=$1.id
and vote_type=-1
and user_id not in
(
select user_id
from bans
where board_id=$1.original_board_id
and is_active=true
)
) as v1
join (select * from users where is_banned=0 or users.unban_utc>0) as u1
on u1.id=v1.user_id
join (select * from alts) as a
on (a.user1=v1.user_id or a.user2=v1.user_id)
join (
select * from commentvotes
where comment_id=$1.id
and vote_type=-1
and user_id not in
(
select user_id
from bans
where board_id=$1.original_board_id
and is_active=true
)
) as v2
on ((a.user1=v2.user_id or a.user2=v2.user_id) and v2.id != v1.id)
join (select * from users where is_banned=0 or users.unban_utc>0) as u2
on u2.id=v2.user_id
where v1.id is not null
and v2.id is not null
))
$_$;
--
-- Name: downs(public.submissions); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.downs(public.submissions) RETURNS bigint
LANGUAGE sql
AS $_$
select (
(
SELECT count(*)
from (
select * from votes
where submission_id=$1.id
and vote_type=-1
and user_id not in
(
select user_id
from bans
where board_id=$1.board_id
and is_active=true
)
) as v1
join (select * from users where users.is_banned=0 or users.unban_utc>0) as u0
on u0.id=v1.user_id
)-(
SELECT count(distinct v1.id)
from (
select * from votes
where submission_id=$1.id
and vote_type=-1
and user_id not in
(
select user_id
from bans
where board_id=$1.board_id
and is_active=true
)
) as v1
join (select * from users where is_banned=0 or users.unban_utc>0) as u1
on u1.id=v1.user_id
join (select * from alts) as a
on (a.user1=v1.user_id or a.user2=v1.user_id)
join (
select * from votes
where submission_id=$1.id
and vote_type=-1
and user_id not in
(
select user_id
from bans
where board_id=$1.board_id
and is_active=true
)
) as v2
on ((a.user1=v2.user_id or a.user2=v2.user_id) and v2.id != v1.id)
join (select * from users where is_banned=0 or users.unban_utc>0) as u2
on u2.id=v2.user_id
where v1.id is not null
and v2.id is not null
))
$_$;
--
-- Name: energy(public.users); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.energy(public.users) RETURNS bigint
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT COALESCE(
(
SELECT SUM(submissions.score_top)
FROM submissions
WHERE submissions.author_id=$1.id
AND submissions.is_banned=false
),
0
)
$_$;
--
-- Name: flag_count(public.comments); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.flag_count(public.comments) RETURNS bigint
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT COUNT(*)
FROM commentflags
JOIN users ON commentflags.user_id=users.id
WHERE comment_id=$1.id
AND users.is_banned=0
$_$;
--
-- Name: flag_count(public.submissions); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.flag_count(public.submissions) RETURNS bigint
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT COUNT(*)
FROM flags
JOIN users ON flags.user_id=users.id
WHERE post_id=$1.id
AND users.is_banned=0
$_$;
--
-- Name: follower_count(public.users); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.follower_count(public.users) RETURNS bigint
LANGUAGE sql IMMUTABLE STRICT
AS $_$
select (
(select count(*)
from follows
left join users
on follows.user_id=users.id
where follows.target_id=$1.id
and (users.is_banned=0 or users.created_utc>0)
and users.is_deleted=false
)-(
select count(distinct f1.id)
from
(
select *
from follows
where target_id=$1.id
) as f1
join (select * from users where is_banned=0 or unban_utc>0) as u1
on u1.id=f1.user_id
join (select * from alts) as a
on (a.user1=f1.user_id or a.user2=f1.user_id)
join (
select *
from follows
where target_id=$1.id
) as f2
on ((a.user1=f2.user_id or a.user2=f2.user_id) and f2.id != f1.id)
join (select * from users where is_banned=0 or unban_utc>0) as u2
on u2.id=f2.user_id
where f1.id is not null
and f2.id is not null
)
)
$_$;
--
-- Name: is_banned(public.notifications); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.is_banned(public.notifications) RETURNS boolean
LANGUAGE sql IMMUTABLE STRICT
AS $_$
select is_banned from comments
where comments.id=$1.comment_id
$_$;
--
-- Name: is_deleted(public.notifications); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.is_deleted(public.notifications) RETURNS boolean
LANGUAGE sql IMMUTABLE STRICT
AS $_$
select is_deleted from comments
where comments.id=$1.comment_id
$_$;
--
-- Name: is_public(public.comments); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.is_public(public.comments) RETURNS boolean
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT submissions.is_public
FROM submissions
WHERE submissions.id=$1.parent_submission
$_$;
--
-- Name: is_public(public.submissions); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.is_public(public.submissions) RETURNS boolean
LANGUAGE sql IMMUTABLE STRICT
AS $_$
select
case
when $1.post_public=true
then true
when (select (is_private)
from boards
where id=$1.board_id
)=true
then false
else
true
end
$_$;
--
-- Name: mod_count(public.users); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.mod_count(public.users) RETURNS bigint
LANGUAGE sql IMMUTABLE STRICT
AS $_$select count(*) from mods where accepted=true and invite_rescinded=false and user_id=$1.id;$_$;
--
-- Name: rank_activity(public.submissions); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.rank_activity(public.submissions) RETURNS double precision
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT 1000000.0*CAST($1.comment_count AS float)/((CAST(($1.age+5000) AS FLOAT)/100.0)^(1.35))
$_$;
--
-- Name: rank_best(public.submissions); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.rank_best(public.submissions) RETURNS double precision
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT 10000000.0*CAST(($1.upvotes - $1.downvotes + 1) AS float)/((CAST(($1.age+3600) AS FLOAT)*cast((select boards.subscriber_count from boards where boards.id=$1.board_id)+10000 as float)/1000.0)^(1.35))
$_$;
--
-- Name: rank_fiery(public.comments); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.rank_fiery(public.comments) RETURNS double precision
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT SQRT(CAST(($1.upvotes * $1.downvotes) AS float))/((CAST(($1.age+100000) AS FLOAT)/6.0)^(1.5))
$_$;
--
-- Name: rank_fiery(public.submissions); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.rank_fiery(public.submissions) RETURNS double precision
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT 1000000.0*SQRT(CAST(($1.upvotes * $1.downvotes) AS float))/((CAST(($1.age+5000) AS FLOAT)/100.0)^(1.35))
$_$;
--
-- Name: rank_hot(public.comments); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.rank_hot(public.comments) RETURNS double precision
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT CAST(($1.upvotes - $1.downvotes) AS float)/((CAST(($1.age+100000) AS FLOAT)/6.0)^(1.5))
$_$;
--
-- Name: rank_hot(public.submissions); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.rank_hot(public.submissions) RETURNS double precision
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT 1000000.0*CAST(($1.upvotes - $1.downvotes) AS float)/((CAST(($1.age+5000) AS FLOAT)/100.0)^(1.5))
$_$;
--
-- Name: recent_subscriptions(public.boards); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.recent_subscriptions(public.boards) RETURNS bigint
LANGUAGE sql IMMUTABLE STRICT
AS $_$
select count(*)
from subscriptions
left join users
on subscriptions.user_id=users.id
where subscriptions.board_id=$1.id
and subscriptions.is_active=true
and subscriptions.created_utc > CAST( EXTRACT( EPOCH FROM CURRENT_TIMESTAMP) AS int) - 60*60*24
and users.is_banned=0
$_$;
--
-- Name: referral_count(public.users); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.referral_count(public.users) RETURNS bigint
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT COUNT(*)
FROM USERS
WHERE users.is_banned=0
AND users.referred_by=$1.id
$_$;
--
-- Name: report_count(public.submissions); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.report_count(public.submissions) RETURNS bigint
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT COUNT(*)
FROM reports
JOIN users ON reports.user_id=users.id
WHERE post_id=$1.id
AND users.is_banned=0
and reports.created_utc >= $1.edited_utc
$_$;
--
-- Name: score(public.comments); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.score(public.comments) RETURNS integer
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT ($1.upvotes - $1.downvotes)
$_$;
--
-- Name: score(public.submissions); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.score(public.submissions) RETURNS integer
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT ($1.upvotes - $1.downvotes)
$_$;
--
-- Name: similar_count(public.comments); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.similar_count(public.comments) RETURNS bigint
LANGUAGE sql
AS $_$ select count(*) from comments where author_id=$1.id and similarity(comments.body, $1.body) > 0.5 $_$;
--
-- Name: images; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.images (
id integer NOT NULL,
state character varying(8),
text character varying(255),
number integer
);
--
-- Name: splash(text); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.splash(text) RETURNS SETOF public.images
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT *
FROM images
WHERE state=$1
ORDER BY random()
LIMIT 1
$_$;
--
-- Name: subscriber_count(public.boards); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.subscriber_count(public.boards) RETURNS bigint
LANGUAGE sql IMMUTABLE STRICT
AS $_$
select
case
when $1.is_private=false
then
(
(
select count(*)
from subscriptions
left join users
on subscriptions.user_id=users.id
where subscriptions.board_id=$1.id
and subscriptions.is_active=true
and users.is_deleted=false and (users.is_banned=0 or users.unban_utc>0)
)-(
select count(distinct s1.id)
from
(
select *
from subscriptions
where board_id=$1.id
and is_active=true
) as s1
join (select * from users where is_banned=0 or unban_utc>0) as u1
on u1.id=s1.user_id
join (select * from alts) as a
on (a.user1=s1.user_id or a.user2=s1.user_id)
join (
select *
from subscriptions
where board_id=$1.id
and is_active=true
) as s2
on ((a.user1=s2.user_id or a.user2=s2.user_id) and s2.id != s1.id)
join (select * from users where is_banned=0 or unban_utc>0) as u2
on u2.id=s2.user_id
where s1.id is not null
and s2.id is not null
)
)
when $1.is_private=true
then
(
(
select count(*)
from subscriptions
left join users
on subscriptions.user_id=users.id
left join (
select * from contributors
where contributors.board_id=$1.id
)as contribs
on contribs.user_id=users.id
left join (
select * from mods
where mods.board_id=$1.id
and accepted=true
)as m
on m.user_id=users.id
where subscriptions.board_id=$1.id
and subscriptions.is_active=true
and users.is_deleted=false and (users.is_banned=0 or users.unban_utc>0)
and (contribs.user_id is not null or m.id is not null)
)
)
end
$_$;
--
-- Name: trending_rank(public.boards); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.trending_rank(public.boards) RETURNS double precision
LANGUAGE sql IMMUTABLE STRICT
AS $_$
select
case
when $1.subscriber_count<=10 then 0
when $1.age < 60*60*24*5 then 0
when $1.recent_subscriptions<=5 then 0
when $1.subscriber_count>=9 then ((cast($1.subscriber_count as float))^(1/3) + cast($1.recent_subscriptions as float)) / cast($1.subscriber_count + 10000 as float)
end
$_$;
--
-- Name: ups(public.comments); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.ups(public.comments) RETURNS bigint
LANGUAGE sql
AS $_$
select (
(
SELECT count(*)
from (
select * from commentvotes
where comment_id=$1.id
and vote_type=1