// Messages function buildMsgContent(content, recips) { if (content.substr(0,6) == "") 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 '
' + '' + nick + '' + ' ' + '' + '' + builtContent + '' + '
'; } 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 }); }