-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMessage.js
194 lines (182 loc) · 7.37 KB
/
Message.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
'use strict';
/**
*
* @type {Class}
* @property {number} id
* @property {number} folderId
* @property {string} recipients FIXME this needs to be parsed into username possible. expect to change
* @property {string} title
* @property {string} message
* @property {string} messagePlain
* @property {string} messageBBCode
* @property {'new' || 'old' || 'replied'} status
* @property {Date} time
* @property {number} userId
* @property {string} username
* @property {Object} user
* @property {number} user.id
* @property {string} user.username
* @property {string} user.title
* @property {string} user.signature
* @property {string} user.avatarUrl
* @property {boolean} user.online
* @property {Date} user.joinDate
*/
class Message {
/**
*
* @typedef {Object} RawMessageData
* @property {Object} messageData
* @property {Object} messageData.HTML
* @property {[]} messageData.HTML.bccrecipients
* @property {[]} messageData.HTML.ccrecipients
* @property {Object} messageData.HTML.pm
* @property {string} messageData.HTML.pm.pmid
* @property {string} messageData.HTML.pm.title
* @property {string} messageData.HTML.pm.recipients 'UserHere ; ' possibly delimited by ;
* @property {string || number} messageData.HTML.pm.savecopy 0
* @property {string} messageData.HTML.pm.folderid '0'
* @property {string} messageData.HTML.pm.fromusername
* @property {Object} messageData.HTML.postbit
* @property {Object} messageData.HTML.postbit.show
* @property {Object} messageData.HTML.postbit.post
* @property {'new' || 'old' || 'replied'} messageData.HTML.postbit.post.statusicon
* @property {string} messageData.HTML.postbit.post.posttime
* @property {string || number} messageData.HTML.postbit.post.checkbox_value
* @property {string} messageData.HTML.postbit.post.onlinestatusphrase 'x_is_online_now'
* @property {string || number} messageData.HTML.postbit.post.userid
* @property {string} messageData.HTML.postbit.post.username
* @property {string} messageData.HTML.postbit.post.avatarUrl 'customavatars/avatar0000_14.gif'
* @property {Object} messageData.HTML.postbit.post.onlinestatus
* @property {string || number} messageData.HTML.postbit.post.onlinestatus.onlinestatus 0
* @property {string} messageData.HTML.postbit.post.usertitle
* @property {string} messageData.HTML.postbit.post.joindate
* @property {string} messageData.HTML.postbit.post.title Message title
* @property {string} messageData.HTML.postbit.post.message
* @property {string} messageData.HTML.postbit.post.message_plain
* @property {string} messageData.HTML.postbit.post.message_bbcode
* @property {string} messageData.HTML.postbit.post.signature
*/
/**
*
* @param {RawMessageData} rawData
*/
constructor(rawData) {
this.rawData = rawData;
this.__parseData();
this.__cleanup();
};
__parseData() {
let that = this;
if (that.rawData
&& that.rawData.hasOwnProperty('HTML')
&& that.rawData.HTML.hasOwnProperty('pm')
&& that.rawData.HTML.hasOwnProperty('postbit')
&& that.rawData.HTML.hasOwnProperty('postbit')
&& that.rawData.HTML.postbit.hasOwnProperty('post')
) {
let pm = that.rawData.HTML.pm;
let post = that.rawData.HTML.postbit.post;
that.id = parseInt(pm.pmid); // number
that.folderId = parseInt(pm.folderid);
that.recipients = pm.recipients; // FIXME need to parse this
that.title = post.title || pm.title;
that.message = post.message;
that.messagePlain = post.message_plain;
that.messageBBCode = post.message_bbcode;
that.status = post.statusicon;
that.time = new Date(parseInt(post.posttime) * 1000);
that.userId = parseInt(post.userid);
that.username = pm.fromusername;
that.user = {
id: parseInt(post.userid),
username: post.username,
title: post.usertitle,
signature: post.signature,
avatarUrl: post.avatarurl,
online: !!parseInt(post.onlinestatus.onlinestatus),
joinDate: new Date(parseInt(post.joindate) * 1000),
};
}
};
__cleanup() {
delete (this.rawData);
};
/**
* Get details of a specific Message for the logged in user
* @param {VBApi} VBApi
* @param {number} id
* @param {object=} options
* @param {number=} options.pmid - Ignore, already required at id
* @returns {Promise<Message>} - Returns a Message object
* @fulfill {Message}
* @reject {string} - Error Reason. Expects: (TODO list common errors here)
*/
static async get(VBApi, id, options) {
let that = VBApi;
options = options || {};
options.pmid = id || options.pmid || ''; //required
return new Promise(async function (resolve, reject) {
let message = null;
try {
let response = await that.callMethod({
method: 'private_showpm',
params: options
});
if (
response
&& response.hasOwnProperty('response')
) {
message = new Message(response.response);
}
} catch (e) {
reject(e);
}
if (message !== null) {
resolve(message);
} else {
reject();
}
});
}
/**
*
* @param {VBApi} VBApi
* @param {string} username - Username to send the message to
* @param {string} title - Message Subject
* @param {string} message - Message content
* @param {object=} options
* @param {boolean=} options.signature - Optionally append your signature
* @param {string=} options.recipients - Ignore, already required at username
* @param {string=} options.title - Ignore, already required at title
* @param {string=} options.message - Ignore, already required at message
* TODO note additional options
* @returns {Promise<void>} - Successfully completes if sent. TODO: provide a better response
* @fulfill {void}
* @reject {string} - Error Reason. Expects: (TODO list common errors here)
*/
static async create(VBApi, username, title, message, options) {
let that = VBApi;
options = options || {};
options.recipients = username || options.recipients || ''; //required
options.title = title || options.title || ''; //required
options.message = message || options.message || ''; //required
options.signature = options.signature === true ? '1' : '0';
return new Promise(async function (resolve, reject) {
try {
let response = await that.callMethod({
method: 'private_insertpm',
params: options
});
let possibleError = that.constructor.parseErrorMessage(response);
if (possibleError !== 'pm_messagesent') {
reject(possibleError || response);
}
} catch (e) {
reject(e);
}
resolve();
});
}
}
module.exports = Message;