Completed
Pull Request — master (#451)
by Joas
13:14
created

Api   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 7
c 3
b 1
f 0
lcom 0
cbo 3
dl 0
loc 64
ccs 40
cts 40
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSinceFromOffset() 0 19 2
B get() 0 34 5
1
<?php
2
3
/**
4
 * ownCloud - Activity App
5
 *
6
 * @author Joas Schilling
7
 * @copyright 2014 Joas Schilling [email protected]
8
 *
9
 * This library is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
11
 * License as published by the Free Software Foundation; either
12
 * version 3 of the License, or any later version.
13
 *
14
 * This library is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public
20
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Activity;
25
26
/**
27
 * Class Api
28
 *
29
 * @package OCA\Activity
30
 */
31
class Api
32
{
33
	const DEFAULT_LIMIT = 30;
34
35 5
	static public function get() {
36 5
		$app = new AppInfo\Application();
37
		/** @var Data $data */
38 5
		$data = $app->getContainer()->query('ActivityData');
39
40 5
		$start = isset($_GET['start']) ? (int) $_GET['start'] : 0;
41 5
		$count = isset($_GET['count']) ? (int) $_GET['count'] : self::DEFAULT_LIMIT;
42 5
		$user = $app->getContainer()->getServer()->getUserSession()->getUser()->getUID();
43
44 5
		if ($start !== 0) {
45 2
			$start = self::getSinceFromOffset($user, $start);
46 2
		}
47
48 5
		$activities = $data->get(
49 5
			$app->getContainer()->query('GroupHelper'),
50 5
			$app->getContainer()->query('UserSettings'),
51 5
			$user, $start, $count, 'desc', 'all'
52 5
		);
53 5
		$parser = new PlainTextParser(\OC::$server->getL10NFactory()->get('activity'));
54
55 5
		$entries = array();
56 5
		foreach($activities['data'] as $entry) {
57 4
			$entries[] = array(
58 4
				'id' => $entry['activity_id'],
59 4
				'subject' => $parser->parseMessage($entry['subject_prepared']),
60 4
				'message' => $parser->parseMessage($entry['message_prepared']),
61 4
				'file' => $entry['object_name'],
62 4
				'link' => $entry['link'],
63 4
				'date' => date('c', $entry['timestamp']),
64
			);
65 5
		}
66
67 5
		return new \OC_OCS_Result($entries);
68
	}
69
70
	/**
71
	 * @param string $user
72
	 * @param int $offset
73
	 * @return int
74
	 */
75 2
	protected static function getSinceFromOffset($user, $offset) {
76 2
		$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
77 2
		$query->select('activity_id')
78 2
			->from('activity')
79 2
			->where($query->expr()->eq('affecteduser', $query->createNamedParameter($user)))
80 2
			->orderBy('activity_id', 'desc')
81 2
			->setFirstResult($offset - 1)
82 2
			->setMaxResults(1);
83
84 2
		$result = $query->execute();
85 2
		$row = $result->fetch();
86 2
		$result->closeCursor();
87
88 2
		if ($row) {
89 1
			return (int) $row['activity_id'];
90
		}
91
92 1
		return 0;
93
	}
94
}
95