-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel-renderer.php
550 lines (454 loc) · 15.5 KB
/
channel-renderer.php
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<?php
namespace HitchinHackspace\SlackViewer;
use Exception;
abstract class BaseRenderer {
/**
* @var int
*/
protected $page;
/**
* @var int
*/
protected $pageSize;
/**
* @var int|null
*/
protected $messageCount;
/**
* @param int|null $page
* @param int $pageSize
*/
function __construct($page = null, $pageSize = 100) {
$this->setPagination($page, $pageSize);
}
/**
* @param int|null $page
* @param int $pageSize
*/
function setPagination($page, $pageSize) {
$this->page = $page ?: 1;
$this->pageSize = $pageSize;
}
/**
* @return MessageSequence
*/
abstract function getMessageSequence();
/**
* @return string
*/
abstract function getPath();
/**
* @return string
*/
abstract function getTitle();
/**
* @return int
*/
function getMessageCount() {
if ($this->messageCount === null)
$this->messageCount = $this->getMessageSequence()->getCount();
return $this->messageCount;
}
/**
* @return Collection<SlackMessage>
*/
function getMessages() {
return $this->getMessageSequence()->getContent($this->getFirstIndex(), $this->pageSize);
}
/**
* @return SlackArchive
*/
function getArchive() { return $this->getMessageSequence()->getArchive(); }
/**
* @return int
*/
function getFirstIndex() { return ($this->page - 1) * $this->pageSize; }
/**
* @return int
*/
function getLastIndex() { return min($this->getFirstIndex() + $this->pageSize, $this->getMessageCount()); }
/**
* @return int
*/
function getPageCount() { return ceil($this->getMessageCount() / $this->pageSize); }
/**
* @return bool
*/
function hasPrevPage() { return $this->page > 1; }
/**
* @return bool
*/
function hasNextPage() { return $this->page < $this->getPageCount(); }
/**
* @return int|null
*/
function getPrevPage() { return $this->hasPrevPage() ? $this->page - 1 : null; }
/**
* @return int|null
*/
function getNextPage() { return $this->hasNextPage() ? $this->page + 1 : null; }
/**
* @param int $page
* @return string
*/
function getPageLink($page) { return "?path={$this->getPath()}/$page"; }
/*
function getPageName($page) {
if ($page == 1)
return 'First';
if ($page == $this->getPageCount())
return 'Last';
if ($page == $this->page - 1)
return 'Previous';
if ($page == $this->page + 1)
return 'Next';
return $page;
}
*/
/**
* @param int $page
* @param mixed $name
*/
function renderPageLink($page, $name) {
$class = ($page == $this->page) ? ' class="current"' : '';
$timestamp = $this->getMessageSequence()->getApproximateTimestamp(($page - 1) * $this->pageSize);
?>
<li title="<?= date('d/m/Y', $timestamp) ?>"<?= $class ?>><a href="<?= $this->getPageLink($page) ?>"<?= $class ?>><?= $name ?></a></li>
<?php
}
function renderNav() {
// Which pages should we link to?
// How often should we add page links? It depends how many pages there are in total.
$skip = intval($this->getPageCount() / 10);
// If it's more than five, make sure it's a multiple of five.
if ($skip > 5)
$skip -= ($skip % 5);
// Make sure it's not zero.
if ($skip < 1)
$skip = 1;
$pages = array_merge(
// An overview of the entire range
range($this->page, -$skip, -$skip),
range($this->page, $this->getPageCount() + $skip, $skip),
// ... plus five pages back and forward from our current location.
range($this->page - 3, $this->page + 3),
// ... and the first and last page.
[1, $this->getPageCount()]
);
// Remove those that are out of range.
$pages = array_filter($pages, function ($page) { return $page > 1 && $page < $this->getPageCount(); });
// ... and duplicates
$pages = array_values(array_unique($pages));
// ... then make sure they're in order.
sort($pages, SORT_NUMERIC);
?>
<nav class="pagenav">
<ul>
<?php
if ($this->getPageCount() > 1)
$this->renderPageLink(1, 'First');
if ($this->page > 1)
$this->renderPageLink($this->page - 1, 'Prev');
foreach ($pages as $page)
$this->renderPageLink($page, $page);
if ($this->page < $this->getPageCount())
$this->renderPageLink($this->page + 1, 'Next');
if ($this->getPageCount() > 1)
$this->renderPageLink($this->getPageCount(), 'Last');
?>
</ul>
</nav>
<?php
}
/**
* @return string|null
*/
function getFilePreview($file, $atleast = null) {
static $thumbKeys = [
'1024' => 'thumb_1024',
'960' => 'thumb_960',
'800' => 'thumb_800',
'720' => 'thumb_720',
'480' => 'thumb_480',
'360' => 'thumb_360',
'160' => 'thumb_160',
'80' => 'thumb_80',
'64' => 'thumb_64'
];
$best = null;
foreach ($thumbKeys as $size => $key) {
$url = $file[$key] ?? null;
if (!$url)
continue;
if ($atleast === null)
return $url;
if ($size < $atleast)
return $best;
$best = $url;
}
return null;
}
const ELEMENT_HANDLERS = [
'rich_text_section' => 'renderRichTextElement',
'text' => 'renderTextElement',
'user' => 'renderUserElement',
'link' => 'renderLinkElement',
'emoji' => 'renderEmojiElement',
'channel' => 'renderChannelElement'
];
const BLOCK_HANDLERS = [
'rich_text' => 'renderRichTextBlock'
];
const MESSAGE_HANDLERS = [
'message' => 'renderBlocks'
];
const SUBTYPE_HANDLERS = [
'channel_join' => 'renderChannelJoin'
];
function handle($type, $handlers, $subtype, ... $args) {
$handler = $handlers[$subtype] ?? null;
if (!$handler)
throw new Exception("Unrecognised $type type: '$subtype'");
$this->$handler(... $args);
}
function renderTextElement($message, $element) {
?>
<?= htmlspecialchars($element['text']) ?>
<?php
}
function renderRichTextElement($message, $element) {
foreach ($element['elements'] as $child)
$this->renderElement($message, $child);
}
function renderUserElement($message, $element) {
$user = $this->getArchive()->getUser($element['user_id']);
if ($user) {
?>
<span class="user"><?= $user->getName() ?></span>
<?php
}
else {
?>
<span class="user unknown">unknown</span>
<?php
}
}
function renderLinkElement($message, $element) {
$url = $element['url'];
$text = $element['text'] ?? $url;
?>
<a class="message-link" href="<?= $url ?>" target="_blank"><?= $text ?></a>
<?php
}
function renderEmojiElement($message, $element) {
?>
<span class="emoji">&#x<?= $element['unicode'] ?>;</span>
<?php
}
function renderChannelElement($message, $element) {
$channel = $this->getArchive()->getChannelByID($element['channel_id']);
if ($channel) {
?>
<a class="channel-link" href="?path=<?= esc_attr($channel->getName()) ?>"><?= htmlspecialchars($channel->getName()) ?></a>
<?php
}
else {
?>
<a class="channel-link unknown">unknown</a>
<?php
}
}
function renderElement($message, $element) {
$this->handle('element', self::ELEMENT_HANDLERS, $element['type'], $message, $element);
}
function renderRichTextBlock($message, $block) {
foreach ($block['elements'] as $element)
$this->renderElement($message, $element);
}
function renderBlock($message, $block) {
$this->handle('block', self::BLOCK_HANDLERS, $block['type'], $message, $block);
}
function renderChannelJoin($message) {
$user = $this->getArchive()->getUser($message['user']);
?>
<span class="user"><?= $user ? $user->getName() : 'unknown' ?></span> has joined the channel.
<?php
}
function renderBlocks($message) {
$subtype = $message['subtype'] ?? null;
if ($subtype) {
$this->handle('message subtype', self::SUBTYPE_HANDLERS, $subtype, $message);
return;
}
foreach ($message['blocks'] as $block)
$this->renderBlock($message, $block);
}
function renderContent($message) {
ob_start();
try {
$this->handle('message', self::MESSAGE_HANDLERS, $message['type'], $message);
ob_flush();
}
catch (Exception $e) {
error_log($e);
ob_clean();
?>
<div class="content-decode-error" title="<?= $e->getMessage(); ?>">
<?= htmlspecialchars($message['text']) ?>
</div>
<?php
ob_flush();
}
finally {
ob_end_clean();
}
}
function renderMessage($message) {
$message = $message->getData();
$files = $message['files'] ?? [];
?>
<div class="message">
<?php
$this->renderContent($message);
if ($files) { ?>
<ul class="files">
<?php foreach ($files as $file) {
if ($file['mode'] == 'hidden_by_limit') {
?>
<li class="hidden_by_limit">
Due to Slack limits, this attachment can't be shown.
</li>
<?php
}
else {
?>
<li>
<a target="_blank" href="<?= $file['url_private'] ?>">
<?php
$preview = $this->getFilePreview($file, 160);
if ($preview) {
?><img class="file-preview" src="<?= $preview ?>"><?php
}
else {
?><?= $file['name'] ?><?php
}
?>
</a>
</li>
<?php
}
}
?>
</ul>
<?php } ?>
</div>
<?php
}
function renderMeta($message) {
$user = $message->getData()['user'] ?? null;
if ($user)
$user = $this->getArchive()->getUser($user);
?>
<div class="meta">
<span class="user-display-name"><?= $user ? $user->getDisplayName() : 'Unknown' ?></span>
@
<span class="time"><?= date('Y-m-d H:i:s', $message->getTimestamp()) ?></span>
</div>
<?php
}
function render($message) {
$user = $message->getData()['user'] ?? null;
if ($user)
$user = $this->getArchive()->getUser($user);
$avatarURL = null;
if ($user)
$avatarURL = $user->getAvatarURL(48);
?>
<div class="avatar">
<?php if ($avatarURL) { ?><img class="avatar" src="<?= $avatarURL ?>"><?php } ?>
</div>
<div class="data">
<?php $this->renderMessage($message); ?>
<?php $this->renderMeta($message); ?>
</div>
<?php
}
}
class ChannelRenderer extends BaseRenderer {
/**
* @var SlackChannel
*/
protected $channel;
/**
* @param SlackChannel $channel
* @param int|null $page
* @param int $pageSize
*/
function __construct($channel, $page = null, $pageSize = 100) {
parent::__construct($page, $pageSize);
$this->channel = $channel;
}
public function getMessageSequence() { return $this->channel; }
public function getPath() { return $this->channel->getName(); }
public function getTitle() { return '#' . htmlspecialchars($this->channel->getName()); }
}
class SearchRenderer extends BaseRenderer {
/**
* @var SearchResults
*/
protected $results;
/**
* @param SearchResults $results
* @param int|null $page
* @param int $pageSize
*/
function __construct($results, $page = null, $pageSize = 100) {
parent::__construct($page, $pageSize);
$this->results = $results;
}
public function getMessageSequence() {
return $this->results;
}
public function getPath() {
$term = urlencode($this->results->getSearchTerm());
return "search/$term";
}
public function getTitle() {
$term = htmlspecialchars($this->results->getSearchTerm());
return "Search results for <span class=\"search-highlight\">$term</span>";
}
public function renderTextElement($message, $element) {
$term = $this->results->getSearchTerm();
$content = $element['text'];
while (true) {
$i = stripos($content, $term);
if ($i === false) {
echo htmlspecialchars($content);
break;
}
?>
<?= htmlspecialchars(substr($content, 0, $i)) ?><span class="search-highlight"><?= htmlspecialchars(substr($content, $i, strlen($term))) ?></span>
<?php
$content = substr($content, $i + strlen($term));
}
}
function renderMeta($message) {
$user = $message->getData()['user'] ?? null;
if ($user)
$user = $this->getArchive()->getUser($user);
$channel = null;
if ($message instanceof SearchResultMessage) {
$channel = $message->getChannel();
$path = $channel->getName() . '/message/' . $message->getOffset() . '#' . $message->getTimestamp();
}
?>
<a href="<?= $path ? '?path=' . esc_attr($path) : '' ?>" class="meta">
<span class="user-display-name"><?= $user ? $user->getDisplayName() : 'Unknown' ?></span>
<?php if ($channel) { ?>
in <span class="channel-link"><?= htmlspecialchars($channel->getName()) ?></span>
<?php } ?>
<span class="time"><?= date('Y-m-d H:i:s', $message->getTimestamp()) ?></span>
</a>
<?php
}
}