Skip to content

Commit

Permalink
Add Emote.get/setChannelDisplayName methods
Browse files Browse the repository at this point in the history
Allows actual setting of the display name rather than relying solely on
the API. This also allows third parties to force a display name where
they know what they want the display name to be, e.g. `BetterTTV Global
Emotes` to help avoid hitting the API if we don't need to.
  • Loading branch information
cletusc committed May 12, 2016
1 parent 2dc20d9 commit 6fba769
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 104 deletions.
6 changes: 3 additions & 3 deletions script.min.js

Large diffs are not rendered by default.

122 changes: 71 additions & 51 deletions script.user.js

Large diffs are not rendered by default.

120 changes: 70 additions & 50 deletions src/modules/emotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ function Emote(details) {
var getterName = null;
var channel = {
name: null,
displayName: null,
badge: null
};

Expand Down Expand Up @@ -407,6 +408,72 @@ function Emote(details) {
channel.badge = theBadge;
};

/**
* Get a channel's display name.
* @return {string} The channel's display name. May be equivalent to the channel the first time the API needs to be called.
*/
this.getChannelDisplayName = function () {
var twitchApi = require('./twitch-api');
var channelName = this.getChannelName();
var self = this;

var forcedChannelToDisplayNames = {
'~global': 'Global',
'turbo': 'Turbo'
};

// No channel.
if (!channelName) {
return null;
}

// Forced display name.
if (forcedChannelToDisplayNames[channelName]) {
return forcedChannelToDisplayNames[channelName];
}

// Already have one preset.
if (channel.displayName) {
return channel.displayName;
}

// Check storage.
channel.displayName = storage.displayNames.get(channelName);
if (channel.displayName !== null) {
return channel.displayName;
}
// Get from API.
else {
// Set default until API returns something.
channel.displayName = channelName;

logger.debug('Getting fresh display name for: ' + channelName);
twitchApi.getUser(channelName, function (user) {
if (!user || !user.display_name) {
logger.debug('Failed to get display name for: ' + channelName);
return;
}

// Save it.
self.setChannelDisplayName(user.display_name);
});
}

return channel.displayName;
};

/**
* Sets the emote's channel badge image URL.
* @param {string} theBadge The badge image URL to set.
*/
this.setChannelDisplayName = function (displayName) {
if (typeof displayName !== 'string' || displayName.length < 1) {
throw new Error('Invalid displayName');
}
channel.displayName = displayName;
storage.displayNames.set(this.getChannelName(), displayName, 86400000);
};

/**
* Initialize the details.
*/
Expand All @@ -422,6 +489,9 @@ function Emote(details) {
if (details.channel) {
this.setChannelName(details.channel);
}
if (details.channelDisplayName) {
this.setChannelDisplayName(details.channelDisplayName);
}
if (details.badge) {
this.setChannelBadge(details.badge);
}
Expand Down Expand Up @@ -497,56 +567,6 @@ Emote.prototype.isSmiley = function () {
* Property getters/setters.
*/

/**
* Get a channel's display name.
* @return {string} The channel's display name. May be equivalent to the channel the first time the API needs to be called.
*/
Emote.prototype.getChannelDisplayName = function () {
var twitchApi = require('./twitch-api');
var channelName = this.getChannelName();
var displayName = null;

var forcedChannelToDisplayNames = {
'~global': 'Global',
'turbo': 'Turbo'
};

// No channel.
if (!channelName) {
return null;
}

// Forced display name.
if (forcedChannelToDisplayNames[channelName]) {
return forcedChannelToDisplayNames[channelName];
}

// Check storage.
displayName = storage.displayNames.get(channelName);
if (displayName !== null) {
return displayName;
}
// Get from API.
else {
// Set default until API returns something.
storage.displayNames.set(channelName, channelName, 86400000);

logger.debug('Getting fresh display name for: ' + channelName);
twitchApi.getUser(channelName, function (user) {
if (!user || !user.display_name) {
logger.debug('Failed to get display name for: ' + channelName);
return;
}

displayName = user.display_name;
// Save in storage.
storage.displayNames.set(channelName, displayName, 86400000);
});
}

return displayName || channelName;
};

/**
* Gets the usable emote text from a regex.
*/
Expand Down

0 comments on commit 6fba769

Please sign in to comment.