A bare bones front-end for knockout designed for maximum compatibility with "obsolete" browsers
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

210 строки
5.8KB

  1. <?php
  2. namespace App\Knockout;
  3. use Illuminate\Support\Facades\Cache;
  4. use Symfony\Component\VarDumper\Cloner\Data;
  5. class Thread {
  6. // meta
  7. public $id;
  8. public $icon;
  9. public $title;
  10. public $postCount;
  11. // related
  12. public $posts = [];
  13. public Subforum $subforum;
  14. public Post $lastPost;
  15. public User $user;
  16. private static function getIcon(int $iconId)
  17. {
  18. $icons = [
  19. 0 => 'default',
  20. 1 => 'drama',
  21. 2 => 'games',
  22. 3 => 'life',
  23. 4 => 'money',
  24. 5 => 'announcement',
  25. 6 => 'arcade-cabinet',
  26. 7 => 'blog',
  27. 8 => 'bomb',
  28. 9 => 'bug',
  29. 10 => 'code',
  30. 11 => 'combo-chart',
  31. 12 => 'cooker',
  32. 13 => 'crying-baby',
  33. 14 => 'dota',
  34. 15 => 'europe-news',
  35. 16 => 'gallery',
  36. 17 => 'grocery-bag',
  37. 18 => 'gun',
  38. 19 => 'heart-with-pulse',
  39. 20 => 'help',
  40. 21 => 'hot-article',
  41. 22 => 'news',
  42. 23 => 'nintendo-entertainment-system',
  43. 24 => 'pin',
  44. 25 => 'planner',
  45. 26 => 'tv-show',
  46. 27 => 'uk-news',
  47. 28 => 'us-music',
  48. 29 => 'us-news',
  49. 30 => 'retro-tv',
  50. 31 => 'smiling',
  51. 32 => 'vomited',
  52. 33 => 'test-tube',
  53. 34 => 'easel',
  54. 35 => 'banana',
  55. 36 => 'heart-with-arrow',
  56. 37 => 'radio-active',
  57. 38 => 'poison',
  58. 39 => 'invisible',
  59. 40 => 'visible',
  60. 41 => 'controller',
  61. 42 => 'boring',
  62. 43 => 'swearing-male',
  63. 44 => 'idea',
  64. 45 => 'megaphone',
  65. 46 => 'banknotes',
  66. 47 => 'robot-3',
  67. 48 => 'phonelink-ring',
  68. 49 => 'thriller',
  69. 50 => 'hamburger',
  70. 51 => 'sakura',
  71. 52 => 'sushi',
  72. 53 => 'lifebuoy',
  73. 54 => 'handshake',
  74. 55 => 'clenched-fist',
  75. 56 => 'anonymous-mask',
  76. 57 => 'rocket',
  77. 58 => 'vodka',
  78. 59 => 'poo',
  79. 60 => 'sedan',
  80. 61 => 'tesla-model-x',
  81. 62 => 'memory-slot',
  82. 63 => 'processor',
  83. 64 => 'fire-element',
  84. 65 => 'firework',
  85. 66 => 'siren',
  86. 67 => 'confetti',
  87. 68 => 'campfire',
  88. 69 => 'panties',
  89. 70 => 'mushroom-cloud',
  90. 71 => 'bomb-with-timer'
  91. ];
  92. return $icons[$iconId] ?? 'poo';
  93. }
  94. public static function unwrap($thread)
  95. {
  96. $s = new self();
  97. $s->id = $thread->id;
  98. $s->icon = self::getIcon($thread->iconId);
  99. $s->title = $thread->title;
  100. $s->postCount = $thread->postCount;
  101. // grab subforum if available
  102. if (isset($thread->subforum)) {
  103. $s->subforum = Subforum::unwrap($thread->subforum);
  104. }
  105. // grab posts if available
  106. $s->posts = array_map(function($post) {
  107. return Post::unwrap($post);
  108. }, $thread->posts ?? []);
  109. // grab user if availble
  110. if (isset($thread->user)) {
  111. $s->user = User::unwrap($thread->user);
  112. }
  113. // grab last post if available
  114. if (isset($thread->lastPost) && isset($thread->lastPost->id)) {
  115. $s->lastPost = Post::unwrap($thread->lastPost);
  116. }
  117. return $s;
  118. }
  119. private static function requestOne(int $threadId, int $page = 1): Dataset
  120. {
  121. $data = (new AbstractData)->httpGet(sprintf('/thread/%s/%s', $threadId, $page));
  122. $json = json_decode($data);
  123. $record = self::unwrap($json);
  124. return (new Dataset($record))
  125. ->setTotalRecords($json->totalPosts)
  126. ->setCurrentPage($json->currentPage)
  127. ->setRecordsPerPage(20);
  128. }
  129. public static function updateOne(int $subforumId, int $page = 1): Dataset
  130. {
  131. $key = sprintf('threads-%u-%u', $subforumId, $page);
  132. $data = self::requestOne($subforumId, $page);
  133. Cache::put($key, $data, 3600);
  134. return $data;
  135. }
  136. public static function one(int $subforumId, int $page = 1): Dataset
  137. {
  138. $key = sprintf('threads-%u-%u', $subforumId, $page);
  139. return Cache::get($key, function() use($subforumId, $page) {
  140. return self::updateOne($subforumId, $page);
  141. });
  142. }
  143. private static function requestPopular(): Dataset
  144. {
  145. $data = (new AbstractData)->httpGet('/thread/popular');
  146. $json = json_decode($data);
  147. $records = array_map(function($thread) {
  148. return self::unwrap($thread);
  149. }, $json->list);
  150. return (new Dataset($records));
  151. }
  152. public static function updatePopular(): Dataset
  153. {
  154. $data = self::requestPopular();
  155. Cache::put('popular-threads', $data, 3600);
  156. return $data;
  157. }
  158. public static function popular(): Dataset
  159. {
  160. return Cache::get('popular-threads', function() {
  161. return self::updatePopular();
  162. });
  163. }
  164. private static function requestLatest(): Dataset
  165. {
  166. $data = (new AbstractData)->httpGet('/thread/latest');
  167. $json = json_decode($data);
  168. $records = array_map(function($thread) {
  169. return self::unwrap($thread);
  170. }, $json->list);
  171. return (new Dataset($records));
  172. }
  173. public static function updateLatest(): Dataset
  174. {
  175. $data = self::requestLatest();
  176. Cache::put('latest-threads', $data, 3600);
  177. return $data;
  178. }
  179. public static function latest(): Dataset
  180. {
  181. return Cache::get('latest-threads', function() {
  182. return self::updateLatest();
  183. });
  184. }
  185. }