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
|
var is_iphone =
navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i);
var is_ipad = navigator.userAgent.match(/iPad/i);
var is_android = navigator.userAgent.match(/Android/i);
var is_mobile = is_iphone || is_ipad || is_android;
var is_desktop = !is_mobile;
document.body.classList.add(is_desktop ? "desktop" : "mobile");
function choice(a) {
return a[randint(a.length)];
}
function csrf() {
return $("[name=_csrf]").attr("value");
}
function bold_terms(s, terms) {
s = sanitizeHTML(s);
terms.forEach((term) => {
s = s.replace(new RegExp(term, "ig"), "<b>" + term + "</b>");
});
return s;
}
function querystring(opt) {
return (
"?" +
Object.keys(opt)
.map((key) => {
return encodeURIComponent(key) + "=" + encodeURIComponent(opt[key]);
})
.join("&")
);
}
function commatize(n, radix) {
radix = radix || 1000;
var nums = [],
i,
counter = 0,
r = Math.floor;
if (radix !== -1 && n > radix) {
n /= radix;
nums.unshift(r((n * 10) % 10));
nums.unshift(".");
}
do {
i = r(n % 10);
n = r(n / 10);
counter += 1;
if (n && !(counter % 3)) {
i = " " + r(i);
}
nums.unshift(i);
} while (n);
return nums.join("");
}
function privacy_dot(p) {
if (!p) return "·";
else return ".:";
}
var short_months = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" ");
function verbose_date(date, no_pad_hours) {
var date = new Date(date * 1000);
var d = date.getDate();
var m = date.getMinutes();
var h = date.getHours();
var meridian;
if (h == 0) {
h = 12;
meridian = " am";
} else if (h == 12) {
meridian = " pm";
} else if (h > 12) {
h -= 12;
meridian = " pm";
} else {
meridian = " am";
}
if (d < 10) d = "0" + d;
if (m < 10) m = "0" + m;
if (!no_pad_hours && h < 10) h = "0" + h;
// non-breaking hyphen: ‑
var date =
d +
" " +
short_months[date.getMonth()] +
" " +
date.getFullYear();
var time = h + ":" + m + meridian;
return [date, time];
}
function carbon_date(date, no_bold) {
var span = +new Date() / 1000 - date;
if (!no_bold && span < 86400) {
// modified today
color = "new";
} else if (span < 604800) {
// modifed this week
color = "recent";
} else if (span < 1209600) {
// modifed 2 weeks ago
color = "med";
} else if (span < 3024000) {
// modifed 5 weeks ago
color = "old";
} else if (span < 12315200) {
// modifed 6 months ago
color = "older";
} else {
color = "quiet";
}
return color;
}
function hush_views(n, bias, no_bold) {
var txt = commatize(n, 1000);
bias = bias || 1;
n = n || 0;
if (n < 30) {
return ["quiet", n + " v."];
}
if (n < 200) {
return ["quiet", txt + " v."];
} else if (n < 500) {
return ["old", txt + " v."];
} else if (n < 1000) {
return ["med", txt + " v."];
} else if (n < 5000) {
return ["recent", txt + " kv."];
} else if (no_bold || n < 10000) {
return ["recent", txt + " kv."];
} else {
return ["new", txt + " kv."];
}
}
function hush_threads(n, bias, no_bold) {
var txt = commatize(n, -1);
bias = bias || 1;
n = n || 0;
if (n < 10) {
return ["quiet", n + " t."];
} else if (n < 25) {
return ["old", txt + " t."];
} else if (n < 50) {
return ["med", txt + " t."];
} else if (no_bold || n < 100) {
return ["recent", txt + " t."];
} else {
return ["new", txt + " t."];
}
}
function hush_size(n, bias, no_bold) {
var txt = commatize(Math.floor(n / 1024), 1000);
bias = 1 || bias;
n = n || 0;
if (n < 1024) {
return ["quiet", n + " b."];
}
if (n < 1000 * 1000) {
return ["quiet", txt + " kb."];
}
if (n < 20000000 / bias) {
return ["quiet", txt + " mb."];
}
if (n < 50000000 / bias) {
return ["old", txt + " mb."];
}
if (n < 80000000 / bias) {
return ["med", txt + " mb."];
}
if (no_bold || n < 170000000 / bias) {
return ["recent", txt + " mb."];
}
if (n >= 10000 * 1000 * 1000) {
console.log(n);
txt = commatize(Math.floor(n / (1024 * 1024 * 1024)));
return ["new", txt + " gb."];
}
return ["new", txt + " mb."];
}
function hush_null(n, unit, no_bold) {
n = commatize(n, -1);
var s = unit ? n + " " + unit + "." : n;
if (n < 3) {
return ["quiet", s];
} else if (n < 6) {
return ["older", s];
} else if (n < 10) {
return ["old", s];
} else if (n < 16) {
return ["med", s];
} else if (no_bold || n < 21) {
return ["recent", s];
} else {
return ["new", s];
}
}
function courtesy_s(n, s) {
return n == 1 ? "" : s || "s";
}
var revision_letters = "z a b c d f g h j k l m n p q r s t v w x y".split(" ");
function get_revision(thread) {
if (!thread.revision) return "";
var rev = thread.revision;
var n = 0;
var digits = "";
do {
n = rev % 21;
rev = Math.floor(rev / 21);
digits = revision_letters[n] + digits;
} while (rev !== 0);
return digits;
}
function get_age(t, long) {
var age = Math.abs(Date.now() / 1000 - t);
var r = Math.floor;
var m;
if (age < 5) {
return "now";
}
if (age < 60) {
return r(age) + (long ? " seconds" : "s");
}
age /= 60;
if (age < 60) {
return r(age) + (long ? " minutes" : "m");
}
m = r(age % 60);
age /= 60;
if (m > 0 && age < 2) {
return r(age) + (long ? " hours " + m + " minutes" : "h" + m + "m");
}
if (age < 24) {
return r(age) + (long ? " hours" : "h");
}
age /= 24;
if (age < 7) {
return r(age) + (long ? " days" : "d");
}
age /= 7;
if (age < 12) {
return r(age) + (long ? " weeks" : "w");
}
age /= 4;
if (age < 12) {
return r(age) + (long ? " months" : "m");
}
age /= 12;
return r(age) + (long ? " years" : "y");
}
const age_scale = [
[0, 1.0],
[60 * 10, 1.0],
[60 * 60, 0.95],
[60 * 60 * 8, 0.9],
[60 * 60 * 24, 0.85],
[60 * 60 * 24 * 7, 0.8],
[60 * 60 * 24 * 7 * 4, 0.75],
[60 * 60 * 24 * 7 * 4 * 12, 0.7],
];
function get_age_opacity(t) {
var age = Math.abs(Date.now() / 1000 - t);
return get_scale_opacity(age, age_scale);
}
const size_scale = [
[0, 0.0],
[1, 0.7],
[5, 0.8],
[10, 0.9],
[15, 0.95],
[20, 0.99],
];
function get_size_opacity(n) {
return get_scale_opacity(n, size_scale);
}
/**
* find a value on one axis of an array of points, and return the proportional point
* @param {Array} value a value, to be matched on the first column of scale
* @param {Array} scale an array of points: (value, target)
* @return {Number} a proportional point within the closest target values
*/
function get_scale_opacity(value, scale) {
for (let i = 1; i < scale.length; i++) {
const [max_value, max_lerp] = scale[i];
if (value === max_value) {
return max_lerp;
}
if (value < max_value) {
const [min_value, min_lerp] = scale[i - 1];
return lerp(norm(value, min_value, max_value), min_lerp, max_lerp);
}
}
return scale[scale.length - 1][1];
}
function trimComment(isViewingThread) {
return function (comment) {
return isViewingThread || comment.comment.length < 256
? comment
: {
...comment,
comment:
comment.comment
.split("\n")
.slice(0, 5)
.join("\n")
.substr(0, 512)
.replace(/\s+\w+$/, "") +
`... <a href="/stream/thread/${comment.thread}" class="readMore">Read more...</a>`,
};
};
}
function tidy_urls(s, short_urls) {
var ret = s
.split("\n")
.map(function (line) {
if (line.indexOf("<") !== -1) {
return line;
}
return line.replace(/https?:\/\/[^ ]+/g, function (url) {
if (is_image(url)) {
return (
'<a href="' +
url +
'" target="_blank" rel="noopener noreferrer"><img src="' +
url +
'"></a>'
);
} else if (short_urls) {
return (
'<a href="' +
url +
'" target="_blank" rel="noopener noreferrer">[' +
get_domain(url) +
"]</a>"
);
} else {
return (
'<a href="' +
url +
'" target="_blank" rel="noopener noreferrer">' +
url +
"</a>"
);
}
});
})
.join("\n")
.replace(/\r/g, "")
.replace(/<code>\n/g, "<code>")
.replace(/<\/code>\n/g, "</code>");
return ret;
}
function get_domain(url) {
return url
.replace(/https?:\/\//, "")
.replace(/\/.*/, "")
.replace(/www\./, "");
}
function is_image(url) {
return !!url.match(/\.(gif|jpe?g|png)(\?.*)?$/i);
}
function make_link(file) {
if (file.storage) {
return (
"//s3.amazonaws.com/" +
file.storage +
sdk.opt.s3.path +
"/data/" +
file.thread +
"/" +
encodeURIComponent(file.filename)
);
}
if (file.filename.indexOf("http") !== 0) {
return "/data/" + file.thread + "/" + encodeURIComponent(file.filename);
}
return file.filename;
}
function profile_image(username) {
return "//s3.amazonaws.com/i.asdf.us/bucky/profile/" + username + ".jpg";
}
function make_thumb(file) {
if (file.storage) {
return (
"//s3.amazonaws.com/" +
file.storage +
sdk.opt.s3.path +
"/data/" +
file.thread +
"/" +
encodeURIComponent(file.filename)
);
}
if (file.filename.indexOf("http") !== 0) {
return "/data/" + file.thread + "/" + file.filename;
}
return "/data/" + file.thread + "/" + file.filename;
// var partz = file.filename.toLowerCase().split("/")
// return partz.splice(partz.length-2, 0, ".thumb").join("/")
}
var metadataTemplate = $(".metadata_template").html();
function metadata(thread) {
var datetime = verbose_date(thread.createdate, true);
var age = get_age(thread.lastmodified, true);
var t = metadataTemplate
.replace(/{{username}}/g, thread.username)
.replace(/{{date}}/g, datetime[0])
.replace(/{{time}}/g, datetime[1])
.replace(/{{active}}/g, age + " ago")
.replace(/{{views}}/g, thread.viewed + " view" + courtesy_s(thread.viewed));
return t;
}
|