Status.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. namespace App;
  3. use Auth, Cache, Hashids, Storage;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\HasSnowflakePrimary;
  6. use App\Http\Controllers\StatusController;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8. use App\Models\Poll;
  9. class Status extends Model
  10. {
  11. use HasSnowflakePrimary, SoftDeletes;
  12. /**
  13. * Indicates if the IDs are auto-incrementing.
  14. *
  15. * @var bool
  16. */
  17. public $incrementing = false;
  18. /**
  19. * The attributes that should be mutated to dates.
  20. *
  21. * @var array
  22. */
  23. protected $dates = ['deleted_at'];
  24. protected $fillable = ['profile_id', 'visibility', 'in_reply_to_id', 'reblog_of_id', 'type'];
  25. const STATUS_TYPES = [
  26. 'text',
  27. 'photo',
  28. 'photo:album',
  29. 'video',
  30. 'video:album',
  31. 'photo:video:album',
  32. 'share',
  33. 'reply',
  34. 'story',
  35. 'story:reply',
  36. 'story:reaction',
  37. 'story:live',
  38. 'loop'
  39. ];
  40. const MAX_MENTIONS = 5;
  41. const MAX_HASHTAGS = 30;
  42. const MAX_LINKS = 2;
  43. public function profile()
  44. {
  45. return $this->belongsTo(Profile::class);
  46. }
  47. public function media()
  48. {
  49. return $this->hasMany(Media::class);
  50. }
  51. public function firstMedia()
  52. {
  53. return $this->hasMany(Media::class)->orderBy('order', 'asc')->first();
  54. }
  55. public function viewType()
  56. {
  57. if($this->type) {
  58. return $this->type;
  59. }
  60. return $this->setType();
  61. }
  62. public function setType()
  63. {
  64. if(in_array($this->type, self::STATUS_TYPES)) {
  65. return $this->type;
  66. }
  67. $mimes = $this->media->pluck('mime')->toArray();
  68. $type = StatusController::mimeTypeCheck($mimes);
  69. if($type) {
  70. $this->type = $type;
  71. $this->save();
  72. return $type;
  73. }
  74. }
  75. public function thumb($showNsfw = false)
  76. {
  77. $key = $showNsfw ? 'status:thumb:nsfw1'.$this->id : 'status:thumb:nsfw0'.$this->id;
  78. return Cache::remember($key, now()->addMinutes(15), function() use ($showNsfw) {
  79. $type = $this->type ?? $this->setType();
  80. $is_nsfw = !$showNsfw ? $this->is_nsfw : false;
  81. if ($this->media->count() == 0 || $is_nsfw || !in_array($type,['photo', 'photo:album', 'video'])) {
  82. return url(Storage::url('public/no-preview.png'));
  83. }
  84. return url(Storage::url($this->firstMedia()->thumbnail_path));
  85. });
  86. }
  87. public function url($forceLocal = false)
  88. {
  89. if($this->uri) {
  90. return $forceLocal ? "/i/web/post/_/{$this->profile_id}/{$this->id}" : $this->uri;
  91. } else {
  92. $id = $this->id;
  93. $username = $this->profile->username;
  94. $path = url(config('app.url')."/p/{$username}/{$id}");
  95. return $path;
  96. }
  97. }
  98. public function permalink($suffix = '/activity')
  99. {
  100. $id = $this->id;
  101. $username = $this->profile->username;
  102. $path = config('app.url')."/p/{$username}/{$id}{$suffix}";
  103. return url($path);
  104. }
  105. public function editUrl()
  106. {
  107. return $this->url().'/edit';
  108. }
  109. public function mediaUrl()
  110. {
  111. $media = $this->firstMedia();
  112. $path = $media->media_path;
  113. $hash = is_null($media->processed_at) ? md5('unprocessed') : md5($media->created_at);
  114. $url = $media->cdn_url ? $media->cdn_url . "?v={$hash}" : url(Storage::url($path)."?v={$hash}");
  115. return $url;
  116. }
  117. public function likes()
  118. {
  119. return $this->hasMany(Like::class);
  120. }
  121. public function liked() : bool
  122. {
  123. if(!Auth::check()) {
  124. return false;
  125. }
  126. $pid = Auth::user()->profile_id;
  127. return Like::select('status_id', 'profile_id')
  128. ->whereStatusId($this->id)
  129. ->whereProfileId($pid)
  130. ->exists();
  131. }
  132. public function likedBy()
  133. {
  134. return $this->hasManyThrough(
  135. Profile::class,
  136. Like::class,
  137. 'status_id',
  138. 'id',
  139. 'id',
  140. 'profile_id'
  141. );
  142. }
  143. public function comments()
  144. {
  145. return $this->hasMany(self::class, 'in_reply_to_id');
  146. }
  147. public function bookmarked()
  148. {
  149. if (!Auth::check()) {
  150. return false;
  151. }
  152. $profile = Auth::user()->profile;
  153. return Bookmark::whereProfileId($profile->id)->whereStatusId($this->id)->count();
  154. }
  155. public function shares()
  156. {
  157. return $this->hasMany(self::class, 'reblog_of_id');
  158. }
  159. public function shared() : bool
  160. {
  161. if(!Auth::check()) {
  162. return false;
  163. }
  164. $pid = Auth::user()->profile_id;
  165. return $this->select('profile_id', 'reblog_of_id')
  166. ->whereProfileId($pid)
  167. ->whereReblogOfId($this->id)
  168. ->exists();
  169. }
  170. public function sharedBy()
  171. {
  172. return $this->hasManyThrough(
  173. Profile::class,
  174. Status::class,
  175. 'reblog_of_id',
  176. 'id',
  177. 'id',
  178. 'profile_id'
  179. );
  180. }
  181. public function parent()
  182. {
  183. $parent = $this->in_reply_to_id ?? $this->reblog_of_id;
  184. if (!empty($parent)) {
  185. return $this->findOrFail($parent);
  186. } else {
  187. return false;
  188. }
  189. }
  190. public function conversation()
  191. {
  192. return $this->hasOne(Conversation::class);
  193. }
  194. public function hashtags()
  195. {
  196. return $this->hasManyThrough(
  197. Hashtag::class,
  198. StatusHashtag::class,
  199. 'status_id',
  200. 'id',
  201. 'id',
  202. 'hashtag_id'
  203. );
  204. }
  205. public function mentions()
  206. {
  207. return $this->hasManyThrough(
  208. Profile::class,
  209. Mention::class,
  210. 'status_id',
  211. 'id',
  212. 'id',
  213. 'profile_id'
  214. );
  215. }
  216. public function reportUrl()
  217. {
  218. return route('report.form')."?type=post&id={$this->id}";
  219. }
  220. public function toActivityStream()
  221. {
  222. $media = $this->media;
  223. $mediaCollection = [];
  224. foreach ($media as $image) {
  225. $mediaCollection[] = [
  226. 'type' => 'Link',
  227. 'href' => $image->url(),
  228. 'mediaType' => $image->mime,
  229. ];
  230. }
  231. $obj = [
  232. '@context' => 'https://www.w3.org/ns/activitystreams',
  233. 'type' => 'Image',
  234. 'name' => null,
  235. 'url' => $mediaCollection,
  236. ];
  237. return $obj;
  238. }
  239. public function replyToText()
  240. {
  241. $actorName = $this->profile->username;
  242. return "{$actorName} ".__('notification.commented');
  243. }
  244. public function replyToHtml()
  245. {
  246. $actorName = $this->profile->username;
  247. $actorUrl = $this->profile->url();
  248. return "<a href='{$actorUrl}' class='profile-link'>{$actorName}</a> ".
  249. __('notification.commented');
  250. }
  251. public function shareToText()
  252. {
  253. $actorName = $this->profile->username;
  254. return "{$actorName} ".__('notification.shared');
  255. }
  256. public function shareToHtml()
  257. {
  258. $actorName = $this->profile->username;
  259. $actorUrl = $this->profile->url();
  260. return "<a href='{$actorUrl}' class='profile-link'>{$actorName}</a> ".
  261. __('notification.shared');
  262. }
  263. public function recentComments()
  264. {
  265. return $this->comments()->orderBy('created_at', 'desc')->take(3);
  266. }
  267. public function toActivityPubObject()
  268. {
  269. if($this->local == false) {
  270. return;
  271. }
  272. $profile = $this->profile;
  273. $to = $this->scopeToAudience('to');
  274. $cc = $this->scopeToAudience('cc');
  275. return [
  276. '@context' => 'https://www.w3.org/ns/activitystreams',
  277. 'id' => $this->permalink(),
  278. 'type' => 'Create',
  279. 'actor' => $profile->permalink(),
  280. 'published' => $this->created_at->format('c'),
  281. 'to' => $to,
  282. 'cc' => $cc,
  283. 'object' => [
  284. 'id' => $this->url(),
  285. 'type' => 'Note',
  286. 'summary' => null,
  287. 'inReplyTo' => null,
  288. 'published' => $this->created_at->format('c'),
  289. 'url' => $this->url(),
  290. 'attributedTo' => $this->profile->url(),
  291. 'to' => $to,
  292. 'cc' => $cc,
  293. 'sensitive' => (bool) $this->is_nsfw,
  294. 'content' => $this->rendered,
  295. 'attachment' => $this->media->map(function($media) {
  296. return [
  297. 'type' => 'Document',
  298. 'mediaType' => $media->mime,
  299. 'url' => $media->url(),
  300. 'name' => null
  301. ];
  302. })->toArray()
  303. ]
  304. ];
  305. }
  306. public function scopeToAudience($audience)
  307. {
  308. if(!in_array($audience, ['to', 'cc']) || $this->local == false) {
  309. return;
  310. }
  311. $res = [];
  312. $res['to'] = [];
  313. $res['cc'] = [];
  314. $scope = $this->scope;
  315. $mentions = $this->mentions->map(function ($mention) {
  316. return $mention->permalink();
  317. })->toArray();
  318. if($this->in_reply_to_id != null) {
  319. $parent = $this->parent();
  320. if($parent) {
  321. $mentions = array_merge([$parent->profile->permalink()], $mentions);
  322. }
  323. }
  324. switch ($scope) {
  325. case 'public':
  326. $res['to'] = [
  327. "https://www.w3.org/ns/activitystreams#Public"
  328. ];
  329. $res['cc'] = array_merge([$this->profile->permalink('/followers')], $mentions);
  330. break;
  331. case 'unlisted':
  332. $res['to'] = array_merge([$this->profile->permalink('/followers')], $mentions);
  333. $res['cc'] = [
  334. "https://www.w3.org/ns/activitystreams#Public"
  335. ];
  336. break;
  337. case 'private':
  338. $res['to'] = array_merge([$this->profile->permalink('/followers')], $mentions);
  339. $res['cc'] = [];
  340. break;
  341. // TODO: Update scope when DMs are supported
  342. case 'direct':
  343. $res['to'] = [];
  344. $res['cc'] = [];
  345. break;
  346. }
  347. return $res[$audience];
  348. }
  349. public function place()
  350. {
  351. return $this->belongsTo(Place::class);
  352. }
  353. public function directMessage()
  354. {
  355. return $this->hasOne(DirectMessage::class);
  356. }
  357. public function poll()
  358. {
  359. return $this->hasOne(Poll::class);
  360. }
  361. }