Add AccountInterstitial model and controller

This commit is contained in:
Daniel Supernault 2020-12-05 00:08:05 -07:00
parent 77bb2299bd
commit 8766ccfe4f
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
3 changed files with 159 additions and 0 deletions

View file

@ -0,0 +1,30 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class AccountInterstitial extends Model
{
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['read_at', 'appeal_requested_at'];
public const JSON_MESSAGE = 'Please use web browser to proceed.';
public function user()
{
return $this->belongsTo(User::class);
}
public function status()
{
if($this->item_type != 'App\Status') {
return;
}
return $this->hasOne(Status::class, 'id', 'item_id');
}
}

View file

@ -0,0 +1,76 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Status;
use App\AccountInterstitial;
class AccountInterstitialController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function get(Request $request)
{
$interstitial = $request->user()
->interstitials()
->whereNull('read_at')
->first();
if(!$interstitial) {
$user = $request->user();
$user->has_interstitial = false;
$user->save();
return redirect('/');
}
$meta = json_decode($interstitial->meta);
$view = $interstitial->view;
return view($view, compact('interstitial', 'meta'));
}
public function read(Request $request)
{
$this->validate($request, [
'id' => 'required',
'type' => 'required|in:post.cw,post.removed,post.unlist',
'action' => 'required|in:appeal,confirm',
'appeal_message' => 'nullable|max:500'
]);
$redirect = '/';
$id = decrypt($request->input('id'));
$action = $request->input('action');
$user = $request->user();
$ai = AccountInterstitial::whereUserId($user->id)
->whereType($request->input('type'))
->findOrFail($id);
if($action == 'appeal') {
$ai->appeal_requested_at = now();
$ai->appeal_message = $request->input('appeal_message');
}
$ai->read_at = now();
$ai->save();
$more = AccountInterstitial::whereUserId($user->id)
->whereNull('read_at')
->exists();
if(!$more) {
$user->has_interstitial = false;
$user->save();
}
if(in_array($ai->type, ['post.cw', 'post.unlist'])) {
$redirect = Status::findOrFail($ai->item_id)->url();
}
return redirect($redirect);
}
}

View file

@ -0,0 +1,53 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAccountInterstitialsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('account_interstitials', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('user_id')->nullable()->index();
$table->string('type')->nullable();
$table->string('view')->nullable();
$table->bigInteger('item_id')->unsigned()->nullable();
$table->string('item_type')->nullable();
$table->boolean('has_media')->default(false)->nullable();
$table->string('blurhash')->nullable();
$table->text('message')->nullable();
$table->text('violation_header')->nullable();
$table->text('violation_body')->nullable();
$table->json('meta')->nullable();
$table->text('appeal_message')->nullable();
$table->timestamp('appeal_requested_at')->nullable()->index();
$table->timestamp('appeal_handled_at')->nullable()->index();
$table->timestamp('read_at')->nullable()->index();
$table->timestamps();
});
Schema::table('users', function(Blueprint $table) {
$table->boolean('has_interstitial')->default(false)->index();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('account_interstitials');
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('has_interstitial');
});
}
}