Load emoticons through public API.

This commit is contained in:
flash 2024-11-14 03:34:55 +00:00
parent 4aa2b4cb1a
commit 3dff2e250f
4 changed files with 328 additions and 214 deletions

498
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -22,6 +22,21 @@ var FutamiCommon = function(vars) {
xhr.setRequestHeader('Cache-Control', 'no-cache'); xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.send(); xhr.send();
}, },
getApiJson: function(path, onload, onerror, noCache) {
if(typeof onload !== 'function')
throw 'onload must be specified';
var xhr = new XMLHttpRequest;
xhr.onload = function() {
onload(JSON.parse(xhr.responseText));
};
if(typeof onerror === 'function')
xhr.onerror = function() { onerror(); };
xhr.open('GET', get('api') + path);
if(noCache)
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.send();
},
}; };
}; };

View file

@ -188,9 +188,9 @@ var AmiContext = function(title, auth, loading) {
return; return;
} }
futami.getJson('emotes', function(emotes) { futami.getApiJson('/v1/emotes', function(emotes) {
if(Array.isArray(emotes)) if(Array.isArray(emotes))
emoticons.loadLegacy(emotes); emoticons.load(emotes);
chat.emoticonList.render(emoticons); chat.emoticonList.render(emoticons);
}); });
}); });

View file

@ -49,44 +49,31 @@ var AmiEmoticons = function() {
}; };
var add = function(emote) { var add = function(emote) {
emotes.push(emote); if(emote.url.substring(0, 6) === 'https:')
}; emote.url = emote.url.substring(6);
var addLegacy = function(emoteOld) { emotes.push(emote);
var emote = {
url: emoteOld.Image.replace('https:', ''),
minRank: emoteOld.Hierarchy,
strings: [],
};
for(var i = 0; i < emoteOld.Text.length; ++i)
emote.strings.push(emoteOld.Text[i].slice(1, -1));
add(emote);
}; };
return { return {
clear: clear, clear: clear,
add: add, add: add,
addLegacy: addLegacy,
load: function(batch) { load: function(batch) {
for(var i in batch) for(var i in batch)
add(batch[i]); add(batch[i]);
}, },
loadLegacy: function(batch) {
for(var i in batch)
addLegacy(batch[i]);
},
forEach: function(minRank, callback) { forEach: function(minRank, callback) {
if(minRank === undefined) minRank = 0; if(minRank === undefined) minRank = 0;
for(var i in emotes) { for(var i in emotes) {
var emote = emotes[i]; var emote = emotes[i];
if(emote.minRank <= minRank) if(!emote.min_rank || emote.min_rank <= minRank)
callback(emote); callback(emote);
} }
}, },
any: function(minRank) { any: function(minRank) {
if(minRank === undefined) minRank = 0; if(minRank === undefined) minRank = 0;
for(var i in emotes) for(var i in emotes)
if(emotes[i].minRank <= minRank) if(!emotes[i].min_rank || emotes[i].min_rank <= minRank)
return true; return true;
return false; return false;
}, },
@ -94,7 +81,7 @@ var AmiEmoticons = function() {
var found = []; var found = [];
for(var i in emotes) { for(var i in emotes) {
var emote = emotes[i]; var emote = emotes[i];
if(emote.minRank <= minRank) { if(!emote.min_rank || emote.min_rank <= minRank) {
for(var j in emote.strings) { for(var j in emote.strings) {
var string = emote.strings[j]; var string = emote.strings[j];
if(string.indexOf(name) === 0) { if(string.indexOf(name) === 0) {