Skip to content

Commit

Permalink
refactor: fix PHP styling
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt authored and github-actions[bot] committed Oct 25, 2024
1 parent f84baec commit 101b463
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 147 deletions.
51 changes: 26 additions & 25 deletions app/Http/Controllers/BrowseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,49 +646,53 @@ public function getSublist(Request $request, $key) {
}

/**
* Shows the character likes leaderboard
* Shows the character likes leaderboard.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getLikesLeaderboard(Request $request)
{
//this is a mess. please don't look.
public function getLikesLeaderboard(Request $request) {
//this is a mess. please don't look.

//abort if logged out to reduce strain
if(!Auth::check()) abort(404);

if (!Auth::check()) {
abort(404);
}

//check if enabled, if not, there's no point to fetch anything lol
if (!Settings::get('character_likes_leaderboard_enable')) {
abort(404);
}

//fetch characters
$query = Character::with('user.rank')->with('image.features')->with('rarity')->with('image.species')->myo(0)->where(function($query) {
$query = Character::with('user.rank')->with('image.features')->with('rarity')->with('image.species')->myo(0)->where(function ($query) {
//only display characters whose users allow likes
$query = $query->whereRelation('user.settings', 'allow_character_likes', 1);
});

$imageQuery = CharacterImage::images(Auth::check() ? Auth::user() : null)->with('features')->with('rarity')->with('species')->with('features');

$query->whereIn('id', $imageQuery->pluck('character_id')->toArray());

$randomcharacter = $query->visible()->get()->random(1)->first() ?? null;

if($request->get('name')) $query->where(function($query) use ($request) {
$query->where('characters.name', 'LIKE', '%' . $request->get('name') . '%')->orWhere('characters.slug', 'LIKE', '%' . $request->get('name') . '%');
});
if ($request->get('name')) {
$query->where(function ($query) use ($request) {
$query->where('characters.name', 'LIKE', '%'.$request->get('name').'%')->orWhere('characters.slug', 'LIKE', '%'.$request->get('name').'%');
});
}

if($request->get('owner')) {
if ($request->get('owner')) {
$owner = User::find($request->get('owner'));
$query->where(function($query) use ($owner) {
$query->where(function ($query) use ($owner) {
$query->where('user_id', $owner->id);
});
}

if(!Auth::check() || !Auth::user()->hasPower('manage_characters')) $query->visible();

switch($request->get('sort')) {

if (!Auth::check() || !Auth::user()->hasPower('manage_characters')) {
$query->visible();
}

switch ($request->get('sort')) {
case 'desc':
$sort = 'sortByDesc';
break;
Expand All @@ -699,15 +703,12 @@ public function getLikesLeaderboard(Request $request)
$sort = 'sortByDesc';
}



return view('browse.character_likes_leaderboard', [
'isMyo' => false,
'characters' => $query->get()->$sort('likeTotal')->paginate(24)->appends($request->query()),
'sublists' => Sublist::orderBy('sort', 'DESC')->get(),
'userOptions' => User::query()->orderBy('name')->pluck('name', 'id')->toArray(),
'isMyo' => false,
'characters' => $query->get()->$sort('likeTotal')->paginate(24)->appends($request->query()),
'sublists' => Sublist::orderBy('sort', 'DESC')->get(),
'userOptions' => User::query()->orderBy('name')->pluck('name', 'id')->toArray(),
'randomcharacter' => $randomcharacter,
]);
}

}
53 changes: 31 additions & 22 deletions app/Http/Controllers/Characters/CharacterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,37 @@ public function postCharacterApproval($slug, DesignUpdateManager $service) {
return redirect()->back();
}

/**
* Like a character.
*
* @param mixed $slug
*/
public function postLikeCharacter(Request $request, CharacterManager $service, $slug) {
if (!Auth::check()) {
abort(404);
}

//owned by same user
if (Auth::user()->id == $this->character->user->id) {
abort(404);
}

//user disabled likes
if (!$this->character->user->settings->allow_character_likes) {
abort(404);
}

if ($service->likeCharacter($this->character, Auth::user())) {
flash('Character '.__('character_likes.liked').' successfully.')->success();
} else {
foreach ($service->errors()->getMessages()['error'] as $error) {
flash($error)->error();
}
}

return redirect()->back();
}

/**
* Transfers inventory items back to a user.
*
Expand Down Expand Up @@ -616,26 +647,4 @@ private function postDelete(Request $request, InventoryManager $service) {

return redirect()->back();
}

/**
* Like a character
*/
public function postLikeCharacter(Request $request, CharacterManager $service, $slug)
{
if(!Auth::check()) abort(404);

//owned by same user
if(Auth::user()->id == $this->character->user->id) abort(404);

//user disabled likes
if(!$this->character->user->settings->allow_character_likes) abort(404);

if($service->likeCharacter($this->character, Auth::user())) {
flash('Character '.__('character_likes.liked').' successfully.')->success();
}
else {
foreach($service->errors()->getMessages()['error'] as $error) flash($error)->error();
}
return redirect()->back();
}
}
21 changes: 11 additions & 10 deletions app/Http/Controllers/Users/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,21 +476,22 @@ public function postReactivate(Request $request, UserService $service) {
return redirect()->back();
}

/**
* Change if the user's characters can be liked
/**
* Change if the user's characters can be liked.
*
* @param App\Services\UserService $service
*
* @param \Illuminate\Http\Request $request
* @param App\Services\UserService $service
* @return \Illuminate\Http\RedirectResponse
*/
public function postAllowCharacterLikes(Request $request, UserService $service)
{
if($service->updateAllowCharacterLikes($request->input('allow_character_likes'), Auth::user())) {
public function postAllowCharacterLikes(Request $request, UserService $service) {
if ($service->updateAllowCharacterLikes($request->input('allow_character_likes'), Auth::user())) {
flash('Like status updated successfully.')->success();
} else {
foreach ($service->errors()->getMessages()['error'] as $error) {
flash($error)->error();
}
}
else {
foreach($service->errors()->getMessages()['error'] as $error) flash($error)->error();
}

return redirect()->back();
}
}
15 changes: 8 additions & 7 deletions app/Models/Character/Character.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,9 @@ public function items() {
}

/**
* Get all of the likes that are not NULL
* Get all of the likes that are not NULL.
*/
public function characterLikes()
{
public function characterLikes() {
return $this->hasMany('App\Models\Character\CharacterLike')->where('character_id', $this->id)->whereNotNull('liked_at');
}

Expand Down Expand Up @@ -569,15 +568,17 @@ public function notifyBookmarkers($type) {
}
}

/**
* Return like count based on site setting
/**
* Return like count based on site setting.
*
* Will always return the accurate count even if settings are flip flopped around (i am paranoid.)
*/
public function getLikeTotalAttribute() {
//can like only once
if(!Settings::get('character_likes')) {
if (!Settings::get('character_likes')) {
return $this->characterLikes->count();
}else return $this->profile->like_count;
} else {
return $this->profile->like_count;
}
}
}
15 changes: 5 additions & 10 deletions app/Models/Character/CharacterLike.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Models\Model;

class CharacterLike extends Model {

/**
* The attributes that are mass assignable.
*
Expand All @@ -29,33 +28,29 @@ class CharacterLike extends Model {
*/
public $dates = ['liked_at'];


/**********************************************************************************************
RELATIONS
**********************************************************************************************/

/**
* Character associated with the like
* Character associated with the like.
*/
public function character() {
return $this->belongsTo('App\Models\Character\Character', 'character_id');
}


/**
* User associated with the like
* User associated with the like.
*/
public function user() {
return $this->belongsTo('App\Models\User\User', 'user_id');
}

/**********************************************************************************************
ATTRIBUTES
**********************************************************************************************/


}
}
27 changes: 15 additions & 12 deletions app/Models/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ public function commentLikes() {
/**
* Get all of the user's character like data.
*/
public function characterLikes()
{
public function characterLikes() {
return $this->hasMany('App\Models\Character\CharacterLike')->where('user_id', $this->id);
}

Expand Down Expand Up @@ -684,17 +683,18 @@ public function hasBookmarked($character) {
}

/**
* Check the user's like for the character
* Check the user's like for the character.
*
* @param mixed $character
*/
public function checkLike($character) {

//check for the like and create if nonexistent

$like = $this->characterLikes()->where('character_id', $character->id)->first();

if (!$like) {
$createdlike = $this->characterLikes()->create([
'user_id' => $this->id,
'user_id' => $this->id,
'character_id' => $character->id,
]);
$this->refresh();
Expand All @@ -703,37 +703,40 @@ public function checkLike($character) {
}

/**
* Check if user can like the character again
* Check if user can like the character again.
*
* @param mixed $character
*/
public function canLike($character) {

//triplecheck that a like exists even though we spammed this check literally everywhere.
$like = $this->characterLikes()->where('character_id', $character->id)->first();

if (!$like) {
$createdlike = $this->characterLikes()->create([
'user_id' => $this->id,
'user_id' => $this->id,
'character_id' => $character->id,
]);
$this->refresh();
$createdlike->refresh();
}

//user disabled likes on their characters
if(!$character->user->settings->allow_character_likes) return false;
if (!$character->user->settings->allow_character_likes) {
return false;
}

//already liked
if ($like->liked_at) {

//can only like once
if(!Settings::get('character_likes')) {
if (!Settings::get('character_likes')) {
return false;
}
//can like daily
if ($like->liked_at->isToday()) {
return false;
}
}

//else you can :)
return true;
}
Expand Down
Loading

0 comments on commit 101b463

Please sign in to comment.