-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f6afd52
commit fa5738d
Showing
15 changed files
with
520 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Comment; | ||
use Illuminate\Http\Request; | ||
use App\Models\User; | ||
|
||
class ProfileCommentsController extends Controller | ||
{ | ||
/** | ||
* @param \App\Models\User $user | ||
* @param array $data | ||
* | ||
* @return string | ||
*/ | ||
public function show(User $user, array $data = []) | ||
{ | ||
return view( | ||
'pages.profile.tabs.comments-tab', | ||
array_merge($data, [ | ||
'comments' => $user->comments, | ||
'user' => $user, | ||
'active' => 'comments' | ||
]) | ||
)->fragmentIf(!request()->isMethod('GET'), 'comments'); | ||
} | ||
|
||
/** | ||
* @param \App\Models\Comment $comment | ||
* | ||
* @return string | ||
*/ | ||
public function showReply(Comment $comment) | ||
{ | ||
return $this->show($comment->commenter, [ | ||
'reply' => $comment->getKey(), | ||
]); | ||
} | ||
|
||
/** | ||
* @param \App\Models\Comment $comment | ||
* | ||
* @return string | ||
*/ | ||
public function showEdit(Comment $comment) | ||
{ | ||
return $this->show($comment->commenter, [ | ||
'edit' => $comment->getKey(), | ||
]); | ||
} | ||
|
||
/** | ||
* @param \Illuminate\Http\Request $request | ||
* @param \App\Models\Comment $comment | ||
* | ||
* @return string | ||
*/ | ||
public function reply(Request $request, Comment $comment) | ||
{ | ||
$this->authorize('reply', $comment); | ||
|
||
$request->validate([ | ||
'message' => 'required|string', | ||
]); | ||
|
||
$reply = new Comment([ | ||
'comment' => $request->input('message'), | ||
'approved' => true, | ||
]); | ||
|
||
$reply->commenter()->associate($request->user()); | ||
$reply->commentable()->associate($comment->commentable); | ||
$reply->parent()->associate($comment); | ||
$reply->save(); | ||
|
||
return $this->show($comment->commenter); | ||
} | ||
|
||
/** | ||
* @param \Illuminate\Http\Request $request | ||
* @param \App\Models\Comment $comment | ||
* | ||
* @return string | ||
*/ | ||
public function update(Request $request, Comment $comment) | ||
{ | ||
$this->authorize('update', $comment); | ||
|
||
$request->validate([ | ||
'message' => 'required|string', | ||
]); | ||
|
||
$comment->update([ | ||
'comment' => $request->message, | ||
]); | ||
|
||
return $this->show($comment->commenter); | ||
} | ||
|
||
/** | ||
* @param \App\Models\Comment $comment | ||
* | ||
* @return string | ||
*/ | ||
public function delete(Comment $comment) | ||
{ | ||
$this->authorize('delete', $comment); | ||
|
||
$comment->children()->exists() | ||
? $comment->delete() | ||
: $comment->forceDelete(); | ||
|
||
return $this->show($comment->commenter); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace App\Policies; | ||
|
||
use App\Models\User; | ||
use Illuminate\Auth\Access\HandlesAuthorization; | ||
use Illuminate\Auth\Access\Response; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
trait OwnerPolicy | ||
{ | ||
use HandlesAuthorization; | ||
|
||
/** | ||
* Determine whether the user can view the model. | ||
* | ||
* @param User $user | ||
* @param Model $model | ||
* | ||
* @return mixed | ||
*/ | ||
public function owner(User $user, Model $model) | ||
{ | ||
return ($user->id === $model->user_id) || is_null($model->user_id) | ||
? Response::allow() | ||
: Response::denyAsNotFound(); | ||
} | ||
|
||
/** | ||
* Determine whether the user can view the model. | ||
* | ||
* @param User $user | ||
* @param Model $model | ||
* | ||
* @return bool | ||
*/ | ||
public function isOwner(User $user, Model $model): bool | ||
{ | ||
return is_null($model->user_id) || ($user->id === $model->user_id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace App\Policies; | ||
|
||
class PostPolicy | ||
{ | ||
use OwnerPolicy; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.