Bridged WebSocket Events and REST Requests. Now REST Requests will respond exactly like WS Events to data

This commit is contained in:
hydrabolt
2016-04-24 16:30:58 +01:00
parent c42e303b7b
commit 1676a5e73f
12 changed files with 139 additions and 23 deletions

View File

@@ -0,0 +1,27 @@
'use strict';
/*
ABOUT ACTIONS
Actions are similar to WebSocket Packet Handlers, but since introducing
the REST API methods, in order to prevent rewriting code to handle data,
"actions" have been introduced. They're basically what Packet Handlers
used to be but they're strictly for manipulating data and making sure
that WebSocket events don't clash with REST methods.
*/
class GenericAction {
constructor(client) {
this.client = client;
}
handle(data) {
}
};
module.exports = GenericAction;

View File

@@ -0,0 +1,19 @@
'use strict';
const requireAction = name => require(`./${name}`);
class ActionsManager {
constructor(client) {
this.client = client;
this.register('MessageCreate');
this.register('MessageDelete');
}
register(name) {
let Action = requireAction(name);
this[name] = new Action(this.client);
}
}
module.exports = ActionsManager;

View File

@@ -0,0 +1,31 @@
'use strict';
const Action = require('./Action');
const Constants = require('../../util/Constants');
const Message = require('../../structures/Message');
class MessageCreateAction extends Action {
constructor(client) {
super(client);
}
handle(data) {
let client = this.client;
let channel = client.store.get('channels', data.channel_id);
if (channel) {
let message = channel._cacheMessage(new Message(channel, data, client));
return {
m: message,
};
}
return {
m: null,
};
}
};
module.exports = MessageCreateAction;

View File

@@ -0,0 +1,42 @@
'use strict';
const Action = require('./Action');
const Constants = require('../../util/Constants');
const Message = require('../../structures/Message');
class MessageDeleteAction extends Action {
constructor(client) {
super(client);
this.timeouts = [];
}
handle(data) {
let client = this.client;
let channel = client.store.get('channels', data.channel_id);
if (channel) {
let message = channel.store.get('messages', data.id);
if (message && !message._deleted) {
message._deleted = true;
this.scheduleForDeletion(channel, message.id);
}
return {
m: message,
};
}
return {
m: null,
};
}
scheduleForDeletion(channel, id) {
this.timeouts.push(
setTimeout(() => channel.store.remove('messages', id),
this.client.options.rest_ws_bridge_timeout)
);
}
};
module.exports = MessageDeleteAction;