1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* T3Bot. |
4
|
|
|
* |
5
|
|
|
* @author Frank Nägler <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @link http://www.t3bot.de |
8
|
|
|
* @link http://wiki.typo3.org/T3Bot |
9
|
|
|
*/ |
10
|
|
|
namespace T3Bot\Commands; |
11
|
|
|
|
12
|
|
|
use Slack\Payload; |
13
|
|
|
use Slack\RealTimeClient; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class TellCommand. |
17
|
|
|
* |
18
|
|
|
* @property array helpCommands |
19
|
|
|
* @property string commandName |
20
|
|
|
*/ |
21
|
|
|
class TellCommand extends AbstractCommand |
22
|
|
|
{ |
23
|
|
|
const PRESENCE_ACTIVE = 'active'; |
24
|
|
|
const PRESENCE_AWAY = 'away'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* AbstractCommand constructor. |
28
|
|
|
* |
29
|
|
|
* @param Payload $payload |
30
|
|
|
* @param RealTimeClient $client |
31
|
|
|
*/ |
32
|
8 |
View Code Duplication |
public function __construct(Payload $payload, RealTimeClient $client) |
|
|
|
|
33
|
|
|
{ |
34
|
8 |
|
$this->commandName = 'tell'; |
35
|
8 |
|
$this->helpCommands = [ |
36
|
|
|
'help' => 'shows this help', |
37
|
|
|
'tell [@to-user] about review:[Gerrit-ID]' => 'tell the target user about the given review', |
38
|
|
|
'tell [@to-user] about forge:[Issue-ID]' => 'tell the target user about the given forge issue', |
39
|
|
|
'tell [@to-user] [your message]' => 'tell the target user your message', |
40
|
|
|
]; |
41
|
8 |
|
parent::__construct($payload, $client); |
42
|
8 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return bool|string |
46
|
|
|
* |
47
|
|
|
* @throws \Doctrine\DBAL\DBALException |
48
|
|
|
*/ |
49
|
6 |
|
public function process() |
50
|
|
|
{ |
51
|
6 |
|
$message = $this->payload->getData()['text']; |
52
|
6 |
|
$this->params = array_map('trim', explode(' ', $message)); |
53
|
6 |
|
$result = false; |
54
|
6 |
|
if ($this->params[0] === 'tell') { |
55
|
6 |
|
$result = $this->processTell(); |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
return $result; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $user |
63
|
|
|
* @param string $presence |
64
|
|
|
* |
65
|
|
|
* @throws \Doctrine\DBAL\DBALException |
66
|
|
|
*/ |
67
|
4 |
|
public function processPresenceChange($user, $presence) |
68
|
|
|
{ |
69
|
4 |
|
if ($presence === self::PRESENCE_ACTIVE) { |
70
|
3 |
|
$queryBuilder = $this->getDatabaseConnection()->createQueryBuilder(); |
71
|
|
|
$notifications = $queryBuilder |
72
|
3 |
|
->select('*') |
73
|
3 |
|
->from('notifications') |
74
|
3 |
|
->where($queryBuilder->expr()->eq('to_user', $queryBuilder->createNamedParameter($user))) |
75
|
3 |
|
->andWhere($queryBuilder->expr()->eq('delivered', $queryBuilder->createNamedParameter('0000-00-00 00:00:00'))) |
76
|
3 |
|
->execute() |
77
|
3 |
|
->fetchAll(); |
78
|
3 |
|
foreach ($notifications as $notification) { |
79
|
3 |
|
if (strpos($notification['message'], 'review:') === 0) { |
80
|
1 |
|
$this->processReviewMessage($notification, $user); |
81
|
2 |
|
} elseif (strpos($notification['message'], 'forge:') === 0) { |
82
|
1 |
|
$this->processForgeMessage($notification, $user); |
83
|
|
|
} else { |
84
|
1 |
|
$this->processTextMessage($notification, $user); |
85
|
|
|
} |
86
|
3 |
|
$now = new \DateTime(); |
87
|
3 |
|
$now->setTimestamp(time()); |
88
|
3 |
|
$this->getDatabaseConnection()->update( |
89
|
3 |
|
'notifications', |
90
|
3 |
|
['delivered' => $now], |
91
|
3 |
|
['id' => $notification['id']], |
92
|
3 |
|
['datetime'] |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
} |
96
|
4 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $notification |
100
|
|
|
* @param string $user |
101
|
|
|
*/ |
102
|
1 |
|
protected function processReviewMessage(array $notification, string $user) |
103
|
|
|
{ |
104
|
1 |
|
$parts = explode(':', $notification['message']); |
105
|
1 |
|
$refId = (int) trim($parts[1]); |
106
|
1 |
|
$result = $this->queryGerrit('change:' . $refId); |
107
|
1 |
|
$msg = '*Hi <@' . $user . '>, <@' . $notification['from_user'] . '>' |
108
|
1 |
|
. ' ask you to look at this patch:*'; |
109
|
|
|
|
110
|
1 |
|
if (is_array($result)) { |
111
|
1 |
|
foreach ($result as $item) { |
112
|
1 |
|
if ((int) $item->_number === $refId) { |
113
|
1 |
|
$message = $this->buildReviewMessage($item); |
114
|
1 |
|
$message->setText($msg); |
115
|
1 |
|
$this->sendResponse($message, $user); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
1 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param array $notification |
123
|
|
|
* @param string $user |
124
|
|
|
*/ |
125
|
1 |
|
protected function processForgeMessage(array $notification, string $user) |
126
|
|
|
{ |
127
|
1 |
|
$parts = explode(':', $notification['message']); |
128
|
1 |
|
$issueNumber = (int) trim($parts[1]); |
129
|
1 |
|
$result = $this->queryForge('issues/' . $issueNumber); |
130
|
1 |
|
if ($result) { |
131
|
1 |
|
$msg = '*Hi <@' . $user . '>, <@' . $notification['from_user'] . '>' |
132
|
1 |
|
. ' ask you to look at this issue:*'; |
133
|
1 |
|
$this->sendResponse($msg . "\n" . $this->buildIssueMessage($result->issue), $user); |
134
|
|
|
} |
135
|
1 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param array $notification |
139
|
|
|
* @param string $user |
140
|
|
|
*/ |
141
|
1 |
|
protected function processTextMessage(array $notification, string $user) |
142
|
|
|
{ |
143
|
1 |
|
$msg = '*Hi <@' . $user . '>, here is a message from <@' . $notification['from_user'] . '>' . ' for you:*'; |
144
|
1 |
|
$this->sendResponse($msg . chr(10) . $notification['message'], $user); |
145
|
1 |
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string |
149
|
|
|
* |
150
|
|
|
* @throws \Doctrine\DBAL\DBALException |
151
|
|
|
*/ |
152
|
6 |
|
protected function processTell() : string |
153
|
|
|
{ |
154
|
6 |
|
$params = $this->params; |
155
|
6 |
|
array_shift($params); |
156
|
6 |
|
$toUser = array_shift($params); |
157
|
6 |
|
$toUser = str_replace(['<', '>', '@'], '', $toUser); |
158
|
6 |
|
if ($params[0] === 'about' |
159
|
6 |
|
&& (strpos($params[1], 'review:') !== false || strpos($params[1], 'forge:') !== false)) { |
160
|
4 |
|
$message = $params[1]; |
161
|
|
|
} else { |
162
|
2 |
|
$message = implode(' ', $params); |
163
|
|
|
} |
164
|
6 |
|
$this->getDatabaseConnection()->insert('notifications', [ |
165
|
6 |
|
'from_user' => $this->payload->getData()['user'], |
166
|
6 |
|
'to_user' => $toUser, |
167
|
6 |
|
'message' => $message, |
168
|
|
|
]); |
169
|
|
|
|
170
|
6 |
|
return 'OK, I will tell <@' . $toUser . '> about your message'; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.