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
|
|
|
};
|
2022-01-08 11:02:14 +00:00
|
|
|
use Auth, Cache, DB;
|
2021-07-21 08:00:57 +00:00
|
|
|
use Illuminate\Support\Facades\Redis;
|
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,
|
2022-01-27 12:11:34 +00:00
|
|
|
BookmarkService,
|
2021-12-05 03:41:46 +00:00
|
|
|
FollowerService,
|
2021-07-02 07:34:35 +00:00
|
|
|
LikeService,
|
2019-11-23 05:43:20 +00:00
|
|
|
PublicTimelineService,
|
2021-07-27 00:47:40 +00:00
|
|
|
ProfileService,
|
2022-06-09 10:15:23 +00:00
|
|
|
NetworkTimelineService,
|
2022-01-29 05:06:43 +00:00
|
|
|
ReblogService,
|
2021-10-20 10:31:07 +00:00
|
|
|
RelationshipService,
|
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
|
|
|
{
|
2021-10-07 09:30:23 +00:00
|
|
|
if(!$user) {
|
|
|
|
return [];
|
|
|
|
} else {
|
2019-11-23 05:43:20 +00:00
|
|
|
return AccountService::get($user->profile_id);
|
2021-10-07 09:30:23 +00:00
|
|
|
}
|
2018-11-09 05:23:36 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 06:46:17 +00:00
|
|
|
public function getStatus(Request $request, $id)
|
|
|
|
{
|
|
|
|
abort_if(!$request->user(), 403);
|
|
|
|
$status = StatusService::get($id, false);
|
|
|
|
abort_if(!$status, 404);
|
|
|
|
if(in_array($status['visibility'], ['public', 'unlisted'])) {
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
if($status['account']['id'] == $pid) {
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
if($status['visibility'] == 'private') {
|
|
|
|
if(FollowerService::follows($pid, $status['account']['id'])) {
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
|
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);
|
2021-08-05 02:29:21 +00:00
|
|
|
if(!$request->user()) {
|
2022-08-14 04:51:02 +00:00
|
|
|
$cached = StatusService::get($status->id, false);
|
|
|
|
abort_if(!in_array($cached['visibility'], ['public', 'unlisted']), 403);
|
|
|
|
$res = ['status' => $cached];
|
2021-08-05 02:29:21 +00:00
|
|
|
} else {
|
2021-10-07 09:30:23 +00:00
|
|
|
$item = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
|
|
|
|
$res = [
|
|
|
|
'status' => $this->fractal->createData($item)->toArray(),
|
|
|
|
];
|
2020-12-10 04:40:24 +00:00
|
|
|
}
|
2021-08-05 02:29:21 +00:00
|
|
|
|
2020-12-14 05:51:44 +00:00
|
|
|
return response()->json($res);
|
|
|
|
}
|
|
|
|
|
2022-03-31 02:33:34 +00:00
|
|
|
public function statusState(Request $request, $username, int $postid)
|
2020-12-14 05:51:44 +00:00
|
|
|
{
|
|
|
|
$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;
|
2021-12-05 03:41:46 +00:00
|
|
|
$scope = $p->id == $status->profile_id || FollowerService::follows($p->id, $profile->id) ? ['public', 'private', 'unlisted'] : ['public','unlisted'];
|
2019-07-21 02:09:46 +00:00
|
|
|
} else {
|
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)
|
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)
|
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 {
|
2021-12-05 03:41:46 +00:00
|
|
|
$replies = Status::whereInReplyToId($status->id)
|
2018-12-25 06:55:11 +00:00
|
|
|
->whereNull('reblog_of_id')
|
2019-11-28 03:13:07 +00:00
|
|
|
->whereIn('scope', $scope)
|
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);
|
|
|
|
}
|
|
|
|
|
2022-02-16 11:41:36 +00:00
|
|
|
$resource = new Fractal\Resource\Collection($replies, new StatusStatelessTransformer(), 'data');
|
2018-11-09 05:23:36 +00:00
|
|
|
$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
|
|
|
|
|
|
|
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
|
|
|
]);
|
|
|
|
|
2022-03-31 06:04:04 +00:00
|
|
|
if(!$request->user()) {
|
|
|
|
return response('', 403);
|
2019-07-08 02:24:10 +00:00
|
|
|
}
|
|
|
|
|
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-10-22 01:02:15 +00:00
|
|
|
$filtered = $user ? UserFilterService::filters($user->profile_id) : [];
|
|
|
|
|
2022-11-28 12:13:12 +00:00
|
|
|
$hideNsfw = config('instance.hide_nsfw_on_public_feeds');
|
2021-10-22 01:02:15 +00:00
|
|
|
if(config('exp.cached_public_timeline') == false) {
|
2021-12-05 03:41:46 +00:00
|
|
|
if($min || $max) {
|
|
|
|
$dir = $min ? '>' : '<';
|
|
|
|
$id = $min ?? $max;
|
|
|
|
$timeline = Status::select(
|
|
|
|
'id',
|
|
|
|
'profile_id',
|
|
|
|
'type',
|
|
|
|
'scope',
|
|
|
|
'local'
|
|
|
|
)
|
|
|
|
->where('id', $dir, $id)
|
2021-12-22 07:59:03 +00:00
|
|
|
->whereNull(['in_reply_to_id', 'reblog_of_id'])
|
2021-12-05 03:41:46 +00:00
|
|
|
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
|
|
|
|
->whereLocal(true)
|
2022-11-28 12:13:12 +00:00
|
|
|
->when($hideNsfw, function($q, $hideNsfw) {
|
|
|
|
return $q->where('is_nsfw', false);
|
|
|
|
})
|
2021-12-05 03:41:46 +00:00
|
|
|
->whereScope('public')
|
|
|
|
->orderBy('id', 'desc')
|
|
|
|
->limit($limit)
|
|
|
|
->get()
|
|
|
|
->map(function($s) use ($user) {
|
|
|
|
$status = StatusService::getFull($s->id, $user->profile_id);
|
2022-01-10 02:32:41 +00:00
|
|
|
if(!$status) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-05 03:41:46 +00:00
|
|
|
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
|
2022-01-27 12:11:34 +00:00
|
|
|
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
|
2022-01-29 05:06:43 +00:00
|
|
|
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
|
2021-12-05 03:41:46 +00:00
|
|
|
return $status;
|
|
|
|
})
|
|
|
|
->filter(function($s) use($filtered) {
|
2022-01-08 02:47:21 +00:00
|
|
|
return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
|
2021-12-22 07:59:03 +00:00
|
|
|
})
|
|
|
|
->values();
|
2021-12-05 03:41:46 +00:00
|
|
|
$res = $timeline->toArray();
|
|
|
|
} else {
|
|
|
|
$timeline = Status::select(
|
|
|
|
'id',
|
|
|
|
'uri',
|
|
|
|
'caption',
|
|
|
|
'rendered',
|
|
|
|
'profile_id',
|
|
|
|
'type',
|
|
|
|
'in_reply_to_id',
|
|
|
|
'reblog_of_id',
|
|
|
|
'is_nsfw',
|
|
|
|
'scope',
|
|
|
|
'local',
|
|
|
|
'reply_count',
|
|
|
|
'comments_disabled',
|
|
|
|
'created_at',
|
|
|
|
'place_id',
|
|
|
|
'likes_count',
|
|
|
|
'reblogs_count',
|
|
|
|
'updated_at'
|
|
|
|
)
|
2021-12-22 07:59:03 +00:00
|
|
|
->whereNull(['in_reply_to_id', 'reblog_of_id'])
|
2021-12-05 03:41:46 +00:00
|
|
|
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
|
|
|
|
->whereLocal(true)
|
2022-11-28 12:13:12 +00:00
|
|
|
->when($hideNsfw, function($q, $hideNsfw) {
|
|
|
|
return $q->where('is_nsfw', false);
|
|
|
|
})
|
2021-12-05 03:41:46 +00:00
|
|
|
->whereScope('public')
|
|
|
|
->orderBy('id', 'desc')
|
|
|
|
->limit($limit)
|
|
|
|
->get()
|
|
|
|
->map(function($s) use ($user) {
|
|
|
|
$status = StatusService::getFull($s->id, $user->profile_id);
|
2022-01-10 02:32:41 +00:00
|
|
|
if(!$status) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-05 03:41:46 +00:00
|
|
|
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
|
2022-01-27 12:11:34 +00:00
|
|
|
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
|
2022-01-29 05:06:43 +00:00
|
|
|
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
|
2021-12-05 03:41:46 +00:00
|
|
|
return $status;
|
|
|
|
})
|
|
|
|
->filter(function($s) use($filtered) {
|
2022-04-03 06:50:25 +00:00
|
|
|
return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
|
2021-12-22 07:59:03 +00:00
|
|
|
})
|
|
|
|
->values();
|
2021-12-05 03:41:46 +00:00
|
|
|
|
|
|
|
$res = $timeline->toArray();
|
|
|
|
}
|
2021-10-22 01:02:15 +00:00
|
|
|
} else {
|
2021-12-05 03:41:46 +00:00
|
|
|
Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() {
|
|
|
|
if(PublicTimelineService::count() == 0) {
|
|
|
|
PublicTimelineService::warmCache(true, 400);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($max) {
|
|
|
|
$feed = PublicTimelineService::getRankedMaxId($max, $limit);
|
|
|
|
} else if ($min) {
|
|
|
|
$feed = PublicTimelineService::getRankedMinId($min, $limit);
|
|
|
|
} else {
|
|
|
|
$feed = PublicTimelineService::get(0, $limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = collect($feed)
|
2022-06-19 13:05:07 +00:00
|
|
|
->take($limit)
|
2021-12-05 03:41:46 +00:00
|
|
|
->map(function($k) use($user) {
|
|
|
|
$status = StatusService::get($k);
|
2022-04-03 06:50:25 +00:00
|
|
|
if($status && isset($status['account']) && $user) {
|
2021-12-05 03:41:46 +00:00
|
|
|
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
|
2022-01-27 12:11:34 +00:00
|
|
|
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $k);
|
2022-01-29 05:06:43 +00:00
|
|
|
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k);
|
2021-12-05 03:41:46 +00:00
|
|
|
$status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
|
|
|
|
}
|
|
|
|
return $status;
|
|
|
|
})
|
|
|
|
->filter(function($s) use($filtered) {
|
2022-04-03 06:50:25 +00:00
|
|
|
return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
|
2021-12-05 03:41:46 +00:00
|
|
|
})
|
|
|
|
->values()
|
|
|
|
->toArray();
|
2021-10-22 01:02:15 +00:00
|
|
|
}
|
2021-07-07 07:06:20 +00:00
|
|
|
|
2018-12-09 23:03:53 +00:00
|
|
|
return response()->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function homeTimelineApi(Request $request)
|
|
|
|
{
|
2022-03-31 06:14:48 +00:00
|
|
|
if(!$request->user()) {
|
|
|
|
return response('', 403);
|
2018-12-09 23:03:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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;
|
2022-12-16 07:43:20 +00:00
|
|
|
if(Cache::get($key) == null) {
|
2021-01-09 09:41:17 +00:00
|
|
|
$user->last_active_at = now();
|
|
|
|
$user->save();
|
2022-12-16 07:43:20 +00:00
|
|
|
Cache::put($key, true, 43200);
|
|
|
|
}
|
2018-12-09 23:03:53 +00:00
|
|
|
|
2021-07-16 04:39:40 +00:00
|
|
|
$pid = $user->profile_id;
|
2018-12-09 23:03:53 +00:00
|
|
|
|
2022-12-16 07:43:20 +00:00
|
|
|
$following = Cache::remember('profile:following:'.$pid, 1209600, 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-07-16 04:39:40 +00:00
|
|
|
$filtered = $user ? UserFilterService::filters($user->profile_id) : [];
|
2021-08-05 02:29:21 +00:00
|
|
|
$types = ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'];
|
2021-12-05 03:41:46 +00:00
|
|
|
// $types = ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album', 'text'];
|
2021-08-05 02:29:21 +00:00
|
|
|
|
|
|
|
$textOnlyReplies = false;
|
|
|
|
|
2018-12-09 23:03:53 +00:00
|
|
|
if($min || $max) {
|
|
|
|
$dir = $min ? '>' : '<';
|
|
|
|
$id = $min ?? $max;
|
2022-01-29 03:29:46 +00:00
|
|
|
return 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-07-16 04:39:40 +00:00
|
|
|
)
|
2021-10-07 09:30:23 +00:00
|
|
|
->whereIn('type', $types)
|
2023-02-06 05:50:12 +00:00
|
|
|
->when(!$textOnlyReplies, function($q, $textOnlyReplies) {
|
2021-10-07 09:30:23 +00:00
|
|
|
return $q->whereNull('in_reply_to_id');
|
|
|
|
})
|
2018-12-09 23:03:53 +00:00
|
|
|
->where('id', $dir, $id)
|
|
|
|
->whereIn('profile_id', $following)
|
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)
|
2022-01-29 03:29:46 +00:00
|
|
|
->get()
|
|
|
|
->map(function($s) use ($user) {
|
2023-03-05 15:18:22 +00:00
|
|
|
try {
|
2022-06-05 09:23:41 +00:00
|
|
|
$status = StatusService::get($s->id, false);
|
2022-01-29 03:29:46 +00:00
|
|
|
if(!$status) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-03-05 15:18:22 +00:00
|
|
|
} catch(\Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-29 03:29:46 +00:00
|
|
|
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
|
|
|
|
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
|
2022-01-29 05:06:43 +00:00
|
|
|
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
|
2022-01-29 03:29:46 +00:00
|
|
|
return $status;
|
|
|
|
})
|
|
|
|
->filter(function($s) use($filtered) {
|
|
|
|
return $s && in_array($s['account']['id'], $filtered) == false;
|
|
|
|
})
|
|
|
|
->values()
|
|
|
|
->toArray();
|
2018-12-09 23:03:53 +00:00
|
|
|
} else {
|
2022-01-29 03:29:46 +00:00
|
|
|
return 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-07-16 04:39:40 +00:00
|
|
|
)
|
2021-10-07 09:30:23 +00:00
|
|
|
->whereIn('type', $types)
|
|
|
|
->when(!$textOnlyReplies, function($q, $textOnlyReplies) {
|
|
|
|
return $q->whereNull('in_reply_to_id');
|
|
|
|
})
|
2018-12-09 23:03:53 +00:00
|
|
|
->whereIn('profile_id', $following)
|
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')
|
2022-01-29 03:29:46 +00:00
|
|
|
->limit($limit)
|
|
|
|
->get()
|
|
|
|
->map(function($s) use ($user) {
|
2023-03-05 15:18:22 +00:00
|
|
|
try {
|
|
|
|
$status = StatusService::get($s->id, false);
|
|
|
|
if(!$status) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch(\Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-01-29 03:29:46 +00:00
|
|
|
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
|
|
|
|
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
|
2022-01-29 05:06:43 +00:00
|
|
|
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
|
2022-01-29 03:29:46 +00:00
|
|
|
return $status;
|
|
|
|
})
|
|
|
|
->filter(function($s) use($filtered) {
|
|
|
|
return $s && in_array($s['account']['id'], $filtered) == false;
|
|
|
|
})
|
|
|
|
->values()
|
|
|
|
->toArray();
|
2018-12-09 23:03:53 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-10 20:42:26 +00:00
|
|
|
|
2019-03-14 19:52:36 +00:00
|
|
|
public function networkTimelineApi(Request $request)
|
|
|
|
{
|
2022-03-31 06:14:48 +00:00
|
|
|
if(!$request->user()) {
|
|
|
|
return response('', 403);
|
|
|
|
}
|
|
|
|
|
2021-04-07 03:17:42 +00:00
|
|
|
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();
|
2022-05-21 07:49:39 +00:00
|
|
|
$amin = SnowflakeService::byDate(now()->subDays(config('federation.network_timeline_days_falloff')));
|
2021-04-07 03:17:42 +00:00
|
|
|
|
2021-07-16 04:39:40 +00:00
|
|
|
$filtered = $user ? UserFilterService::filters($user->profile_id) : [];
|
2022-11-28 12:13:12 +00:00
|
|
|
$hideNsfw = config('instance.hide_nsfw_on_public_feeds');
|
2021-07-16 04:39:40 +00:00
|
|
|
|
2022-06-09 10:15:23 +00:00
|
|
|
if(config('instance.timeline.network.cached') == false) {
|
|
|
|
if($min || $max) {
|
|
|
|
$dir = $min ? '>' : '<';
|
|
|
|
$id = $min ?? $max;
|
|
|
|
$timeline = Status::select(
|
|
|
|
'id',
|
|
|
|
'uri',
|
|
|
|
'type',
|
|
|
|
'scope',
|
|
|
|
'created_at',
|
|
|
|
)
|
2022-11-26 05:57:31 +00:00
|
|
|
->where('id', $dir, $id)
|
2022-11-28 12:13:12 +00:00
|
|
|
->when($hideNsfw, function($q, $hideNsfw) {
|
|
|
|
return $q->where('is_nsfw', false);
|
|
|
|
})
|
2022-06-09 10:15:23 +00:00
|
|
|
->whereNull(['in_reply_to_id', 'reblog_of_id'])
|
|
|
|
->whereNotIn('profile_id', $filtered)
|
|
|
|
->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) use ($user) {
|
|
|
|
$status = StatusService::get($s->id);
|
|
|
|
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
|
|
|
|
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
|
|
|
|
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
|
|
|
|
return $status;
|
|
|
|
});
|
|
|
|
$res = $timeline->toArray();
|
|
|
|
} else {
|
|
|
|
$timeline = Status::select(
|
|
|
|
'id',
|
|
|
|
'uri',
|
|
|
|
'type',
|
|
|
|
'scope',
|
|
|
|
'created_at',
|
|
|
|
)
|
|
|
|
->whereNull(['in_reply_to_id', 'reblog_of_id'])
|
|
|
|
->whereNotIn('profile_id', $filtered)
|
2022-11-28 12:13:12 +00:00
|
|
|
->when($hideNsfw, function($q, $hideNsfw) {
|
|
|
|
return $q->where('is_nsfw', false);
|
|
|
|
})
|
2022-06-09 10:15:23 +00:00
|
|
|
->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) use ($user) {
|
|
|
|
$status = StatusService::get($s->id);
|
|
|
|
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
|
|
|
|
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
|
|
|
|
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
|
|
|
|
return $status;
|
|
|
|
});
|
|
|
|
$res = $timeline->toArray();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Cache::remember('api:v1:timelines:network:cache_check', 10368000, function() {
|
|
|
|
if(NetworkTimelineService::count() == 0) {
|
|
|
|
NetworkTimelineService::warmCache(true, 400);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($max) {
|
|
|
|
$feed = NetworkTimelineService::getRankedMaxId($max, $limit);
|
|
|
|
} else if ($min) {
|
|
|
|
$feed = NetworkTimelineService::getRankedMinId($min, $limit);
|
|
|
|
} else {
|
|
|
|
$feed = NetworkTimelineService::get(0, $limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = collect($feed)
|
2022-06-19 13:05:07 +00:00
|
|
|
->take($limit)
|
2022-06-09 10:15:23 +00:00
|
|
|
->map(function($k) use($user) {
|
|
|
|
$status = StatusService::get($k);
|
|
|
|
if($status && isset($status['account']) && $user) {
|
|
|
|
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
|
|
|
|
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $k);
|
|
|
|
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k);
|
|
|
|
$status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
|
|
|
|
}
|
|
|
|
return $status;
|
|
|
|
})
|
|
|
|
->filter(function($s) use($filtered) {
|
|
|
|
return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
|
|
|
|
})
|
|
|
|
->values()
|
|
|
|
->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
|
|
|
|
2021-10-20 10:31:07 +00:00
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
|
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-10-20 10:31:07 +00:00
|
|
|
$res = $ids->filter(function($v) use($pid) {
|
|
|
|
return $v != $pid;
|
|
|
|
})
|
|
|
|
->map(function($id) use($pid) {
|
2021-12-05 03:41:46 +00:00
|
|
|
return RelationshipService::get($pid, $id);
|
2019-02-10 20:42:26 +00:00
|
|
|
});
|
2021-10-20 10:31:07 +00:00
|
|
|
|
2019-02-10 20:42:26 +00:00
|
|
|
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 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
|
|
|
|
2021-07-25 07:39:03 +00:00
|
|
|
$user = $request->user();
|
2021-10-07 09:30:23 +00:00
|
|
|
$profile = AccountService::get($id);
|
|
|
|
abort_if(!$profile, 404);
|
2019-03-04 05:09:35 +00:00
|
|
|
|
|
|
|
$limit = $request->limit ?? 9;
|
|
|
|
$max_id = $request->max_id;
|
|
|
|
$min_id = $request->min_id;
|
2021-07-27 05:59:38 +00:00
|
|
|
$scope = ['photo', 'photo:album', 'video', 'video:album'];
|
2022-01-06 02:31:49 +00:00
|
|
|
$onlyMedia = $request->input('only_media', true);
|
2021-04-07 03:17:42 +00:00
|
|
|
|
2022-01-03 07:59:39 +00:00
|
|
|
if(!$min_id && !$max_id) {
|
|
|
|
$min_id = 1;
|
|
|
|
}
|
|
|
|
|
2021-10-07 09:30:23 +00:00
|
|
|
if($profile['locked']) {
|
2021-07-25 07:39:03 +00:00
|
|
|
if(!$user) {
|
2019-03-04 05:09:35 +00:00
|
|
|
return response()->json([]);
|
|
|
|
}
|
2021-07-25 07:39:03 +00:00
|
|
|
$pid = $user->profile_id;
|
2019-03-04 05:09:35 +00:00
|
|
|
$following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
|
|
|
|
$following = Follower::whereProfileId($pid)->pluck('following_id');
|
|
|
|
return $following->push($pid)->toArray();
|
|
|
|
});
|
2021-11-04 01:40:46 +00:00
|
|
|
$visibility = true == in_array($profile['id'], $following) ? ['public', 'unlisted', 'private'] : [];
|
2019-02-11 04:49:54 +00:00
|
|
|
} else {
|
2021-07-25 07:39:03 +00:00
|
|
|
if($user) {
|
|
|
|
$pid = $user->profile_id;
|
2019-03-04 05:09:35 +00:00
|
|
|
$following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) {
|
|
|
|
$following = Follower::whereProfileId($pid)->pluck('following_id');
|
|
|
|
return $following->push($pid)->toArray();
|
|
|
|
});
|
2021-10-07 09:30:23 +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
|
|
|
$dir = $min_id ? '>' : '<';
|
|
|
|
$id = $min_id ?? $max_id;
|
2021-10-07 09:30:23 +00:00
|
|
|
$res = Status::whereProfileId($profile['id'])
|
2021-07-27 05:59:38 +00:00
|
|
|
->whereNull('in_reply_to_id')
|
|
|
|
->whereNull('reblog_of_id')
|
2021-07-25 07:39:03 +00:00
|
|
|
->whereIn('type', $scope)
|
|
|
|
->where('id', $dir, $id)
|
|
|
|
->whereIn('scope', $visibility)
|
|
|
|
->limit($limit)
|
|
|
|
->orderByDesc('id')
|
|
|
|
->get()
|
|
|
|
->map(function($s) use($user) {
|
2021-10-07 09:30:23 +00:00
|
|
|
try {
|
|
|
|
$status = StatusService::get($s->id, false);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$status = false;
|
|
|
|
}
|
2021-07-27 05:59:38 +00:00
|
|
|
if($user && $status) {
|
2021-10-07 09:30:23 +00:00
|
|
|
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
|
2021-07-25 07:39:03 +00:00
|
|
|
}
|
|
|
|
return $status;
|
2021-07-27 05:59:38 +00:00
|
|
|
})
|
2022-01-06 02:31:49 +00:00
|
|
|
->filter(function($s) use($onlyMedia) {
|
|
|
|
if($onlyMedia) {
|
|
|
|
if(
|
|
|
|
!isset($s['media_attachments']) ||
|
|
|
|
!is_array($s['media_attachments']) ||
|
|
|
|
empty($s['media_attachments'])
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-10-07 09:30:23 +00:00
|
|
|
return $s;
|
2021-07-27 05:59:38 +00:00
|
|
|
})
|
|
|
|
->values();
|
|
|
|
|
2021-07-25 07:39:03 +00:00
|
|
|
return response()->json($res);
|
2019-02-11 04:49:54 +00:00
|
|
|
}
|
2018-11-09 05:23:36 +00:00
|
|
|
}
|