Story.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App;
  3. use Auth;
  4. use Storage;
  5. use Illuminate\Database\Eloquent\Model;
  6. use App\HasSnowflakePrimary;
  7. use App\Util\Lexer\Bearcap;
  8. class Story extends Model
  9. {
  10. use HasSnowflakePrimary;
  11. public const MAX_PER_DAY = 20;
  12. /**
  13. * Indicates if the IDs are auto-incrementing.
  14. *
  15. * @var bool
  16. */
  17. public $incrementing = false;
  18. protected $casts = [
  19. 'expires_at' => 'datetime'
  20. ];
  21. protected $fillable = ['profile_id', 'view_count'];
  22. protected $visible = ['id'];
  23. protected $hidden = ['json'];
  24. public function profile()
  25. {
  26. return $this->belongsTo(Profile::class);
  27. }
  28. public function views()
  29. {
  30. return $this->hasMany(StoryView::class);
  31. }
  32. public function seen($pid = false)
  33. {
  34. return StoryView::whereStoryId($this->id)
  35. ->whereProfileId(Auth::user()->profile->id)
  36. ->exists();
  37. }
  38. public function permalink()
  39. {
  40. $username = $this->profile->username;
  41. return url("/stories/{$username}/{$this->id}/activity");
  42. }
  43. public function url()
  44. {
  45. $username = $this->profile->username;
  46. return url("/stories/{$username}/{$this->id}");
  47. }
  48. public function mediaUrl()
  49. {
  50. return url(Storage::url($this->path));
  51. }
  52. public function bearcapUrl()
  53. {
  54. return Bearcap::encode($this->url(), $this->bearcap_token);
  55. }
  56. public function scopeToAudience($scope)
  57. {
  58. $res = [];
  59. switch ($scope) {
  60. case 'to':
  61. $res = [
  62. $this->profile->permalink('/followers')
  63. ];
  64. break;
  65. default:
  66. $res = [];
  67. break;
  68. }
  69. return $res;
  70. }
  71. }