NotificationTransformer.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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) str_replace('+00:00', 'Z', $notification->created_at->format(DATE_RFC3339_EXTENDED)),
  17. ];
  18. $n = $notification;
  19. if($n->actor_id) {
  20. $res['account'] = AccountService::get($n->actor_id);
  21. if($n->profile_id != $n->actor_id) {
  22. $res['relationship'] = RelationshipService::get($n->actor_id, $n->profile_id);
  23. }
  24. }
  25. if($n->item_id && $n->item_type == 'App\Status') {
  26. $res['status'] = StatusService::get($n->item_id, false);
  27. }
  28. if($n->item_id && $n->item_type == 'App\ModLog') {
  29. $ml = $n->item;
  30. if($ml && $ml->object_uid) {
  31. $res['modlog'] = [
  32. 'id' => $ml->object_uid,
  33. 'url' => url('/i/admin/users/modlogs/' . $ml->object_uid)
  34. ];
  35. }
  36. }
  37. if($n->item_id && $n->item_type == 'App\MediaTag') {
  38. $ml = $n->item;
  39. if($ml && $ml->tagged_username) {
  40. $res['tagged'] = [
  41. 'username' => $ml->tagged_username,
  42. 'post_url' => '/p/'.HashidService::encode($ml->status_id)
  43. ];
  44. }
  45. }
  46. return $res;
  47. }
  48. public function replaceTypeVerb($verb)
  49. {
  50. $verbs = [
  51. 'dm' => 'direct',
  52. 'follow' => 'follow',
  53. 'mention' => 'mention',
  54. 'reblog' => 'share',
  55. 'share' => 'share',
  56. 'like' => 'favourite',
  57. 'group:like' => 'favourite',
  58. 'comment' => 'comment',
  59. 'admin.user.modlog.comment' => 'modlog',
  60. 'tagged' => 'tagged',
  61. 'story:react' => 'story:react',
  62. 'story:comment' => 'story:comment',
  63. ];
  64. if(!isset($verbs[$verb])) {
  65. return $verb;
  66. }
  67. return $verbs[$verb];
  68. }
  69. }