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
|
|
|
|
54
|
5 |
|
$entries = array(); |
55
|
5 |
|
foreach($activities['data'] as $entry) { |
56
|
4 |
|
$entries[] = array( |
57
|
4 |
|
'id' => $entry['activity_id'], |
58
|
4 |
|
'subject' => self::parseMessage($entry['subject_prepared']), |
59
|
4 |
|
'message' => self::parseMessage($entry['message_prepared']), |
60
|
4 |
|
'file' => $entry['object_name'], |
61
|
4 |
|
'link' => $entry['link'], |
62
|
4 |
|
'date' => date('c', $entry['timestamp']), |
63
|
|
|
); |
64
|
5 |
|
} |
65
|
|
|
|
66
|
5 |
|
return new \OC_OCS_Result($entries); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $user |
71
|
|
|
* @param int $offset |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
2 |
|
protected static function getSinceFromOffset($user, $offset) { |
75
|
2 |
|
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
76
|
2 |
|
$query->select('activity_id') |
77
|
2 |
|
->from('activity') |
78
|
2 |
|
->where($query->expr()->eq('affecteduser', $query->createNamedParameter($user))) |
79
|
2 |
|
->orderBy('activity_id', 'desc') |
80
|
2 |
|
->setFirstResult($offset - 1) |
81
|
2 |
|
->setMaxResults(1); |
82
|
|
|
|
83
|
2 |
|
$result = $query->execute(); |
84
|
2 |
|
$row = $result->fetch(); |
85
|
2 |
|
$result->closeCursor(); |
86
|
|
|
|
87
|
2 |
|
if ($row) { |
88
|
1 |
|
return (int) $row['activity_id']; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
return 0; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Parse the parameters in the subject and message |
96
|
|
|
* |
97
|
|
|
* @param string $message |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
4 |
|
protected static function parseMessage($message) { |
101
|
4 |
|
$message = self::parseCollections($message); |
102
|
4 |
|
$message = self::parseParameters($message); |
103
|
4 |
|
return $message; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Parse collections |
108
|
|
|
* |
109
|
|
|
* @param string $message |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
4 |
|
protected static function parseCollections($message) { |
113
|
|
|
return preg_replace_callback('/<collection>(.*?)<\/collection>/', function($match) { |
114
|
|
|
$parameterList = explode('><', $match[1]); |
115
|
|
|
$parameterListLength = sizeof($parameterList); |
116
|
|
|
|
117
|
|
|
$parameters = []; |
118
|
|
View Code Duplication |
for ($i = 0; $i < $parameterListLength; $i++) { |
|
|
|
|
119
|
|
|
$parameter = $parameterList[$i]; |
120
|
|
|
if ($i > 0) { |
121
|
|
|
$parameter = '<' . $parameter; |
122
|
|
|
} |
123
|
|
|
if ($i + 1 < $parameterListLength) { |
124
|
|
|
$parameter = $parameter . '>'; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$parameters[] = self::parseParameters($parameter); |
128
|
|
|
} |
129
|
|
|
if ($parameterListLength === 1) { |
130
|
|
|
return array_pop($parameters); |
131
|
|
|
} else { |
132
|
|
|
$l = \OC::$server->getL10NFactory()->get('activity'); |
133
|
|
|
$lastParameter = array_pop($parameters); |
134
|
|
|
return $l->t('%s and %s', [ |
135
|
|
|
implode($l->t(', '), $parameters), |
136
|
|
|
$lastParameter, |
137
|
|
|
]); |
138
|
|
|
} |
139
|
4 |
|
}, $message); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Parse the parameters in the subject and message |
144
|
|
|
* |
145
|
|
|
* @param string $message |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
4 |
|
protected static function parseParameters($message) { |
149
|
4 |
|
$message = self::parseUntypedParameters($message); |
150
|
4 |
|
$message = self::parseUserParameters($message); |
151
|
4 |
|
$message = self::parseFederatedCloudIDParameters($message); |
152
|
4 |
|
$message = self::parseFileParameters($message); |
153
|
4 |
|
return $message; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Display the parameter value |
158
|
|
|
* |
159
|
|
|
* @param string $message |
160
|
|
|
* @return string |
161
|
|
|
*/ |
162
|
4 |
|
protected static function parseUntypedParameters($message) { |
163
|
|
|
return preg_replace_callback('/<parameter>(.*?)<\/parameter>/', function($match) { |
164
|
|
|
return $match[1]; |
165
|
4 |
|
}, $message); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Display the users display name |
170
|
|
|
* |
171
|
|
|
* @param string $message |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
4 |
|
protected static function parseUserParameters($message) { |
175
|
|
|
return preg_replace_callback('/<user\ display\-name=\"(.*?)\">(.*?)<\/user>/', function($match) { |
176
|
3 |
|
return $match[1]; |
177
|
4 |
|
}, $message); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Display the full cloud id |
182
|
|
|
* |
183
|
|
|
* @param string $message |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
4 |
|
protected static function parseFederatedCloudIDParameters($message) { |
187
|
|
|
return preg_replace_callback('/<federated-cloud-id\ display\-name=\"(.*?)\"\ user=\"(.*?)\"\ server=\"(.*?)\">(.*?)<\/federated-cloud-id>/', function($match) { |
188
|
|
|
return $match[1]; |
189
|
4 |
|
}, $message); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Display the path for files |
194
|
|
|
* |
195
|
|
|
* @param string $message |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
protected static function parseFileParameters($message) { |
199
|
4 |
|
return preg_replace_callback('/<file\ link=\"(.*?)\"\ id=\"(.*?)\">(.*?)<\/file>/', function($match) { |
200
|
4 |
|
return $match[3]; |
201
|
4 |
|
}, $message); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.