1
0
Fork 0
pixelfed/app/Http/Controllers/PublicApiController.php

511 lines
19 KiB
PHP
Raw Normal View History

2018-11-09 05:23:36 +00:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\{
Hashtag,
2018-11-29 08:32:27 +00:00
Follower,
2018-11-09 05:23:36 +00:00
Like,
Media,
Notification,
Profile,
StatusHashtag,
Status,
2018-12-09 23:03:53 +00:00
UserFilter
2018-11-09 05:23:36 +00:00
};
use Auth,Cache;
use Carbon\Carbon;
use League\Fractal;
use App\Transformer\Api\{
AccountTransformer,
2019-02-10 20:42:26 +00:00
RelationshipTransformer,
2018-11-09 05:23:36 +00:00
StatusTransformer,
};
use App\Jobs\StatusPipeline\NewStatusPipeline;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
class PublicApiController extends Controller
{
protected $fractal;
public function __construct()
{
$this->fractal = new Fractal\Manager();
$this->fractal->setSerializer(new ArraySerializer());
}
protected function getUserData()
{
if(false == Auth::check()) {
return [];
} else {
$profile = Auth::user()->profile;
2018-12-24 04:50:35 +00:00
if($profile->status) {
return [];
}
2018-11-09 05:23:36 +00:00
$user = new Fractal\Resource\Item($profile, new AccountTransformer());
return $this->fractal->createData($user)->toArray();
}
}
2018-12-02 04:01:15 +00:00
protected function getLikes($status)
{
if(false == Auth::check()) {
return [];
} else {
$profile = Auth::user()->profile;
2018-12-24 04:50:35 +00:00
if($profile->status) {
return [];
}
2018-12-02 04:01:15 +00:00
$likes = $status->likedBy()->orderBy('created_at','desc')->paginate(10);
$collection = new Fractal\Resource\Collection($likes, new AccountTransformer());
return $this->fractal->createData($collection)->toArray();
}
}
protected function getShares($status)
{
if(false == Auth::check()) {
return [];
} else {
$profile = Auth::user()->profile;
2018-12-25 06:52:42 +00:00
if($profile->status) {
return [];
}
2018-12-02 04:01:15 +00:00
$shares = $status->sharedBy()->orderBy('created_at','desc')->paginate(10);
$collection = new Fractal\Resource\Collection($shares, new AccountTransformer());
return $this->fractal->createData($collection)->toArray();
}
}
2018-11-09 05:23:36 +00:00
public function status(Request $request, $username, int $postid)
{
2018-12-24 04:50:35 +00:00
$profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
$status = Status::whereProfileId($profile->id)->findOrFail($postid);
2018-11-29 08:32:27 +00:00
$this->scopeCheck($profile, $status);
2018-11-17 19:58:58 +00:00
$item = new Fractal\Resource\Item($status, new StatusTransformer());
2018-11-09 05:23:36 +00:00
$res = [
2018-11-17 19:58:58 +00:00
'status' => $this->fractal->createData($item)->toArray(),
'user' => $this->getUserData(),
2018-12-02 04:01:15 +00:00
'likes' => $this->getLikes($status),
'shares' => $this->getShares($status),
2018-11-17 19:58:58 +00:00
'reactions' => [
'liked' => $status->liked(),
'shared' => $status->shared(),
'bookmarked' => $status->bookmarked(),
],
2018-11-09 05:23:36 +00:00
];
return response()->json($res, 200, [], JSON_PRETTY_PRINT);
}
public function statusComments(Request $request, $username, int $postId)
{
$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 = $request->limit ?? 10;
2018-12-24 04:50:35 +00:00
$profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
$status = Status::whereProfileId($profile->id)->findOrFail($postId);
2018-11-29 08:32:27 +00:00
$this->scopeCheck($profile, $status);
2018-11-09 05:23:36 +00:00
if($request->filled('min_id') || $request->filled('max_id')) {
if($request->filled('min_id')) {
$replies = $status->comments()
2018-12-25 06:52:42 +00:00
->whereNull('reblog_of_id')
2018-11-09 05:23:36 +00:00
->select('id', 'caption', 'rendered', 'profile_id', 'in_reply_to_id', 'created_at')
->where('id', '>=', $request->min_id)
->orderBy('id', 'desc')
->paginate($limit);
}
if($request->filled('max_id')) {
$replies = $status->comments()
2018-12-25 06:52:42 +00:00
->whereNull('reblog_of_id')
2018-11-09 05:23:36 +00:00
->select('id', 'caption', 'rendered', 'profile_id', 'in_reply_to_id', 'created_at')
->where('id', '<=', $request->max_id)
->orderBy('id', 'desc')
->paginate($limit);
}
} else {
$replies = $status->comments()
2018-12-25 06:55:11 +00:00
->whereNull('reblog_of_id')
2018-11-09 05:23:36 +00:00
->select('id', 'caption', 'rendered', 'profile_id', 'in_reply_to_id', 'created_at')
->orderBy('id', 'desc')
->paginate($limit);
}
$resource = new Fractal\Resource\Collection($replies, new StatusTransformer(), 'data');
$resource->setPaginator(new IlluminatePaginatorAdapter($replies));
$res = $this->fractal->createData($resource)->toArray();
return response()->json($res, 200, [], JSON_PRETTY_PRINT);
}
2018-11-29 08:32:27 +00:00
2018-12-02 04:01:15 +00:00
public function statusLikes(Request $request, $username, $id)
{
2018-12-24 04:50:35 +00:00
$profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
$status = Status::whereProfileId($profile->id)->findOrFail($id);
2018-12-02 04:01:15 +00:00
$this->scopeCheck($profile, $status);
$likes = $this->getLikes($status);
return response()->json([
'data' => $likes
]);
}
public function statusShares(Request $request, $username, $id)
{
2018-12-24 04:50:35 +00:00
$profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
$status = Status::whereProfileId($profile->id)->findOrFail($id);
2018-12-02 04:01:15 +00:00
$this->scopeCheck($profile, $status);
$shares = $this->getShares($status);
return response()->json([
'data' => $shares
]);
}
2018-11-29 08:32:27 +00:00
protected function scopeCheck(Profile $profile, Status $status)
{
if($profile->is_private == true && Auth::check() == false) {
abort(404);
}
switch ($status->scope) {
case 'public':
case 'unlisted':
2019-02-15 19:58:27 +00:00
break;
2018-12-08 12:43:17 +00:00
case 'private':
2018-11-29 08:32:27 +00:00
$user = Auth::check() ? Auth::user() : false;
2019-02-15 19:58:27 +00:00
if(!$user) {
abort(403);
} else {
$follows = $profile->followedBy(Auth::user()->profile);
2018-11-29 08:32:27 +00:00
if($follows == false && $profile->id !== $user->profile->id) {
abort(404);
}
}
break;
case 'direct':
abort(404);
break;
case 'draft':
abort(404);
break;
default:
abort(404);
break;
}
}
2018-12-09 23:03:53 +00:00
public function publicTimelineApi(Request $request)
{
if(!Auth::check()) {
return abort(403);
}
$this->validate($request,[
'page' => 'nullable|integer|max:40',
'min_id' => 'nullable|integer',
'max_id' => 'nullable|integer',
'limit' => 'nullable|integer|max:20'
]);
$page = $request->input('page');
$min = $request->input('min_id');
$max = $request->input('max_id');
2019-02-25 07:11:52 +00:00
$limit = $request->input('limit') ?? 3;
2018-12-09 23:03:53 +00:00
// TODO: Use redis for timelines
// $timeline = Timeline::build()->local();
$pid = Auth::user()->profile->id;
2019-02-25 18:56:24 +00:00
$private = Cache::remember('profiles:private', now()->addMinutes(1440), function() {
2019-02-25 06:22:06 +00:00
return Profile::whereIsPrivate(true)
->orWhere('unlisted', true)
->orWhere('status', '!=', null)
->pluck('id');
});
2018-12-09 23:03:53 +00:00
$filters = UserFilter::whereUserId($pid)
->whereFilterableType('App\Profile')
->whereIn('filter_type', ['mute', 'block'])
->pluck('filterable_id')->toArray();
$filtered = array_merge($private->toArray(), $filters);
if($min || $max) {
$dir = $min ? '>' : '<';
$id = $min ?? $max;
2019-02-14 01:16:58 +00:00
$timeline = Status::select(
'id',
'uri',
'caption',
'rendered',
'profile_id',
'type',
'in_reply_to_id',
'reblog_of_id',
'is_nsfw',
'scope',
'local',
'created_at',
'updated_at'
2019-02-25 06:50:39 +00:00
)->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
2019-01-10 19:25:25 +00:00
->whereLocal(true)
->whereNull('uri')
2018-12-09 23:03:53 +00:00
->where('id', $dir, $id)
->whereNotIn('profile_id', $filtered)
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->whereVisibility('public')
->orderBy('created_at', 'desc')
->limit($limit)
->get();
} else {
2019-02-25 06:28:35 +00:00
$timeline = Status::select(
'id',
'uri',
'caption',
'rendered',
'profile_id',
'type',
'in_reply_to_id',
'reblog_of_id',
'is_nsfw',
'scope',
'local',
'created_at',
'updated_at'
2019-02-25 06:50:39 +00:00
)->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
2019-01-10 19:25:25 +00:00
->whereLocal(true)
->whereNull('uri')
2018-12-09 23:03:53 +00:00
->whereNotIn('profile_id', $filtered)
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->whereVisibility('public')
->orderBy('created_at', 'desc')
->simplePaginate($limit);
}
$fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
$res = $this->fractal->createData($fractal)->toArray();
return response()->json($res);
}
public function homeTimelineApi(Request $request)
{
if(!Auth::check()) {
return abort(403);
}
$this->validate($request,[
'page' => 'nullable|integer|max:40',
'min_id' => 'nullable|integer',
'max_id' => 'nullable|integer',
'limit' => 'nullable|integer|max:20'
]);
$page = $request->input('page');
$min = $request->input('min_id');
$max = $request->input('max_id');
2019-02-25 07:11:52 +00:00
$limit = $request->input('limit') ?? 3;
2018-12-09 23:03:53 +00:00
// TODO: Use redis for timelines
// $timeline = Timeline::build()->local();
$pid = Auth::user()->profile->id;
2019-02-25 18:56:24 +00:00
$following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
2019-02-25 06:02:11 +00:00
$following = Follower::whereProfileId($pid)->pluck('following_id');
return $following->push($pid)->toArray();
});
2018-12-09 23:03:53 +00:00
2019-02-25 06:22:06 +00:00
$private = Cache::remember('profiles:private', 1440, function() {
return Profile::whereIsPrivate(true)
->orWhere('unlisted', true)
->orWhere('status', '!=', null)
->pluck('id');
});
2018-12-09 23:03:53 +00:00
$filters = UserFilter::whereUserId($pid)
->whereFilterableType('App\Profile')
->whereIn('filter_type', ['mute', 'block'])
->pluck('filterable_id')->toArray();
$filtered = array_merge($private->toArray(), $filters);
if($min || $max) {
$dir = $min ? '>' : '<';
$id = $min ?? $max;
2019-02-14 01:16:58 +00:00
$timeline = Status::select(
'id',
'uri',
'caption',
'rendered',
'profile_id',
'type',
'in_reply_to_id',
'reblog_of_id',
'is_nsfw',
'scope',
'local',
'created_at',
'updated_at'
2019-02-25 06:50:39 +00:00
)->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
2019-01-10 19:25:25 +00:00
->whereLocal(true)
->whereNull('uri')
2018-12-09 23:03:53 +00:00
->where('id', $dir, $id)
->whereIn('profile_id', $following)
->whereNotIn('profile_id', $filtered)
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
2018-12-27 08:02:08 +00:00
->whereIn('visibility',['public', 'unlisted', 'private'])
2018-12-09 23:03:53 +00:00
->orderBy('created_at', 'desc')
->limit($limit)
->get();
} else {
2019-02-25 06:28:35 +00:00
$timeline = Status::select(
'id',
'uri',
'caption',
'rendered',
'profile_id',
'type',
'in_reply_to_id',
'reblog_of_id',
'is_nsfw',
'scope',
'local',
'created_at',
'updated_at'
2019-02-25 06:50:39 +00:00
)->whereIn('type', ['photo', 'photo:album', 'video', 'video:album'])
2019-01-10 19:25:25 +00:00
->whereLocal(true)
->whereNull('uri')
2018-12-09 23:03:53 +00:00
->whereIn('profile_id', $following)
->whereNotIn('profile_id', $filtered)
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
2018-12-27 08:02:08 +00:00
->whereIn('visibility',['public', 'unlisted', 'private'])
2018-12-09 23:03:53 +00:00
->orderBy('created_at', 'desc')
->simplePaginate($limit);
}
$fractal = new Fractal\Resource\Collection($timeline, new StatusTransformer());
$res = $this->fractal->createData($fractal)->toArray();
return response()->json($res);
}
2019-02-10 20:42:26 +00:00
public function relationships(Request $request)
{
abort_if(!Auth::check(), 403);
$this->validate($request, [
2019-02-11 04:49:54 +00:00
'id' => 'required|array|min:1|max:20',
'id.*' => 'required|integer'
2019-02-10 20:42:26 +00:00
]);
$ids = collect($request->input('id'));
$filtered = $ids->filter(function($v) {
return $v != Auth::user()->profile->id;
});
$relations = Profile::findOrFail($filtered->all());
$fractal = new Fractal\Resource\Collection($relations, new RelationshipTransformer());
$res = $this->fractal->createData($fractal)->toArray();
return response()->json($res);
}
2019-02-11 04:49:54 +00:00
public function account(Request $request, $id)
{
$profile = Profile::whereNull('status')->findOrFail($id);
$resource = new Fractal\Resource\Item($profile, new AccountTransformer());
$res = $this->fractal->createData($resource)->toArray();
return response()->json($res);
}
public function accountFollowers(Request $request, $id)
{
2019-02-25 04:30:02 +00:00
abort_unless(Auth::check(), 403);
$profile = Profile::with('user')->whereNull('status')->whereNull('domain')->findOrFail($id);
if($profile->is_private || !$profile->user->settings->show_profile_followers) {
return [];
}
$followers = $profile->followers()->orderByDesc('followers.created_at')->paginate(10);
2019-02-11 04:49:54 +00:00
$resource = new Fractal\Resource\Collection($followers, new AccountTransformer());
$res = $this->fractal->createData($resource)->toArray();
return response()->json($res);
}
public function accountFollowing(Request $request, $id)
{
2019-02-25 04:30:02 +00:00
abort_unless(Auth::check(), 403);
$profile = Profile::with('user')->whereNull('status')->whereNull('domain')->findOrFail($id);
if($profile->is_private || !$profile->user->settings->show_profile_following) {
return [];
}
$following = $profile->following()->orderByDesc('followers.created_at')->paginate(10);
2019-02-11 04:49:54 +00:00
$resource = new Fractal\Resource\Collection($following, new AccountTransformer());
$res = $this->fractal->createData($resource)->toArray();
return response()->json($res);
}
public function accountStatuses(Request $request, $id)
{
$this->validate($request, [
'only_media' => 'nullable',
'pinned' => 'nullable',
'exclude_replies' => 'nullable',
'max_id' => 'nullable|integer|min:1',
'since_id' => 'nullable|integer|min:1',
'min_id' => 'nullable|integer|min:1',
'limit' => 'nullable|integer|min:1|max:24'
]);
$limit = $request->limit ?? 20;
$max_id = $request->max_id ?? false;
$min_id = $request->min_id ?? false;
$since_id = $request->since_id ?? false;
$only_media = $request->only_media ?? false;
$user = Auth::user();
$account = Profile::findOrFail($id);
2019-02-15 19:58:27 +00:00
$statuses = $account->statuses()
->getQuery()
->whereNull('uri');
if(!$user || $user->profile->id != $account->id && !$user->profile->follows($account)) {
$statuses = $statuses->whereVisibility('public');
} else {
$statuses = $statuses->whereIn('visibility', ['public', 'unlisted', 'private']);
}
2019-02-11 04:49:54 +00:00
if($only_media == true) {
$statuses = $statuses
->whereHas('media')
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id');
}
if($id == $account->id && !$max_id && !$min_id && !$since_id) {
$statuses = $statuses->orderBy('id', 'desc')
->paginate($limit);
} else if($since_id) {
$statuses = $statuses->where('id', '>', $since_id)
->orderBy('id', 'DESC')
->paginate($limit);
} else if($min_id) {
$statuses = $statuses->where('id', '>', $min_id)
->orderBy('id', 'ASC')
->paginate($limit);
} else if($max_id) {
$statuses = $statuses->where('id', '<', $max_id)
->orderBy('id', 'DESC')
->paginate($limit);
} else {
2019-02-15 19:58:27 +00:00
$statuses = $statuses->orderBy('id', 'desc')->paginate($limit);
2019-02-11 04:49:54 +00:00
}
$resource = new Fractal\Resource\Collection($statuses, new StatusTransformer());
$res = $this->fractal->createData($resource)->toArray();
return response()->json($res);
}
2019-02-25 04:30:02 +00:00
2018-11-09 05:23:36 +00:00
}