diff --git a/lib/classes/task/period/apiwebhooks.php b/lib/classes/task/period/apiwebhooks.php new file mode 100644 index 0000000000..c9ac7d6665 --- /dev/null +++ b/lib/classes/task/period/apiwebhooks.php @@ -0,0 +1,140 @@ +get_connection()->prepare('SELECT id, `type`, `data` FROM api_webhooks'); + $stmt->execute(); + $rs = $stmt->fetchAll(\PDO::FETCH_ASSOC); + $stmt->closeCursor(); + + return $rs; + } + + protected function processOneContent(appbox $appbox, array $row) + { + $data = null; + switch ($row['type']) { + case \API_Webhook::NEW_FEED_ENTRY: + $data = $this->processNewFeedEntry($row); + } + + if (null === $data) { + return; + } + $urls = $this->getApplicationHookUrls($appbox); + $this->sendData($urls, $data); + } + + protected function postProcessOneContent(appbox $appbox, array $row) + { + $w = new API_Webhook($appbox, $row['id']); + $w->delete(); + } + + protected function getApplicationHookUrls(appbox $appbox) + { + $stmt = $appbox->get_connection()->prepare('SELECT webhook_url FROM api_applications WHERE webhook_url IS NOT NULL'); + $stmt->execute(); + $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); + $stmt->closeCursor(); + + return array_map(function($row) { + return $row['webhook_url']; + }, $rows); + } + + protected function sendData(array $urls, array $data) + { + if (count($urls) === 0) { + return; + } + $client = new GuzzleClient(); + $body = json_encode($data); + $requests = []; + foreach ($urls as $url) { + $requests[] = $client->createRequest('POST', $url, [ + 'Content-Type' => 'application/json' + ], $body); + } + $client->send($requests); + } + + protected function processNewFeedEntry(array $row) + { + $data = json_decode($row['data']); + if (!isset($data->{"feed_id"}) || !isset($data->{"entry_id"})) { + return; + } + $feed = new Feed_Adapter($this->dependencyContainer, $data->{"feed_id"}); + $entry = new \Feed_Entry_Adapter($this->dependencyContainer, $feed, $data->{"entry_id"}); + + $Query = new \User_Query($this->dependencyContainer); + + $Query->include_phantoms(true) + ->include_invite(false) + ->include_templates(false) + ->email_not_null(true); + + if ($entry->get_feed()->get_collection()) { + $Query->on_base_ids(array($entry->get_feed()->get_collection()->get_base_id())); + } + + $start = 0; + $perLoop = 100; + $users = array(); + + do { + $results = $Query->limit($start, $perLoop)->execute()->get_results(); + foreach ($results as $user) { + $users[] = array( + 'mail' => $user->get_email(), + 'firstname' => $user->get_firstname(), + 'lastname' => $user->get_lastname(), + ); + } + $start += $perLoop; + } while (count($results) > 0); + + return array( + 'name' => $row['type'], + 'data' => array( + 'feed' => array( + 'title' => $feed->get_title(), + 'description' => $feed->get_subtitle(), + ), + 'entry' => array( + 'author' => array( + 'name' => $entry->get_author_name(), + 'email' => $entry->get_author_email() + ), + 'title' => $entry->get_title(), + 'description' => $entry->get_subtitle(), + ), + 'users' => $users + ) + ); + } +}