Completed
Push — irc-link-update ( 5c98e1 )
by Simon
17s queued 13s
created

IrcNotificationHelper::requestWelcomeFailed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Helpers;
10
11
use Exception;
12
use Waca\DataObjects\Ban;
13
use Waca\DataObjects\Comment;
14
use Waca\DataObjects\EmailTemplate;
15
use Waca\DataObjects\Notification;
16
use Waca\DataObjects\Request;
17
use Waca\DataObjects\User;
18
use Waca\DataObjects\WelcomeTemplate;
19
use Waca\ExceptionHandler;
20
use Waca\IrcColourCode;
21
use Waca\PdoDatabase;
22
use Waca\SiteConfiguration;
23
24
/**
25
 * Class IrcNotificationHelper
26
 * @package Waca\Helpers
27
 */
28
class IrcNotificationHelper
29
{
30
    /** @var PdoDatabase $notificationsDatabase */
31
    private $notificationsDatabase;
32
    /** @var PdoDatabase $primaryDatabase */
33
    private $primaryDatabase;
34
    /** @var bool $notificationsEnabled */
35
    private $notificationsEnabled;
36
    /** @var int $notificationType */
37
    private $notificationType;
38
    /** @var User $currentUser */
39
    private $currentUser;
40
    /** @var string $instanceName */
41
    private $instanceName;
42
    /** @var string */
43
    private $baseUrl;
44
    /** @var array */
45
    private $requestStates;
46
    /** @var SiteConfiguration */
47
    private $siteConfiguration;
48
49
    /**
50
     * IrcNotificationHelper constructor.
51
     *
52
     * @param SiteConfiguration $siteConfiguration
53
     * @param PdoDatabase       $primaryDatabase
54
     * @param PdoDatabase|null  $notificationsDatabase
55
     */
56
    public function __construct(
57
        SiteConfiguration $siteConfiguration,
58
        PdoDatabase $primaryDatabase,
59
        ?PdoDatabase $notificationsDatabase = null
60
    ) {
61
        $this->primaryDatabase = $primaryDatabase;
62
        $this->siteConfiguration = $siteConfiguration;
63
64
        if ($notificationsDatabase !== null) {
65
            $this->notificationsDatabase = $notificationsDatabase;
66
            $this->notificationsEnabled = $siteConfiguration->getIrcNotificationsEnabled();
67
        }
68
        else {
69
            $this->notificationsEnabled = false;
70
        }
71
72
        $this->notificationType = $siteConfiguration->getIrcNotificationType();
73
        $this->instanceName = $siteConfiguration->getIrcNotificationsInstance();
74
        $this->baseUrl = $siteConfiguration->getBaseUrl();
75
        $this->requestStates = $siteConfiguration->getRequestStates();
76
77
        $this->currentUser = User::getCurrent($primaryDatabase);
78
    }
79
80
    /**
81
     * Send a notification
82
     *
83
     * @param string $message The text to send
84
     */
85
    protected function send($message)
86
    {
87
        $instanceName = $this->instanceName;
88
89
        if (!$this->notificationsEnabled) {
90
            return;
91
        }
92
93
        $blacklist = array("DCC", "CCTP", "PRIVMSG");
94
        $message = str_replace($blacklist, "(IRC Blacklist)", $message); // Lets stop DCC etc
95
96
        $msg = IrcColourCode::RESET . IrcColourCode::BOLD . "[$instanceName]" . IrcColourCode::RESET . ": $message";
97
98
        try {
99
            $notification = new Notification();
100
            $notification->setDatabase($this->notificationsDatabase);
101
            $notification->setType($this->notificationType);
102
            $notification->setText($msg);
103
104
            $notification->save();
105
        }
106
        catch (Exception $ex) {
107
            // OK, so we failed to send the notification - that db might be down?
108
            // This is non-critical, so silently fail.
109
            ExceptionHandler::logExceptionToDisk($ex, $this->siteConfiguration);
110
111
            // Disable notifications for remainder of request.
112
            $this->notificationsEnabled = false;
113
        }
114
    }
115
116
    #region user management
117
118
    /**
119
     * send a new user notification
120
     *
121
     * @param User $user
122
     */
123
    public function userNew(User $user)
124
    {
125
        $this->send("New user: {$user->getUsername()}");
126
    }
127
128
    /**
129
     * send an approved notification
130
     *
131
     * @param User $user
132
     */
133
    public function userApproved(User $user)
134
    {
135
        $this->send("{$user->getUsername()} approved by " . $this->currentUser->getUsername());
136
    }
137
138
    /**
139
     * send a declined notification
140
     *
141
     * @param User   $user
142
     * @param string $reason the reason the user was declined
143
     */
144
    public function userDeclined(User $user, $reason)
145
    {
146
        $this->send("{$user->getUsername()} declined by " . $this->currentUser->getUsername() . " ($reason)");
147
    }
148
149
    /**
150
     * send a suspended notification
151
     *
152
     * @param User   $user
153
     * @param string $reason The reason the user has been suspended
154
     */
155
    public function userSuspended(User $user, $reason)
156
    {
157
        $this->send("{$user->getUsername()} suspended by " . $this->currentUser->getUsername() . " ($reason)");
158
    }
159
160
    /**
161
     * Send a preference change notification
162
     *
163
     * @param User $user
164
     */
165
    public function userPrefChange(User $user)
166
    {
167
        $this->send("{$user->getUsername()}'s preferences were changed by " . $this->currentUser->getUsername());
168
    }
169
170
    /**
171
     * Send a user renamed notification
172
     *
173
     * @param User   $user
174
     * @param string $old
175
     */
176
    public function userRenamed(User $user, $old)
177
    {
178
        $this->send($this->currentUser->getUsername() . " renamed $old to {$user->getUsername()}");
179
    }
180
181
    /**
182
     * @param User   $user
183
     * @param string $reason
184
     */
185
    public function userRolesEdited(User $user, $reason)
186
    {
187
        $currentUser = $this->currentUser->getUsername();
188
        $this->send("Active roles for {$user->getUsername()} changed by " . $currentUser . " ($reason)");
189
    }
190
191
    #endregion
192
193
    #region Site Notice
194
195
    /**
196
     * Summary of siteNoticeEdited
197
     */
198
    public function siteNoticeEdited()
199
    {
200
        $this->send("Site notice edited by " . $this->currentUser->getUsername());
201
    }
202
    #endregion
203
204
    #region Welcome Templates
205
    /**
206
     * Summary of welcomeTemplateCreated
207
     *
208
     * @param WelcomeTemplate $template
209
     */
210
    public function welcomeTemplateCreated(WelcomeTemplate $template)
211
    {
212
        $this->send("Welcome template {$template->getId()} created by " . $this->currentUser->getUsername());
213
    }
214
215
    /**
216
     * Summary of welcomeTemplateDeleted
217
     *
218
     * @param int $templateid
219
     */
220
    public function welcomeTemplateDeleted($templateid)
221
    {
222
        $this->send("Welcome template {$templateid} deleted by " . $this->currentUser->getUsername());
223
    }
224
225
    /**
226
     * Summary of welcomeTemplateEdited
227
     *
228
     * @param WelcomeTemplate $template
229
     */
230
    public function welcomeTemplateEdited(WelcomeTemplate $template)
231
    {
232
        $this->send("Welcome template {$template->getId()} edited by " . $this->currentUser->getUsername());
233
    }
234
235
    #endregion
236
237
    #region bans
238
    /**
239
     * Summary of banned
240
     *
241
     * @param Ban $ban
242
     */
243
    public function banned(Ban $ban)
244
    {
245
        if ($ban->getDuration() === null) {
246
            $duration = "indefinitely";
247
        }
248
        else {
249
            $duration = "until " . date("F j, Y, g:i a", $ban->getDuration());
250
        }
251
252
        $username = $this->currentUser->getUsername();
253
254
        if ($ban->getVisibility() == 'user') {
255
            $this->send("Ban {$ban->getId()} set by {$username} for '{$ban->getReason()}' {$duration}");
256
        }
257
        else {
258
            $this->send("Ban {$ban->getId()} set by {$username} {$duration}");
259
        }
260
    }
261
262
    /**
263
     * Summary of unbanned
264
     *
265
     * @param Ban    $ban
266
     * @param string $unbanReason
267
     */
268
    public function unbanned(Ban $ban, $unbanReason)
269
    {
270
        $this->send("Ban {$ban->getId()} unbanned by " . $this->currentUser
271
                ->getUsername() . " (" . $unbanReason . ")");
272
    }
273
274
    #endregion
275
276
    #region request management
277
278
    /**
279
     * Summary of requestReceived
280
     *
281
     * @param Request $request
282
     */
283
    public function requestReceived(Request $request)
284
    {
285
        $this->send(
286
            IrcColourCode::DARK_GREY . "[["
287
            . IrcColourCode::DARK_GREEN . "acc:"
288
            . IrcColourCode::ORANGE . $request->getId()
289
            . IrcColourCode::DARK_GREY . "]]"
290
            . IrcColourCode::RED . " N "
291
            . IrcColourCode::DARK_BLUE . $this->baseUrl . "/internal.php/viewRequest?id={$request->getId()} "
292
            . IrcColourCode::DARK_RED . "* "
293
            . IrcColourCode::DARK_GREEN . $request->getName()
294
            . IrcColourCode::DARK_RED . " * "
295
            . IrcColourCode::RESET
296
        );
297
    }
298
299
    /**
300
     * Summary of requestDeferred
301
     *
302
     * @param Request $request
303
     */
304
    public function requestDeferred(Request $request)
305
    {
306
        $availableRequestStates = $this->requestStates;
307
308
        $deferTo = $availableRequestStates[$request->getStatus()]['deferto'];
309
        $username = $this->currentUser->getUsername();
310
311
        $this->send("Request {$request->getId()} ({$request->getName()}) deferred to {$deferTo} by {$username}");
312
    }
313
314
    /**
315
     *
316
     * Summary of requestDeferredWithMail
317
     *
318
     * @param Request $request
319
     */
320
    public function requestDeferredWithMail(Request $request)
321
    {
322
        $availableRequestStates = $this->requestStates;
323
324
        $deferTo = $availableRequestStates[$request->getStatus()]['deferto'];
325
        $username = $this->currentUser->getUsername();
326
        $id = $request->getId();
327
        $name = $request->getName();
328
329
        $this->send("Request {$id} ({$name}) deferred to {$deferTo} with an email by {$username}");
330
    }
331
332
    /**
333
     * Summary of requestClosed
334
     *
335
     * @param Request $request
336
     * @param string  $closetype
337
     */
338
    public function requestClosed(Request $request, $closetype)
339
    {
340
        $username = $this->currentUser->getUsername();
341
342
        $this->send("Request {$request->getId()} ({$request->getName()}) closed ($closetype) by {$username}");
343
    }
344
345
    /**
346
     * Summary of requestClosed
347
     *
348
     * @param Request $request
349
     * @param string  $closetype
350
     */
351
    public function requestCloseQueued(Request $request, $closetype)
352
    {
353
        $username = $this->currentUser->getUsername();
354
355
        $this->send("Request {$request->getId()} ({$request->getName()}) queued for creation ($closetype) by {$username}");
356
    }
357
358
    /**
359
     * Summary of requestClosed
360
     *
361
     * @param Request $request
362
     * @param User    $triggerUser
363
     */
364
    public function requestCreationFailed(Request $request, User $triggerUser)
365
    {
366
        $this->send("Request {$request->getId()} ({$request->getName()}) failed auto-creation for {$triggerUser->getUsername()}, and was sent to the hospital queue.");
367
    }
368
369
    /**
370
     * @param Request $request
371
     * @param User    $triggerUser
372
     */
373
    public function requestWelcomeFailed(Request $request, User $triggerUser)
374
    {
375
        $this->send("Request {$request->getId()} ({$request->getName()}) failed welcome for {$triggerUser->getUsername()}.");
376
    }
377
378
    /**
379
     * Summary of sentMail
380
     *
381
     * @param Request $request
382
     */
383
    public function sentMail(Request $request)
384
    {
385
        $this->send($this->currentUser->getUsername()
386
            . " sent an email related to Request {$request->getId()} ({$request->getName()})");
387
    }
388
389
    #endregion
390
391
    #region reservations
392
393
    /**
394
     * Summary of requestReserved
395
     *
396
     * @param Request $request
397
     */
398
    public function requestReserved(Request $request)
399
    {
400
        $username = $this->currentUser->getUsername();
401
402
        $this->send("Request {$request->getId()} ({$request->getName()}) reserved by {$username}");
403
    }
404
405
    /**
406
     * Summary of requestReserveBroken
407
     *
408
     * @param Request $request
409
     */
410
    public function requestReserveBroken(Request $request)
411
    {
412
        $username = $this->currentUser->getUsername();
413
414
        $this->send("Reservation on request {$request->getId()} ({$request->getName()}) broken by {$username}");
415
    }
416
417
    /**
418
     * Summary of requestUnreserved
419
     *
420
     * @param Request $request
421
     */
422
    public function requestUnreserved(Request $request)
423
    {
424
        $this->send("Request {$request->getId()} ({$request->getName()}) is no longer being handled.");
425
    }
426
427
    /**
428
     * Summary of requestReservationSent
429
     *
430
     * @param Request $request
431
     * @param User    $target
432
     */
433
    public function requestReservationSent(Request $request, User $target)
434
    {
435
        $username = $this->currentUser->getUsername();
436
437
        $this->send(
438
            "Reservation of request {$request->getId()} ({$request->getName()}) sent to {$target->getUsername()} by "
439
            . $username);
440
    }
441
442
    #endregion
443
444
    #region comments
445
446
    /**
447
     * Summary of commentCreated
448
     *
449
     * @param Comment $comment
450
     * @param Request $request
451
     */
452
    public function commentCreated(Comment $comment, Request $request)
453
    {
454
        $username = $this->currentUser->getUsername();
455
        $visibility = ($comment->getVisibility() == "admin" ? "private " : "");
456
457
        $this->send("{$username} posted a {$visibility}comment on request {$request->getId()} ({$request->getName()})");
458
    }
459
460
    /**
461
     * Summary of commentEdited
462
     *
463
     * @param Comment $comment
464
     * @param Request $request
465
     */
466
    public function commentEdited(Comment $comment, Request $request)
467
    {
468
        $username = $this->currentUser->getUsername();
469
470
        $this->send(<<<TAG
471
Comment {$comment->getId()} on request {$request->getId()} ({$request->getName()}) edited by {$username}
472
TAG
473
        );
474
    }
475
476
    #endregion
477
478
    #region email management (close reasons)
479
480
    /**
481
     * Summary of emailCreated
482
     *
483
     * @param EmailTemplate $template
484
     */
485
    public function emailCreated(EmailTemplate $template)
486
    {
487
        $username = $this->currentUser->getUsername();
488
        $this->send("Email {$template->getId()} ({$template->getName()}) created by " . $username);
489
    }
490
491
    /**
492
     * Summary of emailEdited
493
     *
494
     * @param EmailTemplate $template
495
     */
496
    public function emailEdited(EmailTemplate $template)
497
    {
498
        $username = $this->currentUser->getUsername();
499
        $this->send("Email {$template->getId()} ({$template->getName()}) edited by " . $username);
500
    }
501
    #endregion
502
}
503