From 92fc288f6200ff0c8943cde89c62f675862cb1fa Mon Sep 17 00:00:00 2001 From: Scott Ostler Date: Tue, 22 Feb 2011 21:42:33 -0500 Subject: Add friendly dates to mute error --- src/admin.clj | 19 ++++++++++--------- src/site.clj | 10 +++++----- src/utils.clj | 21 ++++++++++++++------- 3 files changed, 29 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/admin.clj b/src/admin.clj index 3593072..0daf66a 100644 --- a/src/admin.clj +++ b/src/admin.clj @@ -29,11 +29,12 @@ ;; Muting -(def *mute-refresh-period-sec* 60) +(def mute-refresh-period-sec 60) (def fetch-active-mutes-query " SELECT m.*, (m.set_on + m.duration) AS expiry, + (m.set_on + m.duration) - now() AS remaining, a.nick AS admin_nick, o.nick AS nick FROM mutes m, users a, users o @@ -81,9 +82,9 @@ LIMIT 1 (defn fetch-mute [mute-id] (first (do-select [fetch-mute-query mute-id]))) -(def *active-mutes* +(def active-mutes (scheduled-agent fetch-active-mute-map - *mute-refresh-period-sec* + mute-refresh-period-sec nil)) (defn parse-pos-interval [time unit] @@ -97,7 +98,7 @@ LIMIT 1 (do-prepared! "INSERT INTO mutes (user_id, admin_id, duration, reason) VALUES (?, ?, CAST (? AS INTERVAL), ?)" [user-id admin-id duration reason]) - (update! *active-mutes*)) + (update! active-mutes)) (defn mute! [session params] (if-vip @@ -136,7 +137,7 @@ AND cancelled = false :cancel_admin_id admin-id :cancel_reason reason})) (do - (update! *active-mutes*) + (update! active-mutes) (resp-success "OK")) (resp-error "UPDATE_ERROR"))))) @@ -147,10 +148,10 @@ AND cancelled = false admin-id (session :user_id)] (cancel-mute! mute-id admin-id reason)))) -(defn format-mute [mute] +(defn format-mute-error [mute] (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))) + "You'll be able to post again in %s.") + (mute :reason) (format-friendly-interval (:remaining mute)))) (def mute-formatter {:duration format-interval :set_on format-date-first-timestamp @@ -189,7 +190,7 @@ AND cancelled = false (defn debug-page [session flash] (if-vip - (let [mutes (poll *active-mutes*) + (let [mutes (poll active-mutes) st (fetch-template "debug" session)] (.setAttribute st "flash" (:msg flash)) (.setAttribute st "mailtemps" (lookup-templates "template/mail" "welcome")) diff --git a/src/site.clj b/src/site.clj index 98524d4..6fce880 100644 --- a/src/site.clj +++ b/src/site.clj @@ -607,7 +607,7 @@ WHERE user_id IN (defn msg [session params] (let [user-id (session :user_id) - mute (get (poll *active-mutes*) user-id) + mute (get (poll active-mutes) user-id) nick (session :nick) room-key (params :room) room (lookup-room room-key) @@ -617,7 +617,7 @@ WHERE user_id IN (cond (not room) (resp-error "BAD_ROOM") (not nick) (resp-error "NOT_LOGGED_IN") content-too-long? (resp-error "TOO_LONG") - mute (resp-error (format-mute mute)) + mute (resp-error (format-mute-error mute)) :else (let [content (validated-content content session) msg-info (insert-message! user-id nick (:avatar session) room content) @@ -1086,11 +1086,11 @@ WHERE user_id IN nick (session :nick) user-id (session :user_id) image (params :image) - mute (get (poll *active-mutes*) user-id) + mute (get (poll active-mutes) user-id) has-access (validate-room-access room-key session)] (cond (not nick) [200 "NOT_LOGGED_IN"] (not image) [200 "INVALID_REQUEST"] - mute [200 (format-mute mute)] + mute [200 (format-mute-error mute)] (not has-access) [200 "UNKNOWN_ROOM"] :else (do-upload session image (lookup-room room-key))))) @@ -1385,7 +1385,7 @@ WHERE user_id IN (def server (start-server (options :port))) -(start! *active-mutes*) +(start! active-mutes) (start-user-flusher!) (start-session-pruner!) diff --git a/src/utils.clj b/src/utils.clj index a332898..c1a998c 100755 --- a/src/utils.clj +++ b/src/utils.clj @@ -157,14 +157,21 @@ (defn pluralize [word val] (if (= val 1) word (str word "s"))) +(defn build-interval-table [i] + [[(.getYears i) "year" ] + [(.getMonths i) "month" ] + [(.getDays i) "day" ] + [(.getHours i) "hour" ] + [(.getMinutes i) "minute"]]) + +(defn format-friendly-interval [i] + (if-let [[val unit] (first + (filter #(> (first %) 0) + (build-interval-table i)))] + (format "%s %s" (inc val) (pluralize unit (inc val))))) + (defn format-interval [i] - (let [vals [(.getYears i) - (.getMonths i) - (.getDays i) - (.getHours i) - (.getMinutes i)] - labels ["year" "month" "day" "hour" "minute"] - arr (into [] (for [[l v] (map vector labels vals) :when (> v 0)] + (let [arr (into [] (for [[v l] (build-interval-table i) :when (> v 0)] (str v " " (pluralize l v))))] (str-join ", " arr))) -- cgit v1.2.3-70-g09d2