hamen (owner)

Revisions

gist: 148982 Download_button fork
public
Public Clone URL: git://gist.github.com/148982.git
Embed All Files: show embed
Identi.ca Ubiquity command #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// This is just a customization of original Twitter Ubiquity command by Mozilla
// I just replaced some strings. For credits refer to mozilla.com
const IDENTICA_STATUS_MAXLEN = 140;
 
CmdUtils.CreateCommand({
  names: ["identi.ca", "share using identi.ca"],
  arguments: [
    {role: "object", label: 'status', nountype: noun_arb_text},
    {role: "alias", nountype: noun_type_twitter_user}
  ],
  icon: "http://identi.ca/favicon.ico",
  description:
  "Sets your Identi.ca status to a message of at most 160 characters.",
  help: ("You'll need a <a href=\"http://identi.ca\">Identica account</a>," +
         " obviously. If you're not already logged in" +
         " you'll be asked to log in."),
  preview: function(previewBlock, args) {
    var statusText = (args.object ? args.object.text : '');
    var usernameText = "";
    if (args.alias) {
      usernameText = args.alias.text;
    } else if (args.as) {
      usernameText = args.as.text;
    }
    var previewTemplate = (
      "<div class='identica'>"+_("Updates your Identi.ca status ${username} to:")+"<br/>" +
      "<b class='status'>${status}</b><br/><br/>" +
      _("Characters remaining: <b>${chars}</b>") +
      "<p><small>"+"</small></p></div>");
    var truncateTemplate = (
      "<strong>"+_("The last <b>${truncate}</b> characters will be truncated!")+"</strong>");
    var previewData = {
      status: <>{statusText}</>.toXMLString(),
      username: usernameText && _("(For user <b>${usernameText}</b>)"),
      chars: IDENTICA_STATUS_MAXLEN - statusText.length
    };
 
    var previewHTML = CmdUtils.renderTemplate(
                        CmdUtils.renderTemplate(previewTemplate, previewData),
                        {usernameText:usernameText});
 
    if (previewData.chars < 0) {
      var truncateData = {
        truncate: 0 - previewData.chars
      };
 
      previewHTML += CmdUtils.renderTemplate(truncateTemplate, truncateData);
    }
 
    previewBlock.innerHTML = previewHTML;
  },
  execute: function(args) {
    var statusText = args.object.text;
    if(statusText.length < 1) {
      this._show(_("requires a status to be entered"));
      return;
    }
 
    var updateUrl = "http://identi.ca/api/statuses/update.json";
    var updateParams = {
      source: "ubiquity",
      status: statusText
      //dont cut the input since sometimes, the user selects a big url,
      //and the total lenght is more than 140, but tinyurl takes care of that
    };
    var me = this;
 
    function sendMessage() {
      jQuery.ajax({
        type: "POST",
        url: updateUrl,
        data: updateParams,
        dataType: "json",
        error: function() {
          me._show(_("error - status not updated"));
        },
        success: function() {
          me._show(/^d /.test(statusText)
                   ? _("direct message sent")
                   : _("status updated"));
        },
        username: login.username,
        password: login.password
      });
    }
 
    var login;
    var alias = args.alias;
    if (alias && alias.text && alias.data) {
      login = alias.data;
      sendMessage();
    } else {
      login = {username: null,
               password: null};
      if (alias && alias.text)
        login.username = alias.text;
      sendMessage();
    }
  },
  _show: function(txt){
    displayMessage({icon: this.icon, title: this.name, text: txt});
  }
});