mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-22 14:31:26 +00:00
Update public timeline api, use cached sorted set and client side block/mute filtering
This commit is contained in:
parent
be194b8a3f
commit
37abcf3898
6 changed files with 115 additions and 82 deletions
|
@ -26,6 +26,8 @@ use League\Fractal;
|
||||||
use League\Fractal\Serializer\ArraySerializer;
|
use League\Fractal\Serializer\ArraySerializer;
|
||||||
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
||||||
use App\Transformer\Api\Mastodon\v1\AccountTransformer;
|
use App\Transformer\Api\Mastodon\v1\AccountTransformer;
|
||||||
|
use App\Services\AccountService;
|
||||||
|
use App\Services\UserFilterService;
|
||||||
|
|
||||||
class AccountController extends Controller
|
class AccountController extends Controller
|
||||||
{
|
{
|
||||||
|
@ -34,6 +36,8 @@ class AccountController extends Controller
|
||||||
'user.block',
|
'user.block',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const FILTER_LIMIT = 'You cannot block or mute more than 100 accounts';
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->middleware('auth');
|
$this->middleware('auth');
|
||||||
|
@ -140,6 +144,12 @@ class AccountController extends Controller
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$user = Auth::user()->profile;
|
$user = Auth::user()->profile;
|
||||||
|
$count = UserFilterService::muteCount($user->id);
|
||||||
|
abort_if($count >= 100, 422, self::FILTER_LIMIT);
|
||||||
|
if($count == 0) {
|
||||||
|
$filterCount = UserFilter::whereUserId($user->id)->count();
|
||||||
|
abort_if($filterCount >= 100, 422, self::FILTER_LIMIT);
|
||||||
|
}
|
||||||
$type = $request->input('type');
|
$type = $request->input('type');
|
||||||
$item = $request->input('item');
|
$item = $request->input('item');
|
||||||
$action = $type . '.mute';
|
$action = $type . '.mute';
|
||||||
|
@ -237,6 +247,12 @@ class AccountController extends Controller
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$user = Auth::user()->profile;
|
$user = Auth::user()->profile;
|
||||||
|
$count = UserFilterService::blockCount($user->id);
|
||||||
|
abort_if($count >= 100, 422, self::FILTER_LIMIT);
|
||||||
|
if($count == 0) {
|
||||||
|
$filterCount = UserFilter::whereUserId($user->id)->count();
|
||||||
|
abort_if($filterCount >= 100, 422, self::FILTER_LIMIT);
|
||||||
|
}
|
||||||
$type = $request->input('type');
|
$type = $request->input('type');
|
||||||
$item = $request->input('item');
|
$item = $request->input('item');
|
||||||
$action = $type.'.block';
|
$action = $type.'.block';
|
||||||
|
@ -552,5 +568,21 @@ class AccountController extends Controller
|
||||||
$prev = $page > 1 ? $page - 1 : 1;
|
$prev = $page > 1 ? $page - 1 : 1;
|
||||||
$links = '<'.$url.'?page='.$next.'&limit='.$limit.'>; rel="next", <'.$url.'?page='.$prev.'&limit='.$limit.'>; rel="prev"';
|
$links = '<'.$url.'?page='.$next.'&limit='.$limit.'>; rel="next", <'.$url.'?page='.$prev.'&limit='.$limit.'>; rel="prev"';
|
||||||
return response()->json($res, 200, ['Link' => $links]);
|
return response()->json($res, 200, ['Link' => $links]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function accountBlocksV2(Request $request)
|
||||||
|
{
|
||||||
|
return response()->json(UserFilterService::blocks($request->user()->profile_id), 200, [], JSON_UNESCAPED_SLASHES);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function accountMutesV2(Request $request)
|
||||||
|
{
|
||||||
|
return response()->json(UserFilterService::mutes($request->user()->profile_id), 200, [], JSON_UNESCAPED_SLASHES);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function accountFiltersV2(Request $request)
|
||||||
|
{
|
||||||
|
return response()->json(UserFilterService::filters($request->user()->profile_id), 200, [], JSON_UNESCAPED_SLASHES);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -563,9 +563,12 @@ class ApiV1Controller extends Controller
|
||||||
'id.*' => 'required|integer|min:1|max:' . PHP_INT_MAX
|
'id.*' => 'required|integer|min:1|max:' . PHP_INT_MAX
|
||||||
]);
|
]);
|
||||||
$pid = $request->user()->profile_id ?? $request->user()->profile->id;
|
$pid = $request->user()->profile_id ?? $request->user()->profile->id;
|
||||||
$ids = collect($request->input('id'));
|
$res = collect($request->input('id'))
|
||||||
$res = $ids->map(function($id) use($pid) {
|
->filter(function($id) use($pid) {
|
||||||
return RelationshipService::get($pid, $id);
|
return $id != $pid;
|
||||||
|
})
|
||||||
|
->map(function($id) use($pid) {
|
||||||
|
return RelationshipService::get($pid, $id);
|
||||||
});
|
});
|
||||||
return response()->json($res);
|
return response()->json($res);
|
||||||
}
|
}
|
||||||
|
@ -1485,13 +1488,13 @@ class ApiV1Controller extends Controller
|
||||||
$limit = $request->input('limit') ?? 3;
|
$limit = $request->input('limit') ?? 3;
|
||||||
$user = $request->user();
|
$user = $request->user();
|
||||||
|
|
||||||
Cache::remember('api:v1:timelines:public:cache_check', 3600, function() {
|
Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() {
|
||||||
if(PublicTimelineService::count() == 0) {
|
if(PublicTimelineService::count() == 0) {
|
||||||
PublicTimelineService::warmCache(true, 400);
|
PublicTimelineService::warmCache(true, 400);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
if ($max) {
|
if ($max) {
|
||||||
$feed = PublicTimelineService::getRankedMaxId($max, $limit);
|
$feed = PublicTimelineService::getRankedMaxId($max, $limit);
|
||||||
} else if ($min) {
|
} else if ($min) {
|
||||||
$feed = PublicTimelineService::getRankedMinId($min, $limit);
|
$feed = PublicTimelineService::getRankedMinId($min, $limit);
|
||||||
|
@ -1500,14 +1503,15 @@ class ApiV1Controller extends Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
$res = collect($feed)
|
$res = collect($feed)
|
||||||
->map(function($k) use($user) {
|
->map(function($k) use($user) {
|
||||||
$status = StatusService::get($k);
|
$status = StatusService::get($k);
|
||||||
if($user) {
|
if($user) {
|
||||||
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
|
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
|
||||||
}
|
$status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
|
||||||
return $status;
|
}
|
||||||
})
|
return $status;
|
||||||
->toArray();
|
})
|
||||||
|
->toArray();
|
||||||
return response()->json($res);
|
return response()->json($res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@ use App\Services\{
|
||||||
LikeService,
|
LikeService,
|
||||||
PublicTimelineService,
|
PublicTimelineService,
|
||||||
ProfileService,
|
ProfileService,
|
||||||
|
RelationshipService,
|
||||||
StatusService,
|
StatusService,
|
||||||
SnowflakeService,
|
SnowflakeService,
|
||||||
UserFilterService
|
UserFilterService
|
||||||
|
@ -288,69 +289,30 @@ class PublicApiController extends Controller
|
||||||
$limit = $request->input('limit') ?? 3;
|
$limit = $request->input('limit') ?? 3;
|
||||||
$user = $request->user();
|
$user = $request->user();
|
||||||
|
|
||||||
$filtered = $user ? UserFilterService::filters($user->profile_id) : [];
|
Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() {
|
||||||
|
if(PublicTimelineService::count() == 0) {
|
||||||
|
PublicTimelineService::warmCache(true, 400);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if($min || $max) {
|
if ($max) {
|
||||||
$dir = $min ? '>' : '<';
|
$feed = PublicTimelineService::getRankedMaxId($max, $limit);
|
||||||
$id = $min ?? $max;
|
} else if ($min) {
|
||||||
$timeline = Status::select(
|
$feed = PublicTimelineService::getRankedMinId($min, $limit);
|
||||||
'id',
|
} else {
|
||||||
'profile_id',
|
$feed = PublicTimelineService::get(0, $limit);
|
||||||
'type',
|
}
|
||||||
'scope',
|
|
||||||
'local'
|
|
||||||
)
|
|
||||||
->where('id', $dir, $id)
|
|
||||||
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
|
|
||||||
->whereNotIn('profile_id', $filtered)
|
|
||||||
->whereLocal(true)
|
|
||||||
->whereScope('public')
|
|
||||||
->orderBy('id', 'desc')
|
|
||||||
->limit($limit)
|
|
||||||
->get()
|
|
||||||
->map(function($s) use ($user) {
|
|
||||||
$status = StatusService::getFull($s->id, $user->profile_id);
|
|
||||||
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
|
|
||||||
return $status;
|
|
||||||
});
|
|
||||||
$res = $timeline->toArray();
|
|
||||||
} else {
|
|
||||||
$timeline = Status::select(
|
|
||||||
'id',
|
|
||||||
'uri',
|
|
||||||
'caption',
|
|
||||||
'rendered',
|
|
||||||
'profile_id',
|
|
||||||
'type',
|
|
||||||
'in_reply_to_id',
|
|
||||||
'reblog_of_id',
|
|
||||||
'is_nsfw',
|
|
||||||
'scope',
|
|
||||||
'local',
|
|
||||||
'reply_count',
|
|
||||||
'comments_disabled',
|
|
||||||
'created_at',
|
|
||||||
'place_id',
|
|
||||||
'likes_count',
|
|
||||||
'reblogs_count',
|
|
||||||
'updated_at'
|
|
||||||
)
|
|
||||||
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
|
|
||||||
->whereNotIn('profile_id', $filtered)
|
|
||||||
->with('profile', 'hashtags', 'mentions')
|
|
||||||
->whereLocal(true)
|
|
||||||
->whereScope('public')
|
|
||||||
->orderBy('id', 'desc')
|
|
||||||
->limit($limit)
|
|
||||||
->get()
|
|
||||||
->map(function($s) use ($user) {
|
|
||||||
$status = StatusService::getFull($s->id, $user->profile_id);
|
|
||||||
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
|
|
||||||
return $status;
|
|
||||||
});
|
|
||||||
|
|
||||||
$res = $timeline->toArray();
|
$res = collect($feed)
|
||||||
}
|
->map(function($k) use($user) {
|
||||||
|
$status = StatusService::get($k);
|
||||||
|
if($user) {
|
||||||
|
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
|
||||||
|
$status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
|
||||||
|
}
|
||||||
|
return $status;
|
||||||
|
})
|
||||||
|
->toArray();
|
||||||
|
|
||||||
return response()->json($res);
|
return response()->json($res);
|
||||||
}
|
}
|
||||||
|
@ -580,17 +542,20 @@ class PublicApiController extends Controller
|
||||||
return response()->json([]);
|
return response()->json([]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$pid = $request->user()->profile_id;
|
||||||
|
|
||||||
$this->validate($request, [
|
$this->validate($request, [
|
||||||
'id' => 'required|array|min:1|max:20',
|
'id' => 'required|array|min:1|max:20',
|
||||||
'id.*' => 'required|integer'
|
'id.*' => 'required|integer'
|
||||||
]);
|
]);
|
||||||
$ids = collect($request->input('id'));
|
$ids = collect($request->input('id'));
|
||||||
$filtered = $ids->filter(function($v) {
|
$res = $ids->filter(function($v) use($pid) {
|
||||||
return $v != Auth::user()->profile->id;
|
return $v != $pid;
|
||||||
|
})
|
||||||
|
->map(function($id) use($pid) {
|
||||||
|
return RelationshipService::get($pid, $id);
|
||||||
});
|
});
|
||||||
$relations = Profile::whereNull('status')->findOrFail($filtered->all());
|
|
||||||
$fractal = new Fractal\Resource\Collection($relations, new RelationshipTransformer());
|
|
||||||
$res = $this->fractal->createData($fractal)->toArray();
|
|
||||||
return response()->json($res);
|
return response()->json($res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -741,5 +706,4 @@ class PublicApiController extends Controller
|
||||||
|
|
||||||
return response()->json($res);
|
return response()->json($res);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,8 @@ use App\Status;
|
||||||
use App\Transformer\Api\AccountTransformer;
|
use App\Transformer\Api\AccountTransformer;
|
||||||
use League\Fractal;
|
use League\Fractal;
|
||||||
use League\Fractal\Serializer\ArraySerializer;
|
use League\Fractal\Serializer\ArraySerializer;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
class AccountService
|
class AccountService
|
||||||
{
|
{
|
||||||
|
@ -62,4 +64,22 @@ class AccountService
|
||||||
Cache::put($key, 1, 900);
|
Cache::put($key, 1, 900);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function usernameToId($username)
|
||||||
|
{
|
||||||
|
$key = self::CACHE_KEY . 'u2id:' . hash('sha256', $username);
|
||||||
|
return Cache::remember($key, 900, function() use($username) {
|
||||||
|
$s = Str::of($username);
|
||||||
|
if($s->contains('@') && !$s->startsWith('@')) {
|
||||||
|
$username = "@{$username}";
|
||||||
|
}
|
||||||
|
$profile = DB::table('profiles')
|
||||||
|
->whereUsername($username)
|
||||||
|
->first();
|
||||||
|
if(!$profile) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return (string) $profile->id;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,4 +98,14 @@ class UserFilterService {
|
||||||
}
|
}
|
||||||
return $exists;
|
return $exists;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function blockCount(int $profile_id)
|
||||||
|
{
|
||||||
|
return Redis::zcard(self::USER_BLOCKS_KEY . $profile_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function muteCount(int $profile_id)
|
||||||
|
{
|
||||||
|
return Redis::zcard(self::USER_MUTES_KEY . $profile_id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -202,6 +202,9 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact
|
||||||
Route::post('status/{id}/archive', 'ApiController@archive');
|
Route::post('status/{id}/archive', 'ApiController@archive');
|
||||||
Route::post('status/{id}/unarchive', 'ApiController@unarchive');
|
Route::post('status/{id}/unarchive', 'ApiController@unarchive');
|
||||||
Route::get('statuses/archives', 'ApiController@archivedPosts');
|
Route::get('statuses/archives', 'ApiController@archivedPosts');
|
||||||
|
Route::get('mutes', 'AccountController@accountMutesV2');
|
||||||
|
Route::get('blocks', 'AccountController@accountBlocksV2');
|
||||||
|
Route::get('filters', 'AccountController@accountFiltersV2');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue