pixelfed/app/Http/Controllers/Api/ApiV1Controller.php

4268 lines
147 KiB
PHP
Raw Normal View History

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;
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;
use App\Jobs\AvatarPipeline\AvatarOptimize;
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;
use App\Jobs\MediaPipeline\MediaDeletePipeline;
2024-03-05 08:58:15 +00:00
use App\Jobs\MediaPipeline\MediaSyncLicensePipeline;
use App\Jobs\SharePipeline\SharePipeline;
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;
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,
};
2024-03-05 08:58:15 +00:00
use App\User;
use App\UserFilter;
use App\UserSetting;
use App\Util\Lexer\Autolink;
use App\Util\Lexer\PrettyNumber;
use App\Util\Localization\Localization;
2024-03-05 08:58:15 +00:00
use App\Util\Media\Filter;
use App\Util\Media\License;
2024-03-05 08:58:15 +00:00
use Cache;
use Carbon\Carbon;
use DB;
use Illuminate\Http\Request;
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;
use Purify;
2024-03-05 08:58:15 +00:00
use Storage;
2019-09-14 01:10:22 +00:00
class ApiV1Controller extends Controller
2019-09-14 03:48:16 +00:00
{
protected $fractal;
2024-03-05 08:58:15 +00:00
const PF_API_ENTITY_KEY = '_pe';
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);
}
/**
* GET /api/v1/apps/verify_credentials
*/
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);
$client = $request->user()->token()->client;
$res = [
'name' => $client->name,
'website' => null,
2024-03-05 08:58:15 +00:00
'vapid_key' => null,
];
return $this->json($res);
}
/**
* POST /api/v1/apps
*/
public function apps(Request $request)
{
2024-03-12 09:55:51 +00:00
abort_if(! (bool) config_cache('pixelfed.oauth_enabled'), 404);
$this->validate($request, [
2024-03-05 08:58:15 +00:00
'client_name' => 'required',
'redirect_uris' => 'required',
]);
$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,
];
return $this->json($res, 200, [
2024-03-05 08:58:15 +00:00
'Access-Control-Allow-Origin' => '*',
]);
}
/**
* 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);
abort_unless($request->user()->tokenCan('read'), 403);
$user = $request->user();
abort_if($user->status != null, 403);
AccountService::setLastActive($user->id);
$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' => [],
];
if ($request->has(self::PF_API_ENTITY_KEY)) {
$res['settings'] = AccountService::getAccountSettings($user->profile_id);
}
return $this->json($res);
}
/**
* GET /api/v1/accounts/{id}
*
2024-03-05 08:58:15 +00:00
* @param int $id
* @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);
abort_unless($request->user()->tokenCan('read'), 403);
$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) {
return response()->json(['error' => 'Record not found'], 404);
}
2024-03-05 08:58:15 +00:00
if ($res && strpos($res['acct'], '@') != -1) {
$domain = parse_url($res['url'], PHP_URL_HOST);
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
}
2024-03-05 08:58:15 +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);
abort_unless($request->user()->tokenCan('write'), 403);
2024-03-05 08:58:15 +00:00
if (config('pixelfed.bouncer.cloud_ips.ban_api')) {
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',
// '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),
]);
$user = $request->user();
AccountService::setLastActive($user->id);
$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')) {
$av = Avatar::whereProfileId($profile->id)->first();
2024-03-05 08:58:15 +00:00
if ($av) {
$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();
$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]')) {
$lang = $request->input('source[language]');
2024-03-05 08:58:15 +00:00
if (in_array($lang, Localization::languages())) {
$user->language = $lang;
$changes = true;
$other['language'] = $lang;
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('website')) {
$website = $request->input('website');
2024-03-05 08:58:15 +00:00
if ($website != $profile->website) {
if ($website) {
if (! strpos($website, '.')) {
$website = null;
}
2024-03-05 08:58:15 +00:00
if ($website && ! strpos($website, '://')) {
$website = 'https://'.$website;
}
$host = parse_url($website, PHP_URL_HOST);
$bannedInstances = InstanceService::getBannedDomains();
2024-03-05 08:58:15 +00:00
if (in_array($host, $bannedInstances)) {
$website = null;
}
}
$profile->website = $website ? $website : null;
$changes = true;
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('display_name')) {
$displayName = $request->input('display_name');
2024-03-05 08:58:15 +00:00
if ($displayName !== $user->name) {
$user->name = $displayName;
$profile->name = $displayName;
$changes = true;
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('note')) {
$note = $request->input('note');
2024-03-05 08:58:15 +00:00
if ($note !== strip_tags($profile->bio)) {
$profile->bio = Autolink::create()->autolink(strip_tags($note));
$changes = true;
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('locked')) {
$locked = $request->boolean('locked');
2024-03-05 08:58:15 +00:00
if ($profile->is_private != $locked) {
$profile->is_private = $locked;
$changes = true;
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('reduce_motion')) {
$reduced = $request->boolean('reduce_motion');
2024-03-05 08:58:15 +00:00
if ($settings->reduce_motion != $reduced) {
$settings->reduce_motion = $reduced;
$changes = true;
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('high_contrast_mode')) {
$contrast = $request->boolean('high_contrast_mode');
2024-03-05 08:58:15 +00:00
if ($settings->high_contrast_mode != $contrast) {
$settings->high_contrast_mode = $contrast;
$changes = true;
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('video_autoplay')) {
$autoplay = $request->boolean('video_autoplay');
2024-03-05 08:58:15 +00:00
if ($settings->video_autoplay != $autoplay) {
$settings->video_autoplay = $autoplay;
$changes = true;
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('license')) {
$license = $request->input('license');
2024-03-05 08:58:15 +00:00
abort_if(! in_array($license, License::keys()), 422, 'Invalid media license id');
$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) {
$composeSettings['default_license'] = $license;
$licenseChanged = true;
$changes = true;
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('media_descriptions')) {
$md = $request->boolean('media_descriptions');
2024-03-05 08:58:15 +00:00
if ($composeSettings['media_descriptions'] != $md) {
$composeSettings['media_descriptions'] = $md;
$changes = true;
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('crawlable')) {
$crawlable = $request->boolean('crawlable');
2024-03-05 08:58:15 +00:00
if ($settings->crawlable != $crawlable) {
$settings->crawlable = $crawlable;
$changes = true;
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('show_profile_follower_count')) {
$show_profile_follower_count = $request->boolean('show_profile_follower_count');
2024-03-05 08:58:15 +00:00
if ($settings->show_profile_follower_count != $show_profile_follower_count) {
$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);
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('show_profile_following_count')) {
$show_profile_following_count = $request->boolean('show_profile_following_count');
2024-03-05 08:58:15 +00:00
if ($settings->show_profile_following_count != $show_profile_following_count) {
$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);
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('public_dm')) {
$public_dm = $request->boolean('public_dm');
2024-03-05 08:58:15 +00:00
if ($settings->public_dm != $public_dm) {
$settings->public_dm = $public_dm;
$changes = true;
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('source[privacy]')) {
$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) {
$composeSettings['default_scope'] = $profile->is_private ? 'private' : $scope;
$changes = true;
}
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('disable_embeds')) {
$disabledEmbeds = $request->boolean('disable_embeds');
2024-03-05 08:58:15 +00:00
if ($other['disable_embeds'] != $disabledEmbeds) {
$other['disable_embeds'] = $disabledEmbeds;
$changes = true;
}
}
2024-03-05 08:58:15 +00:00
if ($changes) {
$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);
Cache::forget('profile:view:'.$profile->username);
Cache::forget('profile:atom:enabled:'.$profile->id);
Cache::forget('pfc:cached-user:wt:'.strtolower($profile->username));
Cache::forget('pfc:cached-user:wot:'.strtolower($profile->username));
Cache::forget('pf:acct:settings:hidden-followers:'.$profile->id);
Cache::forget('pf:acct:settings:hidden-following:'.$profile->id);
Cache::forget('pf:acct-trans:hideFollowing:'.$profile->id);
Cache::forget('pf:acct-trans:hideFollowers:'.$profile->id);
AccountService::del($user->profile_id);
AccountService::forgetAccountSettings($profile->id);
}
2024-03-05 08:58:15 +00:00
if ($syncLicenses && $licenseChanged) {
$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)) {
$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);
}
return $this->json($res);
}
/**
* GET /api/v1/accounts/{id}/followers
*
2024-03-05 08:58:15 +00:00
* @param int $id
* @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);
abort_unless($request->user()->tokenCan('read'), 403);
$account = AccountService::get($id);
2024-03-05 08:58:15 +00:00
abort_if(! $account, 404);
$pid = $request->user()->profile_id;
$this->validate($request, [
2024-03-05 08:58:15 +00:00
'limit' => 'sometimes|integer|min:1',
]);
$limit = $request->input('limit', 10);
2024-03-05 08:58:15 +00:00
if ($limit > 80) {
$limit = 80;
}
$napi = $request->has(self::PF_API_ENTITY_KEY);
2024-03-05 08:58:15 +00:00
if ($account && strpos($account['acct'], '@') != -1) {
$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'])) {
return [];
}
}
2024-03-05 08:58:15 +00:00
if (AccountService::hiddenFollowers($id)) {
return [];
}
2024-03-05 08:58:15 +00:00
if ($request->has('page') && $request->user()->is_admin == false) {
$page = (int) $request->input('page');
2024-03-05 08:58:15 +00:00
if (($page * $limit) >= 100) {
return [];
}
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('page')) {
$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) {
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) {
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()) {
$link = '<'.$paginator->nextPageUrl().'>; rel="prev"';
}
} else {
2024-03-05 08:58:15 +00:00
if ($paginator->previousPageUrl()) {
$link = '<'.$paginator->previousPageUrl().'>; rel="next"';
}
2024-03-05 08:58:15 +00:00
if ($paginator->hasMorePages()) {
$link .= ($link ? ', ' : '').'<'.$paginator->nextPageUrl().'>; rel="prev"';
}
}
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) {
return $account && isset($account['id']);
})
->values()
->toArray();
$headers = isset($link) ? ['Link' => $link] : [];
2024-03-05 08:58:15 +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
* @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);
abort_unless($request->user()->tokenCan('read'), 403);
$account = AccountService::get($id);
2024-03-05 08:58:15 +00:00
abort_if(! $account, 404);
$pid = $request->user()->profile_id;
$this->validate($request, [
2024-03-05 08:58:15 +00:00
'limit' => 'sometimes|integer|min:1',
]);
$limit = $request->input('limit', 10);
2024-03-05 08:58:15 +00:00
if ($limit > 80) {
$limit = 80;
}
$napi = $request->has(self::PF_API_ENTITY_KEY);
2024-03-05 08:58:15 +00:00
if ($account && strpos($account['acct'], '@') != -1) {
$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'])) {
return [];
}
}
2024-03-05 08:58:15 +00:00
if (AccountService::hiddenFollowing($id)) {
return [];
}
2024-03-05 08:58:15 +00:00
if ($request->has('page') && $request->user()->is_admin == false) {
$page = (int) $request->input('page');
2024-03-05 08:58:15 +00:00
if (($page * $limit) >= 100) {
return [];
}
}
}
2024-03-05 08:58:15 +00:00
if ($request->has('page')) {
$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) {
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) {
return $account && isset($account['id']);
})
->values()
->toArray();
2024-03-05 08:58:15 +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()) {
$link = '<'.$paginator->nextPageUrl().'>; rel="prev"';
}
} else {
2024-03-05 08:58:15 +00:00
if ($paginator->previousPageUrl()) {
$link = '<'.$paginator->previousPageUrl().'>; rel="next"';
}
2024-03-05 08:58:15 +00:00
if ($paginator->hasMorePages()) {
$link .= ($link ? ', ' : '').'<'.$paginator->nextPageUrl().'>; rel="prev"';
}
}
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) {
return $account && isset($account['id']);
})
->values()
->toArray();
$headers = isset($link) ? ['Link' => $link] : [];
2024-03-05 08:58:15 +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
* @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);
abort_unless($request->user()->tokenCan('read'), 403);
$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',
]);
$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) {
return $this->json(['error' => 'Account not found'], 404);
}
2024-03-05 08:58:15 +00:00
if ($profile && strpos($profile['acct'], '@') != -1) {
$domain = parse_url($profile['url'], PHP_URL_HOST);
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
}
$limit = $request->input('limit') ?? 20;
2024-03-05 08:58:15 +00:00
if ($limit > 40) {
$limit = 40;
}
$max_id = $request->max_id;
$min_id = $request->min_id;
2024-03-05 08:58:15 +00:00
if (! $max_id && ! $min_id) {
$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')) {
$mt = $request->input('media_type');
2024-03-05 08:58:15 +00:00
if ($mt == 'video') {
$scope = ['video', 'video:album'];
}
}
2024-03-05 08:58:15 +00:00
if (intval($pid) === intval($profile['id'])) {
$visibility = ['public', 'unlisted', 'private'];
2024-03-05 08:58:15 +00:00
} elseif ($profile['locked']) {
$following = FollowerService::follows($pid, $profile['id']);
2024-03-05 08:58:15 +00:00
if (! $following) {
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;
}
2024-03-05 08:58:15 +00:00
if ($profile) {
$status['account'] = $profile;
}
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();
return $this->json($res);
}
/**
* POST /api/v1/accounts/{id}/follow
*
2024-03-05 08:58:15 +00:00
* @param int $id
* @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);
abort_unless($request->user()->tokenCan('follow'), 403);
$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');
AccountService::setLastActive($user->id);
$target = Profile::where('id', '!=', $user->profile_id)
->whereNull('status')
->findOrFail($id);
2024-03-05 08:58:15 +00:00
if ($target && $target->domain) {
$domain = $target->domain;
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
}
$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();
2024-03-05 08:58:15 +00:00
if ($blocked == true) {
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) {
$res = RelationshipService::get($user->profile_id, $target->id) ?? [];
2024-03-05 08:58:15 +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');
}
2024-03-05 08:58:15 +00:00
if ($private == true) {
$follow = FollowRequest::firstOrCreate([
'follower_id' => $user->profile_id,
2024-03-05 08:58:15 +00:00
'following_id' => $target->id,
]);
2024-03-05 08:58:15 +00:00
if ($remote == true && config('federation.activitypub.remoteFollow') == true) {
(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,
]);
2024-03-05 08:58:15 +00:00
if ($remote == true && config('federation.activitypub.remoteFollow') == true) {
(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
* @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);
abort_unless($request->user()->tokenCan('follow'), 403);
$user = $request->user();
AccountService::setLastActive($user->id);
$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) {
$followRequest = FollowRequest::whereFollowerId($user->profile_id)
->whereFollowingId($target->id)
->first();
2024-03-05 08:58:15 +00:00
if ($followRequest) {
$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) {
(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
* @return \App\Services\RelationshipService
*/
public function accountRelationshipsById(Request $request)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user(), 403);
$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,
]);
$ids = $request->input('id');
2024-03-05 08:58:15 +00:00
if (count($ids) > 20) {
$ids = collect($ids)->take(20)->toArray();
}
$napi = $request->has(self::PF_API_ENTITY_KEY);
$pid = $request->user()->profile_id ?? $request->user()->profile->id;
$res = collect($ids)
2024-03-05 08:58:15 +00:00
->map(function ($id) use ($pid, $napi) {
if (intval($id) === intval($pid)) {
return [
'id' => $id,
'following' => false,
'followed_by' => false,
'blocking' => false,
'muting' => false,
'muting_notifications' => false,
'requested' => false,
'domain_blocking' => false,
'showing_reblogs' => false,
'endorsed' => false,
];
}
return $napi ?
RelationshipService::getWithDate($pid, $id) :
RelationshipService::get($pid, $id);
2024-03-05 08:58:15 +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);
abort_unless($request->user()->tokenCan('read'), 403);
$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',
]);
$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');
AccountService::setLastActive($user->id);
$query = $request->input('q');
$limit = $request->input('limit') ?? 20;
2024-03-05 08:58:15 +00:00
if ($limit > 20) {
$limit = 20;
}
$resolve = $request->boolean('resolve', false);
2024-03-05 08:58:15 +00:00
$q = $query.'%';
$profiles = Profile::where('username', 'like', $q)
->orderByDesc('followers_count')
->limit($limit)
->pluck('id')
2024-03-05 08:58:15 +00:00
->map(function ($id) {
return AccountService::getMastodon($id);
})
2024-03-05 08:58:15 +00:00
->filter(function ($account) {
return $account && isset($account['id']);
})
->values();
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);
abort_unless($request->user()->tokenCan('read'), 403);
$this->validate($request, [
2024-03-05 08:58:15 +00:00
'limit' => 'sometimes|integer|min:1',
'page' => 'sometimes|integer|min:1',
]);
$user = $request->user();
$limit = $request->input('limit') ?? 40;
2024-03-05 08:58:15 +00:00
if ($limit > 80) {
$limit = 80;
}
2024-03-05 08:58:15 +00:00
$blocks = UserFilter::select('filterable_id', 'filterable_type', 'filter_type', 'user_id')
->whereUserId($user->profile_id)
->whereFilterableType('App\Profile')
->whereFilterType('block')
->orderByDesc('id')
->simplePaginate($limit)
->withQueryString();
$res = $blocks->pluck('filterable_id')
2024-03-05 08:58:15 +00:00
->map(function ($id) {
return AccountService::get($id, true);
})
2024-03-05 08:58:15 +00:00
->filter(function ($account) {
return $account && isset($account['id']);
})
->values();
2024-03-05 08:58:15 +00:00
$baseUrl = config('app.url').'/api/v1/blocks?limit='.$limit.'&';
$next = $blocks->nextPageUrl();
$prev = $blocks->previousPageUrl();
2024-03-05 08:58:15 +00:00
if ($next && ! $prev) {
$link = '<'.$next.'>; rel="next"';
}
2024-03-05 08:58:15 +00:00
if (! $next && $prev) {
$link = '<'.$prev.'>; rel="prev"';
}
2024-03-05 08:58:15 +00:00
if ($next && $prev) {
$link = '<'.$next.'>; rel="next",<'.$prev.'>; rel="prev"';
}
$headers = isset($link) ? ['Link' => $link] : [];
2024-03-05 08:58:15 +00:00
return $this->json($res, 200, $headers);
}
/**
* POST /api/v1/accounts/{id}/block
*
2024-03-05 08:58:15 +00:00
* @param int $id
* @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);
abort_unless($request->user()->tokenCan('write'), 403);
$user = $request->user();
$pid = $user->profile_id ?? $user->profile->id;
AccountService::setLastActive($user->id);
2024-03-05 08:58:15 +00:00
if (intval($id) === intval($pid)) {
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) {
abort(400, 'You cannot block an admin');
}
$count = UserFilterService::blockCount($pid);
2024-03-14 06:06:33 +00:00
$maxLimit = (int) config_cache('instance.user_filters.max_user_blocks');
2024-03-05 08:58:15 +00:00
if ($count == 0) {
$filterCount = UserFilter::whereUserId($pid)
->whereFilterType('block')
->get()
2024-03-05 08:58:15 +00:00
->map(function ($rec) {
return AccountService::get($rec->filterable_id, true);
})
2024-03-05 08:58:15 +00:00
->filter(function ($account) {
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');
} else {
2024-03-05 08:58:15 +00:00
abort_if($count >= $maxLimit, 422, AccountController::FILTER_LIMIT_BLOCK_TEXT.$maxLimit.' accounts');
}
$followed = Follower::whereProfileId($profile->id)->whereFollowingId($pid)->first();
2024-03-05 08:58:15 +00:00
if ($followed) {
$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) {
$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) {
NotificationService::del($pid, $n['id']);
$n->forceDelete();
2024-03-05 08:58:15 +00:00
});
$filter = UserFilter::firstOrCreate([
2024-03-05 08:58:15 +00:00
'user_id' => $pid,
'filterable_id' => $profile->id,
'filterable_type' => 'App\Profile',
2024-03-05 08:58:15 +00:00
'filter_type' => 'block',
]);
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
* @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);
abort_unless($request->user()->tokenCan('write'), 403);
$user = $request->user();
$pid = $user->profile_id ?? $user->profile->id;
AccountService::setLastActive($user->id);
2024-03-05 08:58:15 +00:00
if (intval($id) === intval($pid)) {
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) {
$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);
abort_unless($request->user()->tokenCan('read'), 403);
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);
abort_unless($request->user()->tokenCan('read'), 403);
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);
abort_unless($request->user()->tokenCan('read'), 403);
$this->validate($request, [
2024-03-05 08:58:15 +00:00
'limit' => 'sometimes|integer|min:1',
]);
$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) {
$limit = 40;
}
$res = Like::whereProfileId($user->profile_id)
2024-03-05 08:58:15 +00:00
->when($maxId, function ($q, $maxId) {
return $q->where('id', '<', $maxId);
})
2024-03-05 08:58:15 +00:00
->when($minId, function ($q, $minId) {
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);
$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
return $status;
})
2024-03-05 08:58:15 +00:00
->filter(function ($status) {
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) {
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.'&';
$link = '<'.$baseUrl.'max_id='.$max.'>; rel="next",<'.$baseUrl.'min_id='.$min.'>; rel="prev"';
2024-03-05 08:58:15 +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
* @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);
abort_unless($request->user()->tokenCan('write'), 403);
$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');
$napi = $request->has(self::PF_API_ENTITY_KEY);
$status = $napi ? StatusService::get($id, false) : StatusService::getMastodon($id, false);
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) {
$domain = parse_url($status['account']['url'], PHP_URL_HOST);
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
}
$spid = $status['account']['id'];
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);
} else {
2024-03-05 08:58:15 +00:00
abort_if(! in_array($status['visibility'], ['public', 'unlisted']), 403);
}
}
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)) {
abort(422);
}
$like = Like::firstOrCreate([
'profile_id' => $user->profile_id,
2024-03-05 08:58:15 +00:00
'status_id' => $status['id'],
]);
2024-03-05 08:58:15 +00:00
if ($like->wasRecentlyCreated == true) {
$like->status_profile_id = $spid;
2024-03-05 08:58:15 +00:00
$like->is_comment = ! empty($status['in_reply_to_id']);
$like->save();
Status::findOrFail($status['id'])->update([
2024-03-05 08:58:15 +00:00
'likes_count' => ($status['favourites_count'] ?? 0) + 1,
]);
LikePipeline::dispatch($like)->onQueue('feed');
}
$status['favourited'] = true;
$status['favourites_count'] = $status['favourites_count'] + 1;
2024-03-05 08:58:15 +00:00
return $this->json($status);
}
/**
* POST /api/v1/statuses/{id}/unfavourite
*
2024-03-05 08:58:15 +00:00
* @param int $id
* @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);
abort_unless($request->user()->tokenCan('write'), 403);
$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');
$napi = $request->has(self::PF_API_ENTITY_KEY);
$status = $napi ? StatusService::get($id, false) : StatusService::getMastodon($id, false);
abort_unless($status && isset($status['account']), 404);
if ($status && isset($status['account'], $status['account']['acct']) && strpos($status['account']['acct'], '@') != -1) {
$domain = parse_url($status['account']['url'], PHP_URL_HOST);
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
}
$spid = $status['account']['id'];
AccountService::setLastActive($user->id);
if (intval($spid) !== intval($user->profile_id)) {
if ($status['visibility'] == 'private') {
abort_if(! FollowerService::follows($user->profile_id, $spid), 403);
} else {
abort_if(! in_array($status['visibility'], ['public', 'unlisted']), 403);
}
}
$like = Like::whereProfileId($user->profile_id)
->whereStatusId($status['id'])
->first();
2024-03-05 08:58:15 +00:00
if ($like) {
$like->forceDelete();
$ogStatus = Status::find($status['id']);
if ($ogStatus) {
$ogStatus->likes_count = $ogStatus->likes_count > 1 ? $ogStatus->likes_count - 1 : 0;
$ogStatus->save();
}
}
StatusService::del($status['id']);
$status['favourited'] = false;
$status['favourites_count'] = isset($ogStatus) ? $ogStatus->likes_count : $status['favourites_count'] - 1;
2024-03-05 08:58:15 +00:00
return $this->json($status);
}
/**
* 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);
abort_unless($request->user()->tokenCan('read'), 403);
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);
abort_unless($request->user()->tokenCan('read'), 403);
$this->validate($request, [
2024-03-05 08:58:15 +00:00
'limit' => 'sometimes|integer|min:1|max:100',
]);
$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) {
return AccountService::getMastodon($id, true);
})
2024-03-05 08:58:15 +00:00
->filter(function ($acct) {
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
* @return null
*/
public function accountFollowRequestAccept(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('follow'), 403);
$pid = $request->user()->profile_id;
$target = AccountService::getMastodon($id);
2024-03-05 08:58:15 +00:00
if (! $target) {
return response()->json(['error' => 'Record not found'], 404);
}
2024-03-05 08:58:15 +00:00
if ($target && strpos($target['acct'], '@') != -1) {
$domain = parse_url($target['url'], PHP_URL_HOST);
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
}
$followRequest = FollowRequest::whereFollowingId($pid)->whereFollowerId($id)->first();
2024-03-05 08:58:15 +00:00
if (! $followRequest) {
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) {
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
return $this->json($res);
}
/**
* POST /api/v1/follow_requests/{id}/reject
*
2024-03-05 08:58:15 +00:00
* @param int $id
* @return null
*/
public function accountFollowRequestReject(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('follow'), 403);
$pid = $request->user()->profile_id;
$target = AccountService::getMastodon($id);
2024-03-05 08:58:15 +00:00
if (! $target) {
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) {
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) {
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
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);
abort_unless($request->user()->tokenCan('read'), 403);
// 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')) {
return AccountService::getMastodon(config_cache('instance.admin.pid'), true);
}
$admin = User::whereIsAdmin(true)->first();
2024-03-05 08:58:15 +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() : [];
});
return [
'uri' => config('pixelfed.domain.app'),
'title' => config_cache('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').')',
'urls' => [
2024-02-19 11:00:31 +00:00
'streaming_api' => null,
],
'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'),
'contact_account' => $contact,
'rules' => $rules,
'configuration' => [
'media_attachments' => [
'image_matrix_limit' => 16777216,
2024-03-11 06:29:55 +00:00
'image_size_limit' => config_cache('pixelfed.max_photo_size') * 1024,
2024-03-12 07:28:08 +00:00
'supported_mime_types' => explode(',', config_cache('pixelfed.media_types')),
'video_frame_rate_limit' => 120,
'video_matrix_limit' => 2304000,
2024-03-11 06:29:55 +00:00
'video_size_limit' => config_cache('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,
],
'statuses' => [
'characters_reserved_per_url' => 23,
2024-03-10 10:19:44 +00:00
'max_characters' => (int) config_cache('pixelfed.max_caption_length'),
'max_media_attachments' => (int) config_cache('pixelfed.max_album_length'),
2024-03-05 08:58:15 +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);
abort_unless($request->user()->tokenCan('read'), 403);
return response()->json([]);
}
/**
* GET /api/v1/accounts/{id}/lists
*
2024-03-05 08:58:15 +00:00
* @param int $id
* @return null
*/
public function accountListsById(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('read'), 403);
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);
abort_unless($request->user()->tokenCan('write'), 403);
$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'),
],
'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'),
],
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'),
]);
$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
AccountService::setLastActive($user->id);
2024-03-05 08:58:15 +00:00
if ($user->last_active_at == null) {
return [];
}
2024-03-05 08:58:15 +00:00
if (empty($request->file('file'))) {
return response('', 422);
}
2024-03-05 08:58:15 +00:00
$limitKey = 'compose:rate-limit:media-upload:'.$user->id;
$limitTtl = now()->addMinutes(15);
2024-03-05 08:58:15 +00:00
$limitReached = Cache::remember($limitKey, $limitTtl, function () use ($user) {
$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) {
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.');
}
}
$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) {
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)) {
$compose = $settings->compose_settings;
2024-03-05 08:58:15 +00:00
if (isset($compose['default_license']) && $compose['default_license'] != 1) {
$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) {
$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();
return $this->json($res);
}
/**
* PUT /api/v1/media/{id}
*
2024-03-05 08:58:15 +00:00
* @param int $id
* @return MediaTransformer
*/
public function mediaUpdate(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('write'), 403);
$this->validate($request, [
2024-03-05 08:58:15 +00:00
'description' => 'nullable|string|max:'.config_cache('pixelfed.max_altext_length'),
]);
$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');
AccountService::setLastActive($user->id);
$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) {
$caption = Purify::clean($request->input('description'));
2024-03-05 08:58:15 +00:00
if ($caption != $media->caption) {
$media->caption = $caption;
$media->save();
2024-03-05 08:58:15 +00:00
if ($media->status_id) {
MediaService::del($media->status_id);
StatusService::del($media->status_id);
}
}
2024-03-05 08:58:15 +00:00
});
2024-03-05 08:58:15 +00:00
if (! $executed) {
return response()->json([
2024-03-05 08:58:15 +00:00
'error' => 'Too many attempts. Try again in a few minutes.',
], 429);
2024-03-05 08:58:15 +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
return $this->json($fractal->createData($resource)->toArray());
}
/**
* GET /api/v1/media/{id}
*
2024-03-05 08:58:15 +00:00
* @param int $id
* @return MediaTransformer
*/
public function mediaGet(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('read'), 403);
$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');
AccountService::setLastActive($user->id);
$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
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);
abort_unless($request->user()->tokenCan('write'), 403);
$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'),
],
'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'),
],
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',
]);
$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-03-05 08:58:15 +00:00
if ($user->last_active_at == null) {
return [];
}
AccountService::setLastActive($user->id);
2024-03-05 08:58:15 +00:00
if (empty($request->file('file'))) {
return response('', 422);
}
2024-03-05 08:58:15 +00:00
$limitKey = 'compose:rate-limit:media-upload:'.$user->id;
$limitTtl = now()->addMinutes(15);
2024-03-05 08:58:15 +00:00
$limitReached = Cache::remember($limitKey, $limitTtl, function () use ($user) {
$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) {
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.');
}
}
$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) {
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)) {
$compose = $settings->compose_settings;
2024-03-05 08:58:15 +00:00
if (isset($compose['default_license']) && $compose['default_license'] != 1) {
$license = $compose['default_license'];
}
}
abort_if(MediaBlocklistService::exists($hash) == true, 451);
2024-03-05 08:58:15 +00:00
if ($request->has('replace_id')) {
$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) {
$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) {
$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'] = null;
2024-03-05 08:58:15 +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);
abort_unless($request->user()->tokenCan('read'), 403);
$this->validate($request, [
2024-03-05 08:58:15 +00:00
'limit' => 'sometimes|integer|min:1',
]);
$user = $request->user();
$limit = $request->input('limit', 40);
2024-03-05 08:58:15 +00:00
if ($limit > 80) {
$limit = 80;
}
$mutes = UserFilter::whereUserId($user->profile_id)
->whereFilterableType('App\Profile')
->whereFilterType('mute')
->orderByDesc('id')
->simplePaginate($limit)
->withQueryString();
$res = $mutes->pluck('filterable_id')
2024-03-05 08:58:15 +00:00
->map(function ($id) {
return AccountService::get($id, true);
})
2024-03-05 08:58:15 +00:00
->filter(function ($account) {
return $account && isset($account['id']);
})
->values();
2024-03-05 08:58:15 +00:00
$baseUrl = config('app.url').'/api/v1/mutes?limit='.$limit.'&';
$next = $mutes->nextPageUrl();
$prev = $mutes->previousPageUrl();
2024-03-05 08:58:15 +00:00
if ($next && ! $prev) {
$link = '<'.$next.'>; rel="next"';
}
2024-03-05 08:58:15 +00:00
if (! $next && $prev) {
$link = '<'.$prev.'>; rel="prev"';
}
2024-03-05 08:58:15 +00:00
if ($next && $prev) {
$link = '<'.$next.'>; rel="next",<'.$prev.'>; rel="prev"';
}
$headers = isset($link) ? ['Link' => $link] : [];
2024-03-05 08:58:15 +00:00
return $this->json($res, 200, $headers);
}
/**
* POST /api/v1/accounts/{id}/mute
*
2024-03-05 08:58:15 +00:00
* @param int $id
* @return RelationshipTransformer
*/
public function accountMuteById(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('write'), 403);
$user = $request->user();
$pid = $user->profile_id;
2024-03-05 08:58:15 +00:00
if (intval($pid) === intval($id)) {
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) {
$domain = $account->domain;
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
}
$count = UserFilterService::muteCount($pid);
2024-03-14 06:06:33 +00:00
$maxLimit = (int) config_cache('instance.user_filters.max_user_mutes');
2024-03-05 08:58:15 +00:00
if ($count == 0) {
$filterCount = UserFilter::whereUserId($pid)
->whereFilterType('mute')
->get()
2024-03-05 08:58:15 +00:00
->map(function ($rec) {
return AccountService::get($rec->filterable_id, true);
})
2024-03-05 08:58:15 +00:00
->filter(function ($account) {
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');
} else {
2024-03-05 08:58:15 +00:00
abort_if($count >= $maxLimit, 422, AccountController::FILTER_LIMIT_MUTE_TEXT.$maxLimit.' accounts');
}
$filter = UserFilter::firstOrCreate([
2024-03-05 08:58:15 +00:00
'user_id' => $pid,
'filterable_id' => $account->id,
'filterable_type' => 'App\Profile',
2024-03-05 08:58:15 +00:00
'filter_type' => 'mute',
]);
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
return $this->json($res);
}
/**
* POST /api/v1/accounts/{id}/unmute
*
2024-03-05 08:58:15 +00:00
* @param int $id
* @return RelationshipTransformer
*/
public function accountUnmuteById(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('write'), 403);
$user = $request->user();
$pid = $user->profile_id;
2024-03-05 08:58:15 +00:00
if (intval($pid) === intval($id)) {
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) {
$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
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);
abort_unless($request->user()->tokenCan('read'), 403);
$this->validate($request, [
'limit' => 'sometimes|integer|min:1',
'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,
'types[]' => 'sometimes|array',
'type' => 'sometimes|string|in:mention,reblog,follow,favourite',
'_pe' => 'sometimes',
]);
$pid = $request->user()->profile_id;
$limit = $request->input('limit', 20);
2024-03-05 08:58:15 +00:00
if ($limit > 40) {
$limit = 40;
}
$since = $request->input('since_id');
$min = $request->input('min_id');
$max = $request->input('max_id');
$pe = $request->filled('_pe');
2024-03-05 08:58:15 +00:00
if (! $since && ! $min && ! $max) {
$min = 1;
}
2024-03-05 08:58:15 +00:00
if ($since) {
$min = $since + 1;
}
$types = $request->input('types');
$maxId = null;
$minId = null;
AccountService::setLastActive($request->user()->id);
$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)) {
$maxId = max($ids);
$minId = min($ids);
}
2024-03-05 08:58:15 +00:00
if (empty($res)) {
if (! Cache::has('pf:services:notifications:hasSynced:'.$pid)) {
Cache::put('pf:services:notifications:hasSynced:'.$pid, 1, 1209600);
NotificationService::warmCache($pid, 400, true);
}
}
2024-03-05 08:58:15 +00:00
$baseUrl = config('app.url').'/api/v1/notifications?limit='.$limit.'&';
2024-03-05 08:58:15 +00:00
if ($minId == $maxId) {
$minId = null;
}
$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;
}
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 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:58:15 +00:00
return true;
})->values();
2024-03-05 08:58:15 +00:00
if ($maxId) {
$link = '<'.$baseUrl.'max_id='.$minId.'>; rel="next"';
}
2024-03-05 08:58:15 +00:00
if ($minId) {
$link = '<'.$baseUrl.'min_id='.$maxId.'>; rel="prev"';
}
2024-03-05 08:58:15 +00:00
if ($maxId && $minId) {
$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
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);
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',
'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) {
$limit = 40;
}
$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'];
AccountService::setLastActive($request->user()->id);
2024-03-05 08:58:15 +00:00
if (config('exp.cached_home_timeline')) {
$paddedLimit = $includeReblogs ? $limit + 10 : $limit + 50;
2024-03-05 08:58:15 +00:00
if ($min || $max) {
if ($request->has('min_id')) {
$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);
FeedWarmCachePipeline::dispatchSync($pid);
2024-03-05 08:58:15 +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);
return response()->json([], 206);
}
}
$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();
2024-03-05 08:58:15 +00:00
$baseUrl = config('app.url').'/api/v1/timelines/home?limit='.$limit.'&';
$minId = $res->map(function ($s) {
return ['id' => $s['id']];
})->min('id');
2024-03-05 08:58:15 +00:00
$maxId = $res->map(function ($s) {
return ['id' => $s['id']];
})->max('id');
2024-03-05 08:58:15 +00:00
if ($minId == $maxId) {
$minId = null;
}
2024-03-05 08:58:15 +00:00
if ($maxId) {
$link = '<'.$baseUrl.'max_id='.$minId.'>; rel="next"';
}
2024-03-05 08:58:15 +00:00
if ($minId) {
$link = '<'.$baseUrl.'min_id='.$maxId.'>; rel="prev"';
}
2024-03-05 08:58:15 +00:00
if ($maxId && $minId) {
$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
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) {
$following = Follower::whereProfileId($pid)->pluck('following_id');
2024-03-05 08:58:15 +00:00
return $following->push($pid)->toArray();
});
$muted = UserFilterService::mutes($pid);
2024-03-05 08:58:15 +00:00
if ($muted && count($muted)) {
$following = array_diff($following, $muted);
}
2024-03-05 08:58:15 +00:00
if ($min || $max) {
$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) {
return false;
}
2024-03-05 08:58:15 +00:00
$status['account'] = $account;
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']);
}
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();
} 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) {
return false;
}
2024-03-05 08:58:15 +00:00
$status['account'] = $account;
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']);
}
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();
}
2024-03-05 08:58:15 +00:00
$baseUrl = config('app.url').'/api/v1/timelines/home?limit='.$limit.'&';
$minId = $res->map(function ($s) {
return ['id' => $s['id']];
})->min('id');
2024-03-05 08:58:15 +00:00
$maxId = $res->map(function ($s) {
return ['id' => $s['id']];
})->max('id');
2024-03-05 08:58:15 +00:00
if ($minId == $maxId) {
$minId = null;
}
2024-03-05 08:58:15 +00:00
if ($maxId) {
$link = '<'.$baseUrl.'max_id='.$minId.'>; rel="next"';
}
2024-03-05 08:58:15 +00:00
if ($minId) {
$link = '<'.$baseUrl.'min_id='.$maxId.'>; rel="prev"';
}
2024-03-05 08:58:15 +00:00
if ($maxId && $minId) {
$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
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',
]);
$napi = $request->has(self::PF_API_ENTITY_KEY);
$min = $request->input('min_id');
$max = $request->input('max_id');
if ($max == 0) {
$min = 1;
}
$minOrMax = $request->anyFilled(['max_id', 'min_id']);
$limit = $request->input('limit') ?? 20;
2024-03-05 08:58:15 +00:00
if ($limit > 40) {
$limit = 40;
}
$user = $request->user();
2023-12-27 09:51:47 +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 [];
}
$filtered = $user ? UserFilterService::filters($user->profile_id) : [];
AccountService::setLastActive($user->id);
$domainBlocks = UserFilterService::domainBlocks($user->profile_id);
$hideNsfw = config('instance.hide_nsfw_on_public_feeds');
$amin = SnowflakeService::byDate(now()->subDays(config('federation.network_timeline_days_falloff')));
$asf = AdminShadowFilterService::getHideFromPublicFeedsList();
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) {
NetworkTimelineService::warmCache(true, config('instance.timeline.network.cache_dropoff'));
}
});
if ($max) {
$feed = NetworkTimelineService::getRankedMaxId($max, $limit + 5);
2024-03-05 08:58:15 +00:00
} elseif ($min) {
$feed = NetworkTimelineService::getRankedMinId($min, $limit + 5);
} else {
$feed = NetworkTimelineService::get(0, $limit + 5);
}
} else {
$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(false)
->whereScope('public')
->where('id', '>', $amin)
->orderByDesc('id')
->limit(($limit * 2))
->pluck('id')
->values()
->toArray();
}
} 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) {
PublicTimelineService::warmCache(true, 400);
}
});
if ($max) {
$feed = PublicTimelineService::getRankedMaxId($max, $limit + 5);
2024-03-05 08:58:15 +00:00
} elseif ($min) {
$feed = PublicTimelineService::getRankedMinId($min, $limit + 5);
} else {
$feed = PublicTimelineService::get(0, $limit + 5);
}
} else {
$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();
}
}
$res = collect($feed)
2024-03-05 08:58:15 +00:00
->filter(function ($k) use ($min, $max) {
if (! $min && ! $max) {
return true;
}
2024-03-05 08:58:15 +00:00
if ($min) {
return $min != $k;
}
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) {
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;
}
2024-03-05 08:58:15 +00:00
$status['account'] = $account;
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));
2024-03-05 08:58:15 +00:00
return ! in_array($domain, $domainBlocks);
})
->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) {
$baseUrl .= 'remote=1&';
}
if ($local) {
$baseUrl .= 'local=1&';
}
2024-03-05 08:58:15 +00:00
$minId = $res->map(function ($s) {
return ['id' => $s['id']];
})->min('id');
2024-03-05 08:58:15 +00:00
$maxId = $res->map(function ($s) {
return ['id' => $s['id']];
})->max('id');
2024-03-05 08:58:15 +00:00
if ($minId == $maxId) {
$minId = null;
}
2024-03-05 08:58:15 +00:00
if ($maxId) {
$link = '<'.$baseUrl.'max_id='.$minId.'>; rel="next"';
}
2024-03-05 08:58:15 +00:00
if ($minId) {
$link = '<'.$baseUrl.'min_id='.$maxId.'>; rel="prev"';
}
2024-03-05 08:58:15 +00:00
if ($maxId && $minId) {
$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
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);
abort_unless($request->user()->tokenCan('read'), 403);
$this->validate($request, [
'limit' => 'min:1|max:40',
2024-03-05 08:58:15 +00:00
'scope' => 'nullable|in:inbox,sent,requests',
]);
$limit = $request->input('limit', 20);
$scope = $request->input('scope', 'inbox');
$user = $request->user();
2024-03-05 08:58:15 +00:00
if ($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id)) {
return [];
}
$pid = $user->profile_id;
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) {
return $q->whereFromId($pid)->groupBy(['to_id', 'id']);
})
2024-03-05 08:58:15 +00:00
->when($scope === 'requests', function ($q, $scope) use ($pid) {
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) {
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);
});
}
$dms = $dms->orderByDesc('status_id')
->simplePaginate($limit)
2024-03-05 08:58:15 +00:00
->map(function ($dm) use ($pid) {
$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),
],
2024-03-05 08:58:15 +00:00
'last_status' => StatusService::getDirectMessage($dm->status_id),
];
2024-03-05 08:58:15 +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'])) {
return false;
}
2024-03-05 08:58:15 +00:00
return true;
})
2024-03-05 08:58:15 +00:00
->unique(function ($item, $key) {
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
* @return StatusTransformer
*/
public function statusById(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('read'), 403);
AccountService::setLastActive($request->user()->id);
$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'])) {
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) {
$domain = parse_url($res['account']['url'], PHP_URL_HOST);
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
}
$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)) {
abort_unless(FollowerService::follows($pid, $res['account']['id']), 403);
}
} else {
abort(400, 'Invalid request');
}
}
2024-03-05 08:58:15 +00:00
if (! empty($res['reblog']) && isset($res['reblog']['id'])) {
$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']);
}
$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
* @return StatusTransformer
*/
public function statusContext(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('read'), 403);
$user = $request->user();
$pid = $user->profile_id;
$status = StatusService::getMastodon($id, false);
$pe = $request->has(self::PF_API_ENTITY_KEY);
2024-03-05 08:58:15 +00:00
if (! $status || ! isset($status['account'])) {
return response('', 404);
}
2024-03-05 08:58:15 +00:00
if ($status && isset($status['account'], $status['account']['acct']) && strpos($status['account']['acct'], '@') != -1) {
$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'])) {
return response('', 404);
}
} else {
2024-03-05 08:58:15 +00:00
if (! in_array($status['visibility'], ['public', 'unlisted'])) {
return response('', 404);
}
}
}
$ancestors = [];
$descendants = [];
2024-03-05 08:58:15 +00:00
if ($status['in_reply_to_id']) {
$ancestors[] = $pe ?
StatusService::get($status['in_reply_to_id'], false) :
StatusService::getMastodon($status['in_reply_to_id'], false);
}
2024-03-05 08:58:15 +00:00
if ($status['replies_count']) {
$filters = UserFilterService::filters($pid);
$descendants = DB::table('statuses')
->where('in_reply_to_id', $id)
->limit(20)
->pluck('id')
->map(function ($sid) use ($pe) {
return $pe ?
StatusService::get($sid, false) :
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);
})
2024-03-05 08:58:15 +00:00
->map(function ($status) use ($pid) {
$status['favourited'] = LikeService::liked($pid, $status['id']);
$status['reblogged'] = ReblogService::get($pid, $status['id']);
2024-03-05 08:58:15 +00:00
return $status;
})
->values();
}
$res = [
'ancestors' => $ancestors,
2024-03-05 08:58:15 +00:00
'descendants' => $descendants,
];
return $this->json($res);
}
/**
* GET /api/v1/statuses/{id}/card
*
2024-03-05 08:58:15 +00:00
* @param int $id
* @return StatusTransformer
*/
public function statusCard(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('read'), 403);
$res = [];
2024-03-05 08:58:15 +00:00
return response()->json($res);
}
/**
* GET /api/v1/statuses/{id}/reblogged_by
*
2024-03-05 08:58:15 +00:00
* @param int $id
* @return AccountTransformer
*/
public function statusRebloggedBy(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('read'), 403);
$this->validate($request, [
2024-03-05 08:58:15 +00:00
'limit' => 'sometimes|integer|min:1|max:80',
]);
$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) {
$domain = parse_url($account['url'], PHP_URL_HOST);
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
}
$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']),
404,
);
2024-03-05 08:58:15 +00:00
if (! $author) {
if ($status->scope == 'private') {
abort_if(! FollowerService::follows($pid, $status->profile_id), 403);
} else {
2024-03-05 08:58:15 +00:00
abort_if(! in_array($status->scope, ['public', 'unlisted']), 403);
}
2024-03-05 08:58:15 +00:00
if ($request->has('cursor')) {
return $this->json([]);
}
}
$res = Status::where('reblog_of_id', $status->id)
2024-03-05 08:58:15 +00:00
->orderByDesc('id')
->cursorPaginate($limit)
->withQueryString();
2024-03-05 08:58:15 +00:00
if (! $res) {
return $this->json([]);
}
$headers = [];
2024-03-05 08:58:15 +00:00
if ($author && $res->hasPages()) {
$links = '';
2024-03-05 08:58:15 +00:00
if ($res->onFirstPage()) {
if ($res->nextPageUrl()) {
$links = '<'.$res->nextPageUrl().'>; rel="prev"';
}
} else {
2024-03-05 08:58:15 +00:00
if ($res->previousPageUrl()) {
$links = '<'.$res->previousPageUrl().'>; rel="next"';
}
2024-03-05 08:58:15 +00:00
if ($res->nextPageUrl()) {
if (! empty($links)) {
$links .= ', ';
}
2024-03-05 08:58:15 +00:00
$links .= '<'.$res->nextPageUrl().'>; rel="prev"';
}
}
$headers = ['Link' => $links];
}
2024-03-05 08:58:15 +00:00
$res = $res->map(function ($status) use ($pid, $napi) {
$account = $napi ? AccountService::get($status->profile_id, true) : AccountService::getMastodon($status->profile_id, true);
2024-03-05 08:58:15 +00:00
if (! $account) {
return false;
}
2024-03-05 08:58:15 +00:00
if ($napi) {
$account['follows'] = $status->profile_id == $pid ? null : FollowerService::follows($pid, $status->profile_id);
}
2024-03-05 08:58:15 +00:00
return $account;
})
2024-03-05 08:58:15 +00:00
->filter(function ($account) {
return $account && isset($account['id']);
})
->values();
return $this->json($res, 200, $headers);
}
/**
* GET /api/v1/statuses/{id}/favourited_by
*
2024-03-05 08:58:15 +00:00
* @param int $id
* @return AccountTransformer
*/
public function statusFavouritedBy(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('read'), 403);
$this->validate($request, [
2024-03-05 08:58:15 +00:00
'limit' => 'sometimes|integer|min:1',
]);
$limit = $request->input('limit', 40);
2024-03-05 08:58:15 +00:00
if ($limit > 80) {
$limit = 80;
}
$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) {
$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);
$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']),
404,
);
2024-03-05 08:58:15 +00:00
if (! $author) {
if ($status->scope == 'private') {
abort_if(! FollowerService::follows($pid, $status->profile_id), 403);
} else {
2024-03-05 08:58:15 +00:00
abort_if(! in_array($status->scope, ['public', 'unlisted']), 403);
}
2024-03-05 08:58:15 +00:00
if ($request->has('cursor')) {
return $this->json([]);
}
}
$res = Like::where('status_id', $status->id)
2024-03-05 08:58:15 +00:00
->orderByDesc('id')
->cursorPaginate($limit)
->withQueryString();
2024-03-05 08:58:15 +00:00
if (! $res) {
return $this->json([]);
}
$headers = [];
2024-03-05 08:58:15 +00:00
if ($author && $res->hasPages()) {
$links = '';
2024-03-05 08:58:15 +00:00
if ($res->onFirstPage()) {
if ($res->nextPageUrl()) {
$links = '<'.$res->nextPageUrl().'>; rel="prev"';
}
} else {
2024-03-05 08:58:15 +00:00
if ($res->previousPageUrl()) {
$links = '<'.$res->previousPageUrl().'>; rel="next"';
}
2024-03-05 08:58:15 +00:00
if ($res->nextPageUrl()) {
if (! empty($links)) {
$links .= ', ';
}
2024-03-05 08:58:15 +00:00
$links .= '<'.$res->nextPageUrl().'>; rel="prev"';
}
}
$headers = ['Link' => $links];
}
2024-03-05 08:58:15 +00:00
$res = $res->map(function ($like) use ($pid, $napi) {
$account = $napi ? AccountService::get($like->profile_id, true) : AccountService::getMastodon($like->profile_id, true);
2024-03-05 08:58:15 +00:00
if (! $account) {
return false;
}
2024-03-05 08:58:15 +00:00
if ($napi) {
$account['follows'] = $like->profile_id == $pid ? null : FollowerService::follows($pid, $like->profile_id);
}
2024-03-05 08:58:15 +00:00
return $account;
})
2024-03-05 08:58:15 +00:00
->filter(function ($account) {
return $account && isset($account['id']);
})
->values();
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);
abort_unless($request->user()->tokenCan('write'), 403);
$this->validate($request, [
'status' => 'nullable|string|max:'.(int) config_cache('pixelfed.max_caption_length'),
'in_reply_to_id' => 'nullable',
'media_ids' => 'sometimes|array|max:'.(int) config_cache('pixelfed.max_album_length'),
'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'));
$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) {
$blockedKeywords = config('costar.keyword.block');
2024-03-05 08:58:15 +00:00
if ($blockedKeywords !== null && $request->status) {
$keywords = config('costar.keyword.block');
2024-03-05 08:58:15 +00:00
foreach ($keywords as $kw) {
if (Str::contains($request->status, $kw) == true) {
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')) {
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-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');
} else {
2024-03-05 08:58:15 +00:00
abort_if(! UserRoleService::can('can-post', $user->id), 403, 'Invalid permissions for this action');
}
}
$profile = $user->profile;
2024-03-05 08:58:15 +00:00
$limitKey = 'compose:rate-limit:store:'.$user->id;
$limitTtl = now()->addMinutes(15);
2024-03-05 08:58:15 +00:00
$limitReached = Cache::remember($limitKey, $limitTtl, function () use ($user) {
$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) {
return [];
}
$content = strip_tags($request->input('status'));
$rendered = Autolink::create()->autolink($content);
$cw = $user->profile->cw == true ? true : $request->boolean('sensitive', false);
$spoilerText = $cw && $request->filled('spoiler_text') ? $request->input('spoiler_text') : null;
2024-03-05 08:58:15 +00:00
if ($in_reply_to_id) {
$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);
}
$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);
}
2024-03-05 08:58:15 +00:00
if ($ids) {
if (Media::whereUserId($user->id)
->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) {
$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')) {
$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 > (int) config_cache('pixelfed.max_album_length')) {
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) {
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)) {
$status->delete();
abort(400, 'Invalid media ids');
}
2024-03-05 08:58:15 +00:00
if ($request->has('comments_disabled') && $request->input('comments_disabled')) {
$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) {
abort(500, 'An error occured.');
}
NewStatusPipeline::dispatch($status);
2024-03-05 08:58:15 +00:00
if ($status->in_reply_to_id) {
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);
Cache::forget($limitKey);
2024-03-05 08:58:15 +00:00
if ($request->has('collection_ids') && $ids) {
$collections = Collection::whereProfileId($user->profile_id)
->find($request->input('collection_ids'))
2024-03-05 08:58:15 +00:00
->each(function ($collection) use ($status) {
$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,
]);
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);
});
}
$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
return $this->json($res);
}
/**
* DELETE /api/v1/statuses
*
2024-03-05 08:58:15 +00:00
* @param int $id
* @return null
*/
public function statusDelete(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('write'), 403);
AccountService::setLastActive($request->user()->id);
$status = Status::whereProfileId($request->user()->profile->id)
2024-03-05 08:58:15 +00:00
->findOrFail($id);
$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
* @return StatusTransformer
*/
public function statusShare(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('write'), 403);
$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');
AccountService::setLastActive($user->id);
$status = Status::whereScope('public')->findOrFail($id);
2024-03-05 08:58:15 +00:00
if ($status && ($status->uri || $status->url || $status->object_url)) {
$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);
} else {
2024-03-05 08:58:15 +00:00
abort_if(! in_array($status->scope, ['public', 'unlisted']), 403);
}
$blocks = UserFilterService::blocks($status->profile_id);
2024-03-05 08:58:15 +00:00
if ($blocks && in_array($user->profile_id, $blocks)) {
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',
]);
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
* @return StatusTransformer
*/
public function statusUnshare(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('write'), 403);
$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');
AccountService::setLastActive($user->id);
$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);
} else {
2024-03-05 08:58:15 +00:00
abort_if(! in_array($status->scope, ['public', 'unlisted']), 403);
}
}
$reblog = Status::whereProfileId($user->profile_id)
2024-03-05 08:58:15 +00:00
->whereReblogOfId($status->id)
->first();
2024-03-05 08:58:15 +00:00
if (! $reblog) {
$res = StatusService::getMastodon($status->id);
$res['reblogged'] = false;
2024-03-05 08:58:15 +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);
abort_unless($request->user()->tokenCan('read'), 403);
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',
'only_media' => 'sometimes',
2024-03-05 08:58:15 +00:00
'_pe' => 'sometimes',
]);
$user = $request->user();
abort_if(
2024-03-05 08:58:15 +00:00
$user->has_roles && ! UserRoleService::can('can-view-hashtag-feed', $user->id),
403,
'Invalid permissions for this action'
);
2024-03-05 08:58:15 +00:00
if (config('database.default') === 'pgsql') {
$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();
}
2024-03-05 08:58:15 +00:00
if (! $tag) {
return response()->json([]);
}
2024-03-05 08:58:15 +00:00
if ($tag->is_banned == true) {
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) {
$limit = 40;
}
$onlyMedia = $request->boolean('only_media', true);
$pe = $request->has(self::PF_API_ENTITY_KEY);
$pid = $request->user()->profile_id;
2024-03-05 08:58:15 +00:00
if ($min || $max) {
$minMax = SnowflakeService::byDate(now()->subMonths(6));
2024-03-05 08:58:15 +00:00
if ($min && intval($min) < $minMax) {
return [];
}
2024-03-05 08:58:15 +00:00
if ($max && intval($max) < $minMax) {
return [];
}
}
$filters = UserFilterService::filters($pid);
$domainBlocks = UserFilterService::domainBlocks($pid);
2024-03-05 08:58:15 +00:00
if (! $min && ! $max) {
$id = 1;
$dir = '>';
} else {
$dir = $min ? '>' : '<';
$id = $min ?? $max;
}
$res = StatusHashtag::whereHashtagId($tag->id)
->whereIn('status_visibility', ['public', 'private', 'unlisted'])
->where('status_id', $dir, $id)
->orderBy('status_id', 'desc')
->limit(100)
->pluck('status_id')
2024-03-05 08:58:15 +00:00
->map(function ($i) use ($pe) {
return $pe ? StatusService::get($i, false) : StatusService::getMastodon($i, false);
})
->filter(function ($i) use ($onlyMedia, $pid) {
if (! $i || ! isset($i['account'], $i['account']['id'])) {
return false;
}
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;
}
}
2024-03-05 08:58:15 +00:00
return $i && isset($i['account'], $i['url']);
})
2024-03-05 08:58:15 +00:00
->filter(function ($i) use ($filters, $domainBlocks) {
$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);
})
->take($limit)
->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);
abort_unless($request->user()->tokenCan('read'), 403);
$this->validate($request, [
'limit' => 'sometimes|integer|min:1',
'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',
]);
$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) {
$limit = 40;
}
$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)
->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);
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;
})
->filter()
->values()
->toArray();
$links = null;
$headers = [];
2024-03-05 08:58:15 +00:00
if ($bookmarkQuery->nextCursor()) {
$links .= '<'.$bookmarkQuery->nextPageUrl().'&limit='.$limit.'>; rel="next"';
}
2024-03-05 08:58:15 +00:00
if ($bookmarkQuery->previousCursor()) {
if ($links != null) {
$links .= ', ';
}
$links .= '<'.$bookmarkQuery->previousPageUrl().'&limit='.$limit.'>; rel="prev"';
}
2024-03-05 08:58:15 +00:00
if ($links) {
$headers = ['Link' => $links];
}
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);
abort_unless($request->user()->tokenCan('write'), 403);
$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');
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);
2024-03-05 08:58:15 +00:00
if ($status->scope == 'private') {
abort_if(
2024-03-05 08:58:15 +00:00
$pid !== $status->profile_id && ! FollowerService::follows($pid, $status->profile_id),
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,
]);
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);
abort_unless($request->user()->tokenCan('write'), 403);
$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');
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);
$bookmark = Bookmark::whereStatusId($status->id)
->whereProfileId($pid)
->first();
2024-03-05 08:58:15 +00:00
if ($bookmark) {
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);
abort_unless($request->user()->tokenCan('read'), 403);
$this->validate($request, [
2024-03-05 08:58:15 +00:00
'limit' => 'integer|min:1|max:40',
]);
$limit = $request->input('limit', 40);
$pid = $request->user()->profile_id;
$filters = UserFilterService::filters($pid);
$forYou = DiscoverService::getForYou();
$posts = $forYou->take(50)->map(function ($post) {
return StatusService::getMastodon($post);
})
->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);
})
->take(12)
->values();
2024-03-05 08:58:15 +00:00
return $this->json(compact('posts'));
}
/**
* GET /api/v2/statuses/{id}/replies
*
*
* @return array
*/
public function statusReplies(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user(), 403);
$this->validate($request, [
'limit' => 'sometimes|integer|min:1',
2024-03-05 08:58:15 +00:00
'sort' => 'in:all,newest,popular',
]);
$limit = $request->input('limit', 3);
2024-03-05 08:58:15 +00:00
if ($limit > 10) {
$limit = 10;
}
$pid = $request->user()->profile_id;
$status = StatusService::getMastodon($id, false);
2024-03-05 08:58:15 +00:00
abort_if(! $status, 404);
2024-03-05 08:58:15 +00:00
if ($status['visibility'] == 'private') {
if ($pid != $status['account']['id']) {
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);
}
}
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) {
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') {
return $q->orderBy('id');
}
2024-03-05 08:58:15 +00:00
if ($sortBy === 'newest') {
return $q->orderByDesc('created_at');
}
2024-03-05 08:58:15 +00:00
if ($sortBy === 'popular') {
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);
})
2024-03-05 08:58:15 +00:00
->map(function ($post) use ($pid) {
$status = StatusService::get($post->id, false);
2024-03-05 08:58:15 +00:00
if (! $status || ! isset($status['id'])) {
return false;
}
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();
$res = [
'data' => $data,
2024-03-05 08:58:15 +00:00
'next' => $ids->nextPageUrl(),
];
return $this->json($res);
}
/**
* GET /api/v2/statuses/{id}/state
*
*
* @return array
*/
public function statusState(Request $request, $id)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user(), 403);
$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);
return $this->json(StatusService::getState($status->id, $pid));
}
/**
* GET /api/v1.1/discover/accounts/popular
*
*
* @return array
*/
public function discoverAccountsPopular(Request $request)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('read'), 403);
$pid = $request->user()->profile_id;
$ids = Cache::remember('api:v1.1:discover:accounts:popular', 14400, function () {
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();
});
$filters = UserFilterService::filters($pid);
$asf = AdminShadowFilterService::getHideFromPublicFeedsList();
2024-03-05 08:58:15 +00:00
$ids = $ids->map(function ($profile) {
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);
})
->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();
return $this->json($ids);
}
/**
* GET /api/v1/preferences
*
*
* @return array
*/
public function getPreferences(Request $request)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('read'), 403);
$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,
]);
}
/**
* GET /api/v1/trends
*
*
* @return array
*/
public function getTrends(Request $request)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('read'), 403);
return $this->json([]);
}
/**
* GET /api/v1/announcements
*
*
* @return array
*/
public function getAnnouncements(Request $request)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('read'), 403);
return $this->json([]);
}
/**
* GET /api/v1/markers
*
*
* @return array
*/
public function getMarkers(Request $request)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('read'), 403);
$type = $request->input('timeline');
2024-03-05 08:58:15 +00:00
if (is_array($type)) {
$type = $type[0];
}
2024-03-05 08:58:15 +00:00
if (! $type || ! in_array($type, ['home', 'notifications'])) {
return $this->json([]);
}
$pid = $request->user()->profile_id;
2024-03-05 08:58:15 +00:00
return $this->json(MarkerService::get($pid, $type));
}
/**
* POST /api/v1/markers
*
*
* @return array
*/
public function setMarkers(Request $request)
{
2024-03-05 08:58:15 +00:00
abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('write'), 403);
$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) {
return $this->json(MarkerService::set($pid, 'home', $home));
}
2024-03-05 08:58:15 +00:00
if ($notifications) {
return $this->json(MarkerService::set($pid, 'notifications', $notifications));
}
return $this->json([]);
}
/**
* GET /api/v1/instance/peers
*
*
* @return array
*/
public function instancePeers(Request $request)
{
if ((bool) config('instance.show_peers') == false) {
return $this->json([]);
}
return $this->json(
Cache::remember(InstanceService::CACHE_KEY_API_PEERS_LIST, now()->addHours(24), function () {
return Instance::whereNotNull('nodeinfo_last_fetched')
->whereBanned(false)
->where('nodeinfo_last_fetched', '>', now()->subDays(8))
->pluck('domain');
})
);
}
}