This repository was archived by the owner on Aug 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnotifications.php
More file actions
238 lines (216 loc) · 5.81 KB
/
notifications.php
File metadata and controls
238 lines (216 loc) · 5.81 KB
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
<?php
/**
* @author: James Dryden <james.dryden@kentprojects.com>
* @license: Copyright KentProjects
* @link: http://kentprojects.com
*
* The idea behind this script is one queues a call to this script and passes it a JSON object of parameters.
*
* $ php notifications.php
* {type:"user_wants_to_access_a_year", actor_id:22, references:{year:2014}, targets:["convener"]}
* $ php notifications.php
* {type:"user_wants_to_join_a_group", actor_id: 4, references:{group_id: 6}, targets:["group/6", "user/2"]}
*
* This script will try to be clever and send a notification to as many people are possible, depending on the targets.
* #NahThatAin'tMe
*/
require_once __DIR__ . "/functions.php";
Timing::start("notifications");
$parameters = array();
try
{
if (empty($argv[1]))
{
throw new InvalidArgumentException("No parameters passed to the notification script.");
}
if ($argv[1] === "Hello")
{
Log::debug("Why hello, dear chap!");
exit();
}
$parameters = json_decode($argv[1], true);
if (empty($parameters))
{
throw new InvalidArgumentException("Invalid JSON passed to the notification script.");
}
if (empty($parameters["type"]))
{
throw new InvalidArgumentException("No type parameter passed to the notification script.");
}
elseif (empty($parameters["actor_id"]))
{
throw new InvalidArgumentException("No actor_id parameter passed to the notification script.");
}
elseif (empty($parameters["references"]))
{
$parameters["references"] = array();
}
elseif (empty($parameters["targets"]))
{
throw new InvalidArgumentException("No targets parameter passed to the notification script.");
}
$actor = Model_User::getById($parameters["actor_id"]);
if (empty($actor))
{
throw new InvalidArgumentException("Actor not found for notification script. Aborting.");
}
Notification::validate($parameters["type"], $actor, $parameters["references"], $parameters["targets"]);
$notification = new Model_Notification($parameters["type"], $actor);
if (!empty($parameters["references"]["group_id"]))
{
$group = Model_Group::getById($parameters["references"]["group_id"]);
if (empty($group))
{
throw new InvalidArgumentException("Group not found for notification script. Aborting.");
}
$notification->setGroup($group);
}
if (!empty($parameters["references"]["intent_id"]))
{
$intent = Model_Intent::getById($parameters["references"]["intent_id"]);
if (empty($intent))
{
throw new InvalidArgumentException("Intent not found for notification script. Aborting.");
}
$notification->setIntent($intent);
}
if (!empty($parameters["references"]["project_id"]))
{
$project = Model_Project::getById($parameters["references"]["project_id"]);
if (empty($project))
{
throw new InvalidArgumentException("Project not found for notification script. Aborting.");
}
$notification->setProject($project);
}
if (!empty($parameters["references"]["user_id"]))
{
$user = Model_User::getById($parameters["references"]["user_id"]);
if (empty($user))
{
throw new InvalidArgumentException("User not found for notification script. Aborting.");
}
$notification->setUser($user);
}
if (!empty($parameters["references"]["year"]))
{
$year = Model_Year::getById($parameters["references"]["year"]);
if (empty($year))
{
throw new InvalidArgumentException("Year not found for notification script. Aborting.");
}
$notification->setYear($year);
}
/** @var Model_User[] $targets */
$targets = array();
foreach ($parameters["targets"] as $target)
{
if ($target === "conveners")
{
$year = Model_Year::getCurrentYear();
foreach ($year->getConveners() as $convener)
{
$targets[] = $convener;
}
continue;
}
$target = explode("/", $target);
switch ($target[0])
{
case "group":
$group = Model_Group::getById($target[1]);
foreach ($group->getStudents() as $student)
{
/** @var Model_User $student */
$targets[] = $student;
}
break;
case "project":
$project = Model_Project::getById($target[1]);
$targets[] = $project->getSupervisor();
foreach ($project->getGroup()->getStudents() as $student)
{
/** @var Model_User $student */
$targets[] = $student;
}
break;
case "user":
$user = Model_User::getById($target[1]);
$targets[] = $user;
break;
default:
throw new InvalidArgumentException(
"Invalid target '{$target[0]}' passed to notification script switch. Aborting."
);
}
}
$uniqueIds = array();
foreach ($targets as $key => $user)
{
if (in_array($user->getId(), $uniqueIds))
{
unset($targets[$key]);
}
else
{
$uniqueIds[] = $user->getId();
}
}
unset($uniqueIds);
$notification->save();
Log::debug($notification, array_map(
function ($user)
{
/** @var Model_User $user */
return $user->getId();
},
$targets
));
$notificationUserMap = new NotificationUserMap($notification);
foreach ($targets as $user)
{
$notificationUserMap->add($user);
}
$notificationUserMap->clearCaches();
$notificationUserMap->save();
Timing::stop("notifications");
if (config("environment") === "development")
{
Log::debug($parameters, Timing::export());
}
Log::write();
exit();
}
catch (Exception $e)
{
$error = array(
"error" => true,
"exception" => get_class($e),
"message" => $e->getMessage()
);
$status = 500;
switch (get_class($e))
{
case "DatabaseException":
/** @var DatabaseException $e */
if (config("environment") === "development")
{
$error["query"] = $e->getQuery();
$error["types"] = $e->getTypes();
$error["params"] = $e->getParams();
}
break;
case "HttpStatusException":
/** @var HttpStatusException $e */
$error["status"] = $status = $e->getCode();
break;
}
/** @var Exception $e */
if (true)
{
$error["trace"] = explode("\n", $e->getTraceAsString());
}
Log::error($error, $parameters);
Log::write();
exit(1);
}