Blame view
sources/apps/activity/lib/data.php
11.2 KB
|
d1bafeea1
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php /** * ownCloud - Activity App * * @author Frank Karlitschek * @copyright 2013 Frank Karlitschek frank@owncloud.org * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE * License as published by the Free Software Foundation; either * version 3 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * 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. * |
|
6d9380f96
|
19 |
* You should have received a copy of the GNU Affero General Public |
|
d1bafeea1
|
20 21 22 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. * */ |
|
d1bafeea1
|
23 |
namespace OCA\Activity; |
|
6d9380f96
|
24 25 26 |
use \OCP\DB; use \OCP\User; use \OCP\Util; |
|
d1bafeea1
|
27 28 29 30 31 32 33 34 35 36 37 |
/**
* @brief Class for managing the data in the activities
*/
class Data
{
const PRIORITY_VERYLOW = 10;
const PRIORITY_LOW = 20;
const PRIORITY_MEDIUM = 30;
const PRIORITY_HIGH = 40;
const PRIORITY_VERYHIGH = 50;
|
|
6d9380f96
|
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
const TYPE_SHARED = 'shared';
const TYPE_SHARE_EXPIRED = 'share_expired';
const TYPE_SHARE_UNSHARED = 'share_unshared';
const TYPE_SHARE_CREATED = 'file_created';
const TYPE_SHARE_CHANGED = 'file_changed';
const TYPE_SHARE_DELETED = 'file_deleted';
const TYPE_SHARE_RESHARED = 'file_reshared';
const TYPE_SHARE_DOWNLOADED = 'file_downloaded';
const TYPE_SHARE_UPLOADED = 'file_uploaded';
const TYPE_STORAGE_QUOTA_90 = 'storage_quota_90';
const TYPE_STORAGE_FAILURE = 'storage_failure';
/** @var \OCP\Activity\IManager */
protected $activityManager;
public function __construct(\OCP\Activity\IManager $activityManager){
$this->activityManager = $activityManager;
}
protected $notificationTypes = array();
|
|
d1bafeea1
|
61 |
/** |
|
6d9380f96
|
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
* @param \OC_L10N $l
* @return array Array "stringID of the type" => "translated string description for the setting"
*/
public function getNotificationTypes(\OC_L10N $l) {
if (isset($this->notificationTypes[$l->getLanguageCode()]))
{
return $this->notificationTypes[$l->getLanguageCode()];
}
$notificationTypes = array(
self::TYPE_SHARED => $l->t('A file or folder has been <strong>shared</strong>'),
// self::TYPE_SHARE_UNSHARED => $l->t('Previously shared file or folder has been <strong>unshared</strong>'),
// self::TYPE_SHARE_EXPIRED => $l->t('Expiration date of shared file or folder <strong>expired</strong>'),
self::TYPE_SHARE_CREATED => $l->t('A new file or folder has been <strong>created</strong>'),
self::TYPE_SHARE_CHANGED => $l->t('A file or folder has been <strong>changed</strong>'),
self::TYPE_SHARE_DELETED => $l->t('A file or folder has been <strong>deleted</strong>'),
// self::TYPE_SHARE_RESHARED => $l->t('A file or folder has been <strong>reshared</strong>'),
// self::TYPE_SHARE_DOWNLOADED => $l->t('A file or folder shared via link has been <strong>downloaded</strong>'),
// self::TYPE_SHARE_UPLOADED => $l->t('A file has been <strong>uploaded</strong> into a folder shared via link'),
// self::TYPE_STORAGE_QUOTA_90 => $l->t('<strong>Storage usage</strong> is at 90%%'),
// self::TYPE_STORAGE_FAILURE => $l->t('An <strong>external storage</strong> has an error'),
);
// Allow other apps to add new notification types
$additionalNotificationTypes = $this->activityManager->getNotificationTypes($l->getLanguageCode());
$notificationTypes = array_merge($notificationTypes, $additionalNotificationTypes);
$this->notificationTypes[$l->getLanguageCode()] = $notificationTypes;
return $notificationTypes;
}
/**
* Send an event into the activity stream
*
|
|
d1bafeea1
|
97 98 |
* @param string $app The app where this event is associated with * @param string $subject A short description of the event |
|
6d9380f96
|
99 |
* @param array $subjectparams Array with parameters that are filled in the subject |
|
d1bafeea1
|
100 |
* @param string $message A longer description of the event |
|
6d9380f96
|
101 |
* @param array $messageparams Array with parameters that are filled in the message |
|
d1bafeea1
|
102 103 |
* @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) |
|
6d9380f96
|
104 105 106 107 |
* @param string $affecteduser If empty the current user will be used * @param string $type Type of the notification * @param int $prio Priority of the notification * @return bool |
|
d1bafeea1
|
108 |
*/ |
|
6d9380f96
|
109 |
public static function send($app, $subject, $subjectparams = array(), $message = '', $messageparams = array(), $file = '', $link = '', $affecteduser = '', $type = '', $prio = Data::PRIORITY_MEDIUM) {
|
|
d1bafeea1
|
110 |
$timestamp = time(); |
|
6d9380f96
|
111 |
$user = User::getUser(); |
|
d1bafeea1
|
112 |
|
|
6d9380f96
|
113 114 115 |
if ($affecteduser === '') {
$auser = $user;
} else {
|
|
d1bafeea1
|
116 117 118 119 |
$auser = $affecteduser; } // store in DB |
|
6d9380f96
|
120 |
$query = DB::prepare('INSERT INTO `*PREFIX*activity`(`app`, `subject`, `subjectparams`, `message`, `messageparams`, `file`, `link`, `user`, `affecteduser`, `timestamp`, `priority`, `type`)' . ' VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )');
|
|
d1bafeea1
|
121 |
$query->execute(array($app, $subject, serialize($subjectparams), $message, serialize($messageparams), $file, $link, $user, $auser, $timestamp, $prio, $type)); |
|
d1bafeea1
|
122 |
// fire a hook so that other apps like notification systems can connect |
|
6d9380f96
|
123 |
Util::emitHook('OC_Activity', 'post_event', array('app' => $app, 'subject' => $subject, 'user' => $user, 'affecteduser' => $affecteduser, 'message' => $message, 'file' => $file, 'link'=> $link, 'prio' => $prio, 'type' => $type));
|
|
d1bafeea1
|
124 125 126 127 128 |
return true; } /** |
|
6d9380f96
|
129 130 131 132 133 134 135 136 137 |
* @brief Send an event into the activity stream * * @param string $app The app where this event is associated with * @param string $subject A short description of the event * @param array $subjectParams Array of parameters that are filled in the placeholders * @param string $affectedUser Name of the user we are sending the activity to * @param string $type Type of notification * @param int $latestSendTime Activity time() + batch setting of $affecteduser * @return bool |
|
d1bafeea1
|
138 |
*/ |
|
6d9380f96
|
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
public static function storeMail($app, $subject, array $subjectParams, $affectedUser, $type, $latestSendTime) {
$timestamp = time();
// store in DB
$query = DB::prepare('INSERT INTO `*PREFIX*activity_mq` '
. ' (`amq_appid`, `amq_subject`, `amq_subjectparams`, `amq_affecteduser`, `amq_timestamp`, `amq_type`, `amq_latest_send`) '
. ' VALUES(?, ?, ?, ?, ?, ?, ?)');
$query->execute(array(
$app,
$subject,
serialize($subjectParams),
$affectedUser,
$timestamp,
$type,
$latestSendTime,
));
// fire a hook so that other apps like notification systems can connect
Util::emitHook('OC_Activity', 'post_email', array(
'app' => $app,
'subject' => $subject,
'subjectparams' => $subjectParams,
'affecteduser' => $affectedUser,
'timestamp' => $timestamp,
'type' => $type,
'latest_send' => $latestSendTime,
));
return true;
|
|
d1bafeea1
|
168 169 170 |
} /** |
|
6d9380f96
|
171 172 173 174 |
* Filter the activity types * * @param array $types * @param string $filter |
|
d1bafeea1
|
175 176 |
* @return array */ |
|
6d9380f96
|
177 178 179 180 181 182 |
public function filterNotificationTypes($types, $filter) {
switch ($filter) {
case 'shares':
return array_intersect(array(
Data::TYPE_SHARED,
), $types);
|
|
d1bafeea1
|
183 |
} |
|
d1bafeea1
|
184 |
|
|
6d9380f96
|
185 186 |
// Allow other apps to add new notification types return $this->activityManager->filterNotificationTypes($types, $filter); |
|
d1bafeea1
|
187 188 189 |
} /** |
|
6d9380f96
|
190 191 192 |
* @brief Read a list of events from the activity stream * @param GroupHelper $groupHelper Allows activities to be grouped * @param int $start The start entry |
|
d1bafeea1
|
193 |
* @param int $count The number of statements to read |
|
6d9380f96
|
194 |
* @param string $filter Filter the activities |
|
d1bafeea1
|
195 196 |
* @return array */ |
|
6d9380f96
|
197 |
public function read(GroupHelper $groupHelper, $start, $count, $filter = 'all') {
|
|
d1bafeea1
|
198 |
// get current user |
|
6d9380f96
|
199 200 201 |
$user = User::getUser(); $enabledNotifications = UserSettings::getNotificationTypes($user, 'stream'); $enabledNotifications = $this->filterNotificationTypes($enabledNotifications, $filter); |
|
d1bafeea1
|
202 |
|
|
6d9380f96
|
203 204 205 206 |
// We don't want to display any activities
if (empty($enabledNotifications)) {
return array();
}
|
|
d1bafeea1
|
207 |
|
|
6d9380f96
|
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
$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);
}
}
}
|
|
d1bafeea1
|
236 |
} |
|
d1bafeea1
|
237 |
|
|
6d9380f96
|
238 239 240 241 242 243 244 245 246 247 |
// 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); |
|
d1bafeea1
|
248 249 250 |
} /** |
|
6d9380f96
|
251 252 253 254 255 |
* Process the result and return the activities * * @param \OC_DB_StatementWrapper|int $result * @param \OCA\Activity\GroupHelper $groupHelper * @return array |
|
d1bafeea1
|
256 |
*/ |
|
6d9380f96
|
257 258 259 260 261 262 |
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);
|
|
d1bafeea1
|
263 |
} |
|
d1bafeea1
|
264 |
} |
|
6d9380f96
|
265 266 |
return $groupHelper->getActivities(); } |
|
d1bafeea1
|
267 |
|
|
6d9380f96
|
268 269 270 271 272 273 274 275 |
/**
* Get the casted page number from $_GET
* @return int
*/
public function getPageFromParam() {
if (isset($_GET['page'])) {
return (int) $_GET['page'];
}
|
|
d1bafeea1
|
276 |
|
|
6d9380f96
|
277 |
return 1; |
|
d1bafeea1
|
278 |
} |
|
6d9380f96
|
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
/**
* Get the filter from $_GET
* @return string
*/
public function getFilterFromParam() {
if (!isset($_GET['filter']))
return 'all';
$filterValue = $_GET['filter'];
switch ($filterValue) {
case 'by':
case 'self':
case 'shares':
case 'all':
case 'files':
return $filterValue;
default:
if ($this->activityManager->isFilterValid($filterValue)) {
return $filterValue;
}
return 'all';
}
}
|
|
d1bafeea1
|
302 303 |
/** |
|
6d9380f96
|
304 305 306 307 |
* Delete old events * * @param int $expireDays Minimum 1 day * @return null |
|
d1bafeea1
|
308 |
*/ |
|
6d9380f96
|
309 310 |
public function expire($expireDays = 365) {
$ttl = (60 * 60 * 24 * max(1, $expireDays));
|
|
d1bafeea1
|
311 312 |
$timelimit = time() - $ttl; |
|
6d9380f96
|
313 314 315 |
$this->deleteActivities(array( 'timestamp' => array($timelimit, '<'), )); |
|
d1bafeea1
|
316 |
} |
|
d1bafeea1
|
317 |
/** |
|
6d9380f96
|
318 319 320 321 322 323 |
* Delete activities that match certain conditions
*
* @param array $conditions Array with conditions that have to be met
* 'field' => 'value' => `field` = 'value'
* 'field' => array('value', 'operator') => `field` operator 'value'
* @return null
|
|
d1bafeea1
|
324 |
*/ |
|
6d9380f96
|
325 326 327 328 329 330 |
public function deleteActivities($conditions) {
$sqlWhere = '';
$sqlParameters = $sqlWhereList = array();
foreach ($conditions as $column => $comparison) {
$sqlWhereList[] = " `$column` " . ((is_array($comparison) && isset($comparison[1])) ? $comparison[1] : '=') . ' ? ';
$sqlParameters[] = (is_array($comparison)) ? $comparison[0] : $comparison;
|
|
d1bafeea1
|
331 |
} |
|
6d9380f96
|
332 333 334 |
if (!empty($sqlWhereList)) {
$sqlWhere = ' WHERE ' . implode(' AND ', $sqlWhereList);
}
|
|
d1bafeea1
|
335 |
|
|
6d9380f96
|
336 337 338 |
$query = DB::prepare( 'DELETE FROM `*PREFIX*activity`' . $sqlWhere); $query->execute($sqlParameters); |
|
d1bafeea1
|
339 |
} |
|
d1bafeea1
|
340 |
} |