pixelfed/app/Http/Controllers/ApiController.php

82 lines
2.3 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;
2019-08-06 03:09:10 +00:00
use App\Util\Site\Config;
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)
{
2019-08-06 03:09:10 +00:00
return response()->json(Config::get());
2018-08-10 05:04:21 +00:00
}
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
}