/** * @PublicPage * @NoCSRFRequired * * @return TemplateResponse */ public function show() { try { $user = $this->activityManager->getCurrentUserId(); $userLang = $this->config->getUserValue($user, 'core', 'lang'); // Overwrite user and language in the helper $l = Util::getL10N('activity', $userLang); $l->forceLanguage($userLang); $this->helper->setL10n($l); $this->helper->setUser($user); $description = (string) $l->t('Personal activity feed for %s', $user); $activities = $this->data->read($this->helper, $this->settings, 0, self::DEFAULT_PAGE_SIZE, 'all', $user); } catch (\UnexpectedValueException $e) { $l = Util::getL10N('activity'); $description = (string) $l->t('Your feed URL is invalid'); $activities = [['activity_id' => -1, 'timestamp' => time(), 'subject' => true, 'subjectformatted' => ['full' => $description]]]; } $response = new TemplateResponse('activity', 'rss', ['rssLang' => $l->getLanguageCode(), 'rssLink' => $this->urlGenerator->linkToRouteAbsolute('activity.Feed.show'), 'rssPubDate' => date('r'), 'description' => $description, 'activities' => $activities], ''); if ($this->request->getHeader('accept') !== null && stristr($this->request->getHeader('accept'), 'application/rss+xml')) { $response->addHeader('Content-Type', 'application/rss+xml'); } else { $response->addHeader('Content-Type', 'text/xml; charset=UTF-8'); } return $response; }
/** * @PublicPage * @NoCSRFRequired * * @return TemplateResponse */ public function show() { try { $user = $this->activityManager->getCurrentUserId(); $userLang = $this->config->getUserValue($user, 'core', 'lang'); // Overwrite user and language in the helper $this->l = $this->l10nFactory->get('activity', $userLang); $parser = new PlainTextParser($this->l); $this->helper->setL10n($this->l); $this->helper->setUser($user); $description = (string) $this->l->t('Personal activity feed for %s', $user); $response = $this->data->get($this->helper, $this->settings, $user, 0, self::DEFAULT_PAGE_SIZE, 'desc', 'all'); $data = $response['data']; $activities = []; foreach ($data as $activity) { $activity['subject_prepared'] = $parser->parseMessage($activity['subject_prepared']); $activity['message_prepared'] = $parser->parseMessage($activity['message_prepared']); $activities[] = $activity; } } catch (\UnexpectedValueException $e) { $this->l = $this->l10nFactory->get('activity'); $description = (string) $this->l->t('Your feed URL is invalid'); $activities = [['activity_id' => -1, 'timestamp' => time(), 'subject' => true, 'subject_prepared' => $description]]; } $response = new TemplateResponse('activity', 'rss', ['rssLang' => $this->l->getLanguageCode(), 'rssLink' => $this->urlGenerator->linkToRouteAbsolute('activity.Feed.show'), 'rssPubDate' => date('r'), 'description' => $description, 'activities' => $activities], ''); if ($this->request->getHeader('accept') !== null && stristr($this->request->getHeader('accept'), 'application/rss+xml')) { $response->addHeader('Content-Type', 'application/rss+xml'); } else { $response->addHeader('Content-Type', 'text/xml; charset=UTF-8'); } return $response; }
/** * @dataProvider groupHelperData */ public function testGroupHelper($allowGrouping, $activities, $expected) { $activityManager = $this->getMock('\\OCP\\Activity\\IManager'); $activityManager->expects($this->any())->method('getGroupParameter')->with($this->anything())->will($this->returnValue(false)); $activityLanguage = \OCP\Util::getL10N('activity'); $helper = new GroupHelper($activityManager, new DataHelper($activityManager, new ParameterHelper($activityManager, new \OC\Files\View(''), $this->getMockBuilder('OCP\\IConfig')->disableOriginalConstructor()->getMock(), $activityLanguage), $activityLanguage), $allowGrouping); foreach ($activities as $activity) { $helper->addActivity($activity); } $result = $helper->getActivities(); $clearedResult = array(); foreach ($result as $activity) { unset($activity['subjectformatted']); unset($activity['messageformatted']); unset($activity['timestamp']); $clearedResult[] = $activity; } $this->assertEquals($expected, $clearedResult); }
/** * @dataProvider groupHelperData */ public function testGroupHelper($allowGrouping, $activities, $expected) { $activityLanguage = \OCP\Util::getL10N('activity', 'en'); $activityManager = new ActivityManager($this->getMock('OCP\\IRequest'), $this->getMock('OCP\\IUserSession'), $this->getMock('OCP\\IConfig')); $activityManager->registerExtension(function () use($activityLanguage) { return new Extension($activityLanguage, $this->getMock('\\OCP\\IURLGenerator')); }); $helper = new GroupHelper($activityManager, new DataHelper($activityManager, new ParameterHelper($activityManager, $this->getMockBuilder('OCP\\IUserManager')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('\\OCP\\IURLGenerator')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('OCP\\Contacts\\IManager')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('OC\\Files\\View')->disableOriginalConstructor()->getMock(), $this->getMockBuilder('OCP\\IConfig')->disableOriginalConstructor()->getMock(), $activityLanguage, 'test'), $activityLanguage), $allowGrouping); foreach ($activities as $activity) { $helper->addActivity($activity); } $result = $helper->getActivities(); $clearedResult = array(); foreach ($result as $activity) { unset($activity['subjectformatted']); unset($activity['messageformatted']); unset($activity['timestamp']); $clearedResult[] = $activity; } $this->assertEquals($expected, $clearedResult); }
/** * Process the result and return the activities * * @param \OC_DB_StatementWrapper|int $result * @param \OCA\Activity\GroupHelper $groupHelper * @return array */ public function getActivitiesFromQueryResult($result, GroupHelper $groupHelper) { if (DB::isError($result)) { Util::writeLog('Activity', DB::getErrorMessage($result), Util::ERROR); } else { while ($row = $result->fetchRow()) { $groupHelper->addActivity($row); } } return $groupHelper->getActivities(); }
/** * 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]; }
/** * Process the result and return the activities * * @param int $count * @param int $start * @param string $limitActivities * @param array $parameters * @param \OCA\Activity\GroupHelper $groupHelper * @return array */ protected function getActivities($count, $start, $limitActivities, $parameters, GroupHelper $groupHelper) { $query = $this->connection->prepare('SELECT * ' . ' FROM `*PREFIX*activity` ' . ' WHERE `affecteduser` = ? ' . $limitActivities . ' ORDER BY `timestamp` DESC', $count, $start); $query->execute($parameters); while ($row = $query->fetch()) { $groupHelper->addActivity($row); } $query->closeCursor(); return $groupHelper->getActivities(); }