2018-11-29 06:51:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Transformer\Api;
|
|
|
|
|
2019-05-27 05:04:43 +00:00
|
|
|
use App\{
|
|
|
|
Notification,
|
|
|
|
Status
|
|
|
|
};
|
2020-12-14 00:28:33 +00:00
|
|
|
use App\Services\HashidService;
|
2018-11-29 06:51:47 +00:00
|
|
|
use League\Fractal;
|
|
|
|
|
|
|
|
class NotificationTransformer extends Fractal\TransformerAbstract
|
|
|
|
{
|
|
|
|
protected $defaultIncludes = [
|
|
|
|
'account',
|
|
|
|
'status',
|
2020-02-22 04:00:00 +00:00
|
|
|
'relationship',
|
2020-07-15 16:37:37 +00:00
|
|
|
'modlog',
|
|
|
|
'tagged'
|
2018-11-29 06:51:47 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
public function transform(Notification $notification)
|
|
|
|
{
|
|
|
|
return [
|
2019-01-28 02:40:36 +00:00
|
|
|
'id' => (string) $notification->id,
|
2018-11-29 06:51:47 +00:00
|
|
|
'type' => $this->replaceTypeVerb($notification->action),
|
2019-09-21 05:45:19 +00:00
|
|
|
'created_at' => (string) $notification->created_at->format('c'),
|
2018-11-29 06:51:47 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function includeAccount(Notification $notification)
|
|
|
|
{
|
|
|
|
return $this->item($notification->actor, new AccountTransformer());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function includeStatus(Notification $notification)
|
|
|
|
{
|
2019-05-27 05:04:43 +00:00
|
|
|
$item = $notification;
|
|
|
|
if($item->item_id && $item->item_type == 'App\Status') {
|
|
|
|
$status = Status::with('media')->find($item->item_id);
|
|
|
|
if($status) {
|
|
|
|
return $this->item($status, new StatusTransformer());
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-29 06:51:47 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function replaceTypeVerb($verb)
|
|
|
|
{
|
|
|
|
$verbs = [
|
2020-11-18 21:19:02 +00:00
|
|
|
'dm' => 'direct',
|
2018-11-29 06:51:47 +00:00
|
|
|
'follow' => 'follow',
|
|
|
|
'mention' => 'mention',
|
|
|
|
'reblog' => 'share',
|
2019-01-15 05:34:27 +00:00
|
|
|
'share' => 'share',
|
2018-11-29 06:51:47 +00:00
|
|
|
'like' => 'favourite',
|
2018-12-10 19:00:50 +00:00
|
|
|
'comment' => 'comment',
|
2020-07-15 16:37:37 +00:00
|
|
|
'admin.user.modlog.comment' => 'modlog',
|
|
|
|
'tagged' => 'tagged'
|
2018-11-29 06:51:47 +00:00
|
|
|
];
|
|
|
|
return $verbs[$verb];
|
|
|
|
}
|
2019-05-27 05:04:43 +00:00
|
|
|
|
|
|
|
public function includeRelationship(Notification $notification)
|
|
|
|
{
|
|
|
|
return $this->item($notification->actor, new RelationshipTransformer());
|
|
|
|
}
|
2020-02-22 04:00:00 +00:00
|
|
|
|
|
|
|
public function includeModlog(Notification $notification)
|
|
|
|
{
|
|
|
|
$n = $notification;
|
|
|
|
if($n->item_id && $n->item_type == 'App\ModLog') {
|
|
|
|
$ml = $n->item;
|
|
|
|
if(!empty($ml)) {
|
|
|
|
$res = $this->item($ml, function($ml) {
|
|
|
|
return [
|
|
|
|
'id' => $ml->object_uid,
|
|
|
|
'url' => url('/i/admin/users/modlogs/' . $ml->object_uid)
|
|
|
|
];
|
|
|
|
});
|
|
|
|
return $res;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 16:37:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
public function includeTagged(Notification $notification)
|
|
|
|
{
|
|
|
|
$n = $notification;
|
|
|
|
if($n->item_id && $n->item_type == 'App\MediaTag') {
|
|
|
|
$ml = $n->item;
|
|
|
|
$res = $this->item($ml, function($ml) {
|
|
|
|
return [
|
2020-12-14 00:28:33 +00:00
|
|
|
'username' => $ml->tagged_username,
|
|
|
|
'post_url' => '/p/'.HashidService::encode($ml->status_id)
|
2020-07-15 16:37:37 +00:00
|
|
|
];
|
|
|
|
});
|
|
|
|
return $res;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2018-11-29 06:51:47 +00:00
|
|
|
}
|