1
0

Group.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\HasSnowflakePrimary;
  6. use App\Profile;
  7. use App\Services\GroupService;
  8. use Illuminate\Database\Eloquent\SoftDeletes;
  9. class Group extends Model
  10. {
  11. use HasSnowflakePrimary, HasFactory, SoftDeletes;
  12. /**
  13. * Indicates if the IDs are auto-incrementing.
  14. *
  15. * @var bool
  16. */
  17. public $incrementing = false;
  18. protected $casts = [
  19. 'metadata' => 'json'
  20. ];
  21. public function url()
  22. {
  23. return url("/groups/{$this->id}");
  24. }
  25. public function permalink($suffix = null)
  26. {
  27. if(!$this->local) {
  28. return $this->remote_url;
  29. }
  30. return $this->url() . $suffix;
  31. }
  32. public function members()
  33. {
  34. return $this->hasMany(GroupMember::class);
  35. }
  36. public function admin()
  37. {
  38. return $this->belongsTo(Profile::class, 'profile_id');
  39. }
  40. public function isMember($id = false)
  41. {
  42. $id = $id ?? request()->user()->profile_id;
  43. // return $this->members()->whereProfileId($id)->whereJoinRequest(false)->exists();
  44. return GroupService::isMember($this->id, $id);
  45. }
  46. public function getMembershipType()
  47. {
  48. return $this->is_private ? 'private' : ($this->is_local ? 'local' : 'all');
  49. }
  50. public function selfRole($id = false)
  51. {
  52. $id = $id ?? request()->user()->profile_id;
  53. return optional($this->members()->whereProfileId($id)->first())->role ?? null;
  54. }
  55. }