forked from Fenrirthviti/stream-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrachni.js
243 lines (224 loc) · 6.95 KB
/
rachni.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
/**
* Created by Joel on 10/27/2016.
*/
$(function () {
let pauseHeartbeat = false;
let heartbeatXHR;
let recIconName = $('.record-button').eq(0).next('i').text();
let lastid = 0;
let toggled = false;
var scrolledPct = 0;
/** CHAT FUNCTIONS **/
// Chatbox show/hide
$("#toggleChat").click(function () {
if (toggled) {
$('.chat-container').stop().animate({'width': '20%'}, 500);
$('.video-container').stop().animate({'width': '80%'}, 500);
toggled = false;
} else {
$('.chat-container').stop().animate({'width': '0%'}, 500);
$('.video-container').stop().animate({'width': '100%'}, 500);
toggled = true;
}
});
// Join request sent on page load, but only if chat is present on the page.
if (ischat === true) {
$.ajax({
url: "/api/" + api_key + "/chat/join/" + current_channel,
dataType: 'json'
});
}
// Leave request sent on page unload
$(window).on('beforeunload', function () {
if (ischat === true) {
$.ajax({
url: "/api/" + api_key + "/chat/leave/" + current_channel,
dataType: 'json'
});
}
});
// Simple regex for identifying URLs in the chat.
function urlify(text) {
let regex = /(https?:\/\/[^\s]+)/g;
return text.replace(regex, '<a href="$1">$1</a>')
}
// Check for new messages every 500ms and output to chatbox
setInterval(function () {
let chatbox = $('#output .mCSB_container');
$.ajax({
url: "/api/" + api_key + "/chat/read/" + current_channel,
dataType: 'json'
}).done(function (getLines) {
let scrollHeight = chatbox.prop('scrollHeight') - chatbox.height();
$.each(getLines, function (id, line) {
id = parseInt(line.id, 10);
let type = line.type;
if (lastid < id) {
let unixTimeStamp = line.timestamp;
let timestampInMilliSeconds = unixTimeStamp * 1000;
let date = new Date(timestampInMilliSeconds);
let formattedDate = date.format('h:i a');
lastid = id;
if (type === 'SYSTEM') {
if (jp_status === 't') {
chatbox.append(" <span style='color: rgb(179, 179, 179);'>(" + formattedDate + ')<span style="font-style: italic"> ' + line.sender + ' ' + urlify(line.message) + '</span></span><br />');
}
} else {
chatbox.append('(' + formattedDate + ") <span style='color: rgb(0, 188, 212); font-weight: bold'>" + line.sender + ':</span> ' + urlify(line.message) + '<br />');
}
}
});
if (scrolledPct === 100) {
$('#output').mCustomScrollbar('scrollTo', 'bottom');
}
});
},
500
);
// Capture the input box so that enter submits a message instead of newline, but still allow for shift+enter
$("#inputMessage").keypress(function (e) {
if (e.which == 13 && !e.shiftKey) {
$(this).closest("form").submit();
e.preventDefault();
return false;
}
});
// Submit message to chat API
$('#chatMessage').submit(function (event) {
const data = {
'message': $('#inputMessage').val(),
'timestamp': Math.round(new Date().getTime() / 1000),
'user': display_name,
'channel': current_channel,
'type': 'USER'
};
event.preventDefault();
$.ajax({
type: "POST",
url: "/api/" + api_key + "/chat/write/",
data: data,
dataType: 'json'
});
$('#inputMessage').val("").parent('div').removeClass('is-dirty');
});
/** CHANNEL FUNCTIONS **/
// Get current recording status and set record button state
setInterval(function () {
if (!pauseHeartbeat) {
heartbeatXHR = $.getJSON('/api/' + api_key + '/stream/ping', function (info) {
$('.record-button').each(function () {
let channel = $(this).parent('label').parent('td').parent('tr').attr('channel');
if (typeof info[channel] !== 'undefined') {
if (info[channel].recording === true && $(this).not(':checked')) {
$(this).prop('checked', true);
$(this).parent('label').addClass('is-checked');
$(this).next('i').text('stop');
} else if (info[channel].recording === false && $(this).is(':checked')) {
$(this).prop('checked', false);
$(this).parent('label').removeClass('is-checked');
$(this).next('i').text(recIconName);
}
}
});
});
}
}, 500);
// Recording start/stop on icon button click
$('.record-button').click(function () {
let channel = $(this).parent('label').parent('td').parent('tr').attr('channel');
let icon = $(this).next('i');
pauseHeartbeat = true;
heartbeatXHR.abort();
if ($(this).is(':checked')) {
icon.text('stop');
$.getJSON('/api/' + api_key + '/stream/record-start/' + channel, function (recordingPath) {
if (recordingPath === "") {
console.log('Error starting recording');
} else {
console.log(recordingPath);
}
pauseHeartbeat = false;
});
} else {
icon.text(recIconName);
$.getJSON('/api/' + api_key + '/stream/record-stop/' + channel, function (recordingPath) {
if (recordingPath === "") {
console.log('Error stopping recording');
} else {
console.log(recordingPath);
}
pauseHeartbeat = false;
});
}
});
// Subscribe button functions
$('#subButton').click(function () {
let snackbarContainer = document.querySelector('#subToast');
let button = $(this);
let channel = $(this).attr('channel');
let action = $(this).text();
'use strict';
if (action === 'Unsubscribe') {
console.log('Action = Unsubscribe');
$.getJSON('/api/' + api_key + '/subscription/remove/' + channel, function (result) {
if (result === false) {
console.log('Error unsubscribing');
} else {
const data = {
message: 'Unsubscribed from ' + stream_key + '.',
timeout: 5000
};
console.log(result);
button.text('Subscribe');
button.css('background-color', '');
snackbarContainer.MaterialSnackbar.showSnackbar(data);
}
});
} else if (action === 'Subscribe') {
console.log('Action = Subscribe');
$.getJSON('/api/' + api_key + '/subscription/add/' + channel, function (result) {
if (result === false) {
const data = {
message: 'Error subscribing (probably already subscribed)!',
timeout: 5000
};
console.log('Error subscribing' + result);
snackbarContainer.MaterialSnackbar.showSnackbar(data);
} else {
const data = {
message: 'Subscribed to ' + stream_key + '!',
timeout: 5000
};
console.log(result);
button.text('Unsubscribe');
button.css('background-color', '#00bcd4');
snackbarContainer.MaterialSnackbar.showSnackbar(data);
}
});
}
});
// File upload name update workaround
$('#avatar').change(function () {
document.getElementById("file").value = this.files[0].name;
});
//$('.nano').nanoScroller({alwaysVisible: true});
$(window).load(function () {
$(".scrollContent").mCustomScrollbar({
theme: "inset",
scrollInertia: 400,
scrollButtons: {enable: true}
});
});
$("#output").mCustomScrollbar({
theme: "inset",
scrollInertia: 400,
callbacks: {
onInit: function () {
$("#output").mCustomScrollbar('scrollTo', 'bottom');
},
whileScrolling: function () {
scrolledPct = this.mcs.topPct;
},
}
});
});