@@ -5,6 +5,8 @@ namespace App\Http\Controllers; | |||
use App\Knockout\Post; | |||
use Illuminate\Http\Request; | |||
use App\Knockout\User; | |||
use App\Knockout\UserPost; | |||
use App\Knockout\UserThread; | |||
class UserController extends Controller { | |||
@@ -38,7 +40,7 @@ class UserController extends Controller { | |||
$page = $request->route('page', 1); | |||
$user = User::one($userId); | |||
$posts = Post::all($userId, $page); | |||
$posts = UserPost::all($userId, $page); | |||
return view('page/user-posts', [ | |||
'user' => $user->getRecord(), | |||
@@ -47,9 +49,26 @@ class UserController extends Controller { | |||
]); | |||
} | |||
public function threads() | |||
public function threads(Request $request) | |||
{ | |||
return 'not implemented'; | |||
$userId = $request->route('user', null); | |||
$page = $request->route('page', 1); | |||
$user = User::one($userId); | |||
$threads = UserThread::all($userId, $page); | |||
// nasty bodge to cover up api inconsistency | |||
$userRecord = $user->getRecord(); | |||
$threadRecords = array_map(function($thread) use($userRecord) { | |||
$thread->user = $userRecord; | |||
return $thread; | |||
}, $threads->getRecords()); | |||
return view('page/user-threads', [ | |||
'user' => $userRecord, | |||
'threads' => $threadRecords, | |||
'paginator' => $threads->getPaginator($request) | |||
]); | |||
} | |||
public function bans() | |||
@@ -0,0 +1,38 @@ | |||
<?php | |||
namespace App\Knockout; | |||
use Illuminate\Support\Facades\Cache; | |||
class UserPost { | |||
private static function requestAll(int $userId, int $page = 1): Dataset | |||
{ | |||
$data = (new AbstractData)->httpGet(sprintf('/user/%u/posts/%u', $userId, $page)); | |||
$json = json_decode($data); | |||
$records = array_map(function($user) { | |||
return Post::unwrap($user); | |||
}, $json->posts); | |||
return (new Dataset($records)) | |||
->setTotalRecords($json->totalPosts) | |||
->setCurrentPage($page) | |||
->setRecordsPerPage(40); | |||
} | |||
public static function updateAll(int $userId, int $page = 0): Dataset | |||
{ | |||
$key = sprintf('user-posts-%u-%u', $userId, $page); | |||
$data = self::requestAll($userId, $page); | |||
Cache::put($key, $data, 300); | |||
return $data; | |||
} | |||
public static function all(int $userId, int $page = 0): Dataset | |||
{ | |||
$key = sprintf('user-posts-%u-%u', $userId, $page); | |||
return Cache::get($key, function() use($userId, $page) { | |||
return self::updateAll($userId, $page); | |||
}); | |||
} | |||
} |
@@ -0,0 +1,38 @@ | |||
<?php | |||
namespace App\Knockout; | |||
use Illuminate\Support\Facades\Cache; | |||
class UserThread { | |||
private static function requestAll(int $userId, int $page = 1): Dataset | |||
{ | |||
$data = (new AbstractData)->httpGet(sprintf('/user/%u/threads/%u', $userId, $page)); | |||
$json = json_decode($data); | |||
$records = array_map(function($user) { | |||
return Thread::unwrap($user); | |||
}, $json->threads); | |||
return (new Dataset($records)) | |||
->setTotalRecords($json->totalThreads) | |||
->setCurrentPage($page) | |||
->setRecordsPerPage(40); | |||
} | |||
public static function updateAll(int $userId, int $page = 0): Dataset | |||
{ | |||
$key = sprintf('user-threads-%u-%u', $userId, $page); | |||
$data = self::requestAll($userId, $page); | |||
Cache::put($key, $data, 300); | |||
return $data; | |||
} | |||
public static function all(int $userId, int $page = 0): Dataset | |||
{ | |||
$key = sprintf('user-threads-%u-%u', $userId, $page); | |||
return Cache::get($key, function() use($userId, $page) { | |||
return self::updateAll($userId, $page); | |||
}); | |||
} | |||
} |
@@ -0,0 +1,25 @@ | |||
@extends('default') | |||
@section('title', $user->username . ' Threads') | |||
@section('breadcrumb') | |||
<a href="{{ route('index') }}">Home</a> | |||
<a>></a> | |||
<a href="{{ route('users') }}">Users</a> | |||
<a>></a> | |||
<a href="{{ route('user', ['user' => $user->id]) }}">{{ $user->username }}</a> | |||
<a>></a> | |||
<a href="{{ route('user.threads', ['user' => $user->id]) }}">Threads</a> | |||
@endsection | |||
@section('content') | |||
{!! $paginator !!} | |||
@foreach($threads as $key => $thread) | |||
@include('partial/thread') | |||
@endforeach | |||
{!! $paginator !!} | |||
@endsection | |||
@section('scripts') | |||
<link rel='stylesheet' type='text/css' href='/css/page/subforum.css'/> | |||
@endsection |