Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ add experimental endpoint to search for status bodies #3065

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions app/Http/Controllers/API/v1/StatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Models\Stopover;
use App\Models\Trip;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
Expand All @@ -29,7 +30,6 @@
use Illuminate\Validation\Rules\Enum;
use Illuminate\Validation\ValidationException;
use InvalidArgumentException;
use OpenApi\Annotations as OA;

class StatusController extends Controller
{
Expand Down Expand Up @@ -251,6 +251,60 @@ public function getLivePositionForStatus($ids): AnonymousResourceCollection {
return JsonResource::collection(StatusBackend::getLivePositionForStatus($ids));
}

/**
* Experimental - do not add to docs now.
*
* Used in Lokbuch for testing.
*/
public function list(Request $request): AnonymousResourceCollection {
$validated = $request->validate([
'body' => ['nullable', 'string', 'max:32'],
]);

$user = auth()->user();
$query = Status::query()->orderByDesc('created_at');

if (isset($validated['body'])) {
$query->where('body', 'like', '%' . $validated['body'] . '%');
}

//TODO: check if user is allowed to see this status

$query->join('train_checkins', 'train_checkins.status_id', '=', 'statuses.id')
->join('users', 'statuses.user_id', '=', 'users.id')
->where(function(Builder $query) use ($user) {
//Visibility checks: One of the following options must be true

//Option 1: User is public AND status is public
$query->where(function(Builder $query) {
$query->where('users.private_profile', 0)
->whereIn('visibility', [
StatusVisibility::PUBLIC->value,
StatusVisibility::AUTHENTICATED->value
]);
});

//Option 2: Status is from oneself
$query->orWhere('users.id', $user->id);

//Option 3: Status is from a followed BUT not unlisted or private
$query->orWhere(function(Builder $query) use ($user) {
$query->whereIn('users.id', $user->follows()->select('follow_id'))
->whereNotIn('statuses.visibility', [
StatusVisibility::UNLISTED->value,
StatusVisibility::PRIVATE->value,
]);
});
})
->where('train_checkins.departure', '<', \Carbon\Carbon::now()->addMinutes(20))
->whereNotIn('statuses.user_id', $user->mutedUsers()->select('muted_id'))
->whereNotIn('statuses.user_id', $user->blockedUsers()->select('blocked_id'))
->whereNotIn('statuses.user_id', $user->blockedByUsers()->select('user_id'))
->select('statuses.*');

return StatusResource::collection($query->cursorPaginate(20));
}

/**
* @OA\Get(
* path="/status/{id}",
Expand Down Expand Up @@ -419,7 +473,7 @@ public function update(Request $request, int $statusId): JsonResponse {
$this->authorize('update', $status);

//Check for disallowed status visibility changes
if(auth()->user()->can('disallow-status-visibility-change') && $validated['visibility'] !== StatusVisibility::PRIVATE->value) {
if (auth()->user()->can('disallow-status-visibility-change') && $validated['visibility'] !== StatusVisibility::PRIVATE->value) {
return $this->sendError('You are not allowed to change the visibility to anything else than private', 403);
}

Expand Down
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
Route::get('statuses', [StatusController::class, 'enRoute']);
Route::get('positions', [StatusController::class, 'livePositions']);
Route::get('positions/{ids}', [StatusController::class, 'getLivePositionForStatus']);
Route::get('status', [StatusController::class, 'list']);
Route::get('status/{id}', [StatusController::class, 'show']);
Route::get('status/{id}/likes', [LikesController::class, 'show']);
Route::get('status/{statusId}/tags', [StatusTagController::class, 'index']);
Expand Down
Loading