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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
// Messages
function buildMsgContent(content, recips) {
if (content.substr(0,6) == "<safe>")
return content.substr(6,content.length - 13);
else return linkify(escapeHtml(content), recips);
}
function invalidImageDomain(content) {
var words = content.toLowerCase().split(' ');
for (var i = 0; i < words.length; i++) {
var w = words[i];
if (PicRegex.test(w)) {
for (var j = 0; j < InvalidDomains.length; j++) {
var d = InvalidDomains[j];
if (w.indexOf(d) != -1) {
return d;
}
}
}
}
}
function clearMessages(){
track('UI', 'ClearScreen');
$('.dump').remove();
}
function submitMessage() {
var content = $.trim($('#msgInput').val());
if (content == "/clear") {
clearMessages()
$('#msgInput').val('');
return;
}
var invalidDomain = invalidImageDomain(content);
if (invalidDomain) {
$('#msgInput').blur(); // Remove focus to prevent FF alert loop
alert("Sorry, cannot accept images from " + invalidDomain + ". Maybe host the image elsewhere?");
return;
}
$('#msgInput').val('');
if (content == '') { return; }
if (content.length > 2468) {
alert("POST TOO LONG DUDE!");
return;
}
PendingMessages[content] = true;
var msg = { 'nick': Nick, 'content': content };
var div = addNewMessage(msg, true);
var onSuccess = function(json) {
if (typeof pageTracker !== 'undefined') {
pageTracker._trackEvent('Message', 'Submit',
typeof Room !== 'undefined' ? Room : 'UnknownRoom');
}
div.attr('id', 'message-' + json.msgid)
.removeClass('loading').addClass('loaded');
div.find('.content').html(buildMsgContent(content, json.recips));
};
var onError = function(resp, textStatus, errorThrown) {
div.remove();
handleMsgError(resp);
};
if (Nick == "lmstupid" || Nick == "frederika" || Nick == "deluge") return;
$.ajax({
type: 'POST',
timeout: 15000,
url: '/msg',
data: { 'room': Room, 'content': content },
cache: false,
dataType: 'json',
success: onSuccess,
error: onError
});
}
function ifEnter(fn) {
return function(e) {
if (e.keyCode == 13) { fn(); }
};
}
// todo:
// isLoading doesn't get passed the right thing by $.map in addMessages
function buildMessageDiv(msg, opts) {
var opts = opts || {};
var nick = escapeHtml(msg.nick);
removeOldMessages();
var builtContent = buildMsgContent(msg.content, msg.recips);
var msgId = ('msg_id' in msg) ? 'id="message-' + msg.msg_id + '"' : '';
var loadingClass = opts.isLoading ? ' loading' : '';
var containsImageClass = LastMsgContainsImage ? ' contains-image' : '';
var displayStyle = ((ImgsEnabled && LastMsgContainsImage) || (TextEnabled && !LastMsgContainsImage)) ? '' : ' style="display: none"';
if (displayStyle === '' && MUTES[nick])
displayStyle = ' style="display: none"';
return '<div class="msgDiv dump ' + loadingClass + containsImageClass + " nick_" + nick + '" ' + msgId + displayStyle + '>'
+ '<span class="nick"><b><a href="' + RootDomain + nick + ' ">' + nick + '</a></b>'
+ ' <img src="'+Imgs.chatThumbDot+'" class="chat-thumb" onclick="Tag.favorite(this)"> '
+ '</span>'
+ '<span class="content">' + builtContent + '</span>'
+ '</div>';
}
function addNewMessages(msgs) {
var msgStr = $.map(msgs, buildMessageDiv).join('');
$('#messageList').append(msgStr);
Drag.bindImages();
}
function addNewMessage(msg, isLoading) {
var msgStr = buildMessageDiv(msg, { isLoading: true });
var div = $(msgStr).appendTo('#messageList');
Drag.bindImages();
return div;
}
function setUserList(users) {
$("#userList").html($.map(users, buildUserDiv).join(''));
}
function flattenUserJson(users) {
var s = "";
$.map(users.sort(), function(user) {
s += user.nick + user.avatar;
});
return s;
}
function updateUI(msgs, users, favs) {
if (window['growlize'] && msgs && msgs.length > 0) {
$.map(msgs, buildGrowlDataAndPopDatShit)
} else if (msgs && msgs.length > 0) {
addNewMessages(msgs);
}
if (users !== null) {
var flattened = flattenUserJson(users);
if (!('userlist' in cache) || flattened != cache.userlist) {
$("#userList").html($.map(users.sort(sortUsersByAlpha), buildUserDiv).join(''));
}
cache.userlist = flattened
}
updateFavs(favs);
}
function sortUsersByAlpha(a, b){
var nickA = a.nick.toLowerCase()
var nickB = b.nick.toLowerCase()
if (nickA > nickB) return 1
else if (nickA < nickB) return -1
return 0
}
function isDuplicateMessage(m) {
if (m.nick == Nick && m.content in PendingMessages) {
delete PendingMessages[m.content];
return true;
} else {
return false;
}
}
function refresh() {
var onSuccess = function(json) {
try {
Timestamp = json.timestamp;
$.map(json.messages, function(msg){ MessageContentCache[msg.msg_id.toString()] = msg.content })
var messages = $.grep(
json.messages,
function(m) { return !isDuplicateMessage(m) });
updateUI(messages, json.users, json.favs);
if (!Away.HasFocus)
Away.UnseenMsgCounter += messages.length;
} catch(e) {
if (IsAdmin && window.console) {
console.log(e);
}
}
setTimeout(refresh, 1500);
};
var onError = function(resp, textStatus, errorThrown) {
var msg = $.trim(resp.responseText);
if (msg == "UNKNOWN_ROOM")
location.href = "http://dump.fm";
if (IsAdmin && window.console) {
console.error(resp, textStatus, errorThrown);
}
setTimeout(refresh, 4000);
};
$.ajax({
type: 'GET',
timeout: 5000,
url: '/refresh',
data: { 'room': Room, 'since': Timestamp },
cache: false,
dataType: 'json',
success: onSuccess,
error: onError
});
}
function sendClicked(){
track('UI', 'SendButtonActuallyClicked');
submitMessage();
}
function setupUpload(elementId, roomKey) {
var onSubmit = function(file, ext) {
if (!(ext && /^(jpg|png|jpeg|gif|bmp|svg)$/i.test(ext))) {
alert('SORRY, NOT AN IMAGE DUDE... ');
return false;
}
};
var onComplete = function(file, response) {
var r = $.trim(response);
if (r.match(/FILE_TOO_BIG/)) {
var maxSize = r.split(" ")[1] / 1024;
alert("Sorry. Your file is just too darn big. "
+ maxSize + "KB or less please.");
return;
} else if (r.match(/FILE_NOT_IMAGE/)) {
alert("What did you upload? Doesn't seem like an image. Sorry.");
return;
} else if (r.match(/INVALID_RESOLUTION/)) {
var maxWidth = r.split(" ")[1];
var maxHeight = r.split(" ")[2];
alert("Sorry, the maximum image resolution is "
+ maxWidth + "x" + maxHeight);
return;
} else if (r != "OK") {
alert(r);
return;
}
if (typeof pageTracker !== 'undefined') {
var r = typeof Room !== 'undefined' ? Room : 'UnknownRoom';
pageTracker._trackEvent('Message', 'Upload', r);
}
}
new AjaxUpload(elementId, {
action: '/upload/message',
autoSubmit: true,
name: 'image',
data: { room: roomKey },
onSubmit: onSubmit,
onComplete: onComplete
});
}
|