Emotemodule refactor to use new builtin base

This commit is contained in:
Jiiks 2018-08-25 17:43:29 +03:00
parent a4cbfd9235
commit da8e912a95
1 changed files with 92 additions and 85 deletions

View File

@ -29,6 +29,10 @@ const EMOTE_SOURCES = [
export default new class EmoteModule extends BuiltinModule {
/* Getters */
get moduleName() { return 'EmoteModule' }
/**
* @returns {String} Path to local emote database
*/
@ -76,13 +80,14 @@ export default new class EmoteModule extends BuiltinModule {
// Read favourites and most used from database
await this.loadUserData();
this.patchMessageContent();
this.patchSendAndEdit();
const ImageWrapper = await ReactComponents.getComponent('ImageWrapper', { selector: Reflection.resolve('imageWrapper').selector });
MonkeyPatch('BD:EMOTEMODULE', ImageWrapper.component.prototype).after('render', this.beforeRenderImageWrapper.bind(this));
}
async disabled() {
DiscordContextMenu.remove(this.favCm);
}
/* Methods */
/**
* Adds an emote to favourites.
* @param {Object|String} emote
@ -115,12 +120,6 @@ export default new class EmoteModule extends BuiltinModule {
return !!this.favourites.find(e => e.name === emote || e.name === emote.name);
}
async disabled() {
// Unpatch all patches
for (const patch of Patcher.getPatchesByCaller('BD:EMOTEMODULE')) patch.unpatch();
DiscordContextMenu.remove(this.favCm);
}
/**
* Load emotes from local database
*/
@ -151,15 +150,96 @@ export default new class EmoteModule extends BuiltinModule {
});
}
/**
* Add/update emote to most used
* @param {Object} emote emote to add/update
* @return {Promise}
*/
addToMostUsed(emote) {
const isMostUsed = this.mostUsed.find(mu => mu.key === emote.name);
if (isMostUsed) {
isMostUsed.useCount += 1;
} else {
this.mostUsed.push({
key: emote.name,
id: emote.id,
type: emote.type,
useCount: 1
});
}
// Save most used to database
// TODO only save first n
return this.saveUserData();
}
/**
* Find an emote by name
* @param {String} name Emote name
* @param {Boolean} simple Simple object or Emote instance
* @returns {Object|Emote}
*/
findByName(name, simple = false) {
const emote = this.database.get(name);
if (!emote) return null;
return this.parseEmote(name, emote, simple);
}
/**
* Parse emote object
* @param {String} name Emote name
* @param {Object} emote Emote object
* @param {Boolean} simple Simple object or Emote instance
* @returns {Object|Emote}
*/
parseEmote(name, emote, simple = false) {
const { type, id } = emote;
if (type < 0 || type > 2) return null;
return simple ? { type, id, name } : new Emote(type, id, name);
}
/**
* Search for anything else
* @param {any} regex
* @param {any} limit
*/
search(regex, limit = 10) {
if (typeof regex === 'string') regex = new RegExp(regex, 'i');
const matching = [];
for (const [key, value] of this.database.entries()) {
if (matching.length >= limit) break;
if (regex.test(key)) matching.push({ key, value })
}
return matching;
}
/* Patches */
async applyPatches() {
this.patchMessageContent();
this.patchSendAndEdit();
const ImageWrapper = await ReactComponents.getComponent('ImageWrapper', { selector: Reflection.resolve('imageWrapper').selector });
MonkeyPatch('BD:EMOTEMODULE', ImageWrapper.component.prototype).after('render', this.beforeRenderImageWrapper.bind(this));
}
/**
* Patches MessageContent render method
*/
async patchMessageContent() {
const MessageContent = await ReactComponents.getComponent('MessageContent', { selector: Reflection.resolve('container', 'containerCozy', 'containerCompact', 'edited').selector });
MonkeyPatch('BD:EMOTEMODULE', MessageContent.component.prototype).after('render', this.afterRenderMessageContent.bind(this));
this.patch(MessageContent.component.prototype, 'render', this.afterRenderMessageContent);
MessageContent.forceUpdateAll();
}
/**
* Patches MessageActions send and edit
*/
patchSendAndEdit() {
const { MessageActions } = Reflection.modules;
this.patch(MessageActions, 'sendMessage', this.handleSendMessage, 'instead');
this.patch(MessageActions, 'editMessage', this.handleEditMessage, 'instead');
}
/**
* Handle message render
*/
@ -173,15 +253,6 @@ export default new class EmoteModule extends BuiltinModule {
markup.children[1] = this.processMarkup(markup.children[1]);
}
/**
* Patches MessageActions send and edit
*/
patchSendAndEdit() {
const { MessageActions } = Reflection.modules;
MonkeyPatch('BD:EMOTEMODULE', MessageActions).instead('sendMessage', this.handleSendMessage.bind(this));
MonkeyPatch('BD:EMOTEMODULE', MessageActions).instead('editMessage', this.handleEditMessage.bind(this));
}
/**
* Handle send message
*/
@ -253,28 +324,6 @@ export default new class EmoteModule extends BuiltinModule {
retVal.props.children = emote.render();
}
/**
* Add/update emote to most used
* @param {Object} emote emote to add/update
* @return {Promise}
*/
addToMostUsed(emote) {
const isMostUsed = this.mostUsed.find(mu => mu.key === emote.name);
if (isMostUsed) {
isMostUsed.useCount += 1;
} else {
this.mostUsed.push({
key: emote.name,
id: emote.id,
type: emote.type,
useCount: 1
});
}
// Save most used to database
// TODO only save first n
return this.saveUserData();
}
/**
* Inject emotes into markup
*/
@ -330,46 +379,4 @@ export default new class EmoteModule extends BuiltinModule {
return newMarkup;
}
/**
* Find an emote by name
* @param {String} name Emote name
* @param {Boolean} simple Simple object or Emote instance
* @returns {Object|Emote}
*/
findByName(name, simple = false) {
const emote = this.database.get(name);
if (!emote) return null;
return this.parseEmote(name, emote, simple);
}
/**
* Parse emote object
* @param {String} name Emote name
* @param {Object} emote Emote object
* @param {Boolean} simple Simple object or Emote instance
* @returns {Object|Emote}
*/
parseEmote(name, emote, simple = false) {
const { type, id } = emote;
if (type < 0 || type > 2) return null;
return simple ? { type, id, name } : new Emote(type, id, name);
}
/**
* Search for anything else
* @param {any} regex
* @param {any} limit
*/
search(regex, limit = 10) {
if (typeof regex === 'string') regex = new RegExp(regex, 'i');
const matching = [];
for (const [key, value] of this.database.entries()) {
if (matching.length >= limit) break;
if (regex.test(key)) matching.push({ key, value })
}
return matching;
}
}