-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-chat.js
More file actions
174 lines (154 loc) · 3.73 KB
/
api-chat.js
File metadata and controls
174 lines (154 loc) · 3.73 KB
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
function Post(init) {
fill(this,init,[
'id',
'chat',
'author',
'time',
'body',
'track',
'custom',
'tree'
]);
// Recursively fill the tree
var tree = this.tree = this.tree || { count: 0, top: [] };
var chat = this.chat;
if (tree.top.length > 0) {
tree.top = tree.top.map(function(data) {
data.chat = chat;
return new Post(data);
});
}
}
Post.prototype = Object.create({
// Load post data from the server
//
Load: function() {
var self = this;
return request('GET',['chat',this.chat,'posts',this.id]).then(function(data) {
Post.call(self,data.info);
data.people.forEach(Person.Cache);
return self;
});
},
// Delete the post from the server
//
Delete: function() {
return request('DELETE',['chat',this.chat,'posts',this.id]);
},
// Reply to the post
//
Reply: function(params) {
var self = this;
params = keep(params,['body','custom']);
params.reply = this.id;
return request('POST',['chat',this.chat,'posts'],{},params).then(function(data) {
return new Chat.Post({
body: params.body,
custom: params.custom || null,
id: data.id,
chat: self.chat
});
});
},
// Start (or stop) tracking a post
//
Track: function(track) {
return request('POST',['chat',this.chat,'posts',this.id,'track'],{},track);
},
// List replies
//
Replies: function(params) {
var self = this;
params = keep(params || {},['limit','offset']);
params.under = self.id;
return request('GET',['chat',this.chat,'posts'],params).then(function(data) {
data.people.forEach(Person.Cache);
return data.posts.map(function(post) {
post.chat = self.chat;
return new Chat.Post(post);
});
});
}
});
function Chat(init) {
fill(this,init,[
'id',
'subject',
'custom',
'audience',
'count',
'last',
'access',
'track'
]);
}
Chat.prototype = Object.create({
// Load chat data from the server
//
Load: function() {
var self = this;
return request('GET',['chat',this.id]).then(function(data) {
Chat.call(self,data.info);
return self;
});
},
// Update an existing chatroom
//
Save: function() {
return request('PUT',['chat',this.id],{},keep(this,['subject','custom','audience']));
},
// Create a chatroom and retrieve its identifier
//
Create: function() {
var self = this;
return request('POST','chat',{},keep(this,['subject','custom','audience'])).then(function(data) {
self.id = data.id;
return self;
});
},
// Delete a chatroom
//
Delete: function() {
return request('DELETE',['chat',this.id]);
},
// Start (or stop) tracking a chatroom
//
Track: function(track) {
return request('POST',['chat',this.id,'track'],{},track);
},
// Grab a tree of posts from a chatroom
//
Posts: function(params) {
var self = this;
params = params || {};
return request('GET',['chat',this.id,'posts'],keep(params,['limit','offset'])).then(function(data) {
data.people.forEach(Person.Cache);
return data.posts.map(function(post) {
post.chat = self.id;
return new Chat.Post(post);
});
});
},
// Create a new post in this chatroom
//
Post: function(params) {
var self = this;
return request('POST',['chat',this.id,'posts'],{},keep(params,['body','custom'])).then(function(data) {
return new Chat.Post({
body: params.body,
custom: params.custom || null,
id: data.id,
chat: self.id
});
});
}
});
// List all visible chatrooms
//
Chat.List = function(params) {
params = params || {};
return request('GET','chat',keep(params,['limit','offset'])).then(function(data) {
return data.list.map(function(info) { return new Chat(info); });
});
};
Chat.Post = Post;