Cooldown is now working!

This commit is contained in:
Nicolas Stepien 2012-01-25 22:51:05 +01:00
parent 93895e081e
commit fb3dee8e6e
2 changed files with 86 additions and 17 deletions

View File

@ -1282,14 +1282,43 @@
if (g.dead) { if (g.dead) {
value = 404; value = 404;
disabled = true; disabled = true;
} else if (data.progress) { } else {
value = data.progress; value = qr.cooldown.seconds || data.progress || value;
} }
if (!qr.el) return; if (!qr.el) return;
input = qr.status.input; input = qr.status.input;
input.value = value || 'Submit'; input.value = value || 'Submit';
return input.disabled = disabled || false; return input.disabled = disabled || false;
}, },
cooldown: {
init: function() {
if (!conf['Cooldown']) return;
qr.cooldown.start($.get("/" + g.BOARD + "/cooldown", 0));
return $.on(window, 'storage', function(e) {
var timeout;
if (e.key === ("" + NAMESPACE + "/" + g.BOARD + "/cooldown") && (timeout = JSON.parse(e.newValue))) {
return qr.cooldown.start(timeout);
}
});
},
start: function(timeout) {
var seconds;
seconds = Math.floor((timeout - Date.now()) / 1000);
return qr.cooldown.count(seconds);
},
set: function(seconds) {
if (!conf['Cooldown']) return;
qr.cooldown.count(seconds);
return $.set("/" + g.BOARD + "/cooldown", Date.now() + seconds * SECOND);
},
count: function(seconds) {
if (!((60 >= seconds && seconds >= 0))) return;
setTimeout(qr.cooldown.count, 1000, seconds - 1);
if (seconds === 0) $["delete"]("/" + g.BOARD + "/cooldown");
qr.cooldown.seconds = seconds;
return qr.status();
}
},
pickThread: function(thread) { pickThread: function(thread) {
return $('select', qr.el).value = thread; return $('select', qr.el).value = thread;
}, },
@ -1540,7 +1569,7 @@
}); });
qr.mimeTypes = mimeTypes.split(', '); qr.mimeTypes = mimeTypes.split(', ');
qr.spoiler = !!$('#com_submit + label'); qr.spoiler = !!$('#com_submit + label');
qr.el = ui.dialog('qr', 'top:0;right:0;', "<div class=move> Quick Reply <input type=checkbox name=autohide id=autohide title=Auto-hide> <span>" + (g.REPLY ? '' : threads) + " <a class=close>x</a></span></div><form> <div><input id=dump class=field type=button title='Dump mode' value=+><input name=name title=Name placeholder=Name class=field size=1><input name=email title=E-mail placeholder=E-mail class=field size=1><input name=sub title=Subject placeholder=Subject class=field size=1></div> <output id=replies><div><a id=addReply href=javascript:;>+</a></div></output> <div><textarea name=com title=Comment placeholder=Comment class=field></textarea></div> <div class=captcha title=Reload><img></div> <div><input name=captcha title=Verification class=field autocomplete=off size=1></div> <div><input type=file name=upfile max=" + ($('[name=MAX_FILE_SIZE]').value) + " accept='" + mimeTypes + "' multiple><input type=submit></div> <label" + (qr.spoiler ? '' : ' hidden') + "><input type=checkbox id=spoiler> Spoiler Image?</label> <div class=warning></div></form>"); qr.el = ui.dialog('qr', 'top:0;right:0;', "<div class=move> Quick Reply <input type=checkbox name=autohide id=autohide title=Auto-hide> <span>" + (g.REPLY ? '' : threads) + " <a class=close>x</a></span></div><form> <div><input id=dump class=field type=button title='Dump mode' value=+><input name=name title=Name placeholder=Name class=field size=1><input name=email title=E-mail placeholder=E-mail class=field size=1><input name=sub title=Subject placeholder=Subject class=field size=1></div> <output id=replies><div><a id=addReply href=javascript:;>+</a></div></output> <div><textarea name=com title=Comment placeholder=Comment class=field></textarea></div> <div class=captcha title=Reload><img></div> <div><input name=captcha title=Verification class=field autocomplete=off size=1></div> <div><input type=file name=upfile max=" + ($('[name=MAX_FILE_SIZE]').value) + " accept='" + mimeTypes + "' multiple><input type=submit></div> <label" + (qr.spoiler ? '' : ' hidden') + "><input type=checkbox id=spoiler> Spoiler Image</label> <div class=warning></div></form>");
if (!g.REPLY) { if (!g.REPLY) {
$.on($('select', qr.el), 'mousedown', function(e) { $.on($('select', qr.el), 'mousedown', function(e) {
return e.stopPropagation(); return e.stopPropagation();
@ -1585,6 +1614,7 @@
}); });
qr.status.input = $('[type=submit]', qr.el); qr.status.input = $('[type=submit]', qr.el);
qr.status(); qr.status();
qr.cooldown.init();
qr.captcha.init(); qr.captcha.init();
qr.message.init(); qr.message.init();
return $.add(d.body, qr.el); return $.add(d.body, qr.el);
@ -1592,6 +1622,7 @@
submit: function(e) { submit: function(e) {
var captcha, captchas, challenge, err, file, m, post, reader, reply, response, threadID; var captcha, captchas, challenge, err, file, m, post, reader, reply, response, threadID;
if (e != null) e.preventDefault(); if (e != null) e.preventDefault();
if (qr.cooldown.seconds) return;
qr.message.send({ qr.message.send({
abort: true abort: true
}); });
@ -1653,7 +1684,7 @@
return qr.message.send(post); return qr.message.send(post);
}, },
response: function(html) { response: function(html) {
var b, err, node, persona, reply, sage; var b, err, node, persona, postNumber, reply, thread, _, _ref;
qr.status(); qr.status();
if (!(b = $('td b', $.el('a', { if (!(b = $('td b', $.el('a', {
innerHTML: html innerHTML: html
@ -1662,14 +1693,19 @@
} else if (b.childElementCount) { } else if (b.childElementCount) {
if (b.firstChild.tagName) node = b.firstChild; if (b.firstChild.tagName) node = b.firstChild;
err = b.firstChild.textContent; err = b.firstChild.textContent;
if (err === 'You seem to have mistyped the verification.') {} if (err === 'You seem to have mistyped the verification.') {
qr.cooldown.set(10);
}
} }
if (err) { if (err) {
qr.error(err, node); qr.error(err, node);
return; return;
} }
reply = qr.replies[0]; reply = qr.replies[0];
sage = /sage/i.test(reply.email); _ref = b.lastChild.textContent.match(/thread:(\d+),no:(\d+)/), _ = _ref[0], thread = _ref[1], postNumber = _ref[2];
if (thread === 0) {} else {
qr.cooldown.set(/sage/i.test(reply.email) ? 60 : 30);
}
persona = $.get('qr.persona', {}); persona = $.get('qr.persona', {});
persona = { persona = {
name: reply.name, name: reply.name,
@ -3586,11 +3622,11 @@ textarea.field {\
width: 100%;\ width: 100%;\
}\ }\
#qr [type=file] {\ #qr [type=file] {\
width: 80%;\ width: 75%;\
}\ }\
#qr [type=submit] {\ #qr [type=submit] {\
padding: 0 -moz-calc(1px); /* Gecko does not respect box-sizing: border-box */\ padding: 0 -moz-calc(1px); /* Gecko does not respect box-sizing: border-box */\
width: 20%;\ width: 25%;\
}\ }\
\ \
.new {\ .new {\

View File

@ -923,13 +923,37 @@ qr =
if g.dead if g.dead
value = 404 value = 404
disabled = true disabled = true
else if data.progress else
value = data.progress # do not cancel `value = 'Loading'` once the cooldown is over
value = qr.cooldown.seconds or data.progress or value
return unless qr.el return unless qr.el
{input} = qr.status {input} = qr.status
input.value = value or 'Submit' input.value = value or 'Submit' # dispaly "Auto #{...}" when auto postan
input.disabled = disabled or false input.disabled = disabled or false
cooldown:
init: ->
return unless conf['Cooldown']
qr.cooldown.start $.get "/#{g.BOARD}/cooldown", 0
$.on window, 'storage', (e) ->
if e.key is "#{NAMESPACE}/#{g.BOARD}/cooldown" and timeout = JSON.parse e.newValue
qr.cooldown.start timeout
start: (timeout) ->
seconds = Math.floor (timeout - Date.now()) / 1000
qr.cooldown.count seconds
set: (seconds) ->
return unless conf['Cooldown']
qr.cooldown.count seconds
$.set "/#{g.BOARD}/cooldown", Date.now() + seconds*SECOND
count: (seconds) ->
return unless 60 >= seconds >= 0
setTimeout qr.cooldown.count, 1000, seconds-1
if seconds is 0
$.delete "/#{g.BOARD}/cooldown"
# auto postan
qr.cooldown.seconds = seconds
qr.status()
pickThread: (thread) -> pickThread: (thread) ->
$('select', qr.el).value = thread $('select', qr.el).value = thread
@ -1133,7 +1157,7 @@ qr =
<div class=captcha title=Reload><img></div> <div class=captcha title=Reload><img></div>
<div><input name=captcha title=Verification class=field autocomplete=off size=1></div> <div><input name=captcha title=Verification class=field autocomplete=off size=1></div>
<div><input type=file name=upfile max=#{$('[name=MAX_FILE_SIZE]').value} accept='#{mimeTypes}' multiple><input type=submit></div> <div><input type=file name=upfile max=#{$('[name=MAX_FILE_SIZE]').value} accept='#{mimeTypes}' multiple><input type=submit></div>
<label#{if qr.spoiler then '' else ' hidden'}><input type=checkbox id=spoiler> Spoiler Image?</label> <label#{if qr.spoiler then '' else ' hidden'}><input type=checkbox id=spoiler> Spoiler Image</label>
<div class=warning></div> <div class=warning></div>
</form>" </form>"
unless g.REPLY unless g.REPLY
@ -1160,12 +1184,16 @@ qr =
qr.status.input = $ '[type=submit]', qr.el qr.status.input = $ '[type=submit]', qr.el
qr.status() qr.status()
qr.cooldown.init()
qr.captcha.init() qr.captcha.init()
qr.message.init() qr.message.init()
$.add d.body, qr.el $.add d.body, qr.el
submit: (e) -> submit: (e) ->
e?.preventDefault() e?.preventDefault()
if qr.cooldown.seconds
# toggle auto postan
return
qr.message.send abort: true qr.message.send abort: true
reply = qr.replies[0] reply = qr.replies[0]
@ -1239,15 +1267,20 @@ qr =
node = b.firstChild if b.firstChild.tagName # duplicate image link node = b.firstChild if b.firstChild.tagName # duplicate image link
err = b.firstChild.textContent err = b.firstChild.textContent
if err is 'You seem to have mistyped the verification.' if err is 'You seem to have mistyped the verification.'
;# cooldown to prevent autoban qr.cooldown.set 10 # prevent autoban
# turn auto postan ON? Might depend if we have captchas in stock or not
if err if err
qr.error err, node qr.error err, node
return return
reply = qr.replies[0] reply = qr.replies[0]
sage = /sage/i.test reply.email
# cooldown [_, thread, postNumber] = b.lastChild.textContent.match /thread:(\d+),no:(\d+)/
if thread is 0 # new thread
# auto noko to postNumber
else
qr.cooldown.set if /sage/i.test reply.email then 60 else 30
persona = $.get 'qr.persona', {} persona = $.get 'qr.persona', {}
persona = persona =
@ -2825,11 +2858,11 @@ textarea.field {
width: 100%; width: 100%;
} }
#qr [type=file] { #qr [type=file] {
width: 80%; width: 75%;
} }
#qr [type=submit] { #qr [type=submit] {
padding: 0 -moz-calc(1px); /* Gecko does not respect box-sizing: border-box */ padding: 0 -moz-calc(1px); /* Gecko does not respect box-sizing: border-box */
width: 20%; width: 25%;
} }
.new { .new {