hackchat-engine 1.1.8 → 1.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Client.js CHANGED
@@ -405,6 +405,22 @@ class Client extends EventEmitter {
405
405
  signedTransaction,
406
406
  });
407
407
  }
408
+
409
+ /**
410
+ * Send `updateMessage` operation to the server
411
+ * @param {string} customId The customId of the target message
412
+ * @param {string} text The text to apply
413
+ * @param {string} [mode='overwrite'] The update mode: 'overwrite', 'append', 'prepend', or 'complete'
414
+ */
415
+ updateMessage(customId, text, mode = 'overwrite') {
416
+ this.ws.send({
417
+ cmd: OPCodes.UPDATE_MESSAGE,
418
+ channel: this.channel, // @todo Multichannel
419
+ customId,
420
+ text,
421
+ mode,
422
+ });
423
+ }
408
424
  }
409
425
 
410
426
  export default Client;
@@ -42,6 +42,7 @@ class EventsManager {
42
42
  this.HackAttempt = new HackAttempt(this.client);
43
43
  this.SignMessage = new SignMessage(this.client);
44
44
  this.SignTransaction = new SignTransaction(this.client);
45
+ this.UpdateMessage = new UpdateMessage(this.client);
45
46
  }
46
47
  }
47
48
 
@@ -5,7 +5,7 @@ import HackAttemptStruct from '../structures/HackAttemptStruct.js';
5
5
  * This class handles an incoming `hackattempt` event from the server
6
6
  * @private
7
7
  */
8
- class PublicChannels extends AbstractEvent {
8
+ class HackAttempt extends AbstractEvent {
9
9
  /**
10
10
  * Event handler function
11
11
  * @param {object} data Incoming event data
@@ -21,4 +21,4 @@ class PublicChannels extends AbstractEvent {
21
21
  }
22
22
  }
23
23
 
24
- export default PublicChannels;
24
+ export default HackAttempt;
@@ -0,0 +1,24 @@
1
+ import AbstractEvent from './AbstractEvent.js';
2
+ import UpdateMessageStruct from '../structures/UpdateMessageStruct.js';
3
+
4
+ /**
5
+ * This class handles an incoming `updateMessage` event from the server
6
+ * @private
7
+ */
8
+ class UpdateMessage extends AbstractEvent {
9
+ /**
10
+ * Event handler function
11
+ * @param {object} data Incoming event data
12
+ * @returns {object}
13
+ */
14
+ handle(data) {
15
+ const { client } = this;
16
+ const update = new UpdateMessageStruct(client, data);
17
+
18
+ return {
19
+ update,
20
+ };
21
+ }
22
+ }
23
+
24
+ export default UpdateMessage;
package/package.json CHANGED
@@ -59,5 +59,5 @@
59
59
  "lintfix": "eslint --fix --ignore-path .gitignore .",
60
60
  "test": "echo \"Error: no test specified\" && exit 1"
61
61
  },
62
- "version": "1.1.8"
62
+ "version": "1.1.9"
63
63
  }
@@ -58,7 +58,7 @@ class HackAttemptStruct {
58
58
  this.lib = data.lib;
59
59
 
60
60
  /**
61
- * The timestamp the publicchannels was sent at
61
+ * The timestamp the hackattempt was sent at
62
62
  * @type {number}
63
63
  */
64
64
  this.timestamp = new Date();
@@ -0,0 +1,64 @@
1
+ /**
2
+ * This class handles parsing of the data of an `updateMessage` event
3
+ */
4
+ class UpdateMessageStruct {
5
+ /**
6
+ * @param {Client} client Main client reference
7
+ * @param {object} data Incoming event data
8
+ */
9
+ constructor(client, data) {
10
+ /**
11
+ * Add client reference
12
+ * @type {Client}
13
+ * @readonly
14
+ */
15
+ Object.defineProperty(this, 'client', { value: client });
16
+
17
+ if (data) this.setup(data);
18
+ }
19
+
20
+ /**
21
+ * Fill in this structure with provided data
22
+ * @param {object} data Incoming event data
23
+ * @returns {void}
24
+ */
25
+ setup(data) {
26
+ /**
27
+ * The channel the message resides in
28
+ * @type {string}
29
+ */
30
+ this.channel = data.channel;
31
+
32
+ /**
33
+ * The customId of the target message
34
+ * @type {string}
35
+ */
36
+ this.customId = data.customId;
37
+
38
+ /**
39
+ * The update mode (overwrite, append, prepend, complete)
40
+ * @type {string}
41
+ */
42
+ this.mode = data.mode;
43
+
44
+ /**
45
+ * The new text content (or text to append/prepend)
46
+ * @type {string}
47
+ */
48
+ this.text = data.text;
49
+
50
+ /**
51
+ * The user who initiated the update
52
+ * @type {User}
53
+ */
54
+ this.user = this.client.users.get(data.userid);
55
+
56
+ /**
57
+ * The timestamp of the update
58
+ * @type {Date}
59
+ */
60
+ this.timestamp = new Date();
61
+ }
62
+ }
63
+
64
+ export default UpdateMessageStruct;
package/util/Constants.js CHANGED
@@ -76,6 +76,7 @@ export const OPCodes = {
76
76
  REQUEST_SIW: 'siw',
77
77
  SIGN_SIW: 'signsiw',
78
78
  CONFIRM_TX: 'confirmtx',
79
+ UPDATE_MESSAGE: 'updateMessage',
79
80
  };
80
81
 
81
82
  /**
@@ -130,4 +131,5 @@ export const WSEvents = {
130
131
  HACK_ATTEMPT: 'hackAttempt',
131
132
  SIGN_MESSAGE: 'signMessage',
132
133
  SIGN_TRANSACTION: 'signTransaction',
134
+ UPDATE_MESSAGE: 'updateMessage',
133
135
  };
@@ -21,6 +21,7 @@ import PubChannelsHandler from './handlers/PubChannelsHandler.js';
21
21
  import HackAttemptHandler from './handlers/HackAttemptHandler.js';
22
22
  import SignMessageHandler from './handlers/SignMessageHandler.js';
23
23
  import SignTransactionHandler from './handlers/SignTransactionHandler.js';
24
+ import UpdateMessageHandler from './handlers/UpdateMessageHandler.js';
24
25
 
25
26
  const BeforeReadyWhitelist = [
26
27
  WSEvents.SESSION,
@@ -77,6 +78,7 @@ class PacketRouter {
77
78
  this.registerEvent(WSEvents.HACK_ATTEMPT, HackAttemptHandler);
78
79
  this.registerEvent(WSEvents.SIGN_MESSAGE, SignMessageHandler);
79
80
  this.registerEvent(WSEvents.SIGN_TRANSACTION, SignTransactionHandler);
81
+ this.registerEvent(WSEvents.UPDATE_MESSAGE, UpdateMessageHandler);
80
82
  }
81
83
 
82
84
  /**
@@ -0,0 +1,30 @@
1
+ import AbstractHandler from './AbstractHandler.js';
2
+ import { Events } from '../../../util/Constants.js';
3
+
4
+ /**
5
+ * Handles an updateMessage packet received from the server
6
+ * @private
7
+ */
8
+ class UpdateMessageHandler extends AbstractHandler {
9
+ /**
10
+ * Parses incoming packet data and emits related events
11
+ * @param {object} packet Incoming packet data
12
+ * @returns {void}
13
+ */
14
+ handle(packet) {
15
+ const { client } = this.packetRouter;
16
+ const response = client.events.UpdateMessage.handle(packet);
17
+
18
+ /**
19
+ * Emitted when a message update is received
20
+ * @event Client#updateMessage
21
+ * @param {UpdateMessageStruct} update The update details
22
+ */
23
+ client.emit(Events.UPDATE_MESSAGE, response.update);
24
+
25
+ // Emit debug info
26
+ client.emit(Events.DEBUG, `[${Events.UPDATE_MESSAGE}]: ${packet}`);
27
+ }
28
+ }
29
+
30
+ export default UpdateMessageHandler;