This repository has been archived by the owner on Apr 14, 2018. It is now read-only.
/
room.js
253 lines (222 loc) · 7.24 KB
/
room.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
var Message = require("./message").Message,
Upload = require("./upload").Upload,
User = require("./user").User,
urlFor = require("./restful").urlFor;
/**
* A chat room.
* @constructor
* @param {Connection} connection A server connection.
* @param {Object} attrs The initial attributes of this room.
*/
exports.Room = function (connection, attrs) {
var self, resultToMessages, get, post, put, sendMessage, listenConnection;
self = this;
this.id = attrs.id;
this.name = attrs.name;
this.topic = attrs.topic;
this.membershipLimit = attrs.membership_limit;
this.locked = attrs.locked;
this.openToGuests = attrs.open_to_guests;
this.guestToken = attrs.active_token_value;
this.createdAt = new Date(attrs.created_at);
this.updatedAt = new Date(attrs.updated_at);
/**
* Returns an array of message objects given the server response.
* @param {Object} result The server result.
* @return {Array.<Message>} The resulting messages.
*/
resultToMessages = function (result) {
var i, messages = [];
for (i = 0; i < result.messages.length; i++) {
messages.push(new Message(connection, result.messages[i]));
}
return messages;
};
get = function (action, params, callback) {
connection.get(urlFor("room", self.id, action), params, callback);
};
post = function (action, body, callback) {
connection.post(urlFor("room", self.id, action), body, callback);
};
put = function (body, callback) {
connection.put(urlFor("room", self.id), body, callback);
};
/**
* Sends the specified message.
* @param {string} message The message to send.
* @param {string} type The type of message.
* @param {function()=} callback The optional callback to call when complete.
*/
sendMessage = this.sendMessage = function (message, type, callback) {
post("speak", {
message: {
body: message,
type: type
}
}, callback);
};
/**
* Sends the specified message.
* @param {string} message The message to send.
* @param {function()=} callback The optional callback to call when complete.
*/
this.speak = function (message, callback) {
sendMessage(message, "TextMessage", callback);
};
/**
* Sends the specified pre-formatted message.
* @param {string} message The message to send.
* @param {function()=} callback The optional callback to call when complete.
*/
this.paste = function (message, callback) {
sendMessage(message, "PasteMessage", callback);
};
/**
* Plays the sound with the specified name.
* @param {string} name The name of the sound to play.
* @param {function()=} callback The optional callback to call when complete.
*/
this.play = function (name, callback) {
sendMessage(name, "SoundMessage", callback);
};
/**
* Inserts a twitter status url into the chat
* @param {string} tweetUrl url of tweet
* @param {function()=} callback the optional callback to call when complete.
*/
this.tweet = function (tweetUrl, callback) {
sendMessage(tweetUrl, "TweetMessage", callback);
};
/**
* Updates the specified room attributes.
* @param {Object} attributes The attributes to update.
* @param {function()=} callback The optional callback to call when complete.
*/
this.update = function (attributes, callback) {
// TODO: Update the name and topic of the room after complete
put({ room: attributes }, callback);
};
/**
* Join this room.
* @param {function()=} callback The optional callback to call when complete.
*/
this.join = function (callback) {
post("join", callback);
};
/**
* Leave this room.
* @param {function()=} callback The optional callback to call when complete.
*/
this.leave = function (callback) {
post("leave", callback);
};
/**
* Lock this room. You must be in a room to lock it.
* @param {function()=} callback The optional callback to call when complete.
*/
this.lock = function (callback) {
post("lock", callback);
};
/**
* Unlock this room.
* @param {function()=} callback The optional callback to call when complete.
*/
this.unlock = function (callback) {
post("unlock", callback);
};
/**
* Gets an array of users currently in this room.
* @param {function(Array.<User>)} callback The callback to call with the array of users.
*/
this.users = function (callback) {
get(null, function (data) {
var i, users = [];
for (i = 0; i < data.room.users.length; i++) {
users.push(new User(connection, data.room.users[i]));
}
callback(users);
});
};
/**
* Gets up to 5 recent file uploads in the room.
* @param {function(Array.<Upload>)} callback The callback to call with the array of uploads.
*/
this.recentUploads = function (callback) {
get("uploads", function (data) {
var i, uploads = [];
for (i = 0; i < data.uploads.length; i++) {
uploads.push(new Upload(connection, data.uploads[i]));
}
callback(uploads);
});
};
/**
* Gets up to 100 recent messages in the room.
* The following options are valid:
* limit - Limits the number of results (a max of 100 will be returned).
* sinceMessageId - Only retrieve messages after this message id.
* @param {Object=} options The optional options to use.
* @param {function(Array.<Message>)} callback The callback to call with the array of messages.
*/
this.recentMessages = function (options, callback) {
// Change case to what Campfire expects
if (options && options.sinceMessageId) {
options.since_message_id = options.sinceMessageId;
delete options.sinceMessageId;
}
get("recent", options, function (data) {
callback(resultToMessages(data));
});
};
/**
* Listen for messages in the room. Currently only one listener is supported,
* subsequent listeners will be ignored.
* @param {function(Message)} callback The callback to call with each message.
*/
this.listen = function (callback) {
if (self.isListening()) {
return;
}
connection.stream(urlFor("room", this.id, "live"), function (streamConnection) {
listenConnection = streamConnection;
}, function (data) {
callback(new Message(connection, data));
});
};
/**
* Stops listening for messages in this room.
*/
this.stopListening = function () {
if (self.isListening()) {
listenConnection.destroy();
listenConnection = null;
}
};
/**
* Returns whether there is currently a connection listening on this room.
* @return {boolean} Whether there is currently a connection listening.
*/
this.isListening = function () {
return !!(listenConnection);
};
/**
* Gets all messages for a specified date.
* @param {Date=} date The date to get messages for (defaults to today).
* @param {function(Array.<Message>)} callback The callback to call with the array of messages.
*/
this.transcript = function (date, callback) {
var action = "transcript";
if (typeof date === "function") {
callback = date;
date = null;
}
if (date) {
action += "/" + date.getFullYear();
action += "/" + (date.getMonth() + 1);
action += "/" + date.getDate();
}
get(action, function (data) {
callback(resultToMessages(data));
});
};
};