1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2024-12-23 16:24:29 +00:00
pixelfed/app/Transformer/Api/NotificationTransformer.php

98 lines
2.4 KiB
PHP
Raw Normal View History

2018-11-29 06:51:47 +00:00
<?php
namespace App\Transformer\Api;
use App\Notification;
use App\Services\AccountService;
use App\Services\HashidService;
use App\Services\StatusService;
2018-11-29 06:51:47 +00:00
use League\Fractal;
class NotificationTransformer extends Fractal\TransformerAbstract
{
protected $defaultIncludes = [
// 'relationship',
2018-11-29 06:51:47 +00:00
];
public function transform(Notification $notification)
{
$res = [
'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
];
$n = $notification;
if($n->item_id && $n->item_type == 'App\Status' && in_array($n->action, ['group:comment'])) {
$status = $n->status;
$res['group_id'] = $status->group_id;
2018-11-29 06:51:47 +00:00
if($n->action == 'group:comment') {
$res['group_post_url'] = GroupPost::whereStatusId($status->id)->first()->url();
2019-05-27 05:04:43 +00:00
}
2018-11-29 06:51:47 +00:00
}
if(in_array($n->action, ['group.join.approved', 'group.join.rejected', 'group.like'])) {
$res['group'] = GroupService::get($n->item_id);
}
if($n->actor_id) {
$res['account'] = AccountService::get($n->actor_id);
}
if($n->item_id && $n->item_type == 'App\Status') {
$res['status'] = StatusService::get($n->item_id, false);
}
if($n->item_id && $n->item_type == 'App\ModLog') {
$ml = $n->item;
$res['modlog'] = [
'id' => $ml->object_uid,
'url' => url('/i/admin/users/modlogs/' . $ml->object_uid)
];
}
if($n->item_id && $n->item_type == 'App\MediaTag') {
$ml = $n->item;
$res['tagged'] = [
'username' => $ml->tagged_username,
'post_url' => '/p/'.HashidService::encode($ml->status_id)
];
}
return $res;
2018-11-29 06:51:47 +00:00
}
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',
'group:like' => 'favourite',
2018-12-10 19:00:50 +00:00
'comment' => 'comment',
'admin.user.modlog.comment' => 'modlog',
2021-09-04 03:23:43 +00:00
'tagged' => 'tagged',
'group:comment' => 'group:comment',
'story:react' => 'story:react',
'story:comment' => 'story:comment',
'group:join:approved' => 'group:join:approved',
'group:join:rejected' => 'group:join:rejected'
2018-11-29 06:51:47 +00:00
];
if(!isset($verbs[$verb])) {
return $verb;
}
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());
}
2018-11-29 06:51:47 +00:00
}