GitHub PullRequestReviewCommentEvent at 2014-05-20 12:29:40
Repository: apps
Actor: DeepDiver1975
Payload: {"comment":{"url":"https://api.github.com/repos/owncloud/apps/pulls/comments/12840722","id":12840722,"diff_hunk":"@@ -0,0 +1,218 @@\n+<?php\n+\n+/**\n+ * ownCloud - Activity App\n+ *\n+ * @author Joas Schilling\n+ * @copyright 2014 Joas Schilling nickvergessen@owncloud.com\n+ *\n+ * This library is free software; you can redistribute it and/or\n+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE\n+ * License as published by the Free Software Foundation; either\n+ * version 3 of the License, or any later version.\n+ *\n+ * This library is distributed in the hope that it will be useful,\n+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.\n+ *\n+ * You should have received a copy of the GNU Lesser General Public\n+ * License along with this library.  If not, see <http://www.gnu.org/licenses/>.\n+ *\n+ */\n+\n+namespace OCA\\Activity;\n+\n+/**\n+ * Class MailQueueHandler\n+ * Gets the users from the database and\n+ *\n+ * @package OCA\\Activity\n+ */\n+class MailQueueHandler {\n+\t/** @var array */\n+\tprotected $languages;\n+\n+\t/** @var string */\n+\tprotected $sender_address;\n+\n+\t/** @var string */\n+\tprotected $sender_name;\n+\n+\t/**\n+\t * Get the users we want to send an email to\n+\t *\n+\t * @param int|null $limit\n+\t * @return array\n+\t */\n+\tpublic function getAffectedUsers($limit) {\n+\t\t$limit = (!$limit) ? null : (int) $limit;\n+\n+\t\t$query = \\OCP\\DB::prepare(\n+\t\t\t'SELECT `amq_affecteduser` '\n+\t\t\t. ' FROM `*PREFIX*activity_mq` '\n+\t\t\t. ' WHERE `amq_latest_send` < ? '\n+\t\t\t. ' GROUP BY `amq_affecteduser` '\n+\t\t\t. ' ORDER BY `amq_latest_send` ASC',\n+\t\t\t$limit);\n+\t\t$result = $query->execute(array(time()));\n+\n+\t\t$affected_users = array();\n+\t\tif (\\OCP\\DB::isError($result)) {\n+\t\t\t\\OCP\\Util::writeLog('OCA\\Activity\\BackgroundJob\\EmailNotification::getAffectedUsers', \\OC_DB::getErrorMessage($result), \\OC_Log::ERROR);\n+\t\t} else {\n+\t\t\twhile ($row = $result->fetchRow()) {\n+\t\t\t\t$affected_users[] = $row['amq_affecteduser'];\n+\t\t\t}\n+\t\t}\n+\n+\t\treturn $affected_users;\n+\t}\n+\n+\t/**\n+\t * Get all items for the users we want to send an email to\n+\t *\n+\t * @param array $affected_users\n+\t * @param int $max_time\n+\t * @return array Notification data (user => array of rows from the table)\n+\t */\n+\tpublic function getItemsForUsers($affected_users, $max_time) {\n+\t\t$placeholders = implode(',', array_fill(0, sizeof($affected_users), '?'));\n+\t\t$query_params = $affected_users;\n+\t\tarray_unshift($query_params, (int) $max_time);\n+\n+\t\t$query = \\OCP\\DB::prepare(\n+\t\t\t'SELECT * '\n+\t\t\t. ' FROM `*PREFIX*activity_mq` '\n+\t\t\t. ' WHERE `amq_timestamp` <= ? '\n+\t\t\t. ' AND `amq_affecteduser` IN (' . $placeholders . ')'\n+\t\t\t. ' ORDER BY `amq_timestamp`'\n+\t\t);\n+\t\t$result = $query->execute($query_params);\n+\n+\t\t$user_activity_map = array();\n+\t\tif (\\OCP\\DB::isError($result)) {\n+\t\t\t\\OCP\\Util::writeLog('OCA\\Activity\\BackgroundJob\\EmailNotification::getItemsForUsers', \\OC_DB::getErrorMessage($result), \\OC_Log::ERROR);\n+\t\t} else {\n+\t\t\twhile ($row = $result->fetchRow()) {\n+\t\t\t\t$user_activity_map[$row['amq_affecteduser']][] = $row;\n+\t\t\t}\n+\t\t}\n+\n+\t\treturn $user_activity_map;\n+\t}\n+\n+\t/**\n+\t * Get a language object for a specific language\n+\t *\n+\t * @param string $lang Language identifier\n+\t * @return \\OC_L10N Language object of $lang\n+\t */\n+\tprotected function getLanguage($lang) {\n+\t\tif (!isset($this->languages[$lang])) {\n+\t\t\t$this->languages[$lang] = \\OC_L10N::get('activity', $lang);\n+\t\t}\n+\n+\t\treturn $this->languages[$lang];\n+\t}\n+\n+\t/**\n+\t * Get the sender data\n+\t * @param string $setting Either `email` or `name`\n+\t * @return string\n+\t */\n+\tprotected function getSenderData($setting) {\n+\t\tif (empty($this->sender_address)) {\n+\t\t\t$this->sender_address = \\OCP\\Util::getDefaultEmailAddress('no-reply');\n+\t\t}\n+\t\tif (empty($this->sender_name)) {\n+\t\t\t$defaults = new \\OC_Defaults();\n+\t\t\t$this->sender_name = $defaults->getName();\n+\t\t}\n+\n+\t\tif ($setting === 'email') {\n+\t\t\treturn $this->sender_address;\n+\t\t}\n+\t\treturn $this->sender_name;\n+\t}\n+\n+\t/**\n+\t * Get the language string for a timeframe\n+\t * However we are not very accurate here, so we match the setting of the user\n+\t * a bit better\n+\t *\n+\t * @param \\OC_L10N $l\n+\t * @param int      $timestamp\n+\t * @return \\OC_L10N_String\n+\t */\n+\tprotected function getLangForApproximatedTimeframe(\\OC_L10N $l, $timestamp) {\n+\t\tif (time() - $timestamp < 4000) {\n+\t\t\treturn $l->t('in the last hour');\n+\t\t} else if (time() - $timestamp < 90000) {\n+\t\t\treturn $l->t('in the last day');\n+\t\t} else {\n+\t\t\treturn $l->t('in the last week');\n+\t\t}\n+\t}\n+\n+\t/**\n+\t * Send a notification to one user\n+\t *\n+\t * @param string $user Username of the recipient\n+\t * @param string $email Email address of the recipient\n+\t * @param string $lang Selected language of the recipient\n+\t * @param array $mailData Notification data we send to the user\n+\t */\n+\tpublic function sendEmailToUser($user, $email, $lang, $mailData) {\n+\t\t$l = $this->getLanguage($lang);\n+\n+\t\t$activityList = array();\n+\t\tforeach ($mailData as $activity) {\n+\t\t\t$activityList[] = \\OCA\\Activity\\Data::translation(\n+\t\t\t\t$activity['amq_appid'], $activity['amq_subject'], unserialize($activity['amq_subjectparams']),\n+\t\t\t\tfalse, false, $l\n+\t\t\t);\n+\t\t}\n+\n+\t\t$alttext = new \\OCP\\Template('activity', 'email.notification', '');\n+\t\t$alttext->assign('username', $user);\n+\t\t$alttext->assign('timeframe', $this->getLangForApproximatedTimeframe($l, $mailData[0]['amq_timestamp']));\n+\t\t$alttext->assign('activities', $activityList);\n+\t\t$alttext->assign('owncloud_installation', \\OC_Helper::makeURLAbsolute('/'));\n+\t\t$emailText = $alttext->fetchPage();\n+\n+\t\ttry {\n+\t\t\t\\OC_Mail::send(\n+\t\t\t\t$email, $user,\n+\t\t\t\t$l->t('Activity notification'), $emailText,\n+\t\t\t\t$this->getSenderData('email'), $this->getSenderData('name')\n+\t\t\t);\n+\t\t} catch (\\Exception $e) {\n+\t\t\t$message = $l->t('A problem occurred while sending the e-mail. Please revisit your settings.');\n+\t\t\t\\OC_JSON::error( array( \"data\" => array( \"message\" => $message)) );\n+\t\t\texit;\n+\t\t}\n+\t}\n+\n+\t/**\n+\t * Delete all entries we dealed with","path":"activity/lib/mailqueuehandler.php","position":198,"original_position":198,"commit_id":"a81e08c2327345a99b56c494c23dea68e81b8f08","original_commit_id":"2a1d1f9481a6927f06e088b89140837edec39567","user":{"login":"DeepDiver1975","id":1005065,"avatar_url":"https://avatars.githubusercontent.com/u/1005065?","gravatar_id":"bdba071ed68da9ce8edae53c364902ef","url":"https://api.github.com/users/DeepDiver1975","html_url":"https://github.com/DeepDiver1975","followers_url":"https://api.github.com/users/DeepDiver1975/followers","following_url":"https://api.github.com/users/DeepDiver1975/following{/other_user}","gists_url":"https://api.github.com/users/DeepDiver1975/gists{/gist_id}","starred_url":"https://api.github.com/users/DeepDiver1975/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DeepDiver1975/subscriptions","organizations_url":"https://api.github.com/users/DeepDiver1975/orgs","repos_url":"https://api.github.com/users/DeepDiver1975/repos","events_url":"https://api.github.com/users/DeepDiver1975/events{/privacy}","received_events_url":"https://api.github.com/users/DeepDiver1975/received_events","type":"User","site_admin":false},"body":"dealt","created_at":"2014-05-20T12:29:40Z","updated_at":"2014-05-20T12:29:40Z","html_url":"https://github.com/owncloud/apps/pull/1743#discussion_r12840722","pull_request_url":"https://api.github.com/repos/owncloud/apps/pulls/1743","_links":{"self":{"href":"https://api.github.com/repos/owncloud/apps/pulls/comments/12840722"},"html":{"href":"https://github.com/owncloud/apps/pull/1743#discussion_r12840722"},"pull_request":{"href":"https://api.github.com/repos/owncloud/apps/pulls/1743"}}}}