Story.php 2.2 KB

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