1
0

GroupAccountService.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace App\Services\Groups;
  3. use App\Models\Group;
  4. use App\Models\GroupPost;
  5. use App\Models\GroupMember;
  6. use Cache;
  7. use Purify;
  8. use App\Services\AccountService;
  9. use App\Services\GroupService;
  10. class GroupAccountService
  11. {
  12. const CACHE_KEY = 'pf:services:groups:accounts-v0:';
  13. public static function get($gid, $pid)
  14. {
  15. $group = GroupService::get($gid);
  16. if(!$group) {
  17. return;
  18. }
  19. $account = AccountService::get($pid, true);
  20. if(!$account) {
  21. return;
  22. }
  23. $key = self::CACHE_KEY . $gid . ':' . $pid;
  24. $account['group'] = Cache::remember($key, 3600, function() use($gid, $pid) {
  25. $membership = GroupMember::whereGroupId($gid)->whereProfileId($pid)->first();
  26. if(!$membership) {
  27. return [];
  28. }
  29. return [
  30. 'joined' => $membership->created_at->format('c'),
  31. 'role' => $membership->role,
  32. 'local_group' => (bool) $membership->local_group,
  33. 'local_profile' => (bool) $membership->local_profile,
  34. ];
  35. });
  36. return $account;
  37. }
  38. public static function del($gid, $pid)
  39. {
  40. $key = self::CACHE_KEY . $gid . ':' . $pid;
  41. return Cache::forget($key);
  42. }
  43. }