NotificationTransformer.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Transformer\Api\Mastodon\v1;
  3. use App\{
  4. Notification,
  5. Status
  6. };
  7. use League\Fractal;
  8. class NotificationTransformer extends Fractal\TransformerAbstract
  9. {
  10. protected $defaultIncludes = [
  11. 'account',
  12. 'status',
  13. ];
  14. public function transform(Notification $notification)
  15. {
  16. return [
  17. 'id' => (string) $notification->id,
  18. 'type' => $this->replaceTypeVerb($notification->action),
  19. 'created_at' => (string) $notification->created_at->toJSON(),
  20. ];
  21. }
  22. public function includeAccount(Notification $notification)
  23. {
  24. return $this->item($notification->actor, new AccountTransformer());
  25. }
  26. public function includeStatus(Notification $notification)
  27. {
  28. $item = $notification;
  29. if($item->item_id && $item->item_type == 'App\Status') {
  30. $status = Status::with('media')->find($item->item_id);
  31. if($status) {
  32. return $this->item($status, new StatusTransformer());
  33. } else {
  34. return;
  35. }
  36. } else {
  37. return;
  38. }
  39. }
  40. public function replaceTypeVerb($verb)
  41. {
  42. $verbs = [
  43. 'dm' => 'direct',
  44. 'follow' => 'follow',
  45. 'mention' => 'mention',
  46. 'share' => 'reblog',
  47. 'like' => 'favourite',
  48. 'comment' => 'mention',
  49. 'admin.user.modlog.comment' => 'modlog',
  50. 'tagged' => 'tagged'
  51. ];
  52. return $verbs[$verb];
  53. }
  54. }