GroupsFeedController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace App\Http\Controllers\Groups;
  3. use Illuminate\Support\Facades\Cache;
  4. use Illuminate\Support\Facades\RateLimiter;
  5. use App\Http\Controllers\Controller;
  6. use Illuminate\Http\Request;
  7. use App\Services\AccountService;
  8. use App\Services\GroupService;
  9. use App\Services\UserFilterService;
  10. use App\Services\Groups\GroupFeedService;
  11. use App\Services\Groups\GroupPostService;
  12. use App\Services\RelationshipService;
  13. use App\Services\Groups\GroupsLikeService;
  14. use App\Follower;
  15. use App\Profile;
  16. use App\Models\Group;
  17. use App\Models\GroupPost;
  18. use App\Models\GroupInvitation;
  19. class GroupsFeedController extends Controller
  20. {
  21. public function __construct()
  22. {
  23. $this->middleware('auth');
  24. }
  25. public function getSelfFeed(Request $request)
  26. {
  27. abort_if(!$request->user(), 404);
  28. $pid = $request->user()->profile_id;
  29. $limit = $request->input('limit', 5);
  30. $page = $request->input('page');
  31. $initial = $request->has('initial');
  32. if($initial) {
  33. $res = Cache::remember('groups:self:feed:' . $pid, 900, function() use($pid) {
  34. return $this->getSelfFeedV0($pid, 5, null);
  35. });
  36. } else {
  37. abort_if($page && $page > 5, 422);
  38. $res = $this->getSelfFeedV0($pid, $limit, $page);
  39. }
  40. return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  41. }
  42. protected function getSelfFeedV0($pid, $limit, $page)
  43. {
  44. return GroupPost::join('group_members', 'group_posts.group_id', 'group_members.group_id')
  45. ->select('group_posts.*', 'group_members.group_id', 'group_members.profile_id')
  46. ->where('group_members.profile_id', $pid)
  47. ->whereIn('group_posts.type', ['text', 'photo', 'video'])
  48. ->orderByDesc('group_posts.id')
  49. ->limit($limit)
  50. // ->pluck('group_posts.status_id')
  51. ->simplePaginate($limit)
  52. ->map(function($gp) use($pid) {
  53. $status = GroupPostService::get($gp['group_id'], $gp['id']);
  54. if(!$status) {
  55. return false;
  56. }
  57. $status['favourited'] = (bool) GroupsLikeService::liked($pid, $gp['id']);
  58. $status['favourites_count'] = GroupsLikeService::count($gp['id']);
  59. $status['pf_type'] = $gp['type'];
  60. $status['visibility'] = 'public';
  61. $status['url'] = url("/groups/{$gp['group_id']}/p/{$gp['id']}");
  62. $status['group'] = GroupService::get($gp['group_id']);
  63. $status['account']['url'] = url("/groups/{$gp['group_id']}/user/{$status['account']['id']}");
  64. return $status;
  65. });
  66. }
  67. public function getGroupProfileFeed(Request $request, $id, $pid)
  68. {
  69. abort_if(!$request->user(), 404);
  70. $cid = $request->user()->profile_id;
  71. $group = Group::findOrFail($id);
  72. abort_if(!$group->isMember($pid), 404);
  73. $feed = GroupPost::whereGroupId($id)
  74. ->whereProfileId($pid)
  75. ->latest()
  76. ->paginate(3)
  77. ->map(function($gp) use($pid) {
  78. $status = GroupPostService::get($gp['group_id'], $gp['id']);
  79. if(!$status) {
  80. return false;
  81. }
  82. $status['favourited'] = (bool) GroupsLikeService::liked($pid, $gp['id']);
  83. $status['favourites_count'] = GroupsLikeService::count($gp['id']);
  84. $status['pf_type'] = $gp['type'];
  85. $status['visibility'] = 'public';
  86. $status['url'] = $gp->url();
  87. // if($gp['type'] == 'poll') {
  88. // $status['poll'] = PollService::get($status['id']);
  89. // }
  90. $status['account']['url'] = "/groups/{$gp['group_id']}/user/{$status['account']['id']}";
  91. return $status;
  92. })
  93. ->filter(function($status) {
  94. return $status;
  95. });
  96. return $feed;
  97. }
  98. public function getGroupFeed(Request $request, $id)
  99. {
  100. $group = Group::findOrFail($id);
  101. $user = $request->user();
  102. $pid = optional($user)->profile_id ?? false;
  103. abort_if(!$group->isMember($pid), 404);
  104. $max = $request->input('max_id');
  105. $limit = $request->limit ?? 3;
  106. $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
  107. // $posts = GroupPost::whereGroupId($group->id)
  108. // ->when($maxId, function($q, $maxId) {
  109. // return $q->where('status_id', '<', $maxId);
  110. // })
  111. // ->whereNull('in_reply_to_id')
  112. // ->orderByDesc('status_id')
  113. // ->simplePaginate($limit)
  114. // ->map(function($gp) use($pid) {
  115. // $status = StatusService::get($gp['status_id'], false);
  116. // if(!$status) {
  117. // return false;
  118. // }
  119. // $status['favourited'] = (bool) LikeService::liked($pid, $gp['status_id']);
  120. // $status['favourites_count'] = LikeService::count($gp['status_id']);
  121. // $status['pf_type'] = $gp['type'];
  122. // $status['visibility'] = 'public';
  123. // $status['url'] = $gp->url();
  124. // if($gp['type'] == 'poll') {
  125. // $status['poll'] = PollService::get($status['id']);
  126. // }
  127. // $status['account']['url'] = url("/groups/{$gp['group_id']}/user/{$status['account']['id']}");
  128. // return $status;
  129. // })->filter(function($status) {
  130. // return $status;
  131. // });
  132. // return $posts;
  133. Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() use($id) {
  134. if(GroupFeedService::count($id) == 0) {
  135. GroupFeedService::warmCache($id, true, 400);
  136. }
  137. });
  138. if ($max) {
  139. $feed = GroupFeedService::getRankedMaxId($id, $max, $limit);
  140. } else {
  141. $feed = GroupFeedService::get($id, 0, $limit);
  142. }
  143. $res = collect($feed)
  144. ->map(function($k) use($user, $id) {
  145. $status = GroupPostService::get($id, $k);
  146. if($status && $user) {
  147. $pid = $user->profile_id;
  148. $sid = $status['account']['id'];
  149. $status['favourited'] = (bool) GroupsLikeService::liked($pid, $status['id']);
  150. $status['favourites_count'] = GroupsLikeService::count($status['id']);
  151. $status['relationship'] = $pid == $sid ? [] : RelationshipService::get($pid, $sid);
  152. }
  153. return $status;
  154. })
  155. ->filter(function($s) use($filtered) {
  156. return $s && in_array($s['account']['id'], $filtered) == false;
  157. })
  158. ->values()
  159. ->toArray();
  160. return $res;
  161. }
  162. }