NotificationTransformer.php 2.5 KB

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