From 76aa7b3bc4998f2f9e8e961a673430df8927494e Mon Sep 17 00:00:00 2001 From: Dessa Simpson Date: Sat, 19 Sep 2020 12:18:45 -0700 Subject: [PATCH] Make bans work properly --- db/50-bans.sql | 3 ++- db/90-views.sql | 22 +++++++++++++++++----- src/app.ts | 2 ++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/db/50-bans.sql b/db/50-bans.sql index 42e7b68..979ae21 100644 --- a/db/50-bans.sql +++ b/db/50-bans.sql @@ -1,3 +1,4 @@ CREATE TABLE bans ( - userid integer + userid integer, + PRIMARY KEY (userid) ) diff --git a/db/90-views.sql b/db/90-views.sql index d6e834e..94d3b3a 100644 --- a/db/90-views.sql +++ b/db/90-views.sql @@ -86,23 +86,35 @@ as in the case of no votes, user.* will be NULL. */ CREATE OR REPLACE VIEW vote_score_normal_vw AS - SELECT requests.url,COUNT(votes.requesturl),(COUNT(votes.requesturl) * (SELECT normaluservotepoints FROM config)) AS votescore FROM requests + SELECT requests.url, + COUNT(votes.requesturl) FILTER(WHERE bans.userid IS NULL), + (COUNT(votes.requesturl) FILTER(WHERE bans.userid IS NULL) * (SELECT normaluservotepoints FROM config)) AS votescore + FROM requests LEFT JOIN votes ON votes.requesturl = requests.url LEFT JOIN users on votes.userid = users.userid - WHERE COALESCE(users.isfollower,FALSE) = FALSE AND COALESCE(users.issubscriber,FALSE) = FALSE + LEFT JOIN bans ON users.userid = bans.userid + WHERE COALESCE(users.isfollower,FALSE) = FALSE + AND COALESCE(users.issubscriber,FALSE) = FALSE GROUP BY requests.url; CREATE OR REPLACE VIEW vote_score_follower_vw AS - SELECT requests.url,COUNT(votes.requesturl),(COUNT(votes.requesturl) * (SELECT followervotepoints FROM config)) AS votescore FROM requests + SELECT requests.url, + COUNT(votes.requesturl) FILTER(WHERE bans.userid IS NULL), + (COUNT(votes.requesturl) FILTER(WHERE bans.userid IS NULL) * (SELECT followervotepoints FROM config)) AS votescore LEFT JOIN votes ON votes.requesturl = requests.url LEFT JOIN users on votes.userid = users.userid - WHERE COALESCE(users.isfollower,FALSE) = TRUE AND COALESCE(users.issubscriber,FALSE) = FALSE + LEFT JOIN bans ON users.userid = bans.userid + WHERE COALESCE(users.isfollower,FALSE) = TRUE + AND COALESCE(users.issubscriber,FALSE) = FALSE GROUP BY requests.url; CREATE OR REPLACE VIEW vote_score_subscriber_vw AS - SELECT requests.url,COUNT(votes.requesturl),(COUNT(votes.requesturl) * (SELECT subscribervotepoints FROM config)) AS votescore FROM requests + SELECT requests.url, + COUNT(votes.requesturl) FILTER(WHERE bans.userid IS NULL), + (COUNT(votes.requesturl) FILTER(WHERE bans.userid IS NULL) * (SELECT subscribervotepoints FROM config)) AS votescore LEFT JOIN votes ON votes.requesturl = requests.url LEFT JOIN users on votes.userid = users.userid + LEFT JOIN bans ON users.userid = bans.userid WHERE COALESCE(users.issubscriber,FALSE) = TRUE GROUP BY requests.url; diff --git a/src/app.ts b/src/app.ts index 163f1ec..d1d7a9d 100644 --- a/src/app.ts +++ b/src/app.ts @@ -310,9 +310,11 @@ async function processBannedUsers() { var dbconn = await db.connect(); try { await dbconn.query('BEGIN'); + db.query("DELETE FROM bans"); for (var ban of response.data) { if (ban.expires_at == '') dbconn.query(Object.assign(queries.insertBan,{ values: [ban.user_id] })) } + db.query("CALL update_scores()"); await dbconn.query('COMMIT'); } catch (e) { await dbconn.query('ROLLBACK');