|
- <?php
-
- namespace App\Knockout;
-
- use Illuminate\Support\Facades\Cache;
- use Symfony\Component\VarDumper\Cloner\Data;
-
- class Thread {
-
- // meta
- public $id;
- public $icon;
- public $title;
- public $postCount;
-
- // related
- public $posts = [];
- public Subforum $subforum;
- public Post $lastPost;
- public User $user;
-
- private static function getIcon(int $iconId)
- {
- $icons = [
- 0 => 'default',
- 1 => 'drama',
- 2 => 'games',
- 3 => 'life',
- 4 => 'money',
- 5 => 'announcement',
- 6 => 'arcade-cabinet',
- 7 => 'blog',
- 8 => 'bomb',
- 9 => 'bug',
- 10 => 'code',
- 11 => 'combo-chart',
- 12 => 'cooker',
- 13 => 'crying-baby',
- 14 => 'dota',
- 15 => 'europe-news',
- 16 => 'gallery',
- 17 => 'grocery-bag',
- 18 => 'gun',
- 19 => 'heart-with-pulse',
- 20 => 'help',
- 21 => 'hot-article',
- 22 => 'news',
- 23 => 'nintendo-entertainment-system',
- 24 => 'pin',
- 25 => 'planner',
- 26 => 'tv-show',
- 27 => 'uk-news',
- 28 => 'us-music',
- 29 => 'us-news',
- 30 => 'retro-tv',
- 31 => 'smiling',
- 32 => 'vomited',
- 33 => 'test-tube',
- 34 => 'easel',
- 35 => 'banana',
- 36 => 'heart-with-arrow',
- 37 => 'radio-active',
- 38 => 'poison',
- 39 => 'invisible',
- 40 => 'visible',
- 41 => 'controller',
- 42 => 'boring',
- 43 => 'swearing-male',
- 44 => 'idea',
- 45 => 'megaphone',
- 46 => 'banknotes',
- 47 => 'robot-3',
- 48 => 'phonelink-ring',
- 49 => 'thriller',
- 50 => 'hamburger',
- 51 => 'sakura',
- 52 => 'sushi',
- 53 => 'lifebuoy',
- 54 => 'handshake',
- 55 => 'clenched-fist',
- 56 => 'anonymous-mask',
- 57 => 'rocket',
- 58 => 'vodka',
- 59 => 'poo',
- 60 => 'sedan',
- 61 => 'tesla-model-x',
- 62 => 'memory-slot',
- 63 => 'processor',
- 64 => 'fire-element',
- 65 => 'firework',
- 66 => 'siren',
- 67 => 'confetti',
- 68 => 'campfire',
- 69 => 'panties',
- 70 => 'mushroom-cloud',
- 71 => 'bomb-with-timer'
- ];
-
- return $icons[$iconId] ?? 'poo';
- }
-
- public static function unwrap($thread)
- {
- $s = new self();
-
- $s->id = $thread->id;
- $s->icon = self::getIcon($thread->iconId);
- $s->title = $thread->title;
- $s->postCount = $thread->postCount;
-
- // grab subforum if available
- if (isset($thread->subforum)) {
- $s->subforum = Subforum::unwrap($thread->subforum);
- }
-
- // grab posts if available
- $s->posts = array_map(function($post) {
- return Post::unwrap($post);
- }, $thread->posts ?? []);
-
- // grab user if availble
- if (isset($thread->user)) {
- $s->user = User::unwrap($thread->user);
- }
-
- // grab last post if available
- if (isset($thread->lastPost) && isset($thread->lastPost->id)) {
- $s->lastPost = Post::unwrap($thread->lastPost);
- }
-
- return $s;
- }
-
- private static function requestOne(int $threadId, int $page = 1): Dataset
- {
- $data = (new AbstractData)->httpGet(sprintf('/thread/%s/%s', $threadId, $page));
- $json = json_decode($data);
- $record = self::unwrap($json);
- return (new Dataset($record))
- ->setTotalRecords($json->totalPosts)
- ->setCurrentPage($json->currentPage)
- ->setRecordsPerPage(20);
- }
-
- public static function updateOne(int $subforumId, int $page = 1): Dataset
- {
- $key = sprintf('threads-%u-%u', $subforumId, $page);
- $data = self::requestOne($subforumId, $page);
- Cache::put($key, $data, 3600);
- return $data;
- }
-
- public static function one(int $subforumId, int $page = 1): Dataset
- {
- $key = sprintf('threads-%u-%u', $subforumId, $page);
- return Cache::get($key, function() use($subforumId, $page) {
- return self::updateOne($subforumId, $page);
- });
- }
-
- private static function requestPopular(): Dataset
- {
- $data = (new AbstractData)->httpGet('/thread/popular');
- $json = json_decode($data);
- $records = array_map(function($thread) {
- return self::unwrap($thread);
- }, $json->list);
- return (new Dataset($records));
- }
-
- public static function updatePopular(): Dataset
- {
- $data = self::requestPopular();
- Cache::put('popular-threads', $data, 3600);
- return $data;
- }
-
- public static function popular(): Dataset
- {
- return Cache::get('popular-threads', function() {
- return self::updatePopular();
- });
- }
-
- private static function requestLatest(): Dataset
- {
- $data = (new AbstractData)->httpGet('/thread/latest');
- $json = json_decode($data);
- $records = array_map(function($thread) {
- return self::unwrap($thread);
- }, $json->list);
- return (new Dataset($records));
- }
-
- public static function updateLatest(): Dataset
- {
- $data = self::requestLatest();
- Cache::put('latest-threads', $data, 3600);
- return $data;
- }
-
- public static function latest(): Dataset
- {
- return Cache::get('latest-threads', function() {
- return self::updateLatest();
- });
- }
-
- }
|