-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathInbox.js
178 lines (167 loc) · 7.32 KB
/
Inbox.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
'use strict';
const _ = require('underscore');
/**
*
* @type {Class}
* @property {number} totalMessages
* @property {number} messageQuota
* @property {Object[]} messages
* @property {number} messages.id
* @property {string} messages.title
* @property {Date} messages.time
* @property {'new' || 'old' || 'replied'} messages.status
* @property {number} messages.userId
* @property {string} messages.username
* @property {boolean} messages.unread
*/
class Inbox {
/**
*
* @typedef {Object} RawInboxData
* @property {Object} inboxData
* @property {Object} inboxData.HTML
* @property {number} inboxData.HTML.folderid 0
* @property {string} inboxData.HTML.pagenav
* @property {number} inboxData.HTML.pagenumber 1
* @property {number} inboxData.HTML.perpage 50
* @property {string} inboxData.HTML.pmquota 2000
* @property {string} inboxData.HTML.pmtotal 2
* @property {Object} inboxData.HTML.receipts
* @property {Object} inboxData.HTML.sortfilter
* @property {string} inboxData.HTML.totalmessages 2
* @property {string} inboxData.HTML.startmessage 1
* @property {string} inboxData.HTML.endmessage 2
* @property {Object} inboxData.HTML.messagelist_periodgroups
* @property {string} inboxData.HTML.messagelist_periodgroups.group_id '0_yesterday'
* @property {number} inboxData.HTML.messagelist_periodgroups.messagesingroup
* @property {Object || Object[]} inboxData.HTML.messagelist_periodgroups.messagelistbits
* @property {Object} inboxData.HTML.messagelist_periodgroups.messagelistbits.pm
* @property {string} inboxData.HTML.messagelist_periodgroups.messagelistbits.pm.pmid
* @property {string} inboxData.HTML.messagelist_periodgroups.messagelistbits.pm.sendtime
* @property {string} inboxData.HTML.messagelist_periodgroups.messagelistbits.pm.statusicon 'new' || 'old' || 'replied'
* @property {string} inboxData.HTML.messagelist_periodgroups.messagelistbits.pm.title
* @property {Object} inboxData.HTML.messagelist_periodgroups.messagelistbits.userbit
* @property {Object} inboxData.HTML.messagelist_periodgroups.messagelistbits.userbit.0
* @property {Object} inboxData.HTML.messagelist_periodgroups.messagelistbits.userbit.1
* @property {Object} inboxData.HTML.messagelist_periodgroups.messagelistbits.userbit.userinfo
* @property {string} inboxData.HTML.messagelist_periodgroups.messagelistbits.userbit.userinfo.userid
* @property {string} inboxData.HTML.messagelist_periodgroups.messagelistbits.userbit.userinfo.username
* @property {Object} inboxData.HTML.messagelist_periodgroups.messagelistbits.show
* @property {string || number} inboxData.HTML.messagelist_periodgroups.messagelistbits.show.pmicon true/false
* @property {string || number} inboxData.HTML.messagelist_periodgroups.messagelistbits.show.unread true/false
*/
/**
*
* @param {RawInboxData} rawData
*/
constructor(rawData) {
this.rawData = rawData;
this.messages = [];
this.__parseData();
this.__cleanup();
};
__parseData() {
// TODO Only handling single folder at this time
let that = this;
if (this.rawData) {
let rawData = this.rawData;
if (rawData.hasOwnProperty('HTML')) {
that.totalMessages = parseInt(rawData.HTML.pmtotal);
that.messageQuota = parseInt(rawData.HTML.pmquota);
if (
rawData.HTML.hasOwnProperty('messagelist_periodgroups')
&& rawData.HTML.messagelist_periodgroups.hasOwnProperty('messagelistbits')
) {
// Can be a object (singular) or array
let messageListBits = rawData.HTML.messagelist_periodgroups.messagelistbits;
if (!_.isArray(messageListBits)) {
messageListBits = [messageListBits];
}
messageListBits.forEach(function (rawMessage) {
that.messages.push({
id: parseInt(rawMessage.pm.pmid), // number
title: rawMessage.pm.title, // string
time: new Date(parseInt(rawMessage.pm.sendtime) * 1000), // Date
status: rawMessage.pm.statusicon, // string
userId: parseInt(rawMessage.userbit.userinfo.userid), // number
username: rawMessage.userbit.userinfo.username, // string
unread: !!parseInt(rawMessage.show.unread), // boolean
});
});
}
}
}
};
__cleanup() {
delete (this.rawData);
};
/**
* Get logged in user's Inbox and list of private Messages
* @param {VBApi} VBApi
* @param {object=} options
* @returns {Promise<Inbox>} - Returns an Inbox object
* @fulfill {Inbox}
* @reject {string} - Error Reason. Expects: (TODO list common errors here)
*/
static async get(VBApi, options) {
let that = VBApi;
options = options || {};
return new Promise(async function (resolve, reject) {
let inbox = null;
try {
let response = await that.callMethod({
method: 'private_messagelist',
params: options
});
if (
response
&& response.hasOwnProperty('response')
) {
inbox = new Inbox(response.response);
}
} catch (e) {
reject(e);
}
if (inbox !== null) {
resolve(inbox);
} else {
reject();
}
});
}
/**
* Attempts to submit a new Thread into a specified Forum. This will also be considered the first Post
* @param {Date} date - Delete all messages from before the specified date
* @param {number=0} folderId - Folder Id, defaults to 0
* @param {object=} options
* @param {string=} options.dateline - Ignore, already required at date
* @param {number=} options.folderid - Ignore, already required at folderId
* TODO note additional options
* @param {VBApi} VBApi
* @returns {Promise<void>} - Returns a unhandled response currently
* @fulfill {void}
* @reject {string} - Error Reason. Expects: (TODO list common errors here)
*/
static async empty(VBApi, date, folderId, options) {
let that = VBApi;
options = options || {};
options.dateline = parseInt((date.getTime() / 1000).toFixed(0)) || options.dateline || ''; //required
options.folderid = folderId || options.folderid || '0';
return new Promise(async function (resolve, reject) {
try {
let response = await that.callMethod({
method: 'private_confirmemptyfolder',
params: options
});
let possibleError = that.constructor.parseErrorMessage(response);
if (possibleError !== 'pm_messagesdeleted') {
reject(possibleError || response);
}
} catch (e) {
reject(e);
}
resolve();
});
}
}
module.exports = Inbox;