GroupPostService.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Services\Groups;
  3. use App\Models\GroupPost;
  4. use Cache;
  5. use Illuminate\Support\Facades\Redis;
  6. use League\Fractal;
  7. use League\Fractal\Serializer\ArraySerializer;
  8. use League\Fractal\Pagination\IlluminatePaginatorAdapter;
  9. use App\Transformer\Api\GroupPostTransformer;
  10. class GroupPostService
  11. {
  12. const CACHE_KEY = 'pf:services:groups:post:';
  13. public static function key($gid, $pid)
  14. {
  15. return self::CACHE_KEY . $gid . ':' . $pid;
  16. }
  17. public static function get($gid, $pid)
  18. {
  19. return Cache::remember(self::key($gid, $pid), 604800, function() use($gid, $pid) {
  20. $gp = GroupPost::whereGroupId($gid)->find($pid);
  21. if(!$gp) {
  22. return null;
  23. }
  24. $fractal = new Fractal\Manager();
  25. $fractal->setSerializer(new ArraySerializer());
  26. $resource = new Fractal\Resource\Item($gp, new GroupPostTransformer());
  27. $res = $fractal->createData($resource)->toArray();
  28. $res['pf_type'] = $gp['type'];
  29. $res['url'] = $gp->url();
  30. // if($gp['type'] == 'poll') {
  31. // $status['poll'] = PollService::get($status['id']);
  32. // }
  33. //$status['account']['url'] = url("/groups/{$gp['group_id']}/user/{$status['account']['id']}");
  34. return $res;
  35. });
  36. }
  37. public static function del($gid, $pid)
  38. {
  39. return Cache::forget(self::key($gid, $pid));
  40. }
  41. public function getStatus(Request $request)
  42. {
  43. $gid = $request->input('gid');
  44. $sid = $request->input('sid');
  45. $pid = optional($request->user())->profile_id ?? false;
  46. $group = Group::findOrFail($gid);
  47. if($group->is_private) {
  48. abort_if(!$group->isMember($pid), 404);
  49. }
  50. $gp = GroupPost::whereGroupId($group->id)->whereId($sid)->firstOrFail();
  51. $status = GroupPostService::get($gp['group_id'], $gp['id']);
  52. if(!$status) {
  53. return false;
  54. }
  55. $status['reply_count'] = $gp['reply_count'];
  56. $status['favourited'] = (bool) GroupsLikeService::liked($pid, $gp['id']);
  57. $status['favourites_count'] = GroupsLikeService::count($gp['id']);
  58. $status['pf_type'] = $gp['type'];
  59. $status['visibility'] = 'public';
  60. $status['url'] = $gp->url();
  61. $status['account']['url'] = url("/groups/{$gp->group_id}/user/{$gp->profile_id}");
  62. // if($gp['type'] == 'poll') {
  63. // $status['poll'] = PollService::get($status['id']);
  64. // }
  65. return $status;
  66. }
  67. }