Example #1
0
 protected function setUp()
 {
     parent::setUp();
     $this->deleteTestActivities();
     $this->userSettings = $this->getMockBuilder('OCA\\Activity\\UserSettings')->setMethods(array('getUserSetting'))->disableOriginalConstructor()->getMock();
     $this->userSettings->expects($this->any())->method('getUserSetting')->with($this->stringContains('affectedUser'), $this->anything(), $this->anything())->will($this->returnValueMap(array(array('affectedUser', 'stream', 'type', true), array('affectedUser2', 'stream', 'type', true), array('affectedUser', 'setting', 'self', true), array('affectedUser2', 'setting', 'self', false), array('affectedUser', 'email', 'type', true), array('affectedUser2', 'email', 'type', true), array('affectedUser', 'setting', 'selfemail', true), array('affectedUser2', 'setting', 'selfemail', false), array('affectedUser', 'setting', 'batchtime', 10), array('affectedUser2', 'setting', 'batchtime', 10))));
 }
 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']);
 }
Example #4
0
 /**
  * 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);
     }
 }
Example #5
0
 /**
  * @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')], '');
 }
 /**
  * @dataProvider dataAddNotificationsForUser
  *
  * @param string $user
  * @param string $subject
  * @param array $parameter
  * @param int $fileId
  * @param string $path
  * @param bool $isFile
  * @param bool $stream
  * @param bool $email
  * @param int $type
  * @param bool $selfSetting
  * @param bool $selfEmailSetting
  * @param string $app
  * @param bool $sentStream
  * @param bool $sentEmail
  */
 public function testAddNotificationsForUser($user, $subject, $parameter, $fileId, $path, $isFile, $stream, $email, $type, $selfSetting, $selfEmailSetting, $app, $sentStream, $sentEmail)
 {
     $this->settings->expects($this->any())->method('getUserSetting')->willReturnMap([[$user, 'setting', 'self', $selfSetting], [$user, 'setting', 'selfemail', $selfEmailSetting]]);
     $event = $this->getMockBuilder('OCP\\Activity\\IEvent')->disableOriginalConstructor()->getMock();
     $event->expects($this->once())->method('setApp')->with($app)->willReturnSelf();
     $event->expects($this->once())->method('setType')->with($type)->willReturnSelf();
     $event->expects($this->once())->method('setAffectedUser')->with($user)->willReturnSelf();
     $event->expects($this->once())->method('setAuthor')->with('user')->willReturnSelf();
     $event->expects($this->once())->method('setTimestamp')->willReturnSelf();
     $event->expects($this->once())->method('setSubject')->with($subject, $parameter)->willReturnSelf();
     $event->expects($this->once())->method('setLink')->willReturnSelf();
     if ($fileId) {
         $event->expects($this->once())->method('setObject')->with('files', $fileId, $path)->willReturnSelf();
     } else {
         $event->expects($this->once())->method('setObject')->with('', $fileId, $path)->willReturnSelf();
     }
     $this->activityManager->expects($this->once())->method('generateEvent')->willReturn($event);
     $this->data->expects($sentStream ? $this->once() : $this->never())->method('send')->with($event);
     $this->data->expects($sentEmail ? $this->once() : $this->never())->method('storeMail')->with($event, $this->anything());
     $this->invokePrivate($this->filesHooks, 'addNotificationsForUser', [$user, $subject, $parameter, $fileId, $path, $isFile, $stream, $email, $type]);
 }
Example #7
0
 /**
  * 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);
     }
 }
Example #8
0
 /**
  * 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);
     }
 }
Example #9
0
 /**
  * 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);
     }
 }
Example #10
0
 /**
  * @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')]], '');
 }
Example #11
0
 /**
  * @dataProvider filterUsersBySettingData
  */
 public function testFilterUsersBySetting($users, $method, $type, $expected)
 {
     $this->assertEquals($expected, UserSettings::filterUsersBySetting($users, $method, $type));
 }
Example #12
0
 /**
  * @brief Read a list of events from the activity stream
  * @param GroupHelper $groupHelper Allows activities to be grouped
  * @param int $start The start entry
  * @param int $count The number of statements to read
  * @param string $filter Filter the activities
  * @return array
  */
 public function read(GroupHelper $groupHelper, $start, $count, $filter = 'all')
 {
     // get current user
     $user = User::getUser();
     $enabledNotifications = UserSettings::getNotificationTypes($user, 'stream');
     $enabledNotifications = $this->filterNotificationTypes($enabledNotifications, $filter);
     // We don't want to display any activities
     if (empty($enabledNotifications)) {
         return array();
     }
     $parameters = array($user);
     $limitActivities = " AND `type` IN ('" . implode("','", $enabledNotifications) . "')";
     if ($filter === 'self') {
         $limitActivities .= ' AND `user` = ?';
         $parameters[] = $user;
     } else {
         if ($filter === 'by') {
             $limitActivities .= ' AND `user` <> ?';
             $parameters[] = $user;
         } else {
             if ($filter !== 'all') {
                 switch ($filter) {
                     case 'files':
                         $limitActivities .= ' AND `app` = ?';
                         $parameters[] = 'files';
                         break;
                     default:
                         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);
 }
Example #13
0
* 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();
Example #14
0
 /**
  * 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];
 }
 /**
  * @dataProvider filterUsersBySettingData
  *
  * @param array $users
  * @param string $method
  * @param string $type
  * @param array $expected
  */
 public function testFilterUsersBySetting($users, $method, $type, $expected)
 {
     $this->config->expects($this->any())->method('getUserValueForUsers')->with($this->anything(), $this->anything(), $this->anything())->willReturnMap([['activity', 'notify_stream_file_created', ['test', 'test6'], ['test6' => '1']], ['activity', 'notify_stream_file_nodefault', ['test', 'test6'], ['test6' => '1']], ['activity', 'notify_stream_type1', ['test', 'test1', 'test2', 'test3', 'test4'], ['test1' => '1', 'test2' => '0', 'test3' => '', 'test4' => '1']], ['activity', 'notify_email_file_created', ['test', 'test6'], ['test6' => '1']], ['activity', 'notify_email_shared', ['test6'], ['test6' => '1']], ['activity', 'notify_email_shared', ['test', 'test6'], ['test6' => '1']], ['activity', 'notify_email_type1', ['test', 'test1', 'test2', 'test3', 'test4', 'test5'], ['test1' => '1', 'test2' => '0', 'test3' => '', 'test4' => '3', 'test5' => '1']], ['activity', 'notify_setting_batchtime', ['test6'], ['test6' => '2700']], ['activity', 'notify_setting_batchtime', ['test', 'test6'], ['test6' => '2700']], ['activity', 'notify_setting_batchtime', ['test1', 'test4', 'test5'], ['test1' => '1', 'test4' => '4']]]);
     $this->assertEquals($expected, $this->userSettings->filterUsersBySetting($users, $method, $type));
 }
Example #16
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);
 }
Example #17
0
 /**
  * Filters the given user array by their notification setting
  *
  * @param array $users
  * @param string $method
  * @param string $type
  * @return array Returns a "username => b:true" Map for method = stream
  *               Returns a "username => i:batchtime" Map for method = email
  */
 public static function filterUsersBySetting($users, $method, $type)
 {
     if (empty($users) || !is_array($users)) {
         return array();
     }
     $preferences = new \OC\Preferences(\OC_DB::getConnection());
     $filteredUsers = array();
     $potentialUsers = $preferences->getValueForUsers('activity', 'notify_' . $method . '_' . $type, $users);
     foreach ($potentialUsers as $user => $value) {
         if ($value) {
             $filteredUsers[$user] = true;
         }
         unset($users[array_search($user, $users)]);
     }
     // Get the batch time setting from the database
     if ($method == 'email') {
         $potentialUsers = $preferences->getValueForUsers('activity', 'notify_setting_batchtime', array_keys($filteredUsers));
         foreach ($potentialUsers as $user => $value) {
             $filteredUsers[$user] = $value;
         }
     }
     if (empty($users)) {
         return $filteredUsers;
     }
     // If the setting is enabled by default,
     // we add all users that didn't set the preference yet.
     if (UserSettings::getDefaultSetting($method, $type)) {
         foreach ($users as $user) {
             if ($method == 'stream') {
                 $filteredUsers[$user] = true;
             } else {
                 $filteredUsers[$user] = self::getDefaultSetting('setting', 'batchtime');
             }
         }
     }
     return $filteredUsers;
 }
Example #18
0
 /**
  * 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);
 }
Example #19
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
  * @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);
 }