NotificationTransformer.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Transformer\Api;
  3. use App\{
  4. Notification,
  5. Status
  6. };
  7. use App\Services\HashidService;
  8. use League\Fractal;
  9. class NotificationTransformer extends Fractal\TransformerAbstract
  10. {
  11. protected $defaultIncludes = [
  12. 'account',
  13. 'status',
  14. 'relationship',
  15. 'modlog',
  16. 'tagged'
  17. ];
  18. public function transform(Notification $notification)
  19. {
  20. return [
  21. 'id' => (string) $notification->id,
  22. 'type' => $this->replaceTypeVerb($notification->action),
  23. 'created_at' => (string) $notification->created_at->format('c'),
  24. ];
  25. }
  26. public function includeAccount(Notification $notification)
  27. {
  28. return $this->item($notification->actor, new AccountTransformer());
  29. }
  30. public function includeStatus(Notification $notification)
  31. {
  32. $item = $notification;
  33. if($item->item_id && $item->item_type == 'App\Status') {
  34. $status = Status::with('media')->find($item->item_id);
  35. if($status) {
  36. return $this->item($status, new StatusTransformer());
  37. } else {
  38. return null;
  39. }
  40. } else {
  41. return null;
  42. }
  43. }
  44. public function replaceTypeVerb($verb)
  45. {
  46. $verbs = [
  47. 'dm' => 'direct',
  48. 'follow' => 'follow',
  49. 'mention' => 'mention',
  50. 'reblog' => 'share',
  51. 'share' => 'share',
  52. 'like' => 'favourite',
  53. 'comment' => 'comment',
  54. 'admin.user.modlog.comment' => 'modlog',
  55. 'tagged' => 'tagged'
  56. ];
  57. return $verbs[$verb];
  58. }
  59. public function includeRelationship(Notification $notification)
  60. {
  61. return $this->item($notification->actor, new RelationshipTransformer());
  62. }
  63. public function includeModlog(Notification $notification)
  64. {
  65. $n = $notification;
  66. if($n->item_id && $n->item_type == 'App\ModLog') {
  67. $ml = $n->item;
  68. if(!empty($ml)) {
  69. $res = $this->item($ml, function($ml) {
  70. return [
  71. 'id' => $ml->object_uid,
  72. 'url' => url('/i/admin/users/modlogs/' . $ml->object_uid)
  73. ];
  74. });
  75. return $res;
  76. } else {
  77. return null;
  78. }
  79. } else {
  80. return null;
  81. }
  82. }
  83. public function includeTagged(Notification $notification)
  84. {
  85. $n = $notification;
  86. if($n->item_id && $n->item_type == 'App\MediaTag') {
  87. $ml = $n->item;
  88. $res = $this->item($ml, function($ml) {
  89. return [
  90. 'username' => $ml->tagged_username,
  91. 'post_url' => '/p/'.HashidService::encode($ml->status_id)
  92. ];
  93. });
  94. return $res;
  95. } else {
  96. return null;
  97. }
  98. }
  99. }