Merge pull request #5163 from pixelfed/staging

Update ApiController, add pe support to like/unlike endpoints
This commit is contained in:
daniel 2024-06-08 06:22:51 -06:00 committed by GitHub
commit c24e5b9bf7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 466 additions and 453 deletions

View file

@ -1352,7 +1352,8 @@ class ApiV1Controller extends Controller
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && ! UserRoleService::can('can-like', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-like', $user->id), 403, 'Invalid permissions for this action');
$status = StatusService::getMastodon($id, false); $napi = $request->has(self::PF_API_ENTITY_KEY);
$status = $napi ? StatusService::get($id, false) : StatusService::getMastodon($id, false);
abort_unless($status, 404); abort_unless($status, 404);
@ -1420,34 +1421,47 @@ class ApiV1Controller extends Controller
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && ! UserRoleService::can('can-like', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-like', $user->id), 403, 'Invalid permissions for this action');
$napi = $request->has(self::PF_API_ENTITY_KEY);
$status = $napi ? StatusService::get($id, false) : StatusService::getMastodon($id, false);
abort_unless($status && isset($status['account']), 404);
if ($status && isset($status['account'], $status['account']['acct']) && strpos($status['account']['acct'], '@') != -1) {
$domain = parse_url($status['account']['url'], PHP_URL_HOST);
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
}
$spid = $status['account']['id'];
AccountService::setLastActive($user->id); AccountService::setLastActive($user->id);
$status = Status::findOrFail($id); if (intval($spid) !== intval($user->profile_id)) {
if ($status['visibility'] == 'private') {
if (intval($status->profile_id) !== intval($user->profile_id)) { abort_if(! FollowerService::follows($user->profile_id, $spid), 403);
if ($status->scope == 'private') {
abort_if(! $status->profile->followedBy($user->profile), 403);
} else { } else {
abort_if(! in_array($status->scope, ['public', 'unlisted']), 403); abort_if(! in_array($status['visibility'], ['public', 'unlisted']), 403);
} }
} }
$like = Like::whereProfileId($user->profile_id) $like = Like::whereProfileId($user->profile_id)
->whereStatusId($status->id) ->whereStatusId($status['id'])
->first(); ->first();
if ($like) { if ($like) {
$like->forceDelete(); $like->forceDelete();
$status->likes_count = $status->likes_count > 1 ? $status->likes_count - 1 : 0; $ogStatus = Status::find($status['id']);
$status->save(); if ($ogStatus) {
$ogStatus->likes_count = $ogStatus->likes_count > 1 ? $ogStatus->likes_count - 1 : 0;
$ogStatus->save();
}
} }
StatusService::del($status->id); StatusService::del($status['id']);
$res = StatusService::getMastodon($status->id, false); $status['favourited'] = false;
$res['favourited'] = false; $status['favourites_count'] = isset($ogStatus) ? $ogStatus->likes_count : $status['favourites_count'] - 1;
return $this->json($res); return $this->json($status);
} }
/** /**

View file

@ -2,47 +2,28 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use App\Follower;
use App\{ use App\Profile;
Hashtag, use App\Services\AccountService;
Follower, use App\Services\BookmarkService;
Like, use App\Services\FollowerService;
Media,
Notification,
Profile,
StatusHashtag,
Status,
StatusView,
UserFilter
};
use Auth, Cache, DB;
use Illuminate\Support\Facades\Redis;
use Carbon\Carbon;
use League\Fractal;
use App\Transformer\Api\{
AccountTransformer,
RelationshipTransformer,
StatusTransformer,
StatusStatelessTransformer
};
use App\Services\{
AccountService,
BookmarkService,
FollowerService,
LikeService,
PublicTimelineService,
ProfileService,
NetworkTimelineService,
ReblogService,
RelationshipService,
StatusService,
SnowflakeService,
UserFilterService
};
use App\Jobs\StatusPipeline\NewStatusPipeline;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use App\Services\InstanceService; use App\Services\InstanceService;
use App\Services\LikeService;
use App\Services\NetworkTimelineService;
use App\Services\PublicTimelineService;
use App\Services\ReblogService;
use App\Services\RelationshipService;
use App\Services\SnowflakeService;
use App\Services\StatusService;
use App\Services\UserFilterService;
use App\Status;
use App\Transformer\Api\StatusStatelessTransformer;
use Auth;
use Cache;
use Illuminate\Http\Request;
use League\Fractal;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Serializer\ArraySerializer;
class PublicApiController extends Controller class PublicApiController extends Controller
{ {
@ -118,6 +99,7 @@ class PublicApiController extends Controller
'bookmarked' => false, 'bookmarked' => false,
], ],
]; ];
return response()->json($res); return response()->json($res);
} }
$res = [ $res = [
@ -130,6 +112,7 @@ class PublicApiController extends Controller
'bookmarked' => (bool) $status->bookmarked(), 'bookmarked' => (bool) $status->bookmarked(),
], ],
]; ];
return response()->json($res); return response()->json($res);
} }
@ -138,7 +121,7 @@ class PublicApiController extends Controller
$this->validate($request, [ $this->validate($request, [
'min_id' => 'nullable|integer|min:1', 'min_id' => 'nullable|integer|min:1',
'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX, 'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
'limit' => 'nullable|integer|min:5|max:50' 'limit' => 'nullable|integer|min:5|max:50',
]); ]);
$limit = $request->limit ?? 10; $limit = $request->limit ?? 10;
@ -184,6 +167,7 @@ class PublicApiController extends Controller
$resource = new Fractal\Resource\Collection($replies, new StatusStatelessTransformer(), 'data'); $resource = new Fractal\Resource\Collection($replies, new StatusStatelessTransformer(), 'data');
$resource->setPaginator(new IlluminatePaginatorAdapter($replies)); $resource->setPaginator(new IlluminatePaginatorAdapter($replies));
$res = $this->fractal->createData($resource)->toArray(); $res = $this->fractal->createData($resource)->toArray();
return response()->json($res, 200, [], JSON_PRETTY_PRINT); return response()->json($res, 200, [], JSON_PRETTY_PRINT);
} }
@ -202,8 +186,8 @@ class PublicApiController extends Controller
if (! $user) { if (! $user) {
abort(403); abort(403);
} else { } else {
$follows = $profile->followedBy($user->profile); $follows = FollowerService::follows($profile->id, $user->profile_id);
if($follows == false && $profile->id !== $user->profile->id && $user->is_admin == false) { if ($follows == false && $profile->id !== $user->profile_id && $user->is_admin == false) {
abort(404); abort(404);
} }
} }
@ -229,7 +213,7 @@ class PublicApiController extends Controller
'page' => 'nullable|integer|max:40', 'page' => 'nullable|integer|max:40',
'min_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX, 'min_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'max_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX, 'max_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'limit' => 'nullable|integer|max:30' 'limit' => 'nullable|integer|max:30',
]); ]);
if (! $request->user()) { if (! $request->user()) {
@ -274,6 +258,7 @@ class PublicApiController extends Controller
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id); $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id); $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
return $status; return $status;
}) })
->filter(function ($s) use ($filtered) { ->filter(function ($s) use ($filtered) {
@ -320,6 +305,7 @@ class PublicApiController extends Controller
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id); $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id); $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
return $status; return $status;
}) })
->filter(function ($s) use ($filtered) { ->filter(function ($s) use ($filtered) {
@ -354,6 +340,7 @@ class PublicApiController extends Controller
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k); $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k);
$status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']); $status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
} }
return $status; return $status;
}) })
->filter(function ($s) use ($filtered) { ->filter(function ($s) use ($filtered) {
@ -378,7 +365,7 @@ class PublicApiController extends Controller
'max_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX, 'max_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'limit' => 'nullable|integer|max:40', 'limit' => 'nullable|integer|max:40',
'recent_feed' => 'nullable', 'recent_feed' => 'nullable',
'recent_min' => 'nullable|integer' 'recent_min' => 'nullable|integer',
]); ]);
$recentFeed = $request->input('recent_feed') == 'true'; $recentFeed = $request->input('recent_feed') == 'true';
@ -400,6 +387,7 @@ class PublicApiController extends Controller
$following = Cache::remember('profile:following:'.$pid, 1209600, function () use ($pid) { $following = Cache::remember('profile:following:'.$pid, 1209600, function () use ($pid) {
$following = Follower::whereProfileId($pid)->pluck('following_id'); $following = Follower::whereProfileId($pid)->pluck('following_id');
return $following->push($pid)->toArray(); return $following->push($pid)->toArray();
}); });
@ -412,6 +400,7 @@ class PublicApiController extends Controller
if ($min || $max) { if ($min || $max) {
$dir = $min ? '>' : '<'; $dir = $min ? '>' : '<';
$id = $min ?? $max; $id = $min ?? $max;
return Status::select( return Status::select(
'id', 'id',
'uri', 'uri',
@ -454,6 +443,7 @@ class PublicApiController extends Controller
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id); $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id); $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
return $status; return $status;
}) })
->filter(function ($s) use ($filtered) { ->filter(function ($s) use ($filtered) {
@ -503,6 +493,7 @@ class PublicApiController extends Controller
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id); $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id); $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
return $status; return $status;
}) })
->filter(function ($s) use ($filtered) { ->filter(function ($s) use ($filtered) {
@ -525,7 +516,7 @@ class PublicApiController extends Controller
'page' => 'nullable|integer|max:40', 'page' => 'nullable|integer|max:40',
'min_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX, 'min_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'max_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX, 'max_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'limit' => 'nullable|integer|max:30' 'limit' => 'nullable|integer|max:30',
]); ]);
$page = $request->input('page'); $page = $request->input('page');
@ -567,6 +558,7 @@ class PublicApiController extends Controller
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id); $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id); $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
return $status; return $status;
}); });
$res = $timeline->toArray(); $res = $timeline->toArray();
@ -595,6 +587,7 @@ class PublicApiController extends Controller
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id); $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id); $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
return $status; return $status;
}); });
$res = $timeline->toArray(); $res = $timeline->toArray();
@ -624,6 +617,7 @@ class PublicApiController extends Controller
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k); $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k);
$status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']); $status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
} }
return $status; return $status;
}) })
->filter(function ($s) use ($filtered) { ->filter(function ($s) use ($filtered) {
@ -646,7 +640,7 @@ class PublicApiController extends Controller
$this->validate($request, [ $this->validate($request, [
'id' => 'required|array|min:1|max:20', 'id' => 'required|array|min:1|max:20',
'id.*' => 'required|integer' 'id.*' => 'required|integer',
]); ]);
$ids = collect($request->input('id')); $ids = collect($request->input('id'));
$res = $ids->filter(function ($v) use ($pid) { $res = $ids->filter(function ($v) use ($pid) {
@ -666,6 +660,7 @@ class PublicApiController extends Controller
$domain = parse_url($res['url'], PHP_URL_HOST); $domain = parse_url($res['url'], PHP_URL_HOST);
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404); abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
} }
return response()->json($res); return response()->json($res);
} }
@ -678,7 +673,7 @@ class PublicApiController extends Controller
'max_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX, 'max_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'since_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX, 'since_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'min_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX, 'min_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'limit' => 'nullable|integer|min:1|max:24' 'limit' => 'nullable|integer|min:1|max:24',
]); ]);
$user = $request->user(); $user = $request->user();
@ -707,17 +702,19 @@ class PublicApiController extends Controller
$pid = $user->profile_id; $pid = $user->profile_id;
$following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function () use ($pid) { $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function () use ($pid) {
$following = Follower::whereProfileId($pid)->pluck('following_id'); $following = Follower::whereProfileId($pid)->pluck('following_id');
return $following->push($pid)->toArray(); return $following->push($pid)->toArray();
}); });
$visibility = true == in_array($profile['id'], $following) ? ['public', 'unlisted', 'private'] : []; $visibility = in_array($profile['id'], $following) == true ? ['public', 'unlisted', 'private'] : [];
} else { } else {
if ($user) { if ($user) {
$pid = $user->profile_id; $pid = $user->profile_id;
$following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function () use ($pid) { $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function () use ($pid) {
$following = Follower::whereProfileId($pid)->pluck('following_id'); $following = Follower::whereProfileId($pid)->pluck('following_id');
return $following->push($pid)->toArray(); return $following->push($pid)->toArray();
}); });
$visibility = true == in_array($profile['id'], $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted']; $visibility = in_array($profile['id'], $following) == true ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
} else { } else {
$visibility = ['public', 'unlisted']; $visibility = ['public', 'unlisted'];
} }
@ -742,6 +739,7 @@ class PublicApiController extends Controller
if ($user && $status) { if ($user && $status) {
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
} }
return $status; return $status;
}) })
->filter(function ($s) use ($onlyMedia) { ->filter(function ($s) use ($onlyMedia) {
@ -754,6 +752,7 @@ class PublicApiController extends Controller
return false; return false;
} }
} }
return $s; return $s;
}) })
->values(); ->values();