This repository has been archived by the owner on Jan 25, 2020. It is now read-only.
/
GeneralController.php
167 lines (148 loc) Β· 4.2 KB
/
GeneralController.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
<?php
namespace App\Http\Controllers;
use App;
use Log;
use JWTAuth;
use Request;
use App\Models\User;
use App\Models\Event;
use App\Models\School;
use App\Models\UserStat;
use App\Models\GithubEvent;
use App\Models\Announcement;
use App\Models\InterestSignup;
class GeneralController extends Controller
{
/**
* GET /ping.
*
* Heartbeat endpoint.
* @return array
*/
public function ping()
{
return response()->success(['pong']);
}
/**
* GET /.
*
* @return mixed
*/
public function info()
{
return response()->success([
'name'=>'BoilerMake API',
'frontend'=>env('FRONTEND_ADDRESS'),
'info'=>'http://github.com/BoilerMake',
'docs'=>env('APP_URL').'/docs',
]);
}
/**
* POST /stats.
*
* Logs a user stat event.
* @return \Response
*/
public function createUserStat()
{
try {
$user_id = JWTAuth::parseToken()->toUser()->id;
} catch (\Exception $e) {
$user_id = null;
}
$eventName = Request::get('event');
$subtitle = Request::get('subtitle');
$client = Request::get('client');
$uuid = Request::header('x-uuid');
$stat = UserStat::create([
'user_id' => $user_id,
'event' => $eventName,
'subtitle' => $subtitle,
'context' => Request::get('context'),
'uuid' => $uuid,
'client_ip' => Request::ip(),
'client_useragent' => Request::header('user-agent'),
'client_referer' => Request::header('referer'),
]);
$shouldLog = (App::environment() == 'production') || env('SHOW_EXTRA_LOGS_DEV');
if ($shouldLog) {
Log::debug('UserStatRecorded', [
'user_id' => $user_id,
'event' => $eventName,
'subtitle' => $subtitle,
'uuid' => $uuid,
'client' => $client,
'id' => $stat->id,
]);
}
return response()->success($stat);
}
/**
* GET /schools.
* @param Request $request
* @return mixed
*/
public function getSchools()
{
$filter = Request::get('filter');
if (! $filter) {
$filter = '';
}
$locs = School::where('name', 'like', '%'.$filter.'%')->orWhere('name', 'Other/School not listed')->get();
return response()->success($locs);
}
/**
* Submits an email for interest signup
* POST interest/signup.
*
* @return mixed
*/
public function interestSignup()
{
$email = Request::get('email');
if (! filter_var($email, FILTER_VALIDATE_EMAIL)) {
Log::debug('InterestSignup Fail');
return response()->error('email is not valid!');
}
$signup = InterestSignup::firstOrCreate(['email' => $email]);
if ($signup->wasRecentlyCreated) {
Log::debug('InterestSignup Success');
return response()->success('all signed up!');
}
return response()->error('you were already signed up!');
}
/*
* Gets schedule for day-of site
* GET /events
*/
public function getEvents()
{
return response()->success(Event::where('hidden', 0)->orderBy('begin')->get(['id', 'title', 'description', 'begin', 'end']));
}
/*
* Gets announcements for the day-of site
* GET /announcements
*/
public function getAnnouncements()
{
return response()->success(Announcement::orderBy('created_at', 'DESC')->get());
}
/*
* GET /activity
* @codeCoverageIgnore
*/
public function getActivity()
{
$github = [];
foreach (GithubEvent::where('type', 'PushEvent')->with('user')->orderBy('timestamp', 'DESC')->get() as $push) {
$github[] = [
'id' => $push->id,
'message' => $push->user->name.' pushed to '.$push->repo,
'timestamp' => $push->timestamp,
];
}
return [
'github'=>$github,
];
}
}