From d0bd678ff36fed7cf492b89542588b82efafb938 Mon Sep 17 00:00:00 2001 From: sostler Date: Tue, 13 Apr 2010 07:30:34 -0400 Subject: Added initial muting --- src/admin.clj | 38 +++++++++++++++++++++++++++++++------- src/site.clj | 30 ++++++++++++++++++------------ src/utils.clj | 7 +++++++ 3 files changed, 56 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/admin.clj b/src/admin.clj index a7eced1..a3fc076 100644 --- a/src/admin.clj +++ b/src/admin.clj @@ -3,6 +3,7 @@ (:require [clojure.contrib.str-utils2 :as s]) (:use compojure email + scheduled-agent utils)) ;; Debug Page @@ -46,6 +47,24 @@ ;; Muting +(def *mute-refresh-period-sec* 60) + +(def fetch-mutes-query " +SELECT *, set_on + duration AS expiry +FROM mutes +WHERE (set_on + duration) < now() +AND NOT is_canceled +") + +(defn update-mutes [] + (let [res (do-select [fetch-mutes-query])] + (zipmap (map :user_id res) res))) + +(def *active-mutes* + (scheduled-agent (no-args-adaptor update-mutes) + *mute-refresh-period-sec* + nil)) + (defn mute-status [session] (if-vip (println session))) @@ -59,11 +78,16 @@ (defn mute! [session params] (if-vip - (let [nick (params :user) - user_id (:user_id (fetch-nick nick)) - interval (parse-pos-interval (params :time) (params :unit)) + (let [nick (params :nick) + user-id (:user_id (fetch-nick nick)) + duration (parse-pos-interval (params :time) (s/butlast (params :unit) 1)) reason (params :reason) - admin-user-id (session :user_id)] - (cond (not user_id) [400 "INVALID_NICK"] - (not interval) [400 "INVALID_INTERVAL"] - :else (do "OK"))))) + admin-id (session :user_id)] + (cond (not user-id) [400 "INVALID_NICK"] + (not duration) [400 "INVALID_DURATION"] + ;; TODO: Ugly interval hack, w/ no escaping. Totally unsafe. + :else (let [q (format "INSERT INTO mutes (user_id, admin_id, duration, reason) + VALUES (%s, %s, '%s', '%s')" + user-id admin-id duration reason)] + (and (do-cmds q) "OK")))))) + diff --git a/src/site.clj b/src/site.clj index 57f9bb1..234f5da 100644 --- a/src/site.clj +++ b/src/site.clj @@ -21,7 +21,8 @@ cookie-login session-sweeper feed - tags)) + tags + scheduled-agent)) (def *run-flusher* true) (def *flusher-sleep* (seconds 4)) @@ -351,11 +352,11 @@ invalid-nick-reason (is-invalid-nick? nick)] (cond invalid-nick-reason (resp-error invalid-nick-reason) (check-nick nick) (resp-error "NICK_TAKEN") - :else (with-connection *db* - (insert-values :users - [:nick :hash :email] - [nick hash email]) - (let [db-user (fetch-nick nick)] + :else (do + (do-insert :users + [:nick :hash :email] + [nick hash email]) + (let [db-user (fetch-nick nick)] (send-registration-email nick email) [(session-assoc-from-db db-user) (resp-success "OK")]))))) @@ -632,14 +633,19 @@ :message_id)))) (defn msg [session params] - (let [user-id (session :user_id) - nick (session :nick) - room-key (params :room) - room (lookup-room room-key) - content (.trim (params :content)) - now (new Date)] + (let [user-id (session :user_id) + mute ((poll *active-mutes*) user-id) + nick (session :nick) + room-key (params :room) + room (lookup-room room-key) + content (.trim (params :content)) + now (new Date)] (cond (not room) (resp-error "BAD_ROOM") (not nick) (resp-error "NOT_LOGGED_IN") + mute (resp-error + (format (str "I'm sorry, you've been muted for %s. " + "You'll be able to post again on %s EST.") + (mute :reason) (mute :expiry))) :else (let [msg-id (msg-db user-id (room :room_id) content) msg (struct message-struct nick content now msg-id)] diff --git a/src/utils.clj b/src/utils.clj index ea696e5..da4d4be 100755 --- a/src/utils.clj +++ b/src/utils.clj @@ -20,6 +20,9 @@ ;; Misc +(defn no-args-adaptor [f] + (fn [& more] (f))) + (defn ms-in-future [ms] (+ ms (System/currentTimeMillis))) @@ -59,6 +62,10 @@ ;; Database +(defn do-cmds [query] + (with-connection *db* + (do-commands query))) + (defn do-select [query] (with-connection *db* (with-query-results rs query -- cgit v1.2.3-70-g09d2