summaryrefslogtreecommitdiff
path: root/frontend/static/js/avatar.js
blob: ce97617d56ebdef45083edac452bf733ca1e1f3f (plain)
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
var Avatar =
	{
	orientation: true,
	loaded: false,
	}
Room.loadCallback = function () { setTimeout(Viewport.fullscreenOn, 500) }
Chat.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)
			{
			if (Avatar.loaded)
				{
				$("#chat").append(newChat.join(""))
				}
			else
				{
				Avatar.loaded = true
				$("#chat").append(newChat[newChat.length-1])
				}
			if (postponeScroll)
				setTimeout('d.scrollToBottom("#chat")', 2000)
			else
				d.scrollToBottom("#chat")
			}
		}
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}
		if (msg.indexOf("bg=") === 0) { Room.setBg( msg.split("=",2)[1] ); 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 (newdiv.indexOf("<img") !== -1)
		// 	setTimeout('d.scrollToBottom("#chat")', 2000)
		if (msg.indexOf("scannerjammer.fm/avatar") === -1)
			msg = "http://scannerjammer.fm/avatar/" + d.choice(AVATARS) + " " + msg
		$.post(API.URL.room.say, {room: Room.name, session: Auth.session, msg: msg}, Room.sayCallback)
		d.scrollToBottom("#chat")
		}
Chat.parse = function (row)
		{
		return Chat.parseWords(row[3])
		},
Chat.parseWords = function (raw)
		{
		var words = raw.split(" ")
		var s = ""
		for (i in words)
			{
			var avatar = ""
			var word = words[i]
			if (word.indexOf("http") !== -1)
				{
				if (word.indexOf("scannerjammer.fm/avatar") !== -1)
					{
					avatar = word
					}
				else 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>'
					}
				else if (word.indexOf("scannerjammer.fm/profile") !== -1)
					{
					var username = word.substr( word.indexOf("profile")+8 ).replace("/","")
					s += '<a href="'+word+'">ScannerJammer: '+username+'</a>'
					}
				else
					{
					var poffset = word.indexOf('//')
					var linktext = word.substr(poffset+2, word.indexOf('/', poffset+2)).replace("www.","")
					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 + " "
			}
		if (! avatar.length)
			avatar = d.choice(AVATARS)
		Avatar.orientation = ! Avatar.orientation
		if (Avatar.orientation)
			{
			var q = '<div class="frame"><table border="0"><tr><td valign="top" class="avatar-left"><img src="'+ avatar +'"/></td>'
			q += '<td valign="top" class="tri"><div class="triangle-left"></div></td><td class="message">'
			q += '<div class="message-blurb">' + s + '</div></td></tr></table></div>'
			return q
			}
		else
			{
			var q = [
				'<div class="frame">',
				'<table border="0"><tr><td valign="top" class="message"><div class="message-blurb">',
				s,
				'</div></td><td valign="top" class="tri"><div class="triangle-right"></div></td><td valign="top" class="avatar-right">',
				'<img src="',
				avatar,
				'" /></td></tr></table></div>'
				].join("")
			return q
			}

		return s
		}