diff options
| author | Pepper <pepper@scannerjammer.com> | 2015-05-20 11:16:13 -0400 |
|---|---|---|
| committer | Pepper <pepper@scannerjammer.com> | 2015-05-20 11:16:13 -0400 |
| commit | a4916103efb2d97896c456ff0e83064b21e85d25 (patch) | |
| tree | b3eb529e4b96375109626bbeada35d4f8a2667ee /frontend/static/js/src/chat.js | |
| parent | 3790eedc2f48c725c586b8c7b924875fedbeb7b4 (diff) | |
first commit in a while
Diffstat (limited to 'frontend/static/js/src/chat.js')
| -rw-r--r-- | frontend/static/js/src/chat.js | 288 |
1 files changed, 288 insertions, 0 deletions
diff --git a/frontend/static/js/src/chat.js b/frontend/static/js/src/chat.js new file mode 100644 index 0000000..689dad6 --- /dev/null +++ b/frontend/static/js/src/chat.js @@ -0,0 +1,288 @@ +var VIMEOregexp = /^(\bhttps?:\/\/)(www.)?vimeo.com\/([0-9]+).*$/i +var Chat = + { + timer: null, + oldChat: {}, + oldVideo: {}, + lastPoll: 0, + delay: 1000, + delayShort: 1000, + delayLong: 5000, + messages: {}, + callback: false, + parse: function (row) + { + var domain = window.location.hostname.split('.').slice(-2).join('.') + var s = '<a href="http://'+row[2]+"."+domain+'/" class="u">' + row[2] + "</a> <span>" + s += Chat.parseWords(row[3],row[0]) + s += "</span><br />" + return s + }, + parseWords: function (raw,id) + { + if (! raw) + return "" + var words = raw.split(" ") + var s = "" + for (i in words) + { + var word = words[i] + if (word.indexOf("http") !== -1) + { + if (word.indexOf("youtube.com/watch?") !== -1) + { + var ytid = "youtube_"+Youtube.getYtid(word) + var txt + if (ytid in Player.videos) + txt = Player.videos[ytid].title + else + txt = word + s += '<a href="'+word+'" class="ytlink" id="'+ytid+'" target="_parent">'+txt+'</a> ' + } + else if (word.indexOf("youtube.com/v/") !== -1) + { + var index = word.indexOf("/v/") + var ytid = "youtube_"+word.substr(index+3,11) + var txt + if (ytid in Player.videos) + txt = Player.videos[ytid].title + else + txt = word + s += '<a href="'+word+'" class="ytlink" id="'+ytid+'" target="_parent">'+txt+'</a> ' + } + else if (word.indexOf("youtu.be") !== -1) + { + var ytid = "youtube_"+word.substr(16,11) + var txt + if (ytid in Player.videos) + txt = Player.videos[ytid].title + else + txt = word + s += '<a href="'+word+'" class="ytlink" id="'+ytid+'" target="_parent">'+txt+'</a> ' + } + // http://www.youtube.com/user/ahchachachacha#p/f/28/1GSBekxLR1E + else if (word.indexOf("youtube.com/user") !== -1) + { + var ytid = "youtube_"+word.substr(-11) + var txt + if (ytid in Player.videos) + txt = Player.videos[ytid].title + else + txt = word + s += '<a href="'+word+'" class="ytlink" id="'+ytid+'" target="_parent">'+txt+'</a> ' + } + else if (word.indexOf("vimeo.com") !== -1) + { + var vimeoid = word.replace(VIMEOregexp, "vimeo_$3") + if (vimeoid in Player.videos) + txt = Player.videos[vimeoid].title + else + txt = word + s += '<a href="'+word+'" class="ytlink" id="'+vimeoid+'" target="_parent">'+txt+'</a> ' + } + else if (word.indexOf("soundcloud.com") !== -1) + { + var scid = "soundcloud_" + $.md5(word) + if (scid in Player.videos) + txt = Player.videos[scid].title + else + txt = word + s += '<a href="'+word+'" class="ytlink" id="'+scid+'" target="_parent">'+txt+'</a> ' + } + else if (word.indexOf(".jpeg") !== -1 || + word.indexOf(".JPG") !== -1 || + word.indexOf(".GIF") !== -1 || + word.indexOf(".PNG") !== -1 || + word.indexOf(".JPEG") !== -1 || + word.indexOf(".jpg") !== -1 || + word.indexOf(".gif") !== -1 || + word.indexOf(".png") !== -1) + { + s += + '<a href="'+word+'" target="_blank" class="pic">'+ + '<img src="'+word+'" />'+ + '</a><br>'; + if(id){ + s+='<span id="like_'+id+'" class="like img_like" onClick="Like.likeContent({id:'+id+'})">like</span>'; + } + } + else if (word.indexOf("scannerjammer.com/profile") !== -1) + { + var username = word.substr( word.indexOf("profile")+8 ).replace("/","") + s += '<a href="'+word+'">@'+username+'</a>' + } + // else if (word.indexOf("@") === 0 && word.length > 2) + // { + // } + else + { + var poffset = word.indexOf('//') + var linktext = word.substr(poffset+2, word.indexOf('/', poffset+2) - 2).replace("www.","").replace(/\/+$/,"") + s += '<a href="'+word+'" target="_blank">'+linktext+'</a> ' + } + } + else if (word.indexOf(".com") !== -1 || + word.indexOf(".net") !== -1 || + word.indexOf(".org") !== -1 || + word.indexOf(".us") !== -1 || + word.indexOf(".nu") !== -1 || + word.indexOf(".uk") !== -1 || + word.indexOf(".fr") !== -1 || + word.indexOf(".de") !== -1 || + word.indexOf(".fm") !== -1) + { + var txt = word.replace("www.","") + s += '<a href="http://'+word+'" target="_blank">'+txt+'</a> ' + } + else + s += word + " " + } + return s + }, + store: function (lines) + { + var newVideos = [] + var newChat = [] + var postponeScroll = false + for (i in lines) + { + if (! lines[i]) + continue + row = lines[i].split("\t") + if (row[0] === 'VIDEO') + { + row.shift() + if (row[0] in Chat.oldVideo) + continue + Chat.oldVideo[row[0]] = row + Playlist.enqueueOldVideoFormat([row]) + } + else if (row[0] === 'ROOM') + { + Room.updateSetting(row[1],row[2]) + } + else if (row[0] === 'LIKE') + { + username = row[1] + Like.enqueue(username) + } + else if (row[0] === 'CAM') + { + VideoChat.updateCount(row[1]) + } + else + { + // 0 id 1 date 2 user 3 msg + if (row[0] in Chat.oldChat) + continue + Chat.oldChat[row[0]] = row + var c = Chat.parse(row) + if (c.indexOf("<img") !== -1) + { + postponeScroll = true + d.joy(">> POSTPONING") + } + if (row[2] === Auth.username && $.md5(row[3]) in Chat.messages) + continue + newChat.push(c) + } + } + if (newChat.length) + { + $("#chat").append(newChat.join("")) + if (postponeScroll) + setTimeout('d.scrollToBottom("#chat")', 2000) + else + d.scrollToBottom("#chat") + } + }, + say: function () + { + d.act("+ sent message") + var msg = d.sanitize( $("#chat-message").val() ) + $("#chat-message").val("") + if (! msg) return + if (msg === "debug=1") { $("#msg").show(); d.scrollToBottom("#msg"); return } + if (msg === "debug=0") { $("#msg").hide(); return } + if (msg === "poll=0") { d.error("+ DISABLED POLLING"); clearTimeout(Chat.timer); return} + var hash = $.md5(msg) + Chat.messages[hash] = true + var newrow = [0, 0, Auth.username, msg] + var newdiv = Chat.parse(newrow) + $("#chat").append(newdiv) + // if (Chat.callback) + // Chat.callback(1) + if (newdiv.indexOf("<img") !== -1) + setTimeout('d.scrollToBottom("#chat")', 2000) + $.post(API.URL.room.say, {room: Room.name, session: Auth.session, msg: msg}, Room.sayCallback) + d.scrollToBottom("#chat") + }, + send: function (msg) + { + $.post(API.URL.room.say, {room: Room.name, session: Auth.session, msg: msg}, Room.sayCallback) + // var hash = $.md5(msg) + // Chat.messages[hash] = true + // var newrow = [0, 0, Auth.username, msg] + // var newdiv = Chat.parse(newrow) + // $("#chat").append(newdiv) + // if (newdiv.indexOf("<img") !== -1) + // setTimeout('d.scrollToBottom("#chat")', 2000) + // d.scrollToBottom("#chat") + }, + sayCallback: function (raw) + { + var lines = API.parse("/room/say", raw) + if (! lines) return + var newid = lines.split("\t")[0] + Chat.oldChat[newid] = true + // Room.store(lines) + d.joy("MESSAGE SENT") + }, + poll: function () + { + // d.warn("Polling") + $.post(API.URL.room.poll, + { + room: Room.name, + session: Auth.session, + last: Chat.lastPoll, + cam: VideoChat.isOpen, + }).success(Chat.pollCallback).error(Chat.pollErrorCallback) + }, + pollErrorCallback: function () + { + d.error("Poll failed, waiting "+Math.floor(Chat.delayLong)+"s...") + Chat.timer = setTimeout(Chat.poll, Chat.delayLong) + }, + pollCallback: function (raw) + { + // d.warn("Poll successful") + Chat.timer = setTimeout(Chat.poll, Chat.delay) + var lines = API.parse("/room/poll", raw) + if (! lines) + return d.error("Poll failed") + Chat.lastPoll = parseInt(lines.shift()) - 1 + Lastlog.update(lines.shift()) + Chat.store(lines) + } + } + +var Lastlog = + { + old: "", + update: function (lastlog) + { + if (Lastlog.old === lastlog) + return + Lastlog.old = lastlog + var names = lastlog.split("\t") + var s = "" + var domain = window.location.hostname.split('.').slice(-2).join('.') + for (i in names.sort()) + { + s += "<li class='ll'><a href='http://"+names[i]+"."+domain+"/'>"+names[i]+"</a></li>" + } + $("#lastlog").html(s) + $("#lastlogbg").css("height", $("#lastlogbox").height()) + } + } |
