Skip to content

Commit

Permalink
feat: add strike expiry, cleanups, some configs
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt committed Oct 25, 2024
1 parent 66fc5aa commit 373c944
Show file tree
Hide file tree
Showing 30 changed files with 465 additions and 301 deletions.
46 changes: 46 additions & 0 deletions app/Console/Commands/RemoveExpiredStrikes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Facades\Settings;
use App\Models\Mail\ModMail;
use App\Services\UserService;

class RemoveExpiredStrikes extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'remove-expired-strikes';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Removes expired strikes from mod mail.';

/**
* Execute the console command.
*/
public function handle()
{
//
$mails = ModMail::where('has_expired', 0)->where('issue_strike', 1)->where('strike_expiry', '<', now())->get();
foreach ($mails as $mail) {
$mail->update(['has_expired' => 1]);
$newStrikeCount = $mail->user->settings->strike_count - $mail->strike_count;
$mail->user->settings->update(['strike_count' => $newStrikeCount > 0 ? $newStrikeCount : 0]);

if (config('lorekeeper.mod_mail.unban_on_strike_expiry') && $newStrikeCount < Settings::get('max_strike_count')) {
$service = new UserService;
$service->unban($mail->user);

$this->info('Unbanned user ' . $mail->recipient->username . ' due to expired strike.');
}
}
}
}
26 changes: 17 additions & 9 deletions app/Http/Controllers/Admin/ModMailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use App\Models\Mail\ModMail;
use App\Models\User\User;
use App\Services\MailService;
use Auth;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class ModMailController extends Controller {
Expand All @@ -15,9 +15,16 @@ class ModMailController extends Controller {
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getIndex() {
public function getIndex(Request $request) {
$query = ModMail::query();
$data = $request->only(['recipient_id']);
if (isset($data['recipient_id']) && $data['recipient_id'] !== 'Select User') {
$query->where('user_id', $data['recipient_id']);
}

return view('admin.mail.index', [
'mails' => ModMail::orderBy('id', 'DESC')->paginate(20),
'mails' => $query->orderBy('id', 'DESC')->paginate(30),
'users' => User::orderBy('id')->pluck('name', 'id')->toArray(),
]);
}

Expand All @@ -44,7 +51,6 @@ public function getMail($id) {
public function getCreateMail() {
return view('admin.mail.create_mail', [
'mail' => new ModMail,
//'users' => ['Select User'] + User::where('id', '!=', Auth::user()->id)->orderBy('id')->pluck('name', 'id')->toArray(),
'users' => ['Select User'] + User::orderBy('id')->pluck('name', 'id')->toArray(),
]);
}
Expand All @@ -54,15 +60,17 @@ public function getCreateMail() {
*/
public function postCreateMail(Request $request, MailService $service) {
$request->validate(ModMail::$createRules);
$data = $request->only(['user_id', 'subject', 'message', 'issue_strike', 'strike_count']);
if ($service->createMail($data, Auth::user())) {
flash('Mod mail sent successfully.')->success();
} else {
$data = $request->only(['user_id', 'subject', 'message', 'issue_strike', 'strike_count', 'strike_expiry']);
if (!$mail = $service->createMail($data, Auth::user())) {
foreach ($service->errors()->getMessages()['error'] as $error) {
flash($error)->error();
}

return redirect()->back();
}

return redirect()->back();
flash('Mod mail sent successfully.')->success();

return redirect()->to('admin/mail/view/'.$mail->id);
}
}
8 changes: 8 additions & 0 deletions app/Http/Controllers/Comments/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use App\Models\Report\Report;
use App\Models\Sales\Sales;
use App\Models\SitePage;
use App\Models\Mail\ModMail;
use App\Models\Mail\UserMail;
use App\Models\User\User;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
Expand Down Expand Up @@ -147,6 +149,12 @@ public function store(Request $request, $model, $id) {
$post = (($type != 'User-User') ? 'your gallery submission\'s staff comments' : 'your gallery submission');
$link = (($type != 'User-User') ? $submission->queueUrl.'/#comment-'.$comment->getKey() : $submission->url.'/#comment-'.$comment->getKey());
break;
case 'App\Models\Mail\ModMail':
$mail = ModMail::find($comment->commentable_id);
$recipient = $mail->staff;
$post = 'your sent mod mails';
$link = 'mail/view/'.$comment->commentable_id.'/#comment-'.$comment->getKey();
break;
default:
throw new \Exception('Comment type not supported.');
break;
Expand Down
50 changes: 33 additions & 17 deletions app/Http/Controllers/Users/MailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Http\Request;

class MailController extends Controller {

/**
* Shows the mail index.
*
Expand All @@ -21,8 +22,8 @@ public function getIndex() {
abort(404);
}

return view('home.mail.mail_index', [
'mails' => ModMail::where('user_id', Auth::user()->id)->orderBy('created_at', 'desc')->get(),
return view('home.mail.index', [
'modMail' => ModMail::where('user_id', Auth::user()->id)->orderBy('created_at', 'desc')->get(),
'inbox' => UserMail::where('recipient_id', Auth::user()->id)->orderBy('created_at', 'desc')->get(),
'outbox' => UserMail::where('sender_id', Auth::user()->id)->orderBy('created_at', 'desc')->get(),
]);
Expand All @@ -35,7 +36,7 @@ public function getIndex() {
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getMail($id) {
public function getModMail($id) {
if (!Auth::check()) {
abort(404);
}
Expand All @@ -45,7 +46,7 @@ public function getMail($id) {
$mail->update(['seen' => 1]);
}

return view('home.mail.mail', [
return view('home.mail.mod_mail', [
'mail' => $mail,
]);
}
Expand All @@ -58,7 +59,7 @@ public function getMail($id) {
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getUserMail($id) {
if (!Auth::check()) {
if (!Auth::check() || !config('lorekeeper.mod_mail.allow_user_mail')) {
abort(404);
}
$mail = UserMail::findOrFail($id);
Expand All @@ -82,32 +83,47 @@ public function getUserMail($id) {
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getCreateUserMail() {
if (!config('lorekeeper.mod_mail.allow_user_mail')) {
abort(404);
}
return view('home.mail.create_user_mail', [
'mail' => new UserMail,
'users' => ['Select User'] + User::orderBy('id')->where('id', '!=', Auth::user()->id)->pluck('name', 'id')->toArray(),
'users' => User::orderBy('id')->where('id', '!=', Auth::user()->id)->pluck('name', 'id')->toArray(),
]);
}

/**
* Sends mail from one user to another.
*
* @param Request $request
* @param MailService $service
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postCreateUserMail(Request $request, MailService $service) {
$request->validate(UserMail::$createRules);
$data = $request->only(['recipient_id', 'subject', 'message', 'parent_id']);
if ($service->createUserMail($data, Auth::user())) {
flash('Message sent successfully.')->success();
public function postCreateUserMail(Request $request, MailService $service, $mail_id = null) {
if (!config('lorekeeper.mod_mail.allow_user_mail')) {
abort(404);
}
$data = $request->only(['recipient_id', 'subject', 'message']);
$mail = $mail_id ? UserMail::findOrFail($mail_id) : null;
if ($mail) {
$data['recipient_id'] = $mail->sender_id;
$data['subject'] = 'Re: '.$mail->subject;
$data['parent_id'] = $mail->id;
} else {
$request->validate(UserMail::$createRules);
}

if (!$mail = $service->createUserMail($data, Auth::user())) {
foreach ($service->errors()->getMessages()['error'] as $error) {
flash($error)->error();
}
}

if (!isset($data['parent_id'])) {
return redirect()->back();
return redirect()->to('mail');
} else {
$child = UserMail::where('parent_id', $data['parent_id'])->latest('id')->first();

return redirect('inbox/view/'.$child->id);
flash('Message sent successfully.')->success();
}

return redirect()->to('mail/view/'.$mail->id);
}
}
22 changes: 19 additions & 3 deletions app/Models/Mail/ModMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace App\Models\Mail;

use App\Models\Model;
use App\Models\User\User;
use Illuminate\Support\Facades\Auth;
use App\Traits\Commentable;

class ModMail extends Model {
Expand All @@ -14,6 +16,7 @@ class ModMail extends Model {
*/
protected $fillable = [
'staff_id', 'user_id', 'subject', 'message', 'issue_strike', 'strike_count', 'previous_strike_count', 'seen',
'strike_expiry', 'has_expired',
];

/**
Expand All @@ -30,6 +33,15 @@ class ModMail extends Model {
*/
public $timestamps = true;

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'strike_expiry' => 'datetime',
];

/**
* Validation rules for creation.
*
Expand All @@ -50,14 +62,14 @@ class ModMail extends Model {
* Get the staff that sent the message.
*/
public function staff() {
return $this->belongsTo('App\Models\User\User', 'staff_id');
return $this->belongsTo(User::class, 'staff_id');
}

/**
* Get the user who was sent the message.
*/
public function user() {
return $this->belongsTo('App\Models\User\User');
return $this->belongsTo(User::class);
}

/**********************************************************************************************
Expand All @@ -81,6 +93,10 @@ public function getDisplayNameAttribute() {
* @return string
*/
public function getViewUrlAttribute() {
return url('mail/view/'.$this->id);
if (Auth::user()->id != $this->recipient_id || Auth::user()->id == $this->user_id) {
return url('admin/mail/view/'.$this->id);
}

return url('inbox/view/'.$this->id);
}
}
21 changes: 8 additions & 13 deletions app/Models/Mail/UserMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace App\Models\Mail;

use App\Models\Model;
use App\Traits\Commentable;
use App\Models\User\User;

class UserMail extends Model {
use Commentable;

/**
* The attributes that are mass assignable.
*
Expand Down Expand Up @@ -51,28 +51,28 @@ class UserMail extends Model {
* Get the staff that sent the message.
*/
public function sender() {
return $this->belongsTo('App\Models\User\User', 'sender_id');
return $this->belongsTo(User::class, 'sender_id');
}

/**
* Get the user who was sent the message.
*/
public function recipient() {
return $this->belongsTo('App\Models\User\User');
return $this->belongsTo(User::class, 'recipient_id');
}

/**
* Get the parent message.
*/
public function parent() {
return $this->belongsTo($this, 'parent_id');
return $this->belongsTo(UserMail::class, 'parent_id');
}

/**
* Get the child messages.
*/
public function children() {
return $this->hasMany($this, 'parent_id');
return $this->hasMany(UserMail::class, 'parent_id', 'id');
}

/**********************************************************************************************
Expand All @@ -87,12 +87,7 @@ public function children() {
* @return string
*/
public function getDisplayNameAttribute() {
$prefix = '';
if ($this->parent) {
$prefix = 'Re:';
}

return '<a href="'.$this->url.'">'.$prefix.$this->subject.'</a>';
return '<a href="'.$this->url.'">'.$this->subject.'</a>';
}

/**
Expand All @@ -101,6 +96,6 @@ public function getDisplayNameAttribute() {
* @return string
*/
public function getViewUrlAttribute() {
return url('inbox/view/'.$this->id);
return url('mail/view/'.$this->id);
}
}
2 changes: 1 addition & 1 deletion app/Models/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public function getDisplayAliasAttribute() {
return '(Unverified)';
}

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

/**
Expand Down
Loading

0 comments on commit 373c944

Please sign in to comment.