Update ApiV1Controller, add missing instance api attributes

This commit is contained in:
Daniel Supernault 2021-04-22 22:46:05 -06:00
parent 716247857c
commit 64b86546b9
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -54,7 +54,7 @@ use App\Services\{
MediaBlocklistService MediaBlocklistService
}; };
class ApiV1Controller extends Controller class ApiV1Controller extends Controller
{ {
protected $fractal; protected $fractal;
@ -98,7 +98,7 @@ class ApiV1Controller extends Controller
'client_secret' => $client->secret, 'client_secret' => $client->secret,
'vapid_key' => null 'vapid_key' => null
]; ];
return response()->json($res, 200, [ return response()->json($res, 200, [
'Access-Control-Allow-Origin' => '*' 'Access-Control-Allow-Origin' => '*'
]); ]);
@ -238,7 +238,7 @@ class ApiV1Controller extends Controller
abort_if(!$profile->followedBy($user->profile), 403); abort_if(!$profile->followedBy($user->profile), 403);
} }
$settings = $profile->user->settings; $settings = $profile->user->settings;
if( in_array($user->profile_id, $profile->blockedIds()->toArray()) || if( in_array($user->profile_id, $profile->blockedIds()->toArray()) ||
$settings->show_profile_followers == false $settings->show_profile_followers == false
) { ) {
$res = []; $res = [];
@ -279,7 +279,7 @@ class ApiV1Controller extends Controller
abort_if(!$profile->followedBy($user->profile), 403); abort_if(!$profile->followedBy($user->profile), 403);
} }
$settings = $profile->user->settings; $settings = $profile->user->settings;
if( in_array($user->profile_id, $profile->blockedIds()->toArray()) || if( in_array($user->profile_id, $profile->blockedIds()->toArray()) ||
$settings->show_profile_following == false $settings->show_profile_following == false
) { ) {
$res = []; $res = [];
@ -322,10 +322,10 @@ class ApiV1Controller extends Controller
$max_id = $request->max_id; $max_id = $request->max_id;
$min_id = $request->min_id; $min_id = $request->min_id;
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$scope = $request->only_media == true ? $scope = $request->only_media == true ?
['photo', 'photo:album', 'video', 'video:album'] : ['photo', 'photo:album', 'video', 'video:album'] :
['photo', 'photo:album', 'video', 'video:album', 'share', 'reply']; ['photo', 'photo:album', 'video', 'video:album', 'share', 'reply'];
if($pid == $profile->id) { if($pid == $profile->id) {
$visibility = ['public', 'unlisted', 'private']; $visibility = ['public', 'unlisted', 'private'];
} else if($profile->is_private) { } else if($profile->is_private) {
@ -346,11 +346,11 @@ class ApiV1Controller extends Controller
$dir = $min_id ? '>' : '<'; $dir = $min_id ? '>' : '<';
$id = $min_id ?? $max_id; $id = $min_id ?? $max_id;
$timeline = Status::select( $timeline = Status::select(
'id', 'id',
'uri', 'uri',
'caption', 'caption',
'rendered', 'rendered',
'profile_id', 'profile_id',
'type', 'type',
'in_reply_to_id', 'in_reply_to_id',
'reblog_of_id', 'reblog_of_id',
@ -371,11 +371,11 @@ class ApiV1Controller extends Controller
->get(); ->get();
} else { } else {
$timeline = Status::select( $timeline = Status::select(
'id', 'id',
'uri', 'uri',
'caption', 'caption',
'rendered', 'rendered',
'profile_id', 'profile_id',
'type', 'type',
'in_reply_to_id', 'in_reply_to_id',
'reblog_of_id', 'reblog_of_id',
@ -458,7 +458,7 @@ class ApiV1Controller extends Controller
]); ]);
if($remote == true && config('federation.activitypub.remoteFollow') == true) { if($remote == true && config('federation.activitypub.remoteFollow') == true) {
(new FollowerController())->sendFollow($user->profile, $target); (new FollowerController())->sendFollow($user->profile, $target);
} }
} else { } else {
$follower = new Follower(); $follower = new Follower();
$follower->profile_id = $user->profile_id; $follower->profile_id = $user->profile_id;
@ -467,9 +467,9 @@ class ApiV1Controller extends Controller
if($remote == true && config('federation.activitypub.remoteFollow') == true) { if($remote == true && config('federation.activitypub.remoteFollow') == true) {
(new FollowerController())->sendFollow($user->profile, $target); (new FollowerController())->sendFollow($user->profile, $target);
} }
FollowPipeline::dispatch($follower); FollowPipeline::dispatch($follower);
} }
Cache::forget('profile:following:'.$target->id); Cache::forget('profile:following:'.$target->id);
Cache::forget('profile:followers:'.$target->id); Cache::forget('profile:followers:'.$target->id);
@ -527,7 +527,7 @@ class ApiV1Controller extends Controller
FollowRequest::whereFollowerId($user->profile_id) FollowRequest::whereFollowerId($user->profile_id)
->whereFollowingId($target->id) ->whereFollowingId($target->id)
->delete(); ->delete();
Follower::whereProfileId($user->profile_id) Follower::whereProfileId($user->profile_id)
->whereFollowingId($target->id) ->whereFollowingId($target->id)
@ -535,7 +535,7 @@ class ApiV1Controller extends Controller
if($remote == true && config('federation.activitypub.remoteFollow') == true) { if($remote == true && config('federation.activitypub.remoteFollow') == true) {
(new FollowerController())->sendUndoFollow($user->profile, $target); (new FollowerController())->sendUndoFollow($user->profile, $target);
} }
Cache::forget('profile:following:'.$target->id); Cache::forget('profile:following:'.$target->id);
Cache::forget('profile:followers:'.$target->id); Cache::forget('profile:followers:'.$target->id);
@ -568,7 +568,7 @@ class ApiV1Controller extends Controller
]); ]);
$pid = $request->user()->profile_id ?? $request->user()->profile->id; $pid = $request->user()->profile_id ?? $request->user()->profile->id;
$ids = collect($request->input('id')); $ids = collect($request->input('id'));
$filtered = $ids->filter(function($v) use($pid) { $filtered = $ids->filter(function($v) use($pid) {
return $v != $pid; return $v != $pid;
}); });
$relations = Profile::whereNull('status')->findOrFail($filtered->values()); $relations = Profile::whereNull('status')->findOrFail($filtered->values());
@ -605,7 +605,7 @@ class ApiV1Controller extends Controller
->orWhere('name', 'like', $q) ->orWhere('name', 'like', $q)
->limit($limit) ->limit($limit)
->get(); ->get();
$resource = new Fractal\Resource\Collection($profiles, new AccountTransformer()); $resource = new Fractal\Resource\Collection($profiles, new AccountTransformer());
$res = $this->fractal->createData($resource)->toArray(); $res = $this->fractal->createData($resource)->toArray();
return response()->json($res); return response()->json($res);
@ -910,10 +910,10 @@ class ApiV1Controller extends Controller
public function accountFollowRequestAccept(Request $request, $id) public function accountFollowRequestAccept(Request $request, $id)
{ {
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
// todo // todo
return response()->json([]); return response()->json([]);
} }
/** /**
@ -926,10 +926,10 @@ class ApiV1Controller extends Controller
public function accountFollowRequestReject(Request $request, $id) public function accountFollowRequestReject(Request $request, $id)
{ {
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
// todo // todo
return response()->json([]); return response()->json([]);
} }
/** /**
@ -942,10 +942,10 @@ class ApiV1Controller extends Controller
public function accountSuggestions(Request $request) public function accountSuggestions(Request $request)
{ {
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
// todo // todo
return response()->json([]); return response()->json([]);
} }
/** /**
@ -958,8 +958,13 @@ class ApiV1Controller extends Controller
public function instance(Request $request) public function instance(Request $request)
{ {
$res = [ $res = [
'approval_required' => false,
'contact_account' => null,
'description' => 'Pixelfed - Photo sharing for everyone', 'description' => 'Pixelfed - Photo sharing for everyone',
'email' => config('instance.email'), 'email' => config('instance.email'),
'invites_enabled' => false,
'rules' => [],
'short_description' => 'Pixelfed - Photo sharing for everyone',
'languages' => ['en'], 'languages' => ['en'],
'max_toot_chars' => (int) config('pixelfed.max_caption_length'), 'max_toot_chars' => (int) config('pixelfed.max_caption_length'),
'registrations' => config('pixelfed.open_registration'), 'registrations' => config('pixelfed.open_registration'),
@ -969,8 +974,8 @@ class ApiV1Controller extends Controller
'domain_count' => 0 'domain_count' => 0
], ],
'thumbnail' => config('app.url') . '/img/pixelfed-icon-color.png', 'thumbnail' => config('app.url') . '/img/pixelfed-icon-color.png',
'title' => 'Pixelfed (' . config('pixelfed.domain.app') . ')', 'title' => config('app.name'),
'uri' => config('app.url'), 'uri' => config('pixelfed.domain.app'),
'urls' => [], 'urls' => [],
'version' => '2.7.2 (compatible; Pixelfed ' . config('pixelfed.version') . ')', 'version' => '2.7.2 (compatible; Pixelfed ' . config('pixelfed.version') . ')',
'environment' => [ 'environment' => [
@ -983,7 +988,7 @@ class ApiV1Controller extends Controller
] ]
]; ];
return response()->json($res, 200, [], JSON_PRETTY_PRINT); return response()->json($res);
} }
/** /**
@ -996,8 +1001,8 @@ class ApiV1Controller extends Controller
public function accountLists(Request $request) public function accountLists(Request $request)
{ {
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
return response()->json([]); return response()->json([]);
} }
/** /**
@ -1010,8 +1015,8 @@ class ApiV1Controller extends Controller
public function accountListsById(Request $request, $id) public function accountListsById(Request $request, $id)
{ {
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
return response()->json([]); return response()->json([]);
} }
/** /**
@ -1057,7 +1062,7 @@ class ApiV1Controller extends Controller
if(config('pixelfed.enforce_account_limit') == true) { if(config('pixelfed.enforce_account_limit') == true) {
$size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function() use($user) { $size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function() use($user) {
return Media::whereUserId($user->id)->sum('size') / 1000; return Media::whereUserId($user->id)->sum('size') / 1000;
}); });
$limit = (int) config('pixelfed.max_account_size'); $limit = (int) config('pixelfed.max_account_size');
if ($size >= $limit) { if ($size >= $limit) {
abort(403, 'Account size limit reached.'); abort(403, 'Account size limit reached.');
@ -1092,7 +1097,7 @@ class ApiV1Controller extends Controller
$media->filter_class = $filterClass; $media->filter_class = $filterClass;
$media->filter_name = $filterName; $media->filter_name = $filterName;
$media->save(); $media->save();
switch ($media->mime) { switch ($media->mime) {
case 'image/jpeg': case 'image/jpeg':
case 'image/png': case 'image/png':
@ -1137,7 +1142,7 @@ class ApiV1Controller extends Controller
$media->caption = $request->input('description'); $media->caption = $request->input('description');
$media->save(); $media->save();
$resource = new Fractal\Resource\Item($media, new MediaTransformer()); $resource = new Fractal\Resource\Item($media, new MediaTransformer());
$res = $this->fractal->createData($resource)->toArray(); $res = $this->fractal->createData($resource)->toArray();
$res['preview_url'] = url('/storage/no-preview.png'); $res['preview_url'] = url('/storage/no-preview.png');
@ -1343,7 +1348,7 @@ class ApiV1Controller extends Controller
$max = $request->input('max_id'); $max = $request->input('max_id');
$limit = $request->input('limit') ?? 3; $limit = $request->input('limit') ?? 3;
$user = $request->user(); $user = $request->user();
if($user->last_active_at) { if($user->last_active_at) {
$key = 'user:last_active_at:id:'.$user->id; $key = 'user:last_active_at:id:'.$user->id;
$ttl = now()->addMinutes(5); $ttl = now()->addMinutes(5);
@ -1365,11 +1370,11 @@ class ApiV1Controller extends Controller
$dir = $min ? '>' : '<'; $dir = $min ? '>' : '<';
$id = $min ?? $max; $id = $min ?? $max;
$timeline = Status::select( $timeline = Status::select(
'id', 'id',
'uri', 'uri',
'caption', 'caption',
'rendered', 'rendered',
'profile_id', 'profile_id',
'type', 'type',
'in_reply_to_id', 'in_reply_to_id',
'reblog_of_id', 'reblog_of_id',
@ -1393,11 +1398,11 @@ class ApiV1Controller extends Controller
->get(); ->get();
} else { } else {
$timeline = Status::select( $timeline = Status::select(
'id', 'id',
'uri', 'uri',
'caption', 'caption',
'rendered', 'rendered',
'profile_id', 'profile_id',
'type', 'type',
'in_reply_to_id', 'in_reply_to_id',
'reblog_of_id', 'reblog_of_id',
@ -1458,7 +1463,7 @@ class ApiV1Controller extends Controller
$max = $request->input('max_id'); $max = $request->input('max_id');
$limit = $request->input('limit') ?? 3; $limit = $request->input('limit') ?? 3;
$user = $request->user(); $user = $request->user();
if($user) { if($user) {
$key = 'user:last_active_at:id:'.$user->id; $key = 'user:last_active_at:id:'.$user->id;
$ttl = now()->addMinutes(5); $ttl = now()->addMinutes(5);
@ -1473,11 +1478,11 @@ class ApiV1Controller extends Controller
$dir = $min ? '>' : '<'; $dir = $min ? '>' : '<';
$id = $min ?? $max; $id = $min ?? $max;
$timeline = Status::select( $timeline = Status::select(
'id', 'id',
'uri', 'uri',
'caption', 'caption',
'rendered', 'rendered',
'profile_id', 'profile_id',
'type', 'type',
'in_reply_to_id', 'in_reply_to_id',
'reblog_of_id', 'reblog_of_id',
@ -1502,11 +1507,11 @@ class ApiV1Controller extends Controller
->get(); ->get();
} else { } else {
$timeline = Status::select( $timeline = Status::select(
'id', 'id',
'uri', 'uri',
'caption', 'caption',
'rendered', 'rendered',
'profile_id', 'profile_id',
'type', 'type',
'in_reply_to_id', 'in_reply_to_id',
'reblog_of_id', 'reblog_of_id',
@ -1733,7 +1738,7 @@ class ApiV1Controller extends Controller
public function statusCreate(Request $request) public function statusCreate(Request $request)
{ {
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
$this->validate($request, [ $this->validate($request, [
'status' => 'nullable|string', 'status' => 'nullable|string',
'in_reply_to_id' => 'nullable|integer', 'in_reply_to_id' => 'nullable|integer',
@ -1779,9 +1784,9 @@ class ApiV1Controller extends Controller
abort_if($limitReached == true, 429); abort_if($limitReached == true, 429);
$visibility = $profile->is_private ? 'private' : ( $visibility = $profile->is_private ? 'private' : (
$profile->unlisted == true && $profile->unlisted == true &&
$request->input('visibility', 'public') == 'public' ? $request->input('visibility', 'public') == 'public' ?
'unlisted' : 'unlisted' :
$request->input('visibility', 'public')); $request->input('visibility', 'public'));
if($user->last_active_at == null) { if($user->last_active_at == null) {
@ -1874,7 +1879,7 @@ class ApiV1Controller extends Controller
->findOrFail($id); ->findOrFail($id);
$resource = new Fractal\Resource\Item($status, new StatusTransformer()); $resource = new Fractal\Resource\Item($status, new StatusTransformer());
Cache::forget('profile:status_count:'.$status->profile_id); Cache::forget('profile:status_count:'.$status->profile_id);
StatusDelete::dispatch($status); StatusDelete::dispatch($status);
@ -1894,7 +1899,7 @@ class ApiV1Controller extends Controller
public function statusShare(Request $request, $id) public function statusShare(Request $request, $id)
{ {
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
$user = $request->user(); $user = $request->user();
$status = Status::findOrFail($id); $status = Status::findOrFail($id);
@ -1936,7 +1941,7 @@ class ApiV1Controller extends Controller
public function statusUnshare(Request $request, $id) public function statusUnshare(Request $request, $id)
{ {
abort_if(!$request->user(), 403); abort_if(!$request->user(), 403);
$user = $request->user(); $user = $request->user();
$status = Status::findOrFail($id); $status = Status::findOrFail($id);
@ -2100,4 +2105,4 @@ class ApiV1Controller extends Controller
return SearchApiV2Service::query($request); return SearchApiV2Service::query($request);
} }
} }