Skip to content

Commit

Permalink
feat: character likes
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt committed Oct 25, 2024
1 parent 101b463 commit 8180609
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 128 deletions.
2 changes: 1 addition & 1 deletion app/Console/Commands/AddSiteSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function handle() {

$this->addSiteSetting('group_currency', 1, 'ID of the group currency to award from gallery submissions (if enabled).');

$this->addSiteSetting('character_likes', 1, '0: Characters can be liked only once, 1: Characters can be liked daily.');
$this->addSiteSetting('daily_character_likes', 1, '0: Characters can be liked only once, 1: Characters can be liked daily.');

$this->addSiteSetting('character_likes_leaderboard_enable', 1, '0: Disable leaderboard, 1: Enable leaderboard.');

Expand Down
18 changes: 4 additions & 14 deletions app/Http/Controllers/BrowseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,33 +651,22 @@ public function getSublist(Request $request, $key) {
* @return \Illuminate\Contracts\Support\Renderable
*/
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);
}

//check if enabled, if not, there's no point to fetch anything lol
if (!Settings::get('character_likes_leaderboard_enable')) {
if (!Auth::check() || !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) {
//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')) {
if ($request->get('id')) {
$query->where(function ($query) use ($request) {
$query->where('characters.name', 'LIKE', '%'.$request->get('name').'%')->orWhere('characters.slug', 'LIKE', '%'.$request->get('name').'%');
$query->where('characters.id', $request->get('id'));
});
}

Expand Down Expand Up @@ -705,6 +694,7 @@ public function getLikesLeaderboard(Request $request) {

return view('browse.character_likes_leaderboard', [
'isMyo' => false,
'sortCharacters' => Character::visible(Auth::user() ?? null)->get()->pluck('fullName', 'id')->toArray(),
'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(),
Expand Down
30 changes: 14 additions & 16 deletions app/Http/Controllers/Characters/CharacterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ public function __construct() {

$this->character->updateOwner();

if (Auth::check()) {
Auth::user()->checkLike($this->character);
}

if (config('lorekeeper.extensions.previous_and_next_characters.display')) {
$query = Character::myo(0);
// Get only characters of this category if pull number is limited to category
Expand Down Expand Up @@ -566,25 +562,27 @@ public function postLikeCharacter(Request $request, CharacterManager $service, $
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) {
if (Auth::user()->id == $this->character->user->id || !$this->character->user->settings->allow_character_likes) {
abort(404);
}

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

return response()->json([
'success' => false,
'message' => implode(' ', $message),
]);
}

return redirect()->back();
return response()->json([
'success' => true,
'message' => 'That was very skibidi of you.',
'likes' => Settings::get('daily_character_likes') ? $this->character->likes->count() : $this->character->likes()->sum('total_likes'),
]);
}

/**
Expand Down
15 changes: 5 additions & 10 deletions app/Models/Character/Character.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ public function items() {
}

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

/**********************************************************************************************
Expand Down Expand Up @@ -571,14 +571,9 @@ public function notifyBookmarkers($type) {
/**
* Return like count based on site setting.
*
* Will always return the accurate count even if settings are flip flopped around (i am paranoid.)
* Will always return the accurate count even if settings are flipped in future.
*/
public function getLikeTotalAttribute() {
//can like only once
if (!Settings::get('character_likes')) {
return $this->characterLikes->count();
} else {
return $this->profile->like_count;
}
return Settings::get('daily_character_likes') ? $this->likes->count() : $this->likes()->sum('total_likes');
}
}
19 changes: 8 additions & 11 deletions app/Models/Character/CharacterLike.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models\Character;

use App\Models\Model;
use App\Models\User\User;

class CharacterLike extends Model {
/**
Expand All @@ -11,7 +12,7 @@ class CharacterLike extends Model {
* @var array
*/
protected $fillable = [
'character_id', 'user_id', 'liked_at',
'character_id', 'user_id', 'liked_at', 'total_likes',
];

/**
Expand All @@ -22,11 +23,13 @@ class CharacterLike extends Model {
protected $table = 'character_likes';

/**
* Dates on the model to convert to Carbon instances.
* The attributes that should be cast to native types.
*
* @var array
*/
public $dates = ['liked_at'];
protected $casts = [
'liked_at' => 'datetime',
];

/**********************************************************************************************
Expand All @@ -38,19 +41,13 @@ class CharacterLike extends Model {
* Character associated with the like.
*/
public function character() {
return $this->belongsTo('App\Models\Character\Character', 'character_id');
return $this->belongsTo(Character::class, 'character_id');
}

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

/**********************************************************************************************
ATTRIBUTES
**********************************************************************************************/
}
2 changes: 1 addition & 1 deletion app/Models/Character/CharacterProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CharacterProfile extends Model {
* @var array
*/
protected $fillable = [
'character_id', 'text', 'parsed_text', 'link', 'like_count',
'character_id', 'text', 'parsed_text', 'link',
];

/**
Expand Down
56 changes: 11 additions & 45 deletions app/Models/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Facades\Settings;
use App\Models\Character\Character;
use App\Models\Character\CharacterBookmark;
use App\Models\Character\CharacterLike;
use App\Models\Character\CharacterImageCreator;
use App\Models\Comment\CommentLike;
use App\Models\Currency\Currency;
Expand Down Expand Up @@ -200,7 +201,7 @@ public function commentLikes() {
* Get all of the user's character like data.
*/
public function characterLikes() {
return $this->hasMany('App\Models\Character\CharacterLike')->where('user_id', $this->id);
return $this->hasMany(CharacterLike::class)->where('user_id', $this->id);
}

/**********************************************************************************************
Expand Down Expand Up @@ -374,7 +375,7 @@ public function getDisplayAliasAttribute() {
return '(Unverified)';
}

return $this->primaryAlias->displayAlias;
return $this->primaryAlias?->displayAlias ?? '(No Alias)';
}

/**
Expand Down Expand Up @@ -682,62 +683,27 @@ public function hasBookmarked($character) {
return CharacterBookmark::where('user_id', $this->id)->where('character_id', $character->id)->first();
}

/**
* 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,
'character_id' => $character->id,
]);
$this->refresh();
$createdlike->refresh();
}
}

/**
* Check if user can like the character again.
*
* @param mixed $character
*
* @return bool
*/
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 (!$character->user->settings->allow_character_likes) {
return false;
}

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

//user disabled likes on their characters
if (!$character->user->settings->allow_character_likes) {
if (!Settings::get('daily_character_likes') || ($like->liked_at && $like->liked_at->isToday())) {
return false;
}

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

//else you can :)
return true;
}
}
18 changes: 8 additions & 10 deletions app/Services/CharacterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Models\Character\CharacterCurrency;
use App\Models\Character\CharacterDesignUpdate;
use App\Models\Character\CharacterFeature;
use App\Models\Character\CharacterLike;
use App\Models\Character\CharacterImage;
use App\Models\Character\CharacterTransfer;
use App\Models\Sales\SalesCharacter;
Expand Down Expand Up @@ -1842,31 +1843,28 @@ public function likeCharacter($character, $user) {
DB::beginTransaction();

try {
//throw in another like check
$user->checkLike($character);

//check all the like criteria
if (!$user->canLike($character)) {
throw new \Exception('You cannot '.__('character_likes.like').' this character.');
}

//character is owned by you!
if ($user->id == $character->user->id) {
throw new \Exception('You cannot '.__('character_likes.like').' a character that you own.');
}

//character's owner disabled likes
if (!$character->user->settings->allow_character_likes) {
throw new \Exception("This character's user is not allowing ".__('character_likes.likes').'.');
}

//increment like count
$character->profile->like_count += 1;
$character->profile->save();

//mark the like as liked now
$like = $user->characterLikes()->where('character_id', $character->id)->first();
if (!$like) {
$like = CharacterLike::create([
'user_id' => $user->id,
'character_id' => $character->id,
]);
}
$like->liked_at = Carbon::now();
$like->total_likes += 1;
$like->save();

return $this->commitReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ public function up() {
$table->id();
$table->unsignedInteger('character_id');
$table->unsignedInteger('user_id');
$table->integer('total_likes')->default(0);
$table->timestamp('liked_at')->nullable()->default(null);
});

Schema::table('character_profiles', function (Blueprint $table) {
$table->unsignedInteger('like_count')->default(0);
});

Schema::table('user_settings', function (Blueprint $table) {
$table->boolean('allow_character_likes')->default(true);
});
Expand All @@ -30,9 +27,6 @@ public function up() {
*/
public function down() {
Schema::dropIfExists('character_likes');
Schema::table('character_profiles', function (Blueprint $table) {
$table->dropColumn('like_count');
});
Schema::table('user_settings', function (Blueprint $table) {
$table->dropColumn('allow_character_likes');
});
Expand Down
11 changes: 9 additions & 2 deletions resources/views/browse/character_likes_leaderboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
{!! Form::open(['method' => 'GET']) !!}
<div class="form-inline justify-content-end">
<div class="masterlist-search-field">
{!! Form::label('name', 'Character Name/Code: ', ['class' => 'mr-2']) !!}
{!! Form::text('name', Request::get('name'), ['class' => 'form-control']) !!}
{!! Form::label('id', 'Character Name/Code: ', ['class' => 'mr-2']) !!}
{!! Form::select('id', $sortCharacters, null, ['class' => 'form-control selectize mt-2', 'style' => 'width: 250px', 'placeholder' => 'Select a Character']) !!}
</div>
<div class="masterlist-search-field">
{!! Form::label('owner', 'Owner Username: ') !!}
Expand Down Expand Up @@ -74,3 +74,10 @@

<div class="text-center mt-4 small text-muted">{{ $characters->total() }} result{{ $characters->total() == 1 ? '' : 's' }} found.</div>
@endsection
@section('scripts')
<script>
$(document).ready(function() {
$('.selectize').selectize();
});
</script>
@endsection
Loading

0 comments on commit 8180609

Please sign in to comment.