Skip to content

Instantly share code, notes, and snippets.

@gialloporpora
Created March 2, 2010 20:10
Show Gist options
  • Save gialloporpora/319860 to your computer and use it in GitHub Desktop.
Save gialloporpora/319860 to your computer and use it in GitHub Desktop.
const STATUSNET_STATUS_MAX_LEN = 140;
const STATUSNET_DOMAIN = "mozilla.status.net";
const STATUSNET_NAME = "mozilla status"; /* Change the name of command manually */
var noun_type_statusnet_user = {
label: "user",
rankLast: true,
noExternalCalls: true,
cacheTime: 0,
suggest: function nt_twuser_suggest(text, html, cb, selected) {
// reject text from selection.
if (!text || selected) return [];
var foundAt = text[0] === "@";
if (foundAt) text = text.slice(1); // strip off the @
var suggs = CmdUtils.grepSuggs(text, this.logins());
// only letters, numbers, and underscores are allowed in status.net usernames.
if (/^\w+$/.test(text)) suggs.push(CmdUtils.makeSugg(text, null, {}, .4));
if (foundAt) suggs = [
{ __proto__: s,
summary: "@" + s.summary,
score: Math.pow(s.score, 0.8) }
for each (s in suggs)];
return suggs;
},
logins: function nt_twuser_logins(reload) {
// TODO: figure out how often to clear this list cache.
if (this._list && !reload) return this._list;
var list = [];
var token = (Cc["@mozilla.org/security/pk11tokendb;1"]
.getService(Ci.nsIPK11TokenDB)
.getInternalKeyToken());
if (!token.needsLogin() || token.isLoggedIn()) {
// Look for status.net usernames stored in password manager
var usersFound = {};
var passwordManager = (Cc["@mozilla.org/login-manager;1"]
.getService(Ci.nsILoginManager));
for each (let url in ["https://" + STATUSNET_DOMAIN, "http://" + STATUSNET_DOMAIN]) {
for each (let login in passwordManager.findLogins({}, url, "", "")) {
let {username} = login;
if (username in usersFound) continue;
usersFound[username] = true;
list.push(CmdUtils.makeSugg(username, null, login));
}
}
}
return this._list = list;
},
_list: null,
};
CmdUtils.CreateCommand({
names: ["mozilla-status", "mozillaca"],
description:
"Sets your " + STATUSNET_NAME + " status to a message of at most 140 characters.",
help: ("You'll need a <a href=\"http://" +
STATUSNET_DOMAIN +
"\">" +
STATUSNET_DOMAIN +
" account</a>," +
" obviously. If you're not already logged in" +
" you'll be asked to log in."),
icon: "http://" + STATUSNET_DOMAIN + "/favicon.ico",
arguments: [{role: "object", label: "status", nountype: noun_arb_text},
{role: "alias", nountype: noun_type_statusnet_user}],
preview: function statusnet_preview (previewBlock, args) {
var statusText = args.object.text;
status2 = statusText;
// status = this._shortText(statusText);
var usernameText = args.alias.text;
var previewTemplate = (
"<div class='twitter'>"+
_("Updates your " + STATUSNET_NAME + " status ${username} to:") + "<br/>" +
"<b class='status'>${status}</b><br/><br/>" +
_("Characters remaining: <b>${chars}</b>") +
"<p><small>" +
_("tip: tweet @ubiquity for help or bug") +
"</small></p></div>");
var previewData = {
status: status2,
username: usernameText && _("(For user <b>${usernameText}</b>)",
{usernameText: args.alias.html}),
chars:STATUSNET_STATUS_MAX_LEN - statusText.length,
};
var previewHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
if (previewData.chars < 0)
previewHTML += (
"<strong>" +
_("The last <b>${truncate}</b> characters will be truncated!",
{truncate: -previewData.chars}) +
"</strong>");
previewBlock.innerHTML = previewHTML;
},
execute: function statusnet_execute(args) {
var statusText = args.object.text;
if (!statusText.length) {
this._show(_("requires a status to be entered"));
return;
}
var me = this;
var login = args.alias.data || {};
jQuery.ajax({
type: "POST",
url: "https://" + STATUSNET_DOMAIN +"/api/statuses/update.json",
data: {
//dont cut the input since sometimes, the user selects a big url,
status: statusText,
source: "ubiquity",
},
dataType: "json",
error: function statusnet_error() {
me._show(_("error - status not updated"));
},
success: function statusnet_success() {
me._show(/^d /.test(statusText)
? _("direct message sent")
: _("status updated"));
},
username: login.username,
password: login.password,
});
},
_show: function statusnet__show(txt) {
displayMessage(txt, this);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment