Store and Model Refactor (#1618)

* UserStore refactor

* Create ChannelStore, remove redundant methods in ClientDataManager

* Create GuildStore

* Emoji stuff

* Use a Base class where possible to reduce code duplication

* Remove unnecessary comments from ChannelStore

* Add Base._clone();

* Remove unused ClientDataManager methods

* Refactor some more stuff

* ESLint

* Move Client#fetchUser to client.users.fetch

* Remove .has checks and just see if .get is truthy

* Fix guild member chunk error

* ESLint

* Fix typo

* Fix channel storing for user bots

* Remove ClientDataManager

* GuildChannelStore

* Reduce use of Util.cloneObject

* and this one too

* update typings

* Fix MessageUpdate handling (#1507)

* Fix role updates (probably fixes #1525)

* fix for eslint

* Address some of appell's comments

* Use debug constant

* start message store crap if it's ugly tell me later k

* fix that

* message store but works™️

* clean up guild stuff

* clean up channel store stuff

* clean up channel event handling

* does this message stuff work? find out soon in the next episode of dIsCoRd.Js

* eslint

* emojis

* emojis and reactions

* hi my name is eslint and im A LIL SHIT

* so i forgot this huh

* user stuff

* Fix @class

* Fix message stuff

* Fix user store docs

* Document all the bases

* fix the super things

* tidy up remove

* fix textbasedchannel

* fix that too

* fix emoji store

* make voice state stuff less ugly

* make voice states even less ugly

* make members less bad

* fix bug

* fix that too

* fix reactions

* how was this broken for so long

* role store

* remove super._patch from UserConnection

* Rename UserProfile#setup to _patch

* remove unnecessary super calls

* update docgen dep (pls fix travis thx)

* doc messagestore

* fix docs

* message store docs

* things

* DOCS PLS

* more things

* Document TextBasedChannel#messages as a MessageStore

* Rebase

* All the stores!
This commit is contained in:
Amish Shah
2017-08-25 21:08:58 +01:00
committed by GitHub
parent 243ff48a67
commit b8315b79c7
80 changed files with 837 additions and 840 deletions

View File

@@ -1,9 +1,14 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
class ChannelCreateAction extends Action {
handle(data) {
const client = this.client;
const channel = client.dataManager.newChannel(data);
const existing = client.channels.has(data.id);
const channel = client.channels.create(data);
if (!existing && channel) {
client.emit(Constants.Events.CHANNEL_CREATE, channel);
}
return { channel };
}
}

View File

@@ -1,4 +1,5 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
class ChannelDeleteAction extends Action {
constructor(client) {
@@ -8,22 +9,21 @@ class ChannelDeleteAction extends Action {
handle(data) {
const client = this.client;
let channel = client.channels.get(data.id);
if (channel) {
client.dataManager.killChannel(channel);
this.deleted.set(channel.id, channel);
this.scheduleForDeletion(channel.id);
} else {
channel = this.deleted.get(data.id) || null;
client.channels.remove(channel.id);
client.emit(Constants.Events.CHANNEL_DELETE, channel);
}
return { channel };
}
scheduleForDeletion(id) {
this.client.setTimeout(() => this.deleted.delete(id), this.client.options.restWsBridgeTimeout);
}
}
/**
* Emitted whenever a channel is deleted.
* @event Client#channelDelete
* @param {Channel} channel The channel that was deleted
*/
module.exports = ChannelDeleteAction;

View File

@@ -1,6 +1,4 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
const Util = require('../../util/Util');
class ChannelUpdateAction extends Action {
handle(data) {
@@ -8,27 +6,15 @@ class ChannelUpdateAction extends Action {
const channel = client.channels.get(data.id);
if (channel) {
const oldChannel = Util.cloneObject(channel);
channel.setup(data);
client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, channel);
const old = channel._update(data);
return {
old: oldChannel,
old,
updated: channel,
};
}
return {
old: null,
updated: null,
};
return {};
}
}
/**
* Emitted whenever a channel is updated - e.g. name change, topic change.
* @event Client#channelUpdate
* @param {Channel} oldChannel The channel before the update
* @param {Channel} newChannel The channel after the update
*/
module.exports = ChannelUpdateAction;

View File

@@ -5,7 +5,7 @@ class GuildBanRemove extends Action {
handle(data) {
const client = this.client;
const guild = client.guilds.get(data.guild_id);
const user = client.dataManager.newUser(data.user);
const user = client.users.create(data.user);
if (guild && user) client.emit(Constants.Events.GUILD_BAN_REMOVE, guild, user);
}
}

View File

@@ -29,7 +29,8 @@ class GuildDeleteAction extends Action {
}
// Delete guild
client.guilds.delete(guild.id);
client.guilds.remove(guild.id);
client.emit(Constants.Events.GUILD_DELETE, guild);
this.deleted.set(guild.id, guild);
this.scheduleForDeletion(guild.id);
} else {

View File

@@ -1,9 +1,10 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
class GuildEmojiCreateAction extends Action {
handle(guild, createdEmoji) {
const client = this.client;
const emoji = client.dataManager.newEmoji(createdEmoji, guild);
const emoji = guild.emojis.create(createdEmoji);
this.client.emit(Constants.Events.GUILD_EMOJI_CREATE, emoji);
return { emoji };
}
}

View File

@@ -1,9 +1,10 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
class GuildEmojiDeleteAction extends Action {
handle(emoji) {
const client = this.client;
client.dataManager.killEmoji(emoji);
emoji.guild.emojis.remove(emoji.id);
this.client.emit(Constants.Events.GUILD_EMOJI_DELETE, emoji);
return { emoji };
}
}

View File

@@ -1,9 +1,11 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
class GuildEmojiUpdateAction extends Action {
handle(oldEmoji, newEmoji) {
const emoji = this.client.dataManager.updateEmoji(oldEmoji, newEmoji);
return { emoji };
handle(current, data) {
const old = current._update(data);
this.client.emit(Constants.Events.GUILD_EMOJI_UPDATE, old, current);
return { emoji: current };
}
}

View File

@@ -2,7 +2,7 @@ const Action = require('./Action');
class GuildMemberGetAction extends Action {
handle(guild, data) {
const member = guild._addMember(data, false);
const member = guild.members.create(data);
return { member };
}
}

View File

@@ -2,11 +2,6 @@ const Action = require('./Action');
const Constants = require('../../util/Constants');
class GuildMemberRemoveAction extends Action {
constructor(client) {
super(client);
this.deleted = new Map();
}
handle(data) {
const client = this.client;
const guild = client.guilds.get(data.guild_id);
@@ -15,20 +10,12 @@ class GuildMemberRemoveAction extends Action {
member = guild.members.get(data.user.id);
if (member) {
guild.memberCount--;
guild._removeMember(member);
this.deleted.set(guild.id + data.user.id, member);
guild.members.remove(member.id);
if (client.status === Constants.Status.READY) client.emit(Constants.Events.GUILD_MEMBER_REMOVE, member);
this.scheduleForDeletion(guild.id, data.user.id);
} else {
member = this.deleted.get(guild.id + data.user.id) || null;
}
}
return { guild, member };
}
scheduleForDeletion(guildID, userID) {
this.client.setTimeout(() => this.deleted.delete(guildID + userID), this.client.options.restWsBridgeTimeout);
}
}
/**

View File

@@ -1,6 +1,5 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
const Role = require('../../structures/Role');
class GuildRoleCreate extends Action {
handle(data) {
@@ -9,8 +8,7 @@ class GuildRoleCreate extends Action {
let role;
if (guild) {
const already = guild.roles.has(data.role.id);
role = new Role(guild, data.role);
guild.roles.set(role.id, role);
role = guild.roles.create(data.role);
if (!already) client.emit(Constants.Events.GUILD_ROLE_CREATE, role);
}
return { role };

View File

@@ -2,11 +2,6 @@ const Action = require('./Action');
const Constants = require('../../util/Constants');
class GuildRoleDeleteAction extends Action {
constructor(client) {
super(client);
this.deleted = new Map();
}
handle(data) {
const client = this.client;
const guild = client.guilds.get(data.guild_id);
@@ -15,21 +10,13 @@ class GuildRoleDeleteAction extends Action {
if (guild) {
role = guild.roles.get(data.role_id);
if (role) {
guild.roles.delete(data.role_id);
this.deleted.set(guild.id + data.role_id, role);
this.scheduleForDeletion(guild.id, data.role_id);
guild.roles.remove(data.role_id);
client.emit(Constants.Events.GUILD_ROLE_DELETE, role);
} else {
role = this.deleted.get(guild.id + data.role_id) || null;
}
}
return { role };
}
scheduleForDeletion(guildID, roleID) {
this.client.setTimeout(() => this.deleted.delete(guildID + roleID), this.client.options.restWsBridgeTimeout);
}
}
/**

View File

@@ -1,6 +1,5 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
const Util = require('../../util/Util');
class GuildRoleUpdateAction extends Action {
handle(data) {
@@ -8,18 +7,16 @@ class GuildRoleUpdateAction extends Action {
const guild = client.guilds.get(data.guild_id);
if (guild) {
const roleData = data.role;
let oldRole = null;
let old = null;
const role = guild.roles.get(roleData.id);
const role = guild.roles.get(data.role.id);
if (role) {
oldRole = Util.cloneObject(role);
role.setup(data.role);
client.emit(Constants.Events.GUILD_ROLE_UPDATE, oldRole, role);
old = role._update(data.role);
client.emit(Constants.Events.GUILD_ROLE_UPDATE, old, role);
}
return {
old: oldRole,
old,
updated: role,
};
}

View File

@@ -14,9 +14,9 @@ class GuildSync extends Action {
for (const syncMember of data.members) {
const member = guild.members.get(syncMember.user.id);
if (member) {
guild._updateMember(member, syncMember);
member._patch(syncMember);
} else {
guild._addMember(syncMember, false);
guild.members.create(syncMember, false);
}
}
}

View File

@@ -1,6 +1,5 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
const Util = require('../../util/Util');
class GuildUpdateAction extends Action {
handle(data) {
@@ -8,11 +7,10 @@ class GuildUpdateAction extends Action {
const guild = client.guilds.get(data.id);
if (guild) {
const oldGuild = Util.cloneObject(guild);
guild.setup(data);
client.emit(Constants.Events.GUILD_UPDATE, oldGuild, guild);
const old = guild._update(data);
client.emit(Constants.Events.GUILD_UPDATE, old, guild);
return {
old: oldGuild,
old,
updated: guild,
};
}

View File

@@ -1,55 +1,39 @@
const Action = require('./Action');
const Message = require('../../structures/Message');
const Constants = require('../../util/Constants');
class MessageCreateAction extends Action {
handle(data) {
const client = this.client;
const channel = client.channels.get((data instanceof Array ? data[0] : data).channel_id);
const user = client.users.get((data instanceof Array ? data[0] : data).author.id);
const channel = client.channels.get(data.channel_id);
const user = client.users.get(data.author.id);
if (channel) {
const existing = channel.messages.get(data.id);
if (existing) return { message: existing };
const member = channel.guild ? channel.guild.member(user) : null;
if (data instanceof Array) {
const messages = new Array(data.length);
for (let i = 0; i < data.length; i++) {
messages[i] = channel._cacheMessage(new Message(channel, data[i], client));
}
const lastMessage = messages[messages.length - 1];
channel.lastMessageID = lastMessage.id;
channel.lastMessage = lastMessage;
if (user) {
user.lastMessageID = lastMessage.id;
user.lastMessage = lastMessage;
}
if (member) {
member.lastMessageID = lastMessage.id;
member.lastMessage = lastMessage;
}
return {
messages,
};
} else {
const message = channel._cacheMessage(new Message(channel, data, client));
channel.lastMessageID = data.id;
channel.lastMessage = message;
if (user) {
user.lastMessageID = data.id;
user.lastMessage = message;
}
if (member) {
member.lastMessageID = data.id;
member.lastMessage = message;
}
return {
message,
};
const message = channel.messages.create(data);
channel.lastMessageID = data.id;
channel.lastMessage = message;
if (user) {
user.lastMessageID = data.id;
user.lastMessage = message;
}
if (member) {
member.lastMessageID = data.id;
member.lastMessage = message;
}
client.emit(Constants.Events.MESSAGE_CREATE, message);
return { message };
}
return {
message: null,
};
return {};
}
}
/**
* Emitted whenever a message is created.
* @event Client#message
* @param {Message} message The created message
*/
module.exports = MessageCreateAction;

View File

@@ -1,11 +1,7 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
class MessageDeleteAction extends Action {
constructor(client) {
super(client);
this.deleted = new Map();
}
handle(data) {
const client = this.client;
const channel = client.channels.get(data.channel_id);
@@ -15,20 +11,18 @@ class MessageDeleteAction extends Action {
message = channel.messages.get(data.id);
if (message) {
channel.messages.delete(message.id);
this.deleted.set(channel.id + message.id, message);
this.scheduleForDeletion(channel.id, message.id);
} else {
message = this.deleted.get(channel.id + data.id) || null;
client.emit(Constants.Events.MESSAGE_DELETE, message);
}
}
return { message };
}
scheduleForDeletion(channelID, messageID) {
this.client.setTimeout(() => this.deleted.delete(channelID + messageID),
this.client.options.restWsBridgeTimeout);
}
}
/**
* Emitted whenever a message is deleted.
* @event Client#messageDelete
* @param {Message} message The deleted message
*/
module.exports = MessageDeleteAction;

View File

@@ -7,16 +7,25 @@ class MessageDeleteBulkAction extends Action {
const client = this.client;
const channel = client.channels.get(data.channel_id);
const ids = data.ids;
const messages = new Collection();
for (const id of ids) {
const message = channel.messages.get(id);
if (message) messages.set(message.id, message);
}
if (channel) {
const ids = data.ids;
const messages = new Collection();
for (const id of ids) {
const message = channel.messages.get(id);
if (message) messages.set(message.id, message);
}
if (messages.size > 0) client.emit(Constants.Events.MESSAGE_BULK_DELETE, messages);
return { messages };
if (messages.size > 0) client.emit(Constants.Events.MESSAGE_BULK_DELETE, messages);
return { messages };
}
return {};
}
}
/**
* Emitted whenever messages are deleted in bulk.
* @event Client#messageDeleteBulk
* @param {Collection<Snowflake, Message>} messages The deleted messages, mapped by their ID
*/
module.exports = MessageDeleteBulkAction;

View File

@@ -1,5 +1,4 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
/*
{ user_id: 'id',
@@ -10,19 +9,22 @@ const Constants = require('../../util/Constants');
class MessageReactionAdd extends Action {
handle(data) {
const user = this.client.users.get(data.user_id);
const user = data.user || this.client.users.get(data.user_id);
if (!user) return false;
// Verify channel
const channel = this.client.channels.get(data.channel_id);
const channel = data.channel || this.client.channels.get(data.channel_id);
if (!channel || channel.type === 'voice') return false;
// Verify message
const message = channel.messages.get(data.message_id);
const message = data.message || channel.messages.get(data.message_id);
if (!message) return false;
if (!data.emoji) return false;
// Verify reaction
const reaction = message._addReaction(data.emoji, user);
if (reaction) this.client.emit(Constants.Events.MESSAGE_REACTION_ADD, reaction, user);
const reaction = message.reactions.create({
emoji: data.emoji,
count: 0,
me: user.id === this.client.user.id,
});
reaction._add(user);
return { message, reaction, user };
}
}

View File

@@ -20,8 +20,11 @@ class MessageReactionRemove extends Action {
if (!message) return false;
if (!data.emoji) return false;
// Verify reaction
const reaction = message._removeReaction(data.emoji, user);
if (reaction) this.client.emit(Constants.Events.MESSAGE_REACTION_REMOVE, reaction, user);
const emojiID = data.emoji.id || decodeURIComponent(data.emoji.name);
const reaction = message.reactions.get(emojiID);
if (!reaction) return false;
reaction._remove(user);
this.client.emit(Constants.Events.MESSAGE_REACTION_REMOVE, reaction, user);
return { message, reaction, user };
}

View File

@@ -9,7 +9,7 @@ class MessageReactionRemoveAll extends Action {
const message = channel.messages.get(data.message_id);
if (!message) return false;
message._clearReactions();
message.reactions.clear();
this.client.emit(Constants.Events.MESSAGE_REACTION_REMOVE_ALL, message);
return { message };

View File

@@ -1,5 +1,4 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
class MessageUpdateAction extends Action {
handle(data) {
@@ -10,31 +9,15 @@ class MessageUpdateAction extends Action {
const message = channel.messages.get(data.id);
if (message) {
message.patch(data);
client.emit(Constants.Events.MESSAGE_UPDATE, message._edits[0], message);
return {
old: message._edits[0],
updated: message,
};
}
return {
old: message,
updated: message,
};
}
return {
old: null,
updated: null,
};
return {};
}
}
/**
* Emitted whenever a message is updated - e.g. embed or content change.
* @event Client#messageUpdate
* @param {Message} oldMessage The message before the update
* @param {Message} newMessage The message after the update
*/
module.exports = MessageUpdateAction;

View File

@@ -3,7 +3,7 @@ const Action = require('./Action');
class UserGetAction extends Action {
handle(data) {
const client = this.client;
const user = client.dataManager.newUser(data);
const user = client.users.create(data);
return { user };
}
}

View File

@@ -1,6 +1,5 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
const Util = require('../../util/Util');
class UserUpdateAction extends Action {
handle(data) {
@@ -14,8 +13,7 @@ class UserUpdateAction extends Action {
};
}
const oldUser = Util.cloneObject(client.user);
client.user.patch(data);
const oldUser = client.user._update(data);
client.emit(Constants.Events.USER_UPDATE, oldUser, client.user);
return {
old: oldUser,