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();
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);
@ -1420,34 +1421,47 @@ class ApiV1Controller extends Controller
$user = $request->user();
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);
$status = Status::findOrFail($id);
if (intval($status->profile_id) !== intval($user->profile_id)) {
if ($status->scope == 'private') {
abort_if(! $status->profile->followedBy($user->profile), 403);
if (intval($spid) !== intval($user->profile_id)) {
if ($status['visibility'] == 'private') {
abort_if(! FollowerService::follows($user->profile_id, $spid), 403);
} 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)
->whereStatusId($status->id)
->whereStatusId($status['id'])
->first();
if ($like) {
$like->forceDelete();
$status->likes_count = $status->likes_count > 1 ? $status->likes_count - 1 : 0;
$status->save();
$ogStatus = Status::find($status['id']);
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);
$res['favourited'] = false;
$status['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;
use Illuminate\Http\Request;
use App\{
Hashtag,
Follower,
Like,
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\Follower;
use App\Profile;
use App\Services\AccountService;
use App\Services\BookmarkService;
use App\Services\FollowerService;
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
{
@ -118,6 +99,7 @@ class PublicApiController extends Controller
'bookmarked' => false,
],
];
return response()->json($res);
}
$res = [
@ -130,6 +112,7 @@ class PublicApiController extends Controller
'bookmarked' => (bool) $status->bookmarked(),
],
];
return response()->json($res);
}
@ -138,7 +121,7 @@ class PublicApiController extends Controller
$this->validate($request, [
'min_id' => 'nullable|integer|min:1',
'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;
@ -184,6 +167,7 @@ class PublicApiController extends Controller
$resource = new Fractal\Resource\Collection($replies, new StatusStatelessTransformer(), 'data');
$resource->setPaginator(new IlluminatePaginatorAdapter($replies));
$res = $this->fractal->createData($resource)->toArray();
return response()->json($res, 200, [], JSON_PRETTY_PRINT);
}
@ -202,8 +186,8 @@ class PublicApiController extends Controller
if (! $user) {
abort(403);
} else {
$follows = $profile->followedBy($user->profile);
if($follows == false && $profile->id !== $user->profile->id && $user->is_admin == false) {
$follows = FollowerService::follows($profile->id, $user->profile_id);
if ($follows == false && $profile->id !== $user->profile_id && $user->is_admin == false) {
abort(404);
}
}
@ -229,7 +213,7 @@ class PublicApiController extends Controller
'page' => 'nullable|integer|max:40',
'min_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()) {
@ -274,6 +258,7 @@ class PublicApiController extends Controller
$status['favourited'] = (bool) LikeService::liked($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);
return $status;
})
->filter(function ($s) use ($filtered) {
@ -320,6 +305,7 @@ class PublicApiController extends Controller
$status['favourited'] = (bool) LikeService::liked($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);
return $status;
})
->filter(function ($s) use ($filtered) {
@ -354,6 +340,7 @@ class PublicApiController extends Controller
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k);
$status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
}
return $status;
})
->filter(function ($s) use ($filtered) {
@ -378,7 +365,7 @@ class PublicApiController extends Controller
'max_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'limit' => 'nullable|integer|max:40',
'recent_feed' => 'nullable',
'recent_min' => 'nullable|integer'
'recent_min' => 'nullable|integer',
]);
$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 = Follower::whereProfileId($pid)->pluck('following_id');
return $following->push($pid)->toArray();
});
@ -412,6 +400,7 @@ class PublicApiController extends Controller
if ($min || $max) {
$dir = $min ? '>' : '<';
$id = $min ?? $max;
return Status::select(
'id',
'uri',
@ -454,6 +443,7 @@ class PublicApiController extends Controller
$status['favourited'] = (bool) LikeService::liked($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);
return $status;
})
->filter(function ($s) use ($filtered) {
@ -503,6 +493,7 @@ class PublicApiController extends Controller
$status['favourited'] = (bool) LikeService::liked($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);
return $status;
})
->filter(function ($s) use ($filtered) {
@ -525,7 +516,7 @@ class PublicApiController extends Controller
'page' => 'nullable|integer|max:40',
'min_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');
@ -567,6 +558,7 @@ class PublicApiController extends Controller
$status['favourited'] = (bool) LikeService::liked($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);
return $status;
});
$res = $timeline->toArray();
@ -595,6 +587,7 @@ class PublicApiController extends Controller
$status['favourited'] = (bool) LikeService::liked($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);
return $status;
});
$res = $timeline->toArray();
@ -624,6 +617,7 @@ class PublicApiController extends Controller
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k);
$status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
}
return $status;
})
->filter(function ($s) use ($filtered) {
@ -646,7 +640,7 @@ class PublicApiController extends Controller
$this->validate($request, [
'id' => 'required|array|min:1|max:20',
'id.*' => 'required|integer'
'id.*' => 'required|integer',
]);
$ids = collect($request->input('id'));
$res = $ids->filter(function ($v) use ($pid) {
@ -666,6 +660,7 @@ class PublicApiController extends Controller
$domain = parse_url($res['url'], PHP_URL_HOST);
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
}
return response()->json($res);
}
@ -678,7 +673,7 @@ class PublicApiController extends Controller
'max_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,
'limit' => 'nullable|integer|min:1|max:24'
'limit' => 'nullable|integer|min:1|max:24',
]);
$user = $request->user();
@ -707,17 +702,19 @@ class PublicApiController extends Controller
$pid = $user->profile_id;
$following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function () use ($pid) {
$following = Follower::whereProfileId($pid)->pluck('following_id');
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 {
if ($user) {
$pid = $user->profile_id;
$following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function () use ($pid) {
$following = Follower::whereProfileId($pid)->pluck('following_id');
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 {
$visibility = ['public', 'unlisted'];
}
@ -742,6 +739,7 @@ class PublicApiController extends Controller
if ($user && $status) {
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
}
return $status;
})
->filter(function ($s) use ($onlyMedia) {
@ -754,6 +752,7 @@ class PublicApiController extends Controller
return false;
}
}
return $s;
})
->values();