Skip to content

Commit

Permalink
✨ save client id if status is created via app (#2251)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu authored Jan 29, 2024
1 parent e02a06a commit 0090246
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .idea/trwl.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions app/Http/Controllers/API/v1/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
* title="Träwelling API",
* description="Träwelling user API description. This is an incomplete documentation with still many errors. The
* API is currently not yet stable. Endpoints are still being restructured. Both the URL and the request or body
* can be changed. Breaking changes will be announced on the Discord server: https://discord.gg/72t7564ZbV",
* can be changed. Breaking changes will be announced on GitHub:
* https://github.com/Traewelling/traewelling/blob/develop/API_CHANGELOG.md",
* @OA\Contact(
* email="[email protected]"
* ),
Expand Down Expand Up @@ -90,7 +91,11 @@ public function sendResponse(
int $code = 200,
array $additional = null
): JsonResponse {
$disclaimer = 'APIv1 is not officially released for use and is also not fully documented. You can find the documentation at https://traewelling.de/api/documentation. Use at your own risk. Data fields may change at any time without notice.';
$disclaimer = [
'message' => 'APIv1 is not officially released for use and is also not fully documented. Use at your own risk. Data fields may change at any time without notice.',
'documentation' => 'https://traewelling.de/api/documentation',
'changelog' => 'https://github.com/Traewelling/traewelling/blob/develop/API_CHANGELOG.md',
];
if ($data === null) {
return response()->json(
data: [
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/API/v1/StatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,9 @@ public function getLivePositionForStatus($ids): AnonymousResourceCollection {
*
* @param int $id
*
* @return StatusResource|Response
* @return StatusResource
*/
public function show(int $id): StatusResource|Response {
public function show(int $id): StatusResource {
$status = StatusBackend::getStatus($id);
try {
$this->authorize('view', $status);
Expand Down
35 changes: 18 additions & 17 deletions app/Http/Controllers/StatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,22 @@ public static function getStatus(int $statusId): Status {
*/
public static function getActiveStatuses(): ?Collection {
return Status::with([
'event', 'likes', 'user.blockedByUsers', 'user.blockedUsers', 'user.followers',
'checkin.originStation', 'checkin.destinationStation',
'checkin.Trip.stopovers.station',
'checkin.Trip.polyline',
])
->whereHas('checkin', function($query) {
$query->where('departure', '<', date('Y-m-d H:i:s'))
->where('arrival', '>', date('Y-m-d H:i:s'));
})
->get()
->filter(function(Status $status) {
return Gate::allows('view', $status) && !$status->user->shadow_banned && $status->visibility !== StatusVisibility::UNLISTED;
})
->sortByDesc(function(Status $status) {
return $status->checkin->departure;
})->values();
'event', 'likes', 'user.blockedByUsers', 'user.blockedUsers', 'user.followers',
'checkin.originStation', 'checkin.destinationStation',
'checkin.Trip.stopovers.station',
'checkin.Trip.polyline',
])
->whereHas('checkin', function($query) {
$query->where('departure', '<', date('Y-m-d H:i:s'))
->where('arrival', '>', date('Y-m-d H:i:s'));
})
->get()
->filter(function(Status $status) {
return Gate::allows('view', $status) && !$status->user->shadow_banned && $status->visibility !== StatusVisibility::UNLISTED;
})
->sortByDesc(function(Status $status) {
return $status->checkin->departure;
})->values();
}

public static function getLivePositions(): array {
Expand Down Expand Up @@ -283,7 +283,8 @@ public static function createStatus(
'body' => $body,
'business' => $business,
'visibility' => $visibility,
'event_id' => $event?->id
'event_id' => $event?->id,
'client_id' => request()?->user()?->token()?->client?->id,
]);
}
}
19 changes: 19 additions & 0 deletions app/Http/Resources/ClientResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

/**
* Model -> OAuthClient
*/
class ClientResource extends JsonResource
{
public function toArray($request) {
return [
'id' => $this->id,
'name' => $this->name,
'privacyPolicyUrl' => $this->privacy_policy_url,
];
}
}
29 changes: 15 additions & 14 deletions app/Http/Resources/StatusResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,23 @@ public function toArray($request): array {
'likes' => (int) $this->likes->count(),
'liked' => (bool) $this->favorited,
'isLikable' => Gate::allows('like', $this->resource),
'client' => new ClientResource($this->client),
'createdAt' => $this->created_at->toIso8601String(),
'train' => [
'trip' => (int) $this->checkin->trip->id,
'hafasId' => (string) $this->checkin->trip->trip_id,
'category' => (string) $this->checkin->trip->category->value,
'number' => (string) $this->checkin->trip->number,
'lineName' => (string) $this->checkin->trip->linename,
'journeyNumber' => $this->checkin->trip->journey_number,
'distance' => (int) $this->checkin->distance,
'points' => (int) $this->checkin->points,
'duration' => (int) $this->checkin->duration,
'manualDeparture' => $this->checkin->manual_departure?->toIso8601String(),
'manualArrival' => $this->checkin->manual_arrival?->toIso8601String(),
'origin' => new StopoverResource($this->checkin->originStopover),
'destination' => new StopoverResource($this->checkin->destinationStopover),
'operator' => new OperatorResource($this?->checkin->trip->operator)
'trip' => (int) $this->checkin->trip->id,
'hafasId' => (string) $this->checkin->trip->trip_id,
'category' => (string) $this->checkin->trip->category->value,
'number' => (string) $this->checkin->trip->number,
'lineName' => (string) $this->checkin->trip->linename,
'journeyNumber' => $this->checkin->trip->journey_number,
'distance' => (int) $this->checkin->distance,
'points' => (int) $this->checkin->points,
'duration' => (int) $this->checkin->duration,
'manualDeparture' => $this->checkin->manual_departure?->toIso8601String(),
'manualArrival' => $this->checkin->manual_arrival?->toIso8601String(),
'origin' => new StopoverResource($this->checkin->originStopover),
'destination' => new StopoverResource($this->checkin->destinationStopover),
'operator' => new OperatorResource($this?->checkin->trip->operator)
],
'event' => new EventResource($this?->event),
];
Expand Down
11 changes: 9 additions & 2 deletions app/Models/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
* @property int user_id
* @property string body
* @property Business business
* @property int event_id
* @property StatusVisibility visibility
* @property int event_id
* @property string tweet_id
* @property string mastodon_post_id
* @property Checkin $checkin
*
* @todo merge model with "Checkin" (later only "Checkin") because the difference between trip sources (HAFAS,
Expand All @@ -28,7 +30,7 @@ class Status extends Model

use HasFactory;

protected $fillable = ['user_id', 'body', 'business', 'visibility', 'event_id', 'tweet_id', 'mastodon_post_id'];
protected $fillable = ['user_id', 'body', 'business', 'visibility', 'event_id', 'tweet_id', 'mastodon_post_id', 'client_id'];
protected $hidden = ['user_id', 'business'];
protected $appends = ['favorited', 'socialText', 'statusInvisibleToMe', 'description'];
protected $casts = [
Expand All @@ -39,6 +41,7 @@ class Status extends Model
'event_id' => 'integer',
'tweet_id' => 'string',
'mastodon_post_id' => 'string',
'client_id' => 'integer'
];

public function user(): BelongsTo {
Expand All @@ -53,6 +56,10 @@ public function checkin(): HasOne {
return $this->hasOne(Checkin::class);
}

public function client(): BelongsTo {
return $this->belongsTo(OAuthClient::class, 'client_id', 'id');
}

/**
* @return HasOne
* @deprecated use ->checkin instead
Expand Down
49 changes: 49 additions & 0 deletions app/Virtual/Models/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Virtual\Models;

/**
* @OA\Schema(
* title="Client",
* description="Client model",
* @OA\Xml(
* name="Client"
* )
* )
*/
class Client
{
/**
* @OA\Property(
* title="ID",
* description="ID",
* format="int64",
* example=39
* )
*
* @var integer
*/
private $id;

/**
* @OA\Property (
* title="name",
* description="Name of client",
* example="Träwelling App"
* )
*
* @var string
*/
private $name;

/**
* @OA\Property (
* title="privacyPolicyUrl",
* description="URL to privacy policy",
* example="https://traewelling.de/privacy-policy"
* )
*
* @var string
*/
private $privacyPolicyUrl;
}
10 changes: 10 additions & 0 deletions app/Virtual/Models/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ class Status
*/
private $isLikable;

/**
* @OA\Property (
* title="client",
* description="Client model",
* )
*
* @var Client
*/
private $client;

/**
* @OA\Property (
* title="createdAt",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{

public function up(): void {
Schema::table('statuses', function(Blueprint $table) {
$table->unsignedBigInteger('client_id')->nullable()->after('tweet_id');
$table->foreign('client_id')->references('id')->on('oauth_clients')->nullOnDelete();
});
}

public function down(): void {
Schema::table('statuses', function(Blueprint $table) {
$table->dropForeign(['client_id']);
$table->dropColumn('client_id');
});
}
};
12 changes: 11 additions & 1 deletion resources/views/admin/status/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,21 @@
@isset($status->checkin->trip->operator?->name)
<small>(Betreiber: {{$status->checkin->trip->operator?->name}})</small>
@endisset
<br />
<br/>
<a href="{{route('admin.trip.show', ['id' => $status->checkin->trip_id])}}">
{{ $status->checkin->trip_id }}
</a>
</div>

<div class="col-4">
<label>Client</label>
</div>
<div class="col-8">
@isset($status?->client)
{{$status->client->name}} (#{{$status->client->id}})
@endisset
</div>

</div>
<hr/>
<form method="POST" action="{{route('admin.status.edit')}}">
Expand Down
Loading

0 comments on commit 0090246

Please sign in to comment.