Skip to content

Instantly share code, notes, and snippets.

@cmdln
Created June 3, 2010 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmdln/424432 to your computer and use it in GitHub Desktop.
Save cmdln/424432 to your computer and use it in GitHub Desktop.
function initUbiquiStatus() {
/** Home page URL for this command set. */
const _HOME_PAGE = "http://bitbucketlabs.net/ubiqui-status/";
/** Icon from the StatusNet home page. */
const _ICON = "http://status.net/sites/default/files/favicon.ico";
/** Maximum character length for StatusNet updates. */
const _MAX_MSG_LEN = 140;
/** Styling for all commands and previews. */
const _STYLESHEET = "<style> " +
".container {" +
"font-size: smaller;" +
"font-family: Geneva, Verdana, Arial, sans-serif;" +
"}\n" +
".msg_line {" +
"border-top: thin solid black;" +
"border-bottom: thin solid black;" +
"background-color: #222255;" +
"margin-top: 0.2em;" +
"padding: 0.2em;" +
"font-size: larger;" +
"opacity: 0.75;" +
"}\n" +
".ftr_line {" +
"font-size: 5em;" +
"text-align: right;" +
"opacity: 0.5;" +
"}\n" +
"</style>\n";
/** Template for post-status command. */
const _POST_PREVIEW_TEMPLATE = "<div class=\"container\">" +
_("Update your ") +
"<strong>${name}</strong>" +
_(" status to:") +
"<div class=\"msg_line\">${status}</div>" +
"<div class=\"ftr_line\" style=\"color: ${color};\">${remaining}</div>" +
"</div>";
/** Template for add-status-server command. */
const _ADD_PREVIEW_TEMPLATE = _("Add the StatusNet instance at ") +
"<strong>${url}</strong>" +
_(" with the name ") +
"<strong>${name}</strong>" +
_(" with ") +
"<strong>${geolocate}</strong>";
/** Template for update-status-server command. */
const _MOD_PREVIEW_TEMPLATE = _("Update the StatusNet instance with the name ") +
"<strong>${name}</strong>" +
_(" with location? ") +
"<strong>${geolocate}</strong>";
/** Template for delete-status-server command. */
const _DEL_PREVIEW_TEMPLATE = _("Remove the StatusNet instance at ") +
"<strong>${url}</strong>" +
_(" with the name ") +
"<strong>${name}</strong>";
const _LIST_PREVIEW_TEMPLATE = "<li>${name} (${url}), " +
_("location? ") +
"${geolocate}</li>";
const _DEFAULT_SERVER_CFG = { name: 'Identi.ca',
url: 'https://identi.ca/api/statuses/',
geolocate: false };
// initialize storage, if needed, placing default
var server_configs = Bin[_HOME_PAGE]() || {};
if (server_configs['Identi.ca'] == undefined) {
server_configs['Identi.ca'] = _DEFAULT_SERVER_CFG;
}
Bin[_HOME_PAGE](server_configs);
/** Noun type to constraint to know servers by name. */
var noun_type_server = {
label: 'server name',
default: function default_fn() {
// this should always be present, but storing it persistently ensures it
// gets listed and can have its location property toggled
return CmdUtils.makeSugg('Identi.ca', null, Bin[_HOME_PAGE]()['Identi.ca']);
},
suggest: function suggest_fn(text, html, callback, selectionIndices) {
var server_configs = Bin[_HOME_PAGE]();
var server_name_lc = text.toLowerCase();
return [CmdUtils.makeSugg(key, null, server_configs[key])
for (key in server_configs)
if (~key.toLowerCase().indexOf(server_name_lc))];
}
};
function _show(title, message){
displayMessage({icon: _ICON, title: title, text: message});
}
/**
* The post-status command accepts a textual message as an object and optionally
* a server configuration name as a goal; defaulting to identi.ca if there is no
* goal.
*/
CmdUtils.CreateCommand({
names: ["post-status"],
icon: _ICON,
description: "Update your status on a StatusNet server.",
help: "Posts the message entered or texted selected and optionally allows specifying a custom StatusNet install.",
author: {name: "Thomas Gideon", email: "cmdln@thecommandline.net"},
license: "AGPL",
homepage: _HOME_PAGE,
// the custom noun type on the goal role prevents arbitrary clauses starting
// with "to" from triggering the role
arguments: [{role: "object", nountype: noun_arb_text, label: _("your status")},
{role: "goal", nountype: noun_type_server, label: _("server to update")}],
/**
* The preview shows to which server the status will be sent, the message as
* it is being written and feedback on the message limit and the current
* message size.
*/
preview: function(pblock, args) {
var server_cfg = args.goal.data;
var status_msg = args.object.text;
var preview_data = { name: server_cfg.name,
status: status_msg,
remaining: _MAX_MSG_LEN - status_msg.length,
color: (status_msg.length > _MAX_MSG_LEN) ? "red" : "lightgreen" };
var previewHtml = _STYLESHEET + CmdUtils.renderTemplate(_POST_PREVIEW_TEMPLATE, preview_data);
pblock.innerHTML = previewHtml;
},
execute: function(args) {
var server_cfg = args.goal.data;
var status_msg = args.object.text;
if(status_msg.length < 1) {
_show('post-status', server_cfg.name + _(" requires a status to be entered"));
return;
}
var update_url = server_cfg.url + "update.json";
var update_params = {
source: "ubiqui-status",
status: status_msg.substring(0,140),
lat: null,
long: null
};
if (server_cfg.geolocate) {
var update_loc = CmdUtils.getGeoLocation();
if (update_loc == undefined) {
delete update_params.lat;
delete update_params.long;
} else {
update_params.lat = update_loc.lat;
update_params.long = update_loc.long;
}
} else {
delete update_params.lat;
delete update_params.long;
}
jQuery.ajax({
type: "POST",
url: update_url,
data: update_params,
dataType: "json",
error: function() {
_show('post-status', server_cfg.name + _(" status not updated"));
},
success: function() {
_show('post-status', server_cfg.name + _(" status updated"));
}
});
}
});
/**
* Allows the user to add custom servers to use with the goal role of the
* post-status command.
*/
CmdUtils.CreateCommand({
names: ["add-status-server"],
icon: _ICON,
description: "Add a custom StatusNet server.",
help: "Requires a base URL for a given instance's API and an arbitrary name.",
author: {name: "Thomas Gideon", email: "cmdln@thecommandline.net"},
license: "AGPL",
homepage: _HOME_PAGE,
arguments: [{role: 'object', nountype: noun_type_url, label: 'base URL for StatusNet API'},
{role: 'goal', nountype: noun_arb_text, label: 'name for this instance'},
{role: 'instrument', nountype: ['location', 'no location'], label: 'location or no location'}],
preview: function preview(pblock, args) {
var name = args.goal.text;
var server_configs = Bin[_HOME_PAGE]() || {};
var data = null;
if (server_configs[args.goal.text] == undefined) {
data = { name: args.goal.text,
url: (name == _DEFAULT_SERVER_CFG.name) ? _DEFAULT_SERVER_CFG.url : args.object.text,
geolocate: args.instrument.text }
} else {
data = server_configs[args.goal.text];
}
pblock.innerHTML = CmdUtils.renderTemplate(_ADD_PREVIEW_TEMPLATE, data);
},
execute: function execute(args) {
var server_configs = Bin[_HOME_PAGE]() || {};
var name = args.goal.text;
var exists = server_configs[name] != undefined;
var url = (_DEFAULT_SERVER_CFG.name == name) ? _DEFAULT_SERVER_CFG.url : args.object.text;
var geolocate = args.instrument && 'location' == args.instrument.text;
server_configs[name] = { name: name, url: url, geolocate: geolocate };
Bin[_HOME_PAGE](server_configs);
_show('add-status-server',
((exists) ? _('Overwrote') : _('Added')) +
_(' server config ') + name);
}
});
/**
* Allows the user to update custom servers to change their configurations more
* simply than removing and re-adding. Also allows modifying the default
* server's geolocate property.
*/
CmdUtils.CreateCommand({
names: ["update-status-server"],
icon: _ICON,
description: "Update a custom StatusNet server.",
help: "Requires the name of an already configured server.",
author: {name: "Thomas Gideon", email: "cmdln@thecommandline.net"},
license: "AGPL",
homepage: _HOME_PAGE,
arguments: [{role: 'object', nountype: noun_type_server, label: 'name for this instance'},
{role: 'instrument', nountype: ['location', 'no location'], label: 'location or no location'}],
preview: function preview(pblock, args) {
var name = args.object.text;
var geolocate = (args.instrument && 'location' == args.instrument.text);
data = { name: name,
geolocate: geolocate }
pblock.innerHTML = CmdUtils.renderTemplate(_MOD_PREVIEW_TEMPLATE, data);
},
execute: function execute(args) {
var server_configs = Bin[_HOME_PAGE]();
if (server_configs == undefined) {
_show('update-status-status' +
_('Should have had some stored configurations'));
return;
}
var name = args.object.text;
var geolocate = args.instrument && 'location' == args.instrument.text;
server_configs[name].geolocate = geolocate;
_show('update-status-server', _('Updated server config ') + name);
}
});
/**
* Allows the user to add custom servers to use with the goal role of the
* post-status command.
*/
CmdUtils.CreateCommand({
names: ["delete-status-server"],
icon: _ICON,
description: "Remove a custom StatusNet server.",
help: "Requires a name of an already configured server.",
author: {name: "Thomas Gideon", email: "cmdln@thecommandline.net"},
license: "AGPL",
homepage: _HOME_PAGE,
arguments: [{role: 'object', nountype: noun_type_server, label: 'name of a configured server'}],
preview: function preview(pblock, args) {
var server_cfg = args.object.data;
var data = { name: (server_cfg == undefined) ? "none" : server_cfg.name,
url: (server_cfg == undefined) ? "none" : server_cfg.url }
pblock.innerHTML = CmdUtils.renderTemplate(_DEL_PREVIEW_TEMPLATE, data);
},
execute: function execute(args) {
if (args.object.data == undefined) {
_show('delete-status-server', _('No configured server for ') +
args.object.text);
return;
}
var server_configs = Bin[_HOME_PAGE]() || {};
var name = args.object.data.name;
delete server_configs[name];
if ('Identi.ca' == name) {
server_configs[name] = _DEFAULT_SERVER_CFG;
}
Bin[_HOME_PAGE](server_configs);
_show('delete-status-server', _('Removed server config ') + name);
}
});
/**
* Allows the user to add custom servers to use with the goal role of the
* post-status command.
*/
CmdUtils.CreateCommand({
names: ["list-status-servers"],
icon: _ICON,
description: "List configured, custom StatusNet servers.",
help: "Lists servers that have been configured, along with their urls and location setting.",
author: {name: "Thomas Gideon", email: "cmdln@thecommandline.net"},
license: "AGPL",
homepage: _HOME_PAGE,
arguments: [],
preview: function preview(pblock, args) {
var server_configs = Bin[_HOME_PAGE]() || {};
var body = "<div class=\"container\">Configured StatusNet servers:</div><ul>";
for (key in server_configs) {
body += CmdUtils.renderTemplate(_LIST_PREVIEW_TEMPLATE, server_configs[key]);
}
body += "</ul>";
pblock.innerHTML = _STYLESHEET + body;
},
execute: function execute(args) {}
});
}
initUbiquiStatus();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment