2019-09-14 01:10:22 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
use App\Avatar;
|
|
|
|
use App\Bookmark;
|
|
|
|
use App\Collection;
|
|
|
|
use App\CollectionItem;
|
|
|
|
use App\DirectMessage;
|
|
|
|
use App\Follower;
|
|
|
|
use App\FollowRequest;
|
|
|
|
use App\Hashtag;
|
|
|
|
use App\Http\Controllers\AccountController;
|
2019-09-14 01:10:22 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
2019-09-25 03:37:25 +00:00
|
|
|
use App\Http\Controllers\FollowerController;
|
2019-09-29 04:50:56 +00:00
|
|
|
use App\Http\Controllers\StatusController;
|
2024-03-05 08:58:15 +00:00
|
|
|
use App\Instance;
|
2021-12-05 00:55:38 +00:00
|
|
|
use App\Jobs\AvatarPipeline\AvatarOptimize;
|
2021-12-11 05:03:33 +00:00
|
|
|
use App\Jobs\CommentPipeline\CommentPipeline;
|
2024-03-05 08:58:15 +00:00
|
|
|
use App\Jobs\FollowPipeline\FollowAcceptPipeline;
|
|
|
|
use App\Jobs\FollowPipeline\FollowPipeline;
|
|
|
|
use App\Jobs\FollowPipeline\FollowRejectPipeline;
|
|
|
|
use App\Jobs\FollowPipeline\UnfollowPipeline;
|
|
|
|
use App\Jobs\HomeFeedPipeline\FeedWarmCachePipeline;
|
|
|
|
use App\Jobs\ImageOptimizePipeline\ImageOptimize;
|
2019-09-26 04:54:05 +00:00
|
|
|
use App\Jobs\LikePipeline\LikePipeline;
|
2023-03-23 07:34:37 +00:00
|
|
|
use App\Jobs\MediaPipeline\MediaDeletePipeline;
|
2024-03-05 08:58:15 +00:00
|
|
|
use App\Jobs\MediaPipeline\MediaSyncLicensePipeline;
|
2019-09-29 06:36:02 +00:00
|
|
|
use App\Jobs\SharePipeline\SharePipeline;
|
2021-12-19 07:48:59 +00:00
|
|
|
use App\Jobs\SharePipeline\UndoSharePipeline;
|
2019-09-29 04:50:56 +00:00
|
|
|
use App\Jobs\StatusPipeline\NewStatusPipeline;
|
2019-09-26 04:54:05 +00:00
|
|
|
use App\Jobs\StatusPipeline\StatusDelete;
|
2024-03-05 08:58:15 +00:00
|
|
|
use App\Jobs\VideoPipeline\VideoThumbnail;
|
|
|
|
use App\Like;
|
|
|
|
use App\Media;
|
|
|
|
use App\Models\Conversation;
|
|
|
|
use App\Notification;
|
|
|
|
use App\Profile;
|
|
|
|
use App\Services\AccountService;
|
2024-03-07 10:11:36 +00:00
|
|
|
use App\Services\AdminShadowFilterService;
|
2024-03-05 08:58:15 +00:00
|
|
|
use App\Services\BookmarkService;
|
|
|
|
use App\Services\BouncerService;
|
|
|
|
use App\Services\CollectionService;
|
|
|
|
use App\Services\CustomEmojiService;
|
|
|
|
use App\Services\DiscoverService;
|
|
|
|
use App\Services\FollowerService;
|
|
|
|
use App\Services\HomeTimelineService;
|
|
|
|
use App\Services\InstanceService;
|
|
|
|
use App\Services\LikeService;
|
|
|
|
use App\Services\MarkerService;
|
|
|
|
use App\Services\MediaBlocklistService;
|
|
|
|
use App\Services\MediaPathService;
|
|
|
|
use App\Services\MediaService;
|
|
|
|
use App\Services\NetworkTimelineService;
|
|
|
|
use App\Services\NotificationService;
|
|
|
|
use App\Services\PublicTimelineService;
|
|
|
|
use App\Services\ReblogService;
|
|
|
|
use App\Services\RelationshipService;
|
|
|
|
use App\Services\SnowflakeService;
|
|
|
|
use App\Services\StatusService;
|
|
|
|
use App\Services\UserFilterService;
|
|
|
|
use App\Services\UserRoleService;
|
|
|
|
use App\Status;
|
|
|
|
use App\StatusHashtag;
|
|
|
|
use App\Transformer\Api\Mastodon\v1\AccountTransformer;
|
|
|
|
use App\Transformer\Api\Mastodon\v1\MediaTransformer;
|
|
|
|
use App\Transformer\Api\Mastodon\v1\NotificationTransformer;
|
|
|
|
use App\Transformer\Api\Mastodon\v1\StatusTransformer;
|
|
|
|
use App\Transformer\Api\{
|
|
|
|
RelationshipTransformer,
|
2020-02-07 00:13:10 +00:00
|
|
|
};
|
2024-03-05 08:58:15 +00:00
|
|
|
use App\User;
|
|
|
|
use App\UserFilter;
|
|
|
|
use App\UserSetting;
|
2021-06-11 04:54:31 +00:00
|
|
|
use App\Util\Lexer\Autolink;
|
2022-02-06 01:39:17 +00:00
|
|
|
use App\Util\Lexer\PrettyNumber;
|
2021-12-05 00:55:38 +00:00
|
|
|
use App\Util\Localization\Localization;
|
2024-03-05 08:58:15 +00:00
|
|
|
use App\Util\Media\Filter;
|
2021-12-05 00:55:38 +00:00
|
|
|
use App\Util\Media\License;
|
2024-03-05 08:58:15 +00:00
|
|
|
use Cache;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use DB;
|
|
|
|
use Illuminate\Http\Request;
|
2023-01-29 07:27:23 +00:00
|
|
|
use Illuminate\Support\Facades\RateLimiter;
|
2024-03-05 08:58:15 +00:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Laravel\Passport\Passport;
|
|
|
|
use League\Fractal;
|
|
|
|
use League\Fractal\Serializer\ArraySerializer;
|
2023-01-29 07:27:23 +00:00
|
|
|
use Purify;
|
2024-03-05 08:58:15 +00:00
|
|
|
use Storage;
|
2019-09-14 01:10:22 +00:00
|
|
|
|
2021-04-23 04:46:05 +00:00
|
|
|
class ApiV1Controller extends Controller
|
2019-09-14 03:48:16 +00:00
|
|
|
{
|
2023-11-18 03:50:07 +00:00
|
|
|
protected $fractal;
|
2024-03-05 08:58:15 +00:00
|
|
|
|
|
|
|
const PF_API_ENTITY_KEY = '_pe';
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->fractal = new Fractal\Manager();
|
|
|
|
$this->fractal->setSerializer(new ArraySerializer());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function json($res, $code = 200, $headers = [])
|
|
|
|
{
|
|
|
|
return response()->json($res, $code, $headers, JSON_UNESCAPED_SLASHES);
|
|
|
|
}
|
|
|
|
|
2024-02-08 01:48:17 +00:00
|
|
|
/**
|
|
|
|
* GET /api/v1/apps/verify_credentials
|
|
|
|
*/
|
2023-11-18 03:50:07 +00:00
|
|
|
public function getApp(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
// FIXME: /api/v1/apps/verify_credentials should be accessible with any
|
|
|
|
// valid Access Token, not just a user's access token (i.e., client
|
|
|
|
// credentails grant flow access tokens)
|
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$client = $request->user()->token()->client;
|
|
|
|
$res = [
|
|
|
|
'name' => $client->name,
|
|
|
|
'website' => null,
|
2024-03-05 08:58:15 +00:00
|
|
|
'vapid_key' => null,
|
2023-11-18 03:50:07 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
2024-02-08 01:48:17 +00:00
|
|
|
/**
|
|
|
|
* POST /api/v1/apps
|
|
|
|
*/
|
2023-11-18 03:50:07 +00:00
|
|
|
public function apps(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! config_cache('pixelfed.oauth_enabled'), 404);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
2024-03-05 08:58:15 +00:00
|
|
|
'client_name' => 'required',
|
|
|
|
'redirect_uris' => 'required',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$uris = implode(',', explode('\n', $request->redirect_uris));
|
|
|
|
|
|
|
|
$client = Passport::client()->forceFill([
|
|
|
|
'user_id' => null,
|
|
|
|
'name' => e($request->client_name),
|
|
|
|
'secret' => Str::random(40),
|
|
|
|
'redirect' => $uris,
|
|
|
|
'personal_access_client' => false,
|
|
|
|
'password_client' => false,
|
|
|
|
'revoked' => false,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$client->save();
|
|
|
|
|
|
|
|
$res = [
|
|
|
|
'id' => (string) $client->id,
|
|
|
|
'name' => $client->name,
|
|
|
|
'website' => null,
|
|
|
|
'redirect_uri' => $client->redirect,
|
|
|
|
'client_id' => (string) $client->id,
|
|
|
|
'client_secret' => $client->secret,
|
2024-03-05 08:58:15 +00:00
|
|
|
'vapid_key' => null,
|
2023-11-18 03:50:07 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
return $this->json($res, 200, [
|
2024-03-05 08:58:15 +00:00
|
|
|
'Access-Control-Allow-Origin' => '*',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/accounts/verify_credentials
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return \App\Transformer\Api\AccountTransformer
|
|
|
|
*/
|
|
|
|
public function verifyCredentials(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$user = $request->user();
|
|
|
|
|
|
|
|
abort_if($user->status != null, 403);
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$res = $request->has(self::PF_API_ENTITY_KEY) ? AccountService::get($user->profile_id) : AccountService::getMastodon($user->profile_id);
|
|
|
|
|
|
|
|
$res['source'] = [
|
|
|
|
'privacy' => $res['locked'] ? 'private' : 'public',
|
|
|
|
'sensitive' => false,
|
|
|
|
'language' => $user->language ?? 'en',
|
|
|
|
'note' => strip_tags($res['note']),
|
2024-03-05 08:58:15 +00:00
|
|
|
'fields' => [],
|
2023-11-18 03:50:07 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/accounts/{id}
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return \App\Transformer\Api\AccountTransformer
|
|
|
|
*/
|
|
|
|
public function accountById(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$res = $request->has(self::PF_API_ENTITY_KEY) ? AccountService::get($id, true) : AccountService::getMastodon($id, true);
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $res) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return response()->json(['error' => 'Record not found'], 404);
|
|
|
|
}
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($res && strpos($res['acct'], '@') != -1) {
|
2024-02-07 11:41:12 +00:00
|
|
|
$domain = parse_url($res['url'], PHP_URL_HOST);
|
|
|
|
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
|
|
|
|
}
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PATCH /api/v1/accounts/update_credentials
|
|
|
|
*
|
|
|
|
* @return \App\Transformer\Api\AccountTransformer
|
|
|
|
*/
|
|
|
|
public function accountUpdateCredentials(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (config('pixelfed.bouncer.cloud_ips.ban_api')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort_if(BouncerService::checkIp($request->ip()), 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->validate($request, [
|
2024-03-05 08:58:15 +00:00
|
|
|
'avatar' => 'sometimes|mimetypes:image/jpeg,image/png|max:'.config('pixelfed.max_avatar_size'),
|
|
|
|
'display_name' => 'nullable|string|max:30',
|
|
|
|
'note' => 'nullable|string|max:200',
|
|
|
|
'locked' => 'nullable',
|
|
|
|
'website' => 'nullable|string|max:120',
|
2023-11-18 03:50:07 +00:00
|
|
|
// 'source.privacy' => 'nullable|in:unlisted,public,private',
|
|
|
|
// 'source.sensitive' => 'nullable|boolean'
|
|
|
|
], [
|
|
|
|
'required' => 'The :attribute field is required.',
|
|
|
|
'avatar.mimetypes' => 'The file must be in jpeg or png format',
|
2024-03-05 08:58:15 +00:00
|
|
|
'avatar.max' => 'The :attribute exceeds the file size limit of '.PrettyNumber::size(config('pixelfed.max_avatar_size'), true, false),
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $request->user();
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
$profile = $user->profile;
|
|
|
|
$settings = $user->settings;
|
|
|
|
|
|
|
|
$changes = false;
|
|
|
|
$other = array_merge(AccountService::defaultSettings()['other'], $settings->other ?? []);
|
|
|
|
$syncLicenses = false;
|
|
|
|
$licenseChanged = false;
|
|
|
|
$composeSettings = array_merge(AccountService::defaultSettings()['compose_settings'], $settings->compose_settings ?? []);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('avatar')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$av = Avatar::whereProfileId($profile->id)->first();
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($av) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$currentAvatar = storage_path('app/'.$av->media_path);
|
|
|
|
$file = $request->file('avatar');
|
|
|
|
$path = "public/avatars/{$profile->id}";
|
2024-03-05 08:58:15 +00:00
|
|
|
$name = strtolower(str_random(6)).'.'.$file->guessExtension();
|
2023-11-18 03:50:07 +00:00
|
|
|
$request->file('avatar')->storePubliclyAs($path, $name);
|
|
|
|
$av->media_path = "{$path}/{$name}";
|
|
|
|
$av->save();
|
|
|
|
Cache::forget("avatar:{$profile->id}");
|
|
|
|
Cache::forget('user:account:id:'.$user->id);
|
|
|
|
AvatarOptimize::dispatch($user->profile, $currentAvatar);
|
|
|
|
}
|
|
|
|
$changes = true;
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('source[language]')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$lang = $request->input('source[language]');
|
2024-03-05 08:58:15 +00:00
|
|
|
if (in_array($lang, Localization::languages())) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$user->language = $lang;
|
|
|
|
$changes = true;
|
|
|
|
$other['language'] = $lang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('website')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$website = $request->input('website');
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($website != $profile->website) {
|
|
|
|
if ($website) {
|
|
|
|
if (! strpos($website, '.')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$website = null;
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($website && ! strpos($website, '://')) {
|
|
|
|
$website = 'https://'.$website;
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$host = parse_url($website, PHP_URL_HOST);
|
|
|
|
|
|
|
|
$bannedInstances = InstanceService::getBannedDomains();
|
2024-03-05 08:58:15 +00:00
|
|
|
if (in_array($host, $bannedInstances)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$website = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$profile->website = $website ? $website : null;
|
|
|
|
$changes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('display_name')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$displayName = $request->input('display_name');
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($displayName !== $user->name) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$user->name = $displayName;
|
|
|
|
$profile->name = $displayName;
|
|
|
|
$changes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('note')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$note = $request->input('note');
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($note !== strip_tags($profile->bio)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$profile->bio = Autolink::create()->autolink(strip_tags($note));
|
|
|
|
$changes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('locked')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$locked = $request->input('locked') == 'true';
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($profile->is_private != $locked) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$profile->is_private = $locked;
|
|
|
|
$changes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('reduce_motion')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$reduced = $request->input('reduce_motion');
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($settings->reduce_motion != $reduced) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$settings->reduce_motion = $reduced;
|
|
|
|
$changes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('high_contrast_mode')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$contrast = $request->input('high_contrast_mode');
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($settings->high_contrast_mode != $contrast) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$settings->high_contrast_mode = $contrast;
|
|
|
|
$changes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('video_autoplay')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$autoplay = $request->input('video_autoplay');
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($settings->video_autoplay != $autoplay) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$settings->video_autoplay = $autoplay;
|
|
|
|
$changes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('license')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$license = $request->input('license');
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! in_array($license, License::keys()), 422, 'Invalid media license id');
|
2023-11-18 03:50:07 +00:00
|
|
|
$syncLicenses = $request->input('sync_licenses') == true;
|
|
|
|
abort_if($syncLicenses && Cache::get('pf:settings:mls_recently:'.$user->id) == 2, 422, 'You can only sync licenses twice per 24 hours');
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($composeSettings['default_license'] != $license) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$composeSettings['default_license'] = $license;
|
|
|
|
$licenseChanged = true;
|
|
|
|
$changes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('media_descriptions')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$md = $request->input('media_descriptions') == true;
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($composeSettings['media_descriptions'] != $md) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$composeSettings['media_descriptions'] = $md;
|
|
|
|
$changes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('crawlable')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$crawlable = $request->input('crawlable');
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($settings->crawlable != $crawlable) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$settings->crawlable = $crawlable;
|
|
|
|
$changes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('show_profile_follower_count')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$show_profile_follower_count = $request->input('show_profile_follower_count');
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($settings->show_profile_follower_count != $show_profile_follower_count) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$settings->show_profile_follower_count = $show_profile_follower_count;
|
|
|
|
$changes = true;
|
2024-03-05 08:58:15 +00:00
|
|
|
Cache::forget('pf:acct-trans:hideFollowers:'.$profile->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('show_profile_following_count')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$show_profile_following_count = $request->input('show_profile_following_count');
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($settings->show_profile_following_count != $show_profile_following_count) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$settings->show_profile_following_count = $show_profile_following_count;
|
|
|
|
$changes = true;
|
2024-03-05 08:58:15 +00:00
|
|
|
Cache::forget('pf:acct-trans:hideFollowing:'.$profile->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('public_dm')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$public_dm = $request->input('public_dm');
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($settings->public_dm != $public_dm) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$settings->public_dm = $public_dm;
|
|
|
|
$changes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('source[privacy]')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$scope = $request->input('source[privacy]');
|
2024-03-05 08:58:15 +00:00
|
|
|
if (in_array($scope, ['public', 'private', 'unlisted'])) {
|
|
|
|
if ($composeSettings['default_scope'] != $scope) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$composeSettings['default_scope'] = $profile->is_private ? 'private' : $scope;
|
|
|
|
$changes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('disable_embeds')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$disabledEmbeds = $request->input('disable_embeds');
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($other['disable_embeds'] != $disabledEmbeds) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$other['disable_embeds'] = $disabledEmbeds;
|
|
|
|
$changes = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($changes) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$settings->other = $other;
|
|
|
|
$settings->compose_settings = $composeSettings;
|
|
|
|
$settings->save();
|
|
|
|
$user->save();
|
|
|
|
$profile->save();
|
2024-03-05 08:58:15 +00:00
|
|
|
Cache::forget('profile:settings:'.$profile->id);
|
|
|
|
Cache::forget('user:account:id:'.$profile->user_id);
|
|
|
|
Cache::forget('profile:follower_count:'.$profile->id);
|
|
|
|
Cache::forget('profile:following_count:'.$profile->id);
|
|
|
|
Cache::forget('profile:embed:'.$profile->id);
|
|
|
|
Cache::forget('profile:compose:settings:'.$user->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
Cache::forget('profile:view:'.$user->username);
|
|
|
|
AccountService::del($user->profile_id);
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($syncLicenses && $licenseChanged) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$key = 'pf:settings:mls_recently:'.$user->id;
|
|
|
|
$val = Cache::has($key) ? 2 : 1;
|
|
|
|
Cache::put($key, $val, 86400);
|
|
|
|
MediaSyncLicensePipeline::dispatch($user->id, $request->input('license'));
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has(self::PF_API_ENTITY_KEY)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$res = AccountService::get($user->profile_id, true);
|
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
$res = AccountService::getMastodon($user->profile_id, true);
|
|
|
|
$res['bio'] = strip_tags($res['note']);
|
|
|
|
$res = array_merge($res, $other);
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/accounts/{id}/followers
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return \App\Transformer\Api\AccountTransformer
|
|
|
|
*/
|
|
|
|
public function accountFollowersById(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$account = AccountService::get($id);
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $account, 404);
|
2023-11-18 03:50:07 +00:00
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
$this->validate($request, [
|
2024-03-05 08:58:15 +00:00
|
|
|
'limit' => 'sometimes|integer|min:1',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
$limit = $request->input('limit', 10);
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($limit > 80) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$limit = 80;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
$napi = $request->has(self::PF_API_ENTITY_KEY);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($account && strpos($account['acct'], '@') != -1) {
|
2024-02-07 11:41:12 +00:00
|
|
|
$domain = parse_url($account['url'], PHP_URL_HOST);
|
|
|
|
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (intval($pid) !== intval($account['id'])) {
|
|
|
|
if ($account['locked']) {
|
|
|
|
if (! FollowerService::follows($pid, $account['id'])) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (AccountService::hiddenFollowers($id)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('page') && $request->user()->is_admin == false) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$page = (int) $request->input('page');
|
2024-03-05 08:58:15 +00:00
|
|
|
if (($page * $limit) >= 100) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('page')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$res = DB::table('followers')
|
|
|
|
->select('id', 'profile_id', 'following_id')
|
|
|
|
->whereFollowingId($account['id'])
|
|
|
|
->orderByDesc('id')
|
|
|
|
->simplePaginate($limit)
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($follower) use ($napi) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $napi ? AccountService::get($follower->profile_id, true) : AccountService::getMastodon($follower->profile_id, true);
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($account) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $account && isset($account['id']);
|
|
|
|
})
|
|
|
|
->values()
|
|
|
|
->toArray();
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
$paginator = DB::table('followers')
|
|
|
|
->select('id', 'profile_id', 'following_id')
|
|
|
|
->whereFollowingId($account['id'])
|
|
|
|
->orderByDesc('id')
|
|
|
|
->cursorPaginate($limit)
|
|
|
|
->withQueryString();
|
|
|
|
|
|
|
|
$link = null;
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($paginator->onFirstPage()) {
|
|
|
|
if ($paginator->hasMorePages()) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$paginator->nextPageUrl().'>; rel="prev"';
|
|
|
|
}
|
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($paginator->previousPageUrl()) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$paginator->previousPageUrl().'>; rel="next"';
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($paginator->hasMorePages()) {
|
|
|
|
$link .= ($link ? ', ' : '').'<'.$paginator->nextPageUrl().'>; rel="prev"';
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$res = $paginator->map(function ($follower) use ($napi) {
|
|
|
|
return $napi ? AccountService::get($follower->profile_id, true) : AccountService::getMastodon($follower->profile_id, true);
|
|
|
|
})
|
|
|
|
->filter(function ($account) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $account && isset($account['id']);
|
|
|
|
})
|
|
|
|
->values()
|
|
|
|
->toArray();
|
|
|
|
|
|
|
|
$headers = isset($link) ? ['Link' => $link] : [];
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res, 200, $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/accounts/{id}/following
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return \App\Transformer\Api\AccountTransformer
|
|
|
|
*/
|
|
|
|
public function accountFollowingById(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$account = AccountService::get($id);
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $account, 404);
|
2023-11-18 03:50:07 +00:00
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
$this->validate($request, [
|
2024-03-05 08:58:15 +00:00
|
|
|
'limit' => 'sometimes|integer|min:1',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
$limit = $request->input('limit', 10);
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($limit > 80) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$limit = 80;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
$napi = $request->has(self::PF_API_ENTITY_KEY);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($account && strpos($account['acct'], '@') != -1) {
|
2024-02-07 11:41:12 +00:00
|
|
|
$domain = parse_url($account['url'], PHP_URL_HOST);
|
|
|
|
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (intval($pid) !== intval($account['id'])) {
|
|
|
|
if ($account['locked']) {
|
|
|
|
if (! FollowerService::follows($pid, $account['id'])) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (AccountService::hiddenFollowing($id)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('page') && $request->user()->is_admin == false) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$page = (int) $request->input('page');
|
2024-03-05 08:58:15 +00:00
|
|
|
if (($page * $limit) >= 100) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('page')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$res = DB::table('followers')
|
|
|
|
->select('id', 'profile_id', 'following_id')
|
|
|
|
->whereProfileId($account['id'])
|
|
|
|
->orderByDesc('id')
|
|
|
|
->simplePaginate($limit)
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($follower) use ($napi) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $napi ? AccountService::get($follower->following_id, true) : AccountService::getMastodon($follower->following_id, true);
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($account) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $account && isset($account['id']);
|
|
|
|
})
|
|
|
|
->values()
|
|
|
|
->toArray();
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
$paginator = DB::table('followers')
|
|
|
|
->select('id', 'profile_id', 'following_id')
|
|
|
|
->whereProfileId($account['id'])
|
|
|
|
->orderByDesc('id')
|
|
|
|
->cursorPaginate($limit)
|
|
|
|
->withQueryString();
|
|
|
|
|
|
|
|
$link = null;
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($paginator->onFirstPage()) {
|
|
|
|
if ($paginator->hasMorePages()) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$paginator->nextPageUrl().'>; rel="prev"';
|
|
|
|
}
|
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($paginator->previousPageUrl()) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$paginator->previousPageUrl().'>; rel="next"';
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($paginator->hasMorePages()) {
|
|
|
|
$link .= ($link ? ', ' : '').'<'.$paginator->nextPageUrl().'>; rel="prev"';
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$res = $paginator->map(function ($follower) use ($napi) {
|
|
|
|
return $napi ? AccountService::get($follower->following_id, true) : AccountService::getMastodon($follower->following_id, true);
|
|
|
|
})
|
|
|
|
->filter(function ($account) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $account && isset($account['id']);
|
|
|
|
})
|
|
|
|
->values()
|
|
|
|
->toArray();
|
|
|
|
|
|
|
|
$headers = isset($link) ? ['Link' => $link] : [];
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res, 200, $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/accounts/{id}/statuses
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return \App\Transformer\Api\StatusTransformer
|
|
|
|
*/
|
|
|
|
public function accountStatusesById(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$user = $request->user();
|
|
|
|
|
|
|
|
$this->validate($request, [
|
|
|
|
'only_media' => 'nullable',
|
|
|
|
'media_type' => 'sometimes|string|in:photo,video',
|
|
|
|
'pinned' => 'nullable',
|
|
|
|
'exclude_replies' => 'nullable',
|
2024-03-05 08:58:15 +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,
|
|
|
|
'limit' => 'nullable|integer|min:1',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$napi = $request->has(self::PF_API_ENTITY_KEY);
|
|
|
|
$profile = $napi ? AccountService::get($id, true) : AccountService::getMastodon($id, true);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $profile || ! isset($profile['id']) || ! $user) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json(['error' => 'Account not found'], 404);
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($profile && strpos($profile['acct'], '@') != -1) {
|
2024-02-07 11:41:12 +00:00
|
|
|
$domain = parse_url($profile['url'], PHP_URL_HOST);
|
|
|
|
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
|
|
|
|
}
|
|
|
|
|
2024-02-19 08:36:09 +00:00
|
|
|
$limit = $request->input('limit') ?? 20;
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($limit > 40) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$limit = 40;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
$max_id = $request->max_id;
|
|
|
|
$min_id = $request->min_id;
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $max_id && ! $min_id) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$min_id = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
$scope = $request->only_media == true ?
|
|
|
|
['photo', 'photo:album', 'video', 'video:album'] :
|
|
|
|
['photo', 'photo:album', 'video', 'video:album', 'share', 'reply'];
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->only_media && $request->has('media_type')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$mt = $request->input('media_type');
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($mt == 'video') {
|
2023-11-18 03:50:07 +00:00
|
|
|
$scope = ['video', 'video:album'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (intval($pid) === intval($profile['id'])) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$visibility = ['public', 'unlisted', 'private'];
|
2024-03-05 08:58:15 +00:00
|
|
|
} elseif ($profile['locked']) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$following = FollowerService::follows($pid, $profile['id']);
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $following) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return response('', 403);
|
|
|
|
}
|
|
|
|
$visibility = ['public', 'unlisted', 'private'];
|
|
|
|
} else {
|
|
|
|
$following = FollowerService::follows($pid, $profile['id']);
|
|
|
|
$visibility = $following ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$dir = $min_id ? '>' : '<';
|
|
|
|
$id = $min_id ?? $max_id;
|
|
|
|
$res = Status::whereProfileId($profile['id'])
|
2024-03-05 08:58:15 +00:00
|
|
|
->whereNull('in_reply_to_id')
|
|
|
|
->whereNull('reblog_of_id')
|
|
|
|
->whereIn('type', $scope)
|
|
|
|
->where('id', $dir, $id)
|
|
|
|
->whereIn('scope', $visibility)
|
|
|
|
->limit($limit)
|
|
|
|
->orderByDesc('id')
|
|
|
|
->get()
|
|
|
|
->map(function ($s) use ($user, $napi, $profile) {
|
|
|
|
try {
|
|
|
|
$status = $napi ? StatusService::get($s->id, false) : StatusService::getMastodon($s->id, false);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($profile) {
|
|
|
|
$status['account'] = $profile;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($user && $status) {
|
|
|
|
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
|
|
|
|
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
|
|
|
|
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
})
|
|
|
|
->filter(function ($s) {
|
|
|
|
return $s;
|
|
|
|
})
|
|
|
|
->values();
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v1/accounts/{id}/follow
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return \App\Transformer\Api\RelationshipTransformer
|
|
|
|
*/
|
|
|
|
public function accountFollowById(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('follow'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$user = $request->user();
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if($user->has_roles && ! UserRoleService::can('can-follow', $user->id), 403, 'Invalid permissions for this action');
|
2024-01-11 11:50:11 +00:00
|
|
|
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$target = Profile::where('id', '!=', $user->profile_id)
|
|
|
|
->whereNull('status')
|
|
|
|
->findOrFail($id);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($target && $target->domain) {
|
2024-02-07 11:41:12 +00:00
|
|
|
$domain = $target->domain;
|
|
|
|
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
|
|
|
|
}
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$private = (bool) $target->is_private;
|
|
|
|
$remote = (bool) $target->domain;
|
|
|
|
$blocked = UserFilter::whereUserId($target->id)
|
2024-03-05 08:58:15 +00:00
|
|
|
->whereFilterType('block')
|
|
|
|
->whereFilterableId($user->profile_id)
|
|
|
|
->whereFilterableType('App\Profile')
|
|
|
|
->exists();
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($blocked == true) {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort(400, 'You cannot follow this user.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$isFollowing = Follower::whereProfileId($user->profile_id)
|
|
|
|
->whereFollowingId($target->id)
|
|
|
|
->exists();
|
|
|
|
|
|
|
|
// Following already, return empty relationship
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($isFollowing == true) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$res = RelationshipService::get($user->profile_id, $target->id) ?? [];
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rate limits, max 7500 followers per account
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($user->profile->following_count && $user->profile->following_count >= Follower::MAX_FOLLOWING) {
|
|
|
|
abort(400, 'You cannot follow more than '.Follower::MAX_FOLLOWING.' accounts');
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($private == true) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$follow = FollowRequest::firstOrCreate([
|
|
|
|
'follower_id' => $user->profile_id,
|
2024-03-05 08:58:15 +00:00
|
|
|
'following_id' => $target->id,
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($remote == true && config('federation.activitypub.remoteFollow') == true) {
|
2023-11-18 03:50:07 +00:00
|
|
|
(new FollowerController())->sendFollow($user->profile, $target);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$follower = Follower::firstOrCreate([
|
|
|
|
'profile_id' => $user->profile_id,
|
2024-03-05 08:58:15 +00:00
|
|
|
'following_id' => $target->id,
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($remote == true && config('federation.activitypub.remoteFollow') == true) {
|
2023-11-18 03:50:07 +00:00
|
|
|
(new FollowerController())->sendFollow($user->profile, $target);
|
|
|
|
}
|
|
|
|
FollowPipeline::dispatch($follower)->onQueue('high');
|
|
|
|
}
|
|
|
|
|
|
|
|
RelationshipService::refresh($user->profile_id, $target->id);
|
|
|
|
Cache::forget('profile:following:'.$target->id);
|
|
|
|
Cache::forget('profile:followers:'.$target->id);
|
|
|
|
Cache::forget('profile:following:'.$user->profile_id);
|
|
|
|
Cache::forget('profile:followers:'.$user->profile_id);
|
|
|
|
Cache::forget('api:local:exp:rec:'.$user->profile_id);
|
|
|
|
Cache::forget('user:account:id:'.$target->user_id);
|
|
|
|
Cache::forget('user:account:id:'.$user->id);
|
|
|
|
Cache::forget('profile:follower_count:'.$target->id);
|
|
|
|
Cache::forget('profile:follower_count:'.$user->profile_id);
|
|
|
|
Cache::forget('profile:following_count:'.$target->id);
|
|
|
|
Cache::forget('profile:following_count:'.$user->profile_id);
|
|
|
|
AccountService::del($user->profile_id);
|
|
|
|
AccountService::del($target->id);
|
|
|
|
|
|
|
|
$res = RelationshipService::get($user->profile_id, $target->id);
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v1/accounts/{id}/unfollow
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return \App\Transformer\Api\RelationshipTransformer
|
|
|
|
*/
|
|
|
|
public function accountUnfollowById(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('follow'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$user = $request->user();
|
2024-01-11 11:50:11 +00:00
|
|
|
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$target = Profile::where('id', '!=', $user->profile_id)
|
|
|
|
->whereNull('status')
|
|
|
|
->findOrFail($id);
|
|
|
|
|
|
|
|
$private = (bool) $target->is_private;
|
|
|
|
$remote = (bool) $target->domain;
|
|
|
|
|
|
|
|
$isFollowing = Follower::whereProfileId($user->profile_id)
|
|
|
|
->whereFollowingId($target->id)
|
|
|
|
->exists();
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($isFollowing == false) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$followRequest = FollowRequest::whereFollowerId($user->profile_id)
|
|
|
|
->whereFollowingId($target->id)
|
|
|
|
->first();
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($followRequest) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$followRequest->delete();
|
|
|
|
RelationshipService::refresh($target->id, $user->profile_id);
|
|
|
|
}
|
|
|
|
$resource = new Fractal\Resource\Item($target, new RelationshipTransformer());
|
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
Follower::whereProfileId($user->profile_id)
|
|
|
|
->whereFollowingId($target->id)
|
|
|
|
->delete();
|
|
|
|
|
|
|
|
UnfollowPipeline::dispatch($user->profile_id, $target->id)->onQueue('high');
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($remote == true && config('federation.activitypub.remoteFollow') == true) {
|
2023-11-18 03:50:07 +00:00
|
|
|
(new FollowerController())->sendUndoFollow($user->profile, $target);
|
|
|
|
}
|
|
|
|
|
|
|
|
RelationshipService::refresh($user->profile_id, $target->id);
|
|
|
|
Cache::forget('profile:following:'.$target->id);
|
|
|
|
Cache::forget('profile:followers:'.$target->id);
|
|
|
|
Cache::forget('profile:following:'.$user->profile_id);
|
|
|
|
Cache::forget('profile:followers:'.$user->profile_id);
|
|
|
|
Cache::forget('api:local:exp:rec:'.$user->profile_id);
|
|
|
|
Cache::forget('user:account:id:'.$target->user_id);
|
|
|
|
Cache::forget('user:account:id:'.$user->id);
|
|
|
|
Cache::forget('profile:follower_count:'.$target->id);
|
|
|
|
Cache::forget('profile:follower_count:'.$user->profile_id);
|
|
|
|
Cache::forget('profile:following_count:'.$target->id);
|
|
|
|
Cache::forget('profile:following_count:'.$user->profile_id);
|
|
|
|
AccountService::del($user->profile_id);
|
|
|
|
AccountService::del($target->id);
|
|
|
|
|
|
|
|
$res = RelationshipService::get($user->profile_id, $target->id);
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/accounts/relationships
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param array|int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return \App\Services\RelationshipService
|
|
|
|
*/
|
|
|
|
public function accountRelationshipsById(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user(), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
2024-03-05 08:58:15 +00:00
|
|
|
'id' => 'required|array|min:1',
|
|
|
|
'id.*' => 'required|integer|min:1|max:'.PHP_INT_MAX,
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
2024-02-19 08:36:09 +00:00
|
|
|
$ids = $request->input('id');
|
2024-03-05 08:58:15 +00:00
|
|
|
if (count($ids) > 20) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$ids = collect($ids)->take(20)->toArray();
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
$napi = $request->has(self::PF_API_ENTITY_KEY);
|
|
|
|
$pid = $request->user()->profile_id ?? $request->user()->profile->id;
|
2024-02-19 08:36:09 +00:00
|
|
|
$res = collect($ids)
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($id) use ($pid) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return intval($id) !== intval($pid);
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($id) use ($pid, $napi) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $napi ?
|
|
|
|
RelationshipService::getWithDate($pid, $id) :
|
|
|
|
RelationshipService::get($pid, $id);
|
2024-03-05 08:58:15 +00:00
|
|
|
});
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/accounts/search
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return \App\Transformer\Api\AccountTransformer
|
|
|
|
*/
|
|
|
|
public function accountSearch(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
2024-03-05 08:58:15 +00:00
|
|
|
'q' => 'required|string|min:1|max:30',
|
|
|
|
'limit' => 'nullable|integer|min:1',
|
|
|
|
'resolve' => 'nullable',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $request->user();
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if($user->has_roles && ! UserRoleService::can('can-view-discover', $user->id), 403, 'Invalid permissions for this action');
|
2024-01-11 11:50:11 +00:00
|
|
|
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
$query = $request->input('q');
|
|
|
|
$limit = $request->input('limit') ?? 20;
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($limit > 20) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$limit = 20;
|
|
|
|
}
|
|
|
|
$resolve = $request->boolean('resolve', false);
|
2024-03-05 08:58:15 +00:00
|
|
|
$q = $query.'%';
|
2024-02-19 08:36:09 +00:00
|
|
|
|
|
|
|
$profiles = Profile::where('username', 'like', $q)
|
|
|
|
->orderByDesc('followers_count')
|
|
|
|
->limit($limit)
|
|
|
|
->pluck('id')
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($id) {
|
2024-02-19 08:36:09 +00:00
|
|
|
return AccountService::getMastodon($id);
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($account) {
|
2024-02-19 08:36:09 +00:00
|
|
|
return $account && isset($account['id']);
|
|
|
|
})
|
|
|
|
->values();
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
return $this->json($profiles);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/blocks
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return \App\Transformer\Api\AccountTransformer
|
|
|
|
*/
|
|
|
|
public function accountBlocks(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
2024-03-05 08:58:15 +00:00
|
|
|
'limit' => 'sometimes|integer|min:1',
|
|
|
|
'page' => 'sometimes|integer|min:1',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $request->user();
|
|
|
|
$limit = $request->input('limit') ?? 40;
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($limit > 80) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$limit = 80;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$blocks = UserFilter::select('filterable_id', 'filterable_type', 'filter_type', 'user_id')
|
2023-11-18 03:50:07 +00:00
|
|
|
->whereUserId($user->profile_id)
|
|
|
|
->whereFilterableType('App\Profile')
|
|
|
|
->whereFilterType('block')
|
|
|
|
->orderByDesc('id')
|
|
|
|
->simplePaginate($limit)
|
2024-02-19 08:36:09 +00:00
|
|
|
->withQueryString();
|
|
|
|
|
|
|
|
$res = $blocks->pluck('filterable_id')
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($id) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return AccountService::get($id, true);
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($account) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $account && isset($account['id']);
|
|
|
|
})
|
|
|
|
->values();
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$baseUrl = config('app.url').'/api/v1/blocks?limit='.$limit.'&';
|
2024-02-19 08:36:09 +00:00
|
|
|
$next = $blocks->nextPageUrl();
|
|
|
|
$prev = $blocks->previousPageUrl();
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($next && ! $prev) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$link = '<'.$next.'>; rel="next"';
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $next && $prev) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$link = '<'.$prev.'>; rel="prev"';
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($next && $prev) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$link = '<'.$next.'>; rel="next",<'.$prev.'>; rel="prev"';
|
|
|
|
}
|
|
|
|
$headers = isset($link) ? ['Link' => $link] : [];
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2024-02-19 08:36:09 +00:00
|
|
|
return $this->json($res, 200, $headers);
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v1/accounts/{id}/block
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return \App\Transformer\Api\RelationshipTransformer
|
|
|
|
*/
|
|
|
|
public function accountBlockById(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$user = $request->user();
|
|
|
|
$pid = $user->profile_id ?? $user->profile->id;
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (intval($id) === intval($pid)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort(400, 'You cannot block yourself');
|
|
|
|
}
|
|
|
|
|
|
|
|
$profile = Profile::findOrFail($id);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($profile->user && $profile->user->is_admin == true) {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort(400, 'You cannot block an admin');
|
|
|
|
}
|
|
|
|
|
|
|
|
$count = UserFilterService::blockCount($pid);
|
|
|
|
$maxLimit = intval(config('instance.user_filters.max_user_blocks'));
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($count == 0) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$filterCount = UserFilter::whereUserId($pid)
|
|
|
|
->whereFilterType('block')
|
|
|
|
->get()
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($rec) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return AccountService::get($rec->filterable_id, true);
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($account) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $account && isset($account['id']);
|
|
|
|
})
|
|
|
|
->values()
|
|
|
|
->count();
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if($filterCount >= $maxLimit, 422, AccountController::FILTER_LIMIT_BLOCK_TEXT.$maxLimit.' accounts');
|
2023-11-18 03:50:07 +00:00
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if($count >= $maxLimit, 422, AccountController::FILTER_LIMIT_BLOCK_TEXT.$maxLimit.' accounts');
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$followed = Follower::whereProfileId($profile->id)->whereFollowingId($pid)->first();
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($followed) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$followed->delete();
|
|
|
|
$profile->following_count = Follower::whereProfileId($profile->id)->count();
|
|
|
|
$profile->save();
|
|
|
|
$selfProfile = $user->profile;
|
|
|
|
$selfProfile->followers_count = Follower::whereFollowingId($pid)->count();
|
|
|
|
$selfProfile->save();
|
|
|
|
FollowerService::remove($profile->id, $pid);
|
|
|
|
AccountService::del($pid);
|
|
|
|
AccountService::del($profile->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$following = Follower::whereProfileId($pid)->whereFollowingId($profile->id)->first();
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($following) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$following->delete();
|
|
|
|
$profile->followers_count = Follower::whereFollowingId($profile->id)->count();
|
|
|
|
$profile->save();
|
|
|
|
$selfProfile = $user->profile;
|
|
|
|
$selfProfile->following_count = Follower::whereProfileId($pid)->count();
|
|
|
|
$selfProfile->save();
|
|
|
|
FollowerService::remove($pid, $profile->pid);
|
|
|
|
AccountService::del($pid);
|
|
|
|
AccountService::del($profile->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
Notification::whereProfileId($pid)
|
|
|
|
->whereActorId($profile->id)
|
|
|
|
->get()
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($n) use ($pid) {
|
2023-11-18 03:50:07 +00:00
|
|
|
NotificationService::del($pid, $n['id']);
|
|
|
|
$n->forceDelete();
|
2024-03-05 08:58:15 +00:00
|
|
|
});
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$filter = UserFilter::firstOrCreate([
|
2024-03-05 08:58:15 +00:00
|
|
|
'user_id' => $pid,
|
|
|
|
'filterable_id' => $profile->id,
|
2023-11-18 03:50:07 +00:00
|
|
|
'filterable_type' => 'App\Profile',
|
2024-03-05 08:58:15 +00:00
|
|
|
'filter_type' => 'block',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
UserFilterService::block($pid, $id);
|
|
|
|
RelationshipService::refresh($pid, $id);
|
|
|
|
$resource = new Fractal\Resource\Item($profile, new RelationshipTransformer());
|
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v1/accounts/{id}/unblock
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return \App\Transformer\Api\RelationshipTransformer
|
|
|
|
*/
|
|
|
|
public function accountUnblockById(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$user = $request->user();
|
|
|
|
$pid = $user->profile_id ?? $user->profile->id;
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (intval($id) === intval($pid)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort(400, 'You cannot unblock yourself');
|
|
|
|
}
|
|
|
|
|
|
|
|
$profile = Profile::findOrFail($id);
|
|
|
|
|
|
|
|
$filter = UserFilter::whereUserId($pid)
|
|
|
|
->whereFilterableId($profile->id)
|
|
|
|
->whereFilterableType('App\Profile')
|
|
|
|
->whereFilterType('block')
|
|
|
|
->first();
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($filter) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$filter->delete();
|
|
|
|
UserFilterService::unblock($pid, $profile->id);
|
|
|
|
RelationshipService::refresh($pid, $id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$resource = new Fractal\Resource\Item($profile, new RelationshipTransformer());
|
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/custom_emojis
|
|
|
|
*
|
|
|
|
* Return custom emoji
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function customEmojis()
|
|
|
|
{
|
|
|
|
return response(CustomEmojiService::all())->header('Content-Type', 'application/json');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/domain_blocks
|
|
|
|
*
|
|
|
|
* Return empty array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function accountDomainBlocks(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return response()->json([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/endorsements
|
|
|
|
*
|
|
|
|
* Return empty array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function accountEndorsements(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return response()->json([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/favourites
|
|
|
|
*
|
|
|
|
* Returns collection of liked statuses
|
|
|
|
*
|
|
|
|
* @return \App\Transformer\Api\StatusTransformer
|
|
|
|
*/
|
|
|
|
public function accountFavourites(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$this->validate($request, [
|
2024-03-05 08:58:15 +00:00
|
|
|
'limit' => 'sometimes|integer|min:1',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $request->user();
|
|
|
|
$maxId = $request->input('max_id');
|
|
|
|
$minId = $request->input('min_id');
|
|
|
|
$limit = $request->input('limit') ?? 10;
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($limit > 40) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$limit = 40;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$res = Like::whereProfileId($user->profile_id)
|
2024-03-05 08:58:15 +00:00
|
|
|
->when($maxId, function ($q, $maxId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $q->where('id', '<', $maxId);
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->when($minId, function ($q, $minId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $q->where('id', '>', $minId);
|
|
|
|
})
|
|
|
|
->orderByDesc('id')
|
|
|
|
->limit($limit)
|
|
|
|
->get()
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($like) {
|
|
|
|
$status = StatusService::getMastodon($like['status_id'], false);
|
2023-11-18 03:50:07 +00:00
|
|
|
$status['favourited'] = true;
|
|
|
|
$status['like_id'] = $like->id;
|
|
|
|
$status['liked_at'] = str_replace('+00:00', 'Z', $like->created_at->format(DATE_RFC3339_EXTENDED));
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $status;
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($status) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $status && isset($status['id'], $status['like_id']);
|
|
|
|
})
|
|
|
|
->values();
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($res->count()) {
|
|
|
|
$ids = $res->map(function ($status) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $status['like_id'];
|
|
|
|
});
|
|
|
|
$max = $ids->max();
|
|
|
|
$min = $ids->min();
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$baseUrl = config('app.url').'/api/v1/favourites?limit='.$limit.'&';
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$baseUrl.'max_id='.$max.'>; rel="next",<'.$baseUrl.'min_id='.$min.'>; rel="prev"';
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res, 200, ['Link' => $link]);
|
|
|
|
} else {
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v1/statuses/{id}/favourite
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return \App\Transformer\Api\StatusTransformer
|
|
|
|
*/
|
|
|
|
public function statusFavouriteById(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$user = $request->user();
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if($user->has_roles && ! UserRoleService::can('can-like', $user->id), 403, 'Invalid permissions for this action');
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$status = StatusService::getMastodon($id, false);
|
|
|
|
|
2024-02-07 11:41:12 +00:00
|
|
|
abort_unless($status, 404);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($status && isset($status['account'], $status['account']['acct']) && strpos($status['account']['acct'], '@') != -1) {
|
2024-02-07 11:41:12 +00:00
|
|
|
$domain = parse_url($status['account']['url'], PHP_URL_HOST);
|
|
|
|
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$spid = $status['account']['id'];
|
|
|
|
|
2024-02-07 11:41:12 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (intval($spid) !== intval($user->profile_id)) {
|
|
|
|
if ($status['visibility'] == 'private') {
|
|
|
|
abort_if(! FollowerService::follows($user->profile_id, $spid), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! in_array($status['visibility'], ['public', 'unlisted']), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abort_if(
|
|
|
|
Like::whereProfileId($user->profile_id)
|
|
|
|
->where('created_at', '>', now()->subDay())
|
|
|
|
->count() >= Like::MAX_PER_DAY,
|
|
|
|
429
|
|
|
|
);
|
|
|
|
|
|
|
|
$blocks = UserFilterService::blocks($spid);
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($blocks && in_array($user->profile_id, $blocks)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort(422);
|
|
|
|
}
|
|
|
|
|
|
|
|
$like = Like::firstOrCreate([
|
|
|
|
'profile_id' => $user->profile_id,
|
2024-03-05 08:58:15 +00:00
|
|
|
'status_id' => $status['id'],
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($like->wasRecentlyCreated == true) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$like->status_profile_id = $spid;
|
2024-03-05 08:58:15 +00:00
|
|
|
$like->is_comment = ! empty($status['in_reply_to_id']);
|
2023-11-18 03:50:07 +00:00
|
|
|
$like->save();
|
|
|
|
Status::findOrFail($status['id'])->update([
|
2024-03-05 08:58:15 +00:00
|
|
|
'likes_count' => ($status['favourites_count'] ?? 0) + 1,
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
LikePipeline::dispatch($like)->onQueue('feed');
|
|
|
|
}
|
|
|
|
|
|
|
|
$status['favourited'] = true;
|
|
|
|
$status['favourites_count'] = $status['favourites_count'] + 1;
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v1/statuses/{id}/unfavourite
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return \App\Transformer\Api\StatusTransformer
|
|
|
|
*/
|
|
|
|
public function statusUnfavouriteById(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$user = $request->user();
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if($user->has_roles && ! UserRoleService::can('can-like', $user->id), 403, 'Invalid permissions for this action');
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$status = Status::findOrFail($id);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (intval($status->profile_id) !== intval($user->profile_id)) {
|
|
|
|
if ($status->scope == 'private') {
|
|
|
|
abort_if(! $status->profile->followedBy($user->profile), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! in_array($status->scope, ['public', 'unlisted']), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$like = Like::whereProfileId($user->profile_id)
|
|
|
|
->whereStatusId($status->id)
|
|
|
|
->first();
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($like) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$like->forceDelete();
|
|
|
|
$status->likes_count = $status->likes_count > 1 ? $status->likes_count - 1 : 0;
|
|
|
|
$status->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
StatusService::del($status->id);
|
|
|
|
|
|
|
|
$res = StatusService::getMastodon($status->id, false);
|
|
|
|
$res['favourited'] = false;
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/filters
|
|
|
|
*
|
|
|
|
* Return empty response since we filter server side
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function accountFilters(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
return response()->json([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/follow_requests
|
|
|
|
*
|
|
|
|
* Return array of Accounts that have sent follow requests
|
|
|
|
*
|
|
|
|
* @return \App\Transformer\Api\AccountTransformer
|
|
|
|
*/
|
|
|
|
public function accountFollowRequests(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$this->validate($request, [
|
2024-03-05 08:58:15 +00:00
|
|
|
'limit' => 'sometimes|integer|min:1|max:100',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $request->user();
|
|
|
|
|
|
|
|
$res = FollowRequest::whereFollowingId($user->profile->id)
|
|
|
|
->limit($request->input('limit', 40))
|
|
|
|
->pluck('follower_id')
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($id) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return AccountService::getMastodon($id, true);
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($acct) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $acct && isset($acct['id']);
|
|
|
|
})
|
|
|
|
->values();
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v1/follow_requests/{id}/authorize
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function accountFollowRequestAccept(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('follow'), 403);
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
$target = AccountService::getMastodon($id);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $target) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return response()->json(['error' => 'Record not found'], 404);
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($target && strpos($target['acct'], '@') != -1) {
|
2024-02-07 11:41:12 +00:00
|
|
|
$domain = parse_url($target['url'], PHP_URL_HOST);
|
|
|
|
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
|
|
|
|
}
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$followRequest = FollowRequest::whereFollowingId($pid)->whereFollowerId($id)->first();
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $followRequest) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return response()->json(['error' => 'Record not found'], 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$follower = $followRequest->follower;
|
|
|
|
$follow = new Follower();
|
|
|
|
$follow->profile_id = $follower->id;
|
|
|
|
$follow->following_id = $pid;
|
|
|
|
$follow->save();
|
|
|
|
|
|
|
|
$profile = Profile::findOrFail($pid);
|
|
|
|
$profile->followers_count++;
|
|
|
|
$profile->save();
|
|
|
|
AccountService::del($profile->id);
|
|
|
|
|
|
|
|
$profile = Profile::findOrFail($follower->id);
|
|
|
|
$profile->following_count++;
|
|
|
|
$profile->save();
|
|
|
|
AccountService::del($profile->id);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($follower->domain != null && $follower->private_key === null) {
|
2023-11-18 03:50:07 +00:00
|
|
|
FollowAcceptPipeline::dispatch($followRequest)->onQueue('follow');
|
|
|
|
} else {
|
|
|
|
FollowPipeline::dispatch($follow);
|
|
|
|
$followRequest->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
RelationshipService::refresh($pid, $id);
|
|
|
|
$res = RelationshipService::get($pid, $id);
|
|
|
|
$res['followed_by'] = true;
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v1/follow_requests/{id}/reject
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function accountFollowRequestReject(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('follow'), 403);
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
$target = AccountService::getMastodon($id);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $target) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return response()->json(['error' => 'Record not found'], 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$followRequest = FollowRequest::whereFollowingId($pid)->whereFollowerId($id)->first();
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $followRequest) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return response()->json(['error' => 'Record not found'], 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$follower = $followRequest->follower;
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($follower->domain != null && $follower->private_key === null) {
|
2023-11-18 03:50:07 +00:00
|
|
|
FollowRejectPipeline::dispatch($followRequest)->onQueue('follow');
|
|
|
|
} else {
|
|
|
|
$followRequest->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
RelationshipService::refresh($pid, $id);
|
|
|
|
$res = RelationshipService::get($pid, $id);
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/suggestions
|
|
|
|
*
|
|
|
|
* Return empty array as we don't support suggestions
|
|
|
|
*
|
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function accountSuggestions(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
// todo
|
|
|
|
|
|
|
|
return response()->json([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/instance
|
|
|
|
*
|
|
|
|
* Information about the server.
|
|
|
|
*
|
|
|
|
* @return Instance
|
|
|
|
*/
|
|
|
|
public function instance(Request $request)
|
|
|
|
{
|
|
|
|
$res = Cache::remember('api:v1:instance-data-response-v1', 1800, function () {
|
|
|
|
$contact = Cache::remember('api:v1:instance-data:contact', 604800, function () {
|
2024-03-05 08:58:15 +00:00
|
|
|
if (config_cache('instance.admin.pid')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return AccountService::getMastodon(config_cache('instance.admin.pid'), true);
|
|
|
|
}
|
|
|
|
$admin = User::whereIsAdmin(true)->first();
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $admin && isset($admin->profile_id) ?
|
|
|
|
AccountService::getMastodon($admin->profile_id, true) :
|
|
|
|
null;
|
|
|
|
});
|
|
|
|
|
|
|
|
$stats = Cache::remember('api:v1:instance-data:stats', 43200, function () {
|
|
|
|
return [
|
|
|
|
'user_count' => User::count(),
|
|
|
|
'status_count' => Status::whereNull('uri')->count(),
|
|
|
|
'domain_count' => Instance::count(),
|
|
|
|
];
|
|
|
|
});
|
|
|
|
|
|
|
|
$rules = Cache::remember('api:v1:instance-data:rules', 604800, function () {
|
|
|
|
return config_cache('app.rules') ?
|
|
|
|
collect(json_decode(config_cache('app.rules'), true))
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($rule, $key) {
|
|
|
|
$id = $key + 1;
|
|
|
|
|
|
|
|
return [
|
|
|
|
'id' => "{$id}",
|
|
|
|
'text' => $rule,
|
|
|
|
];
|
|
|
|
})
|
|
|
|
->toArray() : [];
|
2023-11-18 03:50:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return [
|
|
|
|
'uri' => config('pixelfed.domain.app'),
|
|
|
|
'title' => config('app.name'),
|
|
|
|
'short_description' => config_cache('app.short_description'),
|
|
|
|
'description' => config_cache('app.description'),
|
|
|
|
'email' => config('instance.email'),
|
2024-03-05 08:58:15 +00:00
|
|
|
'version' => '3.5.3 (compatible; Pixelfed '.config('pixelfed.version').')',
|
2023-11-18 03:50:07 +00:00
|
|
|
'urls' => [
|
2024-02-19 11:00:31 +00:00
|
|
|
'streaming_api' => null,
|
2023-11-18 03:50:07 +00:00
|
|
|
],
|
|
|
|
'stats' => $stats,
|
|
|
|
'thumbnail' => config_cache('app.banner_image') ?? url(Storage::url('public/headers/default.jpg')),
|
|
|
|
'languages' => [config('app.locale')],
|
|
|
|
'registrations' => (bool) config_cache('pixelfed.open_registration'),
|
2024-02-19 11:00:31 +00:00
|
|
|
'approval_required' => (bool) config_cache('instance.curated_registration.enabled'),
|
2023-11-18 03:50:07 +00:00
|
|
|
'contact_account' => $contact,
|
|
|
|
'rules' => $rules,
|
|
|
|
'configuration' => [
|
|
|
|
'media_attachments' => [
|
|
|
|
'image_matrix_limit' => 16777216,
|
|
|
|
'image_size_limit' => config('pixelfed.max_photo_size') * 1024,
|
|
|
|
'supported_mime_types' => explode(',', config('pixelfed.media_types')),
|
|
|
|
'video_frame_rate_limit' => 120,
|
|
|
|
'video_matrix_limit' => 2304000,
|
|
|
|
'video_size_limit' => config('pixelfed.max_photo_size') * 1024,
|
|
|
|
],
|
|
|
|
'polls' => [
|
|
|
|
'max_characters_per_option' => 50,
|
|
|
|
'max_expiration' => 2629746,
|
|
|
|
'max_options' => 4,
|
2024-03-05 08:58:15 +00:00
|
|
|
'min_expiration' => 300,
|
2023-11-18 03:50:07 +00:00
|
|
|
],
|
|
|
|
'statuses' => [
|
|
|
|
'characters_reserved_per_url' => 23,
|
|
|
|
'max_characters' => (int) config('pixelfed.max_caption_length'),
|
2024-03-05 08:58:15 +00:00
|
|
|
'max_media_attachments' => (int) config('pixelfed.max_album_length'),
|
|
|
|
],
|
|
|
|
],
|
2023-11-18 03:50:07 +00:00
|
|
|
];
|
|
|
|
});
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/lists
|
|
|
|
*
|
|
|
|
* Return empty array as we don't support lists
|
|
|
|
*
|
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function accountLists(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
return response()->json([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/accounts/{id}/lists
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function accountListsById(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
return response()->json([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v1/media
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return MediaTransformer
|
|
|
|
*/
|
|
|
|
public function mediaUpload(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
|
|
|
'file.*' => [
|
|
|
|
'required_without:file',
|
2024-03-05 08:58:15 +00:00
|
|
|
'mimetypes:'.config_cache('pixelfed.media_types'),
|
|
|
|
'max:'.config_cache('pixelfed.max_photo_size'),
|
2023-11-18 03:50:07 +00:00
|
|
|
],
|
|
|
|
'file' => [
|
|
|
|
'required_without:file.*',
|
2024-03-05 08:58:15 +00:00
|
|
|
'mimetypes:'.config_cache('pixelfed.media_types'),
|
|
|
|
'max:'.config_cache('pixelfed.max_photo_size'),
|
2023-11-18 03:50:07 +00:00
|
|
|
],
|
2024-03-05 08:58:15 +00:00
|
|
|
'filter_name' => 'nullable|string|max:24',
|
|
|
|
'filter_class' => 'nullable|alpha_dash|max:24',
|
|
|
|
'description' => 'nullable|string|max:'.config_cache('pixelfed.max_altext_length'),
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $request->user();
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if($user->has_roles && ! UserRoleService::can('can-post', $user->id), 403, 'Invalid permissions for this action');
|
2023-12-27 09:51:47 +00:00
|
|
|
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($user->last_active_at == null) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (empty($request->file('file'))) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return response('', 422);
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$limitKey = 'compose:rate-limit:media-upload:'.$user->id;
|
2023-11-18 03:50:07 +00:00
|
|
|
$limitTtl = now()->addMinutes(15);
|
2024-03-05 08:58:15 +00:00
|
|
|
$limitReached = Cache::remember($limitKey, $limitTtl, function () use ($user) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$dailyLimit = Media::whereUserId($user->id)->where('created_at', '>', now()->subDays(1))->count();
|
|
|
|
|
|
|
|
return $dailyLimit >= 1250;
|
|
|
|
});
|
|
|
|
abort_if($limitReached == true, 429);
|
|
|
|
|
|
|
|
$profile = $user->profile;
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (config_cache('pixelfed.enforce_account_limit') == true) {
|
|
|
|
$size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function () use ($user) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return Media::whereUserId($user->id)->sum('size') / 1000;
|
|
|
|
});
|
|
|
|
$limit = (int) config_cache('pixelfed.max_account_size');
|
|
|
|
if ($size >= $limit) {
|
2024-03-05 08:58:15 +00:00
|
|
|
abort(403, 'Account size limit reached.');
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$filterClass = in_array($request->input('filter_class'), Filter::classes()) ? $request->input('filter_class') : null;
|
|
|
|
$filterName = in_array($request->input('filter_name'), Filter::names()) ? $request->input('filter_name') : null;
|
|
|
|
|
|
|
|
$photo = $request->file('file');
|
|
|
|
|
|
|
|
$mimes = explode(',', config_cache('pixelfed.media_types'));
|
2024-03-05 08:58:15 +00:00
|
|
|
if (in_array($photo->getMimeType(), $mimes) == false) {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort(403, 'Invalid or unsupported mime type.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$storagePath = MediaPathService::get($user, 2);
|
|
|
|
$path = $photo->storePublicly($storagePath);
|
|
|
|
$hash = \hash_file('sha256', $photo);
|
|
|
|
$license = null;
|
|
|
|
$mime = $photo->getMimeType();
|
|
|
|
|
|
|
|
// if($photo->getMimeType() == 'image/heic') {
|
|
|
|
// abort_if(config('image.driver') !== 'imagick', 422, 'Invalid media type');
|
|
|
|
// abort_if(!in_array('HEIC', \Imagick::queryformats()), 422, 'Unsupported media type');
|
|
|
|
// $oldPath = $path;
|
|
|
|
// $path = str_replace('.heic', '.jpg', $path);
|
|
|
|
// $mime = 'image/jpeg';
|
|
|
|
// \Image::make($photo)->save(storage_path("app/{$path}"));
|
|
|
|
// @unlink(storage_path("app/{$oldPath}"));
|
|
|
|
// }
|
|
|
|
|
|
|
|
$settings = UserSetting::whereUserId($user->id)->first();
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($settings && ! empty($settings->compose_settings)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$compose = $settings->compose_settings;
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (isset($compose['default_license']) && $compose['default_license'] != 1) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$license = $compose['default_license'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abort_if(MediaBlocklistService::exists($hash) == true, 451);
|
|
|
|
|
|
|
|
$media = new Media();
|
|
|
|
$media->status_id = null;
|
|
|
|
$media->profile_id = $profile->id;
|
|
|
|
$media->user_id = $user->id;
|
|
|
|
$media->media_path = $path;
|
|
|
|
$media->original_sha256 = $hash;
|
|
|
|
$media->size = $photo->getSize();
|
|
|
|
$media->mime = $mime;
|
|
|
|
$media->caption = $request->input('description');
|
|
|
|
$media->filter_class = $filterClass;
|
|
|
|
$media->filter_name = $filterName;
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($license) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$media->license = $license;
|
|
|
|
}
|
|
|
|
$media->save();
|
|
|
|
|
|
|
|
switch ($media->mime) {
|
|
|
|
case 'image/jpeg':
|
|
|
|
case 'image/png':
|
|
|
|
ImageOptimize::dispatch($media)->onQueue('mmo');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'video/mp4':
|
|
|
|
VideoThumbnail::dispatch($media)->onQueue('mmo');
|
|
|
|
$preview_url = '/storage/no-preview.png';
|
|
|
|
$url = '/storage/no-preview.png';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cache::forget($limitKey);
|
|
|
|
$resource = new Fractal\Resource\Item($media, new MediaTransformer());
|
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
2024-03-05 08:58:15 +00:00
|
|
|
$res['preview_url'] = $media->url().'?v='.time();
|
|
|
|
$res['url'] = $media->url().'?v='.time();
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PUT /api/v1/media/{id}
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return MediaTransformer
|
|
|
|
*/
|
|
|
|
public function mediaUpdate(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
2024-03-05 08:58:15 +00:00
|
|
|
'description' => 'nullable|string|max:'.config_cache('pixelfed.max_altext_length'),
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $request->user();
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if($user->has_roles && ! UserRoleService::can('can-post', $user->id), 403, 'Invalid permissions for this action');
|
2024-01-11 11:40:25 +00:00
|
|
|
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$media = Media::whereUserId($user->id)
|
|
|
|
->whereProfileId($user->profile_id)
|
|
|
|
->findOrFail($id);
|
|
|
|
|
|
|
|
$executed = RateLimiter::attempt(
|
|
|
|
'media:update:'.$user->id,
|
|
|
|
10,
|
2024-03-05 08:58:15 +00:00
|
|
|
function () use ($media, $request) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$caption = Purify::clean($request->input('description'));
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($caption != $media->caption) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$media->caption = $caption;
|
|
|
|
$media->save();
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($media->status_id) {
|
2023-11-18 03:50:07 +00:00
|
|
|
MediaService::del($media->status_id);
|
|
|
|
StatusService::del($media->status_id);
|
|
|
|
}
|
|
|
|
}
|
2024-03-05 08:58:15 +00:00
|
|
|
});
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $executed) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return response()->json([
|
2024-03-05 08:58:15 +00:00
|
|
|
'error' => 'Too many attempts. Try again in a few minutes.',
|
2023-11-18 03:50:07 +00:00
|
|
|
], 429);
|
2024-03-05 08:58:15 +00:00
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$fractal = new Fractal\Manager();
|
|
|
|
$fractal->setSerializer(new ArraySerializer());
|
|
|
|
$resource = new Fractal\Resource\Item($media, new MediaTransformer());
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($fractal->createData($resource)->toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/media/{id}
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return MediaTransformer
|
|
|
|
*/
|
|
|
|
public function mediaGet(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$user = $request->user();
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if($user->has_roles && ! UserRoleService::can('can-post', $user->id), 403, 'Invalid permissions for this action');
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$media = Media::whereUserId($user->id)
|
|
|
|
->whereNull('status_id')
|
|
|
|
->findOrFail($id);
|
|
|
|
|
|
|
|
$resource = new Fractal\Resource\Item($media, new MediaTransformer());
|
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v2/media
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return MediaTransformer
|
|
|
|
*/
|
|
|
|
public function mediaUploadV2(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
|
|
|
'file.*' => [
|
|
|
|
'required_without:file',
|
2024-03-05 08:58:15 +00:00
|
|
|
'mimetypes:'.config_cache('pixelfed.media_types'),
|
|
|
|
'max:'.config_cache('pixelfed.max_photo_size'),
|
2023-11-18 03:50:07 +00:00
|
|
|
],
|
|
|
|
'file' => [
|
|
|
|
'required_without:file.*',
|
2024-03-05 08:58:15 +00:00
|
|
|
'mimetypes:'.config_cache('pixelfed.media_types'),
|
|
|
|
'max:'.config_cache('pixelfed.max_photo_size'),
|
2023-11-18 03:50:07 +00:00
|
|
|
],
|
2024-03-05 08:58:15 +00:00
|
|
|
'filter_name' => 'nullable|string|max:24',
|
|
|
|
'filter_class' => 'nullable|alpha_dash|max:24',
|
|
|
|
'description' => 'nullable|string|max:'.config_cache('pixelfed.max_altext_length'),
|
|
|
|
'replace_id' => 'sometimes',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $request->user();
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if($user->has_roles && ! UserRoleService::can('can-post', $user->id), 403, 'Invalid permissions for this action');
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($user->last_active_at == null) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (empty($request->file('file'))) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return response('', 422);
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$limitKey = 'compose:rate-limit:media-upload:'.$user->id;
|
2023-11-18 03:50:07 +00:00
|
|
|
$limitTtl = now()->addMinutes(15);
|
2024-03-05 08:58:15 +00:00
|
|
|
$limitReached = Cache::remember($limitKey, $limitTtl, function () use ($user) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$dailyLimit = Media::whereUserId($user->id)->where('created_at', '>', now()->subDays(1))->count();
|
|
|
|
|
|
|
|
return $dailyLimit >= 1250;
|
|
|
|
});
|
|
|
|
abort_if($limitReached == true, 429);
|
|
|
|
|
|
|
|
$profile = $user->profile;
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (config_cache('pixelfed.enforce_account_limit') == true) {
|
|
|
|
$size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function () use ($user) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return Media::whereUserId($user->id)->sum('size') / 1000;
|
|
|
|
});
|
|
|
|
$limit = (int) config_cache('pixelfed.max_account_size');
|
|
|
|
if ($size >= $limit) {
|
2024-03-05 08:58:15 +00:00
|
|
|
abort(403, 'Account size limit reached.');
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$filterClass = in_array($request->input('filter_class'), Filter::classes()) ? $request->input('filter_class') : null;
|
|
|
|
$filterName = in_array($request->input('filter_name'), Filter::names()) ? $request->input('filter_name') : null;
|
|
|
|
|
|
|
|
$photo = $request->file('file');
|
|
|
|
|
|
|
|
$mimes = explode(',', config_cache('pixelfed.media_types'));
|
2024-03-05 08:58:15 +00:00
|
|
|
if (in_array($photo->getMimeType(), $mimes) == false) {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort(403, 'Invalid or unsupported mime type.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$storagePath = MediaPathService::get($user, 2);
|
|
|
|
$path = $photo->storePublicly($storagePath);
|
|
|
|
$hash = \hash_file('sha256', $photo);
|
|
|
|
$license = null;
|
|
|
|
$mime = $photo->getMimeType();
|
|
|
|
|
|
|
|
$settings = UserSetting::whereUserId($user->id)->first();
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($settings && ! empty($settings->compose_settings)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$compose = $settings->compose_settings;
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (isset($compose['default_license']) && $compose['default_license'] != 1) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$license = $compose['default_license'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abort_if(MediaBlocklistService::exists($hash) == true, 451);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('replace_id')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$rpid = $request->input('replace_id');
|
|
|
|
$removeMedia = Media::whereNull('status_id')
|
|
|
|
->whereUserId($user->id)
|
|
|
|
->whereProfileId($profile->id)
|
|
|
|
->where('created_at', '>', now()->subHours(2))
|
|
|
|
->find($rpid);
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($removeMedia) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$dateTime = Carbon::now();
|
|
|
|
MediaDeletePipeline::dispatch($removeMedia)
|
|
|
|
->onQueue('mmo')
|
|
|
|
->delay($dateTime->addMinutes(15));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$media = new Media();
|
|
|
|
$media->status_id = null;
|
|
|
|
$media->profile_id = $profile->id;
|
|
|
|
$media->user_id = $user->id;
|
|
|
|
$media->media_path = $path;
|
|
|
|
$media->original_sha256 = $hash;
|
|
|
|
$media->size = $photo->getSize();
|
|
|
|
$media->mime = $mime;
|
|
|
|
$media->caption = $request->input('description');
|
|
|
|
$media->filter_class = $filterClass;
|
|
|
|
$media->filter_name = $filterName;
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($license) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$media->license = $license;
|
|
|
|
}
|
|
|
|
$media->save();
|
|
|
|
|
|
|
|
switch ($media->mime) {
|
|
|
|
case 'image/jpeg':
|
|
|
|
case 'image/png':
|
|
|
|
ImageOptimize::dispatch($media)->onQueue('mmo');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'video/mp4':
|
|
|
|
VideoThumbnail::dispatch($media)->onQueue('mmo');
|
|
|
|
$preview_url = '/storage/no-preview.png';
|
|
|
|
$url = '/storage/no-preview.png';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cache::forget($limitKey);
|
|
|
|
$resource = new Fractal\Resource\Item($media, new MediaTransformer());
|
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
2024-03-05 08:58:15 +00:00
|
|
|
$res['preview_url'] = $media->url().'?v='.time();
|
2023-11-18 03:50:07 +00:00
|
|
|
$res['url'] = null;
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res, 202);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/mutes
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return AccountTransformer
|
|
|
|
*/
|
|
|
|
public function accountMutes(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
2024-03-05 08:58:15 +00:00
|
|
|
'limit' => 'sometimes|integer|min:1',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $request->user();
|
|
|
|
$limit = $request->input('limit', 40);
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($limit > 80) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$limit = 80;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$mutes = UserFilter::whereUserId($user->profile_id)
|
|
|
|
->whereFilterableType('App\Profile')
|
|
|
|
->whereFilterType('mute')
|
|
|
|
->orderByDesc('id')
|
|
|
|
->simplePaginate($limit)
|
2024-02-19 08:36:09 +00:00
|
|
|
->withQueryString();
|
|
|
|
|
|
|
|
$res = $mutes->pluck('filterable_id')
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($id) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return AccountService::get($id, true);
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($account) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $account && isset($account['id']);
|
|
|
|
})
|
|
|
|
->values();
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$baseUrl = config('app.url').'/api/v1/mutes?limit='.$limit.'&';
|
2024-02-19 08:36:09 +00:00
|
|
|
$next = $mutes->nextPageUrl();
|
|
|
|
$prev = $mutes->previousPageUrl();
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($next && ! $prev) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$link = '<'.$next.'>; rel="next"';
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $next && $prev) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$link = '<'.$prev.'>; rel="prev"';
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($next && $prev) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$link = '<'.$next.'>; rel="next",<'.$prev.'>; rel="prev"';
|
|
|
|
}
|
|
|
|
$headers = isset($link) ? ['Link' => $link] : [];
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2024-02-19 08:36:09 +00:00
|
|
|
return $this->json($res, 200, $headers);
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v1/accounts/{id}/mute
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return RelationshipTransformer
|
|
|
|
*/
|
|
|
|
public function accountMuteById(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$user = $request->user();
|
|
|
|
$pid = $user->profile_id;
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (intval($pid) === intval($id)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json(['error' => 'You cannot mute yourself'], 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
$account = Profile::findOrFail($id);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($account && $account->domain) {
|
2024-02-07 11:41:12 +00:00
|
|
|
$domain = $account->domain;
|
|
|
|
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
|
|
|
|
}
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$count = UserFilterService::muteCount($pid);
|
|
|
|
$maxLimit = intval(config('instance.user_filters.max_user_mutes'));
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($count == 0) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$filterCount = UserFilter::whereUserId($pid)
|
|
|
|
->whereFilterType('mute')
|
|
|
|
->get()
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($rec) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return AccountService::get($rec->filterable_id, true);
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($account) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $account && isset($account['id']);
|
|
|
|
})
|
|
|
|
->values()
|
|
|
|
->count();
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if($filterCount >= $maxLimit, 422, AccountController::FILTER_LIMIT_MUTE_TEXT.$maxLimit.' accounts');
|
2023-11-18 03:50:07 +00:00
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if($count >= $maxLimit, 422, AccountController::FILTER_LIMIT_MUTE_TEXT.$maxLimit.' accounts');
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$filter = UserFilter::firstOrCreate([
|
2024-03-05 08:58:15 +00:00
|
|
|
'user_id' => $pid,
|
|
|
|
'filterable_id' => $account->id,
|
2023-11-18 03:50:07 +00:00
|
|
|
'filterable_type' => 'App\Profile',
|
2024-03-05 08:58:15 +00:00
|
|
|
'filter_type' => 'mute',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
RelationshipService::refresh($pid, $id);
|
|
|
|
|
|
|
|
$resource = new Fractal\Resource\Item($account, new RelationshipTransformer());
|
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v1/accounts/{id}/unmute
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return RelationshipTransformer
|
|
|
|
*/
|
|
|
|
public function accountUnmuteById(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$user = $request->user();
|
|
|
|
$pid = $user->profile_id;
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (intval($pid) === intval($id)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json(['error' => 'You cannot unmute yourself'], 500);
|
|
|
|
}
|
|
|
|
|
|
|
|
$profile = Profile::findOrFail($id);
|
|
|
|
|
|
|
|
$filter = UserFilter::whereUserId($pid)
|
|
|
|
->whereFilterableId($profile->id)
|
|
|
|
->whereFilterableType('App\Profile')
|
|
|
|
->whereFilterType('mute')
|
|
|
|
->first();
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($filter) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$filter->delete();
|
|
|
|
UserFilterService::unmute($pid, $profile->id);
|
|
|
|
RelationshipService::refresh($pid, $id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$resource = new Fractal\Resource\Item($profile, new RelationshipTransformer());
|
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/notifications
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return NotificationTransformer
|
|
|
|
*/
|
|
|
|
public function accountNotifications(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
2024-02-19 08:36:09 +00:00
|
|
|
'limit' => 'sometimes|integer|min:1',
|
2023-11-18 03:50:07 +00:00
|
|
|
'min_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
|
|
|
|
'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
|
|
|
|
'since_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
|
2023-12-13 13:49:00 +00:00
|
|
|
'types[]' => 'sometimes|array',
|
2024-03-05 08:56:16 +00:00
|
|
|
'type' => 'sometimes|string|in:mention,reblog,follow,favourite',
|
|
|
|
'_pe' => 'sometimes',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
$limit = $request->input('limit', 20);
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($limit > 40) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$limit = 40;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$since = $request->input('since_id');
|
|
|
|
$min = $request->input('min_id');
|
|
|
|
$max = $request->input('max_id');
|
2024-03-05 08:56:16 +00:00
|
|
|
$pe = $request->filled('_pe');
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $since && ! $min && ! $max) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$min = 1;
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($since) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$min = $since + 1;
|
|
|
|
}
|
|
|
|
|
2023-12-13 13:49:00 +00:00
|
|
|
$types = $request->input('types');
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$maxId = null;
|
|
|
|
$minId = null;
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($request->user()->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2023-12-13 13:49:00 +00:00
|
|
|
$res = $max ?
|
|
|
|
NotificationService::getMaxMastodon($pid, $max, $limit) :
|
|
|
|
NotificationService::getMinMastodon($pid, $min ?? $since, $limit);
|
|
|
|
$ids = $max ?
|
|
|
|
NotificationService::getRankedMaxId($pid, $max, $limit) :
|
|
|
|
NotificationService::getRankedMinId($pid, $min ?? $since, $limit);
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! empty($ids)) {
|
2023-12-13 13:49:00 +00:00
|
|
|
$maxId = max($ids);
|
|
|
|
$minId = min($ids);
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (empty($res)) {
|
|
|
|
if (! Cache::has('pf:services:notifications:hasSynced:'.$pid)) {
|
2023-12-13 13:49:00 +00:00
|
|
|
Cache::put('pf:services:notifications:hasSynced:'.$pid, 1, 1209600);
|
|
|
|
NotificationService::warmCache($pid, 400, true);
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$baseUrl = config('app.url').'/api/v1/notifications?limit='.$limit.'&';
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($minId == $maxId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$minId = null;
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:56:16 +00:00
|
|
|
$res = collect($res)
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($n) use ($pe) {
|
|
|
|
if (! $pe) {
|
|
|
|
if ($n['type'] == 'comment') {
|
|
|
|
$n['type'] = 'mention';
|
|
|
|
|
|
|
|
return $n;
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:56:16 +00:00
|
|
|
return $n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $n;
|
2024-03-05 08:58:15 +00:00
|
|
|
})
|
|
|
|
->filter(function ($n) use ($pe) {
|
|
|
|
if (in_array($n['type'], ['mention', 'reblog', 'favourite'])) {
|
|
|
|
return isset($n['status'], $n['status']['id']);
|
|
|
|
}
|
2024-03-05 07:39:50 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $pe) {
|
|
|
|
if (in_array($n['type'], [
|
|
|
|
'tagged',
|
|
|
|
'modlog',
|
|
|
|
'story:react',
|
|
|
|
'story:comment',
|
|
|
|
'group:comment',
|
|
|
|
'group:join:approved',
|
|
|
|
'group:join:rejected',
|
|
|
|
])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isset($n['account'], $n['account']['id']);
|
2024-03-05 08:56:16 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
return true;
|
|
|
|
})->values();
|
2024-03-05 07:39:50 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($maxId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$baseUrl.'max_id='.$minId.'>; rel="next"';
|
|
|
|
}
|
2021-12-05 00:55:38 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($minId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$baseUrl.'min_id='.$maxId.'>; rel="prev"';
|
|
|
|
}
|
2021-06-11 04:54:31 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($maxId && $minId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$baseUrl.'max_id='.$minId.'>; rel="next",<'.$baseUrl.'min_id='.$maxId.'>; rel="prev"';
|
|
|
|
}
|
2022-03-25 06:33:30 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$headers = isset($link) ? ['Link' => $link] : [];
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res, 200, $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/timelines/home
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return StatusTransformer
|
|
|
|
*/
|
|
|
|
public function timelineHome(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
|
|
|
|
|
|
|
$this->validate($request, [
|
2024-03-05 08:58:15 +00:00
|
|
|
'page' => 'sometimes|integer|max:40',
|
|
|
|
'min_id' => 'sometimes|integer|min:0|max:'.PHP_INT_MAX,
|
|
|
|
'max_id' => 'sometimes|integer|min:0|max:'.PHP_INT_MAX,
|
|
|
|
'limit' => 'sometimes|integer|min:1',
|
2023-11-18 03:50:07 +00:00
|
|
|
'include_reblogs' => 'sometimes',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$napi = $request->has(self::PF_API_ENTITY_KEY);
|
|
|
|
$page = $request->input('page');
|
|
|
|
$min = $request->input('min_id');
|
|
|
|
$max = $request->input('max_id');
|
|
|
|
$limit = $request->input('limit') ?? 20;
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($limit > 40) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$limit = 40;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
$includeReblogs = $request->filled('include_reblogs') ? $request->boolean('include_reblogs') : false;
|
|
|
|
$nullFields = $includeReblogs ?
|
|
|
|
['in_reply_to_id'] :
|
|
|
|
['in_reply_to_id', 'reblog_of_id'];
|
|
|
|
$inTypes = $includeReblogs ?
|
|
|
|
['photo', 'photo:album', 'video', 'video:album', 'photo:video:album', 'share'] :
|
|
|
|
['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'];
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($request->user()->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (config('exp.cached_home_timeline')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$paddedLimit = $includeReblogs ? $limit + 10 : $limit + 50;
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($min || $max) {
|
|
|
|
if ($request->has('min_id')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$res = HomeTimelineService::getRankedMinId($pid, $min ?? 0, $paddedLimit);
|
|
|
|
} else {
|
|
|
|
$res = HomeTimelineService::getRankedMaxId($pid, $max ?? 0, $paddedLimit);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$res = HomeTimelineService::get($pid, 0, $paddedLimit);
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $res) {
|
|
|
|
$res = Cache::has('pf:services:apiv1:home:cached:coldbootcheck:'.$pid);
|
|
|
|
if (! $res) {
|
|
|
|
Cache::set('pf:services:apiv1:home:cached:coldbootcheck:'.$pid, 1, 86400);
|
2023-11-18 03:50:07 +00:00
|
|
|
FeedWarmCachePipeline::dispatchSync($pid);
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return response()->json([], 206);
|
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
Cache::set('pf:services:apiv1:home:cached:coldbootcheck:'.$pid, 1, 86400);
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return response()->json([], 206);
|
|
|
|
}
|
2023-02-08 05:02:23 +00:00
|
|
|
}
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$res = collect($res)
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($id) use ($napi) {
|
|
|
|
return $napi ? StatusService::get($id, false) : StatusService::getMastodon($id, false);
|
|
|
|
})
|
|
|
|
->filter(function ($res) {
|
|
|
|
return $res && isset($res['account']);
|
|
|
|
})
|
|
|
|
->filter(function ($s) use ($includeReblogs) {
|
|
|
|
return $includeReblogs ? true : $s['reblog'] == null;
|
|
|
|
})
|
|
|
|
->take($limit)
|
|
|
|
->map(function ($status) use ($pid) {
|
|
|
|
if ($pid) {
|
|
|
|
$status['favourited'] = (bool) LikeService::liked($pid, $status['id']);
|
|
|
|
$status['reblogged'] = (bool) ReblogService::get($pid, $status['id']);
|
|
|
|
$status['bookmarked'] = (bool) BookmarkService::get($pid, $status['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
})
|
|
|
|
->values();
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$baseUrl = config('app.url').'/api/v1/timelines/home?limit='.$limit.'&';
|
|
|
|
$minId = $res->map(function ($s) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return ['id' => $s['id']];
|
|
|
|
})->min('id');
|
2024-03-05 08:58:15 +00:00
|
|
|
$maxId = $res->map(function ($s) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return ['id' => $s['id']];
|
|
|
|
})->max('id');
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($minId == $maxId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$minId = null;
|
|
|
|
}
|
2022-03-25 06:33:30 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($maxId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$baseUrl.'max_id='.$minId.'>; rel="next"';
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($minId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$baseUrl.'min_id='.$maxId.'>; rel="prev"';
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($maxId && $minId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$baseUrl.'max_id='.$minId.'>; rel="next",<'.$baseUrl.'min_id='.$maxId.'>; rel="prev"';
|
|
|
|
}
|
|
|
|
|
|
|
|
$headers = isset($link) ? ['Link' => $link] : [];
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res->toArray(), 200, $headers);
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$following = Cache::remember('profile:following:'.$pid, 1209600, function () use ($pid) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$following = Follower::whereProfileId($pid)->pluck('following_id');
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $following->push($pid)->toArray();
|
2022-09-16 02:49:37 +00:00
|
|
|
});
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$muted = UserFilterService::mutes($pid);
|
2021-06-11 04:54:31 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($muted && count($muted)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$following = array_diff($following, $muted);
|
2022-09-16 02:49:37 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($min || $max) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$dir = $min ? '>' : '<';
|
|
|
|
$id = $min ?? $max;
|
|
|
|
$res = Status::select(
|
|
|
|
'id',
|
|
|
|
'profile_id',
|
|
|
|
'type',
|
|
|
|
'visibility',
|
|
|
|
'in_reply_to_id',
|
|
|
|
'reblog_of_id'
|
|
|
|
)
|
2024-03-05 08:58:15 +00:00
|
|
|
->where('id', $dir, $id)
|
|
|
|
->whereNull($nullFields)
|
|
|
|
->whereIntegerInRaw('profile_id', $following)
|
|
|
|
->whereIn('type', $inTypes)
|
|
|
|
->whereIn('visibility', ['public', 'unlisted', 'private'])
|
|
|
|
->orderByDesc('id')
|
|
|
|
->take(($limit * 2))
|
|
|
|
->get()
|
|
|
|
->map(function ($s) use ($pid, $napi) {
|
|
|
|
try {
|
|
|
|
$account = $napi ? AccountService::get($s['profile_id'], true) : AccountService::getMastodon($s['profile_id'], true);
|
|
|
|
if (! $account) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$status = $napi ? StatusService::get($s['id'], false) : StatusService::getMastodon($s['id'], false);
|
|
|
|
if (! $status || ! isset($status['account']) || ! isset($status['account']['id'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$status['account'] = $account;
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($pid) {
|
|
|
|
$status['favourited'] = (bool) LikeService::liked($pid, $s['id']);
|
|
|
|
$status['reblogged'] = (bool) ReblogService::get($pid, $status['id']);
|
|
|
|
$status['bookmarked'] = (bool) BookmarkService::get($pid, $status['id']);
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
return $status;
|
|
|
|
})
|
|
|
|
->filter(function ($status) {
|
|
|
|
return $status && isset($status['account']);
|
|
|
|
})
|
|
|
|
->map(function ($status) use ($pid) {
|
|
|
|
if (! empty($status['reblog'])) {
|
|
|
|
$status['reblog']['favourited'] = (bool) LikeService::liked($pid, $status['reblog']['id']);
|
|
|
|
$status['reblog']['reblogged'] = (bool) ReblogService::get($pid, $status['reblog']['id']);
|
|
|
|
$status['bookmarked'] = (bool) BookmarkService::get($pid, $status['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
})
|
|
|
|
->take($limit)
|
|
|
|
->values();
|
2023-11-18 03:50:07 +00:00
|
|
|
} else {
|
|
|
|
$res = Status::select(
|
|
|
|
'id',
|
|
|
|
'profile_id',
|
|
|
|
'type',
|
|
|
|
'visibility',
|
|
|
|
'in_reply_to_id',
|
|
|
|
'reblog_of_id',
|
|
|
|
)
|
2024-03-05 08:58:15 +00:00
|
|
|
->whereNull($nullFields)
|
|
|
|
->whereIntegerInRaw('profile_id', $following)
|
|
|
|
->whereIn('type', $inTypes)
|
|
|
|
->whereIn('visibility', ['public', 'unlisted', 'private'])
|
|
|
|
->orderByDesc('id')
|
|
|
|
->take(($limit * 2))
|
|
|
|
->get()
|
|
|
|
->map(function ($s) use ($pid, $napi) {
|
|
|
|
try {
|
|
|
|
$account = $napi ? AccountService::get($s['profile_id'], true) : AccountService::getMastodon($s['profile_id'], true);
|
|
|
|
if (! $account) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$status = $napi ? StatusService::get($s['id'], false) : StatusService::getMastodon($s['id'], false);
|
|
|
|
if (! $status || ! isset($status['account']) || ! isset($status['account']['id'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$status['account'] = $account;
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($pid) {
|
|
|
|
$status['favourited'] = (bool) LikeService::liked($pid, $s['id']);
|
|
|
|
$status['reblogged'] = (bool) ReblogService::get($pid, $status['id']);
|
|
|
|
$status['bookmarked'] = (bool) BookmarkService::get($pid, $status['id']);
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
return $status;
|
|
|
|
})
|
|
|
|
->filter(function ($status) {
|
|
|
|
return $status && isset($status['account']);
|
|
|
|
})
|
|
|
|
->map(function ($status) use ($pid) {
|
|
|
|
if (! empty($status['reblog'])) {
|
|
|
|
$status['reblog']['favourited'] = (bool) LikeService::liked($pid, $status['reblog']['id']);
|
|
|
|
$status['reblog']['reblogged'] = (bool) ReblogService::get($pid, $status['reblog']['id']);
|
|
|
|
$status['bookmarked'] = (bool) BookmarkService::get($pid, $status['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
})
|
|
|
|
->take($limit)
|
|
|
|
->values();
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$baseUrl = config('app.url').'/api/v1/timelines/home?limit='.$limit.'&';
|
|
|
|
$minId = $res->map(function ($s) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return ['id' => $s['id']];
|
|
|
|
})->min('id');
|
2024-03-05 08:58:15 +00:00
|
|
|
$maxId = $res->map(function ($s) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return ['id' => $s['id']];
|
|
|
|
})->max('id');
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($minId == $maxId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$minId = null;
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($maxId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$baseUrl.'max_id='.$minId.'>; rel="next"';
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($minId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$baseUrl.'min_id='.$maxId.'>; rel="prev"';
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($maxId && $minId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$baseUrl.'max_id='.$minId.'>; rel="next",<'.$baseUrl.'min_id='.$maxId.'>; rel="prev"';
|
2022-09-16 02:49:37 +00:00
|
|
|
}
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$headers = isset($link) ? ['Link' => $link] : [];
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res->toArray(), 200, $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/timelines/public
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return StatusTransformer
|
|
|
|
*/
|
|
|
|
public function timelinePublic(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
$this->validate($request, [
|
|
|
|
'min_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
|
|
|
|
'max_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
|
|
|
|
'limit' => 'sometimes|integer|min:1',
|
|
|
|
'remote' => 'sometimes',
|
|
|
|
'local' => 'sometimes',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$napi = $request->has(self::PF_API_ENTITY_KEY);
|
|
|
|
$min = $request->input('min_id');
|
|
|
|
$max = $request->input('max_id');
|
2024-03-05 11:43:21 +00:00
|
|
|
if ($max == 0) {
|
|
|
|
$min = 1;
|
|
|
|
}
|
2024-02-12 03:21:46 +00:00
|
|
|
$minOrMax = $request->anyFilled(['max_id', 'min_id']);
|
2023-11-18 03:50:07 +00:00
|
|
|
$limit = $request->input('limit') ?? 20;
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($limit > 40) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$limit = 40;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
$user = $request->user();
|
2023-12-27 09:51:47 +00:00
|
|
|
|
2024-03-05 11:37:20 +00:00
|
|
|
$remote = $request->has('remote') && $request->boolean('remote');
|
|
|
|
$local = $request->boolean('local');
|
2023-12-27 09:51:47 +00:00
|
|
|
$userRoleKey = $remote ? 'can-view-network-feed' : 'can-view-public-feed';
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($user->has_roles && ! UserRoleService::can($userRoleKey, $user->id)) {
|
2023-12-27 09:51:47 +00:00
|
|
|
return [];
|
|
|
|
}
|
2022-04-02 22:18:13 +00:00
|
|
|
$filtered = $user ? UserFilterService::filters($user->profile_id) : [];
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
2023-12-21 07:46:24 +00:00
|
|
|
$domainBlocks = UserFilterService::domainBlocks($user->profile_id);
|
2024-02-12 03:21:46 +00:00
|
|
|
$hideNsfw = config('instance.hide_nsfw_on_public_feeds');
|
|
|
|
$amin = SnowflakeService::byDate(now()->subDays(config('federation.network_timeline_days_falloff')));
|
2024-03-07 10:11:36 +00:00
|
|
|
$asf = AdminShadowFilterService::getHideFromPublicFeedsList();
|
2024-03-05 11:37:20 +00:00
|
|
|
if ($local && $remote) {
|
|
|
|
$feed = Status::select(
|
|
|
|
'id',
|
|
|
|
'uri',
|
|
|
|
'type',
|
|
|
|
'scope',
|
|
|
|
'created_at',
|
|
|
|
'profile_id',
|
|
|
|
'in_reply_to_id',
|
|
|
|
'reblog_of_id'
|
|
|
|
)
|
|
|
|
->when($minOrMax, function ($q, $minOrMax) use ($min, $max) {
|
|
|
|
$dir = $min ? '>' : '<';
|
|
|
|
$id = $min ?? $max;
|
|
|
|
|
|
|
|
return $q->where('id', $dir, $id);
|
|
|
|
})
|
|
|
|
->whereNull(['in_reply_to_id', 'reblog_of_id'])
|
|
|
|
->when($hideNsfw, function ($q, $hideNsfw) {
|
|
|
|
return $q->where('is_nsfw', false);
|
|
|
|
})
|
|
|
|
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
|
|
|
|
->whereScope('public')
|
|
|
|
->where('id', '>', $amin)
|
|
|
|
->orderByDesc('id')
|
|
|
|
->limit(($limit * 2))
|
|
|
|
->pluck('id')
|
|
|
|
->values()
|
|
|
|
->toArray();
|
|
|
|
} elseif ($remote && ! $local) {
|
2024-03-05 08:58:15 +00:00
|
|
|
if (config('instance.timeline.network.cached')) {
|
|
|
|
Cache::remember('api:v1:timelines:network:cache_check', 10368000, function () {
|
|
|
|
if (NetworkTimelineService::count() == 0) {
|
2024-02-11 21:26:45 +00:00
|
|
|
NetworkTimelineService::warmCache(true, config('instance.timeline.network.cache_dropoff'));
|
|
|
|
}
|
|
|
|
});
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-02-11 21:26:45 +00:00
|
|
|
if ($max) {
|
|
|
|
$feed = NetworkTimelineService::getRankedMaxId($max, $limit + 5);
|
2024-03-05 08:58:15 +00:00
|
|
|
} elseif ($min) {
|
2024-02-11 21:26:45 +00:00
|
|
|
$feed = NetworkTimelineService::getRankedMinId($min, $limit + 5);
|
|
|
|
} else {
|
|
|
|
$feed = NetworkTimelineService::get(0, $limit + 5);
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
} else {
|
2024-02-11 21:26:45 +00:00
|
|
|
$feed = Status::select(
|
|
|
|
'id',
|
2024-02-12 03:21:46 +00:00
|
|
|
'uri',
|
2024-02-11 21:26:45 +00:00
|
|
|
'type',
|
2024-02-12 03:21:46 +00:00
|
|
|
'scope',
|
|
|
|
'local',
|
|
|
|
'created_at',
|
|
|
|
'profile_id',
|
2024-02-11 21:26:45 +00:00
|
|
|
'in_reply_to_id',
|
|
|
|
'reblog_of_id'
|
2024-03-05 08:58:15 +00:00
|
|
|
)
|
|
|
|
->when($minOrMax, function ($q, $minOrMax) use ($min, $max) {
|
|
|
|
$dir = $min ? '>' : '<';
|
|
|
|
$id = $min ?? $max;
|
|
|
|
|
|
|
|
return $q->where('id', $dir, $id);
|
|
|
|
})
|
|
|
|
->whereNull(['in_reply_to_id', 'reblog_of_id'])
|
|
|
|
->when($hideNsfw, function ($q, $hideNsfw) {
|
|
|
|
return $q->where('is_nsfw', false);
|
|
|
|
})
|
|
|
|
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
|
|
|
|
->whereLocal(false)
|
|
|
|
->whereScope('public')
|
|
|
|
->where('id', '>', $amin)
|
|
|
|
->orderByDesc('id')
|
|
|
|
->limit(($limit * 2))
|
|
|
|
->pluck('id')
|
|
|
|
->values()
|
|
|
|
->toArray();
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
2024-02-12 03:21:46 +00:00
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
if (config('instance.timeline.local.cached')) {
|
|
|
|
Cache::remember('api:v1:timelines:public:cache_check', 10368000, function () {
|
|
|
|
if (PublicTimelineService::count() == 0) {
|
2024-02-12 03:21:46 +00:00
|
|
|
PublicTimelineService::warmCache(true, 400);
|
|
|
|
}
|
|
|
|
});
|
2024-02-11 21:26:45 +00:00
|
|
|
|
2024-02-12 03:21:46 +00:00
|
|
|
if ($max) {
|
|
|
|
$feed = PublicTimelineService::getRankedMaxId($max, $limit + 5);
|
2024-03-05 08:58:15 +00:00
|
|
|
} elseif ($min) {
|
2024-02-12 03:21:46 +00:00
|
|
|
$feed = PublicTimelineService::getRankedMinId($min, $limit + 5);
|
|
|
|
} else {
|
|
|
|
$feed = PublicTimelineService::get(0, $limit + 5);
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-02-12 03:21:46 +00:00
|
|
|
$feed = Status::select(
|
|
|
|
'id',
|
|
|
|
'uri',
|
|
|
|
'type',
|
|
|
|
'scope',
|
|
|
|
'local',
|
|
|
|
'created_at',
|
|
|
|
'profile_id',
|
|
|
|
'in_reply_to_id',
|
|
|
|
'reblog_of_id'
|
2024-03-05 08:58:15 +00:00
|
|
|
)
|
|
|
|
->when($minOrMax, function ($q, $minOrMax) use ($min, $max) {
|
|
|
|
$dir = $min ? '>' : '<';
|
|
|
|
$id = $min ?? $max;
|
|
|
|
|
|
|
|
return $q->where('id', $dir, $id);
|
|
|
|
})
|
|
|
|
->whereNull(['in_reply_to_id', 'reblog_of_id'])
|
|
|
|
->when($hideNsfw, function ($q, $hideNsfw) {
|
|
|
|
return $q->where('is_nsfw', false);
|
|
|
|
})
|
|
|
|
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
|
|
|
|
->whereLocal(true)
|
|
|
|
->whereScope('public')
|
|
|
|
->where('id', '>', $amin)
|
|
|
|
->orderByDesc('id')
|
|
|
|
->limit(($limit * 2))
|
|
|
|
->pluck('id')
|
|
|
|
->values()
|
|
|
|
->toArray();
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = collect($feed)
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($k) use ($min, $max) {
|
|
|
|
if (! $min && ! $max) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($min) {
|
|
|
|
return $min != $k;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($max) {
|
|
|
|
return $max != $k;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
->map(function ($k) use ($user, $napi) {
|
|
|
|
try {
|
|
|
|
$status = $napi ? StatusService::get($k) : StatusService::getMastodon($k);
|
|
|
|
if (! $status || ! isset($status['account']) || ! isset($status['account']['id'])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$account = $napi ? AccountService::get($status['account']['id'], true) : AccountService::getMastodon($status['account']['id'], true);
|
|
|
|
if (! $account) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$status['account'] = $account;
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($user) {
|
|
|
|
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
|
|
|
|
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $status['id']);
|
|
|
|
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $status['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
})
|
|
|
|
->filter(function ($s) use ($filtered) {
|
|
|
|
return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
|
|
|
|
})
|
|
|
|
->filter(function ($s) use ($domainBlocks) {
|
|
|
|
if (! $domainBlocks || ! count($domainBlocks)) {
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
$domain = strtolower(parse_url($s['url'], PHP_URL_HOST));
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
return ! in_array($domain, $domainBlocks);
|
|
|
|
})
|
2024-03-07 10:11:36 +00:00
|
|
|
->filter(function ($s) use ($asf, $user) {
|
|
|
|
if (! $asf || count($asf) === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array($s['account']['id'], $asf)) {
|
|
|
|
if ($user->profile_id == $s['account']['id']) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->take($limit)
|
|
|
|
->values();
|
|
|
|
|
|
|
|
$baseUrl = config('app.url').'/api/v1/timelines/public?limit='.$limit.'&';
|
|
|
|
if ($remote) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$baseUrl .= 'remote=1&';
|
|
|
|
}
|
2024-03-05 11:37:20 +00:00
|
|
|
if ($local) {
|
|
|
|
$baseUrl .= 'local=1&';
|
|
|
|
}
|
2024-03-05 08:58:15 +00:00
|
|
|
$minId = $res->map(function ($s) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return ['id' => $s['id']];
|
|
|
|
})->min('id');
|
2024-03-05 08:58:15 +00:00
|
|
|
$maxId = $res->map(function ($s) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return ['id' => $s['id']];
|
|
|
|
})->max('id');
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($minId == $maxId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$minId = null;
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($maxId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$baseUrl.'max_id='.$minId.'>; rel="next"';
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($minId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$baseUrl.'min_id='.$maxId.'>; rel="prev"';
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($maxId && $minId) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$link = '<'.$baseUrl.'max_id='.$minId.'>; rel="next",<'.$baseUrl.'min_id='.$maxId.'>; rel="prev"';
|
|
|
|
}
|
|
|
|
|
|
|
|
$headers = isset($link) ? ['Link' => $link] : [];
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res->toArray(), 200, $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/conversations
|
|
|
|
*
|
|
|
|
* Not implemented
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function conversations(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$this->validate($request, [
|
|
|
|
'limit' => 'min:1|max:40',
|
2024-03-05 08:58:15 +00:00
|
|
|
'scope' => 'nullable|in:inbox,sent,requests',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$limit = $request->input('limit', 20);
|
|
|
|
$scope = $request->input('scope', 'inbox');
|
2024-01-11 12:25:23 +00:00
|
|
|
$user = $request->user();
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id)) {
|
2024-01-11 12:25:23 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
$pid = $user->profile_id;
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (config('database.default') == 'pgsql') {
|
|
|
|
$dms = DirectMessage::when($scope === 'inbox', function ($q, $scope) use ($pid) {
|
|
|
|
return $q->whereIsHidden(false)->where('to_id', $pid)->orWhere('from_id', $pid);
|
|
|
|
})
|
|
|
|
->when($scope === 'sent', function ($q, $scope) use ($pid) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $q->whereFromId($pid)->groupBy(['to_id', 'id']);
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->when($scope === 'requests', function ($q, $scope) use ($pid) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $q->whereToId($pid)->whereIsHidden(true);
|
|
|
|
});
|
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
$dms = Conversation::when($scope === 'inbox', function ($q, $scope) use ($pid) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $q->whereIsHidden(false)
|
|
|
|
->where('to_id', $pid)
|
|
|
|
->orWhere('from_id', $pid)
|
|
|
|
->orderByDesc('status_id')
|
|
|
|
->groupBy(['to_id', 'from_id']);
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->when($scope === 'sent', function ($q, $scope) use ($pid) {
|
|
|
|
return $q->whereFromId($pid)->groupBy('to_id');
|
|
|
|
})
|
|
|
|
->when($scope === 'requests', function ($q, $scope) use ($pid) {
|
|
|
|
return $q->whereToId($pid)->whereIsHidden(true);
|
|
|
|
});
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$dms = $dms->orderByDesc('status_id')
|
|
|
|
->simplePaginate($limit)
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($dm) use ($pid) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$from = $pid == $dm->to_id ? $dm->from_id : $dm->to_id;
|
|
|
|
$res = [
|
|
|
|
'id' => $dm->id,
|
|
|
|
'unread' => false,
|
|
|
|
'accounts' => [
|
2024-03-05 08:58:15 +00:00
|
|
|
AccountService::getMastodon($from, true),
|
2023-11-18 03:50:07 +00:00
|
|
|
],
|
2024-03-05 08:58:15 +00:00
|
|
|
'last_status' => StatusService::getDirectMessage($dm->status_id),
|
2023-11-18 03:50:07 +00:00
|
|
|
];
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $res;
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($dm) {
|
|
|
|
if (! $dm || empty($dm['last_status']) || ! isset($dm['accounts']) || ! count($dm['accounts']) || ! isset($dm['accounts'][0]) || ! isset($dm['accounts'][0]['id'])) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return true;
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->unique(function ($item, $key) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $item['accounts'][0]['id'];
|
|
|
|
})
|
|
|
|
->values();
|
|
|
|
|
|
|
|
return $this->json($dms);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/statuses/{id}
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return StatusTransformer
|
|
|
|
*/
|
|
|
|
public function statusById(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
|
|
|
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($request->user()->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
|
|
|
|
$res = $request->has(self::PF_API_ENTITY_KEY) ? StatusService::get($id, false) : StatusService::getMastodon($id, false);
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $res || ! isset($res['visibility'])) {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($res && isset($res['account'], $res['account']['acct'], $res['account']['url']) && strpos($res['account']['acct'], '@') != -1) {
|
2024-02-07 11:41:12 +00:00
|
|
|
$domain = parse_url($res['account']['url'], PHP_URL_HOST);
|
|
|
|
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
|
|
|
|
}
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$scope = $res['visibility'];
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! in_array($scope, ['public', 'unlisted'])) {
|
|
|
|
if ($scope === 'private') {
|
|
|
|
if (intval($res['account']['id']) !== intval($pid)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort_unless(FollowerService::follows($pid, $res['account']['id']), 403);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
abort(400, 'Invalid request');
|
|
|
|
}
|
|
|
|
}
|
2021-06-11 04:54:31 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! empty($res['reblog']) && isset($res['reblog']['id'])) {
|
2023-10-09 19:06:46 +00:00
|
|
|
$res['reblog']['favourited'] = (bool) LikeService::liked($pid, $res['reblog']['id']);
|
|
|
|
$res['reblog']['reblogged'] = (bool) ReblogService::get($pid, $res['reblog']['id']);
|
|
|
|
$res['reblog']['bookmarked'] = BookmarkService::get($pid, $res['reblog']['id']);
|
|
|
|
}
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$res['favourited'] = LikeService::liked($pid, $res['id']);
|
|
|
|
$res['reblogged'] = ReblogService::get($pid, $res['id']);
|
|
|
|
$res['bookmarked'] = BookmarkService::get($pid, $res['id']);
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/statuses/{id}/context
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return StatusTransformer
|
|
|
|
*/
|
|
|
|
public function statusContext(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$user = $request->user();
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
$pid = $user->profile_id;
|
|
|
|
$status = StatusService::getMastodon($id, false);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $status || ! isset($status['account'])) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return response('', 404);
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($status && isset($status['account'], $status['account']['acct']) && strpos($status['account']['acct'], '@') != -1) {
|
2024-02-07 11:41:12 +00:00
|
|
|
$domain = parse_url($status['account']['url'], PHP_URL_HOST);
|
|
|
|
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (intval($status['account']['id']) !== intval($user->profile_id)) {
|
|
|
|
if ($status['visibility'] == 'private') {
|
|
|
|
if (! FollowerService::follows($user->profile_id, $status['account']['id'])) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return response('', 404);
|
|
|
|
}
|
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! in_array($status['visibility'], ['public', 'unlisted'])) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return response('', 404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$ancestors = [];
|
|
|
|
$descendants = [];
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($status['in_reply_to_id']) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$ancestors[] = StatusService::getMastodon($status['in_reply_to_id'], false);
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($status['replies_count']) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$filters = UserFilterService::filters($pid);
|
|
|
|
|
|
|
|
$descendants = DB::table('statuses')
|
|
|
|
->where('in_reply_to_id', $id)
|
|
|
|
->limit(20)
|
|
|
|
->pluck('id')
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($sid) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return StatusService::getMastodon($sid, false);
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($post) use ($filters) {
|
|
|
|
return $post && isset($post['account'], $post['account']['id']) && ! in_array($post['account']['id'], $filters);
|
2023-11-18 03:50:07 +00:00
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($status) use ($pid) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$status['favourited'] = LikeService::liked($pid, $status['id']);
|
|
|
|
$status['reblogged'] = ReblogService::get($pid, $status['id']);
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $status;
|
|
|
|
})
|
|
|
|
->values();
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = [
|
|
|
|
'ancestors' => $ancestors,
|
2024-03-05 08:58:15 +00:00
|
|
|
'descendants' => $descendants,
|
2023-11-18 03:50:07 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/statuses/{id}/card
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return StatusTransformer
|
|
|
|
*/
|
|
|
|
public function statusCard(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$res = [];
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return response()->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/statuses/{id}/reblogged_by
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return AccountTransformer
|
|
|
|
*/
|
|
|
|
public function statusRebloggedBy(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
2024-03-05 08:58:15 +00:00
|
|
|
'limit' => 'sometimes|integer|min:1|max:80',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$limit = $request->input('limit', 10);
|
|
|
|
$user = $request->user();
|
|
|
|
$pid = $user->profile_id;
|
|
|
|
$status = Status::findOrFail($id);
|
|
|
|
$account = AccountService::get($status->profile_id, true);
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $account, 404);
|
|
|
|
if ($account && strpos($account['acct'], '@') != -1) {
|
2024-02-07 11:41:12 +00:00
|
|
|
$domain = parse_url($account['url'], PHP_URL_HOST);
|
|
|
|
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
$author = intval($status->profile_id) === intval($pid) || $user->is_admin;
|
|
|
|
$napi = $request->has(self::PF_API_ENTITY_KEY);
|
|
|
|
|
|
|
|
abort_if(
|
2024-03-05 08:58:15 +00:00
|
|
|
! $status->type ||
|
|
|
|
! in_array($status->type, ['photo', 'photo:album', 'photo:video:album', 'reply', 'text', 'video', 'video:album']),
|
2023-11-18 03:50:07 +00:00
|
|
|
404,
|
|
|
|
);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $author) {
|
|
|
|
if ($status->scope == 'private') {
|
|
|
|
abort_if(! FollowerService::follows($pid, $status->profile_id), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! in_array($status->scope, ['public', 'unlisted']), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('cursor')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json([]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = Status::where('reblog_of_id', $status->id)
|
2024-03-05 08:58:15 +00:00
|
|
|
->orderByDesc('id')
|
|
|
|
->cursorPaginate($limit)
|
|
|
|
->withQueryString();
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $res) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$headers = [];
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($author && $res->hasPages()) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$links = '';
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($res->onFirstPage()) {
|
|
|
|
if ($res->nextPageUrl()) {
|
|
|
|
$links = '<'.$res->nextPageUrl().'>; rel="prev"';
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($res->previousPageUrl()) {
|
|
|
|
$links = '<'.$res->previousPageUrl().'>; rel="next"';
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($res->nextPageUrl()) {
|
|
|
|
if (! empty($links)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$links .= ', ';
|
|
|
|
}
|
2024-03-05 08:58:15 +00:00
|
|
|
$links .= '<'.$res->nextPageUrl().'>; rel="prev"';
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$headers = ['Link' => $links];
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$res = $res->map(function ($status) use ($pid, $napi) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$account = $napi ? AccountService::get($status->profile_id, true) : AccountService::getMastodon($status->profile_id, true);
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $account) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($napi) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$account['follows'] = $status->profile_id == $pid ? null : FollowerService::follows($pid, $status->profile_id);
|
|
|
|
}
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $account;
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($account) {
|
|
|
|
return $account && isset($account['id']);
|
|
|
|
})
|
|
|
|
->values();
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
return $this->json($res, 200, $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/statuses/{id}/favourited_by
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return AccountTransformer
|
|
|
|
*/
|
|
|
|
public function statusFavouritedBy(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
2024-03-05 08:58:15 +00:00
|
|
|
'limit' => 'sometimes|integer|min:1',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
2024-02-19 08:36:09 +00:00
|
|
|
$limit = $request->input('limit', 40);
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($limit > 80) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$limit = 80;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
$user = $request->user();
|
|
|
|
$pid = $user->profile_id;
|
|
|
|
$status = Status::findOrFail($id);
|
|
|
|
$account = AccountService::get($status->profile_id, true);
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($account && strpos($account['acct'], '@') != -1) {
|
2024-02-07 11:41:12 +00:00
|
|
|
$domain = parse_url($account['url'], PHP_URL_HOST);
|
|
|
|
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
|
|
|
|
}
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $account, 404);
|
2023-11-18 03:50:07 +00:00
|
|
|
$author = intval($status->profile_id) === intval($pid) || $user->is_admin;
|
|
|
|
$napi = $request->has(self::PF_API_ENTITY_KEY);
|
|
|
|
|
|
|
|
abort_if(
|
2024-03-05 08:58:15 +00:00
|
|
|
! $status->type ||
|
|
|
|
! in_array($status->type, ['photo', 'photo:album', 'photo:video:album', 'reply', 'text', 'video', 'video:album']),
|
2023-11-18 03:50:07 +00:00
|
|
|
404,
|
|
|
|
);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $author) {
|
|
|
|
if ($status->scope == 'private') {
|
|
|
|
abort_if(! FollowerService::follows($pid, $status->profile_id), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! in_array($status->scope, ['public', 'unlisted']), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('cursor')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json([]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = Like::where('status_id', $status->id)
|
2024-03-05 08:58:15 +00:00
|
|
|
->orderByDesc('id')
|
|
|
|
->cursorPaginate($limit)
|
|
|
|
->withQueryString();
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $res) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$headers = [];
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($author && $res->hasPages()) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$links = '';
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($res->onFirstPage()) {
|
|
|
|
if ($res->nextPageUrl()) {
|
|
|
|
$links = '<'.$res->nextPageUrl().'>; rel="prev"';
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($res->previousPageUrl()) {
|
|
|
|
$links = '<'.$res->previousPageUrl().'>; rel="next"';
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($res->nextPageUrl()) {
|
|
|
|
if (! empty($links)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$links .= ', ';
|
|
|
|
}
|
2024-03-05 08:58:15 +00:00
|
|
|
$links .= '<'.$res->nextPageUrl().'>; rel="prev"';
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$headers = ['Link' => $links];
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$res = $res->map(function ($like) use ($pid, $napi) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$account = $napi ? AccountService::get($like->profile_id, true) : AccountService::getMastodon($like->profile_id, true);
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $account) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($napi) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$account['follows'] = $like->profile_id == $pid ? null : FollowerService::follows($pid, $like->profile_id);
|
|
|
|
}
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $account;
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($account) {
|
|
|
|
return $account && isset($account['id']);
|
|
|
|
})
|
|
|
|
->values();
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
return $this->json($res, 200, $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v1/statuses
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return StatusTransformer
|
|
|
|
*/
|
|
|
|
public function statusCreate(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
|
|
|
'status' => 'nullable|string',
|
|
|
|
'in_reply_to_id' => 'nullable',
|
2024-03-05 08:58:15 +00:00
|
|
|
'media_ids' => 'sometimes|array|max:'.config_cache('pixelfed.max_album_length'),
|
2023-11-18 03:50:07 +00:00
|
|
|
'sensitive' => 'nullable',
|
|
|
|
'visibility' => 'string|in:private,unlisted,public',
|
|
|
|
'spoiler_text' => 'sometimes|max:140',
|
|
|
|
'place_id' => 'sometimes|integer|min:1|max:128769',
|
|
|
|
'collection_ids' => 'sometimes|array|max:3',
|
|
|
|
'comments_disabled' => 'sometimes|boolean',
|
|
|
|
]);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->hasHeader('idempotency-key')) {
|
|
|
|
$key = 'pf:api:v1:status:idempotency-key:'.$request->user()->id.':'.hash('sha1', $request->header('idempotency-key'));
|
2023-11-18 03:50:07 +00:00
|
|
|
$exists = Cache::has($key);
|
|
|
|
abort_if($exists, 400, 'Duplicate idempotency key.');
|
|
|
|
Cache::put($key, 1, 3600);
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (config('costar.enabled') == true) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$blockedKeywords = config('costar.keyword.block');
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($blockedKeywords !== null && $request->status) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$keywords = config('costar.keyword.block');
|
2024-03-05 08:58:15 +00:00
|
|
|
foreach ($keywords as $kw) {
|
|
|
|
if (Str::contains($request->status, $kw) == true) {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort(400, 'Invalid object. Contains banned keyword.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $request->filled('media_ids') && ! $request->filled('in_reply_to_id')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort(403, 'Empty statuses are not allowed');
|
|
|
|
}
|
|
|
|
|
|
|
|
$ids = $request->input('media_ids');
|
|
|
|
$in_reply_to_id = $request->input('in_reply_to_id');
|
|
|
|
|
|
|
|
$user = $request->user();
|
2024-01-11 11:40:25 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($user->has_roles) {
|
|
|
|
if ($in_reply_to_id != null) {
|
|
|
|
abort_if(! UserRoleService::can('can-comment', $user->id), 403, 'Invalid permissions for this action');
|
2024-01-11 11:40:25 +00:00
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! UserRoleService::can('can-post', $user->id), 403, 'Invalid permissions for this action');
|
2024-01-11 11:40:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
$profile = $user->profile;
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$limitKey = 'compose:rate-limit:store:'.$user->id;
|
2023-11-18 03:50:07 +00:00
|
|
|
$limitTtl = now()->addMinutes(15);
|
2024-03-05 08:58:15 +00:00
|
|
|
$limitReached = Cache::remember($limitKey, $limitTtl, function () use ($user) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$dailyLimit = Status::whereProfileId($user->profile_id)
|
|
|
|
->whereNull('in_reply_to_id')
|
|
|
|
->whereNull('reblog_of_id')
|
|
|
|
->where('created_at', '>', now()->subDays(1))
|
|
|
|
->count();
|
|
|
|
|
|
|
|
return $dailyLimit >= 1000;
|
|
|
|
});
|
|
|
|
|
|
|
|
abort_if($limitReached == true, 429);
|
|
|
|
|
|
|
|
$visibility = $profile->is_private ? 'private' : (
|
|
|
|
$profile->unlisted == true &&
|
|
|
|
$request->input('visibility', 'public') == 'public' ?
|
|
|
|
'unlisted' :
|
|
|
|
$request->input('visibility', 'public'));
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($user->last_active_at == null) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = strip_tags($request->input('status'));
|
|
|
|
$rendered = Autolink::create()->autolink($content);
|
2024-02-02 05:46:13 +00:00
|
|
|
$cw = $user->profile->cw == true ? true : $request->boolean('sensitive', false);
|
2023-11-18 03:50:07 +00:00
|
|
|
$spoilerText = $cw && $request->filled('spoiler_text') ? $request->input('spoiler_text') : null;
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($in_reply_to_id) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$parent = Status::findOrFail($in_reply_to_id);
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($parent->comments_disabled) {
|
|
|
|
return $this->json('Comments have been disabled on this post', 422);
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
$blocks = UserFilterService::blocks($parent->profile_id);
|
|
|
|
abort_if(in_array($profile->id, $blocks), 422, 'Cannot reply to this post at this time.');
|
|
|
|
|
|
|
|
$status = new Status;
|
|
|
|
$status->caption = $content;
|
|
|
|
$status->rendered = $rendered;
|
|
|
|
$status->scope = $visibility;
|
|
|
|
$status->visibility = $visibility;
|
|
|
|
$status->profile_id = $user->profile_id;
|
|
|
|
$status->is_nsfw = $cw;
|
|
|
|
$status->cw_summary = $spoilerText;
|
|
|
|
$status->in_reply_to_id = $parent->id;
|
|
|
|
$status->in_reply_to_profile_id = $parent->profile_id;
|
|
|
|
$status->save();
|
|
|
|
StatusService::del($parent->id);
|
2024-03-05 08:58:15 +00:00
|
|
|
Cache::forget('status:replies:all:'.$parent->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($ids) {
|
|
|
|
if (Media::whereUserId($user->id)
|
2023-11-18 03:50:07 +00:00
|
|
|
->whereNull('status_id')
|
|
|
|
->find($ids)
|
|
|
|
->count() == 0
|
|
|
|
) {
|
|
|
|
abort(400, 'Invalid media_ids');
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $in_reply_to_id) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$status = new Status;
|
|
|
|
$status->caption = $content;
|
|
|
|
$status->rendered = $rendered;
|
|
|
|
$status->profile_id = $user->profile_id;
|
|
|
|
$status->is_nsfw = $cw;
|
|
|
|
$status->cw_summary = $spoilerText;
|
|
|
|
$status->scope = 'draft';
|
|
|
|
$status->visibility = 'draft';
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('place_id')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$status->place_id = $request->input('place_id');
|
|
|
|
}
|
|
|
|
$status->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$mimes = [];
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
foreach ($ids as $k => $v) {
|
|
|
|
if ($k + 1 > config_cache('pixelfed.max_album_length')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$m = Media::whereUserId($user->id)->whereNull('status_id')->findOrFail($v);
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($m->profile_id !== $user->profile_id || $m->status_id) {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort(403, 'Invalid media id');
|
|
|
|
}
|
|
|
|
$m->order = $k + 1;
|
|
|
|
$m->status_id = $status->id;
|
|
|
|
$m->save();
|
|
|
|
array_push($mimes, $m->mime);
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (empty($mimes)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$status->delete();
|
|
|
|
abort(400, 'Invalid media ids');
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('comments_disabled') && $request->input('comments_disabled')) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$status->comments_disabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$status->scope = $visibility;
|
|
|
|
$status->visibility = $visibility;
|
|
|
|
$status->type = StatusController::mimeTypeCheck($mimes);
|
|
|
|
$status->save();
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $status) {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort(500, 'An error occured.');
|
|
|
|
}
|
|
|
|
|
|
|
|
NewStatusPipeline::dispatch($status);
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($status->in_reply_to_id) {
|
2023-11-18 03:50:07 +00:00
|
|
|
CommentPipeline::dispatch($parent, $status);
|
|
|
|
}
|
|
|
|
Cache::forget('user:account:id:'.$user->id);
|
|
|
|
Cache::forget('_api:statuses:recent_9:'.$user->profile_id);
|
|
|
|
Cache::forget('profile:status_count:'.$user->profile_id);
|
|
|
|
Cache::forget($user->storageUsedKey());
|
2024-03-05 08:58:15 +00:00
|
|
|
Cache::forget('profile:embed:'.$status->profile_id);
|
2023-11-18 03:50:07 +00:00
|
|
|
Cache::forget($limitKey);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($request->has('collection_ids') && $ids) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$collections = Collection::whereProfileId($user->profile_id)
|
|
|
|
->find($request->input('collection_ids'))
|
2024-03-05 08:58:15 +00:00
|
|
|
->each(function ($collection) use ($status) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$count = $collection->items()->count();
|
|
|
|
$item = CollectionItem::firstOrCreate([
|
|
|
|
'collection_id' => $collection->id,
|
2024-03-05 08:58:15 +00:00
|
|
|
'object_type' => 'App\Status',
|
|
|
|
'object_id' => $status->id,
|
|
|
|
], [
|
|
|
|
'order' => $count,
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
CollectionService::addItem(
|
|
|
|
$collection->id,
|
|
|
|
$status->id,
|
|
|
|
$count
|
|
|
|
);
|
2022-10-01 06:07:28 +00:00
|
|
|
$collection->updated_at = now();
|
|
|
|
$collection->save();
|
|
|
|
CollectionService::setCollection($collection->id, $collection);
|
2023-11-18 03:50:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = StatusService::getMastodon($status->id, false);
|
|
|
|
$res['favourited'] = false;
|
|
|
|
$res['language'] = 'en';
|
|
|
|
$res['bookmarked'] = false;
|
|
|
|
$res['card'] = null;
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DELETE /api/v1/statuses
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return null
|
|
|
|
*/
|
|
|
|
public function statusDelete(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
|
|
|
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($request->user()->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
$status = Status::whereProfileId($request->user()->profile->id)
|
2024-03-05 08:58:15 +00:00
|
|
|
->findOrFail($id);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$resource = new Fractal\Resource\Item($status, new StatusTransformer());
|
|
|
|
|
|
|
|
Cache::forget('profile:status_count:'.$status->profile_id);
|
|
|
|
StatusDelete::dispatch($status);
|
|
|
|
|
|
|
|
$res = $this->fractal->createData($resource)->toArray();
|
|
|
|
$res['text'] = $res['content'];
|
|
|
|
unset($res['content']);
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v1/statuses/{id}/reblog
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return StatusTransformer
|
|
|
|
*/
|
|
|
|
public function statusShare(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$user = $request->user();
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if($user->has_roles && ! UserRoleService::can('can-share', $user->id), 403, 'Invalid permissions for this action');
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
$status = Status::whereScope('public')->findOrFail($id);
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($status && ($status->uri || $status->url || $status->object_url)) {
|
2024-02-07 11:41:12 +00:00
|
|
|
$url = $status->uri ?? $status->url ?? $status->object_url;
|
|
|
|
$domain = parse_url($url, PHP_URL_HOST);
|
|
|
|
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
|
|
|
|
}
|
2024-03-05 08:58:15 +00:00
|
|
|
if (intval($status->profile_id) !== intval($user->profile_id)) {
|
|
|
|
if ($status->scope == 'private') {
|
|
|
|
abort_if(! FollowerService::follows($user->profile_id, $status->profile_id), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! in_array($status->scope, ['public', 'unlisted']), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$blocks = UserFilterService::blocks($status->profile_id);
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($blocks && in_array($user->profile_id, $blocks)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort(422);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$share = Status::firstOrCreate([
|
|
|
|
'profile_id' => $user->profile_id,
|
|
|
|
'reblog_of_id' => $status->id,
|
|
|
|
'type' => 'share',
|
|
|
|
'in_reply_to_profile_id' => $status->profile_id,
|
|
|
|
'scope' => 'public',
|
2024-03-05 08:58:15 +00:00
|
|
|
'visibility' => 'public',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
SharePipeline::dispatch($share)->onQueue('low');
|
|
|
|
|
|
|
|
StatusService::del($status->id);
|
|
|
|
ReblogService::add($user->profile_id, $status->id);
|
|
|
|
$res = StatusService::getMastodon($status->id);
|
|
|
|
$res['reblogged'] = true;
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v1/statuses/{id}/unreblog
|
|
|
|
*
|
2024-03-05 08:58:15 +00:00
|
|
|
* @param int $id
|
2023-11-18 03:50:07 +00:00
|
|
|
* @return StatusTransformer
|
|
|
|
*/
|
|
|
|
public function statusUnshare(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$user = $request->user();
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if($user->has_roles && ! UserRoleService::can('can-share', $user->id), 403, 'Invalid permissions for this action');
|
2023-12-13 13:49:00 +00:00
|
|
|
AccountService::setLastActive($user->id);
|
2023-11-18 03:50:07 +00:00
|
|
|
$status = Status::whereScope('public')->findOrFail($id);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (intval($status->profile_id) !== intval($user->profile_id)) {
|
|
|
|
if ($status->scope == 'private') {
|
|
|
|
abort_if(! FollowerService::follows($user->profile_id, $status->profile_id), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
} else {
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! in_array($status->scope, ['public', 'unlisted']), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$reblog = Status::whereProfileId($user->profile_id)
|
2024-03-05 08:58:15 +00:00
|
|
|
->whereReblogOfId($status->id)
|
|
|
|
->first();
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $reblog) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$res = StatusService::getMastodon($status->id);
|
|
|
|
$res['reblogged'] = false;
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
UndoSharePipeline::dispatch($reblog)->onQueue('low');
|
|
|
|
ReblogService::del($user->profile_id, $status->id);
|
|
|
|
|
|
|
|
$res = StatusService::getMastodon($status->id);
|
|
|
|
$res['reblogged'] = false;
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/timelines/tag/{hashtag}
|
|
|
|
*
|
|
|
|
* @param string $hashtag
|
|
|
|
* @return StatusTransformer
|
|
|
|
*/
|
|
|
|
public function timelineHashtag(Request $request, $hashtag)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$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' => 'sometimes|integer|min:1',
|
2024-03-05 12:39:47 +00:00
|
|
|
'only_media' => 'sometimes',
|
2024-03-05 08:58:15 +00:00
|
|
|
'_pe' => 'sometimes',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
2024-01-03 05:04:27 +00:00
|
|
|
$user = $request->user();
|
|
|
|
abort_if(
|
2024-03-05 08:58:15 +00:00
|
|
|
$user->has_roles && ! UserRoleService::can('can-view-hashtag-feed', $user->id),
|
2024-01-03 05:04:27 +00:00
|
|
|
403,
|
|
|
|
'Invalid permissions for this action'
|
|
|
|
);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (config('database.default') === 'pgsql') {
|
2023-11-18 03:50:07 +00:00
|
|
|
$tag = Hashtag::where('name', 'ilike', $hashtag)
|
|
|
|
->orWhere('slug', 'ilike', $hashtag)
|
|
|
|
->first();
|
|
|
|
} else {
|
|
|
|
$tag = Hashtag::whereName($hashtag)
|
2024-03-05 08:58:15 +00:00
|
|
|
->orWhere('slug', $hashtag)
|
|
|
|
->first();
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $tag) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return response()->json([]);
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($tag->is_banned == true) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$min = $request->input('min_id');
|
|
|
|
$max = $request->input('max_id');
|
|
|
|
$limit = $request->input('limit', 20);
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($limit > 40) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$limit = 40;
|
|
|
|
}
|
2024-03-05 12:39:47 +00:00
|
|
|
$onlyMedia = $request->boolean('only_media', true);
|
2023-11-18 03:50:07 +00:00
|
|
|
$pe = $request->has(self::PF_API_ENTITY_KEY);
|
2023-12-21 09:03:15 +00:00
|
|
|
$pid = $request->user()->profile_id;
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($min || $max) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$minMax = SnowflakeService::byDate(now()->subMonths(6));
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($min && intval($min) < $minMax) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return [];
|
|
|
|
}
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($max && intval($max) < $minMax) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-21 09:03:15 +00:00
|
|
|
$filters = UserFilterService::filters($pid);
|
|
|
|
$domainBlocks = UserFilterService::domainBlocks($pid);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $min && ! $max) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$id = 1;
|
|
|
|
$dir = '>';
|
|
|
|
} else {
|
|
|
|
$dir = $min ? '>' : '<';
|
|
|
|
$id = $min ?? $max;
|
|
|
|
}
|
|
|
|
|
|
|
|
$res = StatusHashtag::whereHashtagId($tag->id)
|
2024-03-05 12:39:47 +00:00
|
|
|
->whereIn('status_visibility', ['public', 'private', 'unlisted'])
|
2023-11-18 03:50:07 +00:00
|
|
|
->where('status_id', $dir, $id)
|
|
|
|
->orderBy('status_id', 'desc')
|
2024-02-19 08:36:09 +00:00
|
|
|
->limit(100)
|
2023-11-18 03:50:07 +00:00
|
|
|
->pluck('status_id')
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($i) use ($pe) {
|
2024-03-05 12:39:47 +00:00
|
|
|
return $pe ? StatusService::get($i, false) : StatusService::getMastodon($i, false);
|
2023-11-18 03:50:07 +00:00
|
|
|
})
|
2024-03-05 12:39:47 +00:00
|
|
|
->filter(function ($i) use ($onlyMedia, $pid) {
|
|
|
|
if (! $i || ! isset($i['account'], $i['account']['id'])) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2024-03-05 12:39:47 +00:00
|
|
|
if ($i['visibility'] === 'unlisted') {
|
|
|
|
if ((int) $i['account']['id'] !== $pid) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($i['visibility'] === 'private') {
|
|
|
|
if ((int) $i['account']['id'] !== $pid) {
|
|
|
|
return FollowerService::follows($pid, $i['account']['id'], true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($onlyMedia == true) {
|
|
|
|
if (! isset($i['media_attachments']) || ! count($i['media_attachments'])) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-12-21 09:03:15 +00:00
|
|
|
return $i && isset($i['account'], $i['url']);
|
2023-11-18 03:50:07 +00:00
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($i) use ($filters, $domainBlocks) {
|
2023-12-21 09:03:15 +00:00
|
|
|
$domain = strtolower(parse_url($i['url'], PHP_URL_HOST));
|
2024-03-05 08:58:15 +00:00
|
|
|
|
|
|
|
return ! in_array($i['account']['id'], $filters) && ! in_array($domain, $domainBlocks);
|
2023-11-18 03:50:07 +00:00
|
|
|
})
|
2024-02-19 08:36:09 +00:00
|
|
|
->take($limit)
|
2023-11-18 03:50:07 +00:00
|
|
|
->values()
|
|
|
|
->toArray();
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/bookmarks
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return StatusTransformer
|
|
|
|
*/
|
|
|
|
public function bookmarks(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
2024-02-19 08:36:09 +00:00
|
|
|
'limit' => 'sometimes|integer|min:1',
|
2023-11-18 03:50:07 +00:00
|
|
|
'max_id' => 'nullable|integer|min:0',
|
|
|
|
'since_id' => 'nullable|integer|min:0',
|
2024-03-05 08:58:15 +00:00
|
|
|
'min_id' => 'nullable|integer|min:0',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$pe = $request->has('_pe');
|
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
$limit = $request->input('limit') ?? 20;
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($limit > 40) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$limit = 40;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
$max_id = $request->input('max_id');
|
|
|
|
$since_id = $request->input('since_id');
|
|
|
|
$min_id = $request->input('min_id');
|
|
|
|
|
|
|
|
$dir = $min_id ? '>' : '<';
|
|
|
|
$id = $min_id ?? $max_id;
|
|
|
|
|
|
|
|
$bookmarkQuery = Bookmark::whereProfileId($pid)
|
2023-02-08 05:02:23 +00:00
|
|
|
->orderByDesc('id')
|
|
|
|
->cursorPaginate($limit);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$bookmarks = $bookmarkQuery->map(function ($bookmark) use ($pid, $pe) {
|
|
|
|
$status = $pe ? StatusService::get($bookmark->status_id, false) : StatusService::getMastodon($bookmark->status_id, false);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($status) {
|
|
|
|
$status['bookmarked'] = true;
|
|
|
|
$status['favourited'] = LikeService::liked($pid, $status['id']);
|
|
|
|
$status['reblogged'] = ReblogService::get($pid, $status['id']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
})
|
2023-11-18 03:50:07 +00:00
|
|
|
->filter()
|
|
|
|
->values()
|
|
|
|
->toArray();
|
2022-03-23 05:13:04 +00:00
|
|
|
|
2023-02-08 05:02:23 +00:00
|
|
|
$links = null;
|
|
|
|
$headers = [];
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($bookmarkQuery->nextCursor()) {
|
2023-02-08 05:02:23 +00:00
|
|
|
$links .= '<'.$bookmarkQuery->nextPageUrl().'&limit='.$limit.'>; rel="next"';
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($bookmarkQuery->previousCursor()) {
|
|
|
|
if ($links != null) {
|
2023-02-08 05:02:23 +00:00
|
|
|
$links .= ', ';
|
|
|
|
}
|
|
|
|
$links .= '<'.$bookmarkQuery->previousPageUrl().'&limit='.$limit.'>; rel="prev"';
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($links) {
|
2023-02-08 05:02:23 +00:00
|
|
|
$headers = ['Link' => $links];
|
|
|
|
}
|
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json($bookmarks, 200, $headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v1/statuses/{id}/bookmark
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return StatusTransformer
|
|
|
|
*/
|
|
|
|
public function bookmarkStatus(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$status = Status::findOrFail($id);
|
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if($user->has_roles && ! UserRoleService::can('can-bookmark', $user->id), 403, 'Invalid permissions for this action');
|
2023-11-18 03:50:07 +00:00
|
|
|
abort_if($status->in_reply_to_id || $status->reblog_of_id, 404);
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! in_array($status->scope, ['public', 'unlisted', 'private']), 404);
|
|
|
|
abort_if(! in_array($status->type, ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album']), 404);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($status->scope == 'private') {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort_if(
|
2024-03-05 08:58:15 +00:00
|
|
|
$pid !== $status->profile_id && ! FollowerService::follows($pid, $status->profile_id),
|
2023-11-18 03:50:07 +00:00
|
|
|
404,
|
|
|
|
'Error: You cannot bookmark private posts from accounts you do not follow.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Bookmark::firstOrCreate([
|
|
|
|
'status_id' => $status->id,
|
2024-03-05 08:58:15 +00:00
|
|
|
'profile_id' => $pid,
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
BookmarkService::add($pid, $status->id);
|
|
|
|
|
|
|
|
$res = StatusService::getMastodon($status->id, false);
|
|
|
|
$res['bookmarked'] = true;
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* POST /api/v1/statuses/{id}/unbookmark
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return StatusTransformer
|
|
|
|
*/
|
|
|
|
public function unbookmarkStatus(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$status = Status::findOrFail($id);
|
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if($user->has_roles && ! UserRoleService::can('can-bookmark', $user->id), 403, 'Invalid permissions for this action');
|
2023-11-18 03:50:07 +00:00
|
|
|
abort_if($status->in_reply_to_id || $status->reblog_of_id, 404);
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! in_array($status->scope, ['public', 'unlisted', 'private']), 404);
|
|
|
|
abort_if(! in_array($status->type, ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album']), 404);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$bookmark = Bookmark::whereStatusId($status->id)
|
|
|
|
->whereProfileId($pid)
|
|
|
|
->first();
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($bookmark) {
|
2023-11-18 03:50:07 +00:00
|
|
|
BookmarkService::del($pid, $status->id);
|
|
|
|
$bookmark->delete();
|
|
|
|
}
|
|
|
|
$res = StatusService::getMastodon($status->id, false);
|
|
|
|
$res['bookmarked'] = false;
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GET /api/v1/discover/posts
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function discoverPosts(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
2024-03-05 08:58:15 +00:00
|
|
|
'limit' => 'integer|min:1|max:40',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$limit = $request->input('limit', 40);
|
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
$filters = UserFilterService::filters($pid);
|
|
|
|
$forYou = DiscoverService::getForYou();
|
2024-02-08 01:48:17 +00:00
|
|
|
$posts = $forYou->take(50)->map(function ($post) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return StatusService::getMastodon($post);
|
|
|
|
})
|
2024-02-08 01:48:17 +00:00
|
|
|
->filter(function ($post) use ($filters) {
|
|
|
|
return $post &&
|
|
|
|
isset($post['account']) &&
|
|
|
|
isset($post['account']['id']) &&
|
2024-03-05 08:58:15 +00:00
|
|
|
! in_array($post['account']['id'], $filters);
|
2024-02-08 01:48:17 +00:00
|
|
|
})
|
|
|
|
->take(12)
|
|
|
|
->values();
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json(compact('posts'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-08 01:48:17 +00:00
|
|
|
* GET /api/v2/statuses/{id}/replies
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-11-18 03:50:07 +00:00
|
|
|
public function statusReplies(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user(), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$this->validate($request, [
|
2024-02-19 08:36:09 +00:00
|
|
|
'limit' => 'sometimes|integer|min:1',
|
2024-03-05 08:58:15 +00:00
|
|
|
'sort' => 'in:all,newest,popular',
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$limit = $request->input('limit', 3);
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($limit > 10) {
|
2024-02-19 08:36:09 +00:00
|
|
|
$limit = 10;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
$status = StatusService::getMastodon($id, false);
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $status, 404);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($status['visibility'] == 'private') {
|
|
|
|
if ($pid != $status['account']['id']) {
|
2023-11-18 03:50:07 +00:00
|
|
|
abort_unless(FollowerService::follows($pid, $status['account']['id']), 404);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$sortBy = $request->input('sort', 'all');
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($sortBy == 'all' && isset($status['replies_count']) && $status['replies_count'] && $request->has('refresh_cache')) {
|
|
|
|
if (! Cache::has('status:replies:all-rc:'.$id)) {
|
|
|
|
Cache::forget('status:replies:all:'.$id);
|
|
|
|
Cache::put('status:replies:all-rc:'.$id, true, 300);
|
2023-11-18 03:50:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($sortBy == 'all' && ! $request->has('cursor')) {
|
|
|
|
$ids = Cache::remember('status:replies:all:'.$id, 3600, function () use ($id) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return DB::table('statuses')
|
|
|
|
->where('in_reply_to_id', $id)
|
|
|
|
->orderBy('id')
|
|
|
|
->cursorPaginate(3);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$ids = DB::table('statuses')
|
|
|
|
->where('in_reply_to_id', $id)
|
2024-03-05 08:58:15 +00:00
|
|
|
->when($sortBy, function ($q, $sortBy) {
|
|
|
|
if ($sortBy === 'all') {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $q->orderBy('id');
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($sortBy === 'newest') {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $q->orderByDesc('created_at');
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($sortBy === 'popular') {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $q->orderByDesc('likes_count');
|
|
|
|
}
|
|
|
|
})
|
|
|
|
->cursorPaginate($limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
$filters = UserFilterService::filters($pid);
|
2024-03-05 08:58:15 +00:00
|
|
|
$data = $ids->filter(function ($post) use ($filters) {
|
|
|
|
return ! in_array($post->profile_id, $filters);
|
2023-11-18 03:50:07 +00:00
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->map(function ($post) use ($pid) {
|
|
|
|
$status = StatusService::get($post->id, false);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $status || ! isset($status['id'])) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-18 03:50:07 +00:00
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
$status['favourited'] = LikeService::liked($pid, $post->id);
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
})
|
|
|
|
->map(function ($post) {
|
|
|
|
if (isset($post['account']) && isset($post['account']['id'])) {
|
|
|
|
$account = AccountService::get($post['account']['id'], true);
|
|
|
|
$post['account'] = $account;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $post;
|
|
|
|
})
|
|
|
|
->filter(function ($post) {
|
|
|
|
return $post && isset($post['id']) && isset($post['account']) && isset($post['account']['id']);
|
|
|
|
})
|
|
|
|
->values();
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$res = [
|
|
|
|
'data' => $data,
|
2024-03-05 08:58:15 +00:00
|
|
|
'next' => $ids->nextPageUrl(),
|
2023-11-18 03:50:07 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
return $this->json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-08 01:48:17 +00:00
|
|
|
* GET /api/v2/statuses/{id}/state
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-11-18 03:50:07 +00:00
|
|
|
public function statusState(Request $request, $id)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user(), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$status = Status::findOrFail($id);
|
|
|
|
$pid = $request->user()->profile_id;
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! in_array($status->scope, ['public', 'unlisted', 'private']), 404);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
return $this->json(StatusService::getState($status->id, $pid));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-08 01:48:17 +00:00
|
|
|
* GET /api/v1.1/discover/accounts/popular
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-11-18 03:50:07 +00:00
|
|
|
public function discoverAccountsPopular(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
|
2024-03-10 06:25:28 +00:00
|
|
|
$ids = Cache::remember('api:v1.1:discover:accounts:popular', 14400, function () {
|
2023-11-18 03:50:07 +00:00
|
|
|
return DB::table('profiles')
|
2024-03-05 08:58:15 +00:00
|
|
|
->where('is_private', false)
|
|
|
|
->whereNull('status')
|
|
|
|
->orderByDesc('profiles.followers_count')
|
|
|
|
->limit(30)
|
|
|
|
->get();
|
2023-11-18 03:50:07 +00:00
|
|
|
});
|
2023-12-16 13:22:56 +00:00
|
|
|
$filters = UserFilterService::filters($pid);
|
2024-03-10 06:25:28 +00:00
|
|
|
$asf = AdminShadowFilterService::getHideFromPublicFeedsList();
|
2024-03-05 08:58:15 +00:00
|
|
|
$ids = $ids->map(function ($profile) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return AccountService::get($profile->id, true);
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($profile) {
|
|
|
|
return $profile && isset($profile['id'], $profile['locked']) && ! $profile['locked'];
|
|
|
|
})
|
|
|
|
->filter(function ($profile) use ($pid) {
|
|
|
|
return $profile['id'] != $pid;
|
|
|
|
})
|
|
|
|
->filter(function ($profile) use ($pid) {
|
|
|
|
return ! FollowerService::follows($pid, $profile['id'], true);
|
|
|
|
})
|
2024-03-10 06:25:28 +00:00
|
|
|
->filter(function ($profile) use ($asf) {
|
|
|
|
return ! in_array($profile['id'], $asf);
|
|
|
|
})
|
2024-03-05 08:58:15 +00:00
|
|
|
->filter(function ($profile) use ($filters) {
|
|
|
|
return ! in_array($profile['id'], $filters);
|
|
|
|
})
|
|
|
|
->take(16)
|
|
|
|
->values();
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
return $this->json($ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-08 01:48:17 +00:00
|
|
|
* GET /api/v1/preferences
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-11-18 03:50:07 +00:00
|
|
|
public function getPreferences(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
$account = AccountService::get($pid);
|
|
|
|
|
|
|
|
return $this->json([
|
2024-03-05 08:58:15 +00:00
|
|
|
'posting:default:visibility' => $account['locked'] ? 'private' : 'public',
|
|
|
|
'posting:default:sensitive' => false,
|
|
|
|
'posting:default:language' => null,
|
|
|
|
'reading:expand:media' => 'default',
|
|
|
|
'reading:expand:spoilers' => false,
|
2023-11-18 03:50:07 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-08 01:48:17 +00:00
|
|
|
* GET /api/v1/trends
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-11-18 03:50:07 +00:00
|
|
|
public function getTrends(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
return $this->json([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-08 01:48:17 +00:00
|
|
|
* GET /api/v1/announcements
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-11-18 03:50:07 +00:00
|
|
|
public function getAnnouncements(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
return $this->json([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-08 01:48:17 +00:00
|
|
|
* GET /api/v1/markers
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-11-18 03:50:07 +00:00
|
|
|
public function getMarkers(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('read'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$type = $request->input('timeline');
|
2024-03-05 08:58:15 +00:00
|
|
|
if (is_array($type)) {
|
2023-11-18 03:50:07 +00:00
|
|
|
$type = $type[0];
|
|
|
|
}
|
2024-03-05 08:58:15 +00:00
|
|
|
if (! $type || ! in_array($type, ['home', 'notifications'])) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json([]);
|
|
|
|
}
|
|
|
|
$pid = $request->user()->profile_id;
|
2024-03-05 08:58:15 +00:00
|
|
|
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json(MarkerService::get($pid, $type));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-02-08 01:48:17 +00:00
|
|
|
* POST /api/v1/markers
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-11-18 03:50:07 +00:00
|
|
|
public function setMarkers(Request $request)
|
|
|
|
{
|
2024-03-05 08:58:15 +00:00
|
|
|
abort_if(! $request->user() || ! $request->user()->token(), 403);
|
2024-02-08 01:48:17 +00:00
|
|
|
abort_unless($request->user()->tokenCan('write'), 403);
|
2023-11-18 03:50:07 +00:00
|
|
|
|
|
|
|
$pid = $request->user()->profile_id;
|
|
|
|
$home = $request->input('home[last_read_id]');
|
|
|
|
$notifications = $request->input('notifications[last_read_id]');
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($home) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json(MarkerService::set($pid, 'home', $home));
|
|
|
|
}
|
|
|
|
|
2024-03-05 08:58:15 +00:00
|
|
|
if ($notifications) {
|
2023-11-18 03:50:07 +00:00
|
|
|
return $this->json(MarkerService::set($pid, 'notifications', $notifications));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->json([]);
|
|
|
|
}
|
2021-04-23 04:46:05 +00:00
|
|
|
}
|