public function execute(InputInterface $input, OutputInterface $output) { $this->config = new \InboxSync\Config(); $gh = $this->getClient(); $client = \InboxSync\Helper::createGoogleOauthClient($this->config); $gmail = new \Google_Service_Gmail($client); $label_id = \InboxSync\Helper::findLabel($gmail, 'Notifications/Github'); if (empty($label_id)) { return; } $github_unread = []; $github_notifications_data = []; $github_notifications_read = []; $github_notifications_unread = []; foreach ($gh->currentUser()->notifications()->all() as $notifications) { $id = substr($notifications['subject']['url'], strlen('https://api.github.com/repos/')); $github_unread[$id] = TRUE; $github_notifications_data[$id] = $notifications; } foreach ($gmail->users_threads->listUsersThreads('me', ['labelIds' => ['UNREAD', $label_id]]) as $thread) { $thread = $gmail->users_threads->get('me', $thread['id'], ['format' => 'metadata']); $issue_ref = NULL; $id = NULL; /** * @var \Google_Service_Gmail_MessagePartHeader $header */ foreach ($thread->getMessages() as $message) { foreach ($message->getPayload()->getHeaders() as $header) { if ($header->getName() == 'References' || $header->getName() == 'Message-ID') { $issue_ref = $header->getValue(); break 2; } } } $match = []; preg_match('/^<(.*)@github\\.com/', $issue_ref, $match); if (!empty($match)) { $id = explode('/', $match[1]); $id = implode('/', array_slice($id, 0, 4)); } if (isset($id)) { // No unread GitHub notification, mark mail as read. if (!isset($github_unread[$id])) { $body = new \Google_Service_Gmail_ModifyThreadRequest(); $body->setRemoveLabelIds(['UNREAD', 'INBOX']); $gmail->users_threads->modify('me', $thread->getId(), $body); $github_notifications_read[$id] = TRUE; } else { $github_notifications_unread[$id] = TRUE; } } print '.' . PHP_EOL; } $diff = array_diff_key($github_unread, $github_notifications_unread); foreach (array_keys($diff) as $id) { $data = $github_notifications_data[$id]; $gh->currentUser()->notifications()->markAsRead($data['id'], []); } }
public function execute(InputInterface $input, OutputInterface $output) { $config = new \InboxSync\Config(); $client = \InboxSync\Helper::createGoogleOauthClient($config); if ($client->isAccessTokenExpired()) { $client->getRefreshToken(); } $gmail = new \Google_Service_Gmail($client); $label_id = \InboxSync\Helper::findLabel($gmail, 'Notifications/Drupal'); /** * @todo: Fix upstream. */ $input = new ArrayInput(array('command' => 'login')); $command = $this->getApplication()->add(new Login()); $command->run($input, $output); $browser = new \DrupalPatchUtils\DoBrowser(); $iq = new \DrupalPatchUtils\User(254778, $browser); $drupal_org_unread = $iq->getUnreadIssues(); $both_unread = []; foreach ($gmail->users_threads->listUsersThreads('me', ['labelIds' => ['UNREAD', $label_id]]) as $thread) { $thread = $gmail->users_threads->get('me', $thread['id'], ['format' => 'metadata']); $issue_ref = NULL; $id = NULL; /** * @var \Google_Service_Gmail_MessagePartHeader $header */ foreach ($thread->getMessages() as $message) { foreach ($message->getPayload()->getHeaders() as $header) { if ($header->getName() == 'Message-Id') { $issue_ref = $header->getValue(); break 2; } } } preg_match('/nid=([0-9]*)/', $issue_ref, $matches); if (isset($matches[1])) { $id = $matches[1]; if (isset($drupal_org_unread[$id])) { $both_unread[] = $id; } else { // No unread Drupal.org issue, mark mail as read. $body = new \Google_Service_Gmail_ModifyThreadRequest(); $body->setRemoveLabelIds(['UNREAD', 'INBOX']); $gmail->users_threads->modify('me', $thread->getId(), $body); } } } // $drupal_org_unread contains all unread issues on drupal.org, the Gmail client // found no unread mails in gmail, Mark all issues without an related mail as // read. $mark_as_read = array_diff($drupal_org_unread, $both_unread); foreach ($mark_as_read as $nid) { $iq->getIssue($nid); } }