Blame view
sources/apps/activity/lib/mailqueuehandler.php
6.21 KB
|
6d9380f96
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
<?php
/**
* ownCloud - Activity App
*
* @author Joas Schilling
* @copyright 2014 Joas Schilling nickvergessen@owncloud.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Activity;
/**
* Class MailQueueHandler
* Gets the users from the database and
*
* @package OCA\Activity
*/
class MailQueueHandler {
/** @var array */
protected $languages;
/** @var string */
protected $senderAddress;
/** @var string */
protected $senderName;
/**
* Get the users we want to send an email to
*
* @param int|null $limit
* @return array
*/
public function getAffectedUsers($limit) {
$limit = (!$limit) ? null : (int) $limit;
$query = \OCP\DB::prepare(
'SELECT `amq_affecteduser`, MIN(`amq_latest_send`) AS `amq_trigger_time` '
. ' FROM `*PREFIX*activity_mq` '
. ' WHERE `amq_latest_send` < ? '
. ' GROUP BY `amq_affecteduser` '
. ' ORDER BY `amq_trigger_time` ASC',
$limit);
$result = $query->execute(array(time()));
$affectedUsers = array();
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('OCA\Activity', \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR);
} else {
while ($row = $result->fetchRow()) {
$affectedUsers[] = $row['amq_affecteduser'];
}
}
return $affectedUsers;
}
/**
* Get all items for the users we want to send an email to
*
* @param array $affectedUsers
* @param int $maxTime
* @return array Notification data (user => array of rows from the table)
*/
public function getItemsForUsers($affectedUsers, $maxTime) {
$placeholders = implode(',', array_fill(0, sizeof($affectedUsers), '?'));
$queryParams = $affectedUsers;
array_unshift($queryParams, (int) $maxTime);
$query = \OCP\DB::prepare(
'SELECT * '
. ' FROM `*PREFIX*activity_mq` '
. ' WHERE `amq_timestamp` <= ? '
. ' AND `amq_affecteduser` IN (' . $placeholders . ')'
. ' ORDER BY `amq_timestamp` ASC'
);
$result = $query->execute($queryParams);
$userActivityMap = array();
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('Activity', \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR);
} else {
while ($row = $result->fetchRow()) {
$userActivityMap[$row['amq_affecteduser']][] = $row;
}
}
return $userActivityMap;
}
/**
* Get a language object for a specific language
*
* @param string $lang Language identifier
* @return \OC_L10N Language object of $lang
*/
protected function getLanguage($lang) {
if (!isset($this->languages[$lang])) {
$this->languages[$lang] = \OC_L10N::get('activity', $lang);
}
return $this->languages[$lang];
}
/**
* Get the sender data
* @param string $setting Either `email` or `name`
* @return string
*/
protected function getSenderData($setting) {
if (empty($this->senderAddress)) {
$this->senderAddress = \OCP\Util::getDefaultEmailAddress('no-reply');
}
if (empty($this->senderName)) {
$defaults = new \OCP\Defaults();
$this->senderName = $defaults->getName();
}
if ($setting === 'email') {
return $this->senderAddress;
}
return $this->senderName;
}
/**
* Get the selected setting for a time frame
* However we are not very accurate here, so we match the setting of the user
* a bit better.
*
* @param int $timestamp
* @return int Email send option that is used.
*/
protected function getLangForApproximatedTimeFrame($timestamp) {
if (time() - $timestamp < 4000) {
return UserSettings::EMAIL_SEND_HOURLY;
} else if (time() - $timestamp < 90000) {
return UserSettings::EMAIL_SEND_DAILY;
} else {
return UserSettings::EMAIL_SEND_WEEKLY;
}
}
/**
* Send a notification to one user
*
* @param string $user Username of the recipient
* @param string $email Email address of the recipient
* @param string $lang Selected language of the recipient
* @param array $mailData Notification data we send to the user
*/
public function sendEmailToUser($user, $email, $lang, $mailData) {
$l = $this->getLanguage($lang);
$dataHelper = new DataHelper(\OC::$server->getActivityManager(), new ParameterHelper(new \OC\Files\View(''), $l), $l);
$activityList = array();
foreach ($mailData as $activity) {
$activityList[] = $dataHelper->translation(
$activity['amq_appid'], $activity['amq_subject'], unserialize($activity['amq_subjectparams'])
);
}
$alttext = new \OCP\Template('activity', 'email.notification', '');
$alttext->assign('username', \OCP\User::getDisplayName($user));
$alttext->assign('timeframe', $this->getLangForApproximatedTimeFrame($mailData[0]['amq_timestamp']));
$alttext->assign('activities', $activityList);
$alttext->assign('owncloud_installation', \OC_Helper::makeURLAbsolute('/'));
$emailText = $alttext->fetchPage();
try {
\OCP\Util::sendMail(
$email, $user,
$l->t('Activity notification'), $emailText,
$this->getSenderData('email'), $this->getSenderData('name')
);
} catch (\Exception $e) {
\OCP\Util::writeLog('Activity', 'A problem occurred while sending the e-mail. Please revisit your settings.', \OCP\Util::ERROR);
}
}
/**
* Delete all entries we dealt with
*
* @param array $affectedUsers
* @param int $maxTime
*/
public function deleteSentItems($affectedUsers, $maxTime) {
$placeholders = implode(',', array_fill(0, sizeof($affectedUsers), '?'));
$queryParams = $affectedUsers;
array_unshift($queryParams, (int) $maxTime);
$query = \OCP\DB::prepare(
'DELETE FROM `*PREFIX*activity_mq` '
. ' WHERE `amq_timestamp` <= ? '
. ' AND `amq_affecteduser` IN (' . $placeholders . ')');
$result = $query->execute($queryParams);
if (\OCP\DB::isError($result)) {
\OCP\Util::writeLog('Activity', \OCP\DB::getErrorMessage($result), \OCP\Util::ERROR);
}
}
}
|