remotes/1693045480750635534/spooky-22
Aevann1 2021-07-26 18:45:06 +02:00
parent 3348f604c8
commit f35ad5bba8
4 changed files with 226 additions and 139 deletions

View File

@ -77,9 +77,6 @@ class Comment(Base, Age_times, Scores, Stndrd, Fuzzing):
# These are virtual properties handled as postgres functions server-side
# There is no difference to SQLAlchemy, but they cannot be written to
ups = deferred(Column(Integer, server_default=FetchedValue()))
downs = deferred(Column(Integer, server_default=FetchedValue()))
score = deferred(Column(Integer, server_default=FetchedValue()))
def __init__(self, *args, **kwargs):

View File

@ -29,27 +29,6 @@ def base36decode(number):
abort(400)
def base_encode(number, base):
alphabet = '0123456789abcdefghijklmnopqrstuvwxyz'[0:base]
output = ''
sign = ''
if number < 0:
sign = '-'
number = -number
if 0 <= number < len(alphabet):
return sign + alphabet[number]
while number != 0:
number, i = divmod(number, len(alphabet))
output = alphabet[i] + output
return sign + output
#got this one from stackoverflow
def hex2bin(hexstr):
value = int(hexstr, 16)
bindigits = []

View File

@ -117,8 +117,8 @@ def api_vote_post(post_id, x, v):
g.db.flush()
post.upvotes = post.ups
post.downvotes = post.downs
post.upvotes = g.db.query(Vote).filter_by(comment_id=comment.id, vote_type=1).count()
post.downvotes = g.db.query(Vote).filter_by(comment_id=comment.id, vote_type=-1).count()
g.db.add(post)
return "", 204
@ -169,7 +169,7 @@ def api_vote_comment(comment_id, x, v):
except:
return jsonify({"error":"Vote already exists."}), 422
comment.upvotes = comment.ups
comment.downvotes = comment.downs
comment.upvotes = g.db.query(CommentVote).filter_by(comment_id=comment.id, vote_type=1).count()
comment.downvotes = g.db.query(CommentVote).filter_by(comment_id=comment.id, vote_type=-1).count()
g.db.add(comment)
return make_response(""), 204

View File

@ -453,8 +453,10 @@ ALTER TABLE public.comments OWNER TO postgres;
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
AS $_$
SELECT CAST( EXTRACT( EPOCH FROM CURRENT_TIMESTAMP) AS int) - $1.created_utc
$_$;
@ -516,8 +518,10 @@ ALTER TABLE public.submissions OWNER TO postgres;
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
AS $_$
SELECT CAST( EXTRACT( EPOCH FROM CURRENT_TIMESTAMP) AS int) - $1.created_utc
$_$;
@ -593,8 +597,10 @@ ALTER TABLE public.users OWNER TO postgres;
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
AS $_$
SELECT CAST( EXTRACT( EPOCH FROM CURRENT_TIMESTAMP) AS int) - $1.created_utc
$_$;
@ -606,10 +612,14 @@ ALTER FUNCTION public.age(public.users) OWNER TO postgres;
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
AS $_$
SELECT submissions.board_id
FROM submissions
WHERE submissions.id=$1.parent_submission
$_$;
@ -635,10 +645,14 @@ ALTER TABLE public.reports OWNER TO postgres;
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
AS $_$
SELECT submissions.board_id
FROM submissions
WHERE submissions.id=$1.post_id
$_$;
@ -675,17 +689,28 @@ ALTER FUNCTION public.comment_count(public.submissions) OWNER TO postgres;
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
)
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
)
$_$;
@ -715,9 +740,12 @@ ALTER TABLE public.notifications OWNER TO postgres;
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
AS $_$
select created_utc from comments
where comments.id=$1.comment_id
$_$;
@ -889,16 +917,26 @@ ALTER FUNCTION public.downs(public.submissions) OWNER TO postgres;
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
)
AS $_$
SELECT COALESCE(
(
SELECT SUM(submissions.score_top)
FROM submissions
WHERE submissions.author_id=$1.id
AND submissions.is_banned=false
),
0
)
$_$;
@ -910,12 +948,18 @@ ALTER FUNCTION public.energy(public.users) OWNER TO postgres;
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
AS $_$
SELECT COUNT(*)
FROM commentflags
JOIN users ON commentflags.user_id=users.id
WHERE comment_id=$1.id
AND users.is_banned=0
$_$;
@ -927,12 +971,18 @@ ALTER FUNCTION public.flag_count(public.comments) OWNER TO postgres;
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
AS $_$
SELECT COUNT(*)
FROM flags
JOIN users ON flags.user_id=users.id
WHERE post_id=$1.id
AND users.is_banned=0
$_$;
@ -944,9 +994,12 @@ ALTER FUNCTION public.flag_count(public.submissions) OWNER TO postgres;
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
AS $_$
select is_banned from comments
where comments.id=$1.comment_id
$_$;
@ -958,9 +1011,12 @@ ALTER FUNCTION public.is_banned(public.notifications) OWNER TO postgres;
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
AS $_$
select is_deleted from comments
where comments.id=$1.comment_id
$_$;
@ -983,11 +1039,16 @@ ALTER FUNCTION public.mod_count(public.users) OWNER TO postgres;
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
AS $_$
SELECT COUNT(*)
FROM USERS
WHERE users.is_banned=0
AND users.referred_by=$1.id
$_$;
@ -999,13 +1060,20 @@ ALTER FUNCTION public.referral_count(public.users) OWNER TO postgres;
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
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
$_$;
@ -1017,8 +1085,10 @@ ALTER FUNCTION public.report_count(public.submissions) OWNER TO postgres;
CREATE FUNCTION public.score(public.comments) RETURNS integer
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT ($1.upvotes - $1.downvotes)
AS $_$
SELECT ($1.upvotes - $1.downvotes)
$_$;
@ -1030,8 +1100,10 @@ ALTER FUNCTION public.score(public.comments) OWNER TO postgres;
CREATE FUNCTION public.score(public.submissions) RETURNS integer
LANGUAGE sql IMMUTABLE STRICT
AS $_$
SELECT ($1.upvotes - $1.downvotes)
AS $_$
SELECT ($1.upvotes - $1.downvotes)
$_$;
@ -1069,12 +1141,18 @@ ALTER TABLE public.images OWNER TO postgres;
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
AS $_$
SELECT *
FROM images
WHERE state=$1
ORDER BY random()
LIMIT 1
$_$;
@ -1240,39 +1318,72 @@ ALTER FUNCTION public.ups(public.submissions) OWNER TO postgres;
CREATE FUNCTION public.ups_test(public.submissions) RETURNS bigint
LANGUAGE sql IMMUTABLE STRICT
AS $_$
select (
(
SELECT count(*)
from (
select * from votes
where submission_id=$1.id
and vote_type=1
) as v1
join (select * from users where users.is_banned=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
) as v1
join (select * from users where is_banned=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
) as v2
on ((a.user1=v2.id or a.user2=v2.id) and v2.id != v1.id)
join (select * from users where is_banned=0) as u2
on u2.id=v2.user_id
where v1.id is not null
and v2.id is not null
))
AS $_$
select (
(
SELECT count(*)
from (
select * from votes
where submission_id=$1.id
and vote_type=1
) as v1
join (select * from users where users.is_banned=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
) as v1
join (select * from users where is_banned=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
) as v2
on ((a.user1=v2.id or a.user2=v2.id) and v2.id != v1.id)
join (select * from users where is_banned=0) as u2
on u2.id=v2.user_id
where v1.id is not null
and v2.id is not null
))
$_$;