Просмотр файла app/Classes/Feed.php

Размер файла: 6.33Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Classes;
  6.  
  7. use App\Models\Article;
  8. use App\Models\Down;
  9. use App\Models\Item;
  10. use App\Models\News;
  11. use App\Models\Photo;
  12. use App\Models\Polling;
  13. use App\Models\Post;
  14. use App\Models\Topic;
  15. use App\Models\User;
  16. use Illuminate\Support\Collection;
  17. use Illuminate\Support\Facades\Cache;
  18. use Illuminate\Support\HtmlString;
  19.  
  20. class Feed
  21. {
  22. /**
  23. * @var User|mixed
  24. */
  25. private $user;
  26.  
  27. public function __construct()
  28. {
  29. $this->user = getUser();
  30. }
  31.  
  32. /**
  33. * Get feed
  34. *
  35. * @return HtmlString
  36. */
  37. public function getFeed(): HtmlString
  38. {
  39. $polls = [];
  40. $collect = new Collection();
  41.  
  42. if (setting('feed_topics_show')) {
  43. $topics = $this->getTopics();
  44. $collect = $collect->merge($topics);
  45.  
  46. if ($this->user) {
  47. $ids = $topics->pluck('last_post_id')->all();
  48. $polls[Post::$morphName] = $this->getPolling($ids, Post::$morphName);
  49. }
  50. }
  51.  
  52. if (setting('feed_news_show')) {
  53. $news = $this->getNews();
  54. $collect = $collect->merge($news);
  55.  
  56. if ($this->user) {
  57. $ids = $news->pluck('id')->all();
  58. $polls[News::$morphName] = $this->getPolling($ids, News::$morphName);
  59. }
  60. }
  61.  
  62. if (setting('feed_photos_show')) {
  63. $photos = $this->getPhotos();
  64. $collect = $collect->merge($photos);
  65.  
  66. if ($this->user) {
  67. $ids = $photos->pluck('id')->all();
  68. $polls[Photo::$morphName] = $this->getPolling($ids, Photo::$morphName);
  69. }
  70. }
  71.  
  72. if (setting('feed_articles_show')) {
  73. $articles = $this->getArticles();
  74. $collect = $collect->merge($articles);
  75.  
  76. if ($this->user) {
  77. $ids = $articles->pluck('id')->all();
  78. $polls[Article::$morphName] = $this->getPolling($ids, Article::$morphName);
  79. }
  80. }
  81.  
  82. if (setting('feed_downs_show')) {
  83. $downs = $this->getDowns();
  84. $collect = $collect->merge($downs);
  85.  
  86. if ($this->user) {
  87. $ids = $downs->pluck('id')->all();
  88. $polls[Down::$morphName] = $this->getPolling($ids, Down::$morphName);
  89. }
  90. }
  91.  
  92. if (setting('feed_items_show')) {
  93. $collect = $collect->merge($this->getItems());
  94. }
  95.  
  96. $posts = $collect
  97. ->sortByDesc('created_at')
  98. ->take(setting('feed_total'));
  99.  
  100. $user = $this->user;
  101. $posts = simplePaginate($posts, setting('feed_per_page'));
  102. $allowDownload = $user || setting('down_guest_download');
  103.  
  104. return new HtmlString(view('widgets/_feed', compact('posts', 'polls', 'user', 'allowDownload')));
  105. }
  106.  
  107. /**
  108. * Get polling
  109. *
  110. * @param array $ids
  111. * @param string $morphName
  112. *
  113. * @return array
  114. */
  115. private function getPolling(array $ids, string $morphName): array
  116. {
  117. return Polling::query()
  118. ->whereIn('pollings.relate_id', $ids)
  119. ->where('pollings.relate_type', $morphName)
  120. ->where('pollings.user_id', $this->user->id)
  121. ->pluck('vote', 'relate_id')
  122. ->all();
  123. }
  124.  
  125. /**
  126. * Get topics
  127. *
  128. * @return Collection<Topic>
  129. */
  130. public function getTopics(): Collection
  131. {
  132. return Cache::remember('TopicFeed', 600, static function () {
  133. return Topic::query()
  134. ->select('topics.*', 'posts.created_at')
  135. ->join('posts', function ($join) {
  136. $join->on('last_post_id', 'posts.id')
  137. ->where('posts.rating', '>', setting('feed_topics_rating'));
  138. })
  139. ->orderByDesc('topics.updated_at')
  140. ->with('lastPost.user', 'lastPost.files', 'forum.parent')
  141. ->limit(setting('feed_last_record'))
  142. ->get();
  143. });
  144. }
  145.  
  146. /**
  147. * Get news
  148. *
  149. * @return Collection<News>
  150. */
  151. public function getNews(): Collection
  152. {
  153. return Cache::remember('NewsFeed', 600, static function () {
  154. return News::query()
  155. ->where('rating', '>', setting('feed_news_rating'))
  156. ->orderByDesc('created_at')
  157. ->limit(setting('feed_last_record'))
  158. ->with('user')
  159. ->get();
  160. });
  161. }
  162.  
  163. /**
  164. * Get photos
  165. *
  166. * @return Collection<Photo>
  167. */
  168. public function getPhotos(): Collection
  169. {
  170. return Cache::remember('PhotoFeed', 600, static function () {
  171. return Photo::query()
  172. ->where('rating', '>', setting('feed_photos_rating'))
  173. ->orderByDesc('created_at')
  174. ->limit(setting('feed_last_record'))
  175. ->with('user', 'files')
  176. ->get();
  177. });
  178. }
  179.  
  180. /**
  181. * Get articles
  182. *
  183. * @return Collection<Article>
  184. */
  185. public function getArticles(): Collection
  186. {
  187. return Cache::remember('ArticleFeed', 600, static function () {
  188. return Article::query()
  189. ->where('rating', '>', setting('feed_downs_rating'))
  190. ->orderByDesc('created_at')
  191. ->limit(setting('feed_last_record'))
  192. ->with('user', 'files', 'category.parent')
  193. ->get();
  194. });
  195. }
  196.  
  197. /**
  198. * Get downs
  199. *
  200. * @return Collection<Down>
  201. */
  202. public function getDowns(): Collection
  203. {
  204. return Cache::remember('DownFeed', 600, static function () {
  205. return Down::query()
  206. ->where('active', 1)
  207. ->where('rating', '>', setting('feed_downs_rating'))
  208. ->orderByDesc('created_at')
  209. ->limit(setting('feed_last_record'))
  210. ->with('user', 'files', 'category.parent')
  211. ->get();
  212. });
  213. }
  214.  
  215. /**
  216. * Get items
  217. *
  218. * @return Collection<Item>
  219. */
  220. public function getItems(): Collection
  221. {
  222. return Cache::remember('ItemFeed', 600, static function () {
  223. return Item::query()
  224. ->where('expires_at', '>', SITETIME)
  225. ->orderByDesc('created_at')
  226. ->limit(setting('feed_last_record'))
  227. ->with('user', 'files', 'category.parent')
  228. ->get();
  229. });
  230. }
  231. }