A bare bones front-end for knockout designed for maximum compatibility with "obsolete" browsers
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

96 Zeilen
2.7KB

  1. <?php
  2. namespace App\Knockout;
  3. use Illuminate\Support\Facades\Cache;
  4. class User {
  5. public $id;
  6. public $username;
  7. public $avatar;
  8. public $background;
  9. public $banned = false;
  10. public static function unwrap($user)
  11. {
  12. $s = new self();
  13. $s->id = $user->id;
  14. $s->username = $user->username;
  15. $s->avatar = sprintf("https://cdn.knockout.chat/image/%u.webp", $user->id);
  16. $s->background = sprintf("https://cdn.knockout.chat/image/%u-bg.webp", $user->id);
  17. $s->banned = $user->banned;
  18. return $s;
  19. }
  20. private static function requestAll(int $page = 0, string $query = ''): Dataset
  21. {
  22. $path = ['/users/'];
  23. if ($page > 0) $path[] = $page;
  24. $data = (new AbstractData)->httpGet(implode('', $path), ['filter' => $query]);
  25. $json = json_decode($data);
  26. $records = array_map(function($user) {
  27. return self::unwrap($user);
  28. }, $json->users);
  29. return (new Dataset($records))
  30. ->setTotalRecords($json->totalUsers)
  31. ->setCurrentPage($page)
  32. ->setRecordsPerPage(40);
  33. }
  34. public static function updateAll(int $page = 0, string $query = ''): Dataset
  35. {
  36. $key = sprintf('users-%u-%s', $page, $query);
  37. $data = self::requestAll($page, $query);
  38. Cache::put($key, $data, 3600);
  39. return $data;
  40. }
  41. public static function all(int $page = 0, string $query = ''): Dataset
  42. {
  43. $key = sprintf('users-%u-%s', $page, $query);
  44. return Cache::get($key, function() use($page, $query) {
  45. return self::updateAll($page, $query);
  46. });
  47. }
  48. private static function requestOne(int $userId): Dataset
  49. {
  50. $data = (new AbstractData)->httpGet(sprintf('/user/%s', $userId));
  51. $json = json_decode($data);
  52. $record = self::unwrap($json);
  53. return (new Dataset($record));
  54. }
  55. public static function updateOne(int $userId): Dataset
  56. {
  57. $key = sprintf('user-%u', $userId);
  58. $data = self::requestOne($userId);
  59. Cache::put($key, $data, 3600);
  60. return $data;
  61. }
  62. public static function one(int $userId): Dataset
  63. {
  64. $key = sprintf('user-%u', $userId);
  65. return Cache::get($key, function() use($userId) {
  66. return self::updateOne($userId);
  67. });
  68. }
  69. public function postInfoClasses() {
  70. $classes = [];
  71. $showAvatar = true;
  72. $showBackground = true;
  73. if ($this->banned) $classes[] = 'banned';
  74. $classes[] = $showAvatar ? 'show-avatar' : 'hide-avatar';
  75. $classes[] = $showBackground ? 'show-backgrounds' : 'hide-backgrounds';
  76. return implode(' ', $classes);
  77. }
  78. }