Bug 955095 - Set twitter topic to the user's self-description, r=clokep,florian.

This commit is contained in:
aleth 2012-09-04 19:17:14 +02:00
Родитель d473cf18a9
Коммит 97444b42a9
2 изменённых файлов: 58 добавлений и 38 удалений

Просмотреть файл

@ -13,6 +13,9 @@ error.tooLong=Status is over 140 characters.
error.general=An error %1$S occurred while sending: %2$S error.general=An error %1$S occurred while sending: %2$S
error.retweet=An error %1$S occurred while retweeting: %2$S error.retweet=An error %1$S occurred while retweeting: %2$S
error.delete=An error %1$S occurred while deleting: %2$S error.delete=An error %1$S occurred while deleting: %2$S
# LOCALIZATION NOTE (error.descriptionTooLong)
# %S is the truncated string that was sent to the server.
error.descriptionTooLong=Description is over the maximum length (160 characters), it was automatically truncated to: %S.
# LOCALIZATION NOTE (timeline): # LOCALIZATION NOTE (timeline):
# This is the title of the conversation tab, %S will be replaced by # This is the title of the conversation tab, %S will be replaced by

Просмотреть файл

@ -167,7 +167,6 @@ Conversation.prototype = {
if (tweet.user.screen_name != this._account.name) if (tweet.user.screen_name != this._account.name)
throw "Wrong screen_name... Uh?"; throw "Wrong screen_name... Uh?";
this._account.displayMessages([tweet]); this._account.displayMessages([tweet]);
this.setTopic(tweet.text, tweet.user.screen_name);
}, },
_parseError: function(aData) { _parseError: function(aData) {
let error = ""; let error = "";
@ -317,16 +316,16 @@ Conversation.prototype = {
this.notifyObservers(new nsSimpleEnumerator([chatBuddy]), this.notifyObservers(new nsSimpleEnumerator([chatBuddy]),
"chat-buddy-add"); "chat-buddy-add");
}, },
setTopic: function(aTopic, aTopicSetter) {
const kEntities = {amp: "&", gt: ">", lt: "<"};
let topic =
aTopic.replace(/&([gl]t|amp);/g, function(str, entity) kEntities[entity]);
GenericConvChatPrototype.setTopic.call(this, topic, aTopicSetter, true);
},
get name() this.nick + " timeline", get name() this.nick + " timeline",
get title() _("timeline", this.nick), get title() _("timeline", this.nick),
get nick() "@" + this._account.name, get nick() this._account.name,
set nick(aNick) {} set nick(aNick) {},
get topicSettable() this.nick == this._account.name,
get topic() this._topic, // can't add a setter without redefining the getter
set topic(aTopic) {
if (this.topicSettable)
this._account.setUserDescription(aTopic);
}
}; };
function Account(aProtocol, aImAccount) function Account(aProtocol, aImAccount)
@ -562,7 +561,7 @@ Account.prototype = {
lastMsgId = id; lastMsgId = id;
this._knownMessageIds[id] = tweet; this._knownMessageIds[id] = tweet;
if ("description" in tweet.user) if ("description" in tweet.user)
this._userInfo[tweet.user.screen_name] = tweet.user; this.setUserInfo(tweet.user);
this.timeline.displayTweet(tweet); this.timeline.displayTweet(tweet);
} }
if (lastMsgId != this._lastMsgId) { if (lastMsgId != this._lastMsgId) {
@ -671,14 +670,8 @@ Account.prototype = {
this._timelineBuffer.sort(this.sortByDate); this._timelineBuffer.sort(this.sortByDate);
this.displayMessages(this._timelineBuffer); this.displayMessages(this._timelineBuffer);
// Use the users' newest tweet as the topic. // Fetch userInfo for the user if we don't already have it.
for (let i = this._timelineBuffer.length - 1; i >= 0; --i) { this.requestBuddyInfo(this.name);
let tweet = this._timelineBuffer[i];
if (tweet.user.screen_name == this.name) {
this.timeline.setTopic(tweet.text, tweet.user.screen_name);
break;
}
}
// Reset in case we get disconnected // Reset in case we get disconnected
delete this._timelineBuffer; delete this._timelineBuffer;
@ -721,29 +714,32 @@ Account.prototype = {
ERROR(e + " while parsing " + message); ERROR(e + " while parsing " + message);
continue; continue;
} }
if ("text" in msg) { if ("text" in msg)
this.displayMessages([msg]); this.displayMessages([msg]);
// If the message is from us, set it as the topic.
if (("user" in msg) && (msg.user.screen_name == this.name))
this.timeline.setTopic(msg.text, msg.user.screen_name);
}
else if ("friends" in msg) else if ("friends" in msg)
this._friends = msg.friends; this._friends = msg.friends;
else if (("event" in msg) && msg.event == "follow") { else if ("event" in msg) {
let user, event; let user, event;
if (msg.source.screen_name == this.name) { switch(msg.event) {
this._friends.push(msg.target.id); case "follow":
user = msg.target; if (msg.source.screen_name == this.name) {
event = "follow"; this._friends.push(msg.target.id);
} user = msg.target;
else if (msg.target.screen_name == this.name) { event = "follow";
user = msg.source; }
event = "followed"; else if (msg.target.screen_name == this.name) {
} user = msg.source;
if (user) { event = "followed";
this._userInfo[user.screen_name] = user; }
this.timeline.systemMessage(_("event." + event, user.screen_name), if (user) {
false, new Date(msg.created_at) / 1000); this.setUserInfo(user);
this.timeline.systemMessage(_("event." + event, user.screen_name),
false, new Date(msg.created_at) / 1000);
}
break;
case "user_update":
this.setUserInfo(msg.target);
break;
} }
} }
} }
@ -938,9 +934,30 @@ Account.prototype = {
this.gotDisconnected(Ci.prplIAccount.ERROR_OTHER_ERROR, aException.toString()); this.gotDisconnected(Ci.prplIAccount.ERROR_OTHER_ERROR, aException.toString());
}, },
setUserDescription: function(aDescription) {
const kMaxUserDescriptionLength = 160;
if (aDescription.length > kMaxUserDescriptionLength) {
aDescription = aDescription.substr(0, kMaxUserDescriptionLength);
WARN("Description too long (over " + kMaxUserDescriptionLength +
" characters):\n" + aDescription + ".");
this.timeline.systemMessage(_("error.descriptionTooLong", aDescription));
}
// Don't need to catch the reply since the stream receives user_update.
this.signAndSend("1/account/update_profile.json", null,
[["description", aDescription]]);
},
setUserInfo: function(aUser) {
let nick = aUser.screen_name;
this._userInfo[nick] = aUser;
// If it's the user's userInfo, update the timeline topic.
if (nick == this.name && "description" in aUser)
this.timeline.setTopic(aUser.description, nick, true);
},
onRequestedInfoReceived: function(aData) { onRequestedInfoReceived: function(aData) {
let user = JSON.parse(aData); let user = JSON.parse(aData);
this._userInfo[user.screen_name] = user; this.setUserInfo(user);
this.requestBuddyInfo(user.screen_name); this.requestBuddyInfo(user.screen_name);
}, },
requestBuddyInfo: function(aBuddyName) { requestBuddyInfo: function(aBuddyName) {