This repository has been archived by the owner on Dec 5, 2017. It is now read-only.
/
CustomListController.php
306 lines (230 loc) · 8.51 KB
/
CustomListController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
namespace VacStatus\Http\Controllers\APIv1\Lists;
use VacStatus\Http\Controllers\Controller;
use VacStatus\Models\Profile;
use VacStatus\Models\Subscription;
use VacStatus\Models\UserList;
use VacStatus\Models\UserListProfile;
use VacStatus\Update\CustomList;
use VacStatus\Update\MultiProfile;
use VacStatus\Steam\Steam;
use VacStatus\Steam\SteamUser;
use Auth;
use DB;
use Input;
use Validator;
class CustomListController extends Controller
{
private $messages = [
'required' => 'The :attribute field is required.',
'numeric' => 'The :attribute field is required.',
'max' => 'List Name is limited to :max characters.',
];
private $listValidationParam = [
'title' => 'required|max:30',
'privacy' => 'required|numeric'
];
private $addUserValidationParam = [
'list_id' => 'required|numeric',
'profile_id' => 'required|numeric'
];
private $errorMessages = [
'list_no_permission_edit' => 'You do not have permission to edit this list',
'list_reached_max' => 'You have reached the limit of list you can create!',
'list_save_error' => 'There was an error while trying to save the list.',
'list_delete_error' => 'There was an error trying to delete the list',
'sub_reached_max' => 'You have reached the maximum subsciption limit!',
'sub_already_subbed' => 'You are already subscribed to the list.',
'user_reached_max' => 'You have reached the maximum users in a list.',
'user_already_exists' => 'This user already exists on the list!',
'user_save_error' => 'There was an error trying to add user to list.',
'user_delete_error' => 'There was an error trying to remove user from list.',
'search_invalid' => 'Invalid search option.',
];
public function get(UserList $userList)
{
if(!isset($userList->id)) {
return ['error' => '404'];
}
$customList = new CustomList($userList);
if($customList->error()) return $customList->error();
return $customList->getCustomList();
}
public function modify($listId = null)
{
$validator = Validator::make(Input::all(), $this->listValidationParam, $this->messages);
if ($validator->fails()) return ['error' => $validator->errors()->all()[0]];
$user = Auth::user();
if(!is_null($listId))
{
$userList = UserList::where('id', $listId)->first();
if($userList->user_id !== $user->id) return $this->error('list_no_permission_edit');
}
elseif(!$user->canMakeList()) return $this->error('list_reached_max');
else $userList = new UserList;
$userList->title = Input::get('title');
$userList->privacy = Input::get('privacy');
if(!is_null($listId) && !$userList->save() || !$user->UserList()->save($userList))
{
return $this->error('list_save_error');
}
return $this->getMyListsDetailed($user->id);
}
public function delete(UserList $userList)
{
if($userList->user_id !== Auth::user()->id) return $this->error('forbidden');
$userList->UserListProfile()->delete();
if(!$userList->delete()) return $this->error('list_delete_error');
return [true];
}
public function subscribe(UserList $userList)
{
if(!isset($userList->id)) return $this->error('404');
$user = Auth::user();
if($user->unlockSubscription() <= $user->Subscription()->count())
{
return $this->error('sub_reached_max');
}
$subscription = Subscription::whereUserId($user->id)
->whereUserListId($userList->id)
->first();
if(isset($subscription->id)) return $this->error('sub_already_subbed');
$subscription = new Subscription;
$subscription->user_id = $user->id;
$subscription->user_list_id = $userList->id;
$subscription->save();
return $this->get($userList);
}
public function unsubscribe(UserList $userList)
{
if(!isset($userList->id)) return $this->error('404');
$user = Auth::user();
$subscription = Subscription::whereUserListId($userList->id)
->whereUserId($user->id)->delete();
return $this->get($userList);
}
public function addProfileToList()
{
$input = Input::all();
$validator = Validator::make($input, $this->addUserValidationParam, $this->messages);
if ($validator->fails()) return $this->error($validator->errors()->all()[0]);
$userListProfile = UserListProfile::where('user_list_id', (int) $input['list_id'])->get();
if(Auth::user()->unlockUser() <= $userListProfile->count())
{
return $this->error('user_reached_max');
}
$userListProfile = $userListProfile
->where('profile_id', (int) $input['profile_id'])
->where('deleted_at', null)
->first();
if(isset($userListProfile->id)) return $this->error('user_already_exists');
$profile = Profile::where('id', (int) $input['profile_id'])->first();
$userListProfile = new UserListProfile;
$userListProfile->user_list_id = $input['list_id'];
$userListProfile->profile_id = $input['profile_id'];
$userListProfile->profile_name = $profile->display_name;
$userListProfile->profile_description = $input['description'];
if(!$userListProfile->save()) return $this->error('user_save_error');
$userList = UserList::where('id', $input['list_id'])->first();
if($userList == null || !isset($userList->id)) return $this->error('invalid_list_error');
return $this->get($userList);
}
public function deleteProfileFromList()
{
$input = Input::all();
$userListProfile = UserListProfile::where('user_list_id', $input['list_id'])
->where('profile_id', $input['profile_id'])
->first();
if(!isset($userListProfile->id) || !$userListProfile->delete())
{
return $this->error('user_delete_error');
}
return $this->get(UserList::where('id', $input['list_id'])->first());
}
public function addManyProfilesToList()
{
$input = Input::all();
$smallIds = $this->findValidProfiles(Steam::parseSearch($input['search']));
if(isset($smallIds['error'])) return $smallIds;
$this->updateValidProfiles($smallIds);
$listId = (int) $input['list_id'];
$userList = UserList::where('id', $listId)->first();
$userListProfiles = UserListProfile::whereIn('profile.small_id', $smallIds)
->where('user_list_profile.user_list_id', $listId)
->whereNull('user_list_profile.deleted_at')
->leftjoin('profile', 'profile.id', '=', 'user_list_profile.profile_id')
->get(['profile.small_id']);
$smallIds = array_diff($smallIds, $userListProfiles->lists('small_id')->toArray());
$profiles = Profile::whereIn('small_id', $smallIds)->get();
$toAddtoList = [];
$totalCurrentProfiles = $userList->UserListProfile()->count();
$description = isset($input['description']) && !empty($input['description']) ? $input['description'] : null;
foreach($profiles as $k => $profile)
{
if(Auth::user()->unlockUser() <= $totalCurrentProfiles + $k) break;
$toAddtoList[] = new UserListProfile([
"profile_id" => $profile->id,
"profile_name" => $profile->display_name,
"profile_description" => $description
]);
}
$userList->UserListProfile()->saveMany($toAddtoList);
return $this->get($userList);
}
private function error($message)
{
$errorMessage = isset($this->errorMessages[$message]) ? $this->errorMessages[$message] : null;
if($errorMessage) return ['error' => $errorMessage];
return ['error' => $message];
}
private function getMyListsDetailed($userId)
{
$return = [];
$myLists = UserList::where('user_list.user_id', $userId)
->groupBy('user_list.id')
->orderBy('user_list.id', 'desc')
->leftJoin('user_list_profile', function($join)
{
$join->on('user_list_profile.user_list_id', '=', 'user_list.id')
->whereNull('user_list_profile.deleted_at');
})
->leftJoin('subscription', function($join)
{
$join->on('subscription.user_list_id', '=', 'user_list.id')
->whereNull('subscription.deleted_at');
})
->get([
'user_list.id',
'user_list.title',
'user_list.privacy',
'user_list.created_at',
DB::raw('count(DISTINCT user_list_profile.profile_id) as users_in_list'),
DB::raw('count(DISTINCT subscription.id) as sub_count'),
]);
return $myLists;
}
private function findValidProfiles($search)
{
if(!is_array($search)) return $this->error('search_invalid');
$validProfile = (new SteamUser($search))->fetch();
if(!is_array($validProfile)) return $this->error($validProfile);
return Steam::toSmallId($validProfile);
}
private function updateValidProfiles($smallIds)
{
$profiles = Profile::whereIn('small_id', $smallIds)
->get(['small_id']);
$profilesParsed = [];
foreach($smallIds as $smallId)
{
$profile = $profiles->where('small_id', $smallId)->first();
if(is_null($profile))
{
$profilesParsed[] = ['small_id' => $smallId];
continue;
}
}
$multiProfile = new MultiProfile($profilesParsed);
$multiProfile->run();
}
}