2018-04-29 16:25:57 +00:00
< ? php
namespace App\Http\Controllers ;
2019-02-05 20:56:10 +00:00
use App\ {
FailedJob ,
Hashtag ,
Instance ,
Media ,
Like ,
OauthClient ,
Profile ,
Report ,
Status ,
User
};
use DB , Cache ;
2018-09-03 01:25:08 +00:00
use Carbon\Carbon ;
2018-04-29 16:25:57 +00:00
use Illuminate\Http\Request ;
2018-12-21 06:14:22 +00:00
use Jackiedo\DotenvEditor\DotenvEditor ;
2019-02-05 20:56:10 +00:00
use App\Http\Controllers\Admin\ {
2019-02-07 19:00:01 +00:00
AdminDiscoverController ,
2019-02-05 20:56:10 +00:00
AdminInstanceController ,
AdminReportController ,
AdminMediaController ,
AdminSettingsController
};
2018-12-21 06:14:22 +00:00
use App\Util\Lexer\PrettyNumber ;
2019-02-12 08:11:02 +00:00
use Illuminate\Validation\Rule ;
2018-04-29 16:25:57 +00:00
class AdminController extends Controller
{
2019-02-05 20:56:10 +00:00
use AdminReportController ,
2019-02-07 19:00:01 +00:00
AdminDiscoverController ,
2019-02-05 20:56:10 +00:00
AdminMediaController ,
AdminSettingsController ,
AdminInstanceController ;
2018-09-01 02:26:56 +00:00
2018-05-26 22:45:05 +00:00
public function __construct ()
{
2018-09-16 05:15:45 +00:00
$this -> middleware ( 'admin' );
$this -> middleware ( 'twofactor' );
2018-05-26 22:45:05 +00:00
}
public function home ()
{
2019-02-11 01:00:05 +00:00
$data = Cache :: remember ( 'admin:dashboard:home:data' , 15 , function () {
2019-02-05 20:56:10 +00:00
return [
'failedjobs' => [
'count' => PrettyNumber :: convert ( FailedJob :: where ( 'failed_at' , '>=' , \Carbon\Carbon :: now () -> subDay ()) -> count ()),
'graph' => FailedJob :: selectRaw ( 'count(*) as count, day(failed_at) as d' ) -> groupBy ( 'd' ) -> whereBetween ( 'failed_at' ,[ now () -> subDays ( 24 ), now ()]) -> orderBy ( 'd' ) -> pluck ( 'count' )
],
'reports' => [
'count' => PrettyNumber :: convert ( Report :: whereNull ( 'admin_seen' ) -> count ()),
'graph' => Report :: selectRaw ( 'count(*) as count, day(created_at) as day' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 14 ), now ()]) -> groupBy ( 'day' ) -> orderBy ( 'day' ) -> pluck ( 'count' )
],
'statuses' => [
'count' => PrettyNumber :: convert ( Status :: whereNull ( 'in_reply_to_id' ) -> whereNull ( 'reblog_of_id' ) -> count ()),
'graph' => Status :: selectRaw ( 'count(*) as count, day(created_at) as day' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 14 ), now ()]) -> groupBy ( 'day' ) -> orderBy ( 'day' ) -> pluck ( 'count' )
],
'replies' => [
'count' => PrettyNumber :: convert ( Status :: whereNotNull ( 'in_reply_to_id' ) -> count ()),
'graph' => Status :: whereNotNull ( 'in_reply_to_id' ) -> selectRaw ( 'count(*) as count, day(created_at) as day' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 14 ), now ()]) -> groupBy ( 'day' ) -> orderBy ( 'day' ) -> pluck ( 'count' )
],
'shares' => [
'count' => PrettyNumber :: convert ( Status :: whereNotNull ( 'reblog_of_id' ) -> count ()),
'graph' => Status :: whereNotNull ( 'reblog_of_id' ) -> selectRaw ( 'count(*) as count, day(created_at) as day' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 14 ), now ()]) -> groupBy ( 'day' ) -> orderBy ( 'day' ) -> pluck ( 'count' )
],
'likes' => [
'count' => PrettyNumber :: convert ( Like :: count ()),
'graph' => Like :: selectRaw ( 'count(*) as count, day(created_at) as day' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 14 ), now ()]) -> groupBy ( 'day' ) -> orderBy ( 'day' ) -> pluck ( 'count' )
],
'profiles' => [
'count' => PrettyNumber :: convert ( Profile :: count ()),
'graph' => Profile :: selectRaw ( 'count(*) as count, day(created_at) as day' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 14 ), now ()]) -> groupBy ( 'day' ) -> orderBy ( 'day' ) -> pluck ( 'count' )
],
'users' => [
'count' => PrettyNumber :: convert ( User :: count ()),
'graph' => User :: selectRaw ( 'count(*) as count, day(created_at) as day' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 14 ), now ()]) -> groupBy ( 'day' ) -> orderBy ( 'day' ) -> pluck ( 'count' )
],
'instances' => [
'count' => PrettyNumber :: convert ( Instance :: count ()),
'graph' => Instance :: selectRaw ( 'count(*) as count, day(created_at) as day' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 28 ), now ()]) -> groupBy ( 'day' ) -> orderBy ( 'day' ) -> pluck ( 'count' )
],
'media' => [
'count' => PrettyNumber :: convert ( Media :: count ()),
'graph' => Media :: selectRaw ( 'count(*) as count, day(created_at) as day' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 14 ), now ()]) -> groupBy ( 'day' ) -> orderBy ( 'day' ) -> pluck ( 'count' )
],
'storage' => [
'count' => Media :: sum ( 'size' ),
'graph' => Media :: selectRaw ( 'sum(size) as count, day(created_at) as day' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 14 ), now ()]) -> groupBy ( 'day' ) -> orderBy ( 'day' ) -> pluck ( 'count' )
]
];
});
return view ( 'admin.home' , compact ( 'data' ));
2018-05-26 22:45:05 +00:00
}
public function users ( Request $request )
{
2018-12-21 06:14:22 +00:00
$col = $request -> query ( 'col' ) ? ? 'id' ;
$dir = $request -> query ( 'dir' ) ? ? 'desc' ;
$stats = $this -> collectUserStats ( $request );
$users = User :: withCount ( 'statuses' ) -> orderBy ( $col , $dir ) -> paginate ( 10 );
2019-02-11 01:00:05 +00:00
2018-09-03 01:25:08 +00:00
return view ( 'admin.users.home' , compact ( 'users' , 'stats' ));
}
public function editUser ( Request $request , $id )
{
2018-12-21 06:14:22 +00:00
$user = User :: findOrFail ( $id );
2018-09-03 01:25:08 +00:00
$profile = $user -> profile ;
return view ( 'admin.users.edit' , compact ( 'user' , 'profile' ));
2018-08-28 03:07:36 +00:00
}
2018-05-26 22:45:05 +00:00
public function statuses ( Request $request )
{
2018-08-28 03:07:36 +00:00
$statuses = Status :: orderBy ( 'id' , 'desc' ) -> paginate ( 10 );
return view ( 'admin.statuses.home' , compact ( 'statuses' ));
2018-05-26 22:45:05 +00:00
}
public function showStatus ( Request $request , $id )
{
2018-08-28 03:07:36 +00:00
$status = Status :: findOrFail ( $id );
return view ( 'admin.statuses.show' , compact ( 'status' ));
2018-05-26 22:45:05 +00:00
}
2018-09-01 02:26:56 +00:00
public function reports ( Request $request )
{
2019-02-05 20:56:10 +00:00
$filter = $request -> input ( 'filter' );
if ( in_array ( $filter , [ 'open' , 'closed' ])) {
if ( $filter == 'open' ) {
$reports = Report :: orderBy ( 'created_at' , 'desc' )
-> whereNotNull ( 'admin_seen' )
-> paginate ( 10 );
} else {
$reports = Report :: orderBy ( 'created_at' , 'desc' )
-> whereNull ( 'admin_seen' )
-> paginate ( 10 );
}
} else {
$reports = Report :: orderBy ( 'created_at' , 'desc' )
-> paginate ( 10 );
}
2018-09-01 02:26:56 +00:00
return view ( 'admin.reports.home' , compact ( 'reports' ));
}
public function showReport ( Request $request , $id )
{
$report = Report :: findOrFail ( $id );
return view ( 'admin.reports.show' , compact ( 'report' ));
}
2018-09-03 01:25:08 +00:00
protected function collectUserStats ( $request )
2018-09-11 03:06:36 +00:00
{
2018-09-03 01:25:08 +00:00
$total_duration = $request -> query ( 'total_duration' ) ? ? '30' ;
$new_duration = $request -> query ( 'new_duration' ) ? ? '7' ;
$stats = [];
$stats [ 'total' ] = [
'count' => User :: where ( 'created_at' , '>' , Carbon :: now () -> subDays ( $total_duration )) -> count (),
2018-09-03 01:57:27 +00:00
'points' => 0 //User::selectRaw('day(created_at) day, count(*) as count')->where('created_at','>', Carbon::now()->subDays($total_duration))->groupBy('day')->pluck('count')
2018-09-03 01:25:08 +00:00
];
$stats [ 'new' ] = [
'count' => User :: where ( 'created_at' , '>' , Carbon :: now () -> subDays ( $new_duration )) -> count (),
2018-09-03 01:57:27 +00:00
'points' => 0 //User::selectRaw('day(created_at) day, count(*) as count')->where('created_at','>', Carbon::now()->subDays($new_duration))->groupBy('day')->pluck('count')
2018-09-03 01:25:08 +00:00
];
$stats [ 'active' ] = [
'count' => Status :: groupBy ( 'profile_id' ) -> count ()
];
$stats [ 'profile' ] = [
'local' => Profile :: whereNull ( 'remote_url' ) -> count (),
'remote' => Profile :: whereNotNull ( 'remote_url' ) -> count ()
];
$stats [ 'avg' ] = [
2018-12-21 06:14:22 +00:00
'likes' => floor ( Like :: average ( 'profile_id' )),
2018-09-03 01:25:08 +00:00
'posts' => floor ( Status :: avg ( 'profile_id' ))
];
return $stats ;
}
2019-02-05 20:56:10 +00:00
public function profiles ( Request $request )
{
2019-02-12 08:11:02 +00:00
$this -> validate ( $request , [
'search' => 'nullable|string|max:250' ,
'filter' => [
'nullable' ,
'string' ,
Rule :: in ([ 'id' , 'username' , 'statuses_count' , 'followers_count' , 'likes_count' ])
],
'order' => [
'nullable' ,
'string' ,
Rule :: in ([ 'asc' , 'desc' ])
],
'layout' => [
'nullable' ,
'string' ,
Rule :: in ([ 'card' , 'list' ])
],
'limit' => 'nullable|integer|min:1|max:50'
]);
$search = $request -> input ( 'search' );
$filter = $request -> input ( 'filter' );
$order = $request -> input ( 'order' ) ? ? 'desc' ;
$limit = $request -> input ( 'limit' ) ? ? 12 ;
if ( $search ) {
$profiles = Profile :: where ( 'username' , 'like' , " % $search % " ) -> orderBy ( 'id' , 'desc' ) -> paginate ( $limit );
} else if ( $filter && $order ) {
$profiles = Profile :: withCount ([ 'likes' , 'statuses' , 'followers' ]) -> orderBy ( $filter , $order ) -> paginate ( $limit );
} else {
$profiles = Profile :: orderBy ( 'id' , 'desc' ) -> paginate ( $limit );
}
2019-02-05 20:56:10 +00:00
return view ( 'admin.profiles.home' , compact ( 'profiles' ));
}
2019-02-12 08:11:02 +00:00
public function profileShow ( Request $request , $id )
{
$profile = Profile :: findOrFail ( $id );
$user = $profile -> user ;
return view ( 'admin.profiles.edit' , compact ( 'profile' , 'user' ));
}
2019-02-05 20:56:10 +00:00
public function appsHome ( Request $request )
{
$filter = $request -> input ( 'filter' );
if ( in_array ( $filter , [ 'revoked' ])) {
$apps = OauthClient :: with ( 'user' )
-> whereNotNull ( 'user_id' )
-> whereRevoked ( true )
-> orderByDesc ( 'id' )
-> paginate ( 10 );
} else {
$apps = OauthClient :: with ( 'user' )
-> whereNotNull ( 'user_id' )
-> orderByDesc ( 'id' )
-> paginate ( 10 );
}
return view ( 'admin.apps.home' , compact ( 'apps' ));
}
public function hashtagsHome ( Request $request )
{
$hashtags = Hashtag :: orderByDesc ( 'id' ) -> paginate ( 10 );
return view ( 'admin.hashtags.home' , compact ( 'hashtags' ));
}
2018-04-29 16:25:57 +00:00
}