|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Http\Controllers; |
|
6
|
|
|
|
|
7
|
|
|
use App\Core as MinepicCore; |
|
8
|
|
|
use App\Helpers\Date as DateHelper; |
|
9
|
|
|
use App\Models\Account; |
|
10
|
|
|
use App\Models\AccountStats; |
|
11
|
|
|
use App\Repositories\AccountRepository; |
|
12
|
|
|
use App\Repositories\AccountStatsRepository; |
|
13
|
|
|
use Illuminate\Http\JsonResponse; |
|
14
|
|
|
use Laravel\Lumen\Http\ResponseFactory; |
|
15
|
|
|
use Laravel\Lumen\Routing\Controller as BaseController; |
|
16
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
17
|
|
|
|
|
18
|
|
|
class JsonController extends BaseController |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ResponseFactory |
|
22
|
|
|
*/ |
|
23
|
|
|
private $responseFactory; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var MinepicCore |
|
26
|
|
|
*/ |
|
27
|
|
|
private $minepicCore; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var AccountRepository |
|
30
|
|
|
*/ |
|
31
|
|
|
private $accountRepository; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var AccountStatsRepository |
|
34
|
|
|
*/ |
|
35
|
|
|
private $accountStatsRepository; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct( |
|
38
|
|
|
AccountRepository $accountRepository, |
|
39
|
|
|
AccountStatsRepository $accountStatsRepository, |
|
40
|
|
|
MinepicCore $minepicCore, |
|
41
|
|
|
ResponseFactory $responseFactory |
|
42
|
|
|
) { |
|
43
|
|
|
$this->minepicCore = $minepicCore; |
|
44
|
|
|
$this->responseFactory = $responseFactory; |
|
45
|
|
|
$this->accountRepository = $accountRepository; |
|
46
|
|
|
$this->accountStatsRepository = $accountStatsRepository; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* User info. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $uuidOrName |
|
53
|
|
|
* @return JsonResponse |
|
54
|
|
|
*/ |
|
55
|
|
|
public function user($uuidOrName = ''): JsonResponse |
|
56
|
|
|
{ |
|
57
|
|
|
if (!$this->minepicCore->initialize($uuidOrName)) { |
|
58
|
|
|
$httpStatus = 404; |
|
59
|
|
|
$response = [ |
|
60
|
|
|
'ok' => false, |
|
61
|
|
|
'message' => 'User not found', |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
return $this->responseFactory->json($response, $httpStatus); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$httpStatus = 200; |
|
68
|
|
|
$account = $this->minepicCore->getUserdata(); |
|
69
|
|
|
|
|
70
|
|
|
if ($account === null) { |
|
71
|
|
|
throw new NotFoundHttpException(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$accountStats = $this->accountStatsRepository->findByUuid($account->uuid); |
|
75
|
|
|
|
|
76
|
|
|
$response = [ |
|
77
|
|
|
'ok' => true, |
|
78
|
|
|
'userdata' => [ |
|
79
|
|
|
'uuid' => $account->uuid, |
|
80
|
|
|
'username' => $account->username, |
|
81
|
|
|
'count_request' => $accountStats->count_request, |
|
82
|
|
|
'count_search' => $accountStats->count_search, |
|
83
|
|
|
'last_request' => DateHelper::humanizeTimestamp($accountStats->time_request), |
|
84
|
|
|
'last_search' => DateHelper::humanizeTimestamp($accountStats->time_search), |
|
85
|
|
|
], |
|
86
|
|
|
]; |
|
87
|
|
|
|
|
88
|
|
|
return $this->responseFactory->json($response, $httpStatus); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Update User data. |
|
93
|
|
|
* @param string $uuidOrName |
|
94
|
|
|
* @return JsonResponse |
|
95
|
|
|
*/ |
|
96
|
|
|
public function updateUser(string $uuidOrName): JsonResponse |
|
97
|
|
|
{ |
|
98
|
|
|
// Force user update |
|
99
|
|
|
$this->minepicCore->setForceUpdate(true); |
|
100
|
|
|
|
|
101
|
|
|
// Check if user exists |
|
102
|
|
|
if ($this->minepicCore->initialize($uuidOrName)) { |
|
103
|
|
|
// Check if data has been updated |
|
104
|
|
|
if ($this->minepicCore->userDataUpdated()) { |
|
105
|
|
|
$response = ['ok' => true, 'message' => 'Data updated']; |
|
106
|
|
|
$httpStatus = 200; |
|
107
|
|
|
} else { |
|
108
|
|
|
$userdata = $this->minepicCore->getUserdata(); |
|
109
|
|
|
$dateString = $userdata->updated_at->toW3cString(); |
|
110
|
|
|
|
|
111
|
|
|
$response = [ |
|
112
|
|
|
'ok' => false, |
|
113
|
|
|
'message' => 'Cannot update user, userdata has been updated recently', |
|
114
|
|
|
'last_update' => $dateString, |
|
115
|
|
|
]; |
|
116
|
|
|
|
|
117
|
|
|
$httpStatus = 403; |
|
118
|
|
|
} |
|
119
|
|
|
} else { |
|
120
|
|
|
$response = ['ok' => false, 'message' => 'User not found']; |
|
121
|
|
|
$httpStatus = 404; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $this->responseFactory->json($response, $httpStatus); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Username Typeahead. |
|
129
|
|
|
* |
|
130
|
|
|
* @param $term |
|
131
|
|
|
* @return JsonResponse |
|
132
|
|
|
*/ |
|
133
|
|
|
public function userTypeahead($term): JsonResponse |
|
134
|
|
|
{ |
|
135
|
|
|
$response = []; |
|
136
|
|
|
$accounts = Account::query() |
|
137
|
|
|
->select(['username']) |
|
138
|
|
|
->where('username', 'LIKE', $term.'%') |
|
139
|
|
|
->take(15) |
|
140
|
|
|
->get(); |
|
141
|
|
|
foreach ($accounts as $account) { |
|
142
|
|
|
$response[]['value'] = $account->username; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $this->responseFactory->json($response); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Get most wanted account list. |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getMostWantedUsers(): JsonResponse |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->responseFactory->json([ |
|
154
|
|
|
'ok' => true, |
|
155
|
|
|
'data' => AccountStats::getMostWanted(), |
|
156
|
|
|
]); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|