-
Notifications
You must be signed in to change notification settings - Fork 2
/
board.js
75 lines (70 loc) · 1.5 KB
/
board.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
var rp = require('request-promise');
var Promise = require('bluebird');
var urlbuilder = require('./urlbuilder');
var constants = require('./constants');
var Board = function(course_sn, sn, caption, jar) {
this.course_sn = course_sn;
this.sn = sn;
this.caption = caption;
this.jar = jar;
this.posts = [];
};
Board.prototype.fetch = function() {
var self = this;
return rp({
headers: constants.headers,
jar: self.jar,
json: true,
method: 'GET',
url: urlbuilder.api({
mode: 'read_board_post',
board: self.sn,
course_sn: self.course_sn
})
}).then(function(data) {
/*
[
{ sn: '1472583',
parent: '0',
author: '',
cauthor: '',
attach: 'image.jpeg',
subject: '',
content: '',
post_time: '2016-05-24 05:06:51.55904+08',
latest_rep: '2016-05-24 12:13:00.497516+08',
count_rep: '1',
file_path: '12345.jpeg' }
]
*/
});
};
Board.prototype.get_posts = function() {
var self = this;
return new Promise(function(resolve, reject) {
if(self.posts.length != 0) {
resolve(self.posts);
} else {
self.fetch().then(resolve);
}
});
};
Board.prototype.post = function(subject, content, parent_sn) {
var self = this;
return rp({
headers: constants.headers,
jar: self.jar,
json: true,
method: 'POST',
url: urlbuilder.api({
mode: 'write_board'
}),
form: {
subject: subject,
content: content,
board_sn: self.sn,
parent: parent_sn
}
});
};
module.exports = Board;