2018-04-29 16:25:57 +00:00
< ? php
namespace App\Http\Controllers ;
2019-02-05 20:56:10 +00:00
use App\ {
2019-06-19 01:15:12 +00:00
Contact ,
2019-02-05 20:56:10 +00:00
FailedJob ,
Hashtag ,
Instance ,
Media ,
Like ,
2019-12-24 06:12:23 +00:00
Newsroom ,
2019-02-05 20:56:10 +00:00
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 ;
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 ,
2019-06-09 06:19:44 +00:00
AdminSettingsController ,
AdminSupportController
2019-02-05 20:56:10 +00:00
};
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-10-08 01:48:58 +00:00
$day = config ( 'database.default' ) == 'pgsql' ? 'DATE_PART(\'day\',' : 'day(' ;
$recent = Cache :: remember ( 'admin:dashboard:home:data:15min' , now () -> addMinutes ( 15 ), function () use ( $day ) {
2019-02-05 20:56:10 +00:00
return [
2019-06-19 01:15:12 +00:00
'contact' => [
'count' => PrettyNumber :: convert ( Contact :: whereNull ( 'read_at' ) -> count ()),
2019-10-08 01:48:58 +00:00
'graph' => Contact :: selectRaw ( 'count(*) as count, ' . $day . 'created_at) as d' ) -> groupBy ( 'd' ) -> whereNull ( 'read_at' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 14 ), now ()]) -> orderBy ( 'd' ) -> pluck ( 'count' )
2019-06-19 01:15:12 +00:00
],
2019-02-05 20:56:10 +00:00
'failedjobs' => [
'count' => PrettyNumber :: convert ( FailedJob :: where ( 'failed_at' , '>=' , \Carbon\Carbon :: now () -> subDay ()) -> count ()),
2019-10-08 01:48:58 +00:00
'graph' => FailedJob :: selectRaw ( 'count(*) as count, ' . $day . 'failed_at) as d' ) -> groupBy ( 'd' ) -> whereBetween ( 'failed_at' ,[ now () -> subDays ( 14 ), now ()]) -> orderBy ( 'd' ) -> pluck ( 'count' )
2019-02-05 20:56:10 +00:00
],
'reports' => [
'count' => PrettyNumber :: convert ( Report :: whereNull ( 'admin_seen' ) -> count ()),
2019-10-08 01:48:58 +00:00
'graph' => Report :: selectRaw ( 'count(*) as count, ' . $day . 'created_at) as d' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 14 ), now ()]) -> groupBy ( 'd' ) -> orderBy ( 'd' ) -> pluck ( 'count' )
2019-02-05 20:56:10 +00:00
],
'statuses' => [
'count' => PrettyNumber :: convert ( Status :: whereNull ( 'in_reply_to_id' ) -> whereNull ( 'reblog_of_id' ) -> count ()),
2019-02-13 02:06:03 +00:00
'graph' => Status :: selectRaw ( 'count(*) as count, ' . $day . 'created_at) as day' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 14 ), now ()]) -> groupBy ( 'day' ) -> orderBy ( 'day' ) -> pluck ( 'count' )
2019-02-05 20:56:10 +00:00
],
'replies' => [
'count' => PrettyNumber :: convert ( Status :: whereNotNull ( 'in_reply_to_id' ) -> count ()),
2019-02-13 02:06:03 +00:00
'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' )
2019-02-05 20:56:10 +00:00
],
'shares' => [
'count' => PrettyNumber :: convert ( Status :: whereNotNull ( 'reblog_of_id' ) -> count ()),
2019-02-13 02:06:03 +00:00
'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' )
2019-02-05 20:56:10 +00:00
],
'likes' => [
'count' => PrettyNumber :: convert ( Like :: count ()),
2019-02-13 02:06:03 +00:00
'graph' => Like :: selectRaw ( 'count(*) as count, ' . $day . 'created_at) as day' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 14 ), now ()]) -> groupBy ( 'day' ) -> orderBy ( 'day' ) -> pluck ( 'count' )
2019-02-05 20:56:10 +00:00
],
'profiles' => [
'count' => PrettyNumber :: convert ( Profile :: count ()),
2019-02-13 02:06:03 +00:00
'graph' => Profile :: selectRaw ( 'count(*) as count, ' . $day . 'created_at) as day' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 14 ), now ()]) -> groupBy ( 'day' ) -> orderBy ( 'day' ) -> pluck ( 'count' )
2019-02-05 20:56:10 +00:00
],
2019-10-08 01:48:58 +00:00
];
});
$longer = Cache :: remember ( 'admin:dashboard:home:data:24hr' , now () -> addHours ( 24 ), function () use ( $day ) {
return [
2019-02-05 20:56:10 +00:00
'users' => [
'count' => PrettyNumber :: convert ( User :: count ()),
2019-02-13 02:06:03 +00:00
'graph' => User :: selectRaw ( 'count(*) as count, ' . $day . 'created_at) as day' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 14 ), now ()]) -> groupBy ( 'day' ) -> orderBy ( 'day' ) -> pluck ( 'count' )
2019-02-05 20:56:10 +00:00
],
'instances' => [
'count' => PrettyNumber :: convert ( Instance :: count ()),
2019-02-13 02:06:03 +00:00
'graph' => Instance :: selectRaw ( 'count(*) as count, ' . $day . 'created_at) as day' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 28 ), now ()]) -> groupBy ( 'day' ) -> orderBy ( 'day' ) -> pluck ( 'count' )
2019-02-05 20:56:10 +00:00
],
'media' => [
'count' => PrettyNumber :: convert ( Media :: count ()),
2019-02-13 02:06:03 +00:00
'graph' => Media :: selectRaw ( 'count(*) as count, ' . $day . 'created_at) as day' ) -> whereBetween ( 'created_at' ,[ now () -> subDays ( 14 ), now ()]) -> groupBy ( 'day' ) -> orderBy ( 'day' ) -> pluck ( 'count' )
2019-02-05 20:56:10 +00:00
],
'storage' => [
'count' => Media :: sum ( 'size' ),
2019-02-13 02:06:03 +00:00
'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' )
2019-02-05 20:56:10 +00:00
]
];
});
2019-10-08 01:48:58 +00:00
$data = array_merge ( $recent , $longer );
2019-02-05 20:56:10 +00:00
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' ;
2019-07-18 05:00:34 +00:00
$users = User :: select ( 'id' , 'username' , 'status' ) -> withCount ( 'statuses' ) -> orderBy ( $col , $dir ) -> simplePaginate ( 10 );
2019-02-11 01:00:05 +00:00
2019-07-18 05:00:34 +00:00
return view ( 'admin.users.home' , compact ( 'users' ));
2018-09-03 01:25:08 +00:00
}
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 )
{
2019-06-09 06:19:44 +00:00
$statuses = Status :: orderBy ( 'id' , 'desc' ) -> simplePaginate ( 10 );
2018-08-28 03:07:36 +00:00
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
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 ) {
2019-06-09 06:19:44 +00:00
$profiles = Profile :: select ( 'id' , 'username' ) -> where ( 'username' , 'like' , " % $search % " ) -> orderBy ( 'id' , 'desc' ) -> simplePaginate ( $limit );
2019-02-12 08:11:02 +00:00
} else if ( $filter && $order ) {
2019-06-09 06:19:44 +00:00
$profiles = Profile :: select ( 'id' , 'username' ) -> withCount ([ 'likes' , 'statuses' , 'followers' ]) -> orderBy ( $filter , $order ) -> simplePaginate ( $limit );
2019-02-12 08:11:02 +00:00
} else {
2019-06-09 06:19:44 +00:00
$profiles = Profile :: select ( 'id' , 'username' ) -> orderBy ( 'id' , 'desc' ) -> simplePaginate ( $limit );
2019-02-12 08:11:02 +00:00
}
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' ));
}
2019-06-19 01:15:12 +00:00
public function messagesHome ( Request $request )
{
$messages = Contact :: orderByDesc ( 'id' ) -> paginate ( 10 );
return view ( 'admin.messages.home' , compact ( 'messages' ));
}
2019-06-19 01:50:08 +00:00
public function messagesShow ( Request $request , $id )
{
$message = Contact :: findOrFail ( $id );
return view ( 'admin.messages.show' , compact ( 'message' ));
}
public function messagesMarkRead ( Request $request )
{
$this -> validate ( $request , [
'id' => 'required|integer|min:1'
]);
$id = $request -> input ( 'id' );
$message = Contact :: findOrFail ( $id );
if ( $message -> read_at ) {
return ;
}
$message -> read_at = now ();
$message -> save ();
return ;
}
2019-12-24 06:12:23 +00:00
public function newsroomHome ( Request $request )
{
$newsroom = Newsroom :: latest () -> paginate ( 10 );
return view ( 'admin.newsroom.home' , compact ( 'newsroom' ));
}
public function newsroomCreate ( Request $request )
{
return view ( 'admin.newsroom.create' );
}
public function newsroomEdit ( Request $request , $id )
{
$news = Newsroom :: findOrFail ( $id );
return view ( 'admin.newsroom.edit' , compact ( 'news' ));
}
public function newsroomDelete ( Request $request , $id )
{
$news = Newsroom :: findOrFail ( $id );
$news -> delete ();
return redirect ( '/i/admin/newsroom' );
}
public function newsroomUpdate ( Request $request , $id )
{
$this -> validate ( $request , [
'title' => 'required|string|min:1|max:100' ,
'summary' => 'nullable|string|max:200' ,
'body' => 'nullable|string'
]);
$changed = false ;
$changedFields = [];
$news = Newsroom :: findOrFail ( $id );
$fields = [
'title' => 'string' ,
'summary' => 'string' ,
'body' => 'string' ,
'category' => 'string' ,
'show_timeline' => 'boolean' ,
'auth_only' => 'boolean' ,
'show_link' => 'boolean' ,
'force_modal' => 'boolean' ,
'published' => 'published'
];
foreach ( $fields as $field => $type ) {
switch ( $type ) {
case 'string' :
if ( $request -> { $field } != $news -> { $field }) {
if ( $field == 'title' ) {
$news -> slug = str_slug ( $request -> { $field });
}
$news -> { $field } = $request -> { $field };
$changed = true ;
array_push ( $changedFields , $field );
}
break ;
case 'boolean' :
$state = $request -> { $field } == 'on' ? true : false ;
if ( $state != $news -> { $field }) {
$news -> { $field } = $state ;
$changed = true ;
array_push ( $changedFields , $field );
}
break ;
case 'published' :
$state = $request -> { $field } == 'on' ? true : false ;
$published = $news -> published_at != null ;
if ( $state != $published ) {
$news -> published_at = $state ? now () : null ;
$changed = true ;
array_push ( $changedFields , $field );
}
break ;
}
}
if ( $changed ) {
$news -> save ();
}
$redirect = $news -> published_at ? $news -> permalink () : $news -> editUrl ();
return redirect ( $redirect );
}
public function newsroomStore ( Request $request )
{
$this -> validate ( $request , [
'title' => 'required|string|min:1|max:100' ,
'summary' => 'nullable|string|max:200' ,
'body' => 'nullable|string'
]);
$changed = false ;
$changedFields = [];
$news = new Newsroom ();
$fields = [
'title' => 'string' ,
'summary' => 'string' ,
'body' => 'string' ,
'category' => 'string' ,
'show_timeline' => 'boolean' ,
'auth_only' => 'boolean' ,
'show_link' => 'boolean' ,
'force_modal' => 'boolean' ,
'published' => 'published'
];
foreach ( $fields as $field => $type ) {
switch ( $type ) {
case 'string' :
if ( $request -> { $field } != $news -> { $field }) {
if ( $field == 'title' ) {
$news -> slug = str_slug ( $request -> { $field });
}
$news -> { $field } = $request -> { $field };
$changed = true ;
array_push ( $changedFields , $field );
}
break ;
case 'boolean' :
$state = $request -> { $field } == 'on' ? true : false ;
if ( $state != $news -> { $field }) {
$news -> { $field } = $state ;
$changed = true ;
array_push ( $changedFields , $field );
}
break ;
case 'published' :
$state = $request -> { $field } == 'on' ? true : false ;
$published = $news -> published_at != null ;
if ( $state != $published ) {
$news -> published_at = $state ? now () : null ;
$changed = true ;
array_push ( $changedFields , $field );
}
break ;
}
}
if ( $changed ) {
$news -> save ();
}
$redirect = $news -> published_at ? $news -> permalink () : $news -> editUrl ();
return redirect ( $redirect );
}
2018-04-29 16:25:57 +00:00
}