pixelfed/app/Http/Controllers/ApiController.php

107 lines
3.4 KiB
PHP
Raw Normal View History

2018-06-04 01:39:38 +00:00
<?php
namespace App\Http\Controllers;
2018-08-10 05:04:21 +00:00
use App\Http\Controllers\Api\BaseApiController;
2019-04-28 23:48:53 +00:00
use App\{
2019-04-29 04:22:03 +00:00
Follower,
2019-04-28 23:48:53 +00:00
Like,
2019-04-29 04:22:03 +00:00
Profile,
UserFilter
2019-04-28 23:48:53 +00:00
};
2019-05-14 02:26:52 +00:00
use Auth, Cache, Redis;
2018-08-28 03:07:36 +00:00
use Illuminate\Http\Request;
2019-04-28 23:48:53 +00:00
use App\Services\SuggestionService;
2018-06-04 01:39:38 +00:00
2018-08-10 05:04:21 +00:00
class ApiController extends BaseApiController
2018-06-04 01:39:38 +00:00
{
2019-02-25 18:56:24 +00:00
// todo: deprecate and remove
2018-06-04 01:39:38 +00:00
public function hydrateLikes(Request $request)
{
2019-03-25 03:34:25 +00:00
return response()->json([]);
}
2018-06-04 01:39:38 +00:00
2019-03-25 03:34:25 +00:00
public function siteConfiguration(Request $request)
{
$res = Cache::remember('api:site:configuration', now()->addMinutes(30), function() {
return [
'uploader' => [
'max_photo_size' => config('pixelfed.max_photo_size'),
'max_caption_length' => config('pixelfed.max_caption_length'),
'album_limit' => config('pixelfed.max_album_length'),
'image_quality' => config('pixelfed.image_quality'),
'optimize_image' => config('pixelfed.optimize_image'),
'optimize_video' => config('pixelfed.optimize_video'),
2019-03-25 03:34:25 +00:00
'media_types' => config('pixelfed.media_types'),
'enforce_account_limit' => config('pixelfed.enforce_account_limit')
2019-04-18 01:40:35 +00:00
],
2019-04-21 02:00:59 +00:00
2019-04-18 01:40:35 +00:00
'activitypub' => [
'enabled' => config('pixelfed.activitypub_enabled'),
'remote_follow' => config('pixelfed.remote_follow_enabled')
2019-04-21 02:00:59 +00:00
],
'ab' => [
2019-04-26 05:24:59 +00:00
'lc' => config('exp.lc'),
'rec' => config('exp.rec'),
2019-04-21 02:00:59 +00:00
],
2019-03-25 03:34:25 +00:00
];
});
2018-08-10 05:04:21 +00:00
return response()->json($res);
}
2019-04-28 23:48:53 +00:00
public function userRecommendations(Request $request)
{
abort_if(!Auth::check(), 403);
abort_if(!config('exp.rec'), 400);
$id = Auth::user()->profile->id;
2019-04-29 04:22:03 +00:00
$following = Cache::remember('profile:following:'.$id, now()->addHours(12), function() use ($id) {
return Follower::whereProfileId($id)->pluck('following_id')->toArray();
});
array_push($following, $id);
2019-04-28 23:48:53 +00:00
$ids = SuggestionService::get();
2019-04-29 04:22:03 +00:00
$filters = UserFilter::whereUserId($id)
->whereFilterableType('App\Profile')
->whereIn('filter_type', ['mute', 'block'])
->pluck('filterable_id')->toArray();
$following = array_merge($following, $filters);
2019-04-28 23:48:53 +00:00
2019-05-14 02:26:52 +00:00
$key = config('cache.prefix').':api:local:exp:rec:'.$id;
$ttl = (int) Redis::ttl($key);
if($request->filled('refresh') == true && (290 > $ttl) == true) {
Cache::forget('api:local:exp:rec:'.$id);
}
2019-04-28 23:48:53 +00:00
$res = Cache::remember('api:local:exp:rec:'.$id, now()->addMinutes(5), function() use($id, $following, $ids) {
return Profile::select(
'id',
'username'
)
->whereNotIn('id', $following)
->whereIn('id', $ids)
->whereIsPrivate(0)
->whereNull('status')
->whereNull('domain')
->inRandomOrder()
2019-04-29 03:02:15 +00:00
->take(3)
2019-04-28 23:48:53 +00:00
->get()
->map(function($item, $key) {
return [
'id' => $item->id,
'avatar' => $item->avatarUrl(),
'username' => $item->username,
'message' => 'Recommended for You'
];
});
});
return response()->json($res->all());
}
2018-06-04 01:39:38 +00:00
}