2018-06-02 13:59:02 +02:00
|
|
|
var zcommand = (function () {
|
|
|
|
|
|
|
|
var exports = {};
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
What in the heck is a zcommand?
|
|
|
|
|
|
|
|
A zcommand is basically a specific type of slash
|
|
|
|
command where the client does almost no work and
|
|
|
|
the server just does something pretty simple like
|
|
|
|
flip a setting.
|
|
|
|
|
|
|
|
The first zcommand we wrote is for "/ping", and
|
|
|
|
the server just responds with a 200 for that.
|
|
|
|
|
|
|
|
Not all slash commands use zcommand under the hood.
|
|
|
|
For more exotic things like /poll see submessage.js
|
|
|
|
and widgetize.js
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
exports.send = function (opts) {
|
|
|
|
var command = opts.command;
|
|
|
|
var on_success = opts.on_success;
|
|
|
|
var data = {
|
|
|
|
command: command,
|
|
|
|
};
|
|
|
|
|
|
|
|
channel.post({
|
|
|
|
url: '/json/zcommand',
|
|
|
|
data: data,
|
|
|
|
success: function (data) {
|
|
|
|
on_success(data);
|
|
|
|
},
|
|
|
|
error: function () {
|
|
|
|
exports.tell_user('server did not respond');
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.tell_user = function (msg) {
|
|
|
|
// This is a bit hacky, but we don't have a super easy API now
|
|
|
|
// for just telling users stuff.
|
|
|
|
$('#compose-send-status').removeClass(common.status_classes)
|
|
|
|
.addClass('alert-error')
|
|
|
|
.stop(true).fadeTo(0, 1);
|
|
|
|
$('#compose-error-msg').text(msg);
|
|
|
|
};
|
|
|
|
|
2018-06-02 16:23:32 +02:00
|
|
|
function update_setting(command) {
|
|
|
|
exports.send({
|
|
|
|
command: command,
|
|
|
|
on_success: function (data) {
|
|
|
|
exports.tell_user(data.msg);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-02 13:59:02 +02:00
|
|
|
exports.process = function (message_content) {
|
|
|
|
|
|
|
|
var content = message_content.trim();
|
|
|
|
|
|
|
|
if (content === '/ping') {
|
|
|
|
var start_time = new Date();
|
|
|
|
|
|
|
|
exports.send({
|
2018-06-20 20:46:33 +02:00
|
|
|
command: content,
|
2018-06-02 13:59:02 +02:00
|
|
|
on_success: function () {
|
|
|
|
var end_time = new Date();
|
|
|
|
var diff = end_time - start_time;
|
|
|
|
diff = Math.round(diff);
|
|
|
|
var msg = "ping time: " + diff + "ms";
|
|
|
|
exports.tell_user(msg);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-11 01:59:01 +02:00
|
|
|
var mode_commands = ['/day', '/night', '/light', '/dark'];
|
|
|
|
if (mode_commands.indexOf(content) >= 0) {
|
2018-06-20 20:46:33 +02:00
|
|
|
update_setting(content);
|
2018-06-02 16:23:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-17 21:10:57 +02:00
|
|
|
if (content === '/settings') {
|
2018-12-02 17:12:47 +01:00
|
|
|
hashchange.go_to_location('settings/your-account');
|
2018-06-17 21:10:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-02 13:59:02 +02:00
|
|
|
// It is incredibly important here to return false
|
|
|
|
// if we don't see an actual zcommand, so that compose.js
|
|
|
|
// knows this is a normal message.
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
return exports;
|
|
|
|
}());
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = zcommand;
|
|
|
|
}
|
2018-05-28 08:04:36 +02:00
|
|
|
|
|
|
|
window.zcommand = zcommand;
|