Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
MrKrisKrisu committed Jun 28, 2024
1 parent 28f58e7 commit 3ac8dd6
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 65 deletions.
20 changes: 20 additions & 0 deletions app/Enum/User/FriendCheckinSetting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace App\Enum\User;

/**
* @OA\Schema(
* title="FriendCheckinSetting",
* type="string",
* enum={"forbidden", "friends", "list"},
* example="forbidden",
* )
*/
enum FriendCheckinSetting: string
{
case FORBIDDEN = 'forbidden'; // default
case FRIENDS = 'friends'; // user who are following each other
case LIST = 'list'; // specific list of users
}

75 changes: 38 additions & 37 deletions app/Http/Controllers/API/v1/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Enum\MapProvider;
use App\Enum\MastodonVisibility;
use App\Enum\StatusVisibility;
use App\Enum\User\FriendCheckinSetting;
use App\Exceptions\RateLimitExceededException;
use App\Http\Controllers\Backend\SettingsController as BackendSettingsController;
use App\Http\Resources\UserProfileSettingsResource;
Expand Down Expand Up @@ -62,39 +63,6 @@ public function updateMail(Request $request): UserProfileSettingsResource|JsonRe
}
}

public function resendMail(): void {
try {
auth()->user()->sendEmailVerificationNotification();
$this->sendResponse('', 204);
} catch (RateLimitExceededException) {
$this->sendError(error: __('email.verification.too-many-requests'), code: 429);
}
}

/**
* @throws ValidationException
*/
public function updatePassword(Request $request): UserProfileSettingsResource|JsonResponse {
$userHasPassword = auth()->user()->password !== null;

$validated = $request->validate([
'currentPassword' => [Rule::requiredIf($userHasPassword)],
'password' => ['required', 'string', 'min:8', 'confirmed']
]);

if ($userHasPassword && !Hash::check($validated['currentPassword'], auth()->user()->password)) {
throw ValidationException::withMessages([__('controller.user.password-wrong')]);
}

$validated['password'] = Hash::make($validated['password']);

try {
return new UserProfileSettingsResource(BackendSettingsController::updateSettings($validated));
} catch (RateLimitExceededException) {
return $this->sendError(error: __('email.verification.too-many-requests'), code: 400);
}
}

/**
* @OA\Put(
* path="/settings/profile",
Expand Down Expand Up @@ -146,10 +114,9 @@ public function updatePassword(Request $request): UserProfileSettingsResource|Js
*/
public function updateSettings(Request $request): UserProfileSettingsResource|JsonResponse {
$validated = $request->validate([
'username' => ['required',
'string',
'max:25',
'regex:/^[a-zA-Z0-9_]*$/'],
'username' => [
'required', 'string', 'max:25', 'regex:/^[a-zA-Z0-9_]*$/'
],
'displayName' => ['required', 'string', 'max:50'],
'privateProfile' => ['boolean', 'nullable'],
'preventIndex' => ['boolean', 'nullable'],
Expand All @@ -163,6 +130,7 @@ public function updateSettings(Request $request): UserProfileSettingsResource|Js
new Enum(MastodonVisibility::class),
],
'mapProvider' => ['nullable', new Enum(MapProvider::class)],
'friendCheckin' => ['nullable', new Enum(FriendCheckinSetting::class)]
]);

try {
Expand All @@ -172,6 +140,39 @@ public function updateSettings(Request $request): UserProfileSettingsResource|Js
}
}

public function resendMail(): void {
try {
auth()->user()->sendEmailVerificationNotification();
$this->sendResponse('', 204);
} catch (RateLimitExceededException) {
$this->sendError(error: __('email.verification.too-many-requests'), code: 429);
}
}

/**
* @throws ValidationException
*/
public function updatePassword(Request $request): UserProfileSettingsResource|JsonResponse {
$userHasPassword = auth()->user()->password !== null;

$validated = $request->validate([
'currentPassword' => [Rule::requiredIf($userHasPassword)],
'password' => ['required', 'string', 'min:8', 'confirmed']
]);

if ($userHasPassword && !Hash::check($validated['currentPassword'], auth()->user()->password)) {
throw ValidationException::withMessages([__('controller.user.password-wrong')]);
}

$validated['password'] = Hash::make($validated['password']);

try {
return new UserProfileSettingsResource(BackendSettingsController::updateSettings($validated));
} catch (RateLimitExceededException) {
return $this->sendError(error: __('email.verification.too-many-requests'), code: 400);
}
}

/**
* Undocumented and unofficial API Endpoint
*
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Resources/UserBaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public function toArray($request): array {
[
'home' => $this->home,
'language' => $this->language,
'defaultStatusVisibility' => $this->default_status_visibility
'defaultStatusVisibility' => $this->default_status_visibility,
'friendCheckin' => $this->friend_checkin,
]),
$this->mergeWhen(isset($this->UserResource),
[
Expand Down
53 changes: 29 additions & 24 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Enum\MapProvider;
use App\Enum\StatusVisibility;
use App\Enum\User\FriendCheckinSetting;
use App\Exceptions\RateLimitExceededException;
use App\Http\Controllers\Backend\Social\MastodonProfileDetails;
use App\Jobs\SendVerificationEmail;
Expand All @@ -25,25 +26,27 @@
use Spatie\Permission\Traits\HasRoles;

/**
* @property int id
* @property string username
* @property string name
* @property string avatar
* @property string email
* @property Carbon email_verified_at
* @property string password
* @property int home_id
* @property Carbon privacy_ack_at
* @property integer default_status_visibility
* @property boolean private_profile
* @property boolean prevent_index
* @property boolean likes_enabled
* @property MapProvider mapprovider
* @property int privacy_hide_days
* @property string language
* @property Carbon last_login
* @property Status[] $statuses
* @property SocialLoginProfile socialProfile
* @property int id
* @property string username
* @property string name
* @property string avatar
* @property string email
* @property Carbon email_verified_at
* @property string password
* @property int home_id
* @property Carbon privacy_ack_at
* @property integer default_status_visibility
* @property boolean private_profile
* @property boolean prevent_index
* @property boolean likes_enabled
* @property MapProvider mapprovider
* @property string timezone
* @property FriendCheckinSetting friend_checkin
* @property int privacy_hide_days
* @property string language
* @property Carbon last_login
* @property Status[] $statuses
* @property SocialLoginProfile socialProfile
*
* @todo replace "role" with an explicit permission system - e.g. spatie/laravel-permission
* @todo replace "experimental" also with an explicit permission system - user can add self to "experimental" group
Expand All @@ -60,7 +63,7 @@ class User extends Authenticatable implements MustVerifyEmail
protected $fillable = [
'username', 'name', 'avatar', 'email', 'email_verified_at', 'password', 'home_id', 'privacy_ack_at',
'default_status_visibility', 'likes_enabled', 'private_profile', 'prevent_index', 'privacy_hide_days',
'language', 'last_login', 'mapprovider', 'timezone',
'language', 'last_login', 'mapprovider', 'timezone', 'friend_checkin',
];
protected $hidden = [
'password', 'remember_token', 'email', 'email_verified_at', 'privacy_ack_at',
Expand All @@ -82,16 +85,14 @@ class User extends Authenticatable implements MustVerifyEmail
'privacy_hide_days' => 'integer',
'last_login' => 'datetime',
'mapprovider' => MapProvider::class,
'timezone' => 'string',
'friend_checkin' => FriendCheckinSetting::class,
];

public function getTrainDistanceAttribute(): float {
return Checkin::where('user_id', $this->id)->sum('distance');
}

public function statuses(): HasMany {
return $this->hasMany(Status::class);
}

public function trainCheckins(): HasMany {
return $this->hasMany(Checkin::class, 'user_id', 'id');
}
Expand Down Expand Up @@ -172,6 +173,10 @@ public function getPointsAttribute(): int {
->sum('points');
}

public function statuses(): HasMany {
return $this->hasMany(Status::class);
}

/**
* @untested
* @todo test
Expand Down
17 changes: 17 additions & 0 deletions app/Policies/UserPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Policies;

use App\Enum\User\FriendCheckinSetting;
use App\Http\Controllers\Backend\User\BlockController;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
Expand Down Expand Up @@ -78,4 +79,20 @@ public function update(User $user, User $model): bool {
public function delete(User $user, User $model): bool {
return $user->id === $model->id;
}

public function checkin(User $user, User $userToCheckin): bool {
if ($user->is($userToCheckin)) {
return true;
}
if ($userToCheckin->friend_checkin === FriendCheckinSetting::FORBIDDEN) {
return false;
}
if ($userToCheckin->friend_checkin === FriendCheckinSetting::FRIENDS) {
//TODO
}
if ($userToCheckin->friend_checkin === FriendCheckinSetting::LIST) {
//TODO
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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('users', function(Blueprint $table) {
$table->string('friend_checkin')->default('forbidden')->after('timezone');
});
}

public function down(): void {
Schema::table('users', function(Blueprint $table) {
$table->dropColumn('friend_checkin');
});
}
};
8 changes: 5 additions & 3 deletions resources/views/includes/status.blade.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
@php
use App\Enum\Business;
use App\Http\Controllers\Backend\Helper\StatusHelper;use App\Http\Controllers\Backend\Transport\StationController;
use App\Http\Controllers\Backend\Transport\StatusController;use App\Http\Controllers\Backend\User\ProfilePictureController;use Illuminate\Support\Facades\Gate;
use App\Http\Controllers\Backend\Helper\StatusHelper;
use App\Http\Controllers\Backend\Transport\StationController;
use App\Http\Controllers\Backend\Transport\StatusController;
use App\Http\Controllers\Backend\User\ProfilePictureController;
@endphp
@php /** @var App\Models\Status $status */ @endphp
<div class="card status mb-3" id="status-{{ $status->id }}"
Expand Down Expand Up @@ -117,7 +119,7 @@ class="text-trwl clearfix">
</span>
@endif
</p>

@if(!empty($status->body))
<p class="status-body"><i class="fas fa-quote-right" aria-hidden="true"></i>
{!! StatusController::getPrintableEscapedBody($status) !!}
Expand Down

0 comments on commit 3ac8dd6

Please sign in to comment.