-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathThread.js
286 lines (271 loc) · 9.81 KB
/
Thread.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
'use strict';
const Post = require('./Post');
/**
* @type {Class}
* @property {number} forumId
* @property {string} forumTitle
* @property {number} id
* @property {string} title
* @property {Post[]} posts
*/
class Thread {
/**
* @typedef {Object} RawThreadData
* @property {Object} thread
* @property {string} thread.forumid
* @property {string} thread.forumtitle
* @property {string} thread.threadid
* @property {string} thread.title
* @property {string} thread.threadtitle
* @property {RawPostData[]} postbits
*/
/**
* @param {RawThreadData} rawData
*/
constructor(rawData) {
this.rawData = rawData;
this.__parseData();
this.__cleanup();
}
/**
*
* @private
*/
__parseData() {
if (this.rawData) {
//TODO need to specify if its fully fetched
let rawData = this.rawData;
if (rawData.hasOwnProperty('thread')) {
let threadData = rawData['thread'];
if (threadData.hasOwnProperty('forumid')) {
this.forumId = parseInt(threadData.forumid);
}
if (threadData.hasOwnProperty('forumtitle')) {
this.forumTitle = threadData.forumtitle;
}
if (threadData.hasOwnProperty('threadid')) {
this.id = parseInt(threadData.threadid);
}
if (threadData.hasOwnProperty('title')) {
this.title = threadData.title;
} else if (threadData.hasOwnProperty('threadtitle')) {
this.title = threadData.threadtitle;
}
}
if (rawData.hasOwnProperty('postbits')) {
let postBits = rawData.postbits;
this.posts = [];
for (let post in postBits) {
if (postBits.hasOwnProperty(post)) {
this.posts.push(new Post(postBits[post]));
}
}
}
}
}
/**
*
* @private
*/
__cleanup() {
delete (this.rawData);
}
/**
* Attempts to submit a new Thread into a specified Forum. This will also be considered the first Post
* @param {VBApi} VBApi
* @param {number} forumId - Forum Id
* @param {string} subject - Post/Thread Subject
* @param {string} message - Post Message
* @param {object=} options
* @param {boolean=} options.signature - Optionally append your signature
* @param {number=} options.forumid - Ignore, already required at postId
* @param {string=} options.subject - Ignore, already required at postId
* @param {string=} options.message - Ignore, already required at postId
* TODO note additional options
* @returns {Promise<*>} - Returns a unhandled response currently
* @fulfill {*}
* @reject {string} - Error Reason. Expects: (TODO list common errors here)
*/
static async create(VBApi, forumId, subject, message, options) {
let that = VBApi;
options = options || {};
options.forumid = forumId || options.forumid || ''; //required
options.subject = subject || options.subject || ''; //required
options.message = message || options.message || ''; //required
if (options.signature === true) {
//System only handle 1 or 0. defaults to 0
options.signature = '1'; // FIXME This didn't seem to work
}
return new Promise(async function (resolve, reject) {
try {
let response = await that.callMethod({
method: 'newthread_postthread',
params: options
});
let possibleError = that.constructor.parseErrorMessage(response);
//success is errormessgae 'redirect_postthanks'
//reports threadid and postid
if (
possibleError === 'redirect_postthanks'
&& response.hasOwnProperty('show')
) {
resolve(response.show);
} else {
reject(possibleError || response);
}
} catch (e) {
reject(e);
}
});
}
/**
* List detailed information about a Thread and it's Posts
* @param {VBApi} VBApi
* @param {number} threadId - Thread id
* @param {object=} options - Secondary Options
* @param {number=} options.threadid - Ignore, already required at threadId
* TODO note additional options
* @returns {Promise<Thread>} - Returns a Thread object
* @fulfill {Thread}
* @reject {string} - Error Reason. Expects: (TODO list common errors here)
*/
static async get(VBApi, threadId, options) {
let that = VBApi;
options = options || {};
options.threadid = threadId || options.threadid || ''; //required
return new Promise(async function (resolve, reject) {
let thread = null;
try {
let response = await that.callMethod({
method: 'showthread',
params: options
});
if (
response
&& response.hasOwnProperty('response')
) {
thread = new Thread(response.response);
}
} catch (e) {
reject(e);
}
if (thread !== null) {
resolve(thread);
} else {
reject();
}
});
}
/**
* TODO incomplete - does not seem to function yet
* Attempts to close a specific Thread. Requires a user to have a 'inline mod' permissions
* @param {VBApi} VBApi
* @param {number} threadId - Id of Thread to close
* @returns {Promise<*>} - Returns a unhandled response currently
* @fulfill {*}
* @reject {string} - Error Reason. Expects: (TODO list common errors here)
*/
static async close(VBApi, threadId) {
let that = VBApi;
let cookies = {};
if (threadId) {
//TODO multiple ids are delimited with a '-'. eg: 123-345-456
cookies.vbulletin_inlinethread = threadId;
}
return new Promise(async function (resolve, reject) {
try {
let response = await that.callMethod({
method: 'inlinemod_close',
cookies: cookies || {}
});
//let possibleError = that.constructor.parseErrorMessage(response);
//unknown responses
/*if (
possibleError === 'redirect_postthanks'
&& response.hasOwnProperty('show')
) {*/
resolve(response);
/*} else {
reject(possibleError || response);
}*/
} catch (e) {
reject(e);
}
});
}
/**
* TODO incomplete - does not seem to function yet
* Attempts to open a specific Thread. Requires a user to have a 'inline mod' permissions
* @param {VBApi} VBApi
* @param {number} threadId - Id of Thread to open
* @returns {Promise<*>} - Returns a unhandled response currently
* @fulfill {*}
* @reject {string} - Error Reason. Expects: (TODO list common errors here)
*/
static async open(VBApi, threadId) {
let that = VBApi;
let cookies = {};
if (threadId) {
//TODO multiple ids are delimited with a '-'. eg: 123-345-456
cookies.vbulletin_inlinethread = threadId;
}
return new Promise(async function (resolve, reject) {
try {
let response = await that.callMethod({
method: 'inlinemod_open',
cookies: cookies || {}
});
//let possibleError = that.constructor.parseErrorMessage(response);
//unknown responses
/*if (
possibleError === 'redirect_postthanks'
&& response.hasOwnProperty('show')
) {*/
resolve(response);
/*} else {
reject(possibleError || response);
}*/
} catch (e) {
reject(e);
}
});
}
/**
* TODO incomplete - does not seem to function yet
* Attempts to delete a specific Thread. Requires a user to have a 'inline mod' permissions
* @param {VBApi} VBApi
* @param {number} threadId - Id of Thread to close
* @returns {Promise<*>} - Returns a unhandled response currently
* @fulfill {*}
* @reject {string} - Error Reason. Expects: (TODO list common errors here)
*/
static async delete(VBApi, threadId) {
let that = VBApi;
let cookies = {};
if (threadId) {
//TODO multiple ids are delimited with a '-'. eg: 123-345-456
cookies.vbulletin_inlinethread = threadId;
}
return new Promise(async function (resolve, reject) {
try {
let response = await that.callMethod({
method: 'inlinemod_dodeletethreads',
cookies: cookies || {}
});
//let possibleError = that.constructor.parseErrorMessage(response);
//unknown responses
/*if (
possibleError === 'redirect_postthanks'
&& response.hasOwnProperty('show')
) {*/
resolve(response);
/*} else {
reject(possibleError || response);
}*/
} catch (e) {
reject(e);
}
});
}
}
module.exports = Thread;