NotificationTransformer.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Transformer\Api;
  3. use App\Notification;
  4. use App\Services\AccountService;
  5. use App\Services\HashidService;
  6. use App\Services\RelationshipService;
  7. use App\Services\StatusService;
  8. use League\Fractal;
  9. class NotificationTransformer extends Fractal\TransformerAbstract
  10. {
  11. public function transform(Notification $notification)
  12. {
  13. $res = [
  14. 'id' => (string) $notification->id,
  15. 'type' => $this->replaceTypeVerb($notification->action),
  16. 'created_at' => (string) $notification->created_at->format('c'),
  17. ];
  18. $n = $notification;
  19. if($n->actor_id) {
  20. $res['account'] = AccountService::get($n->actor_id);
  21. $res['relationship'] = RelationshipService::get($n->actor_id, $n->profile_id);
  22. }
  23. if($n->item_id && $n->item_type == 'App\Status') {
  24. $res['status'] = StatusService::get($n->item_id, false);
  25. }
  26. if($n->item_id && $n->item_type == 'App\ModLog') {
  27. $ml = $n->item;
  28. $res['modlog'] = [
  29. 'id' => $ml->object_uid,
  30. 'url' => url('/i/admin/users/modlogs/' . $ml->object_uid)
  31. ];
  32. }
  33. if($n->item_id && $n->item_type == 'App\MediaTag') {
  34. $ml = $n->item;
  35. $res['tagged'] = [
  36. 'username' => $ml->tagged_username,
  37. 'post_url' => '/p/'.HashidService::encode($ml->status_id)
  38. ];
  39. }
  40. return $res;
  41. }
  42. public function replaceTypeVerb($verb)
  43. {
  44. $verbs = [
  45. 'dm' => 'direct',
  46. 'follow' => 'follow',
  47. 'mention' => 'mention',
  48. 'reblog' => 'share',
  49. 'share' => 'share',
  50. 'like' => 'favourite',
  51. 'group:like' => 'favourite',
  52. 'comment' => 'comment',
  53. 'admin.user.modlog.comment' => 'modlog',
  54. 'tagged' => 'tagged',
  55. 'group:comment' => 'group:comment',
  56. 'story:react' => 'story:react',
  57. 'story:comment' => 'story:comment',
  58. 'group:join:approved' => 'group:join:approved',
  59. 'group:join:rejected' => 'group:join:rejected'
  60. ];
  61. if(!isset($verbs[$verb])) {
  62. return $verb;
  63. }
  64. return $verbs[$verb];
  65. }
  66. }