pixelfed/app/Http/Controllers/InternalApiController.php

290 lines
9.4 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,
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,
};
use App\Jobs\StatusPipeline\NewStatusPipeline;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
class InternalApiController extends Controller
{
protected $fractal;
public function __construct()
{
$this->middleware('auth');
$this->fractal = new Fractal\Manager();
$this->fractal->setSerializer(new ArraySerializer());
}
public function compose(Request $request)
{
$this->validate($request, [
'caption' => 'nullable|string',
'media.*' => 'required',
'media.*.id' => 'required|integer|min:1',
'media.*.filter' => 'nullable|string|max:30',
'media.*.license' => 'nullable|string|max:80',
'visibility' => 'required|string|in:public,private|min:2|max:10'
]);
$profile = Auth::user()->profile;
$visibility = $request->input('visibility');
$medias = $request->input('media');
$attachments = [];
$status = new Status;
2018-11-05 00:17:21 +00:00
foreach($medias as $k => $media) {
2018-10-21 05:15:12 +00:00
$m = Media::findOrFail($media['id']);
if($m->profile_id !== $profile->id || $m->status_id) {
abort(403, 'Invalid media id');
}
$m->filter_class = $media['filter'];
$m->license = $media['license'];
2018-11-05 00:17:21 +00:00
$m->caption = strip_tags($media['alt']);
$m->order = isset($media['cursor']) && is_int($media['cursor']) ? (int) $media['cursor'] : $k;
2018-10-21 05:15:12 +00:00
if($media['cw'] == true) {
$m->is_nsfw = true;
$status->is_nsfw = true;
}
$m->save();
$attachments[] = $m;
}
$status->caption = strip_tags($request->caption);
$status->visibility = 'draft';
$status->scope = 'draft';
$status->profile_id = $profile->id;
$status->save();
foreach($attachments as $media) {
$media->status_id = $status->id;
$media->save();
}
$status->visibility = $visibility;
$status->scope = $visibility;
$status->save();
NewStatusPipeline::dispatch($status);
return $status->url();
}
2018-11-05 00:17:21 +00:00
public function notifications(Request $request)
{
$this->validate($request, [
'page' => 'nullable|min:1|max:3',
]);
$profile = Auth::user()->profile;
$timeago = Carbon::now()->subMonths(6);
$notifications = Notification::with('actor')
->whereProfileId($profile->id)
->whereDate('created_at', '>', $timeago)
->orderBy('id', 'desc')
->simplePaginate(30);
$notifications = $notifications->map(function($k, $v) {
return [
'id' => $k->id,
'action' => $k->action,
'message' => $k->message,
'rendered' => $k->rendered,
'actor' => [
'avatar' => $k->actor->avatarUrl(),
'username' => $k->actor->username,
'url' => $k->actor->url(),
],
2018-11-17 19:57:58 +00:00
'url' => $k->item->url(),
'read_at' => $k->read_at,
2018-11-05 00:17:21 +00:00
];
});
return response()->json($notifications, 200, [], JSON_PRETTY_PRINT);
}
2018-11-27 09:17:27 +00:00
// deprecated
2018-11-05 00:17:21 +00:00
public function discover(Request $request)
{
$profile = Auth::user()->profile;
2018-11-20 02:43:31 +00:00
$pid = $profile->id;
2018-11-25 06:44:02 +00:00
$following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
2018-11-25 06:47:56 +00:00
return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
2018-11-25 06:44:02 +00:00
});
$filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
return UserFilter::whereUserId($pid)
->whereFilterableType('App\Profile')
->whereIn('filter_type', ['mute', 'block'])
->pluck('filterable_id')->toArray();
});
$following = array_merge($following, $filters);
2018-11-20 02:43:31 +00:00
2018-11-27 08:55:10 +00:00
$people = Profile::select('id', 'name', 'username')
2018-11-05 00:17:21 +00:00
->with('avatar')
2018-11-27 08:55:10 +00:00
->orderByRaw('rand()')
2018-11-05 00:17:21 +00:00
->whereHas('statuses')
->whereNull('domain')
->whereNotIn('id', $following)
->whereIsPrivate(false)
->take(3)
->get();
2018-11-27 08:55:10 +00:00
$posts = Status::select('id', 'caption', 'profile_id')
2018-11-25 06:44:02 +00:00
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->whereIsNsfw(false)
->whereVisibility('public')
->whereNotIn('profile_id', $following)
2018-11-27 08:55:10 +00:00
->with('media')
2018-11-25 06:44:02 +00:00
->orderBy('created_at', 'desc')
->take(21)
->get();
2018-11-05 00:17:21 +00:00
$res = [
'people' => $people->map(function($profile) {
return [
2018-11-27 08:55:10 +00:00
'id' => $profile->id,
2018-11-05 00:17:21 +00:00
'avatar' => $profile->avatarUrl(),
'name' => $profile->name,
'username' => $profile->username,
'url' => $profile->url(),
];
}),
'posts' => $posts->map(function($post) {
return [
'url' => $post->url(),
'thumb' => $post->thumb(),
];
})
];
return response()->json($res, 200, [], JSON_PRETTY_PRINT);
}
2018-11-27 09:17:27 +00:00
public function discoverPeople(Request $request)
{
$profile = Auth::user()->profile;
$pid = $profile->id;
$following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
});
$filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
return UserFilter::whereUserId($pid)
->whereFilterableType('App\Profile')
->whereIn('filter_type', ['mute', 'block'])
->pluck('filterable_id')->toArray();
});
$following = array_merge($following, $filters);
$people = Profile::select('id', 'name', 'username')
->with('avatar')
->orderByRaw('rand()')
->whereHas('statuses')
->whereNull('domain')
->whereNotIn('id', $following)
->whereIsPrivate(false)
->take(3)
->get();
$res = [
'people' => $people->map(function($profile) {
return [
'id' => $profile->id,
'avatar' => $profile->avatarUrl(),
'name' => $profile->name,
'username' => $profile->username,
'url' => $profile->url(),
];
})
];
return response()->json($res, 200, [], JSON_PRETTY_PRINT);
}
public function discoverPosts(Request $request)
{
$profile = Auth::user()->profile;
$pid = $profile->id;
$following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) {
return Follower::whereProfileId($pid)->pluck('following_id')->toArray();
});
$filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) {
return UserFilter::whereUserId($pid)
->whereFilterableType('App\Profile')
->whereIn('filter_type', ['mute', 'block'])
->pluck('filterable_id')->toArray();
});
$following = array_merge($following, $filters);
$posts = Status::select('id', 'caption', 'profile_id')
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->whereIsNsfw(false)
->whereVisibility('public')
->whereNotIn('profile_id', $following)
->with('media')
->orderBy('created_at', 'desc')
->take(21)
->get();
$res = [
'posts' => $posts->map(function($post) {
return [
'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);
}
public function notificationMarkAllRead(Request $request)
{
$profile = Auth::user()->profile;
$notifications = Notification::whereProfileId($profile->id)->get();
foreach($notifications as $n) {
$n->read_at = Carbon::now();
$n->save();
}
return;
}
2018-10-21 05:15:12 +00:00
}