/**
  * Send an email to {$limit} users
  *
  * @param int $limit Number of users we want to send an email to
  * @return int Number of users we sent an email to
  */
 protected function runStep($limit)
 {
     // Get all users which should receive an email
     $affectedUsers = $this->mqHandler->getAffectedUsers($limit);
     if (empty($affectedUsers)) {
         // No users found to notify, mission abort
         return 0;
     }
     $preferences = new \OC\Preferences(\OC_DB::getConnection());
     $userLanguages = $preferences->getValueForUsers('core', 'lang', $affectedUsers);
     $userEmails = $preferences->getValueForUsers('settings', 'email', $affectedUsers);
     // Get all items for these users
     // We don't use time() but "time() - 1" here, so we don't run into
     // runtime issues and delete emails later, which were created in the
     // same second, but were not collected for the emails.
     $sendTime = time() - 1;
     $mailData = $this->mqHandler->getItemsForUsers($affectedUsers, $sendTime);
     // Send Email
     $default_lang = \OC_Config::getValue('default_language', 'en');
     foreach ($mailData as $user => $data) {
         if (!isset($userEmails[$user])) {
             // The user did not setup an email address
             // So we will not send an email :(
             continue;
         }
         $language = isset($userLanguages[$user]) ? $userLanguages[$user] : $default_lang;
         $this->mqHandler->sendEmailToUser($user, $userEmails[$user], $language, $data);
     }
     // Delete all entries we dealt with
     $this->mqHandler->deleteSentItems($affectedUsers, $sendTime);
     return sizeof($affectedUsers);
 }
 /**
  * Send an email to {$limit} users
  *
  * @param int $limit Number of users we want to send an email to
  * @param int $sendTime The latest send time
  * @return int Number of users we sent an email to
  */
 protected function runStep($limit, $sendTime)
 {
     // Get all users which should receive an email
     $affectedUsers = $this->mqHandler->getAffectedUsers($limit, $sendTime);
     if (empty($affectedUsers)) {
         // No users found to notify, mission abort
         return 0;
     }
     $userLanguages = $this->config->getUserValueForUsers('core', 'lang', $affectedUsers);
     $userTimezones = $this->config->getUserValueForUsers('core', 'timezone', $affectedUsers);
     $userEmails = $this->config->getUserValueForUsers('settings', 'email', $affectedUsers);
     // Send Email
     $default_lang = $this->config->getSystemValue('default_language', 'en');
     $defaultTimeZone = date_default_timezone_get();
     foreach ($affectedUsers as $user) {
         if (empty($userEmails[$user])) {
             // The user did not setup an email address
             // So we will not send an email :(
             $this->logger->debug("Couldn't send notification email to user '" . $user . "' (email address isn't set for that user)", ['app' => 'activity']);
             continue;
         }
         $language = !empty($userLanguages[$user]) ? $userLanguages[$user] : $default_lang;
         $timezone = !empty($userTimezones[$user]) ? $userTimezones[$user] : $defaultTimeZone;
         $this->mqHandler->sendEmailToUser($user, $userEmails[$user], $language, $timezone, $sendTime);
     }
     // Delete all entries we dealt with
     $this->mqHandler->deleteSentItems($affectedUsers, $sendTime);
     return sizeof($affectedUsers);
 }
 /**
  * Send an email to {$limit} users
  *
  * @param int $limit Number of users we want to send an email to
  * @return int Number of users we sent an email to
  */
 protected function runStep($limit)
 {
     // We don't use time() but "time() - 1" here, so we don't run into
     // runtime issues later and delete emails, which were created in the
     // same second, but were not collected for the emails.
     $sendTime = time() - 1;
     // Get all users which should receive an email
     $affectedUsers = $this->mqHandler->getAffectedUsers($limit, $sendTime);
     if (empty($affectedUsers)) {
         // No users found to notify, mission abort
         return 0;
     }
     $userLanguages = $this->config->getUserValueForUsers('core', 'lang', $affectedUsers);
     $userTimezones = $this->config->getUserValueForUsers('core', 'timezone', $affectedUsers);
     $userEmails = $this->config->getUserValueForUsers('settings', 'email', $affectedUsers);
     // Get all items for these users
     $mailData = $this->mqHandler->getItemsForUsers($affectedUsers, $sendTime);
     // Send Email
     $default_lang = $this->config->getSystemValue('default_language', 'en');
     $defaultTimeZone = date_default_timezone_get();
     foreach ($mailData as $user => $data) {
         if (empty($userEmails[$user])) {
             // The user did not setup an email address
             // So we will not send an email :(
             $this->logger->debug("Couldn't send notification email to user '" . $user . "' (email address isn't set for that user)", ['app' => 'activity']);
             continue;
         }
         $language = !empty($userLanguages[$user]) ? $userLanguages[$user] : $default_lang;
         $timezone = !empty($userTimezones[$user]) ? $userTimezones[$user] : $defaultTimeZone;
         $this->mqHandler->sendEmailToUser($user, $userEmails[$user], $language, $timezone, $data);
     }
     // Delete all entries we dealt with
     $this->mqHandler->deleteSentItems($affectedUsers, $sendTime);
     return sizeof($affectedUsers);
 }
 public function testSendEmailToUser()
 {
     $maxTime = 200;
     $user = '******';
     $userDisplayName = 'user two';
     $email = $user . '@localhost';
     $this->mailer->expects($this->once())->method('send')->with($this->message);
     $this->message->expects($this->once())->method('setTo')->with([$email => $userDisplayName]);
     $this->message->expects($this->once())->method('setSubject');
     $this->message->expects($this->once())->method('setPlainBody');
     $this->message->expects($this->once())->method('setFrom');
     $userObject = $this->getMock('OCP\\IUser');
     $userObject->expects($this->any())->method('getDisplayName')->willReturn($userDisplayName);
     $this->userManager->expects($this->any())->method('get')->willReturnMap([[$user, $userObject], [$user . $user, null]]);
     $users = $this->mailQueueHandler->getAffectedUsers(1, $maxTime);
     $this->assertEquals([$user], $users);
     $this->mailQueueHandler->sendEmailToUser($user, $email, 'en', 'UTC', $maxTime);
     // Invalid user, no object no email
     $this->mailQueueHandler->sendEmailToUser($user . $user, $email, 'en', 'UTC', $maxTime);
 }
 /**
  * @dataProvider getAffectedUsersData
  */
 public function testGetAffectedUsers($limit, $expected)
 {
     $mq = new MailQueueHandler($this->getMock('\\OCP\\IDateTimeFormatter'));
     $users = $mq->getAffectedUsers($limit, time());
     $this->assertEquals($expected, $users);
 }
 /**
  * @dataProvider getAffectedUsersData
  */
 public function testGetAffectedUsers($limit, $expected)
 {
     $mq = new MailQueueHandler();
     $users = $mq->getAffectedUsers($limit);
     $this->assertEquals($expected, $users);
 }