Skip to content
Browse files

added /twittername command and twitter avatars next to usernames.

  • Loading branch information...
1 parent 0480222 commit 12e55ce2394562bed8e1b27bf26dcbca671b402e @toolness committed
Showing with 135 additions and 9 deletions.
  1. +4 −1 app.js
  2. +17 −1 static/cmdline.js
  3. +13 −1 static/index.html
  4. +1 −2 static/irc.js
  5. +9 −3 static/main.js
  6. +84 −0 static/twitter.js
  7. +4 −1 static/user-list-view.js
  8. +3 −0 static/users.js
View
5 app.js
@@ -176,7 +176,10 @@ function onUserLogin(socket, username) {
});
socket.on('set-custom-global-metadata', function(data) {
var metadata = customGlobalMetadata.get('data', {});
- metadata[data.key] = data.value;
+ if (!data.value)
+ delete metadata[data.key];
+ else
+ metadata[data.key] = data.value;
customGlobalMetadata.setSync('data', metadata);
socket.broadcast.emit('change-custom-global-metadata', data);
});
View
18 static/cmdline.js
@@ -1,7 +1,7 @@
"use strict";
define(["jquery", "underscore"], function($, _) {
- return function(cmd, irc, logArea, login) {
+ return function(cmd, irc, logArea, login, twitterUsers) {
var log = logArea.log;
var commands = {
login: function(arg) {
@@ -44,6 +44,22 @@ define(["jquery", "underscore"], function($, _) {
return log("error", "Please specify a channel to leave.");
log("Attempting to leave " + arg + "...");
irc.part(arg);
+ },
+ twittername: function(arg) {
+ if (!arg)
+ return log("error", "Please provide a nick and, optionally, " +
+ "its respective Twitter name.");
+ var parts = arg.split(" ");
+ var nick = parts[0].trim();
+ var twitterName = (parts[1] || "").trim();
+
+ if (!twitterName)
+ log("Dissociating " + nick + " from any Twitter username.");
+ else
+ log("Associating " + nick + " with the Twitter username @" +
+ twitterName + ".");
+
+ twitterUsers.set(nick, twitterName);
}
};
View
14 static/index.html
@@ -29,7 +29,8 @@
}
#users .user {
- padding: 8px;
+ position: relative;
+ padding: 8px 8px 8px 32px;
}
#users .user:first-child {
@@ -40,6 +41,17 @@
border-top: 1px dotted gray;
}
+#users .user .twitter-user {
+ position: absolute;
+ top: 4px;
+ left: 4px;
+}
+
+#users .user .twitter-user img {
+ width: 24px;
+ height: 24px;
+}
+
#messages {
padding: 0;
font-size: 14px;
View
3 static/irc.js
@@ -115,9 +115,8 @@ define([
this.socket.emit('set-custom-global-metadata',
{key: key, value: value});
},
- getCustomGlobalMetadata: function(cb) {
+ getCustomGlobalMetadata: function() {
this.socket.emit('get-custom-global-metadata');
- this.socket.once('custom-global-metadata', cb);
},
getLoggedMessages: function(start, end, cb) {
var self = this;
View
12 static/main.js
@@ -9,9 +9,10 @@ define([
"cmdline",
"login",
"util/pretty-date",
- "util/misc"
+ "util/misc",
+ "twitter"
], function($, _, LogArea, IRC, UserListView, CommandLine, Login,
- prettyDate, misc) {
+ prettyDate, misc, twitter) {
function showLoggedMessages(irc, logArea) {
var CHUNK_SIZE = 10;
var lastChunk = -CHUNK_SIZE;
@@ -62,7 +63,11 @@ define([
var log = logArea.log;
var irc = new IRC();
var userListView = new UserListView(irc.users, $("#users"));
- var cmdLine = new CommandLine($("#cmd"), irc, logArea, login);
+ var twitterUsers = new twitter.TwitterUsers(irc);
+ var twitterViewMixIn = new twitter.TwitterViewMixIn(userListView,
+ twitterUsers);
+ var cmdLine = new CommandLine($("#cmd"), irc, logArea, login,
+ twitterUsers);
var isUnloading = false;
if (loginInfo) {
@@ -78,6 +83,7 @@ define([
log("info", "Your nick is " + irc.nick +
". You are in channels: " + channels.join(", ") + ".");
channels.forEach(function(channel) { irc.getNames(channel); });
+ irc.getCustomGlobalMetadata();
showLoggedMessages(irc, logArea);
});
irc.on('join', function(info) {
View
84 static/twitter.js
@@ -0,0 +1,84 @@
+"use strict";
+
+define([
+ "jquery",
+ "util/misc",
+ "event-emitter"
+], function($, misc, EventEmitter) {
+ var AVATAR_IMG = 'http://twitter.com/api/users/profile_image/';
+ var PROFILE_URL = 'http://twitter.com/';
+
+ function TwitterUsers(irc) {
+ var self = this;
+
+ this.users = {};
+ this.irc = irc;
+
+ irc.on('custom-global-metadata', function(data) {
+ data = data || {};
+ Object.keys(data).forEach(function(key) {
+ if (key.indexOf(self.PREFIX) == 0) {
+ var nick = key.slice(self.PREFIX.length);
+ self.users[nick] = data[key];
+ }
+ });
+ self.emit('change');
+ });
+ }
+
+ TwitterUsers.prototype = {
+ PREFIX: 'twitter-mapping/',
+ set: function(nick, twitterUser) {
+ if (twitterUser)
+ this.users[nick] = twitterUser;
+ else
+ delete this.users[nick];
+ this.irc.setCustomGlobalMetadata(this.PREFIX + nick, twitterUser);
+ this.emit('change');
+ }
+ };
+
+ EventEmitter.mixInto(TwitterUsers);
+
+ return {
+ TwitterUsers: TwitterUsers,
+ TwitterViewMixIn: function(userListView, twitterUsers) {
+ function insertTwitterInfo(nick) {
+ for (var baseNick in twitterUsers.users) {
+ if (misc.doesNickMatch(baseNick, nick)) {
+ var twitterName = twitterUsers.users[baseNick];
+
+ if (!twitterName)
+ return;
+
+ var a = $('<a class="twitter-user" target="_blank"></a>')
+ .attr('href', PROFILE_URL + twitterName)
+ .attr('title', nick + ' is @' + twitterName + ' on Twitter.');
+ var img = $('<img>')
+ .attr('alt', 'Twitter avator for ' + twitterName)
+ .attr('src', AVATAR_IMG + twitterName)
+ .appendTo(a);
+ userListView.getElementForNick(nick).prepend(a);
+ return;
+ }
+ }
+ }
+
+ userListView.users.on('add', insertTwitterInfo);
+ userListView.users.on('rename', function(oldnick, newnick) {
+ userListView.getElementForNick(newnick)
+ .find(".twitter-user").remove();
+ insertTwitterInfo(newnick);
+ });
+ twitterUsers.on('change', function() {
+ userListView.el.find(".twitter-user").remove();
+ userListView.users.getAllNicks().forEach(insertTwitterInfo);
+ });
+
+ return {
+ userListView: userListView,
+ twitterUsers: twitterUsers
+ };
+ }
+ };
+});
View
5 static/user-list-view.js
@@ -37,7 +37,10 @@ define([
});
return {
users: users,
- el: el
+ el: el,
+ getElementForNick: function(nick) {
+ return nicks[nick];
+ }
};
};
});
View
3 static/users.js
@@ -19,6 +19,9 @@ define([
});
return allChannels;
},
+ getAllNicks: function() {
+ return Object.keys(this.nicks);
+ },
add: function(nicks, channel) {
var self = this;
if (typeof(nicks) == "string")

0 comments on commit 12e55ce

Please sign in to comment.
Something went wrong with that request. Please try again.