Skip to content

Commit

Permalink
refactor/fix(DataStores): move instantiating of store related classes…
Browse files Browse the repository at this point in the history
… into their date stores (#1869)

* refactor/fix(DataStores): move instantiating of store related classes into the relevant create methods and fixed FetchMemberOptions

* Renamed ChannelStore's constructor parameter for claritiy

* define client property before creating items from passed iterable to avoid the client property being undefined

* Fix fetching members with a limit

* add static Channel#create back and moved channel instantiating there

* make ChannelStore's constructor accept an iterable

* make iterable an optional parameter, allow to just pass client and options

* fixed a little typo in docs of <User>.displayAvatarURL() (#1872)

* Rewrite presence a little bit (#1853)

* such presence many good

* Update PresenceStore.js

* Update index.js

* Update ClientPresenceStore.js

* Update Presence.js

* Update ClientPresenceStore.js

* Update ClientUser.js

* Update Presence.js

* add timestamps and party

* Update Presence.js

* Update PresenceStore.js

* Update ClientPresenceStore.js

* Update ClientPresenceStore.js

* fix: made options.type optional in ClientUser#setActivity (#1875)

* fix: made options.type optional in ClientUser#setActivity

* some changes, smol fix

due to too many types being possible, it should just be defaulted to 0. this means streamers will have to set the type manually, though.
also mistake with activity.name check

* docs(Collection): Adjust exists documentation (#1876)

Since the return value of Collection#exists is basically just a boolean of Collection#find's result, the same documentation and arguments can and should be applied.

* refactor(Emoji): remove Guild#deleteEmoji in favour of Emoji#delete (#1877)

* refactor/fix(DataStores): move instantiating of store related classes into the relevant create methods and fixed FetchMemberOptions

* Renamed ChannelStore's constructor parameter for claritiy

* define client property before creating items from passed iterable to avoid the client property being undefined

* Fix fetching members with a limit

* add static Channel#create back and moved channel instantiating there

* make ChannelStore's constructor accept an iterable

* make iterable an optional parameter, allow to just pass client and options

* rebase, this time with saving the file

* address issue with falsy activity throwing errors when setting client's presence
  • Loading branch information
SpaceEEC authored and iCrawl committed Sep 3, 2017
1 parent c93c4ad commit c4b5736
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 71 deletions.
32 changes: 12 additions & 20 deletions src/stores/ChannelStore.js
@@ -1,6 +1,5 @@
const DataStore = require('./DataStore');
const DMChannel = require('../structures/DMChannel');
const GroupDMChannel = require('../structures/GroupDMChannel');
const Channel = require('../structures/Channel');
const Constants = require('../util/Constants');

const kLru = Symbol('LRU');
Expand All @@ -12,8 +11,12 @@ const lruable = ['group', 'dm'];
* @extends {DataStore}
*/
class ChannelStore extends DataStore {
constructor(iterable, options = {}) {
super(iterable);
constructor(client, iterableOrOptions = {}, options) {
if (!options && typeof iterableOrOptions[Symbol.iterator] !== 'function') {
options = iterableOrOptions;
iterableOrOptions = undefined;
}
super(client, iterableOrOptions);

if (options.lru) {
const lru = this[kLru] = [];
Expand Down Expand Up @@ -52,22 +55,11 @@ class ChannelStore extends DataStore {
const existing = this.get(data.id);
if (existing) return existing;

let channel;
switch (data.type) {
case Constants.ChannelTypes.DM:
channel = new DMChannel(this.client, data);
break;
case Constants.ChannelTypes.GROUP:
channel = new GroupDMChannel(this.client, data);
break;
default: // eslint-disable-line no-case-declarations
guild = guild || this.client.guilds.get(data.guild_id);
if (!guild) {
this.client.emit(Constants.Events.DEBUG, `Failed to find guild for channel ${data.id} ${data.type}`);
return null;
}
channel = guild.channels.create(data, cache);
break;
const channel = Channel.create(this.client, data, guild);

if (!channel) {
this.client.emit(Constants.Events.DEBUG, `Failed to find guild for channel ${data.id} ${data.type}`);
return null;
}

if (cache) this.set(channel.id, channel);
Expand Down
16 changes: 9 additions & 7 deletions src/stores/ClientPresenceStore.js
Expand Up @@ -16,15 +16,17 @@ class ClientPresenceStore extends PresenceStore {
}

async setClientPresence({ status, since, afk, activity }) { // eslint-disable-line complexity
if (typeof activity.name !== 'string') throw new TypeError('INVALID_TYPE', 'name', 'string');
if (!activity.type) activity.type = 0;
const applicationID = activity && (activity.application ? activity.application.id || activity.application : null);
let assets = new Collection();
if (activity && activity.assets && applicationID) {
try {
const a = await this.client.api.oauth2.applications(applicationID).assets.get();
for (const asset of a) assets.set(asset.name, asset.id);
} catch (err) {} // eslint-disable-line no-empty
if (activity) {
if (typeof activity.name !== 'string') throw new TypeError('INVALID_TYPE', 'name', 'string');
if (!activity.type) activity.type = 0;
if (activity.assets && applicationID) {
try {
const a = await this.client.api.oauth2.applications(applicationID).assets.get();
for (const asset of a) assets.set(asset.name, asset.id);
} catch (err) { } // eslint-disable-line no-empty
}
}

const packet = {
Expand Down
2 changes: 1 addition & 1 deletion src/stores/DataStore.js
Expand Up @@ -7,8 +7,8 @@ const Collection = require('../util/Collection');
class DataStore extends Collection {
constructor(client, iterable) {
super();
if (iterable) for (const item of iterable) this.create(item);
Object.defineProperty(this, 'client', { value: client });
if (iterable) for (const item of iterable) this.create(item);
}

// Stubs
Expand Down
13 changes: 4 additions & 9 deletions src/stores/GuildChannelStore.js
@@ -1,7 +1,6 @@
const DataStore = require('./DataStore');
const TextChannel = require('../structures/TextChannel');
const VoiceChannel = require('../structures/VoiceChannel');
const Constants = require('../util/Constants');
const Channel = require('../structures/Channel');

/**
* Stores guild channels.
* @private
Expand All @@ -13,15 +12,11 @@ class GuildChannelStore extends DataStore {
this.guild = guild;
}

create(data, cache = true) {
create(data) {
const existing = this.get(data.id);
if (existing) return existing;

const ChannelModel = data.type === Constants.ChannelTypes.TEXT ? TextChannel : VoiceChannel;
const channel = new ChannelModel(this.guild, data);
if (cache) this.set(channel.id, channel);

return channel;
return Channel.create(this.client, data, this.guild);
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/stores/GuildMemberStore.js
Expand Up @@ -2,9 +2,10 @@ const DataStore = require('./DataStore');
const GuildMember = require('../structures/GuildMember');
const Constants = require('../util/Constants');
const Collection = require('../util/Collection');
const { Error } = require('../errors');

/**
* Stores guild members.
* @private
* @extends {DataStore}
*/
class GuildMemberStore extends DataStore {
Expand All @@ -13,12 +14,12 @@ class GuildMemberStore extends DataStore {
this.guild = guild;
}

create(data) {
create(data, cache = true) {
const existing = this.get(data.user.id);
if (existing) return existing;

const member = new GuildMember(this.guild, data);
this.set(member.id, member);
if (cache) this.set(member.id, member);

return member;
}
Expand Down Expand Up @@ -66,7 +67,7 @@ class GuildMemberStore extends DataStore {
const user = this.client.resolver.resolveUserID(options);
if (user) return this._fetchSingle({ user, cache: true });
if (options.user) {
options.user = this.client.resolver.resolveUser(options);
options.user = this.client.resolver.resolveUserID(options.user);
if (options.user) return this._fetchSingle(options);
}
return this._fetchMany(options);
Expand All @@ -75,10 +76,7 @@ class GuildMemberStore extends DataStore {
_fetchSingle({ user, cache }) {
if (this.has(user)) return Promise.resolve(this.get(user));
return this.client.api.guilds(this.guild.id).members(user).get()
.then(data => {
if (cache) return this.create(data);
else return new GuildMember(this, data);
});
.then(data => this.create(data, cache));
}

_fetchMany({ query = '', limit = 0 } = {}) {
Expand All @@ -99,9 +97,11 @@ class GuildMemberStore extends DataStore {
const handler = (members, guild) => {
if (guild.id !== this.guild.id) return;
for (const member of members.values()) {
if (query || limit) fetchedMembers.set(member.user.id, member);
if (query || limit) fetchedMembers.set(member.id, member);
}
if (this.guild.memberCount === this.size || ((query || limit) && members.size < 1000)) {
if (this.guild.memberCount <= this.size ||
((query || limit) && members.size < 1000) ||
(limit && fetchedMembers.size >= limit)) {
this.guild.client.removeListener(Constants.Events.GUILD_MEMBERS_CHUNK, handler);
resolve(query || limit ? fetchedMembers : this);
}
Expand Down
4 changes: 2 additions & 2 deletions src/stores/GuildStore.js
Expand Up @@ -6,12 +6,12 @@ const Guild = require('../structures/Guild');
* @extends {DataStore}
*/
class GuildStore extends DataStore {
create(data) {
create(data, cache = true) {
const existing = this.get(data.id);
if (existing) return existing;

const guild = new Guild(this.client, data);
this.set(guild.id, guild);
if (cache) this.set(guild.id, guild);

return guild;
}
Expand Down
8 changes: 3 additions & 5 deletions src/stores/UserStore.js
Expand Up @@ -6,12 +6,12 @@ const User = require('../structures/User');
* @extends {DataStore}
*/
class UserStore extends DataStore {
create(data) {
create(data, cache = true) {
const existing = this.get(data.id);
if (existing) return existing;

const user = new User(this.client, data);
this.set(user.id, user);
if (cache) this.set(user.id, user);
return user;
}

Expand All @@ -26,9 +26,7 @@ class UserStore extends DataStore {
const existing = this.get(id);
if (existing) return Promise.resolve(existing);

return this.client.api.users(id).get().then(data =>
cache ? this.create(data) : new User(this.client, data)
);
return this.client.api.users(id).get().then(data => this.create(data, cache));
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/structures/ClientUser.js
Expand Up @@ -5,8 +5,6 @@ const ClientUserGuildSettings = require('./ClientUserGuildSettings');
const Constants = require('../util/Constants');
const Util = require('../util/Util');
const Guild = require('./Guild');
const Message = require('./Message');
const GroupDMChannel = require('./GroupDMChannel');

/**
* Represents the logged in client's Discord user.
Expand Down Expand Up @@ -258,7 +256,7 @@ class ClientUser extends User {
Util.mergeDefault({ limit: 25, roles: true, everyone: true, guild: null }, options);

return this.client.api.users('@me').mentions.get({ query: options })
.then(data => data.map(m => new Message(this.client.channels.get(m.channel_id), m, this.client)));
.then(data => data.map(m => this.client.channels.get(m.channel_id).messages.create(m, false)));
}

/**
Expand Down Expand Up @@ -324,7 +322,7 @@ class ClientUser extends User {
}, {}),
} : { recipients: recipients.map(u => this.client.resolver.resolveUserID(u.user || u.id)) };
return this.client.api.users('@me').channels.post({ data })
.then(res => new GroupDMChannel(this.client, res));
.then(res => this.client.channels.create(res));
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/structures/Invite.js
Expand Up @@ -13,13 +13,11 @@ class Invite extends Base {
}

_patch(data) {
const Guild = require('./Guild');
const Channel = require('./Channel');
/**
* The guild the invite is for
* @type {Guild}
*/
this.guild = this.client.guilds.get(data.guild.id) || new Guild(this.client, data.guild);
this.guild = this.client.guilds.create(data.guild, false);

/**
* The code for this invite
Expand Down Expand Up @@ -87,7 +85,7 @@ class Invite extends Base {
* The channel the invite is for
* @type {GuildChannel}
*/
this.channel = this.client.channels.get(data.channel.id) || Channel.create(this.client, data.channel, this.guild);
this.channel = this.client.channels.create(data.channel, this.guild, false);

/**
* The timestamp the invite was created at
Expand Down
4 changes: 1 addition & 3 deletions src/structures/Message.js
@@ -1,7 +1,6 @@
const Mentions = require('./MessageMentions');
const Attachment = require('./MessageAttachment');
const Embed = require('./MessageEmbed');
const MessageReaction = require('./MessageReaction');
const ReactionCollector = require('./ReactionCollector');
const ClientApplication = require('./ClientApplication');
const Util = require('../util/Util');
Expand Down Expand Up @@ -118,8 +117,7 @@ class Message extends Base {
this.reactions = new ReactionStore(this);
if (data.reactions && data.reactions.length > 0) {
for (const reaction of data.reactions) {
const id = reaction.emoji.id ? `${reaction.emoji.name}:${reaction.emoji.id}` : reaction.emoji.name;
this.reactions.set(id, new MessageReaction(this, reaction.emoji, reaction.count, reaction.me));
this.reactions.create(reaction);
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/structures/Webhook.js
Expand Up @@ -210,8 +210,7 @@ class Webhook {
auth: false,
}).then(data => {
if (!this.client.channels) return data;
const Message = require('./Message');
return new Message(this.client.channels.get(data.channel_id), data, this.client);
return this.client.channels.get(data.channel_id).messages.create(data, false);
});
}

Expand Down Expand Up @@ -239,8 +238,7 @@ class Webhook {
data: body,
}).then(data => {
if (!this.client.channels) return data;
const Message = require('./Message');
return new Message(this.client.channels.get(data.channel_id), data, this.client);
return this.client.channels.get(data.channel_id).messages.create(data, false);
});
}

Expand Down
3 changes: 1 addition & 2 deletions src/structures/shared/Search.js
Expand Up @@ -84,14 +84,13 @@ module.exports = function search(target, options) {
// Lazy load these because some of them use util
const Channel = require('../Channel');
const Guild = require('../Guild');
const Message = require('../Message');

if (!(target instanceof Channel || target instanceof Guild)) throw new TypeError('SEARCH_CHANNEL_TYPE');

let endpoint = target.client.api[target instanceof Channel ? 'channels' : 'guilds'](target.id).messages().search;
return endpoint.get({ query: options }).then(body => {
const results = body.messages.map(x =>
x.map(m => new Message(target.client.channels.get(m.channel_id), m, target.client))
x.map(m => target.client.channels.get(m.channel_id).messages.create(m, false))
);
return {
total: body.total_results,
Expand Down

0 comments on commit c4b5736

Please sign in to comment.