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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
/* Silly javascript lacks basic functionality */
function LeadingZero(s) {
let ss = s;
while (ss.toString().length < 2) {
ss = '0' + ss;
}
return ss;
}
function DateString(origdate) {
/*
Get the date in the server timezone. Javascript is basically uncapable of this, but we can
add a fixed offset, so let's do that.
Also note that date.getTimezoneOffset() returns the inverse of the actual timezone offset.
*/
let date = new Date(origdate.getTime() + (origdate.getTimezoneOffset()+parseInt(parseInt($('body').data('tzoffset'))))*60000);
return date.getFullYear()+'-'+LeadingZero(date.getMonth()+1)+'-'+LeadingZero(date.getDate())+' '+LeadingZero(date.getHours())+':'+LeadingZero(date.getMinutes());
}
function showstatus(msg, level) {
$('#statusdiv').text(msg);
$('#statusdiv').attr('class', 'alert alert-' + level);
if (level == 'success') {
$('#statusdiv').fadeIn(200).fadeOut(200).fadeIn(200);
} else {
$('#statusdiv').fadeIn(200).fadeOut(200).fadeIn(200).fadeOut(200).fadeIn(200);
}
}
function show_ajax_error(type, xhr) {
if (xhr.status == 412) {
/* 412 Precondition Failed is a controlled error from the backend code */
showstatus('Error ' + type + ': ' + xhr.responseText, 'danger');
}
else {
showstatus('Error ' + type + ': ' + xhr.status, 'danger');
}
}
function reset_state() {
$('#statusdiv').hide();
$('#queuerow').hide();
$('#incomingrow').hide();
$('#buttonrow').show();
}
function add_queue_entry_html(row, d, modbuttons, panelstyle) {
var e = $('<div/>').addClass('queueentry panel panel-' + panelstyle);
e.append($('<div/>').addClass('panel-heading').text(d['time']));
var cdiv = $('<div/>').addClass('panel-body').text(d['txt']);
if (d['hasimage']) {
cdiv.append($('<img/>').addClass('preview-image').attr('src', '?op=thumb&id=' + d['id']));
}
e.append(cdiv);
fdiv = $('<div/>').addClass('panel-footer');
let dstr = d['delivered'] ? ' Delivery complete.':'';
fdiv.append($('<p/>').text('Queued by ' + d['author'] + ' for ' + d['time'] + '.' + dstr));
if (modbuttons) {
fdiv.append($('<button/>').data('tid', d['id']).addClass('btn btn-primary btn-sm approve-button').text('Approve'));
fdiv.append($('<button/>').data('tid', d['id']).addClass('btn btn-default btn-sm discard-button').text('Discard'));
}
e.append(fdiv);
row.append(e);
}
function add_incoming_entry_html(row, d, dismissbutton, panelstyle) {
var e = $('<div/>').addClass('incomingentry panel panel-' + panelstyle).data('replyid', d['id']).data('replyto', d['author']).data('replymaxlength', d['replymaxlength']);
e.append($('<div/>').addClass('panel-heading').text('Posted by @' + d['author'] + ' (' + d['authorfullname'] + ') at ' + d['time']));
var cdiv = $('<div/>').addClass('panel-body').text(d['txt']);
if (d['media']) {
$.each(d['media'], function(i, o) {
cdiv.append($('<img/>').addClass('preview-image').attr('src', o + '?name=thumb'));
});
}
e.append(cdiv);
fdiv = $('<div/>').addClass('panel-footer');
fdiv.append($('<button/>').data('tid', d['id']).addClass('btn btn-primary btn-sm reply-button').text('Reply'));
if (dismissbutton) {
fdiv.append($('<button/>').data('tid', d['id']).addClass('btn btn-default btn-sm dismiss-incoming-button').text('Dismiss'));
}
var rtbtn = $('<button/>').data('tid', d['id']).addClass('btn btn-default btn-sm retweet-button');
if (d['rt'] == 0) {
rtbtn.text('Repost');
}
else {
rtbtn.text('Reposted');
rtbtn.attr('disabled', 'disabled');
}
fdiv.append(rtbtn);
fdiv.append($('<button/>').data('url', d['url']).addClass('btn btn-default btn-sm view-twitter-button').text('View on ' + d['provider']));
e.append(fdiv);
row.append(e);
}
var lastcheck = 0;
var lastqueue = true;
var lastincoming = true;
function check_queue() {
if ((new Date()) - lastcheck < 1000) {
console.log('Less than 1 second since last time, not checking queue')
return;
}
$.ajax({
"method": "GET",
"data": {
"op": "hasqueue",
},
success: function(data, status, xhr) {
if (data['hasqueue'] != lastqueue) {
if (data['hasqueue']) {
$('#tweetQueueButton').removeClass('btn-default').addClass('btn-primary');
}
else {
$('#tweetQueueButton').removeClass('btn-primary').addClass('btn-default');
}
if (data['hasqueue']) {
if ("Notification" in window) {
if (Notification.permission === "granted") {
var not = new Notification($('body').data('confname') + ": One or more tweets arrived in the queue to be processed");
}
}
}
lastqueue = data['hasqueue'];
}
if (data['hasincoming'] != lastincoming) {
if (data['hasincoming']) {
$('#incomingTweetsButton').removeClass('btn-default').addClass('btn-primary');
}
else {
$('#incomingTweetsButton').removeClass('btn-primary').addClass('btn-default');
}
if (data['hasincoming']) {
if ("Notification" in window) {
if (Notification.permission === "granted") {
var not = new Notification($('body').data('confname') + ": One or more incoming tweets arrived");
}
}
}
lastincoming = data['hasincoming'];
}
lastcheck = new Date();
}
});
}
$(function() {
var is_poster = $('body').data('poster');
var is_directposter = $('body').data('directposter');
var is_moderator = $('body').data('moderator');
var global_maxlength = parseInt($('body').data('maxlength'));
$('#loading').hide();
reset_state();
$('#topnavbar').click(function() {
reset_state();
});
$('#statusdiv, #completed_div').click(function() {
$('#statusdiv').hide();
$('#completed_div').hide();
});
$(document).bind('ajaxStart', function() {
$('#loading').show();
});
$(document).bind('ajaxStop', function() {
$('#loading').hide();
});
$('#newTweetModal').on('shown.bs.modal', function() {
$('#newTweetText').focus();
});
$('#newTweetButton').click(function() {
let t = DateString(new Date());
/* Must set this before we call tweet length calculation */
$('#newTweetModal').data('maxlength', global_maxlength);
if ($('body').data('initialhashtags')) {
$('#newTweetText').val("\n\n" + $('body').data('initialhashtags'));
$('#newTweetText').prop('selectionStart', 0);
$('#newTweetText').prop('selectionEnd', 0);
$('#newTweetText').trigger('input');
} else {
$('#newTweetText').val('');
$('#tweetLength').text('0');
}
$('#newTweetSchedule').val(t);
$('#newTweetSchedule').attr('min', t);
$('#newTweetUpload').val('');
$('#tweetBypassApproval').prop('checked', false);
$('#newTweetModal').data('replyid', '');
$('#newTweetModal').modal({});
});
$('#newTweetText').on('input', function() {
let maxlength = parseInt($('#newTweetModal').data('maxlength'));
/* Unfortunatley the input event cannot be canceled, so we have to backwards try to cut the text down */
let l = shortened_post_length($(this).val());
$('#tweetLength').text(l + ' of ' + maxlength);
while (l > maxlength) {
$(this).val($.trim($(this).val()).slice(0, -1));
l = shortened_post_length($(this).val());
$('#tweetLength').text(l);
}
});
$('#posttweetbutton').click(function() {
var txt = $.trim($('#newTweetText').val());
if (txt.length < 5) {
alert('Tweet too short');
return;
}
var fd = new FormData();
fd.append("op", "post");
fd.append("txt", txt);
fd.append("at", $('#newTweetSchedule').val());
fd.append("bypass", (is_directposter && $('#tweetBypassApproval').is(':checked')) ? 1:0);
if ($('#newTweetUpload')[0].files[0]) {
fd.append("image", $('#newTweetUpload')[0].files[0]);
}
if ($('#newTweetModal').data('replyid')) {
fd.append("replyid", $('#newTweetModal').data('replyid'));
}
$('#newTweetModal .disableonpost').attr("disabled", true);
$.ajax({
"method": "POST",
"dataType": "json",
"url": ".",
data: fd,
processData: false,
contentType: false,
success: function(data, status, xhr) {
$('#newTweetModal .disableonpost').removeAttr("disabled");
if ('error' in data) {
alert(data['error']);
}
else {
$('#newTweetModal').modal('hide');
showstatus('Tweet queued', 'success');
}
check_queue();
if ($('#newTweetModal').data('replyid')) {
/* If we did a reply we should remove it from the incoming queue */
$('#incomingrow .incomingentry').each(function () {
if ($(this).data('replyid') == $('#newTweetModal').data('replyid')) {
$(this).hide('fast', function() { e.remove(); });
}
});
};
},
error: function(xhr, status, thrown) {
alert('Error posting tweet: ' + xhr.status);
}
});
});
/*
* Queued outgoing tweets
*/
$('#tweetQueueButton').click(function() {
$.ajax({
"method": "GET",
"data": {
"op": "queue",
},
success: function(data, status, xhr) {
var t = (new Date()).toLocaleTimeString(navigator.language, {hour: '2-digit', minute:'2-digit', second: '2-digit', hour12: false}) + ' (' + Intl.DateTimeFormat().resolvedOptions().timeZone + ')';
/* Remove old entries */
$('#queuerow .queueentry').remove();
var row = $('#queuerow');
row.append($('<div/>').addClass('well sectionwell queueentry').text('Moderation queue at ' + t));
$.each(data['queue'], function(i, d) {
add_queue_entry_html(row, d, is_moderator, 'primary');
});
row.append($('<div/>').addClass('well sectionwell queueentry').text('Latest posts at ' + t));
$.each(data['latest'], function(i, d) {
add_queue_entry_html(row, d, false, 'info');
});
$('#buttonrow').hide();
$('#queuerow').show();
},
error: function(xhr, status, thrown) {
show_ajax_error('getting queue', xhr);
},
});
});
$(document).on('click', 'button.approve-button, button.discard-button', function(e) {
var isapprove = $(this).hasClass('approve-button');
var btn = $(this);
$.ajax({
"method": "POST",
"dataType": "json",
"url": ".",
"data": {
"op": isapprove ? "approve" : "discard",
"id": $(this).data('tid'),
},
success: function(data, status, xhr) {
if ('error' in data) {
alert(data['error']);
return;
}
/* Whichever we did, this item should be removed from the queue */
var e = btn.parent().parent();
e.hide('fast', function() { e.remove(); });
},
error: function(xhr, status, thrown) {
alert('Error updating status: ' + xhr.status);
},
});
});
// End of tweet outgoing queue
/*
* Incoming tweets
*/
$('#incomingTweetsButton').click(function() {
$.ajax({
"method": "GET",
"data": {
"op": "incoming",
},
success: function(data, status, xhr) {
var t = (new Date()).toLocaleTimeString(navigator.language, {hour: '2-digit', minute:'2-digit', second: '2-digit', hour12: false}) + ' (' + Intl.DateTimeFormat().resolvedOptions().timeZone + ')';
/* Remove old entries */
$('#incomingrow .incomingentry').remove();
var row = $('#incomingrow');
row.append($('<div/>').addClass('well sectionwell incomingentry').text('Incoming tweets at ' + t));
$.each(data['incoming'], function(i, d) {
add_incoming_entry_html(row, d, is_moderator, 'primary');
});
row.append($('<div/>').addClass('well sectionwell incomingentry').text('Processed incoming at ' + t));
$.each(data['incominglatest'], function(i, d) {
add_incoming_entry_html(row, d, false, 'info');
});
$('#buttonrow').hide();
$('#incomingrow').show();
},
error: function(xhr, status, thrown) {
show_ajax_error('getting queue', xhr);
},
});
});
$(document).on('click', 'button.dismiss-incoming-button', function(e) {
var btn = $(this);
if (!confirm('Are you sure you want to dismiss this tweet without a reply?')) {
return;
}
$.ajax({
"method": "POST",
"dataType": "json",
"url": ".",
"data": {
"op": 'dismissincoming',
"id": $(this).data('tid'),
},
success: function(data, status, xhr) {
if ('error' in data) {
alert(data['error']);
return;
}
var e = btn.parent().parent();
e.hide('fast', function() { e.remove(); });
},
error: function(xhr, status, thrown) {
alert('Error updating status: ' + xhr.status);
},
});
});
$(document).on('click', 'button.retweet-button', function(e) {
var btn = $(this);
if (!confirm('Are you sure you want to repost this?')) {
return;
}
$.ajax({
"method": "POST",
"dataType": "json",
"url": ".",
"data": {
"op": 'retweet',
"id": $(this).data('tid'),
},
success: function(data, status, xhr) {
if ('error' in data) {
alert(data['error']);
return;
}
btn.text('Reposted');
btn.attr('disabled', 'disabled');
},
error: function(xhr, status, thrown) {
alert('Error updating status: ' + xhr.status);
},
});
});
$(document).on('click', 'button.view-twitter-button', function(e) {
window.open($(this).data('url'));
});
$(document).on('click', 'button.reply-button', function(e) {
var btn = $(this);
var replyto = $(this).parent().parent().data('replyto');
$('#newTweetText').val('@' + replyto + ' ');
$('#newTweetUpload').val('');
$('#tweetBypassApproval').prop('checked', false);
$('#tweetLength').text('0');
$('#newTweetModal').data('replyid', $(this).parent().parent().data('replyid'));
$('#newTweetModal').data('maxlength', $(this).parent().parent().data('replymaxlength'));
$('#newTweetModal').modal({});
});
// End of tweet incoming queue
if ("Notification" in window) {
if (Notification.permission === "default") {
/* Have not asked before, so ask now! */
Notification.requestPermission().then(function (permission) {
/* Nothing yet, but permission is persistent */
});
}
}
$(window).on('focus', function() {
check_queue();
});
check_queue();
/* Update the buttons every 60 seconds */
setInterval(check_queue, 60*1000);
});
/*
* Functions to check length of social media posts, by adjusting for size of
* URLs.
* Number and regex should be kept in sync with js/admin.js
*/
const _re_urlmatcher = new RegExp('\\bhttps?://\\S+', 'ig');
const _url_shortened_len = 23;
function shortened_post_length(p) {
return p.replace(_re_urlmatcher, 'x'.repeat(_url_shortened_len)).length;
}
|