/**
  * @dataProvider getNotificationTypesData
  *
  * @param string $user
  * @param string $method
  * @param array $expected
  */
 public function testGetNotificationTypes($user, $method, $expected)
 {
     $this->config->expects($this->any())->method('getUserValue')->with($this->anything(), 'activity', $this->stringStartsWith('notify_'), $this->anything())->willReturnMap([['test1', 'activity', 'notify_stream_shared', true, true], ['test1', 'activity', 'notify_stream_file_created', true, true], ['test1', 'activity', 'notify_stream_file_changed', true, true], ['test1', 'activity', 'notify_stream_file_deleted', true, true], ['test1', 'activity', 'notify_stream_file_restored', true, true], ['noPreferences', 'activity', 'notify_email_shared', true, true]]);
     $this->assertEquals($expected, $this->userSettings->getNotificationTypes($user, $method));
 }
Ejemplo n.º 2
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);
 }
Ejemplo n.º 3
0
 /**
  * @dataProvider getNotificationTypesData
  */
 public function testGetNotificationTypes($user, $method, $expected)
 {
     $this->assertEquals($expected, UserSettings::getNotificationTypes($user, $method));
 }
Ejemplo n.º 4
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];
 }
Ejemplo n.º 5
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);
 }
Ejemplo n.º 6
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);
 }