-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPost.js
227 lines (214 loc) · 8.07 KB
/
Post.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
'use strict';
/**
*
* @type {Class}
* @property {number} id
* @property {number} threadId
* @property {number} postTime
* @property {string} title
* @property {string} message
* @property {string} messagePlain
* @property {string} messageBBCode
* @property {string} signature
* @property {number} userId
* @property {string} username
*/
class Post {
/**
*
* @typedef {Object} RawPostData
* @property {Object} post
* @property {string} post.postid
* @property {string} post.threadId
* @property {string} post.postTime
* @property {string} post.title
* @property {string} post.message
* @property {string} post.messagePlain
* @property {string} post.messageBBCode
* @property {string} post.signature
* @property {string} post.userId
* @property {string} post.username
*/
/**
*
* @param {RawPostData} rawData
*/
constructor(rawData) {
this.rawData = rawData;
this.__parseData();
this.__cleanup();
};
__parseData() {
if (this.rawData) {
let rawData = this.rawData;
if (rawData.hasOwnProperty('post')) {
let postData = rawData['post'];
if (postData.hasOwnProperty('postid')) {
this.id = parseInt(postData.postid);
}
if (postData.hasOwnProperty('threadid')) {
this.threadId = parseInt(postData.threadid);
}
if (postData.hasOwnProperty('posttime')) {
this.postTime = parseInt(postData.posttime);
}
if (postData.hasOwnProperty('title')) {
this.title = postData.title;
}
if (postData.hasOwnProperty('message')) {
this.message = postData.message;
}
if (postData.hasOwnProperty('message_plain')) {
this.messagePlain = postData.message_plain;
}
if (postData.hasOwnProperty('message_bbcode')) {
this.messageBBCode = postData.message_bbcode;
}
if (postData.hasOwnProperty('signature')) {
this.signature = postData.signature;
}
//TODO handle users
if (postData.hasOwnProperty('userid')) {
this.userId = parseInt(postData.userid);
}
if (postData.hasOwnProperty('username')) {
this.username = postData.username;
}
}
}
};
__cleanup() {
delete (this.rawData);
};
/**
* Attempts to submit a new Post into a specified Thread
* @param {VBApi} VBApi
* @param {number} threadId - Thread id
* @param {string} message - Post Message
* @param {object=} options
* @param {boolean=} options.signature - Optionally append your signature
* @param {number=} options.threadid - Ignore, already required at threadId
* @param {string=} options.message - Ignore, already required at message
* 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, threadId, message, options) {
let that = VBApi;
options = options || {};
options.threadid = threadId || options.threadid || ''; //required
options.message = message || options.message || ''; //required
if (options.signature === true) {
//System only handle 1 or 0. defaults to 0
options.signature = '1';
}
return new Promise(async function (resolve, reject) {
try {
let response = await that.callMethod({
method: 'newreply_postreply',
params: options
});
let possibleError = that.constructor.parseErrorMessage(response);
//success is errormessgae 'redirect_postthanks'
//error 'threadclosed' if thread is closed. FIXME does not error
//reports threadid and postid
if (
possibleError === 'redirect_postthanks'
&& response.hasOwnProperty('show')
) {
resolve(response.show);
} else {
reject(possibleError || response);
}
} catch (e) {
reject(e);
}
});
}
/**
* Attempts to edit an existing Post
* @param {VBApi} VBApi
* @param {number} postId - Post id
* @param {string} message - Post Message
* @param {object=} options
* @param {string=} options.reason - Reason for editing
* @param {boolean=} options.signature - Optionally append your signature
* @param {number=} options.postid - Ignore, already required at postId
* @param {string=} options.message - Ignore, already required at message
* TODO note additional options
* @returns {Promise<*>} - Returns a unhandled response currently
* @fulfill {*}
* @reject {string} - Error Reason. Expects: (TODO list common errors here)
*/
static async edit(VBApi, postId, message, options) {
let that = VBApi;
options = options || {};
options.postid = postId || options.postid || ''; //required
options.message = message || options.message || ''; //required
if (options.signature === true) {
//System only handle 1 or 0. defaults to 0
options.signature = '1';
}
return new Promise(async function (resolve, reject) {
try {
let response = await that.callMethod({
method: 'editpost_updatepost',
params: options
});
let possibleError = that.constructor.parseErrorMessage(response);
//success is errormessgae 'redirect_editthanks'
if (possibleError === 'redirect_editthanks') {
resolve({postid: options.postid});
} else {
reject(possibleError || response);
}
} catch (e) {
reject(e);
}
});
}
/**
* TODO untested - does not seem to function yet
* Attempts to delete an existing Post
* @param {VBApi} VBApi
* @param {number} postId - Post id
* @param {number} threadId - Thread id
* @param {object=} options
* @param {string=} options.reason - Reason for deleting
* @param {number=} options.postid - Ignore, already required at postId
* @param {number=} options.threadid - Ignore, already required at threadId
* TODO note additional options
* @returns {Promise<*>} - Returns a unhandled response currently
* @fulfill {*}
* @reject {string} - Error Reason. Expects: (TODO list common errors here)
*/
static async delete(VBApi, postId, threadId, options) {
let that = VBApi;
options = options || {};
options.postid = postId || options.postid || ''; //required
options.threadid = threadId || options.threadid || ''; // TODO required????
return new Promise(async function (resolve, reject) {
try {
let response = await that.callMethod({
method: 'editpost_deletepost',
params: options
});
let possibleError = that.constructor.parseErrorMessage(response);
//unknown response
if (
possibleError === 'redirect_deletepost'
&& response.hasOwnProperty('show')
) {
//console.log('response', response);
resolve(response.show);
} else {
reject(possibleError || response);
}
} catch (e) {
reject(e);
}
});
}
}
module.exports = Post;