1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2024-12-27 02:06:52 +00:00
pixelfed/app/Http/Controllers/InternalApiController.php

338 lines
11 KiB
PHP
Raw Normal View History

2018-10-21 05:15:12 +00:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\{
2018-11-17 19:57:58 +00:00
DirectMessage,
2019-02-07 19:01:01 +00:00
DiscoverCategory,
2018-11-05 00:17:21 +00:00
Hashtag,
2018-11-20 02:43:31 +00:00
Follower,
2018-10-21 05:15:12 +00:00
Like,
Media,
2018-11-05 00:17:21 +00:00
Notification,
2018-10-21 05:15:12 +00:00
Profile,
2018-11-05 00:17:21 +00:00
StatusHashtag,
2018-10-21 05:15:12 +00:00
Status,
2018-11-20 02:43:31 +00:00
UserFilter,
2018-10-21 05:15:12 +00:00
};
use Auth,Cache;
2018-11-05 00:17:21 +00:00
use Carbon\Carbon;
2018-10-21 05:15:12 +00:00
use League\Fractal;
use App\Transformer\Api\{
AccountTransformer,
StatusTransformer,
};
2019-03-08 08:52:54 +00:00
use App\Util\Media\Filter;
2018-10-21 05:15:12 +00:00
use App\Jobs\StatusPipeline\NewStatusPipeline;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
2019-02-10 23:52:00 +00:00
use Illuminate\Validation\Rule;
2019-04-05 05:33:53 +00:00
use Illuminate\Support\Str;
2018-10-21 05:15:12 +00:00
class InternalApiController extends Controller
{
protected $fractal;
public function __construct()
{
$this->middleware('auth');
$this->fractal = new Fractal\Manager();
$this->fractal->setSerializer(new ArraySerializer());
}
2019-03-25 19:27:02 +00:00
// deprecated v2 compose api
2018-10-21 05:15:12 +00:00
public function compose(Request $request)
{
2019-03-26 02:49:27 +00:00
return redirect('/');
2018-10-21 05:15:12 +00:00
}
2018-11-05 00:17:21 +00:00
2018-11-27 09:17:27 +00:00
// deprecated
2018-11-05 00:17:21 +00:00
public function discover(Request $request)
{
2019-07-11 05:10:00 +00:00
return;
2018-11-05 00:17:21 +00:00
}
2018-11-27 09:17:27 +00:00
public function discoverPosts(Request $request)
{
$profile = Auth::user()->profile;
$pid = $profile->id;
2019-02-25 18:56:24 +00:00
$following = Cache::remember('feature:discover:following:'.$pid, now()->addMinutes(15), function() use ($pid) {
2018-11-27 09:17:27 +00:00
return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
});
2019-02-25 18:56:24 +00:00
$filters = Cache::remember("user:filter:list:$pid", now()->addMinutes(15), function() use($pid) {
2019-02-10 23:52:00 +00:00
$private = Profile::whereIsPrivate(true)
->orWhere('unlisted', true)
->orWhere('status', '!=', null)
->pluck('id')
->toArray();
$filters = UserFilter::whereUserId($pid)
->whereFilterableType('App\Profile')
->whereIn('filter_type', ['mute', 'block'])
->pluck('filterable_id')
->toArray();
return array_merge($private, $filters);
2018-11-27 09:17:27 +00:00
});
$following = array_merge($following, $filters);
2019-05-28 05:06:20 +00:00
$posts = Status::select(
'id',
'caption',
'profile_id',
'type'
)
2019-02-12 00:37:29 +00:00
->whereNull('uri')
2019-06-05 03:46:53 +00:00
->whereIn('type', ['photo','photo:album', 'video'])
2018-11-27 09:17:27 +00:00
->whereIsNsfw(false)
->whereVisibility('public')
->whereNotIn('profile_id', $following)
2019-06-05 04:03:28 +00:00
->whereDate('created_at', '>', now()->subMonths(3))
2018-11-27 09:17:27 +00:00
->with('media')
2019-06-05 04:00:05 +00:00
->inRandomOrder()
->take(36)
2018-11-27 09:17:27 +00:00
->get();
$res = [
'posts' => $posts->map(function($post) {
return [
2019-05-28 05:06:20 +00:00
'type' => $post->type,
2018-11-27 09:17:27 +00:00
'url' => $post->url(),
'thumb' => $post->thumb(),
];
})
];
return response()->json($res);
}
2018-11-17 19:57:58 +00:00
public function directMessage(Request $request, $profileId, $threadId)
{
$profile = Auth::user()->profile;
if($profileId != $profile->id) {
abort(403);
}
$msg = DirectMessage::whereToId($profile->id)
->orWhere('from_id',$profile->id)
->findOrFail($threadId);
$thread = DirectMessage::with('status')->whereIn('to_id', [$profile->id, $msg->from_id])
->whereIn('from_id', [$profile->id,$msg->from_id])
->orderBy('created_at', 'asc')
->paginate(30);
return response()->json(compact('msg', 'profile', 'thread'), 200, [], JSON_PRETTY_PRINT);
}
2018-12-09 23:02:32 +00:00
public function statusReplies(Request $request, int $id)
{
2019-07-11 05:10:00 +00:00
$parent = Status::whereScope('public')->findOrFail($id);
2018-12-09 23:02:32 +00:00
$children = Status::whereInReplyToId($parent->id)
->orderBy('created_at', 'desc')
->take(3)
->get();
$resource = new Fractal\Resource\Collection($children, new StatusTransformer());
$res = $this->fractal->createData($resource)->toArray();
return response()->json($res);
}
2019-01-31 20:27:20 +00:00
public function stories(Request $request)
{
}
2019-02-07 19:01:01 +00:00
public function discoverCategories(Request $request)
{
$categories = DiscoverCategory::whereActive(true)->orderBy('order')->take(10)->get();
$res = $categories->map(function($item) {
return [
'name' => $item->name,
'url' => $item->url(),
'thumb' => $item->thumb()
];
});
return response()->json($res);
}
2019-02-10 23:52:00 +00:00
public function modAction(Request $request)
{
abort_unless(Auth::user()->is_admin, 403);
$this->validate($request, [
'action' => [
'required',
'string',
Rule::in([
'autocw',
'noautolink',
'unlisted',
'disable',
'suspend'
])
],
'item_id' => 'required|integer|min:1',
'item_type' => [
'required',
'string',
Rule::in(['status'])
]
]);
$action = $request->input('action');
$item_id = $request->input('item_id');
$item_type = $request->input('item_type');
switch($action) {
case 'autocw':
$profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
$profile->cw = true;
$profile->save();
break;
case 'noautolink':
$profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
$profile->no_autolink = true;
$profile->save();
break;
case 'unlisted':
$profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
$profile->unlisted = true;
$profile->save();
break;
case 'disable':
$profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
$user = $profile->user;
$profile->status = 'disabled';
$user->status = 'disabled';
$profile->save();
$user->save();
break;
case 'suspend':
$profile = $item_type == 'status' ? Status::findOrFail($item_id)->profile : null;
$user = $profile->user;
$profile->status = 'suspended';
$user->status = 'suspended';
$profile->save();
$user->save();
break;
default:
# code...
break;
}
2019-05-09 02:45:53 +00:00
Cache::forget('profiles:private');
2019-02-10 23:52:00 +00:00
return ['msg' => 200];
}
2019-03-25 19:27:02 +00:00
public function composePost(Request $request)
{
$this->validate($request, [
2019-06-19 07:22:59 +00:00
'caption' => 'nullable|string|max:'.config('pixelfed.max_caption_length', 500),
2019-03-25 19:27:02 +00:00
'media.*' => 'required',
'media.*.id' => 'required|integer|min:1',
'media.*.filter_class' => 'nullable|alpha_dash|max:30',
'media.*.license' => 'nullable|string|max:140',
'media.*.alt' => 'nullable|string|max:140',
2019-03-25 19:27:02 +00:00
'cw' => 'nullable|boolean',
'visibility' => 'required|string|in:public,private,unlisted|min:2|max:10',
'place' => 'nullable',
'comments_disabled' => 'nullable|boolean'
2019-03-25 19:27:02 +00:00
]);
2019-04-05 05:33:53 +00:00
if(config('costar.enabled') == true) {
$blockedKeywords = config('costar.keyword.block');
if($blockedKeywords !== null && $request->caption) {
$keywords = config('costar.keyword.block');
foreach($keywords as $kw) {
if(Str::contains($request->caption, $kw) == true) {
abort(400, 'Invalid object');
}
}
}
}
2019-09-06 05:11:55 +00:00
$user = Auth::user();
$profile = $user->profile;
2019-03-25 19:27:02 +00:00
$visibility = $request->input('visibility');
$medias = $request->input('media');
$attachments = [];
$status = new Status;
$mimes = [];
$cw = $request->input('cw');
foreach($medias as $k => $media) {
2019-03-26 02:39:34 +00:00
if($k + 1 > config('pixelfed.max_album_length')) {
continue;
}
2019-03-25 19:27:02 +00:00
$m = Media::findOrFail($media['id']);
if($m->profile_id !== $profile->id || $m->status_id) {
abort(403, 'Invalid media id');
}
$m->filter_class = in_array($media['filter_class'], Filter::classes()) ? $media['filter_class'] : null;
$m->license = $media['license'];
$m->caption = isset($media['alt']) ? strip_tags($media['alt']) : null;
$m->order = isset($media['cursor']) && is_int($media['cursor']) ? (int) $media['cursor'] : $k;
if($cw == true || $profile->cw == true) {
$m->is_nsfw = $cw;
$status->is_nsfw = $cw;
}
$m->save();
$attachments[] = $m;
array_push($mimes, $m->mime);
}
if($request->filled('place')) {
$status->place_id = $request->input('place')['id'];
}
if($request->filled('comments_disabled')) {
$status->comments_disabled = $request->input('comments_disabled');
}
2019-03-25 19:27:02 +00:00
$status->caption = strip_tags($request->caption);
$status->scope = 'draft';
$status->profile_id = $profile->id;
2019-03-25 19:27:02 +00:00
$status->save();
foreach($attachments as $media) {
$media->status_id = $status->id;
$media->save();
}
$visibility = $profile->unlisted == true && $visibility == 'public' ? 'unlisted' : $visibility;
$cw = $profile->cw == true ? true : $cw;
$status->is_nsfw = $cw;
$status->visibility = $visibility;
$status->scope = $visibility;
$status->type = StatusController::mimeTypeCheck($mimes);
$status->save();
NewStatusPipeline::dispatch($status);
Cache::forget('user:account:id:'.$profile->user_id);
Cache::forget('profile:status_count:'.$profile->id);
2019-09-06 05:11:55 +00:00
Cache::forget($user->storageUsedKey());
2019-03-25 19:27:02 +00:00
return $status->url();
}
public function bookmarks(Request $request)
{
$statuses = Auth::user()->profile
->bookmarks()
->withCount(['likes','comments'])
->orderBy('created_at', 'desc')
->simplePaginate(10);
$resource = new Fractal\Resource\Collection($statuses, new StatusTransformer());
$res = $this->fractal->createData($resource)->toArray();
return response()->json($res);
}
2018-10-21 05:15:12 +00:00
}