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,
|
2021-06-02 02:03:59 +00:00
|
|
|
StatusView,
|
2018-12-09 23:03:53 +00:00
|
|
|
UserFilter
|
2018-11-09 05:23:36 +00:00
|
|
|
};
|
2021-06-29 07:23:24 +00:00
|
|
|
use Auth, Cache;
|
2018-11-09 05:23:36 +00:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use League\Fractal;
|
|
|
|
use App\Transformer\Api\{
|
|
|
|
AccountTransformer,
|
2019-02-10 20:42:26 +00:00
|
|
|
RelationshipTransformer,
|
2020-12-10 04:40:24 +00:00
|
|
|
StatusTransformer,
|
|
|
|
StatusStatelessTransformer
|
2018-11-09 05:23:36 +00:00
|
|
|
};
|
2019-11-23 05:43:20 +00:00
|
|
|
use App\Services\{
|
|
|
|
AccountService,
|
|
|
|
PublicTimelineService,
|
2021-06-29 07:23:24 +00:00
|
|
|
StatusService,
|
|
|
|
SnowflakeService,
|
2019-11-23 05:43:20 +00:00
|
|
|
UserFilterService
|
|
|
|
};
|
2018-11-09 05:23:36 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2019-11-23 05:43:20 +00:00
|
|
|
protected function getUserData($user)
|
2018-11-09 05:23:36 +00:00
|
|
|
{
|
2019-11-23 05:43:20 +00:00
|
|
|
if(!$user) {
|
2018-11-09 05:23:36 +00:00
|
|
|
return [];
|
|
|
|
} else {
|
2019-11-23 05:43:20 +00:00
|
|
|
return AccountService::get($user->profile_id);
|
2018-11-09 05:23:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2020-12-10 04:40:24 +00:00
|
|
|
if(!Auth::check()) {
|
|
|
|
$res = Cache::remember('wapi:v1:status:stateless_byid:' . $status->id, now()->addMinutes(30), function() use($status) {
|
|
|
|
$item = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
|
|
|
|
$res = [
|
|
|
|
'status' => $this->fractal->createData($item)->toArray(),
|
|
|
|
];
|
2020-12-14 05:51:44 +00:00
|
|
|
return $res;
|
2020-12-10 04:40:24 +00:00
|
|
|
});
|
2020-12-14 05:51:44 +00:00
|
|
|
return response()->json($res);
|
2020-12-10 04:40:24 +00:00
|
|
|
}
|
2020-12-14 05:51:44 +00:00
|
|
|
$item = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
|
2018-11-09 05:23:36 +00:00
|
|
|
$res = [
|
2018-11-17 19:58:58 +00:00
|
|
|
'status' => $this->fractal->createData($item)->toArray(),
|
2020-12-14 05:51:44 +00:00
|
|
|
];
|
|
|
|
return response()->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function statusState(Request $request, $username, int $postid)
|
|
|
|
{
|
|
|
|
$profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
|
|
|
|
$status = Status::whereProfileId($profile->id)->findOrFail($postid);
|
|
|
|
$this->scopeCheck($profile, $status);
|
|
|
|
if(!Auth::check()) {
|
|
|
|
$res = [
|
|
|
|
'user' => [],
|
|
|
|
'likes' => [],
|
|
|
|
'shares' => [],
|
|
|
|
'reactions' => [
|
|
|
|
'liked' => false,
|
|
|
|
'shared' => false,
|
|
|
|
'bookmarked' => false,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
return response()->json($res);
|
|
|
|
}
|
|
|
|
$res = [
|
|
|
|
'user' => $this->getUserData($request->user()),
|
|
|
|
'likes' => [],
|
|
|
|
'shares' => [],
|
2018-11-17 19:58:58 +00:00
|
|
|
'reactions' => [
|
2020-12-14 05:51:44 +00:00
|
|
|
'liked' => (bool) $status->liked(),
|
|
|
|
'shared' => (bool) $status->shared(),
|
|
|
|
'bookmarked' => (bool) $status->bookmarked(),
|
2018-11-17 19:58:58 +00:00
|
|
|
],
|
2018-11-09 05:23:36 +00:00
|
|
|
];
|
2020-12-14 05:51:44 +00:00
|
|
|
return response()->json($res);
|
2018-11-09 05:23:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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'
|
|
|
|
]);
|
2019-04-14 03:25:34 +00:00
|
|
|
|
2018-11-09 05:23:36 +00:00
|
|
|
$limit = $request->limit ?? 10;
|
2020-07-22 01:14:48 +00:00
|
|
|
$profile = Profile::whereNull('status')->findOrFail($username);
|
2019-04-01 03:30:16 +00:00
|
|
|
$status = Status::whereProfileId($profile->id)->whereCommentsDisabled(false)->findOrFail($postId);
|
2018-11-29 08:32:27 +00:00
|
|
|
$this->scopeCheck($profile, $status);
|
2019-07-21 02:09:46 +00:00
|
|
|
|
|
|
|
if(Auth::check()) {
|
2019-11-28 03:13:07 +00:00
|
|
|
$p = Auth::user()->profile;
|
|
|
|
$filtered = UserFilter::whereUserId($p->id)
|
2019-07-21 02:09:46 +00:00
|
|
|
->whereFilterableType('App\Profile')
|
|
|
|
->whereIn('filter_type', ['mute', 'block'])
|
|
|
|
->pluck('filterable_id')->toArray();
|
2021-03-04 03:18:32 +00:00
|
|
|
$scope = $p->id == $status->profile_id ? ['public', 'private', 'unlisted'] : ['public','unlisted'];
|
2019-07-21 02:09:46 +00:00
|
|
|
} else {
|
|
|
|
$filtered = [];
|
2021-03-04 03:18:32 +00:00
|
|
|
$scope = ['public', 'unlisted'];
|
2019-07-21 02:09:46 +00:00
|
|
|
}
|
|
|
|
|
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')
|
2019-11-28 03:13:07 +00:00
|
|
|
->whereIn('scope', $scope)
|
2019-07-21 02:09:46 +00:00
|
|
|
->whereNotIn('profile_id', $filtered)
|
2021-03-04 03:18:32 +00:00
|
|
|
->select('id', 'caption', 'local', 'visibility', 'scope', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
|
2018-11-09 05:23:36 +00:00
|
|
|
->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')
|
2019-11-28 03:13:07 +00:00
|
|
|
->whereIn('scope', $scope)
|
2019-07-21 02:09:46 +00:00
|
|
|
->whereNotIn('profile_id', $filtered)
|
2021-03-04 03:18:32 +00:00
|
|
|
->select('id', 'caption', 'local', 'visibility', 'scope', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
|
2018-11-09 05:23:36 +00:00
|
|
|
->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')
|
2019-11-28 03:13:07 +00:00
|
|
|
->whereIn('scope', $scope)
|
2019-07-21 02:09:46 +00:00
|
|
|
->whereNotIn('profile_id', $filtered)
|
2021-03-04 03:18:32 +00:00
|
|
|
->select('id', 'caption', 'local', 'visibility', 'scope', 'is_nsfw', 'rendered', 'profile_id', 'in_reply_to_id', 'type', 'reply_count', 'created_at')
|
2018-11-09 05:23:36 +00:00
|
|
|
->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)
|
|
|
|
{
|
2021-06-01 04:43:53 +00:00
|
|
|
abort_if(!$request->user(), 404);
|
2021-06-01 03:49:44 +00:00
|
|
|
$status = Status::findOrFail($id);
|
|
|
|
$this->scopeCheck($status->profile, $status);
|
2021-06-01 04:43:53 +00:00
|
|
|
$page = $request->input('page');
|
|
|
|
if($page && $page >= 3 && $request->user()->profile_id != $status->profile_id) {
|
|
|
|
return response()->json([
|
|
|
|
'data' => []
|
|
|
|
]);
|
|
|
|
}
|
2018-12-02 04:01:15 +00:00
|
|
|
$likes = $this->getLikes($status);
|
|
|
|
return response()->json([
|
|
|
|
'data' => $likes
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function statusShares(Request $request, $username, $id)
|
|
|
|
{
|
2021-06-01 04:43:53 +00:00
|
|
|
abort_if(!$request->user(), 404);
|
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);
|
2021-06-01 04:43:53 +00:00
|
|
|
$page = $request->input('page');
|
|
|
|
if($page && $page >= 3 && $request->user()->profile_id != $status->profile_id) {
|
|
|
|
return response()->json([
|
|
|
|
'data' => []
|
|
|
|
]);
|
|
|
|
}
|
2018-12-02 04:01:15 +00:00
|
|
|
$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);
|
2021-04-07 03:17:42 +00:00
|
|
|
}
|
2018-11-29 08:32:27 +00:00
|
|
|
|
|
|
|
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 {
|
2019-04-13 05:28:23 +00:00
|
|
|
$follows = $profile->followedBy($user->profile);
|
|
|
|
if($follows == false && $profile->id !== $user->profile->id && $user->is_admin == false) {
|
2018-11-29 08:32:27 +00:00
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'direct':
|
|
|
|
abort(404);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'draft':
|
|
|
|
abort(404);
|
|
|
|
break;
|
2021-04-07 03:17:42 +00:00
|
|
|
|
2018-11-29 08:32:27 +00:00
|
|
|
default:
|
|
|
|
abort(404);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-12-09 23:03:53 +00:00
|
|
|
|
|
|
|
public function publicTimelineApi(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request,[
|
|
|
|
'page' => 'nullable|integer|max:40',
|
2019-03-04 02:31:19 +00:00
|
|
|
'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
|
|
|
|
'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
|
2019-09-21 04:01:36 +00:00
|
|
|
'limit' => 'nullable|integer|max:30'
|
2018-12-09 23:03:53 +00:00
|
|
|
]);
|
|
|
|
|
2019-07-08 02:24:10 +00:00
|
|
|
if(config('instance.timeline.local.is_public') == false && !Auth::check()) {
|
|
|
|
abort(403, 'Authentication required.');
|
|
|
|
}
|
|
|
|
|
2018-12-09 23:03:53 +00:00
|
|
|
$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;
|
2021-01-09 09:41:17 +00:00
|
|
|
$user = $request->user();
|
|
|
|
|
|
|
|
$key = 'user:last_active_at:id:'.$user->id;
|
|
|
|
$ttl = now()->addMinutes(5);
|
|
|
|
Cache::remember($key, $ttl, function() use($user) {
|
|
|
|
$user->last_active_at = now();
|
|
|
|
$user->save();
|
|
|
|
return;
|
|
|
|
});
|
2018-12-09 23:03:53 +00:00
|
|
|
|
2021-01-09 09:41:17 +00:00
|
|
|
$filtered = UserFilter::whereUserId($user->profile_id)
|
2020-04-22 01:45:42 +00:00
|
|
|
->whereFilterableType('App\Profile')
|
|
|
|
->whereIn('filter_type', ['mute', 'block'])
|
|
|
|
->pluck('filterable_id')->toArray();
|
|
|
|
|
2018-12-09 23:03:53 +00:00
|
|
|
if($min || $max) {
|
|
|
|
$dir = $min ? '>' : '<';
|
|
|
|
$id = $min ?? $max;
|
2019-02-14 01:16:58 +00:00
|
|
|
$timeline = Status::select(
|
2021-04-07 03:17:42 +00:00
|
|
|
'id',
|
2019-02-14 01:16:58 +00:00
|
|
|
'uri',
|
|
|
|
'caption',
|
|
|
|
'rendered',
|
2021-04-07 03:17:42 +00:00
|
|
|
'profile_id',
|
2019-02-14 01:16:58 +00:00
|
|
|
'type',
|
|
|
|
'in_reply_to_id',
|
|
|
|
'reblog_of_id',
|
|
|
|
'is_nsfw',
|
|
|
|
'scope',
|
|
|
|
'local',
|
2019-04-03 06:03:40 +00:00
|
|
|
'reply_count',
|
|
|
|
'comments_disabled',
|
2019-08-17 07:42:21 +00:00
|
|
|
'place_id',
|
2019-10-07 03:36:02 +00:00
|
|
|
'likes_count',
|
|
|
|
'reblogs_count',
|
2019-02-14 01:16:58 +00:00
|
|
|
'created_at',
|
|
|
|
'updated_at'
|
2019-03-04 02:31:19 +00:00
|
|
|
)->where('id', $dir, $id)
|
2021-01-12 01:47:16 +00:00
|
|
|
->whereIn('type', ['text', 'photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
|
2020-04-22 01:45:42 +00:00
|
|
|
->whereNotIn('profile_id', $filtered)
|
2019-01-10 19:25:25 +00:00
|
|
|
->whereLocal(true)
|
2020-12-28 01:00:43 +00:00
|
|
|
->whereScope('public')
|
2021-05-12 04:12:10 +00:00
|
|
|
->where('created_at', '>', now()->subMonths(6))
|
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(
|
2021-04-07 03:17:42 +00:00
|
|
|
'id',
|
2019-02-25 06:28:35 +00:00
|
|
|
'uri',
|
|
|
|
'caption',
|
|
|
|
'rendered',
|
2021-04-07 03:17:42 +00:00
|
|
|
'profile_id',
|
2019-02-25 06:28:35 +00:00
|
|
|
'type',
|
|
|
|
'in_reply_to_id',
|
|
|
|
'reblog_of_id',
|
|
|
|
'is_nsfw',
|
|
|
|
'scope',
|
|
|
|
'local',
|
2019-04-03 06:03:40 +00:00
|
|
|
'reply_count',
|
|
|
|
'comments_disabled',
|
2019-02-25 06:28:35 +00:00
|
|
|
'created_at',
|
2019-08-17 07:42:21 +00:00
|
|
|
'place_id',
|
2019-10-07 03:36:02 +00:00
|
|
|
'likes_count',
|
|
|
|
'reblogs_count',
|
2019-02-25 06:28:35 +00:00
|
|
|
'updated_at'
|
2021-01-12 01:47:16 +00:00
|
|
|
)->whereIn('type', ['text', 'photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
|
2020-04-22 01:45:42 +00:00
|
|
|
->whereNotIn('profile_id', $filtered)
|
2019-08-05 04:58:53 +00:00
|
|
|
->with('profile', 'hashtags', 'mentions')
|
2019-01-10 19:25:25 +00:00
|
|
|
->whereLocal(true)
|
2020-12-28 01:00:43 +00:00
|
|
|
->whereScope('public')
|
2021-05-12 04:12:10 +00:00
|
|
|
->where('created_at', '>', now()->subMonths(6))
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function homeTimelineApi(Request $request)
|
|
|
|
{
|
|
|
|
if(!Auth::check()) {
|
|
|
|
return abort(403);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->validate($request,[
|
|
|
|
'page' => 'nullable|integer|max:40',
|
2019-03-04 02:31:19 +00:00
|
|
|
'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
|
|
|
|
'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX,
|
2021-06-02 02:03:59 +00:00
|
|
|
'limit' => 'nullable|integer|max:40',
|
|
|
|
'recent_feed' => 'nullable',
|
|
|
|
'recent_min' => 'nullable|integer'
|
2018-12-09 23:03:53 +00:00
|
|
|
]);
|
|
|
|
|
2021-06-02 02:03:59 +00:00
|
|
|
$recentFeed = $request->input('recent_feed') == 'true';
|
|
|
|
$recentFeedMin = $request->input('recent_min');
|
2018-12-09 23:03:53 +00:00
|
|
|
$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;
|
2021-01-09 09:41:17 +00:00
|
|
|
$user = $request->user();
|
2021-04-07 03:17:42 +00:00
|
|
|
|
2021-01-09 09:41:17 +00:00
|
|
|
$key = 'user:last_active_at:id:'.$user->id;
|
|
|
|
$ttl = now()->addMinutes(5);
|
|
|
|
Cache::remember($key, $ttl, function() use($user) {
|
|
|
|
$user->last_active_at = now();
|
|
|
|
$user->save();
|
|
|
|
return;
|
|
|
|
});
|
2018-12-09 23:03:53 +00:00
|
|
|
|
2021-06-02 02:03:59 +00:00
|
|
|
$pid = Auth::user()->profile_id;
|
2018-12-09 23:03:53 +00:00
|
|
|
|
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
|
|
|
|
2021-06-02 02:03:59 +00:00
|
|
|
if($recentFeed == true) {
|
|
|
|
$key = 'profile:home-timeline-cursor:'.$user->id;
|
|
|
|
$ttl = now()->addMinutes(30);
|
|
|
|
$min = Cache::remember($key, $ttl, function() use($pid) {
|
|
|
|
$res = StatusView::whereProfileId($pid)->orderByDesc('status_id')->first();
|
|
|
|
return $res ? $res->status_id : null;
|
|
|
|
});
|
|
|
|
}
|
2019-03-04 05:09:35 +00:00
|
|
|
|
2019-09-04 01:47:46 +00:00
|
|
|
$filtered = Auth::check() ? UserFilterService::filters(Auth::user()->profile_id) : [];
|
2018-12-09 23:03:53 +00:00
|
|
|
|
|
|
|
if($min || $max) {
|
|
|
|
$dir = $min ? '>' : '<';
|
|
|
|
$id = $min ?? $max;
|
2019-02-14 01:16:58 +00:00
|
|
|
$timeline = Status::select(
|
2021-04-07 03:17:42 +00:00
|
|
|
'id',
|
2019-02-14 01:16:58 +00:00
|
|
|
'uri',
|
|
|
|
'caption',
|
|
|
|
'rendered',
|
2021-04-07 03:17:42 +00:00
|
|
|
'profile_id',
|
2019-02-14 01:16:58 +00:00
|
|
|
'type',
|
|
|
|
'in_reply_to_id',
|
|
|
|
'reblog_of_id',
|
|
|
|
'is_nsfw',
|
|
|
|
'scope',
|
|
|
|
'local',
|
2019-04-03 06:03:40 +00:00
|
|
|
'reply_count',
|
|
|
|
'comments_disabled',
|
2019-08-17 07:42:21 +00:00
|
|
|
'place_id',
|
2019-10-07 03:36:02 +00:00
|
|
|
'likes_count',
|
|
|
|
'reblogs_count',
|
2019-02-14 01:16:58 +00:00
|
|
|
'created_at',
|
|
|
|
'updated_at'
|
2021-01-12 01:47:16 +00:00
|
|
|
)->whereIn('type', ['text','photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
|
2019-08-05 04:58:53 +00:00
|
|
|
->with('profile', 'hashtags', 'mentions')
|
2018-12-09 23:03:53 +00:00
|
|
|
->where('id', $dir, $id)
|
|
|
|
->whereIn('profile_id', $following)
|
|
|
|
->whereNotIn('profile_id', $filtered)
|
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(
|
2021-04-07 03:17:42 +00:00
|
|
|
'id',
|
2019-02-25 06:28:35 +00:00
|
|
|
'uri',
|
|
|
|
'caption',
|
|
|
|
'rendered',
|
2021-04-07 03:17:42 +00:00
|
|
|
'profile_id',
|
2019-02-25 06:28:35 +00:00
|
|
|
'type',
|
|
|
|
'in_reply_to_id',
|
|
|
|
'reblog_of_id',
|
|
|
|
'is_nsfw',
|
|
|
|
'scope',
|
|
|
|
'local',
|
2019-04-03 06:03:40 +00:00
|
|
|
'reply_count',
|
|
|
|
'comments_disabled',
|
2019-08-17 07:42:21 +00:00
|
|
|
'place_id',
|
2019-10-07 03:36:02 +00:00
|
|
|
'likes_count',
|
|
|
|
'reblogs_count',
|
2019-02-25 06:28:35 +00:00
|
|
|
'created_at',
|
|
|
|
'updated_at'
|
2021-01-12 01:47:16 +00:00
|
|
|
)->whereIn('type', ['text','photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
|
2019-08-05 04:58:53 +00:00
|
|
|
->with('profile', 'hashtags', 'mentions')
|
2018-12-09 23:03:53 +00:00
|
|
|
->whereIn('profile_id', $following)
|
|
|
|
->whereNotIn('profile_id', $filtered)
|
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
|
|
|
|
2019-03-14 19:52:36 +00:00
|
|
|
public function networkTimelineApi(Request $request)
|
|
|
|
{
|
2021-04-07 03:17:42 +00:00
|
|
|
abort_if(!Auth::check(), 403);
|
|
|
|
abort_if(config('federation.network_timeline') == false, 404);
|
|
|
|
|
|
|
|
$this->validate($request,[
|
|
|
|
'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'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$page = $request->input('page');
|
|
|
|
$min = $request->input('min_id');
|
|
|
|
$max = $request->input('max_id');
|
|
|
|
$limit = $request->input('limit') ?? 3;
|
|
|
|
$user = $request->user();
|
2021-06-29 07:23:24 +00:00
|
|
|
$amin = SnowflakeService::byDate(now()->subDays(90));
|
2021-04-07 03:17:42 +00:00
|
|
|
|
|
|
|
$key = 'user:last_active_at:id:'.$user->id;
|
|
|
|
$ttl = now()->addMinutes(5);
|
|
|
|
Cache::remember($key, $ttl, function() use($user) {
|
|
|
|
$user->last_active_at = now();
|
|
|
|
$user->save();
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
|
|
|
|
if($min || $max) {
|
|
|
|
$dir = $min ? '>' : '<';
|
|
|
|
$id = $min ?? $max;
|
|
|
|
$timeline = Status::select(
|
|
|
|
'id',
|
|
|
|
'uri',
|
|
|
|
'type',
|
|
|
|
'scope',
|
|
|
|
'created_at',
|
|
|
|
)->where('id', $dir, $id)
|
|
|
|
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
|
|
|
|
->whereNotNull('uri')
|
|
|
|
->whereScope('public')
|
2021-06-29 07:23:24 +00:00
|
|
|
->where('id', '>', $amin)
|
2021-04-07 03:17:42 +00:00
|
|
|
->orderBy('created_at', 'desc')
|
|
|
|
->limit($limit)
|
2021-06-29 07:23:24 +00:00
|
|
|
->get()
|
|
|
|
->map(function($s) {
|
|
|
|
return StatusService::get($s->id);
|
|
|
|
});
|
|
|
|
$res = $timeline->toArray();
|
2021-04-07 03:17:42 +00:00
|
|
|
} else {
|
2021-06-29 07:23:24 +00:00
|
|
|
$timeline = Status::select(
|
|
|
|
'id',
|
|
|
|
'uri',
|
|
|
|
'type',
|
|
|
|
'scope',
|
|
|
|
'created_at',
|
|
|
|
)->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
|
|
|
|
->whereNotNull('uri')
|
|
|
|
->whereScope('public')
|
|
|
|
->where('id', '>', $amin)
|
|
|
|
->orderBy('created_at', 'desc')
|
|
|
|
->limit($limit)
|
|
|
|
->get()
|
|
|
|
->map(function($s) {
|
|
|
|
return StatusService::get($s->id);
|
|
|
|
});
|
|
|
|
$res = $timeline->toArray();
|
2021-04-07 03:17:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($res);
|
2019-03-14 19:52:36 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 20:42:26 +00:00
|
|
|
public function relationships(Request $request)
|
|
|
|
{
|
2019-08-17 07:42:21 +00:00
|
|
|
if(!Auth::check()) {
|
|
|
|
return response()->json([]);
|
|
|
|
}
|
2019-02-10 20:42:26 +00:00
|
|
|
|
|
|
|
$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'));
|
2021-04-07 03:17:42 +00:00
|
|
|
$filtered = $ids->filter(function($v) {
|
2019-02-10 20:42:26 +00:00
|
|
|
return $v != Auth::user()->profile->id;
|
|
|
|
});
|
2019-07-23 04:06:11 +00:00
|
|
|
$relations = Profile::whereNull('status')->findOrFail($filtered->all());
|
2019-02-10 20:42:26 +00:00
|
|
|
$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)
|
|
|
|
{
|
2019-11-23 05:43:20 +00:00
|
|
|
$res = AccountService::get($id);
|
2019-02-11 04:49:54 +00:00
|
|
|
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);
|
2019-04-18 05:58:08 +00:00
|
|
|
if(Auth::id() != $profile->user_id && $profile->is_private || !$profile->user->settings->show_profile_followers) {
|
2019-04-18 05:56:33 +00:00
|
|
|
return response()->json([]);
|
2019-02-25 04:30:02 +00:00
|
|
|
}
|
|
|
|
$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);
|
2020-03-15 03:21:45 +00:00
|
|
|
|
|
|
|
$profile = Profile::with('user')
|
|
|
|
->whereNull('status')
|
|
|
|
->whereNull('domain')
|
|
|
|
->findOrFail($id);
|
|
|
|
|
|
|
|
// filter by username
|
|
|
|
$search = $request->input('fbu');
|
|
|
|
$owner = Auth::id() == $profile->user_id;
|
|
|
|
$filter = ($owner == true) && ($search != null);
|
|
|
|
|
|
|
|
abort_if($owner == false && $profile->is_private == true && !$profile->followedBy(Auth::user()->profile), 404);
|
|
|
|
abort_if($profile->user->settings->show_profile_following == false && $owner == false, 404);
|
|
|
|
|
|
|
|
if($search) {
|
|
|
|
abort_if(!$owner, 404);
|
|
|
|
$following = $profile->following()
|
|
|
|
->where('profiles.username', 'like', '%'.$search.'%')
|
|
|
|
->orderByDesc('followers.created_at')
|
|
|
|
->paginate(10);
|
|
|
|
} else {
|
|
|
|
$following = $profile->following()
|
|
|
|
->orderByDesc('followers.created_at')
|
|
|
|
->paginate(10);
|
2019-02-25 04:30:02 +00:00
|
|
|
}
|
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',
|
2019-03-04 05:09:35 +00:00
|
|
|
'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,
|
2019-02-11 04:49:54 +00:00
|
|
|
'limit' => 'nullable|integer|min:1|max:24'
|
|
|
|
]);
|
2019-03-04 05:09:35 +00:00
|
|
|
|
|
|
|
$profile = Profile::whereNull('status')->findOrFail($id);
|
|
|
|
|
|
|
|
$limit = $request->limit ?? 9;
|
|
|
|
$max_id = $request->max_id;
|
|
|
|
$min_id = $request->min_id;
|
2021-04-07 03:17:42 +00:00
|
|
|
$scope = $request->only_media == true ?
|
2019-03-04 05:09:35 +00:00
|
|
|
['photo', 'photo:album', 'video', 'video:album'] :
|
|
|
|
['photo', 'photo:album', 'video', 'video:album', 'share', 'reply'];
|
2021-04-07 03:17:42 +00:00
|
|
|
|
2019-03-04 05:09:35 +00:00
|
|
|
if($profile->is_private) {
|
|
|
|
if(!Auth::check()) {
|
|
|
|
return response()->json([]);
|
|
|
|
}
|
|
|
|
$pid = Auth::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'] : [];
|
2019-02-11 04:49:54 +00:00
|
|
|
} else {
|
2019-03-04 05:09:35 +00:00
|
|
|
if(Auth::check()) {
|
|
|
|
$pid = Auth::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();
|
|
|
|
});
|
2019-04-01 03:30:16 +00:00
|
|
|
$visibility = true == in_array($profile->id, $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
|
2019-03-04 05:09:35 +00:00
|
|
|
} else {
|
2019-04-01 03:30:16 +00:00
|
|
|
$visibility = ['public', 'unlisted'];
|
2019-03-04 05:09:35 +00:00
|
|
|
}
|
2019-02-11 04:49:54 +00:00
|
|
|
}
|
2019-03-04 05:09:35 +00:00
|
|
|
|
2020-12-10 04:40:24 +00:00
|
|
|
$tag = in_array('private', $visibility) ? 'private' : 'public';
|
|
|
|
if($min_id == 1 && $limit == 9 && $tag == 'public') {
|
|
|
|
$limit = 9;
|
|
|
|
$scope = ['photo', 'photo:album', 'video', 'video:album'];
|
|
|
|
$key = '_api:statuses:recent_9:'.$profile->id;
|
|
|
|
$res = Cache::remember($key, now()->addHours(24), function() use($profile, $scope, $visibility, $limit) {
|
|
|
|
$dir = '>';
|
|
|
|
$id = 1;
|
|
|
|
$timeline = Status::select(
|
2021-04-07 03:17:42 +00:00
|
|
|
'id',
|
2020-12-10 04:40:24 +00:00
|
|
|
'uri',
|
|
|
|
'caption',
|
|
|
|
'rendered',
|
2021-04-07 03:17:42 +00:00
|
|
|
'profile_id',
|
2020-12-10 04:40:24 +00:00
|
|
|
'type',
|
|
|
|
'in_reply_to_id',
|
|
|
|
'reblog_of_id',
|
|
|
|
'is_nsfw',
|
|
|
|
'likes_count',
|
|
|
|
'reblogs_count',
|
|
|
|
'scope',
|
|
|
|
'visibility',
|
|
|
|
'local',
|
|
|
|
'place_id',
|
|
|
|
'comments_disabled',
|
|
|
|
'cw_summary',
|
|
|
|
'created_at',
|
|
|
|
'updated_at'
|
|
|
|
)->whereProfileId($profile->id)
|
|
|
|
->whereIn('type', $scope)
|
|
|
|
->where('id', $dir, $id)
|
|
|
|
->whereIn('visibility', $visibility)
|
|
|
|
->limit($limit)
|
|
|
|
->orderByDesc('id')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$resource = new Fractal\Resource\Collection($timeline, new StatusStatelessTransformer());
|
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
|
|
|
|
|
|
|
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
|
|
|
|
});
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2019-03-04 05:09:35 +00:00
|
|
|
$dir = $min_id ? '>' : '<';
|
|
|
|
$id = $min_id ?? $max_id;
|
|
|
|
$timeline = Status::select(
|
2021-04-07 03:17:42 +00:00
|
|
|
'id',
|
2019-03-04 05:09:35 +00:00
|
|
|
'uri',
|
|
|
|
'caption',
|
|
|
|
'rendered',
|
2021-04-07 03:17:42 +00:00
|
|
|
'profile_id',
|
2019-03-04 05:09:35 +00:00
|
|
|
'type',
|
|
|
|
'in_reply_to_id',
|
|
|
|
'reblog_of_id',
|
|
|
|
'is_nsfw',
|
2019-10-11 01:13:33 +00:00
|
|
|
'likes_count',
|
|
|
|
'reblogs_count',
|
2019-03-04 05:09:35 +00:00
|
|
|
'scope',
|
2020-05-24 05:04:28 +00:00
|
|
|
'visibility',
|
2019-03-04 05:09:35 +00:00
|
|
|
'local',
|
2020-12-10 04:40:24 +00:00
|
|
|
'place_id',
|
|
|
|
'comments_disabled',
|
2020-04-21 04:24:27 +00:00
|
|
|
'cw_summary',
|
2019-03-04 05:09:35 +00:00
|
|
|
'created_at',
|
|
|
|
'updated_at'
|
|
|
|
)->whereProfileId($profile->id)
|
|
|
|
->whereIn('type', $scope)
|
|
|
|
->where('id', $dir, $id)
|
2019-04-01 03:30:16 +00:00
|
|
|
->whereIn('visibility', $visibility)
|
2019-03-04 05:09:35 +00:00
|
|
|
->limit($limit)
|
2020-05-26 02:40:41 +00:00
|
|
|
->orderByDesc('id')
|
2019-03-04 05:09:35 +00:00
|
|
|
->get();
|
|
|
|
|
2020-12-13 06:47:59 +00:00
|
|
|
$resource = new Fractal\Resource\Collection($timeline, new StatusStatelessTransformer());
|
2019-02-11 04:49:54 +00:00
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
|
|
|
|
2020-05-24 05:04:28 +00:00
|
|
|
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
|
2019-02-11 04:49:54 +00:00
|
|
|
}
|
2019-02-25 04:30:02 +00:00
|
|
|
|
2018-11-09 05:23:36 +00:00
|
|
|
}
|