protected function addNotificationsForUser($user, $subject, $subjectParams, $path, $isFile, $streamSetting, $emailSetting, $type = Files_Sharing::TYPE_SHARED, $priority = IExtension::PRIORITY_MEDIUM) { if (!$streamSetting && !$emailSetting) { return; } $selfAction = $user === $this->currentUser; $app = $type === Files_Sharing::TYPE_SHARED ? 'sharing_group' : 'files'; $link = Util::linkToAbsolute('files', 'index.php', array('dir' => $isFile ? dirname($path) : $path)); // Add activity to stream if ($streamSetting && (!$selfAction || $this->userSettings->getUserSetting($this->currentUser, 'setting', 'self'))) { $this->activityData->send($app, $subject, $subjectParams, '', array(), $path, $link, $user, $type, $priority); } // Add activity to mail queue if ($emailSetting && (!$selfAction || $this->userSettings->getUserSetting($this->currentUser, 'setting', 'selfemail'))) { $latestSend = time() + $emailSetting; $this->activityData->storeMail($app, $subject, $subjectParams, $user, $type, $latestSend); } }
/** * Get all items for the users we want to send an email to * * @return array Notification data (user => array of rows from the table) */ public function getLinkList() { $topEntries = [['id' => 'all', 'name' => (string) $this->l->t('All Activities'), 'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList')]]; if ($this->user && $this->userSettings->getUserSetting($this->user, 'setting', 'self')) { $topEntries[] = ['id' => 'self', 'name' => (string) $this->l->t('Activities by you'), 'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', array('filter' => 'self'))]; $topEntries[] = ['id' => 'by', 'name' => (string) $this->l->t('Activities by others'), 'url' => $this->URLGenerator->linkToRoute('activity.Activities.showList', array('filter' => 'by'))]; } $additionalEntries = $this->activityManager->getNavigation(); $topEntries = array_merge($topEntries, $additionalEntries['top']); return array('top' => $topEntries, 'apps' => $additionalEntries['apps']); }
/** * Send an event to the notifications of a user * * @param IEvent $event * @return null */ public function receive(IEvent $event) { $selfAction = $event->getAffectedUser() === $event->getAuthor(); $streamSetting = $this->userSettings->getUserSetting($event->getAffectedUser(), 'stream', $event->getType()); $emailSetting = $this->userSettings->getUserSetting($event->getAffectedUser(), 'email', $event->getType()); $emailSetting = $emailSetting ? $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'batchtime') : false; $types = $this->data->getNotificationTypes($this->l10nFactory->get('activity')); $typeData = $types[$event->getType()]; // User is not the author or wants to see their own actions $createStream = !$selfAction || $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'self'); // User can not control the setting $createStream = $createStream || is_array($typeData) && isset($typeData['methods']) && !in_array(IExtension::METHOD_STREAM, $typeData['methods']); // Add activity to stream if ($streamSetting && $createStream) { $this->data->send($event); } // User is not the author or wants to see their own actions $createEmail = !$selfAction || $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'selfemail'); // User can not control the setting $createEmail = $createEmail || is_array($typeData) && isset($typeData['methods']) && !in_array(IExtension::METHOD_MAIL, $typeData['methods']); // Add activity to mail queue if ($emailSetting && $createEmail) { $latestSend = $event->getTimestamp() + $emailSetting; $this->data->storeMail($event, $latestSend); } }
/** * @NoAdminRequired * @NoCSRFRequired * * @return TemplateResponse */ public function displayPanel() { $types = $this->data->getNotificationTypes($this->l10n); $activities = array(); foreach ($types as $type => $desc) { $activities[$type] = array('desc' => $desc, 'email' => $this->userSettings->getUserSetting($this->user, 'email', $type), 'stream' => $this->userSettings->getUserSetting($this->user, 'stream', $type)); } $settingBatchTime = UserSettings::EMAIL_SEND_HOURLY; if ($this->userSettings->getUserSetting($this->user, 'setting', 'batchtime') == 3600 * 24 * 7) { $settingBatchTime = UserSettings::EMAIL_SEND_WEEKLY; } else { if ($this->userSettings->getUserSetting($this->user, 'setting', 'batchtime') == 3600 * 24) { $settingBatchTime = UserSettings::EMAIL_SEND_DAILY; } } return new TemplateResponse('activity', 'personal', ['activities' => $activities, 'activity_email' => $this->config->getUserValue($this->user, 'settings', 'email', ''), 'setting_batchtime' => $settingBatchTime, 'notify_self' => $this->userSettings->getUserSetting($this->user, 'setting', 'self'), 'notify_selfemail' => $this->userSettings->getUserSetting($this->user, 'setting', 'selfemail')], ''); }
/** * Adds the activity and email for a user when the settings require it * * @param string $user * @param string $subject * @param array $subjectParams * @param int $fileId * @param string $path * @param bool $isFile If the item is a file, we link to the parent directory * @param bool $streamSetting * @param int $emailSetting * @param string $type */ protected function addNotificationsForUser($user, $subject, $subjectParams, $fileId, $path, $isFile, $streamSetting, $emailSetting, $type = Files_Sharing::TYPE_SHARED) { if (!$streamSetting && !$emailSetting) { return; } $selfAction = $user === $this->currentUser; $app = $type === Files_Sharing::TYPE_SHARED ? 'files_sharing' : 'files'; $link = Util::linkToAbsolute('files', 'index.php', array('dir' => $isFile ? dirname($path) : $path)); $objectType = $fileId ? 'files' : ''; $event = $this->manager->generateEvent(); $event->setApp($app)->setType($type)->setAffectedUser($user)->setAuthor($this->currentUser)->setTimestamp(time())->setSubject($subject, $subjectParams)->setObject($objectType, $fileId, $path)->setLink($link); // Add activity to stream if ($streamSetting && (!$selfAction || $this->userSettings->getUserSetting($this->currentUser, 'setting', 'self'))) { $this->activityData->send($event); } // Add activity to mail queue if ($emailSetting && (!$selfAction || $this->userSettings->getUserSetting($this->currentUser, 'setting', 'selfemail'))) { $latestSend = time() + $emailSetting; $this->activityData->storeMail($event, $latestSend); } }
/** * Send an event into the activity stream of a user * * @param string $app The app where this event is associated with * @param string $subject A short description of the event * @param array $subjectParams Array with parameters that are filled in the subject * @param string $message A longer description of the event * @param array $messageParams Array with parameters that are filled in the message * @param string $file The file including path where this event is associated with. (optional) * @param string $link A link where this event is associated with (optional) * @param string $affectedUser If empty the current user will be used * @param string $type Type of the notification * @param int $priority Priority of the notification * @return null */ public function receive($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) { $selfAction = substr($subject, -5) === '_self'; $streamSetting = $this->userSettings->getUserSetting($affectedUser, 'stream', $type); $emailSetting = $this->userSettings->getUserSetting($affectedUser, 'email', $type); $emailSetting = $emailSetting ? $this->userSettings->getUserSetting($affectedUser, 'setting', 'batchtime') : false; // Add activity to stream if ($streamSetting && (!$selfAction || $this->userSettings->getUserSetting($affectedUser, 'setting', 'self'))) { Data::send($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority); } // Add activity to mail queue if ($emailSetting && (!$selfAction || $this->userSettings->getUserSetting($affectedUser, 'setting', 'selfemail'))) { $latestSend = time() + $emailSetting; Data::storeMail($app, $subject, $subjectParams, $affectedUser, $type, $latestSend); } }
/** * Send an event to the notifications of a user * * @param IEvent $event * @return null */ public function receive(IEvent $event) { $selfAction = $event->getAffectedUser() === $event->getAuthor(); $streamSetting = $this->userSettings->getUserSetting($event->getAffectedUser(), 'stream', $event->getType()); $emailSetting = $this->userSettings->getUserSetting($event->getAffectedUser(), 'email', $event->getType()); $emailSetting = $emailSetting ? $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'batchtime') : false; // Add activity to stream if ($streamSetting && (!$selfAction || $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'self'))) { $this->data->send($event); } // Add activity to mail queue if ($emailSetting && (!$selfAction || $this->userSettings->getUserSetting($event->getAffectedUser(), 'setting', 'selfemail'))) { $latestSend = $event->getTimestamp() + $emailSetting; $this->data->storeMail($event, $latestSend); } }
/** * @NoAdminRequired * @NoCSRFRequired * * @return TemplateResponse */ public function displayPanel() { $types = $this->data->getNotificationTypes($this->l10n); $activities = array(); foreach ($types as $type => $desc) { if (is_array($desc)) { $methods = isset($desc['methods']) ? $desc['methods'] : [IExtension::METHOD_STREAM, IExtension::METHOD_MAIL]; $desc = isset($desc['desc']) ? $desc['desc'] : ''; } else { $methods = [IExtension::METHOD_STREAM, IExtension::METHOD_MAIL]; } $activities[$type] = array('desc' => $desc, IExtension::METHOD_MAIL => $this->userSettings->getUserSetting($this->user, 'email', $type), IExtension::METHOD_STREAM => $this->userSettings->getUserSetting($this->user, 'stream', $type), 'methods' => $methods); } $settingBatchTime = UserSettings::EMAIL_SEND_HOURLY; $currentSetting = (int) $this->userSettings->getUserSetting($this->user, 'setting', 'batchtime'); if ($currentSetting === 3600 * 24 * 7) { $settingBatchTime = UserSettings::EMAIL_SEND_WEEKLY; } else { if ($currentSetting === 3600 * 24) { $settingBatchTime = UserSettings::EMAIL_SEND_DAILY; } } return new TemplateResponse('activity', 'personal', ['activities' => $activities, 'activity_email' => $this->config->getUserValue($this->user, 'settings', 'email', ''), 'setting_batchtime' => $settingBatchTime, 'notify_self' => $this->userSettings->getUserSetting($this->user, 'setting', 'self'), 'notify_selfemail' => $this->userSettings->getUserSetting($this->user, 'setting', 'selfemail'), 'methods' => [IExtension::METHOD_MAIL => $this->l10n->t('Mail'), IExtension::METHOD_STREAM => $this->l10n->t('Stream')]], ''); }
/** * Sharing a file or folder via link/public * @param array $params The hook params */ protected function shareFileOrFolder($params) { $path = Filesystem::getPath($params['fileSource']); $this->addNotificationsForUser($this->currentUser, 'shared_link_self', array($path), $path, $params['itemType'] === 'file', $this->userSettings->getUserSetting($this->currentUser, 'stream', Files_Sharing::TYPE_SHARED), $this->userSettings->getUserSetting($this->currentUser, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($this->currentUser, 'setting', 'batchtime') : 0); }
/** * @brief Read a list of events from the activity stream * @param GroupHelper $groupHelper Allows activities to be grouped * @param UserSettings $userSettings Gets the settings of the user * @param int $start The start entry * @param int $count The number of statements to read * @param string $filter Filter the activities * @param string $user User for whom we display the stream * @return array */ public function read(GroupHelper $groupHelper, UserSettings $userSettings, $start, $count, $filter = 'all', $user = '') { // get current user $user = $user !== '' ? $user : User::getUser(); $groupHelper->setUser($user); $enabledNotifications = $userSettings->getNotificationTypes($user, 'stream'); $enabledNotifications = $this->activityManager->filterNotificationTypes($enabledNotifications, $filter); $parameters = array_unique($enabledNotifications); // We don't want to display any activities if (empty($parameters)) { return array(); } $placeholders = implode(',', array_fill(0, sizeof($parameters), '?')); $limitActivities = " AND `type` IN (" . $placeholders . ")"; array_unshift($parameters, $user); if ($filter === 'self') { $limitActivities .= ' AND `user` = ?'; $parameters[] = $user; } else { if ($filter === 'by' || $filter === 'all' && !$userSettings->getUserSetting($user, 'setting', 'self')) { $limitActivities .= ' AND `user` <> ?'; $parameters[] = $user; } } list($condition, $params) = $this->activityManager->getQueryForFilter($filter); if (!is_null($condition)) { $limitActivities .= ' '; $limitActivities .= $condition; if (is_array($params)) { $parameters = array_merge($parameters, $params); } } // fetch from DB $query = DB::prepare('SELECT * ' . ' FROM `*PREFIX*activity` ' . ' WHERE `affecteduser` = ? ' . $limitActivities . ' ORDER BY `timestamp` DESC', $count, $start); $result = $query->execute($parameters); return $this->getActivitiesFromQueryResult($result, $groupHelper); }
/** * Read a list of events from the activity stream * * @param GroupHelper $groupHelper Allows activities to be grouped * @param UserSettings $userSettings Gets the settings of the user * @param string $user User for whom we display the stream * * @param int $since The integer ID of the last activity that has been seen. * @param int $limit How many activities should be returned * @param string $sort Should activities be given ascending or descending * * @param string $filter Filter the activities * @param string $objectType Allows to filter the activities to a given object. May only appear together with $objectId * @param int $objectId Allows to filter the activities to a given object. May only appear together with $objectType * * @return array * * @throws \OutOfBoundsException if the user (Code: 1) or the since (Code: 2) is invalid * @throws \BadMethodCallException if the user has selected to display no types for this filter (Code: 3) */ public function get(GroupHelper $groupHelper, UserSettings $userSettings, $user, $since, $limit, $sort, $filter, $objectType = '', $objectId = 0) { // get current user if ($user === '') { throw new \OutOfBoundsException('Invalid user', 1); } $groupHelper->setUser($user); $enabledNotifications = $userSettings->getNotificationTypes($user, 'stream'); $enabledNotifications = $this->activityManager->filterNotificationTypes($enabledNotifications, $filter); $enabledNotifications = array_unique($enabledNotifications); // We don't want to display any activities if (empty($enabledNotifications)) { throw new \BadMethodCallException('No settings enabled', 3); } $query = $this->connection->getQueryBuilder(); $query->select('*')->from('activity'); $query->where($query->expr()->eq('affecteduser', $query->createNamedParameter($user)))->andWhere($query->expr()->in('type', $query->createNamedParameter($enabledNotifications, \Doctrine\DBAL\Connection::PARAM_STR_ARRAY))); if ($filter === 'self') { $query->andWhere($query->expr()->eq('user', $query->createNamedParameter($user))); } else { if ($filter === 'by') { $query->andWhere($query->expr()->neq('user', $query->createNamedParameter($user))); } else { if ($filter === 'all' && !$userSettings->getUserSetting($user, 'setting', 'self')) { $query->andWhere($query->expr()->orX($query->expr()->neq('user', $query->createNamedParameter($user)), $query->expr()->notIn('type', $query->createNamedParameter(['file_created', 'file_changed', 'file_deleted', 'file_restored'], IQueryBuilder::PARAM_STR_ARRAY)))); } else { if ($filter === 'filter') { if (!$userSettings->getUserSetting($user, 'setting', 'self')) { $query->andWhere($query->expr()->orX($query->expr()->neq('user', $query->createNamedParameter($user)), $query->expr()->notIn('type', $query->createNamedParameter(['file_created', 'file_changed', 'file_deleted', 'file_restored'], IQueryBuilder::PARAM_STR_ARRAY)))); } $query->andWhere($query->expr()->eq('object_type', $query->createNamedParameter($objectType))); $query->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId))); } } } } list($condition, $params) = $this->activityManager->getQueryForFilter($filter); if (!is_null($condition)) { // Strip away ' and ' $condition = substr($condition, 5); if (is_array($params)) { // Replace ? placeholders with named parameters foreach ($params as $param) { $pos = strpos($condition, '?'); if ($pos !== false) { $condition = substr($condition, 0, $pos) . $query->createNamedParameter($param) . substr($condition, $pos + 1); } } } $query->andWhere($query->createFunction($condition)); } /** * Order and specify the offset */ $sqlSort = $sort === 'asc' ? 'ASC' : 'DESC'; $headers = $this->setOffsetFromSince($query, $user, $since, $sqlSort); $query->orderBy('timestamp', $sqlSort)->addOrderBy('activity_id', $sqlSort); $query->setMaxResults($limit + 1); $result = $query->execute(); $hasMore = false; while ($row = $result->fetch()) { if ($limit === 0) { $hasMore = true; break; } $headers['X-Activity-Last-Given'] = (int) $row['activity_id']; $groupHelper->addActivity($row); $limit--; } $result->closeCursor(); return ['data' => $groupHelper->getActivities(), 'has_more' => $hasMore, 'headers' => $headers]; }
* 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 Affero General Public * License along with this library. If not, see <http://www.gnu.org/licenses/>. */ \OCP\Util::addScript('activity', 'settings'); \OCP\Util::addStyle('activity', 'settings'); $l = \OCP\Util::getL10N('activity'); $data = new \OCA\Activity\Data(\OC::$server->getActivityManager()); $types = $data->getNotificationTypes($l); $user = \OCP\User::getUser(); $activities = array(); foreach ($types as $type => $desc) { $activities[$type] = array('desc' => $desc, 'email' => \OCA\Activity\UserSettings::getUserSetting($user, 'email', $type), 'stream' => \OCA\Activity\UserSettings::getUserSetting($user, 'stream', $type)); } $template = new \OCP\Template('activity', 'personal'); $template->assign('activities', $activities); if (\OCA\Activity\UserSettings::getUserSetting($user, 'setting', 'batchtime') == 3600 * 24 * 7) { $template->assign('setting_batchtime', \OCA\Activity\UserSettings::EMAIL_SEND_WEEKLY); } else { if (\OCA\Activity\UserSettings::getUserSetting($user, 'setting', 'batchtime') == 3600 * 24) { $template->assign('setting_batchtime', \OCA\Activity\UserSettings::EMAIL_SEND_DAILY); } else { $template->assign('setting_batchtime', \OCA\Activity\UserSettings::EMAIL_SEND_HOURLY); } } $template->assign('activity_email', \OCP\Config::getUserValue($user, 'settings', 'email', '')); $template->assign('notify_self', \OCA\Activity\UserSettings::getUserSetting($user, 'setting', 'self')); return $template->fetchPage();
/** * @brief Read a list of events from the activity stream * @param GroupHelper $groupHelper Allows activities to be grouped * @param UserSettings $userSettings Gets the settings of the user * @param int $start The start entry * @param int $count The number of statements to read * @param string $filter Filter the activities * @param string $user User for whom we display the stream * @param string $objectType * @param int $objectId * @return array */ public function read(GroupHelper $groupHelper, UserSettings $userSettings, $start, $count, $filter = 'all', $user = '', $objectType = '', $objectId = 0) { // get current user if ($user === '') { $user = $this->userSession->getUser(); if ($user instanceof IUser) { $user = $user->getUID(); } else { // No user given and not logged in => no activities return []; } } $groupHelper->setUser($user); $enabledNotifications = $userSettings->getNotificationTypes($user, 'stream'); $enabledNotifications = $this->activityManager->filterNotificationTypes($enabledNotifications, $filter); $parameters = array_unique($enabledNotifications); // We don't want to display any activities if (empty($parameters)) { return array(); } $placeholders = implode(',', array_fill(0, sizeof($parameters), '?')); $limitActivities = " AND `type` IN (" . $placeholders . ")"; array_unshift($parameters, $user); if ($filter === 'self') { $limitActivities .= ' AND `user` = ?'; $parameters[] = $user; } else { if ($filter === 'by' || $filter === 'all' && !$userSettings->getUserSetting($user, 'setting', 'self')) { $limitActivities .= ' AND `user` <> ?'; $parameters[] = $user; } else { if ($filter === 'filter') { if (!$userSettings->getUserSetting($user, 'setting', 'self')) { $limitActivities .= ' AND `user` <> ?'; $parameters[] = $user; } $limitActivities .= ' AND `object_type` = ?'; $parameters[] = $objectType; $limitActivities .= ' AND `object_id` = ?'; $parameters[] = $objectId; } } } list($condition, $params) = $this->activityManager->getQueryForFilter($filter); if (!is_null($condition)) { $limitActivities .= ' '; $limitActivities .= $condition; if (is_array($params)) { $parameters = array_merge($parameters, $params); } } return $this->getActivities($count, $start, $limitActivities, $parameters, $groupHelper); }