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
{ {
@ -56,7 +37,7 @@ class PublicApiController extends Controller
protected function getUserData($user) protected function getUserData($user)
{ {
if(!$user) { if (! $user) {
return []; return [];
} else { } else {
return AccountService::get($user->profile_id); return AccountService::get($user->profile_id);
@ -65,18 +46,18 @@ class PublicApiController extends Controller
public function getStatus(Request $request, $id) public function getStatus(Request $request, $id)
{ {
abort_if(!$request->user(), 403); abort_if(! $request->user(), 403);
$status = StatusService::get($id, false); $status = StatusService::get($id, false);
abort_if(!$status, 404); abort_if(! $status, 404);
if(in_array($status['visibility'], ['public', 'unlisted'])) { if (in_array($status['visibility'], ['public', 'unlisted'])) {
return $status; return $status;
} }
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
if($status['account']['id'] == $pid) { if ($status['account']['id'] == $pid) {
return $status; return $status;
} }
if($status['visibility'] == 'private') { if ($status['visibility'] == 'private') {
if(FollowerService::follows($pid, $status['account']['id'])) { if (FollowerService::follows($pid, $status['account']['id'])) {
return $status; return $status;
} }
} }
@ -88,9 +69,9 @@ class PublicApiController extends Controller
$profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail(); $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
$status = Status::whereProfileId($profile->id)->findOrFail($postid); $status = Status::whereProfileId($profile->id)->findOrFail($postid);
$this->scopeCheck($profile, $status); $this->scopeCheck($profile, $status);
if(!$request->user()) { if (! $request->user()) {
$cached = StatusService::get($status->id, false); $cached = StatusService::get($status->id, false);
abort_if(!in_array($cached['visibility'], ['public', 'unlisted']), 403); abort_if(! in_array($cached['visibility'], ['public', 'unlisted']), 403);
$res = ['status' => $cached]; $res = ['status' => $cached];
} else { } else {
$item = new Fractal\Resource\Item($status, new StatusStatelessTransformer()); $item = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
@ -107,7 +88,7 @@ class PublicApiController extends Controller
$profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail(); $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
$status = Status::whereProfileId($profile->id)->findOrFail($postid); $status = Status::whereProfileId($profile->id)->findOrFail($postid);
$this->scopeCheck($profile, $status); $this->scopeCheck($profile, $status);
if(!Auth::check()) { if (! Auth::check()) {
$res = [ $res = [
'user' => [], 'user' => [],
'likes' => [], 'likes' => [],
@ -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;
@ -146,15 +129,15 @@ class PublicApiController extends Controller
$status = Status::whereProfileId($profile->id)->whereCommentsDisabled(false)->findOrFail($postId); $status = Status::whereProfileId($profile->id)->whereCommentsDisabled(false)->findOrFail($postId);
$this->scopeCheck($profile, $status); $this->scopeCheck($profile, $status);
if(Auth::check()) { if (Auth::check()) {
$p = Auth::user()->profile; $p = Auth::user()->profile;
$scope = $p->id == $status->profile_id || FollowerService::follows($p->id, $profile->id) ? ['public', 'private', 'unlisted'] : ['public','unlisted']; $scope = $p->id == $status->profile_id || FollowerService::follows($p->id, $profile->id) ? ['public', 'private', 'unlisted'] : ['public', 'unlisted'];
} else { } else {
$scope = ['public', 'unlisted']; $scope = ['public', 'unlisted'];
} }
if($request->filled('min_id') || $request->filled('max_id')) { if ($request->filled('min_id') || $request->filled('max_id')) {
if($request->filled('min_id')) { if ($request->filled('min_id')) {
$replies = $status->comments() $replies = $status->comments()
->whereNull('reblog_of_id') ->whereNull('reblog_of_id')
->whereIn('scope', $scope) ->whereIn('scope', $scope)
@ -163,7 +146,7 @@ class PublicApiController extends Controller
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->paginate($limit); ->paginate($limit);
} }
if($request->filled('max_id')) { if ($request->filled('max_id')) {
$replies = $status->comments() $replies = $status->comments()
->whereNull('reblog_of_id') ->whereNull('reblog_of_id')
->whereIn('scope', $scope) ->whereIn('scope', $scope)
@ -184,12 +167,13 @@ 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);
} }
protected function scopeCheck(Profile $profile, Status $status) protected function scopeCheck(Profile $profile, Status $status)
{ {
if($profile->is_private == true && Auth::check() == false) { if ($profile->is_private == true && Auth::check() == false) {
abort(404); abort(404);
} }
@ -199,11 +183,11 @@ class PublicApiController extends Controller
break; break;
case 'private': case 'private':
$user = Auth::check() ? Auth::user() : false; $user = Auth::check() ? Auth::user() : false;
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);
} }
} }
@ -225,14 +209,14 @@ class PublicApiController extends Controller
public function publicTimelineApi(Request $request) public function publicTimelineApi(Request $request)
{ {
$this->validate($request,[ $this->validate($request, [
'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()) {
return response('', 403); return response('', 403);
} }
@ -244,8 +228,8 @@ class PublicApiController extends Controller
$filtered = $user ? UserFilterService::filters($user->profile_id) : []; $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
$hideNsfw = config('instance.hide_nsfw_on_public_feeds'); $hideNsfw = config('instance.hide_nsfw_on_public_feeds');
if(config('exp.cached_public_timeline') == false) { if (config('exp.cached_public_timeline') == false) {
if($min || $max) { if ($min || $max) {
$dir = $min ? '>' : '<'; $dir = $min ? '>' : '<';
$id = $min ?? $max; $id = $min ?? $max;
$timeline = Status::select( $timeline = Status::select(
@ -259,24 +243,25 @@ class PublicApiController extends Controller
->whereNull(['in_reply_to_id', 'reblog_of_id']) ->whereNull(['in_reply_to_id', 'reblog_of_id'])
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album']) ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->whereLocal(true) ->whereLocal(true)
->when($hideNsfw, function($q, $hideNsfw) { ->when($hideNsfw, function ($q, $hideNsfw) {
return $q->where('is_nsfw', false); return $q->where('is_nsfw', false);
}) })
->whereScope('public') ->whereScope('public')
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->limit($limit) ->limit($limit)
->get() ->get()
->map(function($s) use ($user) { ->map(function ($s) use ($user) {
$status = StatusService::getFull($s->id, $user->profile_id); $status = StatusService::getFull($s->id, $user->profile_id);
if(!$status) { if (! $status) {
return false; return false;
} }
$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) {
return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false; return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
}) })
->values(); ->values();
@ -305,24 +290,25 @@ class PublicApiController extends Controller
->whereNull(['in_reply_to_id', 'reblog_of_id']) ->whereNull(['in_reply_to_id', 'reblog_of_id'])
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album']) ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->whereLocal(true) ->whereLocal(true)
->when($hideNsfw, function($q, $hideNsfw) { ->when($hideNsfw, function ($q, $hideNsfw) {
return $q->where('is_nsfw', false); return $q->where('is_nsfw', false);
}) })
->whereScope('public') ->whereScope('public')
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->limit($limit) ->limit($limit)
->get() ->get()
->map(function($s) use ($user) { ->map(function ($s) use ($user) {
$status = StatusService::getFull($s->id, $user->profile_id); $status = StatusService::getFull($s->id, $user->profile_id);
if(!$status) { if (! $status) {
return false; return false;
} }
$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) {
return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false; return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
}) })
->values(); ->values();
@ -330,15 +316,15 @@ class PublicApiController extends Controller
$res = $timeline->toArray(); $res = $timeline->toArray();
} }
} else { } else {
Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() { Cache::remember('api:v1:timelines:public:cache_check', 10368000, function () {
if(PublicTimelineService::count() == 0) { if (PublicTimelineService::count() == 0) {
PublicTimelineService::warmCache(true, 400); PublicTimelineService::warmCache(true, 400);
} }
}); });
if ($max) { if ($max) {
$feed = PublicTimelineService::getRankedMaxId($max, $limit); $feed = PublicTimelineService::getRankedMaxId($max, $limit);
} else if ($min) { } elseif ($min) {
$feed = PublicTimelineService::getRankedMinId($min, $limit); $feed = PublicTimelineService::getRankedMinId($min, $limit);
} else { } else {
$feed = PublicTimelineService::get(0, $limit); $feed = PublicTimelineService::get(0, $limit);
@ -346,17 +332,18 @@ class PublicApiController extends Controller
$res = collect($feed) $res = collect($feed)
->take($limit) ->take($limit)
->map(function($k) use($user) { ->map(function ($k) use ($user) {
$status = StatusService::get($k); $status = StatusService::get($k);
if($status && isset($status['account']) && $user) { if ($status && isset($status['account']) && $user) {
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $k); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $k); $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $k);
$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) {
return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false; return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
}) })
->values() ->values()
@ -368,17 +355,17 @@ class PublicApiController extends Controller
public function homeTimelineApi(Request $request) public function homeTimelineApi(Request $request)
{ {
if(!$request->user()) { if (! $request->user()) {
return response('', 403); return response('', 403);
} }
$this->validate($request,[ $this->validate($request, [
'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: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';
@ -390,7 +377,7 @@ class PublicApiController extends Controller
$user = $request->user(); $user = $request->user();
$key = 'user:last_active_at:id:'.$user->id; $key = 'user:last_active_at:id:'.$user->id;
if(Cache::get($key) == null) { if (Cache::get($key) == null) {
$user->last_active_at = now(); $user->last_active_at = now();
$user->save(); $user->save();
Cache::put($key, true, 43200); Cache::put($key, true, 43200);
@ -398,8 +385,9 @@ class PublicApiController extends Controller
$pid = $user->profile_id; $pid = $user->profile_id;
$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();
}); });
@ -409,9 +397,10 @@ class PublicApiController extends Controller
$textOnlyReplies = false; $textOnlyReplies = false;
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',
@ -433,30 +422,31 @@ class PublicApiController extends Controller
'updated_at' 'updated_at'
) )
->whereIn('type', $types) ->whereIn('type', $types)
->when(!$textOnlyReplies, function($q, $textOnlyReplies) { ->when(! $textOnlyReplies, function ($q, $textOnlyReplies) {
return $q->whereNull('in_reply_to_id'); return $q->whereNull('in_reply_to_id');
}) })
->where('id', $dir, $id) ->where('id', $dir, $id)
->whereIn('profile_id', $following) ->whereIn('profile_id', $following)
->whereIn('visibility',['public', 'unlisted', 'private']) ->whereIn('visibility', ['public', 'unlisted', 'private'])
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
->limit($limit) ->limit($limit)
->get() ->get()
->map(function($s) use ($user) { ->map(function ($s) use ($user) {
try { try {
$status = StatusService::get($s->id, false); $status = StatusService::get($s->id, false);
if(!$status) { if (! $status) {
return false; return false;
} }
} catch(\Exception $e) { } catch (\Exception $e) {
return false; return false;
} }
$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) {
return $s && in_array($s['account']['id'], $filtered) == false; return $s && in_array($s['account']['id'], $filtered) == false;
}) })
->values() ->values()
@ -483,29 +473,30 @@ class PublicApiController extends Controller
'updated_at' 'updated_at'
) )
->whereIn('type', $types) ->whereIn('type', $types)
->when(!$textOnlyReplies, function($q, $textOnlyReplies) { ->when(! $textOnlyReplies, function ($q, $textOnlyReplies) {
return $q->whereNull('in_reply_to_id'); return $q->whereNull('in_reply_to_id');
}) })
->whereIn('profile_id', $following) ->whereIn('profile_id', $following)
->whereIn('visibility',['public', 'unlisted', 'private']) ->whereIn('visibility', ['public', 'unlisted', 'private'])
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
->limit($limit) ->limit($limit)
->get() ->get()
->map(function($s) use ($user) { ->map(function ($s) use ($user) {
try { try {
$status = StatusService::get($s->id, false); $status = StatusService::get($s->id, false);
if(!$status) { if (! $status) {
return false; return false;
} }
} catch(\Exception $e) { } catch (\Exception $e) {
return false; return false;
} }
$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) {
return $s && in_array($s['account']['id'], $filtered) == false; return $s && in_array($s['account']['id'], $filtered) == false;
}) })
->values() ->values()
@ -515,17 +506,17 @@ class PublicApiController extends Controller
public function networkTimelineApi(Request $request) public function networkTimelineApi(Request $request)
{ {
if(!$request->user()) { if (! $request->user()) {
return response('', 403); return response('', 403);
} }
abort_if(config('federation.network_timeline') == false, 404); abort_if(config('federation.network_timeline') == false, 404);
$this->validate($request,[ $this->validate($request, [
'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');
@ -538,8 +529,8 @@ class PublicApiController extends Controller
$filtered = $user ? UserFilterService::filters($user->profile_id) : []; $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
$hideNsfw = config('instance.hide_nsfw_on_public_feeds'); $hideNsfw = config('instance.hide_nsfw_on_public_feeds');
if(config('instance.timeline.network.cached') == false) { if (config('instance.timeline.network.cached') == false) {
if($min || $max) { if ($min || $max) {
$dir = $min ? '>' : '<'; $dir = $min ? '>' : '<';
$id = $min ?? $max; $id = $min ?? $max;
$timeline = Status::select( $timeline = Status::select(
@ -550,7 +541,7 @@ class PublicApiController extends Controller
'created_at', 'created_at',
) )
->where('id', $dir, $id) ->where('id', $dir, $id)
->when($hideNsfw, function($q, $hideNsfw) { ->when($hideNsfw, function ($q, $hideNsfw) {
return $q->where('is_nsfw', false); return $q->where('is_nsfw', false);
}) })
->whereNull(['in_reply_to_id', 'reblog_of_id']) ->whereNull(['in_reply_to_id', 'reblog_of_id'])
@ -562,11 +553,12 @@ class PublicApiController extends Controller
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
->limit($limit) ->limit($limit)
->get() ->get()
->map(function($s) use ($user) { ->map(function ($s) use ($user) {
$status = StatusService::get($s->id); $status = StatusService::get($s->id);
$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();
@ -580,7 +572,7 @@ class PublicApiController extends Controller
) )
->whereNull(['in_reply_to_id', 'reblog_of_id']) ->whereNull(['in_reply_to_id', 'reblog_of_id'])
->whereNotIn('profile_id', $filtered) ->whereNotIn('profile_id', $filtered)
->when($hideNsfw, function($q, $hideNsfw) { ->when($hideNsfw, function ($q, $hideNsfw) {
return $q->where('is_nsfw', false); return $q->where('is_nsfw', false);
}) })
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album']) ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
@ -590,25 +582,26 @@ class PublicApiController extends Controller
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
->limit($limit) ->limit($limit)
->get() ->get()
->map(function($s) use ($user) { ->map(function ($s) use ($user) {
$status = StatusService::get($s->id); $status = StatusService::get($s->id);
$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();
} }
} else { } else {
Cache::remember('api:v1:timelines:network:cache_check', 10368000, function() { Cache::remember('api:v1:timelines:network:cache_check', 10368000, function () {
if(NetworkTimelineService::count() == 0) { if (NetworkTimelineService::count() == 0) {
NetworkTimelineService::warmCache(true, 400); NetworkTimelineService::warmCache(true, 400);
} }
}); });
if ($max) { if ($max) {
$feed = NetworkTimelineService::getRankedMaxId($max, $limit); $feed = NetworkTimelineService::getRankedMaxId($max, $limit);
} else if ($min) { } elseif ($min) {
$feed = NetworkTimelineService::getRankedMinId($min, $limit); $feed = NetworkTimelineService::getRankedMinId($min, $limit);
} else { } else {
$feed = NetworkTimelineService::get(0, $limit); $feed = NetworkTimelineService::get(0, $limit);
@ -616,17 +609,18 @@ class PublicApiController extends Controller
$res = collect($feed) $res = collect($feed)
->take($limit) ->take($limit)
->map(function($k) use($user) { ->map(function ($k) use ($user) {
$status = StatusService::get($k); $status = StatusService::get($k);
if($status && isset($status['account']) && $user) { if ($status && isset($status['account']) && $user) {
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $k); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $k); $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $k);
$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) {
return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false; return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
}) })
->values() ->values()
@ -638,7 +632,7 @@ class PublicApiController extends Controller
public function relationships(Request $request) public function relationships(Request $request)
{ {
if(!Auth::check()) { if (! Auth::check()) {
return response()->json([]); return response()->json([]);
} }
@ -646,13 +640,13 @@ 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) {
return $v != $pid; return $v != $pid;
}) })
->map(function($id) use($pid) { ->map(function ($id) use ($pid) {
return RelationshipService::get($pid, $id); return RelationshipService::get($pid, $id);
}); });
@ -662,10 +656,11 @@ class PublicApiController extends Controller
public function account(Request $request, $id) public function account(Request $request, $id)
{ {
$res = AccountService::get($id); $res = AccountService::get($id);
if($res && isset($res['local'], $res['url']) && !$res['local']) { if ($res && isset($res['local'], $res['url']) && ! $res['local']) {
$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);
} }
@ -675,17 +670,17 @@ class PublicApiController extends Controller
'only_media' => 'nullable', 'only_media' => 'nullable',
'pinned' => 'nullable', 'pinned' => 'nullable',
'exclude_replies' => 'nullable', 'exclude_replies' => 'nullable',
'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();
$profile = AccountService::get($id); $profile = AccountService::get($id);
abort_if(!$profile, 404); abort_if(! $profile, 404);
if($profile && isset($profile['local'], $profile['url']) && !$profile['local']) { if ($profile && isset($profile['local'], $profile['url']) && ! $profile['local']) {
$domain = parse_url($profile['url'], PHP_URL_HOST); $domain = parse_url($profile['url'], PHP_URL_HOST);
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404); abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
} }
@ -696,28 +691,30 @@ class PublicApiController extends Controller
$scope = ['photo', 'photo:album', 'video', 'video:album']; $scope = ['photo', 'photo:album', 'video', 'video:album'];
$onlyMedia = $request->input('only_media', true); $onlyMedia = $request->input('only_media', true);
if(!$min_id && !$max_id) { if (! $min_id && ! $max_id) {
$min_id = 1; $min_id = 1;
} }
if($profile['locked']) { if ($profile['locked']) {
if(!$user) { if (! $user) {
return response()->json([]); return response()->json([]);
} }
$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'];
} }
@ -733,27 +730,29 @@ class PublicApiController extends Controller
->limit($limit) ->limit($limit)
->orderByDesc('id') ->orderByDesc('id')
->get() ->get()
->map(function($s) use($user) { ->map(function ($s) use ($user) {
try { try {
$status = StatusService::get($s->id, false); $status = StatusService::get($s->id, false);
} catch (\Exception $e) { } catch (\Exception $e) {
$status = false; $status = false;
} }
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) {
if($onlyMedia) { if ($onlyMedia) {
if( if (
!isset($s['media_attachments']) || ! isset($s['media_attachments']) ||
!is_array($s['media_attachments']) || ! is_array($s['media_attachments']) ||
empty($s['media_attachments']) empty($s['media_attachments'])
) { ) {
return false; return false;
} }
} }
return $s; return $s;
}) })
->values(); ->values();