| 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 |  |  |      * @param array|null $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 33 | 8 | View Code Duplication |     public function __construct(Payload $payload, RealTimeClient $client, array $configuration = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 8 |  |         $this->commandName = 'tell'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 8 |  |         $this->helpCommands = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             'help' => 'shows this help', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             'tell [@to-user] about review:[Gerrit-ID]' => 'tell the target user about the given review', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             'tell [@to-user] about forge:[Issue-ID]' => 'tell the target user about the given forge issue', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             'tell [@to-user] [your message]' => 'tell the target user your message', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 8 |  |         parent::__construct($payload, $client, $configuration); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 8 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @return bool|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @throws \Doctrine\DBAL\DBALException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 6 |  |     public function process() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 6 |  |         $message = $this->payload->getData()['text']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 6 |  |         $this->params = array_map('trim', explode(' ', $message)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 6 |  |         $result = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 6 |  |         if ($this->params[0] === 'tell') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 6 |  |             $result = $this->processTell(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 6 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param string $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param string $presence | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @throws \Doctrine\DBAL\DBALException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 4 |  |     public function processPresenceChange($user, $presence) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 4 |  |         if ($presence === self::PRESENCE_ACTIVE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 3 |  |             $notifications = $this->getNotificationsForUser($user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 3 |  |             foreach ($notifications as $notification) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 3 |  |                 if (strpos($notification['message'], 'review:') === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |                     $this->processReviewMessage($notification, $user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 2 |  |                 } elseif (strpos($notification['message'], 'forge:') === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |                     $this->processForgeMessage($notification, $user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |                     $this->processTextMessage($notification, $user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 3 |  |                 $this->markNotificationAsDelivered($notification['id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param string $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @throws \Doctrine\DBAL\DBALException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 3 |  |     protected function getNotificationsForUser(string $user) : array | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 3 |  |         $queryBuilder = $this->getDatabaseConnection()->createQueryBuilder(); | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         return $queryBuilder | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 3 |  |             ->select('*') | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 3 |  |             ->from('notifications') | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 3 |  |             ->where($queryBuilder->expr()->eq('to_user', $queryBuilder->createNamedParameter($user))) | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 3 |  |             ->andWhere($queryBuilder->expr()->eq('delivered', $queryBuilder->createNamedParameter('0000-00-00 00:00:00'))) | 
            
                                                                        
                            
            
                                    
            
            
                | 99 | 3 |  |             ->execute() | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 3 |  |             ->fetchAll(); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param int $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @throws \Doctrine\DBAL\DBALException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 3 |  |     protected function markNotificationAsDelivered(int $id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 3 |  |         $now = new \DateTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 3 |  |         $now->setTimestamp(time()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 3 |  |         $this->getDatabaseConnection()->update( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 3 |  |             'notifications', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 3 |  |             ['delivered' => $now], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 3 |  |             ['id' => $id], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 3 |  |             ['datetime'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @param array $notification | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @param string $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  |     protected function processReviewMessage(array $notification, string $user) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 1 |  |         $parts = explode(':', $notification['message']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  |         $refId = (int) trim($parts[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  |         $result = $this->queryGerrit('change:' . $refId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 1 |  |         $msg = '*Hi <@' . $user . '>, <@' . $notification['from_user'] . '>' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  |             . ' ask you to look at this patch:*'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 1 |  |         if (is_array($result)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |             foreach ($result as $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |                 if ((int) $item->_number === $refId) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 1 |  |                     $message = $this->buildReviewMessage($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 1 |  |                     $message->setText($msg); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 1 |  |                     $this->sendResponse($message, $user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @param array $notification | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      * @param string $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 1 |  |     protected function processForgeMessage(array $notification, string $user) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 1 |  |         $parts = explode(':', $notification['message']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 1 |  |         $issueNumber = (int) trim($parts[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 1 |  |         $result = $this->queryForge('issues/' . $issueNumber); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 1 |  |         if ($result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 1 |  |             $msg = '*Hi <@' . $user . '>, <@' . $notification['from_user'] . '>' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 1 |  |                 . ' ask you to look at this issue:*'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 1 |  |             $this->sendResponse($msg . chr(10) . $this->buildIssueMessage($result->issue), $user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |      * @param array $notification | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |      * @param string $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 1 |  |     protected function processTextMessage(array $notification, string $user) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 1 |  |         $msg = '*Hi <@' . $user . '>, here is a message from <@' . $notification['from_user'] . '>' . ' for you:*'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 1 |  |         $this->sendResponse($msg . chr(10) . $notification['message'], $user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * @throws \Doctrine\DBAL\DBALException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 6 |  |     protected function processTell() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 6 |  |         $params = $this->params; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 6 |  |         array_shift($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 | 6 |  |         $toUser = array_shift($params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 6 |  |         $toUser = str_replace(['<', '>', '@'], '', $toUser); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 | 6 |  |         if ($params[0] === 'about' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 6 |  |             && (strpos($params[1], 'review:') !== false || strpos($params[1], 'forge:') !== false)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 4 |  |             $message = $params[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 | 2 |  |             $message = implode(' ', $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 6 |  |         $this->getDatabaseConnection()->insert('notifications', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 | 6 |  |             'from_user' => $this->payload->getData()['user'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 6 |  |             'to_user' => $toUser, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 | 6 |  |             'message' => $message, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 6 |  |         return 'OK, I will tell <@' . $toUser . '> about your message'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 194 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 195 |  |  |  |