forked from malee31/Slack-to-Discord-Bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageSyntaxTree.js
167 lines (156 loc) · 4.26 KB
/
MessageSyntaxTree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Don't mind me lazily using class instances as a lazy way to deep clone objects :P
/**
* Helper classes used as an intermediate in the bridging process between two services
* @module SyntaxTree
*/
/**
* Base for bridge syntax trees
*/
class SyntaxTreeBase {
// Where the message originates from. In the case of this project, it should always be set to "slack"
source = "Unknown";
// Message Timestamp in seconds (Not milliseconds! Add that to additional if it is needed or just calculate it yourself)
timestamp = 0;
// What action to reflect. Examples: "send", "edit", "pin", "delete"
action = "none";
// Data fetched to assist with plugging holes in message while parsing
// Example: Object linking up a channel ID with a channel name
parseData = {
channels: [],
users: [],
emojis: [],
channel: {
name: "Unknown Channel",
description: "No Description",
topic: "",
purpose: "",
id: ""
}
};
// Miscellaneous. Assign and utilize as needed
// Meant for data that is specific to your project that the bridging end requires to pass the message on
/**
* Currently Used for
* @property {string} channelId Channel ID from Slack
* @property {number} deletedTimestamp The timestamp of the message being deleted in delete events
* @property {Object} thread Thread data if the message was sent in a thread
* @property {Object} thread.timestamp Thread timestamp
*/
additional = {}
// Helper methods that silently validate and set or ignore new values
// Set property as long as a string is passed in and ignore otherwise. More specific validation to be added
setIfString(propName, str) {
if(this[propName] !== undefined && typeof str === "string") {
this[propName] = str;
}
}
}
/**
* Class used between the original message and bridged message
* Purpose: Deconstruct messages from whatever form they come in as into a common structure for the bridge to utilize
* @class MessageSyntaxTree
* @extends {SyntaxTreeBase}
*/
class MessageSyntaxTree extends SyntaxTreeBase {
/**
* Intended action to be performed using the syntax tree data
* @type {string}
*/
action = "send";
/**
* Name of the sender. Defaults to "Unknown Pupper"
* @type {string}
*/
name = "Unknown Pupper";
/**
* URL to sender's profile picture. Defaults to a link to gif profile pic of a dog
* @type {string}
*/
profilePic = "https://media.giphy.com/media/S8aEKUGKXHl8WEsDD9/giphy.gif";
/**
* The main content of the original message in unparsed form. Defaults to "[No Message Contents]"
* @type {string}
*/
unparsedText = "[No Message Contents]";
/**
* Color to use for embeds. Defaults to a shade of blue ("#407ABA")
* @type {string}
*/
color = "#407ABA";
/**
* Files and embeds' data
* @type {Object}
* @property {FileData[]} files Array of file data
* @property {MessageSyntaxTree[]} embeds Array of additional embed data
*/
attachments = {
files: [],
embeds: []
};
constructor() {
super();
/**
* Additional property for thread id added
* @type {string}
*/
this.parseData.thread = {
id: ""
};
}
}
/**
* Syntax Tree for deletion events
* @class DeleteSyntaxTree
* @extends {SyntaxTreeBase}
*/
class DeleteSyntaxTree extends SyntaxTreeBase {
action = "delete";
/**
* An ID used to identify what message to delete
* @type {string}
*/
messageIdentifier = "";
/**
* Time of deletion
* @type {number}
*/
deletedTimestamp = undefined;
/**
* If applicable, the ID of the thread in which the message belongs to
* @type {string|number}
*/
threadId = undefined;
}
/**
* Syntax Tree for content changes
* @class ChangeSyntaxTree
* @extends {SyntaxTreeBase}
*/
class ChangeSyntaxTree extends MessageSyntaxTree {
action = "edit";
/**
* The text to replace the old content with
* @type {string}
*/
newUnparsedText = "[No Message Contents]";
/**
* Time of edit. Never used
* @type {number}
*/
changeTimestamp = undefined;
}
/**
* Class for channel data changes. Literally no different from a SyntaxTreeBase instance<br>
* Extending only to make a clearer name for its purpose
* @class ChannelSyntaxTree
* @extends {SyntaxTreeBase}
*/
class ChannelSyntaxTree extends SyntaxTreeBase {
// Literally no changes
}
module.exports = {
MessageSyntaxTree,
DeleteSyntaxTree,
ChangeSyntaxTree,
ChannelSyntaxTree
};